diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-22 23:48:23 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-22 23:48:23 +0000 |
commit | f35d5e8112599c578f0e5891028bd43553f4e0bd (patch) | |
tree | 4f16a2cd945f4477ca215daaba465907f3232cf6 | |
parent | 601e664f992c48f8a4009cc2d8843075a5e29192 (diff) | |
download | chromium_src-f35d5e8112599c578f0e5891028bd43553f4e0bd.zip chromium_src-f35d5e8112599c578f0e5891028bd43553f4e0bd.tar.gz chromium_src-f35d5e8112599c578f0e5891028bd43553f4e0bd.tar.bz2 |
Canvas refactoring part 1.
- Introduce a new Canvas2 interface. This will become the way that everyone talks to Canvas. It is populated primarily with the cross-platform methods from Canvas.
- Make Canvas implement this interface.
- Hook it up to the Windows RootView.
Review URL: http://codereview.chromium.org/2866010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50543 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | gfx/canvas.cc | 48 | ||||
-rw-r--r-- | gfx/canvas.h | 229 | ||||
-rw-r--r-- | gfx/canvas_2.h | 201 | ||||
-rw-r--r-- | gfx/gfx.gyp | 2 | ||||
-rw-r--r-- | views/widget/root_view_win.cc | 10 |
5 files changed, 318 insertions, 172 deletions
diff --git a/gfx/canvas.cc b/gfx/canvas.cc index 0099e4c..28bc231 100644 --- a/gfx/canvas.cc +++ b/gfx/canvas.cc @@ -12,6 +12,10 @@ #include "gfx/rect.h" #include "third_party/skia/include/core/SkShader.h" +#if defined(OS_WIN) +#include "gfx/canvas_paint.h" +#endif + namespace gfx { bool Canvas::GetClipRect(gfx::Rect* r) { @@ -267,6 +271,10 @@ SkBitmap Canvas::ExtractBitmap() const { return result; } +Canvas* Canvas::AsCanvas() { + return this; +} + // static int Canvas::DefaultCanvasTextAlignment() { if (!base::i18n::IsRTL()) @@ -274,4 +282,44 @@ int Canvas::DefaultCanvasTextAlignment() { return gfx::Canvas::TEXT_ALIGN_RIGHT; } +//////////////////////////////////////////////////////////////////////////////// +// Canvas2, public: + +Canvas2* Canvas2::CreateCanvas() { + return new Canvas; +} + +Canvas2* Canvas2::CreateCanvas(int width, int height, bool is_opaque) { + return new Canvas(width, height, is_opaque); +} + +#if defined(OS_WIN) +// TODO(beng): move to canvas_win.cc, etc. +class CanvasPaintWin : public CanvasPaint, public CanvasPaint2 { + public: + CanvasPaintWin(gfx::NativeView view) : CanvasPaint(view) {} + + // Overridden from CanvasPaint2: + virtual bool IsValid() const { + return isEmpty(); + } + + virtual gfx::Rect GetInvalidRect() const { + return gfx::Rect(paintStruct().rcPaint); + } + + virtual Canvas2* AsCanvas2() { + return this; + } +}; +#endif + +CanvasPaint2* CanvasPaint2::CreateCanvasPaint(gfx::NativeView view) { +#if defined(OS_WIN) + return new CanvasPaintWin(view); +#else + return NULL; +#endif +} + } // namespace gfx diff --git a/gfx/canvas.h b/gfx/canvas.h index ef2b716..53661db 100644 --- a/gfx/canvas.h +++ b/gfx/canvas.h @@ -5,25 +5,16 @@ #ifndef GFX_CANVAS_H_ #define GFX_CANVAS_H_ -#if defined(OS_WIN) -#include <windows.h> -#endif - -#include <string> - #include "base/basictypes.h" +#include "gfx/canvas_2.h" #include "skia/ext/platform_canvas.h" #if defined(OS_POSIX) && !defined(OS_MACOSX) -typedef struct _cairo cairo_t; typedef struct _GdkPixbuf GdkPixbuf; #endif namespace gfx { -class Font; -class Rect; - // Canvas is a SkCanvas subclass that provides a number of methods for common // operations used throughout an application built using base/gfx and app/gfx. // @@ -38,46 +29,9 @@ class Rect; // source and destination colors are combined. Unless otherwise specified, // the variant that does not take a SkXfermode::Mode uses a transfer mode // of kSrcOver_Mode. -class Canvas : public skia::PlatformCanvas { +class Canvas : public skia::PlatformCanvas, + public Canvas2 { public: - // Specifies the alignment for text rendered with the DrawStringInt method. - enum { - TEXT_ALIGN_LEFT = 1, - TEXT_ALIGN_CENTER = 2, - TEXT_ALIGN_RIGHT = 4, - TEXT_VALIGN_TOP = 8, - TEXT_VALIGN_MIDDLE = 16, - TEXT_VALIGN_BOTTOM = 32, - - // Specifies the text consists of multiple lines. - MULTI_LINE = 64, - - // By default DrawStringInt does not process the prefix ('&') character - // specially. That is, the string "&foo" is rendered as "&foo". When - // rendering text from a resource that uses the prefix character for - // mnemonics, the prefix should be processed and can be rendered as an - // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). - SHOW_PREFIX = 128, - HIDE_PREFIX = 256, - - // Prevent ellipsizing - NO_ELLIPSIS = 512, - - // Specifies if words can be split by new lines. - // This only works with MULTI_LINE. - CHARACTER_BREAK = 1024, - - // Instructs DrawStringInt() to render the text using RTL directionality. - // In most cases, passing this flag is not necessary because information - // about the text directionality is going to be embedded within the string - // in the form of special Unicode characters. However, we don't insert - // directionality characters into strings if the locale is LTR because some - // platforms (for example, an English Windows XP with no RTL fonts - // installed) don't support these characters. Thus, this flag should be - // used to render text using RTL directionality when the locale is LTR. - FORCE_RTL_DIRECTIONALITY = 2048, - }; - // Creates an empty Canvas. Callers must use initialize before using the // canvas. Canvas(); @@ -86,124 +40,6 @@ class Canvas : public skia::PlatformCanvas { virtual ~Canvas(); - // Retrieves the clip rectangle and sets it in the specified rectangle if any. - // Returns true if the clip rect is non-empty. - bool GetClipRect(gfx::Rect* clip_rect); - - // Wrapper function that takes integer arguments. - // Returns true if the clip is non-empty. - // See clipRect for specifics. - bool ClipRectInt(int x, int y, int w, int h); - - // Test whether the provided rectangle intersects the current clip rect. - bool IntersectsClipRectInt(int x, int y, int w, int h); - - // Wrapper function that takes integer arguments. - // See translate() for specifics. - void TranslateInt(int x, int y); - - // Wrapper function that takes integer arguments. - // See scale() for specifics. - void ScaleInt(int x, int y); - - // Fills the given rectangle with the given paint's parameters. - void FillRectInt(int x, int y, int w, int h, const SkPaint& paint); - - // Fills the specified region with the specified color using a transfer - // mode of SkXfermode::kSrcOver_Mode. - void FillRectInt(const SkColor& color, int x, int y, int w, int h); - - // Draws a single pixel rect in the specified region with the specified - // color, using a transfer mode of SkXfermode::kSrcOver_Mode. - // - // NOTE: if you need a single pixel line, use DraLineInt. - void DrawRectInt(const SkColor& color, int x, int y, int w, int h); - - // Draws a single pixel rect in the specified region with the specified - // color and transfer mode. - // - // NOTE: if you need a single pixel line, use DraLineInt. - void DrawRectInt(const SkColor& color, int x, int y, int w, int h, - SkXfermode::Mode mode); - - // Draws a single pixel line with the specified color. - void DrawLineInt(const SkColor& color, int x1, int y1, int x2, int y2); - - // Draws a bitmap with the origin at the specified location. The upper left - // corner of the bitmap is rendered at the specified location. - void DrawBitmapInt(const SkBitmap& bitmap, int x, int y); - - // Draws a bitmap with the origin at the specified location, using the - // specified paint. The upper left corner of the bitmap is rendered at the - // specified location. - void DrawBitmapInt(const SkBitmap& bitmap, int x, int y, - const SkPaint& paint); - - // Draws a portion of a bitmap in the specified location. The src parameters - // correspond to the region of the bitmap to draw in the region defined - // by the dest coordinates. - // - // If the width or height of the source differs from that of the destination, - // the bitmap will be scaled. When scaling down, it is highly recommended - // that you call buildMipMap(false) on your bitmap to ensure that it has - // a mipmap, which will result in much higher-quality output. Set |filter| - // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm - // is used for resampling. - // - // An optional custom SkPaint can be provided. - void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, int src_w, - int src_h, int dest_x, int dest_y, int dest_w, int dest_h, - bool filter); - void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, int src_w, - int src_h, int dest_x, int dest_y, int dest_w, int dest_h, - bool filter, const SkPaint& paint); - - // Draws text with the specified color, font and location. The text is - // aligned to the left, vertically centered, clipped to the region. If the - // text is too big, it is truncated and '...' is added to the end. - void DrawStringInt(const std::wstring& text, const gfx::Font& font, - const SkColor& color, int x, int y, int w, int h); - void DrawStringInt(const std::wstring& text, const gfx::Font& font, - const SkColor& color, const gfx::Rect& display_rect); - - // Draws text with the specified color, font and location. The last argument - // specifies flags for how the text should be rendered. It can be one of - // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. - void DrawStringInt(const std::wstring& text, const gfx::Font& font, - const SkColor& color, int x, int y, int w, int h, - int flags); - -#ifdef OS_WIN // Only implemented on Windows for now. - // Draws text with a 1-pixel halo around it of the given color. It allows - // ClearType to be drawn to an otherwise transparenct bitmap for drag images. - // Drag images have only 1-bit of transparency, so we don't do any fancy - // blurring. - void DrawStringWithHalo(const std::wstring& text, const gfx::Font& font, - const SkColor& text_color, const SkColor& halo_color, - int x, int y, int w, int h, int flags); -#endif - - // Draws a dotted gray rectangle used for focus purposes. - void DrawFocusRect(int x, int y, int width, int height); - - // Tiles the image in the specified region. - void TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h); - void TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, - int dest_x, int dest_y, int w, int h); - - // Extracts a bitmap from the contents of this canvas. - SkBitmap ExtractBitmap() const; - -#if defined(OS_POSIX) && !defined(OS_MACOSX) - // Applies current matrix on the canvas to the cairo context. This should be - // invoked anytime you plan on drawing directly to the cairo context. Be - // sure and set the matrix back to the identity when done. - void ApplySkiaMatrixToCairoContext(cairo_t* cr); - - // Draw the pixbuf in its natural size at (x, y). - void DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y); -#endif - // Compute the size required to draw some text with the provided font. // Attempts to fit the text with the provided width and height. Increases // height and then width as needed to make the text fit. This method @@ -220,6 +56,65 @@ class Canvas : public skia::PlatformCanvas { // gfx::Canvas::TEXT_ALIGN_RIGHT. static int DefaultCanvasTextAlignment(); +#if defined(OS_POSIX) && !defined(OS_MACOSX) + // Draw the pixbuf in its natural size at (x, y). + void DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y); +#endif + +#ifdef OS_WIN // Only implemented on Windows for now. + // Draws text with a 1-pixel halo around it of the given color. It allows + // ClearType to be drawn to an otherwise transparenct bitmap for drag images. + // Drag images have only 1-bit of transparency, so we don't do any fancy + // blurring. + void DrawStringWithHalo(const std::wstring& text, + const gfx::Font& font, + const SkColor& text_color, + const SkColor& halo_color, + int x, int y, int w, int h, int flags); +#endif + + // Overridden from Canvas2: + virtual bool GetClipRect(gfx::Rect* clip_rect); + virtual bool ClipRectInt(int x, int y, int w, int h); + virtual bool IntersectsClipRectInt(int x, int y, int w, int h); + virtual void TranslateInt(int x, int y); + virtual void ScaleInt(int x, int y); + virtual void FillRectInt(int x, int y, int w, int h, + const SkPaint& paint); + virtual void FillRectInt(const SkColor& color, int x, int y, int w, + int h); + virtual void DrawRectInt(const SkColor& color, int x, int y, int w, + int h); + virtual void DrawRectInt(const SkColor& color, int x, int y, int w, int h, + SkXfermode::Mode mode); + virtual void DrawLineInt(const SkColor& color, int x1, int y1, int x2, + int y2); + virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y); + virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y, + const SkPaint& paint); + virtual void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, + int src_w, int src_h, int dest_x, int dest_y, + int dest_w, int dest_h, bool filter); + virtual void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, + int src_w, int src_h, int dest_x, int dest_y, + int dest_w, int dest_h, bool filter, + const SkPaint& paint); + virtual void DrawStringInt(const std::wstring& text, const gfx::Font& font, + const SkColor& color, int x, int y, int w, + int h); + virtual void DrawStringInt(const std::wstring& text, const gfx::Font& font, + const SkColor& color, + const gfx::Rect& display_rect); + virtual void DrawStringInt(const std::wstring& text, const gfx::Font& font, + const SkColor& color, int x, int y, int w, int h, + int flags); + virtual void DrawFocusRect(int x, int y, int width, int height); + virtual void TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h); + virtual void TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, + int dest_x, int dest_y, int w, int h); + virtual SkBitmap ExtractBitmap() const; + virtual Canvas* AsCanvas(); + private: #if defined(OS_WIN) // Draws text with the specified color, font and location. The text is diff --git a/gfx/canvas_2.h b/gfx/canvas_2.h new file mode 100644 index 0000000..bf7e244 --- /dev/null +++ b/gfx/canvas_2.h @@ -0,0 +1,201 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef GFX_CANVAS_2_H_ +#define GFX_CANVAS_2_H_ + +#include <string> + +#include "gfx/native_widget_types.h" +// TODO(beng): remove this include when we no longer depend on SkTypes. +#include "skia/ext/platform_canvas.h" + +namespace gfx { + +class Canvas; +class Font; +class Rect; + +// TODO(beng): documentation. +class Canvas2 { + public: + // Specifies the alignment for text rendered with the DrawStringInt method. + enum { + TEXT_ALIGN_LEFT = 1, + TEXT_ALIGN_CENTER = 2, + TEXT_ALIGN_RIGHT = 4, + TEXT_VALIGN_TOP = 8, + TEXT_VALIGN_MIDDLE = 16, + TEXT_VALIGN_BOTTOM = 32, + + // Specifies the text consists of multiple lines. + MULTI_LINE = 64, + + // By default DrawStringInt does not process the prefix ('&') character + // specially. That is, the string "&foo" is rendered as "&foo". When + // rendering text from a resource that uses the prefix character for + // mnemonics, the prefix should be processed and can be rendered as an + // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). + SHOW_PREFIX = 128, + HIDE_PREFIX = 256, + + // Prevent ellipsizing + NO_ELLIPSIS = 512, + + // Specifies if words can be split by new lines. + // This only works with MULTI_LINE. + CHARACTER_BREAK = 1024, + + // Instructs DrawStringInt() to render the text using RTL directionality. + // In most cases, passing this flag is not necessary because information + // about the text directionality is going to be embedded within the string + // in the form of special Unicode characters. However, we don't insert + // directionality characters into strings if the locale is LTR because some + // platforms (for example, an English Windows XP with no RTL fonts + // installed) don't support these characters. Thus, this flag should be + // used to render text using RTL directionality when the locale is LTR. + FORCE_RTL_DIRECTIONALITY = 2048, + }; + + virtual ~Canvas2() {} + + // Creates an empty canvas. Must be initialized before it can be used. + static Canvas2* CreateCanvas(); + + // Creates a canvas with the specified size. + static Canvas2* CreateCanvas(int width, int height, bool is_opaque); + + // Retrieves the clip rectangle and sets it in the specified rectangle if any. + // Returns true if the clip rect is non-empty. + virtual bool GetClipRect(gfx::Rect* clip_rect) = 0; + + // Wrapper function that takes integer arguments. + // Returns true if the clip is non-empty. + // See clipRect for specifics. + virtual bool ClipRectInt(int x, int y, int w, int h) = 0; + + // Test whether the provided rectangle intersects the current clip rect. + virtual bool IntersectsClipRectInt(int x, int y, int w, int h) = 0; + + // Wrapper function that takes integer arguments. + // See translate() for specifics. + virtual void TranslateInt(int x, int y) = 0; + + // Wrapper function that takes integer arguments. + // See scale() for specifics. + virtual void ScaleInt(int x, int y) = 0; + + // Fills the given rectangle with the given paint's parameters. + virtual void FillRectInt(int x, int y, int w, int h, + const SkPaint& paint) = 0; + + // Fills the specified region with the specified color using a transfer + // mode of SkXfermode::kSrcOver_Mode. + virtual void FillRectInt(const SkColor& color, int x, int y, int w, + int h) = 0; + + // Draws a single pixel rect in the specified region with the specified + // color, using a transfer mode of SkXfermode::kSrcOver_Mode. + // + // NOTE: if you need a single pixel line, use DraLineInt. + virtual void DrawRectInt(const SkColor& color, int x, int y, int w, + int h) = 0; + + // Draws a single pixel rect in the specified region with the specified + // color and transfer mode. + // + // NOTE: if you need a single pixel line, use DraLineInt. + virtual void DrawRectInt(const SkColor& color, int x, int y, int w, int h, + SkXfermode::Mode mode) = 0; + + // Draws a single pixel line with the specified color. + virtual void DrawLineInt(const SkColor& color, int x1, int y1, int x2, + int y2) = 0; + + // Draws a bitmap with the origin at the specified location. The upper left + // corner of the bitmap is rendered at the specified location. + virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) = 0; + + // Draws a bitmap with the origin at the specified location, using the + // specified paint. The upper left corner of the bitmap is rendered at the + // specified location. + virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y, + const SkPaint& paint) = 0; + + // Draws a portion of a bitmap in the specified location. The src parameters + // correspond to the region of the bitmap to draw in the region defined + // by the dest coordinates. + // + // If the width or height of the source differs from that of the destination, + // the bitmap will be scaled. When scaling down, it is highly recommended + // that you call buildMipMap(false) on your bitmap to ensure that it has + // a mipmap, which will result in much higher-quality output. Set |filter| + // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm + // is used for resampling. + // + // An optional custom SkPaint can be provided. + virtual void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, + int src_w, int src_h, int dest_x, int dest_y, + int dest_w, int dest_h, bool filter) = 0; + virtual void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, + int src_w, int src_h, int dest_x, int dest_y, + int dest_w, int dest_h, bool filter, + const SkPaint& paint) = 0; + + // Draws text with the specified color, font and location. The text is + // aligned to the left, vertically centered, clipped to the region. If the + // text is too big, it is truncated and '...' is added to the end. + virtual void DrawStringInt(const std::wstring& text, const gfx::Font& font, + const SkColor& color, int x, int y, int w, + int h) = 0; + virtual void DrawStringInt(const std::wstring& text, const gfx::Font& font, + const SkColor& color, + const gfx::Rect& display_rect) = 0; + + // Draws text with the specified color, font and location. The last argument + // specifies flags for how the text should be rendered. It can be one of + // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. + virtual void DrawStringInt(const std::wstring& text, const gfx::Font& font, + const SkColor& color, int x, int y, int w, int h, + int flags) = 0; + + // Draws a dotted gray rectangle used for focus purposes. + virtual void DrawFocusRect(int x, int y, int width, int height) = 0; + + // Tiles the image in the specified region. + virtual void TileImageInt(const SkBitmap& bitmap, int x, int y, int w, + int h) = 0; + virtual void TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, + int dest_x, int dest_y, int w, int h) = 0; + + // Extracts a bitmap from the contents of this canvas. + virtual SkBitmap ExtractBitmap() const = 0; + + // TODO(beng): remove this once we don't need to use any skia-specific methods + // through this interface. + // A quick and dirty way to obtain the underlying SkCanvas. + virtual Canvas* AsCanvas() { return NULL; } +}; + +class CanvasPaint2 { + public: + virtual ~CanvasPaint2() {} + + // Creates a canvas that paints to |view| when it is destroyed. The canvas is + // sized to the client area of |view|. + static CanvasPaint2* CreateCanvasPaint(gfx::NativeView view); + + // Returns true if the canvas has an invalid rect that needs to be repainted. + virtual bool IsValid() const = 0; + + // Returns the rectangle that is invalid. + virtual gfx::Rect GetInvalidRect() const = 0; + + // Returns the underlying Canvas2. + virtual Canvas2* AsCanvas2() = 0; +}; + +} // namespace gfx; + +#endif // GFX_CANVAS_2_H_ diff --git a/gfx/gfx.gyp b/gfx/gfx.gyp index 4b7d612..54d6833 100644 --- a/gfx/gfx.gyp +++ b/gfx/gfx.gyp @@ -64,8 +64,10 @@ 'blit.h', 'canvas.cc', 'canvas.h', + 'canvas_2.h', 'canvas_linux.cc', 'canvas_mac.mm', + 'canvas_paint.h', 'canvas_win.cc', 'codec/jpeg_codec.cc', 'codec/jpeg_codec.h', diff --git a/views/widget/root_view_win.cc b/views/widget/root_view_win.cc index 9535fd8..49717e1 100644 --- a/views/widget/root_view_win.cc +++ b/views/widget/root_view_win.cc @@ -24,12 +24,12 @@ void RootView::OnPaint(HWND hwnd) { RECT win_version = original_dirty_region.ToRECT(); InvalidateRect(hwnd, &win_version, FALSE); } - gfx::CanvasPaint canvas(hwnd); - if (!canvas.isEmpty()) { - const PAINTSTRUCT& ps = canvas.paintStruct(); - SchedulePaint(gfx::Rect(ps.rcPaint), false); + scoped_ptr<gfx::CanvasPaint2> canvas( + gfx::CanvasPaint2::CreateCanvasPaint(hwnd)); + if (!canvas->IsValid()) { + SchedulePaint(canvas->GetInvalidRect(), false); if (NeedsPainting(false)) - ProcessPaint(&canvas); + ProcessPaint(canvas->AsCanvas2()->AsCanvas()); } } |