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

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/trace_event/trace_event.h" 7 #include "base/trace_event/trace_event.h"
8 #include "cc/base/region.h" 8 #include "cc/base/region.h"
9 #include "cc/debug/debug_colors.h" 9 #include "cc/debug/debug_colors.h"
10 #include "cc/playback/display_item_list.h" 10 #include "cc/playback/display_item_list.h"
11 #include "cc/tiles/image_decode_controller.h"
11 #include "skia/ext/analysis_canvas.h" 12 #include "skia/ext/analysis_canvas.h"
12 #include "third_party/skia/include/core/SkCanvas.h" 13 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "third_party/skia/include/core/SkPictureRecorder.h" 14 #include "third_party/skia/include/core/SkPictureRecorder.h"
15 #include "third_party/skia/include/utils/SkNWayCanvas.h"
14 #include "ui/gfx/geometry/rect_conversions.h" 16 #include "ui/gfx/geometry/rect_conversions.h"
15 17
16 namespace cc { 18 namespace cc {
17 19
20 namespace {
21
22 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) {
23 SkRect dst;
24 matrix.mapRect(&dst, src);
25 return dst;
26 }
27
28 bool ExtractScale(const SkMatrix& matrix, SkSize* scale) {
enne (OOO) 2015/12/02 23:33:25 Put this somewhere common and don't copy and paste
vmpstr 2015/12/03 21:20:23 Put up https://codereview.chromium.org/1497543004/
29 *scale = SkSize::Make(matrix.getScaleX(), matrix.getScaleY());
30 if (matrix.getType() & SkMatrix::kAffine_Mask) {
31 if (!matrix.decomposeScale(scale)) {
32 scale->set(1, 1);
33 return false;
34 }
35 }
36 return true;
37 }
38
39 class ImageHijackCanvas : public SkNWayCanvas {
40 public:
41 ImageHijackCanvas(int width,
42 int height,
43 ImageDecodeController* image_decode_controller)
44 : SkNWayCanvas(width, height),
45 image_decode_controller_(image_decode_controller),
46 canvas_bounds_(SkRect::MakeIWH(width, height)) {}
47
48 protected:
49 // Ensure that pictures are unpacked by this canvas, instead of being
50 // forwarded to the raster canvas.
51 void onDrawPicture(const SkPicture* picture,
52 const SkMatrix* matrix,
53 const SkPaint* paint) override {
54 SkCanvas::onDrawPicture(picture, matrix, paint);
55 }
56
57 void onDrawImage(const SkImage* image,
58 SkScalar x,
59 SkScalar y,
60 const SkPaint* paint) override {
61 if (!image->isLazyGenerated()) {
62 SkNWayCanvas::onDrawImage(image, x, y, paint);
63 return;
64 }
65
66 SkMatrix ctm = this->getTotalMatrix();
67 if (!canvas_bounds_.intersects(MapRect(
68 ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())))) {
69 return;
70 }
71
72 SkSize scale;
73 bool is_decomposable = ExtractScale(ctm, &scale);
74 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, scale,
75 is_decomposable, ctm.hasPerspective(),
76 paint);
77 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
78 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
79
80 bool need_scale = !decoded_image.is_scale_adjustment_identity();
81 if (need_scale) {
82 SkNWayCanvas::save();
83 SkNWayCanvas::scale(1.f / (decoded_image.scale_adjustment().width()),
84 1.f / (decoded_image.scale_adjustment().height()));
85 }
86 SkNWayCanvas::onDrawImage(decoded_image.image(), x, y, decoded_paint);
87 if (need_scale)
88 SkNWayCanvas::restore();
89 }
90
91 void onDrawImageRect(const SkImage* image,
92 const SkRect* src,
93 const SkRect& dst,
94 const SkPaint* paint,
95 SrcRectConstraint constraint) override {
96 if (!image->isLazyGenerated()) {
97 SkNWayCanvas::onDrawImageRect(image, src, dst, paint, constraint);
98 return;
99 }
100
101 SkMatrix ctm = this->getTotalMatrix();
102 if (!canvas_bounds_.intersects(MapRect(ctm, dst)))
103 return;
104
105 SkRect src_storage;
106 if (!src) {
107 src_storage = SkRect::MakeIWH(image->width(), image->height());
108 src = &src_storage;
109 }
110 SkMatrix matrix;
111 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit);
112 matrix.postConcat(ctm);
113
114 SkSize scale;
115 bool is_decomposable = ExtractScale(matrix, &scale);
116 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, scale,
117 is_decomposable, matrix.hasPerspective(),
118 paint);
119 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
120 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
121
122 SkRect adjusted_src = *src;
123 if (!decoded_image.is_scale_adjustment_identity()) {
124 float x_scale = decoded_image.scale_adjustment().width();
125 float y_scale = decoded_image.scale_adjustment().height();
126 adjusted_src =
127 SkRect::MakeXYWH(src->x() * x_scale, src->y() * y_scale,
128 src->width() * x_scale, src->height() * y_scale);
129 }
130 SkNWayCanvas::onDrawImageRect(decoded_image.image(), &adjusted_src, dst,
131 decoded_paint, constraint);
132 }
133
134 void onDrawImageNine(const SkImage* image,
135 const SkIRect& center,
136 const SkRect& dst,
137 const SkPaint* paint) override {
138 if (!image->isLazyGenerated()) {
139 SkNWayCanvas::onDrawImageNine(image, center, dst, paint);
140 return;
141 }
142
143 if (!canvas_bounds_.intersects(dst))
144 return;
145
146 SkMatrix ctm = this->getTotalMatrix();
147 SkSize scale;
148 bool is_decomposable = ExtractScale(ctm, &scale);
149 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, scale,
150 is_decomposable, ctm.hasPerspective(),
151 paint);
152 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
153 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
154
155 SkIRect adjusted_center = center;
156 if (!decoded_image.is_scale_adjustment_identity()) {
157 float x_scale = decoded_image.scale_adjustment().width();
158 float y_scale = decoded_image.scale_adjustment().height();
159 adjusted_center = SkIRect::MakeXYWH(
160 static_cast<int>(std::floor(center.x() * x_scale)),
161 static_cast<int>(std::floor(center.y() * y_scale)),
162 static_cast<int>(std::ceil(center.width() * x_scale)),
163 static_cast<int>(std::ceil(center.height() * y_scale)));
164 }
165 SkNWayCanvas::onDrawImageNine(decoded_image.image(), adjusted_center, dst,
166 decoded_paint);
167 }
168
169 private:
170 class ScopedDecodedImageLock {
171 public:
172 ScopedDecodedImageLock(ImageDecodeController* image_decode_controller,
173 const SkImage* image,
174 const SkSize& scale,
175 bool is_decomposable,
176 bool has_perspective,
177 const SkPaint* paint)
178 : image_decode_controller_(image_decode_controller),
179 paint_(paint),
180 draw_image_(image,
181 scale,
182 paint ? paint->getFilterQuality() : kNone_SkFilterQuality,
183 has_perspective,
184 is_decomposable),
185 decoded_draw_image_(
186 image_decode_controller_->GetDecodedImageForDraw(draw_image_)) {
187 DCHECK(image->isLazyGenerated());
188 if (paint) {
189 decoded_paint_ = *paint;
190 decoded_paint_.setFilterQuality(decoded_draw_image_.filter_quality());
191 }
192 }
193
194 ~ScopedDecodedImageLock() {
195 image_decode_controller_->DrawWithImageFinished(draw_image_,
196 decoded_draw_image_);
197 }
198
199 const DecodedDrawImage& decoded_image() const {
200 return decoded_draw_image_;
201 }
202 const SkPaint* decoded_paint() const {
203 return paint_ ? &decoded_paint_ : nullptr;
204 }
205
206 private:
207 ImageDecodeController* image_decode_controller_;
208 const SkPaint* paint_;
209 DrawImage draw_image_;
210 DecodedDrawImage decoded_draw_image_;
211 SkPaint decoded_paint_;
212 };
213
214 ImageDecodeController* image_decode_controller_;
215 const SkRect canvas_bounds_;
216 };
217
218 } // namespace
219
18 scoped_refptr<DisplayListRasterSource> 220 scoped_refptr<DisplayListRasterSource>
19 DisplayListRasterSource::CreateFromDisplayListRecordingSource( 221 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
20 const DisplayListRecordingSource* other, 222 const DisplayListRecordingSource* other,
21 bool can_use_lcd_text) { 223 bool can_use_lcd_text) {
22 return make_scoped_refptr( 224 return make_scoped_refptr(
23 new DisplayListRasterSource(other, can_use_lcd_text)); 225 new DisplayListRasterSource(other, can_use_lcd_text));
24 } 226 }
25 227
26 DisplayListRasterSource::DisplayListRasterSource( 228 DisplayListRasterSource::DisplayListRasterSource(
27 const DisplayListRecordingSource* other, 229 const DisplayListRecordingSource* other,
28 bool can_use_lcd_text) 230 bool can_use_lcd_text)
29 : display_list_(other->display_list_), 231 : display_list_(other->display_list_),
30 painter_reported_memory_usage_(other->painter_reported_memory_usage_), 232 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
31 background_color_(other->background_color_), 233 background_color_(other->background_color_),
32 requires_clear_(other->requires_clear_), 234 requires_clear_(other->requires_clear_),
33 can_use_lcd_text_(can_use_lcd_text), 235 can_use_lcd_text_(can_use_lcd_text),
34 is_solid_color_(other->is_solid_color_), 236 is_solid_color_(other->is_solid_color_),
35 solid_color_(other->solid_color_), 237 solid_color_(other->solid_color_),
36 recorded_viewport_(other->recorded_viewport_), 238 recorded_viewport_(other->recorded_viewport_),
37 size_(other->size_), 239 size_(other->size_),
38 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), 240 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
39 slow_down_raster_scale_factor_for_debug_( 241 slow_down_raster_scale_factor_for_debug_(
40 other->slow_down_raster_scale_factor_for_debug_), 242 other->slow_down_raster_scale_factor_for_debug_),
41 should_attempt_to_use_distance_field_text_(false) {} 243 should_attempt_to_use_distance_field_text_(false),
244 image_decode_controller_(nullptr) {}
42 245
43 DisplayListRasterSource::DisplayListRasterSource( 246 DisplayListRasterSource::DisplayListRasterSource(
44 const DisplayListRasterSource* other, 247 const DisplayListRasterSource* other,
45 bool can_use_lcd_text) 248 bool can_use_lcd_text)
46 : display_list_(other->display_list_), 249 : display_list_(other->display_list_),
47 painter_reported_memory_usage_(other->painter_reported_memory_usage_), 250 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
48 background_color_(other->background_color_), 251 background_color_(other->background_color_),
49 requires_clear_(other->requires_clear_), 252 requires_clear_(other->requires_clear_),
50 can_use_lcd_text_(can_use_lcd_text), 253 can_use_lcd_text_(can_use_lcd_text),
51 is_solid_color_(other->is_solid_color_), 254 is_solid_color_(other->is_solid_color_),
52 solid_color_(other->solid_color_), 255 solid_color_(other->solid_color_),
53 recorded_viewport_(other->recorded_viewport_), 256 recorded_viewport_(other->recorded_viewport_),
54 size_(other->size_), 257 size_(other->size_),
55 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), 258 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
56 slow_down_raster_scale_factor_for_debug_( 259 slow_down_raster_scale_factor_for_debug_(
57 other->slow_down_raster_scale_factor_for_debug_), 260 other->slow_down_raster_scale_factor_for_debug_),
58 should_attempt_to_use_distance_field_text_( 261 should_attempt_to_use_distance_field_text_(
59 other->should_attempt_to_use_distance_field_text_) {} 262 other->should_attempt_to_use_distance_field_text_),
263 image_decode_controller_(other->image_decode_controller_) {}
60 264
61 DisplayListRasterSource::~DisplayListRasterSource() { 265 DisplayListRasterSource::~DisplayListRasterSource() {
62 } 266 }
63 267
64 void DisplayListRasterSource::PlaybackToSharedCanvas( 268 void DisplayListRasterSource::PlaybackToSharedCanvas(
65 SkCanvas* canvas, 269 SkCanvas* raster_canvas,
66 const gfx::Rect& canvas_rect, 270 const gfx::Rect& canvas_rect,
67 float contents_scale) const { 271 float contents_scale) const {
68 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale); 272 SkImageInfo info = raster_canvas->imageInfo();
273 ImageHijackCanvas canvas(info.width(), info.height(),
274 image_decode_controller_);
275 canvas.addCanvas(raster_canvas);
276
277 RasterCommon(&canvas, NULL, canvas_rect, canvas_rect, contents_scale);
69 } 278 }
70 279
71 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas, 280 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas,
72 const gfx::Rect& canvas_rect, 281 const gfx::Rect& canvas_rect,
73 float contents_scale) const { 282 float contents_scale) const {
74 RasterCommon(canvas, canvas, canvas_rect, canvas_rect, contents_scale); 283 RasterCommon(canvas, canvas, canvas_rect, canvas_rect, contents_scale);
75 } 284 }
76 285
77 void DisplayListRasterSource::PlaybackToCanvas( 286 void DisplayListRasterSource::PlaybackToCanvas(
78 SkCanvas* canvas, 287 SkCanvas* raster_canvas,
79 const gfx::Rect& canvas_bitmap_rect, 288 const gfx::Rect& canvas_bitmap_rect,
80 const gfx::Rect& canvas_playback_rect, 289 const gfx::Rect& canvas_playback_rect,
81 float contents_scale) const { 290 float contents_scale) const {
82 PrepareForPlaybackToCanvas(canvas, canvas_bitmap_rect, canvas_playback_rect, 291 PrepareForPlaybackToCanvas(raster_canvas, canvas_bitmap_rect,
83 contents_scale); 292 canvas_playback_rect, contents_scale);
84 RasterCommon(canvas, NULL, canvas_bitmap_rect, canvas_playback_rect, 293
294 SkImageInfo info = raster_canvas->imageInfo();
295 ImageHijackCanvas canvas(info.width(), info.height(),
296 image_decode_controller_);
297 canvas.addCanvas(raster_canvas);
298 RasterCommon(&canvas, NULL, canvas_bitmap_rect, canvas_playback_rect,
85 contents_scale); 299 contents_scale);
86 } 300 }
87 301
88 void DisplayListRasterSource::PrepareForPlaybackToCanvas( 302 void DisplayListRasterSource::PrepareForPlaybackToCanvas(
89 SkCanvas* canvas, 303 SkCanvas* canvas,
90 const gfx::Rect& canvas_bitmap_rect, 304 const gfx::Rect& canvas_bitmap_rect,
91 const gfx::Rect& canvas_playback_rect, 305 const gfx::Rect& canvas_playback_rect,
92 float contents_scale) const { 306 float contents_scale) const {
93 // TODO(hendrikw): See if we can split this up into separate functions. 307 // TODO(hendrikw): See if we can split this up into separate functions.
94 bool partial_update = canvas_bitmap_rect != canvas_playback_rect; 308 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_; 507 return can_use_lcd_text_;
294 } 508 }
295 509
296 scoped_refptr<DisplayListRasterSource> 510 scoped_refptr<DisplayListRasterSource>
297 DisplayListRasterSource::CreateCloneWithoutLCDText() const { 511 DisplayListRasterSource::CreateCloneWithoutLCDText() const {
298 bool can_use_lcd_text = false; 512 bool can_use_lcd_text = false;
299 return scoped_refptr<DisplayListRasterSource>( 513 return scoped_refptr<DisplayListRasterSource>(
300 new DisplayListRasterSource(this, can_use_lcd_text)); 514 new DisplayListRasterSource(this, can_use_lcd_text));
301 } 515 }
302 516
517 void DisplayListRasterSource::SetImageDecodeController(
518 ImageDecodeController* image_decode_controller) {
519 DCHECK(image_decode_controller);
520 // Note that although this function should only be called once, tests tend to
521 // call it several times using the same controller.
522 DCHECK(!image_decode_controller_ ||
enne (OOO) 2015/12/02 23:33:25 rip DCHECK_IMPLIES
vmpstr 2015/12/03 21:20:23 heh yup!
523 image_decode_controller_ == image_decode_controller);
524 image_decode_controller_ = image_decode_controller;
525 }
526
303 } // namespace cc 527 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698