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

Side by Side Diff: cc/playback/discardable_image_map.cc

Issue 1418573002: cc: Add image decode control in the compositor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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
« no previous file with comments | « cc/playback/discardable_image_map.h ('k') | cc/playback/display_item_list.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "cc/playback/discardable_image_map.h" 5 #include "cc/playback/discardable_image_map.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/containers/adapters.h"
10 #include "cc/base/math_util.h" 11 #include "cc/base/math_util.h"
11 #include "cc/playback/display_item_list.h" 12 #include "cc/playback/display_item_list.h"
12 #include "third_party/skia/include/utils/SkNWayCanvas.h" 13 #include "third_party/skia/include/utils/SkNWayCanvas.h"
13 #include "ui/gfx/geometry/rect_conversions.h" 14 #include "ui/gfx/geometry/rect_conversions.h"
14 #include "ui/gfx/skia_util.h" 15 #include "ui/gfx/skia_util.h"
15 16
16 namespace cc { 17 namespace cc {
17 18
18 namespace {
19
20 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { 19 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) {
21 SkRect dst; 20 SkRect dst;
22 matrix.mapRect(&dst, src); 21 matrix.mapRect(&dst, src);
23 return dst; 22 return dst;
24 } 23 }
25 24
26 SkSize ExtractScale(const SkMatrix& matrix) { 25 bool ExtractScale(const SkMatrix& matrix, SkSize* scale) {
27 SkSize scale = SkSize::Make(matrix.getScaleX(), matrix.getScaleY()); 26 *scale = SkSize::Make(matrix.getScaleX(), matrix.getScaleY());
28 if (matrix.getType() & SkMatrix::kAffine_Mask) { 27 if (matrix.getType() & SkMatrix::kAffine_Mask) {
29 if (!matrix.decomposeScale(&scale)) 28 if (!matrix.decomposeScale(scale)) {
30 scale.set(1, 1); 29 scale->set(1, 1);
30 return false;
31 }
31 } 32 }
32 return scale; 33 return true;
33 } 34 }
34 35
36 bool ComputeRectBoundsFromPaint(const SkRect& rect,
37 const SkPaint* current_paint,
38 const std::vector<const SkPaint*>& saved_paints,
39 SkRect* paint_bounds) {
40 *paint_bounds = rect;
41 if (current_paint) {
42 if (!current_paint->canComputeFastBounds())
43 return false;
44 *paint_bounds =
45 current_paint->computeFastBounds(*paint_bounds, paint_bounds);
46 }
47
48 for (const auto* paint : base::Reversed(saved_paints)) {
49 if (!paint)
50 continue;
51 if (!paint->canComputeFastBounds())
52 return false;
53 *paint_bounds = paint->computeFastBounds(*paint_bounds, paint_bounds);
54 }
55 return true;
56 }
57
58 namespace {
59
35 // We're using an NWay canvas with no added canvases, so in effect 60 // We're using an NWay canvas with no added canvases, so in effect
36 // non-overridden functions are no-ops. 61 // non-overridden functions are no-ops.
37 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { 62 class DiscardableImagesMetadataCanvas : public SkNWayCanvas {
38 public: 63 public:
39 DiscardableImagesMetadataCanvas( 64 DiscardableImagesMetadataCanvas(
40 int width, 65 int width,
41 int height, 66 int height,
42 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) 67 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set)
43 : SkNWayCanvas(width, height), 68 : SkNWayCanvas(width, height),
44 image_set_(image_set), 69 image_set_(image_set),
45 canvas_bounds_(SkRect::MakeIWH(width, height)) {} 70 canvas_bounds_(SkRect::MakeIWH(width, height)) {}
46 71
47 protected: 72 protected:
48 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward 73 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward
49 // it. 74 // it.
50 void onDrawPicture(const SkPicture* picture, 75 void onDrawPicture(const SkPicture* picture,
51 const SkMatrix* matrix, 76 const SkMatrix* matrix,
52 const SkPaint* paint) override { 77 const SkPaint* paint) override {
53 SkCanvas::onDrawPicture(picture, matrix, paint); 78 SkCanvas::onDrawPicture(picture, matrix, paint);
54 } 79 }
55 80
56 void onDrawImage(const SkImage* image, 81 void onDrawImage(const SkImage* image,
57 SkScalar x, 82 SkScalar x,
58 SkScalar y, 83 SkScalar y,
59 const SkPaint* paint) override { 84 const SkPaint* paint) override {
60 const SkMatrix& ctm = this->getTotalMatrix(); 85 const SkMatrix& ctm = getTotalMatrix();
61 AddImage(image, MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), 86 AddImage(
62 image->height())), 87 image, SkRect::MakeIWH(image->width(), image->height()),
63 ctm, paint); 88 MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())),
89 ctm, paint);
64 } 90 }
65 91
66 void onDrawImageRect(const SkImage* image, 92 void onDrawImageRect(const SkImage* image,
67 const SkRect* src, 93 const SkRect* src,
68 const SkRect& dst, 94 const SkRect& dst,
69 const SkPaint* paint, 95 const SkPaint* paint,
70 SrcRectConstraint) override { 96 SrcRectConstraint) override {
71 const SkMatrix& ctm = this->getTotalMatrix(); 97 const SkMatrix& ctm = getTotalMatrix();
72 SkRect src_storage; 98 SkRect src_storage;
73 if (!src) { 99 if (!src) {
74 src_storage = SkRect::MakeIWH(image->width(), image->height()); 100 src_storage = SkRect::MakeIWH(image->width(), image->height());
75 src = &src_storage; 101 src = &src_storage;
76 } 102 }
77 SkMatrix matrix; 103 SkMatrix matrix;
78 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); 104 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit);
79 matrix.postConcat(ctm); 105 matrix.postConcat(ctm);
80 AddImage(image, MapRect(ctm, dst), matrix, paint); 106 AddImage(image, *src, MapRect(ctm, dst), matrix, paint);
81 } 107 }
82 108
83 void onDrawImageNine(const SkImage* image, 109 void onDrawImageNine(const SkImage* image,
84 const SkIRect& center, 110 const SkIRect& center,
85 const SkRect& dst, 111 const SkRect& dst,
86 const SkPaint* paint) override { 112 const SkPaint* paint) override {
87 AddImage(image, dst, this->getTotalMatrix(), paint); 113 // Blink doesn't issue image nine calls.
enne (OOO) 2015/12/10 00:07:16 Blink doesn't issue => no cc embedder issues ...h
vmpstr 2015/12/10 00:47:03 Done.
114 NOTREACHED();
115 }
116
117 SaveLayerStrategy willSaveLayer(const SkRect* rect,
118 const SkPaint* paint,
119 SaveFlags save_flags) override {
120 saved_paints_.push_back(paint);
121 return SkNWayCanvas::willSaveLayer(rect, paint, save_flags);
122 }
123
124 void willSave() override {
125 saved_paints_.push_back(nullptr);
126 return SkNWayCanvas::willSave();
127 }
128
129 void willRestore() override {
130 DCHECK_GT(saved_paints_.size(), 0u);
131 saved_paints_.pop_back();
132 SkNWayCanvas::willRestore();
88 } 133 }
89 134
90 private: 135 private:
91 void AddImage(const SkImage* image, 136 void AddImage(const SkImage* image,
137 const SkRect& src_rect,
92 const SkRect& rect, 138 const SkRect& rect,
93 const SkMatrix& matrix, 139 const SkMatrix& matrix,
94 const SkPaint* paint) { 140 const SkPaint* paint) {
95 if (rect.intersects(canvas_bounds_) && image->isLazyGenerated()) { 141 if (!image->isLazyGenerated())
96 SkFilterQuality filter_quality = kNone_SkFilterQuality; 142 return;
97 if (paint) { 143
98 filter_quality = paint->getFilterQuality(); 144 SkRect paint_rect;
99 } 145 bool computed_paint_bounds = ComputePaintBounds(rect, paint, &paint_rect);
100 image_set_->push_back( 146 if (!computed_paint_bounds) {
101 std::make_pair(DrawImage(image, ExtractScale(matrix), filter_quality), 147 // TODO(vmpstr): UMA this case.
102 gfx::ToEnclosingRect(gfx::SkRectToRectF(rect)))); 148 paint_rect = canvas_bounds_;
103 } 149 }
150
151 if (!paint_rect.intersects(canvas_bounds_))
152 return;
153
154 SkFilterQuality filter_quality = kNone_SkFilterQuality;
155 if (paint) {
156 filter_quality = paint->getFilterQuality();
157 }
158
159 SkIRect src_irect;
160 src_rect.roundOut(&src_irect);
161 SkSize scale;
162 bool is_decomposable = ExtractScale(matrix, &scale);
163 image_set_->push_back(
164 std::make_pair(DrawImage(image, src_irect, scale, filter_quality,
165 matrix.hasPerspective(), is_decomposable),
166 gfx::ToEnclosingRect(gfx::SkRectToRectF(paint_rect))));
167 }
168
169 bool ComputePaintBounds(const SkRect& rect,
170 const SkPaint* paint,
171 SkRect* paint_bounds) {
172 return ComputeRectBoundsFromPaint(rect, paint, saved_paints_, paint_bounds);
104 } 173 }
105 174
106 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; 175 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_;
107 const SkRect canvas_bounds_; 176 const SkRect canvas_bounds_;
177 std::vector<const SkPaint*> saved_paints_;
108 }; 178 };
109 179
110 } // namespace 180 } // namespace
111 181
112 DiscardableImageMap::DiscardableImageMap() {} 182 DiscardableImageMap::DiscardableImageMap() {}
113 183
114 DiscardableImageMap::~DiscardableImageMap() {} 184 DiscardableImageMap::~DiscardableImageMap() {}
115 185
116 scoped_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( 186 scoped_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata(
117 const gfx::Size& bounds) { 187 const gfx::Size& bounds) {
(...skipping 23 matching lines...) Expand all
141 DiscardableImageMap* image_map, 211 DiscardableImageMap* image_map,
142 const gfx::Size& bounds) 212 const gfx::Size& bounds)
143 : image_map_(image_map), 213 : image_map_(image_map),
144 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} 214 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {}
145 215
146 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { 216 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() {
147 image_map_->EndGeneratingMetadata(); 217 image_map_->EndGeneratingMetadata();
148 } 218 }
149 219
150 } // namespace cc 220 } // namespace cc
OLDNEW
« no previous file with comments | « cc/playback/discardable_image_map.h ('k') | cc/playback/display_item_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698