| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/image_hijack_canvas.h" | 5 #include "cc/playback/image_hijack_canvas.h" |
| 6 | 6 |
| 7 #include "base/optional.h" | 7 #include "base/optional.h" |
| 8 #include "cc/playback/discardable_image_map.h" | 8 #include "cc/playback/discardable_image_map.h" |
| 9 #include "cc/tiles/image_decode_cache.h" | 9 #include "cc/tiles/image_decode_cache.h" |
| 10 #include "skia/ext/cdl_paint.h" |
| 10 | 11 |
| 11 namespace cc { | 12 namespace cc { |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 SkIRect RoundOutRect(const SkRect& rect) { | 15 SkIRect RoundOutRect(const SkRect& rect) { |
| 15 SkIRect result; | 16 SkIRect result; |
| 16 rect.roundOut(&result); | 17 rect.roundOut(&result); |
| 17 return result; | 18 return result; |
| 18 } | 19 } |
| 19 | 20 |
| 20 class ScopedDecodedImageLock { | 21 class ScopedDecodedImageLock { |
| 21 public: | 22 public: |
| 22 ScopedDecodedImageLock(ImageDecodeCache* image_decode_cache, | 23 ScopedDecodedImageLock(ImageDecodeCache* image_decode_cache, |
| 23 sk_sp<const SkImage> image, | 24 sk_sp<const SkImage> image, |
| 24 const SkRect& src_rect, | 25 const SkRect& src_rect, |
| 25 const SkMatrix& matrix, | 26 const SkMatrix& matrix, |
| 26 const SkPaint* paint) | 27 const CdlPaint* paint) |
| 27 : image_decode_cache_(image_decode_cache), | 28 : image_decode_cache_(image_decode_cache), |
| 28 draw_image_(std::move(image), | 29 draw_image_(std::move(image), |
| 29 RoundOutRect(src_rect), | 30 RoundOutRect(src_rect), |
| 30 paint ? paint->getFilterQuality() : kNone_SkFilterQuality, | 31 paint ? paint->getFilterQuality() : kNone_SkFilterQuality, |
| 31 matrix), | 32 matrix), |
| 32 decoded_draw_image_( | 33 decoded_draw_image_( |
| 33 image_decode_cache_->GetDecodedImageForDraw(draw_image_)) { | 34 image_decode_cache_->GetDecodedImageForDraw(draw_image_)) { |
| 34 DCHECK(draw_image_.image()->isLazyGenerated()); | 35 DCHECK(draw_image_.image()->isLazyGenerated()); |
| 35 if (paint) { | 36 if (paint) { |
| 36 decoded_paint_ = *paint; | 37 decoded_paint_ = *paint; |
| 37 decoded_paint_->setFilterQuality(decoded_draw_image_.filter_quality()); | 38 decoded_paint_->setFilterQuality(decoded_draw_image_.filter_quality()); |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 | 41 |
| 41 ~ScopedDecodedImageLock() { | 42 ~ScopedDecodedImageLock() { |
| 42 image_decode_cache_->DrawWithImageFinished(draw_image_, | 43 image_decode_cache_->DrawWithImageFinished(draw_image_, |
| 43 decoded_draw_image_); | 44 decoded_draw_image_); |
| 44 } | 45 } |
| 45 | 46 |
| 46 const DecodedDrawImage& decoded_image() const { return decoded_draw_image_; } | 47 const DecodedDrawImage& decoded_image() const { return decoded_draw_image_; } |
| 47 const SkPaint* decoded_paint() const { | 48 const CdlPaint* decoded_paint() const { |
| 48 return decoded_paint_ ? &decoded_paint_.value() : nullptr; | 49 return decoded_paint_ ? &decoded_paint_.value() : nullptr; |
| 49 } | 50 } |
| 50 | 51 |
| 51 private: | 52 private: |
| 52 ImageDecodeCache* image_decode_cache_; | 53 ImageDecodeCache* image_decode_cache_; |
| 53 DrawImage draw_image_; | 54 DrawImage draw_image_; |
| 54 DecodedDrawImage decoded_draw_image_; | 55 DecodedDrawImage decoded_draw_image_; |
| 55 base::Optional<SkPaint> decoded_paint_; | 56 base::Optional<CdlPaint> decoded_paint_; |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 } // namespace | 59 } // namespace |
| 59 | 60 |
| 60 ImageHijackCanvas::ImageHijackCanvas(int width, | 61 ImageHijackCanvas::ImageHijackCanvas(SkCanvas* canvas, |
| 61 int height, | |
| 62 ImageDecodeCache* image_decode_cache) | 62 ImageDecodeCache* image_decode_cache) |
| 63 : SkNWayCanvas(width, height), image_decode_cache_(image_decode_cache) {} | 63 : CdlPassThroughCanvas(canvas), image_decode_cache_(image_decode_cache) {} |
| 64 | 64 |
| 65 void ImageHijackCanvas::onDrawPicture(const SkPicture* picture, | 65 void ImageHijackCanvas::onDrawPicture(const CdlPicture* picture, |
| 66 const SkMatrix* matrix, | 66 const SkMatrix* matrix, |
| 67 const SkPaint* paint) { | 67 const CdlPaint* paint) { |
| 68 // Ensure that pictures are unpacked by this canvas, instead of being | 68 // Ensure that pictures are unpacked by this canvas, instead of being |
| 69 // forwarded to the raster canvas. | 69 // forwarded to the raster canvas. |
| 70 SkCanvas::onDrawPicture(picture, matrix, paint); | 70 CdlCanvas::onDrawPicture(picture, matrix, paint); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void ImageHijackCanvas::onDrawImage(const SkImage* image, | 73 void ImageHijackCanvas::onDrawImage(const SkImage* image, |
| 74 SkScalar x, | 74 SkScalar x, |
| 75 SkScalar y, | 75 SkScalar y, |
| 76 const SkPaint* paint) { | 76 const CdlPaint* paint) { |
| 77 if (!image->isLazyGenerated()) { | 77 if (!image->isLazyGenerated()) { |
| 78 SkNWayCanvas::onDrawImage(image, x, y, paint); | 78 CdlPassThroughCanvas::onDrawImage(image, x, y, paint); |
| 79 return; | 79 return; |
| 80 } | 80 } |
| 81 | 81 |
| 82 SkMatrix ctm = getTotalMatrix(); | 82 SkMatrix ctm = getTotalMatrix(); |
| 83 | 83 |
| 84 ScopedDecodedImageLock scoped_lock( | 84 ScopedDecodedImageLock scoped_lock( |
| 85 image_decode_cache_, sk_ref_sp(image), | 85 image_decode_cache_, sk_ref_sp(image), |
| 86 SkRect::MakeIWH(image->width(), image->height()), ctm, paint); | 86 SkRect::MakeIWH(image->width(), image->height()), ctm, paint); |
| 87 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | 87 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); |
| 88 if (!decoded_image.image()) | 88 if (!decoded_image.image()) |
| 89 return; | 89 return; |
| 90 | 90 |
| 91 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width())); | 91 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width())); |
| 92 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height())); | 92 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height())); |
| 93 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | 93 const CdlPaint* decoded_paint = scoped_lock.decoded_paint(); |
| 94 | 94 |
| 95 bool need_scale = !decoded_image.is_scale_adjustment_identity(); | 95 bool need_scale = !decoded_image.is_scale_adjustment_identity(); |
| 96 if (need_scale) { | 96 if (need_scale) { |
| 97 SkNWayCanvas::save(); | 97 CdlPassThroughCanvas::save(); |
| 98 SkNWayCanvas::scale(1.f / (decoded_image.scale_adjustment().width()), | 98 CdlPassThroughCanvas::scale( |
| 99 1.f / (decoded_image.scale_adjustment().height())); | 99 1.f / (decoded_image.scale_adjustment().width()), |
| 100 1.f / (decoded_image.scale_adjustment().height())); |
| 100 } | 101 } |
| 101 SkNWayCanvas::onDrawImage(decoded_image.image().get(), x, y, decoded_paint); | 102 CdlPassThroughCanvas::onDrawImage(decoded_image.image().get(), x, y, |
| 103 decoded_paint); |
| 102 if (need_scale) | 104 if (need_scale) |
| 103 SkNWayCanvas::restore(); | 105 CdlPassThroughCanvas::restore(); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void ImageHijackCanvas::onDrawImageRect(const SkImage* image, | 108 void ImageHijackCanvas::onDrawImageRect( |
| 107 const SkRect* src, | 109 const SkImage* image, |
| 108 const SkRect& dst, | 110 const SkRect* src, |
| 109 const SkPaint* paint, | 111 const SkRect& dst, |
| 110 SrcRectConstraint constraint) { | 112 const CdlPaint* paint, |
| 113 SkCanvas::SrcRectConstraint constraint) { |
| 111 if (!image->isLazyGenerated()) { | 114 if (!image->isLazyGenerated()) { |
| 112 SkNWayCanvas::onDrawImageRect(image, src, dst, paint, constraint); | 115 CdlPassThroughCanvas::onDrawImageRect(image, src, dst, paint, constraint); |
| 113 return; | 116 return; |
| 114 } | 117 } |
| 115 | 118 |
| 116 SkRect src_storage; | 119 SkRect src_storage; |
| 117 if (!src) { | 120 if (!src) { |
| 118 src_storage = SkRect::MakeIWH(image->width(), image->height()); | 121 src_storage = SkRect::MakeIWH(image->width(), image->height()); |
| 119 src = &src_storage; | 122 src = &src_storage; |
| 120 } | 123 } |
| 121 SkMatrix matrix; | 124 SkMatrix matrix; |
| 122 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); | 125 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); |
| 123 matrix.postConcat(getTotalMatrix()); | 126 matrix.postConcat(getTotalMatrix()); |
| 124 | 127 |
| 125 ScopedDecodedImageLock scoped_lock(image_decode_cache_, sk_ref_sp(image), | 128 ScopedDecodedImageLock scoped_lock(image_decode_cache_, sk_ref_sp(image), |
| 126 *src, matrix, paint); | 129 *src, matrix, paint); |
| 127 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | 130 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); |
| 128 if (!decoded_image.image()) | 131 if (!decoded_image.image()) |
| 129 return; | 132 return; |
| 130 | 133 |
| 131 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | 134 const CdlPaint* decoded_paint = scoped_lock.decoded_paint(); |
| 132 | 135 |
| 133 SkRect adjusted_src = | 136 SkRect adjusted_src = |
| 134 src->makeOffset(decoded_image.src_rect_offset().width(), | 137 src->makeOffset(decoded_image.src_rect_offset().width(), |
| 135 decoded_image.src_rect_offset().height()); | 138 decoded_image.src_rect_offset().height()); |
| 136 if (!decoded_image.is_scale_adjustment_identity()) { | 139 if (!decoded_image.is_scale_adjustment_identity()) { |
| 137 float x_scale = decoded_image.scale_adjustment().width(); | 140 float x_scale = decoded_image.scale_adjustment().width(); |
| 138 float y_scale = decoded_image.scale_adjustment().height(); | 141 float y_scale = decoded_image.scale_adjustment().height(); |
| 139 adjusted_src = SkRect::MakeXYWH( | 142 adjusted_src = SkRect::MakeXYWH( |
| 140 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale, | 143 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale, |
| 141 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale); | 144 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale); |
| 142 } | 145 } |
| 143 SkNWayCanvas::onDrawImageRect(decoded_image.image().get(), &adjusted_src, dst, | 146 CdlPassThroughCanvas::onDrawImageRect(decoded_image.image().get(), |
| 144 decoded_paint, constraint); | 147 &adjusted_src, dst, decoded_paint, |
| 148 constraint); |
| 145 } | 149 } |
| 146 | 150 |
| 147 void ImageHijackCanvas::onDrawImageNine(const SkImage* image, | |
| 148 const SkIRect& center, | |
| 149 const SkRect& dst, | |
| 150 const SkPaint* paint) { | |
| 151 // No cc embedder issues image nine calls. | |
| 152 NOTREACHED(); | |
| 153 } | |
| 154 | 151 |
| 155 } // namespace cc | 152 } // namespace cc |
| OLD | NEW |