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

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;
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 // Blink doesn't issue image nine calls.
140 NOTREACHED();
141 }
142
143 SaveLayerStrategy willSaveLayer(const SkRect* rect,
144 const SkPaint* paint,
145 SaveFlags save_flags) override {
146 saved_paints_.push_back(paint);
147 return SkNWayCanvas::willSaveLayer(rect, paint, save_flags);
148 }
149
150 void willSave() override {
151 saved_paints_.push_back(nullptr);
152 return SkNWayCanvas::willSave();
153 }
154
155 void willRestore() override {
156 DCHECK_GT(saved_paints_.size(), 0u);
157 saved_paints_.pop_back();
158 SkNWayCanvas::willRestore();
159 }
160
161 private:
162 class ScopedDecodedImageLock {
163 public:
164 ScopedDecodedImageLock(ImageDecodeController* image_decode_controller,
165 const SkImage* image,
166 const SkRect& src_rect,
167 const SkSize& scale,
168 bool is_decomposable,
169 bool has_perspective,
170 const SkPaint* paint)
171 : image_decode_controller_(image_decode_controller),
172 paint_(paint),
173 draw_image_(image,
174 RoundOutRect(src_rect),
175 scale,
176 paint ? paint->getFilterQuality() : kNone_SkFilterQuality,
177 has_perspective,
178 is_decomposable),
179 decoded_draw_image_(
180 image_decode_controller_->GetDecodedImageForDraw(draw_image_)) {
181 DCHECK(image->isLazyGenerated());
182 if (paint) {
183 decoded_paint_ = *paint;
184 decoded_paint_.setFilterQuality(decoded_draw_image_.filter_quality());
185 }
186 }
187
188 ~ScopedDecodedImageLock() {
189 image_decode_controller_->DrawWithImageFinished(draw_image_,
190 decoded_draw_image_);
191 }
192
193 const DecodedDrawImage& decoded_image() const {
194 return decoded_draw_image_;
195 }
196 const SkPaint* decoded_paint() const {
197 return paint_ ? &decoded_paint_ : nullptr;
198 }
199
200 private:
201 ImageDecodeController* image_decode_controller_;
202 const SkPaint* paint_;
203 DrawImage draw_image_;
204 DecodedDrawImage decoded_draw_image_;
205 SkPaint decoded_paint_;
206 };
207
208 bool ComputePaintBounds(const SkRect& rect,
209 const SkPaint* paint,
210 SkRect* paint_bounds) {
211 return ComputeRectBoundsFromPaint(rect, paint, saved_paints_, paint_bounds);
212 }
213
214 ImageDecodeController* image_decode_controller_;
215 const SkRect canvas_bounds_;
216 std::vector<const SkPaint*> saved_paints_;
217 };
218
219 } // namespace
220
18 scoped_refptr<DisplayListRasterSource> 221 scoped_refptr<DisplayListRasterSource>
19 DisplayListRasterSource::CreateFromDisplayListRecordingSource( 222 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
20 const DisplayListRecordingSource* other, 223 const DisplayListRecordingSource* other,
21 bool can_use_lcd_text) { 224 bool can_use_lcd_text) {
22 return make_scoped_refptr( 225 return make_scoped_refptr(
23 new DisplayListRasterSource(other, can_use_lcd_text)); 226 new DisplayListRasterSource(other, can_use_lcd_text));
24 } 227 }
25 228
26 DisplayListRasterSource::DisplayListRasterSource( 229 DisplayListRasterSource::DisplayListRasterSource(
27 const DisplayListRecordingSource* other, 230 const DisplayListRecordingSource* other,
28 bool can_use_lcd_text) 231 bool can_use_lcd_text)
29 : display_list_(other->display_list_), 232 : display_list_(other->display_list_),
30 painter_reported_memory_usage_(other->painter_reported_memory_usage_), 233 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
31 background_color_(other->background_color_), 234 background_color_(other->background_color_),
32 requires_clear_(other->requires_clear_), 235 requires_clear_(other->requires_clear_),
33 can_use_lcd_text_(can_use_lcd_text), 236 can_use_lcd_text_(can_use_lcd_text),
34 is_solid_color_(other->is_solid_color_), 237 is_solid_color_(other->is_solid_color_),
35 solid_color_(other->solid_color_), 238 solid_color_(other->solid_color_),
36 recorded_viewport_(other->recorded_viewport_), 239 recorded_viewport_(other->recorded_viewport_),
37 size_(other->size_), 240 size_(other->size_),
38 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), 241 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
39 slow_down_raster_scale_factor_for_debug_( 242 slow_down_raster_scale_factor_for_debug_(
40 other->slow_down_raster_scale_factor_for_debug_), 243 other->slow_down_raster_scale_factor_for_debug_),
41 should_attempt_to_use_distance_field_text_(false) {} 244 should_attempt_to_use_distance_field_text_(false),
245 image_decode_controller_(nullptr) {}
42 246
43 DisplayListRasterSource::DisplayListRasterSource( 247 DisplayListRasterSource::DisplayListRasterSource(
44 const DisplayListRasterSource* other, 248 const DisplayListRasterSource* other,
45 bool can_use_lcd_text) 249 bool can_use_lcd_text)
46 : display_list_(other->display_list_), 250 : display_list_(other->display_list_),
47 painter_reported_memory_usage_(other->painter_reported_memory_usage_), 251 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
48 background_color_(other->background_color_), 252 background_color_(other->background_color_),
49 requires_clear_(other->requires_clear_), 253 requires_clear_(other->requires_clear_),
50 can_use_lcd_text_(can_use_lcd_text), 254 can_use_lcd_text_(can_use_lcd_text),
51 is_solid_color_(other->is_solid_color_), 255 is_solid_color_(other->is_solid_color_),
52 solid_color_(other->solid_color_), 256 solid_color_(other->solid_color_),
53 recorded_viewport_(other->recorded_viewport_), 257 recorded_viewport_(other->recorded_viewport_),
54 size_(other->size_), 258 size_(other->size_),
55 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), 259 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
56 slow_down_raster_scale_factor_for_debug_( 260 slow_down_raster_scale_factor_for_debug_(
57 other->slow_down_raster_scale_factor_for_debug_), 261 other->slow_down_raster_scale_factor_for_debug_),
58 should_attempt_to_use_distance_field_text_( 262 should_attempt_to_use_distance_field_text_(
59 other->should_attempt_to_use_distance_field_text_) {} 263 other->should_attempt_to_use_distance_field_text_),
264 image_decode_controller_(other->image_decode_controller_) {}
60 265
61 DisplayListRasterSource::~DisplayListRasterSource() { 266 DisplayListRasterSource::~DisplayListRasterSource() {
62 } 267 }
63 268
64 void DisplayListRasterSource::PlaybackToSharedCanvas( 269 void DisplayListRasterSource::PlaybackToSharedCanvas(
65 SkCanvas* canvas, 270 SkCanvas* raster_canvas,
66 const gfx::Rect& canvas_rect, 271 const gfx::Rect& canvas_rect,
67 float contents_scale) const { 272 float contents_scale) const {
68 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale); 273 // TODO(vmpstr): This can be improved by plumbing whether the tile itself has
274 // discardable images. This way we would only pay for the hijack canvas if the
275 // tile actually needed it.
276 if (display_list_->MayHaveDiscardableImages()) {
277 SkImageInfo info = raster_canvas->imageInfo();
278 ImageHijackCanvas canvas(info.width(), info.height(),
279 image_decode_controller_);
280 canvas.addCanvas(raster_canvas);
281
282 RasterCommon(&canvas, nullptr, canvas_rect, canvas_rect, contents_scale);
283 } else {
284 RasterCommon(raster_canvas, nullptr, canvas_rect, canvas_rect,
285 contents_scale);
286 }
69 } 287 }
70 288
71 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas, 289 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas,
72 const gfx::Rect& canvas_rect, 290 const gfx::Rect& canvas_rect,
73 float contents_scale) const { 291 float contents_scale) const {
74 RasterCommon(canvas, canvas, canvas_rect, canvas_rect, contents_scale); 292 RasterCommon(canvas, canvas, canvas_rect, canvas_rect, contents_scale);
75 } 293 }
76 294
77 void DisplayListRasterSource::PlaybackToCanvas( 295 void DisplayListRasterSource::PlaybackToCanvas(
78 SkCanvas* canvas, 296 SkCanvas* raster_canvas,
79 const gfx::Rect& canvas_bitmap_rect, 297 const gfx::Rect& canvas_bitmap_rect,
80 const gfx::Rect& canvas_playback_rect, 298 const gfx::Rect& canvas_playback_rect,
81 float contents_scale) const { 299 float contents_scale) const {
82 PrepareForPlaybackToCanvas(canvas, canvas_bitmap_rect, canvas_playback_rect, 300 PrepareForPlaybackToCanvas(raster_canvas, canvas_bitmap_rect,
83 contents_scale); 301 canvas_playback_rect, contents_scale);
84 RasterCommon(canvas, NULL, canvas_bitmap_rect, canvas_playback_rect, 302
303 SkImageInfo info = raster_canvas->imageInfo();
304 ImageHijackCanvas canvas(info.width(), info.height(),
305 image_decode_controller_);
306 canvas.addCanvas(raster_canvas);
307 RasterCommon(&canvas, NULL, canvas_bitmap_rect, canvas_playback_rect,
85 contents_scale); 308 contents_scale);
86 } 309 }
87 310
88 void DisplayListRasterSource::PrepareForPlaybackToCanvas( 311 void DisplayListRasterSource::PrepareForPlaybackToCanvas(
89 SkCanvas* canvas, 312 SkCanvas* canvas,
90 const gfx::Rect& canvas_bitmap_rect, 313 const gfx::Rect& canvas_bitmap_rect,
91 const gfx::Rect& canvas_playback_rect, 314 const gfx::Rect& canvas_playback_rect,
92 float contents_scale) const { 315 float contents_scale) const {
93 // TODO(hendrikw): See if we can split this up into separate functions. 316 // TODO(hendrikw): See if we can split this up into separate functions.
94 bool partial_update = canvas_bitmap_rect != canvas_playback_rect; 317 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_; 516 return can_use_lcd_text_;
294 } 517 }
295 518
296 scoped_refptr<DisplayListRasterSource> 519 scoped_refptr<DisplayListRasterSource>
297 DisplayListRasterSource::CreateCloneWithoutLCDText() const { 520 DisplayListRasterSource::CreateCloneWithoutLCDText() const {
298 bool can_use_lcd_text = false; 521 bool can_use_lcd_text = false;
299 return scoped_refptr<DisplayListRasterSource>( 522 return scoped_refptr<DisplayListRasterSource>(
300 new DisplayListRasterSource(this, can_use_lcd_text)); 523 new DisplayListRasterSource(this, can_use_lcd_text));
301 } 524 }
302 525
526 void DisplayListRasterSource::SetImageDecodeController(
527 ImageDecodeController* image_decode_controller) {
528 DCHECK(image_decode_controller);
529 // Note that although this function should only be called once, tests tend to
530 // call it several times using the same controller.
531 DCHECK(!image_decode_controller_ ||
532 image_decode_controller_ == image_decode_controller);
533 image_decode_controller_ = image_decode_controller;
534 }
535
303 } // namespace cc 536 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698