Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 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 { | 32 namespace { |
| 33 | 33 |
| 34 sk_sp<cc::PaintSurface> CreateSurface(const Size& size, bool is_opaque) { | 34 SkBitmap CreateBitmap(const Size& size, bool is_opaque) { |
| 35 // SkSurface cannot be zero-sized, but clients of Canvas sometimes request | 35 // SkSurface cannot be zero-sized, but clients of Canvas sometimes request |
| 36 // that (and then later resize). | 36 // that (and then later resize). |
| 37 int width = std::max(size.width(), 1); | 37 int width = std::max(size.width(), 1); |
| 38 int height = std::max(size.height(), 1); | 38 int height = std::max(size.height(), 1); |
| 39 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | 39 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 40 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha); | 40 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha); |
| 41 return cc::PaintSurface::MakeRaster(info); | 41 SkBitmap bitmap; |
| 42 bool success = bitmap.tryAllocPixels(info); | |
|
danakj
2017/03/21 20:02:56
why not allocPixels? We should crash on OOM
vmpstr
2017/03/21 22:34:53
Done.
| |
| 43 DCHECK(success); | |
| 44 return bitmap; | |
| 42 } | 45 } |
| 43 | 46 |
| 44 } // namespace | 47 } // namespace |
| 45 | 48 |
| 46 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) | 49 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) |
| 47 : image_scale_(image_scale) { | 50 : image_scale_(image_scale) { |
| 48 Size pixel_size = ScaleToCeiledSize(size, image_scale); | 51 Size pixel_size = ScaleToCeiledSize(size, image_scale); |
| 49 surface_ = CreateSurface(pixel_size, is_opaque); | 52 canvas_ = CreateOwnedCanvas(pixel_size, is_opaque); |
| 50 canvas_ = surface_->getCanvas(); | |
| 51 | 53 |
| 52 #if !defined(USE_CAIRO) | 54 #if !defined(USE_CAIRO) |
| 53 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but | 55 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but |
| 54 // uninitialized on other platforms. | 56 // uninitialized on other platforms. |
| 55 if (!is_opaque) | 57 if (!is_opaque) |
| 56 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); | 58 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); |
| 57 #endif | 59 #endif |
| 58 | 60 |
| 59 SkScalar scale_scalar = SkFloatToScalar(image_scale); | 61 SkScalar scale_scalar = SkFloatToScalar(image_scale); |
| 60 canvas_->scale(scale_scalar, scale_scalar); | 62 canvas_->scale(scale_scalar, scale_scalar); |
| 61 } | 63 } |
| 62 | 64 |
| 63 Canvas::Canvas() | 65 Canvas::Canvas() |
| 64 : image_scale_(1.f), | 66 : image_scale_(1.f), canvas_(CreateOwnedCanvas({0, 0}, false)) {} |
| 65 surface_(CreateSurface({0, 0}, false)), | |
| 66 canvas_(surface_->getCanvas()) {} | |
| 67 | 67 |
| 68 Canvas::Canvas(cc::PaintCanvas* canvas, float image_scale) | 68 Canvas::Canvas(cc::PaintCanvas* canvas, float image_scale) |
| 69 : image_scale_(image_scale), canvas_(canvas) { | 69 : image_scale_(image_scale), canvas_(canvas) { |
| 70 DCHECK(canvas_); | 70 DCHECK(canvas_); |
| 71 } | 71 } |
| 72 | 72 |
| 73 Canvas::~Canvas() { | 73 Canvas::~Canvas() { |
| 74 } | 74 } |
| 75 | 75 |
| 76 void Canvas::RecreateBackingCanvas(const Size& size, | 76 void Canvas::RecreateBackingCanvas(const Size& size, |
| 77 float image_scale, | 77 float image_scale, |
| 78 bool is_opaque) { | 78 bool is_opaque) { |
| 79 image_scale_ = image_scale; | 79 image_scale_ = image_scale; |
| 80 Size pixel_size = ScaleToFlooredSize(size, image_scale); | 80 Size pixel_size = ScaleToFlooredSize(size, image_scale); |
| 81 surface_ = CreateSurface(pixel_size, is_opaque); | 81 canvas_ = CreateOwnedCanvas(pixel_size, is_opaque); |
| 82 canvas_ = surface_->getCanvas(); | |
| 83 | 82 |
| 84 SkScalar scale_scalar = SkFloatToScalar(image_scale); | 83 SkScalar scale_scalar = SkFloatToScalar(image_scale); |
| 85 canvas_->scale(scale_scalar, scale_scalar); | 84 canvas_->scale(scale_scalar, scale_scalar); |
| 86 } | 85 } |
| 87 | 86 |
| 88 // static | 87 // static |
| 89 void Canvas::SizeStringInt(const base::string16& text, | 88 void Canvas::SizeStringInt(const base::string16& text, |
| 90 const FontList& font_list, | 89 const FontList& font_list, |
| 91 int* width, | 90 int* width, |
| 92 int* height, | 91 int* height, |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 552 shader_scale)); | 551 shader_scale)); |
| 553 flags->setBlendMode(SkBlendMode::kSrcOver); | 552 flags->setBlendMode(SkBlendMode::kSrcOver); |
| 554 return true; | 553 return true; |
| 555 } | 554 } |
| 556 | 555 |
| 557 void Canvas::Transform(const gfx::Transform& transform) { | 556 void Canvas::Transform(const gfx::Transform& transform) { |
| 558 canvas_->concat(transform.matrix()); | 557 canvas_->concat(transform.matrix()); |
| 559 } | 558 } |
| 560 | 559 |
| 561 SkBitmap Canvas::ToBitmap() { | 560 SkBitmap Canvas::ToBitmap() { |
| 562 SkBitmap bitmap; | 561 DCHECK(bitmap_); |
| 563 bitmap.setInfo(canvas_->imageInfo()); | 562 return *bitmap_; |
| 564 canvas_->readPixels(&bitmap, 0, 0); | |
| 565 return bitmap; | |
| 566 } | 563 } |
| 567 | 564 |
| 568 bool Canvas::IntersectsClipRect(const SkRect& rect) { | 565 bool Canvas::IntersectsClipRect(const SkRect& rect) { |
| 569 SkRect clip; | 566 SkRect clip; |
| 570 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect); | 567 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect); |
| 571 } | 568 } |
| 572 | 569 |
| 573 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, | 570 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, |
| 574 int src_x, | 571 int src_x, |
| 575 int src_y, | 572 int src_y, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 612 cc::PaintFlags flags(original_flags); | 609 cc::PaintFlags flags(original_flags); |
| 613 flags.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); | 610 flags.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
| 614 flags.setShader(CreateImageRepShaderForScale( | 611 flags.setShader(CreateImageRepShaderForScale( |
| 615 image_rep, SkShader::kRepeat_TileMode, shader_scale, | 612 image_rep, SkShader::kRepeat_TileMode, shader_scale, |
| 616 remove_image_scale ? image_rep.scale() : 1.f)); | 613 remove_image_scale ? image_rep.scale() : 1.f)); |
| 617 | 614 |
| 618 // The rect will be filled by the bitmap. | 615 // The rect will be filled by the bitmap. |
| 619 canvas_->drawRect(dest_rect, flags); | 616 canvas_->drawRect(dest_rect, flags); |
| 620 } | 617 } |
| 621 | 618 |
| 619 cc::PaintCanvas* Canvas::CreateOwnedCanvas(const Size& size, bool is_opaque) { | |
| 620 bitmap_ = CreateBitmap(size, is_opaque); | |
|
danakj
2017/03/21 20:02:56
This is the only caller of CreateBitmap, inline it
vmpstr
2017/03/21 22:34:53
Done.
| |
| 621 owned_canvas_ = cc::SkiaPaintCanvas(bitmap_.value()); | |
| 622 return &owned_canvas_.value(); | |
| 623 } | |
| 624 | |
| 622 } // namespace gfx | 625 } // namespace gfx |
| OLD | NEW |