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 #include "ui/gfx/canvas.h" | 5 #include "ui/gfx/canvas.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #include "ui/gfx/geometry/rect_f.h" | 22 #include "ui/gfx/geometry/rect_f.h" |
23 #include "ui/gfx/geometry/safe_integer_conversions.h" | 23 #include "ui/gfx/geometry/safe_integer_conversions.h" |
24 #include "ui/gfx/geometry/size_conversions.h" | 24 #include "ui/gfx/geometry/size_conversions.h" |
25 #include "ui/gfx/scoped_canvas.h" | 25 #include "ui/gfx/scoped_canvas.h" |
26 #include "ui/gfx/skia_paint_util.h" | 26 #include "ui/gfx/skia_paint_util.h" |
27 #include "ui/gfx/skia_util.h" | 27 #include "ui/gfx/skia_util.h" |
28 #include "ui/gfx/transform.h" | 28 #include "ui/gfx/transform.h" |
29 | 29 |
30 namespace gfx { | 30 namespace gfx { |
31 | 31 |
32 namespace { | |
33 | |
34 sk_sp<cc::PaintSurface> CreateSurface(const Size& size, bool is_opaque) { | |
35 // SkSurface cannot be zero-sized, but clients of Canvas sometimes request | |
36 // that (and then later resize). | |
37 int width = std::max(size.width(), 1); | |
38 int height = std::max(size.height(), 1); | |
39 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | |
40 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha); | |
41 return cc::PaintSurface::MakeRaster(info); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) | 32 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) |
47 : image_scale_(image_scale) { | 33 : image_scale_(image_scale) { |
48 Size pixel_size = ScaleToCeiledSize(size, image_scale); | 34 Size pixel_size = ScaleToCeiledSize(size, image_scale); |
49 surface_ = CreateSurface(pixel_size, is_opaque); | 35 canvas_ = CreateOwnedCanvas(pixel_size, is_opaque); |
50 canvas_ = surface_->getCanvas(); | |
51 | 36 |
52 #if !defined(USE_CAIRO) | 37 #if !defined(USE_CAIRO) |
53 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but | 38 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but |
54 // uninitialized on other platforms. | 39 // uninitialized on other platforms. |
55 if (!is_opaque) | 40 if (!is_opaque) |
56 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); | 41 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); |
57 #endif | 42 #endif |
58 | 43 |
59 SkScalar scale_scalar = SkFloatToScalar(image_scale); | 44 SkScalar scale_scalar = SkFloatToScalar(image_scale); |
60 canvas_->scale(scale_scalar, scale_scalar); | 45 canvas_->scale(scale_scalar, scale_scalar); |
61 } | 46 } |
62 | 47 |
63 Canvas::Canvas() | 48 Canvas::Canvas() |
64 : image_scale_(1.f), | 49 : image_scale_(1.f), canvas_(CreateOwnedCanvas({0, 0}, false)) {} |
65 surface_(CreateSurface({0, 0}, false)), | |
66 canvas_(surface_->getCanvas()) {} | |
67 | 50 |
68 Canvas::Canvas(cc::PaintCanvas* canvas, float image_scale) | 51 Canvas::Canvas(cc::PaintCanvas* canvas, float image_scale) |
69 : image_scale_(image_scale), canvas_(canvas) { | 52 : image_scale_(image_scale), canvas_(canvas) { |
70 DCHECK(canvas_); | 53 DCHECK(canvas_); |
71 } | 54 } |
72 | 55 |
73 Canvas::~Canvas() { | 56 Canvas::~Canvas() { |
74 } | 57 } |
75 | 58 |
76 void Canvas::RecreateBackingCanvas(const Size& size, | 59 void Canvas::RecreateBackingCanvas(const Size& size, |
77 float image_scale, | 60 float image_scale, |
78 bool is_opaque) { | 61 bool is_opaque) { |
79 image_scale_ = image_scale; | 62 image_scale_ = image_scale; |
80 Size pixel_size = ScaleToFlooredSize(size, image_scale); | 63 Size pixel_size = ScaleToFlooredSize(size, image_scale); |
81 surface_ = CreateSurface(pixel_size, is_opaque); | 64 canvas_ = CreateOwnedCanvas(pixel_size, is_opaque); |
82 canvas_ = surface_->getCanvas(); | |
83 | 65 |
84 SkScalar scale_scalar = SkFloatToScalar(image_scale); | 66 SkScalar scale_scalar = SkFloatToScalar(image_scale); |
85 canvas_->scale(scale_scalar, scale_scalar); | 67 canvas_->scale(scale_scalar, scale_scalar); |
86 } | 68 } |
87 | 69 |
88 // static | 70 // static |
89 void Canvas::SizeStringInt(const base::string16& text, | 71 void Canvas::SizeStringInt(const base::string16& text, |
90 const FontList& font_list, | 72 const FontList& font_list, |
91 int* width, | 73 int* width, |
92 int* height, | 74 int* height, |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
551 flags->setShader(CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, | 533 flags->setShader(CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, |
552 shader_scale)); | 534 shader_scale)); |
553 flags->setBlendMode(SkBlendMode::kSrcOver); | 535 flags->setBlendMode(SkBlendMode::kSrcOver); |
554 return true; | 536 return true; |
555 } | 537 } |
556 | 538 |
557 void Canvas::Transform(const gfx::Transform& transform) { | 539 void Canvas::Transform(const gfx::Transform& transform) { |
558 canvas_->concat(transform.matrix()); | 540 canvas_->concat(transform.matrix()); |
559 } | 541 } |
560 | 542 |
561 SkBitmap Canvas::ToBitmap() { | 543 SkBitmap Canvas::GetBitmap() const { |
562 SkBitmap bitmap; | 544 DCHECK(bitmap_); |
563 bitmap.setInfo(canvas_->imageInfo()); | 545 SkBitmap bitmap = bitmap_.value(); |
564 canvas_->readPixels(&bitmap, 0, 0); | 546 // When the bitmap is copied, it shares the underlying pixelref, but doesn't |
danakj
2017/03/22 18:46:39
amazing comment for amazing behaviour
| |
547 // initialize pixels unless they are locked. Hence, ensure that the returned | |
548 // bitmap keeps the pixelref alive by locking it. Note that the dtor of | |
549 // SkBitmap will unlock the pixelrefs, so this won't leak. Also note that | |
550 // moving SkBitmap retains the same lock as the source, so the caller | |
551 // will receive a locked-pixels bitmap. | |
552 bitmap.lockPixels(); | |
565 return bitmap; | 553 return bitmap; |
566 } | 554 } |
567 | 555 |
568 bool Canvas::IntersectsClipRect(const SkRect& rect) { | 556 bool Canvas::IntersectsClipRect(const SkRect& rect) { |
569 SkRect clip; | 557 SkRect clip; |
570 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect); | 558 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect); |
571 } | 559 } |
572 | 560 |
573 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, | 561 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, |
574 int src_x, | 562 int src_x, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
612 cc::PaintFlags flags(original_flags); | 600 cc::PaintFlags flags(original_flags); |
613 flags.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); | 601 flags.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
614 flags.setShader(CreateImageRepShaderForScale( | 602 flags.setShader(CreateImageRepShaderForScale( |
615 image_rep, SkShader::kRepeat_TileMode, shader_scale, | 603 image_rep, SkShader::kRepeat_TileMode, shader_scale, |
616 remove_image_scale ? image_rep.scale() : 1.f)); | 604 remove_image_scale ? image_rep.scale() : 1.f)); |
617 | 605 |
618 // The rect will be filled by the bitmap. | 606 // The rect will be filled by the bitmap. |
619 canvas_->drawRect(dest_rect, flags); | 607 canvas_->drawRect(dest_rect, flags); |
620 } | 608 } |
621 | 609 |
610 cc::PaintCanvas* Canvas::CreateOwnedCanvas(const Size& size, bool is_opaque) { | |
611 // SkBitmap cannot be zero-sized, but clients of Canvas sometimes request | |
612 // that (and then later resize). | |
613 int width = std::max(size.width(), 1); | |
614 int height = std::max(size.height(), 1); | |
615 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | |
616 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha); | |
617 | |
618 bitmap_.emplace(); | |
619 bitmap_->allocPixels(info); | |
620 // Ensure that the bitmap is zeroed, since the code expects that. | |
621 memset(bitmap_->getPixels(), 0, bitmap_->getSafeSize()); | |
622 | |
623 owned_canvas_ = cc::SkiaPaintCanvas(bitmap_.value()); | |
624 return &owned_canvas_.value(); | |
625 } | |
626 | |
622 } // namespace gfx | 627 } // namespace gfx |
OLD | NEW |