Chromium Code Reviews| 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 SkSize ExtractScale(const SkMatrix& matrix) { | |
| 23 SkSize scale = SkSize::Make(matrix.getScaleX(), matrix.getScaleY()); | |
| 24 if (matrix.getType() & SkMatrix::kAffine_Mask) { | |
| 25 if (!matrix.decomposeScale(&scale)) | |
| 26 scale.set(1, 1); | |
| 27 } | |
| 28 return scale; | |
| 29 } | |
| 30 | |
| 31 class ImageHijackCanvas : public SkNWayCanvas { | |
| 32 public: | |
| 33 ImageHijackCanvas(int width, | |
| 34 int height, | |
| 35 ImageDecodeController* image_decode_controller) | |
| 36 : SkNWayCanvas(width, height), | |
| 37 image_decode_controller_(image_decode_controller) {} | |
| 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 = this->getTotalMatrix(); | |
| 58 | |
| 59 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, | |
| 60 ExtractScale(ctm), paint); | |
| 61 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | |
| 62 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | |
| 63 | |
| 64 SkNWayCanvas::save(); | |
| 65 SkNWayCanvas::scale(1.f / (decoded_image.scale_adjustment().width()), | |
|
reed1
2015/10/27 17:35:07
Is scale_adjustment = scaled_version.width / origi
| |
| 66 1.f / (decoded_image.scale_adjustment().height())); | |
| 67 SkNWayCanvas::onDrawImage(decoded_image.image(), x, y, decoded_paint); | |
| 68 SkNWayCanvas::restore(); | |
| 69 } | |
| 70 | |
| 71 void onDrawImageRect(const SkImage* image, | |
| 72 const SkRect* src, | |
| 73 const SkRect& dst, | |
| 74 const SkPaint* paint, | |
| 75 SrcRectConstraint constraint) override { | |
| 76 if (!image->isLazyGenerated()) { | |
| 77 SkNWayCanvas::onDrawImageRect(image, src, dst, paint, constraint); | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 SkMatrix ctm = this->getTotalMatrix(); | |
| 82 SkRect src_storage; | |
| 83 if (!src) { | |
| 84 src_storage = SkRect::MakeIWH(image->width(), image->height()); | |
| 85 src = &src_storage; | |
| 86 } | |
| 87 SkMatrix matrix; | |
| 88 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); | |
| 89 matrix.postConcat(ctm); | |
| 90 | |
| 91 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, | |
| 92 ExtractScale(matrix), paint); | |
| 93 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | |
| 94 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | |
| 95 | |
| 96 float x_scale = decoded_image.scale_adjustment().width(); | |
| 97 float y_scale = decoded_image.scale_adjustment().height(); | |
| 98 SkRect adjusted_src = | |
| 99 SkRect::MakeXYWH(src->x() * x_scale, src->y() * y_scale, | |
| 100 src->width() * x_scale, src->height() * y_scale); | |
| 101 SkNWayCanvas::onDrawImageRect(decoded_image.image(), &adjusted_src, dst, | |
| 102 decoded_paint, constraint); | |
| 103 } | |
| 104 | |
| 105 void onDrawImageNine(const SkImage* image, | |
| 106 const SkIRect& center, | |
| 107 const SkRect& dst, | |
| 108 const SkPaint* paint) override { | |
| 109 if (!image->isLazyGenerated()) { | |
| 110 SkNWayCanvas::onDrawImageNine(image, center, dst, paint); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 SkMatrix ctm = this->getTotalMatrix(); | |
| 115 ScopedDecodedImageLock scoped_lock(image_decode_controller_, image, | |
| 116 ExtractScale(ctm), paint); | |
| 117 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | |
| 118 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | |
| 119 | |
| 120 float x_scale = decoded_image.scale_adjustment().width(); | |
| 121 float y_scale = decoded_image.scale_adjustment().height(); | |
| 122 SkIRect adjusted_center = SkIRect::MakeXYWH( | |
| 123 static_cast<int>(std::floor(center.x() * x_scale)), | |
| 124 static_cast<int>(std::floor(center.y() * y_scale)), | |
| 125 static_cast<int>(std::ceil(center.width() * x_scale)), | |
| 126 static_cast<int>(std::ceil(center.height() * y_scale))); | |
| 127 SkNWayCanvas::onDrawImageNine(decoded_image.image(), adjusted_center, dst, | |
| 128 decoded_paint); | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 class ScopedDecodedImageLock { | |
| 133 public: | |
| 134 ScopedDecodedImageLock(ImageDecodeController* image_decode_controller, | |
| 135 const SkImage* image, | |
| 136 const SkSize& scale, | |
| 137 const SkPaint* paint) | |
| 138 : image_decode_controller_(image_decode_controller), | |
| 139 paint_(paint), | |
| 140 draw_image_( | |
| 141 image, | |
| 142 scale, | |
| 143 paint ? paint->getFilterQuality() : kNone_SkFilterQuality), | |
| 144 decoded_draw_image_( | |
| 145 image_decode_controller_->GetDecodedImage(draw_image_)) { | |
| 146 DCHECK(image->isLazyGenerated()); | |
| 147 if (paint) { | |
| 148 decoded_paint_ = *paint; | |
| 149 decoded_paint_.setFilterQuality(decoded_draw_image_.filter_quality()); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 ~ScopedDecodedImageLock() { | |
| 154 image_decode_controller_->DrawWithImageFinished(draw_image_); | |
| 155 } | |
| 156 | |
| 157 const DecodedDrawImage& decoded_image() const { | |
| 158 return decoded_draw_image_; | |
| 159 } | |
| 160 const SkPaint* decoded_paint() const { | |
| 161 return paint_ ? &decoded_paint_ : nullptr; | |
| 162 } | |
| 163 | |
| 164 private: | |
| 165 ImageDecodeController* image_decode_controller_; | |
| 166 const SkPaint* paint_; | |
| 167 DrawImage draw_image_; | |
| 168 DecodedDrawImage decoded_draw_image_; | |
| 169 SkPaint decoded_paint_; | |
| 170 }; | |
| 171 | |
| 172 ImageDecodeController* image_decode_controller_; | |
| 173 }; | |
| 174 | |
| 175 } // namespace | |
| 176 | |
| 18 scoped_refptr<DisplayListRasterSource> | 177 scoped_refptr<DisplayListRasterSource> |
| 19 DisplayListRasterSource::CreateFromDisplayListRecordingSource( | 178 DisplayListRasterSource::CreateFromDisplayListRecordingSource( |
| 20 const DisplayListRecordingSource* other, | 179 const DisplayListRecordingSource* other, |
| 21 bool can_use_lcd_text) { | 180 bool can_use_lcd_text) { |
| 22 return make_scoped_refptr( | 181 return make_scoped_refptr( |
| 23 new DisplayListRasterSource(other, can_use_lcd_text)); | 182 new DisplayListRasterSource(other, can_use_lcd_text)); |
| 24 } | 183 } |
| 25 | 184 |
| 26 DisplayListRasterSource::DisplayListRasterSource( | 185 DisplayListRasterSource::DisplayListRasterSource( |
| 27 const DisplayListRecordingSource* other, | 186 const DisplayListRecordingSource* other, |
| 28 bool can_use_lcd_text) | 187 bool can_use_lcd_text) |
| 29 : display_list_(other->display_list_), | 188 : display_list_(other->display_list_), |
| 30 painter_reported_memory_usage_(other->painter_reported_memory_usage_), | 189 painter_reported_memory_usage_(other->painter_reported_memory_usage_), |
| 31 background_color_(other->background_color_), | 190 background_color_(other->background_color_), |
| 32 requires_clear_(other->requires_clear_), | 191 requires_clear_(other->requires_clear_), |
| 33 can_use_lcd_text_(can_use_lcd_text), | 192 can_use_lcd_text_(can_use_lcd_text), |
| 34 is_solid_color_(other->is_solid_color_), | 193 is_solid_color_(other->is_solid_color_), |
| 35 solid_color_(other->solid_color_), | 194 solid_color_(other->solid_color_), |
| 36 recorded_viewport_(other->recorded_viewport_), | 195 recorded_viewport_(other->recorded_viewport_), |
| 37 size_(other->size_), | 196 size_(other->size_), |
| 38 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), | 197 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), |
| 39 slow_down_raster_scale_factor_for_debug_( | 198 slow_down_raster_scale_factor_for_debug_( |
| 40 other->slow_down_raster_scale_factor_for_debug_), | 199 other->slow_down_raster_scale_factor_for_debug_), |
| 41 should_attempt_to_use_distance_field_text_(false) {} | 200 should_attempt_to_use_distance_field_text_(false), |
| 201 image_decode_controller_(nullptr) {} | |
| 42 | 202 |
| 43 DisplayListRasterSource::DisplayListRasterSource( | 203 DisplayListRasterSource::DisplayListRasterSource( |
| 44 const DisplayListRasterSource* other, | 204 const DisplayListRasterSource* other, |
| 45 bool can_use_lcd_text) | 205 bool can_use_lcd_text) |
| 46 : display_list_(other->display_list_), | 206 : display_list_(other->display_list_), |
| 47 painter_reported_memory_usage_(other->painter_reported_memory_usage_), | 207 painter_reported_memory_usage_(other->painter_reported_memory_usage_), |
| 48 background_color_(other->background_color_), | 208 background_color_(other->background_color_), |
| 49 requires_clear_(other->requires_clear_), | 209 requires_clear_(other->requires_clear_), |
| 50 can_use_lcd_text_(can_use_lcd_text), | 210 can_use_lcd_text_(can_use_lcd_text), |
| 51 is_solid_color_(other->is_solid_color_), | 211 is_solid_color_(other->is_solid_color_), |
| 52 solid_color_(other->solid_color_), | 212 solid_color_(other->solid_color_), |
| 53 recorded_viewport_(other->recorded_viewport_), | 213 recorded_viewport_(other->recorded_viewport_), |
| 54 size_(other->size_), | 214 size_(other->size_), |
| 55 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), | 215 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), |
| 56 slow_down_raster_scale_factor_for_debug_( | 216 slow_down_raster_scale_factor_for_debug_( |
| 57 other->slow_down_raster_scale_factor_for_debug_), | 217 other->slow_down_raster_scale_factor_for_debug_), |
| 58 should_attempt_to_use_distance_field_text_( | 218 should_attempt_to_use_distance_field_text_( |
| 59 other->should_attempt_to_use_distance_field_text_) {} | 219 other->should_attempt_to_use_distance_field_text_), |
| 220 image_decode_controller_(other->image_decode_controller_) {} | |
| 60 | 221 |
| 61 DisplayListRasterSource::~DisplayListRasterSource() { | 222 DisplayListRasterSource::~DisplayListRasterSource() { |
| 62 } | 223 } |
| 63 | 224 |
| 64 void DisplayListRasterSource::PlaybackToSharedCanvas( | 225 void DisplayListRasterSource::PlaybackToSharedCanvas( |
| 65 SkCanvas* canvas, | 226 SkCanvas* canvas, |
| 66 const gfx::Rect& canvas_rect, | 227 const gfx::Rect& canvas_rect, |
| 67 float contents_scale) const { | 228 float contents_scale) const { |
| 68 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale); | 229 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale); |
| 69 } | 230 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 SkRegion::kReplace_Op); | 327 SkRegion::kReplace_Op); |
| 167 canvas->clipRect(gfx::RectToSkRect(deflated_content_rect), | 328 canvas->clipRect(gfx::RectToSkRect(deflated_content_rect), |
| 168 SkRegion::kDifference_Op); | 329 SkRegion::kDifference_Op); |
| 169 canvas->drawColor(background_color_, SkXfermode::kSrc_Mode); | 330 canvas->drawColor(background_color_, SkXfermode::kSrc_Mode); |
| 170 canvas->restore(); | 331 canvas->restore(); |
| 171 } | 332 } |
| 172 } | 333 } |
| 173 } | 334 } |
| 174 | 335 |
| 175 void DisplayListRasterSource::RasterCommon( | 336 void DisplayListRasterSource::RasterCommon( |
| 176 SkCanvas* canvas, | 337 SkCanvas* raster_canvas, |
| 177 SkPicture::AbortCallback* callback, | 338 SkPicture::AbortCallback* callback, |
| 178 const gfx::Rect& canvas_bitmap_rect, | 339 const gfx::Rect& canvas_bitmap_rect, |
| 179 const gfx::Rect& canvas_playback_rect, | 340 const gfx::Rect& canvas_playback_rect, |
| 180 float contents_scale) const { | 341 float contents_scale) const { |
| 181 canvas->translate(-canvas_bitmap_rect.x(), -canvas_bitmap_rect.y()); | 342 SkImageInfo info = raster_canvas->imageInfo(); |
| 343 ImageHijackCanvas canvas(info.width(), info.height(), | |
| 344 image_decode_controller_); | |
| 345 canvas.addCanvas(raster_canvas); | |
| 346 | |
| 347 canvas.translate(-canvas_bitmap_rect.x(), -canvas_bitmap_rect.y()); | |
| 182 gfx::Rect content_rect = | 348 gfx::Rect content_rect = |
| 183 gfx::ScaleToEnclosingRect(gfx::Rect(size_), contents_scale); | 349 gfx::ScaleToEnclosingRect(gfx::Rect(size_), contents_scale); |
| 184 content_rect.Intersect(canvas_playback_rect); | 350 content_rect.Intersect(canvas_playback_rect); |
| 185 | 351 |
| 186 canvas->clipRect(gfx::RectToSkRect(content_rect), SkRegion::kIntersect_Op); | 352 canvas.clipRect(gfx::RectToSkRect(content_rect), SkRegion::kIntersect_Op); |
| 187 | 353 |
| 188 DCHECK(display_list_.get()); | 354 DCHECK(display_list_.get()); |
| 189 gfx::Rect canvas_target_playback_rect = | 355 gfx::Rect canvas_target_playback_rect = |
| 190 canvas_playback_rect - canvas_bitmap_rect.OffsetFromOrigin(); | 356 canvas_playback_rect - canvas_bitmap_rect.OffsetFromOrigin(); |
| 191 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); | 357 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); |
| 192 for (int i = 0; i < repeat_count; ++i) { | 358 for (int i = 0; i < repeat_count; ++i) { |
| 193 display_list_->Raster(canvas, callback, canvas_target_playback_rect, | 359 display_list_->Raster(&canvas, callback, canvas_target_playback_rect, |
| 194 contents_scale); | 360 contents_scale); |
| 195 } | 361 } |
| 196 } | 362 } |
| 197 | 363 |
| 198 skia::RefPtr<SkPicture> DisplayListRasterSource::GetFlattenedPicture() { | 364 skia::RefPtr<SkPicture> DisplayListRasterSource::GetFlattenedPicture() { |
| 199 TRACE_EVENT0("cc", "DisplayListRasterSource::GetFlattenedPicture"); | 365 TRACE_EVENT0("cc", "DisplayListRasterSource::GetFlattenedPicture"); |
| 200 | 366 |
| 201 gfx::Rect display_list_rect(size_); | 367 gfx::Rect display_list_rect(size_); |
| 202 SkPictureRecorder recorder; | 368 SkPictureRecorder recorder; |
| 203 SkCanvas* canvas = recorder.beginRecording(display_list_rect.width(), | 369 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_; | 459 return can_use_lcd_text_; |
| 294 } | 460 } |
| 295 | 461 |
| 296 scoped_refptr<DisplayListRasterSource> | 462 scoped_refptr<DisplayListRasterSource> |
| 297 DisplayListRasterSource::CreateCloneWithoutLCDText() const { | 463 DisplayListRasterSource::CreateCloneWithoutLCDText() const { |
| 298 bool can_use_lcd_text = false; | 464 bool can_use_lcd_text = false; |
| 299 return scoped_refptr<DisplayListRasterSource>( | 465 return scoped_refptr<DisplayListRasterSource>( |
| 300 new DisplayListRasterSource(this, can_use_lcd_text)); | 466 new DisplayListRasterSource(this, can_use_lcd_text)); |
| 301 } | 467 } |
| 302 | 468 |
| 469 void DisplayListRasterSource::SetImageDecodeController( | |
| 470 ImageDecodeController* image_decode_controller) { | |
| 471 // This should only be set once. | |
| 472 DCHECK(image_decode_controller); | |
| 473 DCHECK(!image_decode_controller_); | |
| 474 image_decode_controller_ = image_decode_controller; | |
| 475 } | |
| 476 | |
| 303 } // namespace cc | 477 } // namespace cc |
| OLD | NEW |