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_H_ | 5 #ifndef UI_GFX_CANVAS_H_ |
6 #define UI_GFX_CANVAS_H_ | 6 #define UI_GFX_CANVAS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 #include "skia/ext/platform_canvas.h" | 14 #include "skia/ext/platform_canvas.h" |
| 15 #include "ui/gfx/image/image_skia.h" |
15 #include "ui/gfx/native_widget_types.h" | 16 #include "ui/gfx/native_widget_types.h" |
16 | 17 |
17 class SkBitmap; | 18 class SkBitmap; |
18 | 19 |
19 namespace ui { | 20 namespace ui { |
20 class Transform; | 21 class Transform; |
21 } | 22 } |
22 | 23 |
23 namespace gfx { | 24 namespace gfx { |
24 | 25 |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 int radius, | 234 int radius, |
234 const SkPaint& paint); | 235 const SkPaint& paint); |
235 | 236 |
236 // Draws the given rectangle with rounded corners of |radius| using the | 237 // Draws the given rectangle with rounded corners of |radius| using the |
237 // given |paint| parameters. | 238 // given |paint| parameters. |
238 void DrawRoundRect(const gfx::Rect& rect, int radius, const SkPaint& paint); | 239 void DrawRoundRect(const gfx::Rect& rect, int radius, const SkPaint& paint); |
239 | 240 |
240 // Draws the given path using the given |paint| parameters. | 241 // Draws the given path using the given |paint| parameters. |
241 void DrawPath(const SkPath& path, const SkPaint& paint); | 242 void DrawPath(const SkPath& path, const SkPaint& paint); |
242 | 243 |
243 // Draws a bitmap with the origin at the specified location. The upper left | 244 // Draws an image with the origin at the specified location. The upper left |
244 // corner of the bitmap is rendered at the specified location. | 245 // corner of the bitmap is rendered at the specified location. |
245 void DrawBitmapInt(const SkBitmap& bitmap, int x, int y); | 246 // Parameters are specified relative to current canvas scale not pixels. |
| 247 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
| 248 void DrawBitmapInt(const gfx::ImageSkia&, int x, int y); |
246 | 249 |
247 // Draws a bitmap with the origin at the specified location, using the | 250 // Draws an image with the origin at the specified location, using the |
248 // specified paint. The upper left corner of the bitmap is rendered at the | 251 // specified paint. The upper left corner of the bitmap is rendered at the |
249 // specified location. | 252 // specified location. |
250 void DrawBitmapInt(const SkBitmap& bitmap, | 253 // Parameters are specified relative to current canvas scale not pixels. |
| 254 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
| 255 void DrawBitmapInt(const gfx::ImageSkia& image, |
251 int x, int y, | 256 int x, int y, |
252 const SkPaint& paint); | 257 const SkPaint& paint); |
253 | 258 |
254 // Draws a portion of a bitmap in the specified location. The src parameters | 259 // Draws a portion of an image in the specified location. The src parameters |
255 // correspond to the region of the bitmap to draw in the region defined | 260 // correspond to the region of the bitmap to draw in the region defined |
256 // by the dest coordinates. | 261 // by the dest coordinates. |
257 // | 262 // |
258 // If the width or height of the source differs from that of the destination, | 263 // If the width or height of the source differs from that of the destination, |
259 // the bitmap will be scaled. When scaling down, it is highly recommended | 264 // the image will be scaled. When scaling down, a mipmap will be generated. |
260 // that you call buildMipMap(false) on your bitmap to ensure that it has | 265 // Set |filter| to use filtering for images, otherwise the nearest-neighbor |
261 // a mipmap, which will result in much higher-quality output. Set |filter| | 266 // algorithm is used for resampling. |
262 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm | |
263 // is used for resampling. | |
264 // | 267 // |
265 // An optional custom SkPaint can be provided. | 268 // An optional custom SkPaint can be provided. |
266 void DrawBitmapInt(const SkBitmap& bitmap, | 269 // Parameters are specified relative to current canvas scale not pixels. |
| 270 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
| 271 void DrawBitmapInt(const gfx::ImageSkia& image, |
267 int src_x, int src_y, int src_w, int src_h, | 272 int src_x, int src_y, int src_w, int src_h, |
268 int dest_x, int dest_y, int dest_w, int dest_h, | 273 int dest_x, int dest_y, int dest_w, int dest_h, |
269 bool filter); | 274 bool filter); |
270 void DrawBitmapInt(const SkBitmap& bitmap, | 275 void DrawBitmapInt(const gfx::ImageSkia& image, |
271 int src_x, int src_y, int src_w, int src_h, | 276 int src_x, int src_y, int src_w, int src_h, |
272 int dest_x, int dest_y, int dest_w, int dest_h, | 277 int dest_x, int dest_y, int dest_w, int dest_h, |
273 bool filter, | 278 bool filter, |
274 const SkPaint& paint); | 279 const SkPaint& paint); |
275 | 280 |
276 // TODO(pkotwicz): make this function private once gfx::ImageSkia stops | |
277 // calling this method. | |
278 void DrawBitmapFloat(const SkBitmap& bitmap, | |
279 float src_x, float src_y, float src_w, float src_h, | |
280 float dest_x, float dest_y, float dest_w, float dest_h, | |
281 bool filter, | |
282 const SkPaint& paint); | |
283 | |
284 // Draws text with the specified color, font and location. The text is | 281 // Draws text with the specified color, font and location. The text is |
285 // aligned to the left, vertically centered, clipped to the region. If the | 282 // aligned to the left, vertically centered, clipped to the region. If the |
286 // text is too big, it is truncated and '...' is added to the end. | 283 // text is too big, it is truncated and '...' is added to the end. |
287 void DrawStringInt(const string16& text, | 284 void DrawStringInt(const string16& text, |
288 const gfx::Font& font, | 285 const gfx::Font& font, |
289 SkColor color, | 286 SkColor color, |
290 int x, int y, int w, int h); | 287 int x, int y, int w, int h); |
291 void DrawStringInt(const string16& text, | 288 void DrawStringInt(const string16& text, |
292 const gfx::Font& font, | 289 const gfx::Font& font, |
293 SkColor color, | 290 SkColor color, |
(...skipping 14 matching lines...) Expand all Loading... |
308 const gfx::Font& font, | 305 const gfx::Font& font, |
309 SkColor color, | 306 SkColor color, |
310 const gfx::Rect& text_bounds, | 307 const gfx::Rect& text_bounds, |
311 int flags, | 308 int flags, |
312 const std::vector<ShadowValue>& shadows); | 309 const std::vector<ShadowValue>& shadows); |
313 | 310 |
314 // Draws a dotted gray rectangle used for focus purposes. | 311 // Draws a dotted gray rectangle used for focus purposes. |
315 void DrawFocusRect(const gfx::Rect& rect); | 312 void DrawFocusRect(const gfx::Rect& rect); |
316 | 313 |
317 // Tiles the image in the specified region. | 314 // Tiles the image in the specified region. |
318 void TileImageInt(const SkBitmap& bitmap, | 315 // Parameters are specified relative to current canvas scale not pixels. |
| 316 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
| 317 void TileImageInt(const gfx::ImageSkia& image, |
319 int x, int y, int w, int h); | 318 int x, int y, int w, int h); |
320 void TileImageInt(const SkBitmap& bitmap, | 319 void TileImageInt(const gfx::ImageSkia& image, |
321 int src_x, int src_y, | 320 int src_x, int src_y, |
322 int dest_x, int dest_y, int w, int h); | 321 int dest_x, int dest_y, int w, int h); |
323 | 322 |
324 // Returns a native drawing context for platform specific drawing routines to | 323 // Returns a native drawing context for platform specific drawing routines to |
325 // use. Must be balanced by a call to EndPlatformPaint(). | 324 // use. Must be balanced by a call to EndPlatformPaint(). |
326 NativeDrawingContext BeginPlatformPaint(); | 325 NativeDrawingContext BeginPlatformPaint(); |
327 | 326 |
328 // Signifies the end of platform drawing using the native drawing context | 327 // Signifies the end of platform drawing using the native drawing context |
329 // returned by BeginPlatformPaint(). | 328 // returned by BeginPlatformPaint(). |
330 void EndPlatformPaint(); | 329 void EndPlatformPaint(); |
(...skipping 16 matching lines...) Expand all Loading... |
347 #endif | 346 #endif |
348 | 347 |
349 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } | 348 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } |
350 SkCanvas* sk_canvas() const { return canvas_; } | 349 SkCanvas* sk_canvas() const { return canvas_; } |
351 | 350 |
352 private: | 351 private: |
353 // Test whether the provided rectangle intersects the current clip rect. | 352 // Test whether the provided rectangle intersects the current clip rect. |
354 bool IntersectsClipRectInt(int x, int y, int w, int h); | 353 bool IntersectsClipRectInt(int x, int y, int w, int h); |
355 bool IntersectsClipRect(const gfx::Rect& rect); | 354 bool IntersectsClipRect(const gfx::Rect& rect); |
356 | 355 |
| 356 // Returns the bitmap whose density best matches the current canvas scale. |
| 357 // Returns a null bitmap if |image| contains no bitmaps. |
| 358 // |bitmap_scale_factor| is set to the scale factor of the returned bitmap. |
| 359 // Builds mip map for returned bitmap if necessary. |
| 360 // |
| 361 // An optional additional user defined scale can be provided. |
| 362 const SkBitmap& GetBitmapToPaint(const gfx::ImageSkia& image, |
| 363 float* bitmap_scale_factor) const; |
| 364 const SkBitmap& GetBitmapToPaint(const gfx::ImageSkia& image, |
| 365 float user_defined_scale_factor_x, |
| 366 float user_defined_scale_factor_y, |
| 367 float* bitmap_scale_factor) const; |
| 368 |
357 #if defined(OS_WIN) | 369 #if defined(OS_WIN) |
358 // Draws text with the specified color, font and location. The text is | 370 // Draws text with the specified color, font and location. The text is |
359 // aligned to the left, vertically centered, clipped to the region. If the | 371 // aligned to the left, vertically centered, clipped to the region. If the |
360 // text is too big, it is truncated and '...' is added to the end. | 372 // text is too big, it is truncated and '...' is added to the end. |
361 void DrawStringInt(const string16& text, | 373 void DrawStringInt(const string16& text, |
362 HFONT font, | 374 HFONT font, |
363 SkColor color, | 375 SkColor color, |
364 const gfx::Rect& text_bounds, | 376 const gfx::Rect& text_bounds, |
365 int flags); | 377 int flags); |
366 #endif | 378 #endif |
367 | 379 |
368 scoped_ptr<skia::PlatformCanvas> owned_canvas_; | 380 scoped_ptr<skia::PlatformCanvas> owned_canvas_; |
369 SkCanvas* canvas_; | 381 SkCanvas* canvas_; |
370 | 382 |
371 DISALLOW_COPY_AND_ASSIGN(Canvas); | 383 DISALLOW_COPY_AND_ASSIGN(Canvas); |
372 }; | 384 }; |
373 | 385 |
374 } // namespace gfx | 386 } // namespace gfx |
375 | 387 |
376 #endif // UI_GFX_CANVAS_H_ | 388 #endif // UI_GFX_CANVAS_H_ |
OLD | NEW |