OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_CANVAS_SKIA_H_ | 5 #ifndef UI_GFX_CANVAS_SKIA_H_ |
6 #define UI_GFX_CANVAS_SKIA_H_ | 6 #define UI_GFX_CANVAS_SKIA_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
12 #include "base/string16.h" | 11 #include "base/string16.h" |
13 #include "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
14 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/native_widget_types.h" |
15 | 14 |
16 class SkBitmap; | 15 class SkBitmap; |
17 | 16 |
| 17 namespace ui { |
| 18 class Transform; |
| 19 } |
| 20 |
18 namespace gfx { | 21 namespace gfx { |
19 | 22 |
| 23 class Brush; |
| 24 class Rect; |
| 25 class Font; |
| 26 class Point; |
| 27 class Size; |
| 28 |
20 // CanvasSkia is a SkCanvas wrapper that provides a number of methods for | 29 // CanvasSkia is a SkCanvas wrapper that provides a number of methods for |
21 // common operations used throughout an application built using base/gfx. | 30 // common operations used throughout an application built using ui/gfx. |
22 // | 31 // |
23 // All methods that take integer arguments (as is used throughout views) | 32 // All methods that take integer arguments (as is used throughout views) |
24 // end with Int. If you need to use methods provided by SkCanvas, you'll | 33 // end with Int. If you need to use methods provided by SkCanvas, you'll |
25 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, | 34 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, |
26 // or if converting from a scalar to an integer |SkScalarRound()|. | 35 // or if converting from a scalar to an integer |SkScalarRound()|. |
27 // | 36 // |
28 // A handful of methods in this class are overloaded providing an additional | 37 // A handful of methods in this class are overloaded providing an additional |
29 // argument of type SkXfermode::Mode. SkXfermode::Mode specifies how the | 38 // argument of type SkXfermode::Mode. SkXfermode::Mode specifies how the |
30 // source and destination colors are combined. Unless otherwise specified, | 39 // source and destination colors are combined. Unless otherwise specified, |
31 // the variant that does not take a SkXfermode::Mode uses a transfer mode | 40 // the variant that does not take a SkXfermode::Mode uses a transfer mode |
32 // of kSrcOver_Mode. | 41 // of kSrcOver_Mode. |
33 class UI_EXPORT CanvasSkia : public Canvas { | 42 class UI_EXPORT CanvasSkia { |
34 public: | 43 public: |
35 enum TruncateFadeMode { | 44 enum TruncateFadeMode { |
36 TruncateFadeTail, | 45 TruncateFadeTail, |
37 TruncateFadeHead, | 46 TruncateFadeHead, |
38 TruncateFadeHeadAndTail, | 47 TruncateFadeHeadAndTail, |
39 }; | 48 }; |
40 | 49 |
41 // Creates an empty canvas. | 50 // Creates an empty canvas. |
42 CanvasSkia(); | 51 CanvasSkia(); |
43 | 52 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 void DrawStringWithHalo(const string16& text, | 96 void DrawStringWithHalo(const string16& text, |
88 const gfx::Font& font, | 97 const gfx::Font& font, |
89 const SkColor& text_color, | 98 const SkColor& text_color, |
90 const SkColor& halo_color, | 99 const SkColor& halo_color, |
91 int x, int y, int w, int h, | 100 int x, int y, int w, int h, |
92 int flags); | 101 int flags); |
93 | 102 |
94 // Extracts a bitmap from the contents of this canvas. | 103 // Extracts a bitmap from the contents of this canvas. |
95 SkBitmap ExtractBitmap() const; | 104 SkBitmap ExtractBitmap() const; |
96 | 105 |
97 // Overridden from Canvas: | 106 // Saves a copy of the drawing state onto a stack, operating on this copy |
98 virtual void Save() OVERRIDE; | 107 // until a balanced call to Restore() is made. |
99 virtual void SaveLayerAlpha(uint8 alpha) OVERRIDE; | 108 void Save(); |
100 virtual void SaveLayerAlpha(uint8 alpha, | 109 |
101 const gfx::Rect& layer_bounds) OVERRIDE; | 110 // As with Save(), except draws to a layer that is blended with the canvas |
102 virtual void Restore() OVERRIDE; | 111 // at the specified alpha once Restore() is called. |
103 virtual bool ClipRect(const gfx::Rect& rect) OVERRIDE; | 112 // |layer_bounds| are the bounds of the layer relative to the current |
104 virtual void Translate(const gfx::Point& point) OVERRIDE; | 113 // transform. |
105 virtual void Scale(int x_scale, int y_scale) OVERRIDE; | 114 void SaveLayerAlpha(uint8 alpha); |
106 virtual void FillRect(const gfx::Rect& rect, const SkColor& color) OVERRIDE; | 115 void SaveLayerAlpha(uint8 alpha, |
107 virtual void FillRect(const gfx::Rect& rect, | 116 const gfx::Rect& layer_bounds); |
| 117 |
| 118 // Restores the drawing state after a call to Save*(). It is an error to |
| 119 // call Restore() more times than Save*(). |
| 120 void Restore() ; |
| 121 |
| 122 // Returns true if the clip is non-empty. |
| 123 bool ClipRect(const gfx::Rect& rect) ; |
| 124 |
| 125 void Translate(const gfx::Point& point) ; |
| 126 |
| 127 void Scale(int x_scale, int y_scale) ; |
| 128 |
| 129 // Fills |rect| with |color| using a transfer mode of |
| 130 // SkXfermode::kSrcOver_Mode. |
| 131 void FillRect(const gfx::Rect& rect, const SkColor& color) ; |
| 132 |
| 133 // Fills |rect| with the specified |color| and |mode|. |
| 134 void FillRect(const gfx::Rect& rect, |
108 const SkColor& color, | 135 const SkColor& color, |
109 SkXfermode::Mode mode) OVERRIDE; | 136 SkXfermode::Mode mode) ; |
110 virtual void FillRect(const gfx::Rect& rect, | 137 |
111 const gfx::Brush* brush) OVERRIDE; | 138 // Fills |rect| with the specified |brush|. |
112 virtual void DrawRect(const gfx::Rect& rect, const SkColor& color) OVERRIDE; | 139 void FillRect(const gfx::Rect& rect, |
113 virtual void DrawRect(const gfx::Rect& rect, | 140 const gfx::Brush* brush) ; |
| 141 |
| 142 // Draws a single pixel rect in the specified region with the specified |
| 143 // color, using a transfer mode of SkXfermode::kSrcOver_Mode. |
| 144 // |
| 145 // NOTE: if you need a single pixel line, use DrawLine. |
| 146 void DrawRect(const gfx::Rect& rect, const SkColor& color) ; |
| 147 |
| 148 // Draws a single pixel rect in the specified region with the specified |
| 149 // color and transfer mode. |
| 150 // |
| 151 // NOTE: if you need a single pixel line, use DrawLine. |
| 152 void DrawRect(const gfx::Rect& rect, |
114 const SkColor& color, | 153 const SkColor& color, |
115 SkXfermode::Mode mode) OVERRIDE; | 154 SkXfermode::Mode mode) ; |
116 virtual void DrawRect(const gfx::Rect& rect, const SkPaint& paint) OVERRIDE; | 155 |
117 virtual void DrawLine(const gfx::Point& p1, | 156 // Draws the given rectangle with the given paint's parameters. |
| 157 void DrawRect(const gfx::Rect& rect, const SkPaint& paint) ; |
| 158 |
| 159 // Draws a single pixel line with the specified color. |
| 160 void DrawLine(const gfx::Point& p1, |
118 const gfx::Point& p2, | 161 const gfx::Point& p2, |
119 const SkColor& color) OVERRIDE; | 162 const SkColor& color) ; |
120 virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) OVERRIDE; | 163 |
121 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 164 // Draws a bitmap with the origin at the specified location. The upper left |
| 165 // corner of the bitmap is rendered at the specified location. |
| 166 void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) ; |
| 167 |
| 168 // Draws a bitmap with the origin at the specified location, using the |
| 169 // specified paint. The upper left corner of the bitmap is rendered at the |
| 170 // specified location. |
| 171 void DrawBitmapInt(const SkBitmap& bitmap, |
122 int x, int y, | 172 int x, int y, |
123 const SkPaint& paint) OVERRIDE; | 173 const SkPaint& paint) ; |
124 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 174 |
| 175 // Draws a portion of a bitmap in the specified location. The src parameters |
| 176 // correspond to the region of the bitmap to draw in the region defined |
| 177 // by the dest coordinates. |
| 178 // |
| 179 // If the width or height of the source differs from that of the destination, |
| 180 // the bitmap will be scaled. When scaling down, it is highly recommended |
| 181 // that you call buildMipMap(false) on your bitmap to ensure that it has |
| 182 // a mipmap, which will result in much higher-quality output. Set |filter| |
| 183 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm |
| 184 // is used for resampling. |
| 185 // |
| 186 // An optional custom SkPaint can be provided. |
| 187 void DrawBitmapInt(const SkBitmap& bitmap, |
125 int src_x, int src_y, int src_w, int src_h, | 188 int src_x, int src_y, int src_w, int src_h, |
126 int dest_x, int dest_y, int dest_w, int dest_h, | 189 int dest_x, int dest_y, int dest_w, int dest_h, |
127 bool filter) OVERRIDE; | 190 bool filter) ; |
128 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 191 void DrawBitmapInt(const SkBitmap& bitmap, |
129 int src_x, int src_y, int src_w, int src_h, | 192 int src_x, int src_y, int src_w, int src_h, |
130 int dest_x, int dest_y, int dest_w, int dest_h, | 193 int dest_x, int dest_y, int dest_w, int dest_h, |
131 bool filter, | 194 bool filter, |
132 const SkPaint& paint) OVERRIDE; | 195 const SkPaint& paint) ; |
133 virtual void DrawStringInt(const string16& text, | 196 |
| 197 // Draws text with the specified color, font and location. The text is |
| 198 // aligned to the left, vertically centered, clipped to the region. If the |
| 199 // text is too big, it is truncated and '...' is added to the end. |
| 200 void DrawStringInt(const string16& text, |
134 const gfx::Font& font, | 201 const gfx::Font& font, |
135 const SkColor& color, | 202 const SkColor& color, |
136 int x, int y, int w, int h) OVERRIDE; | 203 int x, int y, int w, int h) ; |
137 virtual void DrawStringInt(const string16& text, | 204 void DrawStringInt(const string16& text, |
138 const gfx::Font& font, | 205 const gfx::Font& font, |
139 const SkColor& color, | 206 const SkColor& color, |
140 const gfx::Rect& display_rect) OVERRIDE; | 207 const gfx::Rect& display_rect) ; |
141 virtual void DrawStringInt(const string16& text, | 208 |
| 209 // Draws text with the specified color, font and location. The last argument |
| 210 // specifies flags for how the text should be rendered. It can be one of |
| 211 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. |
| 212 void DrawStringInt(const string16& text, |
142 const gfx::Font& font, | 213 const gfx::Font& font, |
143 const SkColor& color, | 214 const SkColor& color, |
144 int x, int y, int w, int h, | 215 int x, int y, int w, int h, |
145 int flags) OVERRIDE; | 216 int flags) ; |
| 217 |
| 218 // Draws a dotted gray rectangle used for focus purposes. |
| 219 void DrawFocusRect(const gfx::Rect& rect) ; |
| 220 |
| 221 // Tiles the image in the specified region. |
| 222 void TileImageInt(const SkBitmap& bitmap, |
| 223 int x, int y, int w, int h) ; |
| 224 void TileImageInt(const SkBitmap& bitmap, |
| 225 int src_x, int src_y, |
| 226 int dest_x, int dest_y, int w, int h) ; |
| 227 |
| 228 // Returns a native drawing context for platform specific drawing routines to |
| 229 // use. Must be balanced by a call to EndPlatformPaint(). |
| 230 NativeDrawingContext BeginPlatformPaint() ; |
| 231 |
| 232 // Signifies the end of platform drawing using the native drawing context |
| 233 // returned by BeginPlatformPaint(). |
| 234 void EndPlatformPaint() ; |
| 235 |
| 236 // Apply transformation on the canvas. |
| 237 void Transform(const ui::Transform& transform) ; |
| 238 |
| 239 CanvasSkia* AsCanvasSkia() ; |
| 240 const CanvasSkia* AsCanvasSkia() const ; |
| 241 SkCanvas* GetSkCanvas() ; |
| 242 const SkCanvas* GetSkCanvas() const ; |
| 243 |
146 #if defined(OS_WIN) | 244 #if defined(OS_WIN) |
147 // Draws the given string with the beginning and/or the end using a fade | 245 // Draws the given string with the beginning and/or the end using a fade |
148 // gradient. When truncating the head | 246 // gradient. When truncating the head |
149 // |desired_characters_to_truncate_from_head| specifies the maximum number of | 247 // |desired_characters_to_truncate_from_head| specifies the maximum number of |
150 // characters that can be truncated. | 248 // characters that can be truncated. |
151 virtual void DrawFadeTruncatingString( | 249 void DrawFadeTruncatingString( |
152 const string16& text, | 250 const string16& text, |
153 TruncateFadeMode truncate_mode, | 251 TruncateFadeMode truncate_mode, |
154 size_t desired_characters_to_truncate_from_head, | 252 size_t desired_characters_to_truncate_from_head, |
155 const gfx::Font& font, | 253 const gfx::Font& font, |
156 const SkColor& color, | 254 const SkColor& color, |
157 const gfx::Rect& display_rect); | 255 const gfx::Rect& display_rect); |
158 #endif | 256 #endif |
159 virtual void DrawFocusRect(const gfx::Rect& rect) OVERRIDE; | |
160 virtual void TileImageInt(const SkBitmap& bitmap, | |
161 int x, int y, int w, int h) OVERRIDE; | |
162 virtual void TileImageInt(const SkBitmap& bitmap, | |
163 int src_x, int src_y, | |
164 int dest_x, int dest_y, int w, int h) OVERRIDE; | |
165 virtual gfx::NativeDrawingContext BeginPlatformPaint() OVERRIDE; | |
166 virtual void EndPlatformPaint() OVERRIDE; | |
167 virtual void Transform(const ui::Transform& transform) OVERRIDE; | |
168 virtual CanvasSkia* AsCanvasSkia() OVERRIDE; | |
169 virtual const CanvasSkia* AsCanvasSkia() const OVERRIDE; | |
170 virtual SkCanvas* GetSkCanvas() OVERRIDE; | |
171 virtual const SkCanvas* GetSkCanvas() const OVERRIDE; | |
172 | 257 |
173 SkCanvas* sk_canvas() const { return canvas_; } | 258 SkCanvas* sk_canvas() const { return canvas_; } |
174 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } | 259 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } |
175 | 260 |
176 private: | 261 private: |
177 // Test whether the provided rectangle intersects the current clip rect. | 262 // Test whether the provided rectangle intersects the current clip rect. |
178 bool IntersectsClipRectInt(int x, int y, int w, int h); | 263 bool IntersectsClipRectInt(int x, int y, int w, int h); |
179 | 264 |
180 #if defined(OS_WIN) | 265 #if defined(OS_WIN) |
181 // Draws text with the specified color, font and location. The text is | 266 // Draws text with the specified color, font and location. The text is |
182 // aligned to the left, vertically centered, clipped to the region. If the | 267 // aligned to the left, vertically centered, clipped to the region. If the |
183 // text is too big, it is truncated and '...' is added to the end. | 268 // text is too big, it is truncated and '...' is added to the end. |
184 void DrawStringInt(const string16& text, | 269 void DrawStringInt(const string16& text, |
185 HFONT font, | 270 HFONT font, |
186 const SkColor& color, | 271 const SkColor& color, |
187 int x, int y, int w, int h, | 272 int x, int y, int w, int h, |
188 int flags); | 273 int flags); |
189 #endif | 274 #endif |
190 | 275 |
191 scoped_ptr<skia::PlatformCanvas> owned_canvas_; | 276 scoped_ptr<skia::PlatformCanvas> owned_canvas_; |
192 SkCanvas* canvas_; | 277 SkCanvas* canvas_; |
193 | 278 |
194 DISALLOW_COPY_AND_ASSIGN(CanvasSkia); | 279 DISALLOW_COPY_AND_ASSIGN(CanvasSkia); |
195 }; | 280 }; |
196 | 281 |
197 } // namespace gfx | 282 } // namespace gfx |
198 | 283 |
199 #endif // UI_GFX_CANVAS_SKIA_H_ | 284 #endif // UI_GFX_CANVAS_SKIA_H_ |
OLD | NEW |