OLD | NEW |
---|---|
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 SkSize ExtractScale(const SkMatrix& matrix) { | |
29 SkSize 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 } | |
34 return scale; | |
35 } | |
36 | |
37 class ImageHijackCanvas : public SkNWayCanvas { | |
38 public: | |
39 ImageHijackCanvas(int width, | |
40 int height, | |
41 ImageDecodeController* image_decode_controller) | |
42 : SkNWayCanvas(width, height), | |
43 image_decode_controller_(image_decode_controller), | |
44 canvas_bounds_(SkRect::MakeIWH(width, height)) {} | |
45 | |
46 protected: | |
47 // Ensure that pictures are unpacked by this canvas, instead of being | |
48 // forwarded to the raster canvas. | |
49 void onDrawPicture(const SkPicture* picture, | |
50 const SkMatrix* matrix, | |
51 const SkPaint* paint) override { | |
52 SkCanvas::onDrawPicture(picture, matrix, paint); | |
53 } | |
54 | |
55 void onDrawImage(const SkImage* image, | |
56 SkScalar x, | |
57 SkScalar y, | |
58 const SkPaint* paint) override { | |
59 if (!image->isLazyGenerated()) { | |
60 SkNWayCanvas::onDrawImage(image, x, y, paint); | |
61 return; | |
62 } | |
63 | |
64 SkMatrix ctm = this->getTotalMatrix(); | |
65 if (!canvas_bounds_.intersects(MapRect( | |
66 ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())))) { | |
67 return; | |
68 } | |
69 | |
70 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, | |
71 ExtractScale(ctm), paint); | |
72 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | |
73 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | |
74 | |
75 SkNWayCanvas::save(); | |
enne (OOO)
2015/10/29 22:59:45
Is it worth conditionalizing this save and restore
| |
76 SkNWayCanvas::scale(1.f / (decoded_image.scale_adjustment().width()), | |
77 1.f / (decoded_image.scale_adjustment().height())); | |
78 SkNWayCanvas::onDrawImage(decoded_image.image(), x, y, decoded_paint); | |
79 SkNWayCanvas::restore(); | |
80 } | |
81 | |
82 void onDrawImageRect(const SkImage* image, | |
83 const SkRect* src, | |
84 const SkRect& dst, | |
85 const SkPaint* paint, | |
86 SrcRectConstraint constraint) override { | |
87 if (!image->isLazyGenerated()) { | |
88 SkNWayCanvas::onDrawImageRect(image, src, dst, paint, constraint); | |
89 return; | |
90 } | |
91 | |
92 SkMatrix ctm = this->getTotalMatrix(); | |
93 if (!canvas_bounds_.intersects(MapRect(ctm, dst))) | |
94 return; | |
95 | |
96 SkRect src_storage; | |
97 if (!src) { | |
98 src_storage = SkRect::MakeIWH(image->width(), image->height()); | |
99 src = &src_storage; | |
100 } | |
101 SkMatrix matrix; | |
102 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); | |
103 matrix.postConcat(ctm); | |
104 | |
105 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, | |
106 ExtractScale(matrix), paint); | |
107 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | |
108 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | |
109 | |
110 float x_scale = decoded_image.scale_adjustment().width(); | |
111 float y_scale = decoded_image.scale_adjustment().height(); | |
112 SkRect adjusted_src = | |
113 SkRect::MakeXYWH(src->x() * x_scale, src->y() * y_scale, | |
114 src->width() * x_scale, src->height() * y_scale); | |
115 SkNWayCanvas::onDrawImageRect(decoded_image.image(), &adjusted_src, dst, | |
116 decoded_paint, constraint); | |
117 } | |
118 | |
119 void onDrawImageNine(const SkImage* image, | |
120 const SkIRect& center, | |
121 const SkRect& dst, | |
122 const SkPaint* paint) override { | |
123 if (!image->isLazyGenerated()) { | |
124 SkNWayCanvas::onDrawImageNine(image, center, dst, paint); | |
125 return; | |
126 } | |
127 | |
128 if (!canvas_bounds_.intersects(dst)) | |
129 return; | |
130 | |
131 SkMatrix ctm = this->getTotalMatrix(); | |
132 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, | |
133 ExtractScale(ctm), paint); | |
134 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | |
135 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | |
136 | |
137 float x_scale = decoded_image.scale_adjustment().width(); | |
138 float y_scale = decoded_image.scale_adjustment().height(); | |
139 SkIRect adjusted_center = SkIRect::MakeXYWH( | |
140 static_cast<int>(std::floor(center.x() * x_scale)), | |
141 static_cast<int>(std::floor(center.y() * y_scale)), | |
142 static_cast<int>(std::ceil(center.width() * x_scale)), | |
143 static_cast<int>(std::ceil(center.height() * y_scale))); | |
144 SkNWayCanvas::onDrawImageNine(decoded_image.image(), adjusted_center, dst, | |
145 decoded_paint); | |
146 } | |
147 | |
148 private: | |
149 class ScopedDecodedImageLock { | |
150 public: | |
151 ScopedDecodedImageLock(ImageDecodeController* image_decode_controller, | |
152 const SkImage* image, | |
153 const SkSize& scale, | |
154 const SkPaint* paint) | |
155 : image_decode_controller_(image_decode_controller), | |
156 paint_(paint), | |
157 draw_image_( | |
158 image, | |
159 scale, | |
160 paint ? paint->getFilterQuality() : kNone_SkFilterQuality), | |
161 decoded_draw_image_( | |
162 image_decode_controller_->GetDecodedImageAndRef(draw_image_)) { | |
163 DCHECK(image->isLazyGenerated()); | |
164 if (paint) { | |
165 decoded_paint_ = *paint; | |
166 decoded_paint_.setFilterQuality(decoded_draw_image_.filter_quality()); | |
167 } | |
168 } | |
169 | |
170 ~ScopedDecodedImageLock() { | |
171 image_decode_controller_->DrawWithImageFinished(draw_image_); | |
172 } | |
173 | |
174 const DecodedDrawImage& decoded_image() const { | |
175 return decoded_draw_image_; | |
176 } | |
177 const SkPaint* decoded_paint() const { | |
178 return paint_ ? &decoded_paint_ : nullptr; | |
179 } | |
180 | |
181 private: | |
182 ImageDecodeController* image_decode_controller_; | |
183 const SkPaint* paint_; | |
184 DrawImage draw_image_; | |
185 DecodedDrawImage decoded_draw_image_; | |
186 SkPaint decoded_paint_; | |
187 }; | |
188 | |
189 ImageDecodeController* image_decode_controller_; | |
190 const SkRect canvas_bounds_; | |
191 }; | |
192 | |
193 } // namespace | |
194 | |
18 scoped_refptr<DisplayListRasterSource> | 195 scoped_refptr<DisplayListRasterSource> |
19 DisplayListRasterSource::CreateFromDisplayListRecordingSource( | 196 DisplayListRasterSource::CreateFromDisplayListRecordingSource( |
20 const DisplayListRecordingSource* other, | 197 const DisplayListRecordingSource* other, |
21 bool can_use_lcd_text) { | 198 bool can_use_lcd_text) { |
22 return make_scoped_refptr( | 199 return make_scoped_refptr( |
23 new DisplayListRasterSource(other, can_use_lcd_text)); | 200 new DisplayListRasterSource(other, can_use_lcd_text)); |
24 } | 201 } |
25 | 202 |
26 DisplayListRasterSource::DisplayListRasterSource( | 203 DisplayListRasterSource::DisplayListRasterSource( |
27 const DisplayListRecordingSource* other, | 204 const DisplayListRecordingSource* other, |
28 bool can_use_lcd_text) | 205 bool can_use_lcd_text) |
29 : display_list_(other->display_list_), | 206 : display_list_(other->display_list_), |
30 painter_reported_memory_usage_(other->painter_reported_memory_usage_), | 207 painter_reported_memory_usage_(other->painter_reported_memory_usage_), |
31 background_color_(other->background_color_), | 208 background_color_(other->background_color_), |
32 requires_clear_(other->requires_clear_), | 209 requires_clear_(other->requires_clear_), |
33 can_use_lcd_text_(can_use_lcd_text), | 210 can_use_lcd_text_(can_use_lcd_text), |
34 is_solid_color_(other->is_solid_color_), | 211 is_solid_color_(other->is_solid_color_), |
35 solid_color_(other->solid_color_), | 212 solid_color_(other->solid_color_), |
36 recorded_viewport_(other->recorded_viewport_), | 213 recorded_viewport_(other->recorded_viewport_), |
37 size_(other->size_), | 214 size_(other->size_), |
38 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), | 215 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), |
39 slow_down_raster_scale_factor_for_debug_( | 216 slow_down_raster_scale_factor_for_debug_( |
40 other->slow_down_raster_scale_factor_for_debug_), | 217 other->slow_down_raster_scale_factor_for_debug_), |
41 should_attempt_to_use_distance_field_text_(false) {} | 218 should_attempt_to_use_distance_field_text_(false), |
219 image_decode_controller_(nullptr) {} | |
42 | 220 |
43 DisplayListRasterSource::DisplayListRasterSource( | 221 DisplayListRasterSource::DisplayListRasterSource( |
44 const DisplayListRasterSource* other, | 222 const DisplayListRasterSource* other, |
45 bool can_use_lcd_text) | 223 bool can_use_lcd_text) |
46 : display_list_(other->display_list_), | 224 : display_list_(other->display_list_), |
47 painter_reported_memory_usage_(other->painter_reported_memory_usage_), | 225 painter_reported_memory_usage_(other->painter_reported_memory_usage_), |
48 background_color_(other->background_color_), | 226 background_color_(other->background_color_), |
49 requires_clear_(other->requires_clear_), | 227 requires_clear_(other->requires_clear_), |
50 can_use_lcd_text_(can_use_lcd_text), | 228 can_use_lcd_text_(can_use_lcd_text), |
51 is_solid_color_(other->is_solid_color_), | 229 is_solid_color_(other->is_solid_color_), |
52 solid_color_(other->solid_color_), | 230 solid_color_(other->solid_color_), |
53 recorded_viewport_(other->recorded_viewport_), | 231 recorded_viewport_(other->recorded_viewport_), |
54 size_(other->size_), | 232 size_(other->size_), |
55 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), | 233 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), |
56 slow_down_raster_scale_factor_for_debug_( | 234 slow_down_raster_scale_factor_for_debug_( |
57 other->slow_down_raster_scale_factor_for_debug_), | 235 other->slow_down_raster_scale_factor_for_debug_), |
58 should_attempt_to_use_distance_field_text_( | 236 should_attempt_to_use_distance_field_text_( |
59 other->should_attempt_to_use_distance_field_text_) {} | 237 other->should_attempt_to_use_distance_field_text_), |
238 image_decode_controller_(other->image_decode_controller_) {} | |
60 | 239 |
61 DisplayListRasterSource::~DisplayListRasterSource() { | 240 DisplayListRasterSource::~DisplayListRasterSource() { |
62 } | 241 } |
63 | 242 |
64 void DisplayListRasterSource::PlaybackToSharedCanvas( | 243 void DisplayListRasterSource::PlaybackToSharedCanvas( |
65 SkCanvas* canvas, | 244 SkCanvas* canvas, |
66 const gfx::Rect& canvas_rect, | 245 const gfx::Rect& canvas_rect, |
67 float contents_scale) const { | 246 float contents_scale) const { |
68 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale); | 247 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale); |
69 } | 248 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 SkRegion::kReplace_Op); | 345 SkRegion::kReplace_Op); |
167 canvas->clipRect(gfx::RectToSkRect(deflated_content_rect), | 346 canvas->clipRect(gfx::RectToSkRect(deflated_content_rect), |
168 SkRegion::kDifference_Op); | 347 SkRegion::kDifference_Op); |
169 canvas->drawColor(background_color_, SkXfermode::kSrc_Mode); | 348 canvas->drawColor(background_color_, SkXfermode::kSrc_Mode); |
170 canvas->restore(); | 349 canvas->restore(); |
171 } | 350 } |
172 } | 351 } |
173 } | 352 } |
174 | 353 |
175 void DisplayListRasterSource::RasterCommon( | 354 void DisplayListRasterSource::RasterCommon( |
176 SkCanvas* canvas, | 355 SkCanvas* raster_canvas, |
177 SkPicture::AbortCallback* callback, | 356 SkPicture::AbortCallback* callback, |
178 const gfx::Rect& canvas_bitmap_rect, | 357 const gfx::Rect& canvas_bitmap_rect, |
179 const gfx::Rect& canvas_playback_rect, | 358 const gfx::Rect& canvas_playback_rect, |
180 float contents_scale) const { | 359 float contents_scale) const { |
181 canvas->translate(-canvas_bitmap_rect.x(), -canvas_bitmap_rect.y()); | 360 SkImageInfo info = raster_canvas->imageInfo(); |
361 ImageHijackCanvas canvas(info.width(), info.height(), | |
362 image_decode_controller_); | |
363 canvas.addCanvas(raster_canvas); | |
364 | |
365 canvas.translate(-canvas_bitmap_rect.x(), -canvas_bitmap_rect.y()); | |
182 gfx::Rect content_rect = | 366 gfx::Rect content_rect = |
183 gfx::ScaleToEnclosingRect(gfx::Rect(size_), contents_scale); | 367 gfx::ScaleToEnclosingRect(gfx::Rect(size_), contents_scale); |
184 content_rect.Intersect(canvas_playback_rect); | 368 content_rect.Intersect(canvas_playback_rect); |
185 | 369 |
186 canvas->clipRect(gfx::RectToSkRect(content_rect), SkRegion::kIntersect_Op); | 370 canvas.clipRect(gfx::RectToSkRect(content_rect), SkRegion::kIntersect_Op); |
187 | 371 |
188 DCHECK(display_list_.get()); | 372 DCHECK(display_list_.get()); |
189 gfx::Rect canvas_target_playback_rect = | 373 gfx::Rect canvas_target_playback_rect = |
190 canvas_playback_rect - canvas_bitmap_rect.OffsetFromOrigin(); | 374 canvas_playback_rect - canvas_bitmap_rect.OffsetFromOrigin(); |
191 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); | 375 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); |
192 for (int i = 0; i < repeat_count; ++i) { | 376 for (int i = 0; i < repeat_count; ++i) { |
193 display_list_->Raster(canvas, callback, canvas_target_playback_rect, | 377 display_list_->Raster(&canvas, callback, canvas_target_playback_rect, |
194 contents_scale); | 378 contents_scale); |
195 } | 379 } |
196 } | 380 } |
197 | 381 |
198 skia::RefPtr<SkPicture> DisplayListRasterSource::GetFlattenedPicture() { | 382 skia::RefPtr<SkPicture> DisplayListRasterSource::GetFlattenedPicture() { |
199 TRACE_EVENT0("cc", "DisplayListRasterSource::GetFlattenedPicture"); | 383 TRACE_EVENT0("cc", "DisplayListRasterSource::GetFlattenedPicture"); |
200 | 384 |
201 gfx::Rect display_list_rect(size_); | 385 gfx::Rect display_list_rect(size_); |
202 SkPictureRecorder recorder; | 386 SkPictureRecorder recorder; |
203 SkCanvas* canvas = recorder.beginRecording(display_list_rect.width(), | 387 SkCanvas* canvas = recorder.beginRecording(display_list_rect.width(), |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 return can_use_lcd_text_; | 477 return can_use_lcd_text_; |
294 } | 478 } |
295 | 479 |
296 scoped_refptr<DisplayListRasterSource> | 480 scoped_refptr<DisplayListRasterSource> |
297 DisplayListRasterSource::CreateCloneWithoutLCDText() const { | 481 DisplayListRasterSource::CreateCloneWithoutLCDText() const { |
298 bool can_use_lcd_text = false; | 482 bool can_use_lcd_text = false; |
299 return scoped_refptr<DisplayListRasterSource>( | 483 return scoped_refptr<DisplayListRasterSource>( |
300 new DisplayListRasterSource(this, can_use_lcd_text)); | 484 new DisplayListRasterSource(this, can_use_lcd_text)); |
301 } | 485 } |
302 | 486 |
487 void DisplayListRasterSource::SetImageDecodeController( | |
488 ImageDecodeController* image_decode_controller) { | |
489 // This should only be set once. | |
490 DCHECK(image_decode_controller); | |
491 DCHECK(!image_decode_controller_); | |
492 image_decode_controller_ = image_decode_controller; | |
493 } | |
494 | |
303 } // namespace cc | 495 } // namespace cc |
OLD | NEW |