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 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "skia/ext/platform_canvas.h" | 15 #include "cc/paint/paint_canvas.h" |
16 #include "third_party/skia/include/core/SkCanvas.h" | 16 #include "cc/paint/paint_flags.h" |
17 #include "third_party/skia/include/core/SkSurface.h" | 17 #include "cc/paint/paint_surface.h" |
18 #include "ui/gfx/image/image_skia.h" | 18 #include "ui/gfx/image/image_skia.h" |
19 #include "ui/gfx/native_widget_types.h" | 19 #include "ui/gfx/native_widget_types.h" |
20 #include "ui/gfx/text_constants.h" | 20 #include "ui/gfx/text_constants.h" |
| 21 |
21 namespace gfx { | 22 namespace gfx { |
22 | 23 |
23 class Rect; | 24 class Rect; |
24 class RectF; | 25 class RectF; |
25 class FontList; | 26 class FontList; |
26 class Point; | 27 class Point; |
27 class PointF; | 28 class PointF; |
28 class Size; | 29 class Size; |
29 class Transform; | 30 class Transform; |
30 class Vector2d; | 31 class Vector2d; |
31 | 32 |
32 // Canvas is a SkCanvas wrapper that provides a number of methods for | 33 // Canvas is a PaintCanvas wrapper that provides a number of methods for |
33 // common operations used throughout an application built using ui/gfx. | 34 // common operations used throughout an application built using ui/gfx. |
34 // | 35 // |
35 // All methods that take integer arguments (as is used throughout views) | 36 // All methods that take integer arguments (as is used throughout views) |
36 // end with Int. If you need to use methods provided by SkCanvas, you'll | 37 // end with Int. If you need to use methods provided by PaintCanvas, you'll |
37 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, | 38 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, |
38 // or if converting from a scalar to an integer |SkScalarRound()|. | 39 // or if converting from a scalar to an integer |SkScalarRound()|. |
39 // | 40 // |
40 // A handful of methods in this class are overloaded providing an additional | 41 // A handful of methods in this class are overloaded providing an additional |
41 // argument of type SkBlendMode. SkBlendMode specifies how the | 42 // argument of type SkBlendMode. SkBlendMode specifies how the |
42 // source and destination colors are combined. Unless otherwise specified, | 43 // source and destination colors are combined. Unless otherwise specified, |
43 // the variant that does not take a SkBlendMode uses a transfer mode | 44 // the variant that does not take a SkBlendMode uses a transfer mode |
44 // of kSrcOver_Mode. | 45 // of kSrcOver_Mode. |
45 class GFX_EXPORT Canvas { | 46 class GFX_EXPORT Canvas { |
46 public: | 47 public: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // Creates canvas with provided DIP |size| and |image_scale|. | 85 // Creates canvas with provided DIP |size| and |image_scale|. |
85 // If this canvas is not opaque, it's explicitly cleared to transparent before | 86 // If this canvas is not opaque, it's explicitly cleared to transparent before |
86 // being returned. | 87 // being returned. |
87 Canvas(const Size& size, float image_scale, bool is_opaque); | 88 Canvas(const Size& size, float image_scale, bool is_opaque); |
88 | 89 |
89 // Creates a Canvas backed by an |sk_canvas| with |image_scale_|. | 90 // Creates a Canvas backed by an |sk_canvas| with |image_scale_|. |
90 // |sk_canvas| is assumed to be already scaled based on |image_scale| | 91 // |sk_canvas| is assumed to be already scaled based on |image_scale| |
91 // so no additional scaling is applied. | 92 // so no additional scaling is applied. |
92 // Note: the caller must ensure that sk_canvas outlives this object, or until | 93 // Note: the caller must ensure that sk_canvas outlives this object, or until |
93 // RecreateBackingCanvas is called. | 94 // RecreateBackingCanvas is called. |
94 Canvas(SkCanvas* sk_canvas, float image_scale); | 95 Canvas(cc::PaintCanvas* sk_canvas, float image_scale); |
95 | 96 |
96 virtual ~Canvas(); | 97 virtual ~Canvas(); |
97 | 98 |
98 // Recreates the backing platform canvas with DIP |size| and |image_scale_|. | 99 // Recreates the backing platform canvas with DIP |size| and |image_scale_|. |
99 // If the canvas is not opaque, it is explicitly cleared. | 100 // If the canvas is not opaque, it is explicitly cleared. |
100 // This method is public so that canvas_skia_paint can recreate the platform | 101 // This method is public so that canvas_skia_paint can recreate the platform |
101 // canvas after having initialized the canvas. | 102 // canvas after having initialized the canvas. |
102 // TODO(pkotwicz): Push the image_scale into skia::PlatformCanvas such that | 103 // TODO(pkotwicz): Push the image_scale into skia::PlatformCanvas such that |
103 // this method can be private. | 104 // this method can be private. |
104 void RecreateBackingCanvas(const Size& size, | 105 void RecreateBackingCanvas(const Size& size, |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 254 |
254 // Draws a single pixel rect in the specified region with the specified | 255 // Draws a single pixel rect in the specified region with the specified |
255 // color and transfer mode. | 256 // color and transfer mode. |
256 // | 257 // |
257 // NOTE: if you need a single pixel line, use DrawLine. | 258 // NOTE: if you need a single pixel line, use DrawLine. |
258 void DrawRect(const RectF& rect, SkColor color, SkBlendMode mode); | 259 void DrawRect(const RectF& rect, SkColor color, SkBlendMode mode); |
259 | 260 |
260 // Draws the given rectangle with the given |paint| parameters. | 261 // Draws the given rectangle with the given |paint| parameters. |
261 // DEPRECATED in favor of the RectF version below. | 262 // DEPRECATED in favor of the RectF version below. |
262 // TODO(funkysidd): Remove this (http://crbug.com/553726) | 263 // TODO(funkysidd): Remove this (http://crbug.com/553726) |
263 void DrawRect(const Rect& rect, const SkPaint& paint); | 264 void DrawRect(const Rect& rect, const cc::PaintFlags& paint); |
264 | 265 |
265 // Draws the given rectangle with the given |paint| parameters. | 266 // Draws the given rectangle with the given |paint| parameters. |
266 void DrawRect(const RectF& rect, const SkPaint& paint); | 267 void DrawRect(const RectF& rect, const cc::PaintFlags& paint); |
267 | 268 |
268 // Draw the given point with the given |paint| parameters. | 269 // Draw the given point with the given |paint| parameters. |
269 // DEPRECATED in favor of the RectF version below. | 270 // DEPRECATED in favor of the RectF version below. |
270 // TODO(funkysidd): Remove this (http://crbug.com/553726) | 271 // TODO(funkysidd): Remove this (http://crbug.com/553726) |
271 void DrawPoint(const Point& p, const SkPaint& paint); | 272 void DrawPoint(const Point& p, const cc::PaintFlags& paint); |
272 | 273 |
273 // Draw the given point with the given |paint| parameters. | 274 // Draw the given point with the given |paint| parameters. |
274 void DrawPoint(const PointF& p, const SkPaint& paint); | 275 void DrawPoint(const PointF& p, const cc::PaintFlags& paint); |
275 | 276 |
276 // Draws a single pixel line with the specified color. | 277 // Draws a single pixel line with the specified color. |
277 // DEPRECATED in favor of the RectF version below. | 278 // DEPRECATED in favor of the RectF version below. |
278 // TODO(funkysidd): Remove this (http://crbug.com/553726) | 279 // TODO(funkysidd): Remove this (http://crbug.com/553726) |
279 void DrawLine(const Point& p1, const Point& p2, SkColor color); | 280 void DrawLine(const Point& p1, const Point& p2, SkColor color); |
280 | 281 |
281 // Draws a single pixel line with the specified color. | 282 // Draws a single pixel line with the specified color. |
282 void DrawLine(const PointF& p1, const PointF& p2, SkColor color); | 283 void DrawLine(const PointF& p1, const PointF& p2, SkColor color); |
283 | 284 |
284 // Draws a line with the given |paint| parameters. | 285 // Draws a line with the given |paint| parameters. |
285 // DEPRECATED in favor of the RectF version below. | 286 // DEPRECATED in favor of the RectF version below. |
286 // TODO(funkysidd): Remove this (http://crbug.com/553726) | 287 // TODO(funkysidd): Remove this (http://crbug.com/553726) |
287 void DrawLine(const Point& p1, const Point& p2, const SkPaint& paint); | 288 void DrawLine(const Point& p1, const Point& p2, const cc::PaintFlags& paint); |
288 | 289 |
289 // Draws a line with the given |paint| parameters. | 290 // Draws a line with the given |paint| parameters. |
290 void DrawLine(const PointF& p1, const PointF& p2, const SkPaint& paint); | 291 void DrawLine(const PointF& p1, |
| 292 const PointF& p2, |
| 293 const cc::PaintFlags& paint); |
291 | 294 |
292 // Draws a circle with the given |paint| parameters. | 295 // Draws a circle with the given |paint| parameters. |
293 // DEPRECATED in favor of the RectF version below. | 296 // DEPRECATED in favor of the RectF version below. |
294 // TODO(funkysidd): Remove this (http://crbug.com/553726) | 297 // TODO(funkysidd): Remove this (http://crbug.com/553726) |
295 void DrawCircle(const Point& center_point, | 298 void DrawCircle(const Point& center_point, |
296 int radius, | 299 int radius, |
297 const SkPaint& paint); | 300 const cc::PaintFlags& paint); |
298 | 301 |
299 // Draws a circle with the given |paint| parameters. | 302 // Draws a circle with the given |paint| parameters. |
300 void DrawCircle(const PointF& center_point, | 303 void DrawCircle(const PointF& center_point, |
301 float radius, | 304 float radius, |
302 const SkPaint& paint); | 305 const cc::PaintFlags& paint); |
303 | 306 |
304 // Draws the given rectangle with rounded corners of |radius| using the | 307 // Draws the given rectangle with rounded corners of |radius| using the |
305 // given |paint| parameters. DEPRECATED in favor of the RectF version below. | 308 // given |paint| parameters. DEPRECATED in favor of the RectF version below. |
306 // TODO(mgiuca): Remove this (http://crbug.com/553726). | 309 // TODO(mgiuca): Remove this (http://crbug.com/553726). |
307 void DrawRoundRect(const Rect& rect, int radius, const SkPaint& paint); | 310 void DrawRoundRect(const Rect& rect, int radius, const cc::PaintFlags& paint); |
308 | 311 |
309 // Draws the given rectangle with rounded corners of |radius| using the | 312 // Draws the given rectangle with rounded corners of |radius| using the |
310 // given |paint| parameters. | 313 // given |paint| parameters. |
311 void DrawRoundRect(const RectF& rect, float radius, const SkPaint& paint); | 314 void DrawRoundRect(const RectF& rect, |
| 315 float radius, |
| 316 const cc::PaintFlags& paint); |
312 | 317 |
313 // Draws the given path using the given |paint| parameters. | 318 // Draws the given path using the given |paint| parameters. |
314 void DrawPath(const SkPath& path, const SkPaint& paint); | 319 void DrawPath(const SkPath& path, const cc::PaintFlags& paint); |
315 | 320 |
316 // Draws an image with the origin at the specified location. The upper left | 321 // Draws an image with the origin at the specified location. The upper left |
317 // corner of the bitmap is rendered at the specified location. | 322 // corner of the bitmap is rendered at the specified location. |
318 // Parameters are specified relative to current canvas scale not in pixels. | 323 // Parameters are specified relative to current canvas scale not in pixels. |
319 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1. | 324 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1. |
320 void DrawImageInt(const ImageSkia&, int x, int y); | 325 void DrawImageInt(const ImageSkia&, int x, int y); |
321 | 326 |
322 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and | 327 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and |
323 // calls paint.setAlpha(alpha). | 328 // calls paint.setAlpha(alpha). |
324 void DrawImageInt(const ImageSkia&, int x, int y, uint8_t alpha); | 329 void DrawImageInt(const ImageSkia&, int x, int y, uint8_t alpha); |
325 | 330 |
326 // Draws an image with the origin at the specified location, using the | 331 // Draws an image with the origin at the specified location, using the |
327 // specified paint. The upper left corner of the bitmap is rendered at the | 332 // specified paint. The upper left corner of the bitmap is rendered at the |
328 // specified location. | 333 // specified location. |
329 // Parameters are specified relative to current canvas scale not in pixels. | 334 // Parameters are specified relative to current canvas scale not in pixels. |
330 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. | 335 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
331 void DrawImageInt(const ImageSkia& image, | 336 void DrawImageInt(const ImageSkia& image, |
332 int x, | 337 int x, |
333 int y, | 338 int y, |
334 const SkPaint& paint); | 339 const cc::PaintFlags& paint); |
335 | 340 |
336 // Draws a portion of an image in the specified location. The src parameters | 341 // Draws a portion of an image in the specified location. The src parameters |
337 // correspond to the region of the bitmap to draw in the region defined | 342 // correspond to the region of the bitmap to draw in the region defined |
338 // by the dest coordinates. | 343 // by the dest coordinates. |
339 // | 344 // |
340 // If the width or height of the source differs from that of the destination, | 345 // If the width or height of the source differs from that of the destination, |
341 // the image will be scaled. When scaling down, a mipmap will be generated. | 346 // the image will be scaled. When scaling down, a mipmap will be generated. |
342 // Set |filter| to use filtering for images, otherwise the nearest-neighbor | 347 // Set |filter| to use filtering for images, otherwise the nearest-neighbor |
343 // algorithm is used for resampling. | 348 // algorithm is used for resampling. |
344 // | 349 // |
345 // An optional custom SkPaint can be provided. | 350 // An optional custom cc::PaintFlags can be provided. |
346 // Parameters are specified relative to current canvas scale not in pixels. | 351 // Parameters are specified relative to current canvas scale not in pixels. |
347 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. | 352 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
348 void DrawImageInt(const ImageSkia& image, | 353 void DrawImageInt(const ImageSkia& image, |
349 int src_x, | 354 int src_x, |
350 int src_y, | 355 int src_y, |
351 int src_w, | 356 int src_w, |
352 int src_h, | 357 int src_h, |
353 int dest_x, | 358 int dest_x, |
354 int dest_y, | 359 int dest_y, |
355 int dest_w, | 360 int dest_w, |
356 int dest_h, | 361 int dest_h, |
357 bool filter); | 362 bool filter); |
358 void DrawImageInt(const ImageSkia& image, | 363 void DrawImageInt(const ImageSkia& image, |
359 int src_x, | 364 int src_x, |
360 int src_y, | 365 int src_y, |
361 int src_w, | 366 int src_w, |
362 int src_h, | 367 int src_h, |
363 int dest_x, | 368 int dest_x, |
364 int dest_y, | 369 int dest_y, |
365 int dest_w, | 370 int dest_w, |
366 int dest_h, | 371 int dest_h, |
367 bool filter, | 372 bool filter, |
368 const SkPaint& paint); | 373 const cc::PaintFlags& paint); |
369 | 374 |
370 // Same as the DrawImageInt functions above. Difference being this does not | 375 // Same as the DrawImageInt functions above. Difference being this does not |
371 // do any scaling, i.e. it does not scale the output by the device scale | 376 // do any scaling, i.e. it does not scale the output by the device scale |
372 // factor (the internal image_scale_). It takes an ImageSkiaRep instead of | 377 // factor (the internal image_scale_). It takes an ImageSkiaRep instead of |
373 // an ImageSkia as the caller chooses the exact scale/pixel representation to | 378 // an ImageSkia as the caller chooses the exact scale/pixel representation to |
374 // use, which will not be scaled while drawing it into the canvas. | 379 // use, which will not be scaled while drawing it into the canvas. |
375 void DrawImageIntInPixel(const ImageSkiaRep& image_rep, | 380 void DrawImageIntInPixel(const ImageSkiaRep& image_rep, |
376 int dest_x, | 381 int dest_x, |
377 int dest_y, | 382 int dest_y, |
378 int dest_w, | 383 int dest_w, |
379 int dest_h, | 384 int dest_h, |
380 bool filter, | 385 bool filter, |
381 const SkPaint& paint); | 386 const cc::PaintFlags& paint); |
382 | 387 |
383 // Draws an |image| with the top left corner at |x| and |y|, clipped to | 388 // Draws an |image| with the top left corner at |x| and |y|, clipped to |
384 // |path|. | 389 // |path|. |
385 // Parameters are specified relative to current canvas scale not in pixels. | 390 // Parameters are specified relative to current canvas scale not in pixels. |
386 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1. | 391 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1. |
387 void DrawImageInPath(const ImageSkia& image, | 392 void DrawImageInPath(const ImageSkia& image, |
388 int x, | 393 int x, |
389 int y, | 394 int y, |
390 const SkPath& path, | 395 const SkPath& path, |
391 const SkPaint& paint); | 396 const cc::PaintFlags& paint); |
392 | 397 |
393 // Draws text with the specified color, fonts and location. The text is | 398 // Draws text with the specified color, fonts and location. The text is |
394 // aligned to the left, vertically centered, clipped to the region. If the | 399 // aligned to the left, vertically centered, clipped to the region. If the |
395 // text is too big, it is truncated and '...' is added to the end. | 400 // text is too big, it is truncated and '...' is added to the end. |
396 void DrawStringRect(const base::string16& text, | 401 void DrawStringRect(const base::string16& text, |
397 const FontList& font_list, | 402 const FontList& font_list, |
398 SkColor color, | 403 SkColor color, |
399 const Rect& display_rect); | 404 const Rect& display_rect); |
400 | 405 |
401 // Draws text with the specified color, fonts and location. The last argument | 406 // Draws text with the specified color, fonts and location. The last argument |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 float tile_scale_x, | 445 float tile_scale_x, |
441 float tile_scale_y, | 446 float tile_scale_y, |
442 int dest_x, | 447 int dest_x, |
443 int dest_y, | 448 int dest_y, |
444 int w, | 449 int w, |
445 int h); | 450 int h); |
446 | 451 |
447 // Helper for TileImageInt(). Initializes |paint| for tiling |image| with the | 452 // Helper for TileImageInt(). Initializes |paint| for tiling |image| with the |
448 // given parameters. Returns false if the provided image does not have a | 453 // given parameters. Returns false if the provided image does not have a |
449 // representation for the current scale. | 454 // representation for the current scale. |
450 bool InitSkPaintForTiling(const ImageSkia& image, | 455 bool InitPaintFlagsForTiling(const ImageSkia& image, |
451 int src_x, | 456 int src_x, |
452 int src_y, | 457 int src_y, |
453 float tile_scale_x, | 458 float tile_scale_x, |
454 float tile_scale_y, | 459 float tile_scale_y, |
455 int dest_x, | 460 int dest_x, |
456 int dest_y, | 461 int dest_y, |
457 SkPaint* paint); | 462 cc::PaintFlags* paint); |
458 | 463 |
459 // Apply transformation on the canvas. | 464 // Apply transformation on the canvas. |
460 void Transform(const Transform& transform); | 465 void Transform(const Transform& transform); |
461 | 466 |
462 // Draws the given string with a fade gradient at the end. | 467 // Draws the given string with a fade gradient at the end. |
463 void DrawFadedString(const base::string16& text, | 468 void DrawFadedString(const base::string16& text, |
464 const FontList& font_list, | 469 const FontList& font_list, |
465 SkColor color, | 470 SkColor color, |
466 const Rect& display_rect, | 471 const Rect& display_rect, |
467 int flags); | 472 int flags); |
468 | 473 |
469 SkCanvas* sk_canvas() { return canvas_; } | 474 // TODO(enne): rename sk_canvas members and interface. |
| 475 cc::PaintCanvas* sk_canvas() { return canvas_; } |
470 float image_scale() const { return image_scale_; } | 476 float image_scale() const { return image_scale_; } |
471 | 477 |
472 private: | 478 private: |
473 // Tests whether the provided rectangle intersects the current clip rect. | 479 // Tests whether the provided rectangle intersects the current clip rect. |
474 bool IntersectsClipRect(const SkRect& rect); | 480 bool IntersectsClipRect(const SkRect& rect); |
475 | 481 |
476 // Helper for the DrawImageInt functions declared above. The | 482 // Helper for the DrawImageInt functions declared above. The |
477 // |remove_image_scale| parameter indicates if the scale of the |image_rep| | 483 // |remove_image_scale| parameter indicates if the scale of the |image_rep| |
478 // should be removed when drawing the image, to avoid double-scaling it. | 484 // should be removed when drawing the image, to avoid double-scaling it. |
479 void DrawImageIntHelper(const ImageSkiaRep& image_rep, | 485 void DrawImageIntHelper(const ImageSkiaRep& image_rep, |
480 int src_x, | 486 int src_x, |
481 int src_y, | 487 int src_y, |
482 int src_w, | 488 int src_w, |
483 int src_h, | 489 int src_h, |
484 int dest_x, | 490 int dest_x, |
485 int dest_y, | 491 int dest_y, |
486 int dest_w, | 492 int dest_w, |
487 int dest_h, | 493 int dest_h, |
488 bool filter, | 494 bool filter, |
489 const SkPaint& paint, | 495 const cc::PaintFlags& paint, |
490 bool remove_image_scale); | 496 bool remove_image_scale); |
491 | 497 |
492 // The device scale factor at which drawing on this canvas occurs. | 498 // The device scale factor at which drawing on this canvas occurs. |
493 // An additional scale can be applied via Canvas::Scale(). However, | 499 // An additional scale can be applied via Canvas::Scale(). However, |
494 // Canvas::Scale() does not affect |image_scale_|. | 500 // Canvas::Scale() does not affect |image_scale_|. |
495 float image_scale_; | 501 float image_scale_; |
496 | 502 |
497 // canvas_ is our active canvas object. Sometimes we are also the owner, | 503 // canvas_ is our active canvas object. Sometimes we are also the owner, |
498 // in which case surface_ will be set. Other times we are just | 504 // in which case surface_ will be set. Other times we are just |
499 // borrowing someone else's canvas, in which case canvas_ will point there | 505 // borrowing someone else's canvas, in which case canvas_ will point there |
500 // but surface_ will be null. | 506 // but surface_ will be null. |
501 sk_sp<SkSurface> surface_; | 507 sk_sp<cc::PaintSurface> surface_; |
502 SkCanvas* canvas_; | 508 cc::PaintCanvas* canvas_; |
503 | 509 |
504 DISALLOW_COPY_AND_ASSIGN(Canvas); | 510 DISALLOW_COPY_AND_ASSIGN(Canvas); |
505 }; | 511 }; |
506 | 512 |
507 } // namespace gfx | 513 } // namespace gfx |
508 | 514 |
509 #endif // UI_GFX_CANVAS_H_ | 515 #endif // UI_GFX_CANVAS_H_ |
OLD | NEW |