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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "third_party/skia/include/core/SkPath.h" | 13 #include "third_party/skia/include/core/SkPath.h" |
| 14 #include "third_party/skia/include/core/SkRefCnt.h" | 14 #include "third_party/skia/include/core/SkRefCnt.h" |
| 15 #include "third_party/skia/include/effects/SkGradientShader.h" | 15 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 16 #include "ui/gfx/font_list.h" | 16 #include "ui/gfx/font_list.h" |
| 17 #include "ui/gfx/geometry/insets_f.h" | 17 #include "ui/gfx/geometry/insets_f.h" |
| 18 #include "ui/gfx/geometry/rect.h" | 18 #include "ui/gfx/geometry/rect.h" |
| 19 #include "ui/gfx/geometry/rect_conversions.h" | 19 #include "ui/gfx/geometry/rect_conversions.h" |
| 20 #include "ui/gfx/geometry/rect_f.h" | 20 #include "ui/gfx/geometry/rect_f.h" |
| 21 #include "ui/gfx/geometry/safe_integer_conversions.h" | 21 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 22 #include "ui/gfx/geometry/size_conversions.h" | 22 #include "ui/gfx/geometry/size_conversions.h" |
| 23 #include "ui/gfx/scoped_canvas.h" | 23 #include "ui/gfx/scoped_canvas.h" |
| 24 #include "ui/gfx/skia_util.h" | 24 #include "ui/gfx/skia_util.h" |
| 25 #include "ui/gfx/transform.h" | 25 #include "ui/gfx/transform.h" |
| 26 | 26 |
| 27 namespace gfx { | 27 namespace gfx { |
| 28 | 28 |
| 29 sk_sp<SkSurface> create_surface(int width, int height, bool is_opaque) { | |
|
sadrul
2017/01/06 05:41:32
Should be called 'CreateSurface'.
The signature c
reed1
2017/01/06 16:00:37
Done.
| |
| 30 // SkSurface cannot be zero-sized, but clients of Canvas sometimes request | |
| 31 // that (and then later resize). | |
| 32 width = std::max(width, 1); | |
| 33 height = std::max(height, 1); | |
| 34 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | |
| 35 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha); | |
| 36 return SkSurface::MakeRaster(info); | |
| 37 } | |
|
sadrul
2017/01/06 05:41:32
This should be in anon namespace.
reed1
2017/01/06 16:00:38
Done.
| |
| 38 | |
| 29 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) | 39 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) |
| 30 : image_scale_(image_scale) { | 40 : image_scale_(image_scale) { |
| 31 Size pixel_size = ScaleToCeiledSize(size, image_scale); | 41 Size pixel_size = ScaleToCeiledSize(size, image_scale); |
| 32 canvas_owner_ = skia::CreatePlatformCanvas(pixel_size.width(), | 42 surface_ = create_surface(pixel_size.width(), pixel_size.height(), is_opaque); |
| 33 pixel_size.height(), is_opaque); | 43 canvas_ = surface_->getCanvas(); |
| 34 canvas_ = canvas_owner_.get(); | |
| 35 | 44 |
| 36 #if !defined(USE_CAIRO) | 45 #if !defined(USE_CAIRO) |
| 37 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but | 46 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but |
| 38 // uninitialized on other platforms. | 47 // uninitialized on other platforms. |
| 39 if (!is_opaque) | 48 if (!is_opaque) |
| 40 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); | 49 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); |
| 41 #endif | 50 #endif |
| 42 | 51 |
| 43 SkScalar scale_scalar = SkFloatToScalar(image_scale); | 52 SkScalar scale_scalar = SkFloatToScalar(image_scale); |
| 44 canvas_->scale(scale_scalar, scale_scalar); | 53 canvas_->scale(scale_scalar, scale_scalar); |
| 45 } | 54 } |
| 46 | 55 |
| 47 Canvas::Canvas() | 56 Canvas::Canvas() |
| 48 : image_scale_(1.f), | 57 : image_scale_(1.f), |
| 49 canvas_owner_(skia::CreatePlatformCanvas(0, 0, false)), | 58 surface_(create_surface(0, 0, false)), |
| 50 canvas_(canvas_owner_.get()) {} | 59 canvas_(surface_->getCanvas()) {} |
| 51 | 60 |
| 52 Canvas::Canvas(SkCanvas* canvas, float image_scale) | 61 Canvas::Canvas(SkCanvas* canvas, float image_scale) |
| 53 : image_scale_(image_scale), canvas_(canvas) { | 62 : image_scale_(image_scale), canvas_(canvas) { |
| 54 DCHECK(canvas_); | 63 DCHECK(canvas_); |
| 55 } | 64 } |
| 56 | 65 |
| 57 Canvas::~Canvas() { | 66 Canvas::~Canvas() { |
| 58 } | 67 } |
| 59 | 68 |
| 60 void Canvas::RecreateBackingCanvas(const Size& size, | 69 void Canvas::RecreateBackingCanvas(const Size& size, |
| 61 float image_scale, | 70 float image_scale, |
| 62 bool is_opaque) { | 71 bool is_opaque) { |
| 63 image_scale_ = image_scale; | 72 image_scale_ = image_scale; |
| 64 Size pixel_size = ScaleToFlooredSize(size, image_scale); | 73 Size pixel_size = ScaleToFlooredSize(size, image_scale); |
| 65 canvas_owner_ = skia::CreatePlatformCanvas(pixel_size.width(), | 74 surface_ = create_surface(pixel_size.width(), pixel_size.height(), is_opaque); |
|
sadrul
2017/01/06 05:41:32
Looks like these are the only uses of skia::Create
reed1
2017/01/06 16:00:38
Good observation. Florin and I are interleaving CL
| |
| 66 pixel_size.height(), is_opaque); | 75 canvas_ = surface_->getCanvas(); |
| 67 canvas_ = canvas_owner_.get(); | |
| 68 | 76 |
| 69 SkScalar scale_scalar = SkFloatToScalar(image_scale); | 77 SkScalar scale_scalar = SkFloatToScalar(image_scale); |
| 70 canvas_->scale(scale_scalar, scale_scalar); | 78 canvas_->scale(scale_scalar, scale_scalar); |
| 71 } | 79 } |
| 72 | 80 |
| 73 // static | 81 // static |
| 74 void Canvas::SizeStringInt(const base::string16& text, | 82 void Canvas::SizeStringInt(const base::string16& text, |
| 75 const FontList& font_list, | 83 const FontList& font_list, |
| 76 int* width, | 84 int* width, |
| 77 int* height, | 85 int* height, |
| (...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 588 p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); | 596 p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
| 589 p.setShader(CreateImageRepShaderForScale( | 597 p.setShader(CreateImageRepShaderForScale( |
| 590 image_rep, SkShader::kRepeat_TileMode, shader_scale, | 598 image_rep, SkShader::kRepeat_TileMode, shader_scale, |
| 591 remove_image_scale ? image_rep.scale() : 1.f)); | 599 remove_image_scale ? image_rep.scale() : 1.f)); |
| 592 | 600 |
| 593 // The rect will be filled by the bitmap. | 601 // The rect will be filled by the bitmap. |
| 594 canvas_->drawRect(dest_rect, p); | 602 canvas_->drawRect(dest_rect, p); |
| 595 } | 603 } |
| 596 | 604 |
| 597 } // namespace gfx | 605 } // namespace gfx |
| OLD | NEW |