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

Side by Side Diff: cc/playback/display_list_raster_source.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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/display_list_raster_source.h" 5 #include "cc/playback/display_list_raster_source.h"
6 6
7 #include "base/containers/adapters.h"
7 #include "base/trace_event/trace_event.h" 8 #include "base/trace_event/trace_event.h"
8 #include "cc/base/region.h" 9 #include "cc/base/region.h"
9 #include "cc/debug/debug_colors.h" 10 #include "cc/debug/debug_colors.h"
11 #include "cc/playback/discardable_image_map.h"
10 #include "cc/playback/display_item_list.h" 12 #include "cc/playback/display_item_list.h"
13 #include "cc/tiles/image_decode_controller.h"
11 #include "skia/ext/analysis_canvas.h" 14 #include "skia/ext/analysis_canvas.h"
12 #include "third_party/skia/include/core/SkCanvas.h" 15 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "third_party/skia/include/core/SkPictureRecorder.h" 16 #include "third_party/skia/include/core/SkPictureRecorder.h"
17 #include "third_party/skia/include/utils/SkNWayCanvas.h"
14 #include "ui/gfx/geometry/rect_conversions.h" 18 #include "ui/gfx/geometry/rect_conversions.h"
15 19
16 namespace cc { 20 namespace cc {
17 21
22 namespace {
23
24 SkIRect RoundOutRect(const SkRect& rect) {
25 SkIRect result;
26 rect.roundOut(&result);
27 return result;
28 }
29
30 class ImageHijackCanvas : public SkNWayCanvas {
31 public:
32 ImageHijackCanvas(int width,
33 int height,
34 ImageDecodeController* image_decode_controller)
35 : SkNWayCanvas(width, height),
36 image_decode_controller_(image_decode_controller),
37 canvas_bounds_(SkRect::MakeIWH(width, height)) {}
38
39 protected:
40 // Ensure that pictures are unpacked by this canvas, instead of being
41 // forwarded to the raster canvas.
42 void onDrawPicture(const SkPicture* picture,
43 const SkMatrix* matrix,
44 const SkPaint* paint) override {
45 SkCanvas::onDrawPicture(picture, matrix, paint);
46 }
47
48 void onDrawImage(const SkImage* image,
49 SkScalar x,
50 SkScalar y,
51 const SkPaint* paint) override {
52 if (!image->isLazyGenerated()) {
53 SkNWayCanvas::onDrawImage(image, x, y, paint);
54 return;
55 }
56
57 SkMatrix ctm = getTotalMatrix();
58 SkRect paint_bounds;
59 bool computed_paint_bounds = ComputePaintBounds(
60 SkRect::MakeXYWH(x, y, image->width(), image->height()), paint,
61 &paint_bounds);
62 if (computed_paint_bounds && !canvas_bounds_.intersects(paint_bounds))
63 return;
enne (OOO) 2015/12/08 23:27:24 I'm a little confused why this check and paint ins
vmpstr 2015/12/09 23:53:41 I guess this is more of a safety check, since we c
enne (OOO) 2015/12/10 00:07:16 Safer, but also a needless cost per image draw. I
vmpstr 2015/12/10 00:47:03 Sounds good. I've removed this, since it was addin
64
65 SkSize scale;
66 bool is_decomposable = ExtractScale(ctm, &scale);
67 ScopedDecodedImageLock scoped_lock(
68 image_decode_controller_, image,
69 SkRect::MakeIWH(image->width(), image->height()), scale,
70 is_decomposable, ctm.hasPerspective(), paint);
71 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
72 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width()));
73 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height()));
74 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
75
76 bool need_scale = !decoded_image.is_scale_adjustment_identity();
77 if (need_scale) {
78 SkNWayCanvas::save();
79 SkNWayCanvas::scale(1.f / (decoded_image.scale_adjustment().width()),
80 1.f / (decoded_image.scale_adjustment().height()));
81 }
82 SkNWayCanvas::onDrawImage(decoded_image.image(), x, y, decoded_paint);
83 if (need_scale)
84 SkNWayCanvas::restore();
85 }
86
87 void onDrawImageRect(const SkImage* image,
88 const SkRect* src,
89 const SkRect& dst,
90 const SkPaint* paint,
91 SrcRectConstraint constraint) override {
92 if (!image->isLazyGenerated()) {
93 SkNWayCanvas::onDrawImageRect(image, src, dst, paint, constraint);
94 return;
95 }
96
97 SkMatrix ctm = getTotalMatrix();
98 SkRect paint_bounds;
99 bool computed_paint_bounds =
100 ComputePaintBounds(MapRect(ctm, dst), paint, &paint_bounds);
101 if (computed_paint_bounds && !canvas_bounds_.intersects(paint_bounds))
102 return;
103
104 SkRect src_storage;
105 if (!src) {
106 src_storage = SkRect::MakeIWH(image->width(), image->height());
107 src = &src_storage;
108 }
109 SkMatrix matrix;
110 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit);
111 matrix.postConcat(ctm);
112
113 SkSize scale;
114 bool is_decomposable = ExtractScale(matrix, &scale);
115 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, *src,
116 scale, is_decomposable,
117 matrix.hasPerspective(), paint);
118 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
119 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
120
121 SkRect adjusted_src =
122 src->makeOffset(decoded_image.src_rect_offset().width(),
123 decoded_image.src_rect_offset().height());
124 if (!decoded_image.is_scale_adjustment_identity()) {
125 float x_scale = decoded_image.scale_adjustment().width();
126 float y_scale = decoded_image.scale_adjustment().height();
127 adjusted_src = SkRect::MakeXYWH(
128 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale,
129 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale);
130 }
131 SkNWayCanvas::onDrawImageRect(decoded_image.image(), &adjusted_src, dst,
132 decoded_paint, constraint);
133 }
134
135 void onDrawImageNine(const SkImage* image,
136 const SkIRect& center,
137 const SkRect& dst,
138 const SkPaint* paint) override {
139 // TODO(vmpstr): Maybe this should just call drawImageRect several times.
140 // Does Blink use this?
enne (OOO) 2015/12/08 23:27:24 grep suggests no. If we don't have any testing th
vmpstr 2015/12/09 23:53:41 Done.
141 if (!image->isLazyGenerated()) {
142 SkNWayCanvas::onDrawImageNine(image, center, dst, paint);
143 return;
144 }
145
146 SkRect paint_bounds;
147 bool computed_paint_bounds = ComputePaintBounds(dst, paint, &paint_bounds);
148 if (computed_paint_bounds && !canvas_bounds_.intersects(paint_bounds))
149 return;
150
151 SkMatrix ctm = getTotalMatrix();
152 SkSize scale;
153 bool is_decomposable = ExtractScale(ctm, &scale);
154 ScopedDecodedImageLock scoped_lock(
155 image_decode_controller_, image,
156 SkRect::MakeIWH(image->width(), image->height()), scale,
157 is_decomposable, ctm.hasPerspective(), paint);
158 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
159 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width()));
160 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height()));
161 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
162
163 SkIRect adjusted_center = center;
164 if (!decoded_image.is_scale_adjustment_identity()) {
165 float x_scale = decoded_image.scale_adjustment().width();
166 float y_scale = decoded_image.scale_adjustment().height();
167 adjusted_center = SkIRect::MakeXYWH(
168 static_cast<int>(std::floor(center.x() * x_scale)),
169 static_cast<int>(std::floor(center.y() * y_scale)),
170 static_cast<int>(std::ceil(center.width() * x_scale)),
171 static_cast<int>(std::ceil(center.height() * y_scale)));
172 }
173 SkNWayCanvas::onDrawImageNine(decoded_image.image(), adjusted_center, dst,
174 decoded_paint);
175 }
176
177 SaveLayerStrategy willSaveLayer(const SkRect* rect,
178 const SkPaint* paint,
179 SaveFlags save_flags) override {
180 saved_paints_.push_back(paint);
181 return SkNWayCanvas::willSaveLayer(rect, paint, save_flags);
182 }
183
184 void willSave() override {
185 saved_paints_.push_back(nullptr);
186 return SkNWayCanvas::willSave();
187 }
188
189 void willRestore() override {
190 DCHECK_GT(saved_paints_.size(), 0u);
191 saved_paints_.pop_back();
192 SkNWayCanvas::willRestore();
193 }
194
195 private:
196 class ScopedDecodedImageLock {
197 public:
198 ScopedDecodedImageLock(ImageDecodeController* image_decode_controller,
199 const SkImage* image,
200 const SkRect& src_rect,
201 const SkSize& scale,
202 bool is_decomposable,
203 bool has_perspective,
204 const SkPaint* paint)
205 : image_decode_controller_(image_decode_controller),
206 paint_(paint),
207 draw_image_(image,
208 RoundOutRect(src_rect),
209 scale,
210 paint ? paint->getFilterQuality() : kNone_SkFilterQuality,
211 has_perspective,
212 is_decomposable),
213 decoded_draw_image_(
214 image_decode_controller_->GetDecodedImageForDraw(draw_image_)) {
215 DCHECK(image->isLazyGenerated());
216 if (paint) {
217 decoded_paint_ = *paint;
218 decoded_paint_.setFilterQuality(decoded_draw_image_.filter_quality());
219 }
220 }
221
222 ~ScopedDecodedImageLock() {
223 image_decode_controller_->DrawWithImageFinished(draw_image_,
224 decoded_draw_image_);
225 }
226
227 const DecodedDrawImage& decoded_image() const {
228 return decoded_draw_image_;
229 }
230 const SkPaint* decoded_paint() const {
231 return paint_ ? &decoded_paint_ : nullptr;
232 }
233
234 private:
235 ImageDecodeController* image_decode_controller_;
236 const SkPaint* paint_;
237 DrawImage draw_image_;
238 DecodedDrawImage decoded_draw_image_;
239 SkPaint decoded_paint_;
240 };
241
242 bool ComputePaintBounds(const SkRect& rect,
243 const SkPaint* paint,
244 SkRect* paint_bounds) {
245 return ComputeNewRectFromPaintBounds(rect, paint, saved_paints_,
246 paint_bounds);
247 }
248
249 ImageDecodeController* image_decode_controller_;
250 const SkRect canvas_bounds_;
251 std::vector<const SkPaint*> saved_paints_;
252 };
253
254 } // namespace
255
18 scoped_refptr<DisplayListRasterSource> 256 scoped_refptr<DisplayListRasterSource>
19 DisplayListRasterSource::CreateFromDisplayListRecordingSource( 257 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
20 const DisplayListRecordingSource* other, 258 const DisplayListRecordingSource* other,
21 bool can_use_lcd_text) { 259 bool can_use_lcd_text) {
22 return make_scoped_refptr( 260 return make_scoped_refptr(
23 new DisplayListRasterSource(other, can_use_lcd_text)); 261 new DisplayListRasterSource(other, can_use_lcd_text));
24 } 262 }
25 263
26 DisplayListRasterSource::DisplayListRasterSource( 264 DisplayListRasterSource::DisplayListRasterSource(
27 const DisplayListRecordingSource* other, 265 const DisplayListRecordingSource* other,
28 bool can_use_lcd_text) 266 bool can_use_lcd_text)
29 : display_list_(other->display_list_), 267 : display_list_(other->display_list_),
30 painter_reported_memory_usage_(other->painter_reported_memory_usage_), 268 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
31 background_color_(other->background_color_), 269 background_color_(other->background_color_),
32 requires_clear_(other->requires_clear_), 270 requires_clear_(other->requires_clear_),
33 can_use_lcd_text_(can_use_lcd_text), 271 can_use_lcd_text_(can_use_lcd_text),
34 is_solid_color_(other->is_solid_color_), 272 is_solid_color_(other->is_solid_color_),
35 solid_color_(other->solid_color_), 273 solid_color_(other->solid_color_),
36 recorded_viewport_(other->recorded_viewport_), 274 recorded_viewport_(other->recorded_viewport_),
37 size_(other->size_), 275 size_(other->size_),
38 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), 276 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
39 slow_down_raster_scale_factor_for_debug_( 277 slow_down_raster_scale_factor_for_debug_(
40 other->slow_down_raster_scale_factor_for_debug_), 278 other->slow_down_raster_scale_factor_for_debug_),
41 should_attempt_to_use_distance_field_text_(false) {} 279 should_attempt_to_use_distance_field_text_(false),
280 image_decode_controller_(nullptr) {}
42 281
43 DisplayListRasterSource::DisplayListRasterSource( 282 DisplayListRasterSource::DisplayListRasterSource(
44 const DisplayListRasterSource* other, 283 const DisplayListRasterSource* other,
45 bool can_use_lcd_text) 284 bool can_use_lcd_text)
46 : display_list_(other->display_list_), 285 : display_list_(other->display_list_),
47 painter_reported_memory_usage_(other->painter_reported_memory_usage_), 286 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
48 background_color_(other->background_color_), 287 background_color_(other->background_color_),
49 requires_clear_(other->requires_clear_), 288 requires_clear_(other->requires_clear_),
50 can_use_lcd_text_(can_use_lcd_text), 289 can_use_lcd_text_(can_use_lcd_text),
51 is_solid_color_(other->is_solid_color_), 290 is_solid_color_(other->is_solid_color_),
52 solid_color_(other->solid_color_), 291 solid_color_(other->solid_color_),
53 recorded_viewport_(other->recorded_viewport_), 292 recorded_viewport_(other->recorded_viewport_),
54 size_(other->size_), 293 size_(other->size_),
55 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), 294 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
56 slow_down_raster_scale_factor_for_debug_( 295 slow_down_raster_scale_factor_for_debug_(
57 other->slow_down_raster_scale_factor_for_debug_), 296 other->slow_down_raster_scale_factor_for_debug_),
58 should_attempt_to_use_distance_field_text_( 297 should_attempt_to_use_distance_field_text_(
59 other->should_attempt_to_use_distance_field_text_) {} 298 other->should_attempt_to_use_distance_field_text_),
299 image_decode_controller_(other->image_decode_controller_) {}
60 300
61 DisplayListRasterSource::~DisplayListRasterSource() { 301 DisplayListRasterSource::~DisplayListRasterSource() {
62 } 302 }
63 303
64 void DisplayListRasterSource::PlaybackToSharedCanvas( 304 void DisplayListRasterSource::PlaybackToSharedCanvas(
65 SkCanvas* canvas, 305 SkCanvas* raster_canvas,
66 const gfx::Rect& canvas_rect, 306 const gfx::Rect& canvas_rect,
67 float contents_scale) const { 307 float contents_scale) const {
68 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale); 308 // TODO(vmpstr): This can be improved by plumbing whether the tile itself has
309 // discardable images. This way we would only pay for the hijack canvas if the
enne (OOO) 2015/12/08 23:27:24 Do you have any sense of what the expense is of th
vmpstr 2015/12/09 23:53:41 I don't really know how expensive it is if there a
310 // tile actually needed it.
311 if (display_list_->MayHaveDiscardableImages()) {
312 SkImageInfo info = raster_canvas->imageInfo();
313 ImageHijackCanvas canvas(info.width(), info.height(),
314 image_decode_controller_);
315 canvas.addCanvas(raster_canvas);
316
317 RasterCommon(&canvas, nullptr, canvas_rect, canvas_rect, contents_scale);
318 } else {
319 RasterCommon(raster_canvas, nullptr, canvas_rect, canvas_rect,
320 contents_scale);
321 }
69 } 322 }
70 323
71 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas, 324 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas,
72 const gfx::Rect& canvas_rect, 325 const gfx::Rect& canvas_rect,
73 float contents_scale) const { 326 float contents_scale) const {
74 RasterCommon(canvas, canvas, canvas_rect, canvas_rect, contents_scale); 327 RasterCommon(canvas, canvas, canvas_rect, canvas_rect, contents_scale);
75 } 328 }
76 329
77 void DisplayListRasterSource::PlaybackToCanvas( 330 void DisplayListRasterSource::PlaybackToCanvas(
78 SkCanvas* canvas, 331 SkCanvas* raster_canvas,
79 const gfx::Rect& canvas_bitmap_rect, 332 const gfx::Rect& canvas_bitmap_rect,
80 const gfx::Rect& canvas_playback_rect, 333 const gfx::Rect& canvas_playback_rect,
81 float contents_scale) const { 334 float contents_scale) const {
82 PrepareForPlaybackToCanvas(canvas, canvas_bitmap_rect, canvas_playback_rect, 335 PrepareForPlaybackToCanvas(raster_canvas, canvas_bitmap_rect,
83 contents_scale); 336 canvas_playback_rect, contents_scale);
84 RasterCommon(canvas, NULL, canvas_bitmap_rect, canvas_playback_rect, 337
338 SkImageInfo info = raster_canvas->imageInfo();
339 ImageHijackCanvas canvas(info.width(), info.height(),
340 image_decode_controller_);
341 canvas.addCanvas(raster_canvas);
342 RasterCommon(&canvas, NULL, canvas_bitmap_rect, canvas_playback_rect,
85 contents_scale); 343 contents_scale);
86 } 344 }
87 345
88 void DisplayListRasterSource::PrepareForPlaybackToCanvas( 346 void DisplayListRasterSource::PrepareForPlaybackToCanvas(
89 SkCanvas* canvas, 347 SkCanvas* canvas,
90 const gfx::Rect& canvas_bitmap_rect, 348 const gfx::Rect& canvas_bitmap_rect,
91 const gfx::Rect& canvas_playback_rect, 349 const gfx::Rect& canvas_playback_rect,
92 float contents_scale) const { 350 float contents_scale) const {
93 // TODO(hendrikw): See if we can split this up into separate functions. 351 // TODO(hendrikw): See if we can split this up into separate functions.
94 bool partial_update = canvas_bitmap_rect != canvas_playback_rect; 352 bool partial_update = canvas_bitmap_rect != canvas_playback_rect;
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 return can_use_lcd_text_; 551 return can_use_lcd_text_;
294 } 552 }
295 553
296 scoped_refptr<DisplayListRasterSource> 554 scoped_refptr<DisplayListRasterSource>
297 DisplayListRasterSource::CreateCloneWithoutLCDText() const { 555 DisplayListRasterSource::CreateCloneWithoutLCDText() const {
298 bool can_use_lcd_text = false; 556 bool can_use_lcd_text = false;
299 return scoped_refptr<DisplayListRasterSource>( 557 return scoped_refptr<DisplayListRasterSource>(
300 new DisplayListRasterSource(this, can_use_lcd_text)); 558 new DisplayListRasterSource(this, can_use_lcd_text));
301 } 559 }
302 560
561 void DisplayListRasterSource::SetImageDecodeController(
562 ImageDecodeController* image_decode_controller) {
563 DCHECK(image_decode_controller);
564 // Note that although this function should only be called once, tests tend to
565 // call it several times using the same controller.
566 DCHECK(!image_decode_controller_ ||
567 image_decode_controller_ == image_decode_controller);
568 image_decode_controller_ = image_decode_controller;
569 }
570
303 } // namespace cc 571 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698