Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: ui/gfx/canvas.cc

Issue 2758413002: cc/paint: Remove PaintCanvas::peekPixels. (Closed)
Patch Set: update Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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::AsBitmap() {
562 SkBitmap bitmap; 544 DCHECK(bitmap_);
563 bitmap.setInfo(canvas_->imageInfo()); 545 return *bitmap_;
564 canvas_->readPixels(&bitmap, 0, 0);
565 return bitmap;
566 } 546 }
567 547
568 bool Canvas::IntersectsClipRect(const SkRect& rect) { 548 bool Canvas::IntersectsClipRect(const SkRect& rect) {
569 SkRect clip; 549 SkRect clip;
570 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect); 550 return canvas_->getLocalClipBounds(&clip) && clip.intersects(rect);
571 } 551 }
572 552
573 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, 553 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep,
574 int src_x, 554 int src_x,
575 int src_y, 555 int src_y,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 cc::PaintFlags flags(original_flags); 592 cc::PaintFlags flags(original_flags);
613 flags.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); 593 flags.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
614 flags.setShader(CreateImageRepShaderForScale( 594 flags.setShader(CreateImageRepShaderForScale(
615 image_rep, SkShader::kRepeat_TileMode, shader_scale, 595 image_rep, SkShader::kRepeat_TileMode, shader_scale,
616 remove_image_scale ? image_rep.scale() : 1.f)); 596 remove_image_scale ? image_rep.scale() : 1.f));
617 597
618 // The rect will be filled by the bitmap. 598 // The rect will be filled by the bitmap.
619 canvas_->drawRect(dest_rect, flags); 599 canvas_->drawRect(dest_rect, flags);
620 } 600 }
621 601
602 cc::PaintCanvas* Canvas::CreateOwnedCanvas(const Size& size, bool is_opaque) {
603 // SkBitmap cannot be zero-sized, but clients of Canvas sometimes request
604 // that (and then later resize).
605 int width = std::max(size.width(), 1);
606 int height = std::max(size.height(), 1);
607 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
608 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha);
609 bitmap_->allocPixels(info);
danakj 2017/03/21 22:42:48 don't you have to construct the bitmap_ first?
vmpstr 2017/03/22 17:46:55 Whoops. Done.
610 owned_canvas_ = cc::SkiaPaintCanvas(bitmap_.value());
611 return &owned_canvas_.value();
612 }
613
622 } // namespace gfx 614 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698