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 "third_party/skia/include/core/SkPath.h" | 10 #include "third_party/skia/include/core/SkPath.h" |
11 | 11 |
12 namespace cc { | 12 namespace cc { |
13 namespace { | 13 namespace { |
14 | 14 |
15 SkIRect RoundOutRect(const SkRect& rect) { | 15 SkIRect RoundOutRect(const SkRect& rect) { |
16 SkIRect result; | 16 SkIRect result; |
17 rect.roundOut(&result); | 17 rect.roundOut(&result); |
18 return result; | 18 return result; |
19 } | 19 } |
20 | 20 |
21 class ScopedDecodedImageLock { | 21 class ScopedDecodedImageLock { |
22 public: | 22 public: |
23 ScopedDecodedImageLock(ImageDecodeCache* image_decode_cache, | 23 ScopedDecodedImageLock(ImageDecodeCache* image_decode_cache, |
24 sk_sp<const SkImage> image, | 24 sk_sp<const SkImage> image, |
25 const SkRect& src_rect, | 25 const SkRect& src_rect, |
26 const SkMatrix& matrix, | 26 const SkMatrix& matrix, |
27 const SkPaint* paint) | 27 const PaintFlags* paint) |
28 : image_decode_cache_(image_decode_cache), | 28 : image_decode_cache_(image_decode_cache), |
29 draw_image_(std::move(image), | 29 draw_image_(std::move(image), |
30 RoundOutRect(src_rect), | 30 RoundOutRect(src_rect), |
31 paint ? paint->getFilterQuality() : kNone_SkFilterQuality, | 31 paint ? paint->getFilterQuality() : kNone_SkFilterQuality, |
32 matrix), | 32 matrix), |
33 decoded_draw_image_( | 33 decoded_draw_image_( |
34 image_decode_cache_->GetDecodedImageForDraw(draw_image_)) { | 34 image_decode_cache_->GetDecodedImageForDraw(draw_image_)) { |
35 DCHECK(draw_image_.image()->isLazyGenerated()); | 35 DCHECK(draw_image_.image()->isLazyGenerated()); |
36 if (paint) { | 36 if (paint) { |
37 decoded_paint_ = *paint; | 37 decoded_paint_ = *paint; |
38 decoded_paint_->setFilterQuality(decoded_draw_image_.filter_quality()); | 38 decoded_paint_->setFilterQuality(decoded_draw_image_.filter_quality()); |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 ScopedDecodedImageLock(ScopedDecodedImageLock&& from) | 42 ScopedDecodedImageLock(ScopedDecodedImageLock&& from) |
43 : image_decode_cache_(from.image_decode_cache_), | 43 : image_decode_cache_(from.image_decode_cache_), |
44 draw_image_(std::move(from.draw_image_)), | 44 draw_image_(std::move(from.draw_image_)), |
45 decoded_draw_image_(std::move(from.decoded_draw_image_)), | 45 decoded_draw_image_(std::move(from.decoded_draw_image_)), |
46 decoded_paint_(std::move(from.decoded_paint_)) { | 46 decoded_paint_(std::move(from.decoded_paint_)) { |
47 from.image_decode_cache_ = nullptr; | 47 from.image_decode_cache_ = nullptr; |
48 } | 48 } |
49 | 49 |
50 ~ScopedDecodedImageLock() { | 50 ~ScopedDecodedImageLock() { |
51 if (image_decode_cache_) | 51 if (image_decode_cache_) |
52 image_decode_cache_->DrawWithImageFinished(draw_image_, | 52 image_decode_cache_->DrawWithImageFinished(draw_image_, |
53 decoded_draw_image_); | 53 decoded_draw_image_); |
54 } | 54 } |
55 | 55 |
56 const DecodedDrawImage& decoded_image() const { return decoded_draw_image_; } | 56 const DecodedDrawImage& decoded_image() const { return decoded_draw_image_; } |
57 const SkPaint* decoded_paint() const { | 57 const PaintFlags* decoded_paint() const { |
58 return decoded_paint_ ? &decoded_paint_.value() : nullptr; | 58 return decoded_paint_ ? &decoded_paint_.value() : nullptr; |
59 } | 59 } |
60 | 60 |
61 private: | 61 private: |
62 ImageDecodeCache* image_decode_cache_; | 62 ImageDecodeCache* image_decode_cache_; |
63 DrawImage draw_image_; | 63 DrawImage draw_image_; |
64 DecodedDrawImage decoded_draw_image_; | 64 DecodedDrawImage decoded_draw_image_; |
65 base::Optional<SkPaint> decoded_paint_; | 65 base::Optional<PaintFlags> decoded_paint_; |
66 }; | 66 }; |
67 | 67 |
68 // Encapsulates a ScopedDecodedImageLock and an SkPaint. Use of this class | 68 // Encapsulates a ScopedDecodedImageLock and PaintFlags. Use of this class |
69 // ensures that the ScopedDecodedImageLock outlives the dependent SkPaint. | 69 // ensures that the ScopedDecodedImageLock outlives the dependent PaintFlags. |
70 class ScopedImagePaint { | 70 class ScopedImagePaint { |
71 public: | 71 public: |
72 // Tries to create a ScopedImagePaint for the provided SkPaint. If a | 72 // Tries to create a ScopedImagePaint for the provided PaintFlags. If a |
73 // the SkPaint does not contain an image that we support replacing, | 73 // the PaintFlags does not contain an image that we support replacing, |
74 // an empty base::Optional will be returned. | 74 // an empty base::Optional will be returned. |
75 static base::Optional<ScopedImagePaint> TryCreate( | 75 static base::Optional<ScopedImagePaint> TryCreate( |
76 ImageDecodeCache* image_decode_cache, | 76 ImageDecodeCache* image_decode_cache, |
77 const SkMatrix& ctm, | 77 const SkMatrix& ctm, |
78 const SkPaint& paint) { | 78 const PaintFlags& paint) { |
79 SkShader* shader = paint.getShader(); | 79 SkShader* shader = paint.getShader(); |
80 if (!shader) | 80 if (!shader) |
81 return base::Optional<ScopedImagePaint>(); | 81 return base::Optional<ScopedImagePaint>(); |
82 | 82 |
83 SkMatrix matrix; | 83 SkMatrix matrix; |
84 SkShader::TileMode xy[2]; | 84 SkShader::TileMode xy[2]; |
85 SkImage* image = shader->isAImage(&matrix, xy); | 85 SkImage* image = shader->isAImage(&matrix, xy); |
86 if (!image || !image->isLazyGenerated()) | 86 if (!image || !image->isLazyGenerated()) |
87 return base::Optional<ScopedImagePaint>(); | 87 return base::Optional<ScopedImagePaint>(); |
88 | 88 |
89 SkMatrix total_image_matrix = matrix; | 89 SkMatrix total_image_matrix = matrix; |
90 total_image_matrix.preConcat(ctm); | 90 total_image_matrix.preConcat(ctm); |
91 | 91 |
92 ScopedDecodedImageLock scoped_lock( | 92 ScopedDecodedImageLock scoped_lock( |
93 image_decode_cache, sk_ref_sp(image), | 93 image_decode_cache, sk_ref_sp(image), |
94 SkRect::MakeIWH(image->width(), image->height()), total_image_matrix, | 94 SkRect::MakeIWH(image->width(), image->height()), total_image_matrix, |
95 &paint); | 95 &paint); |
96 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | 96 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); |
97 if (!decoded_image.image()) | 97 if (!decoded_image.image()) |
98 return base::Optional<ScopedImagePaint>(); | 98 return base::Optional<ScopedImagePaint>(); |
99 | 99 |
100 bool need_scale = !decoded_image.is_scale_adjustment_identity(); | 100 bool need_scale = !decoded_image.is_scale_adjustment_identity(); |
101 if (need_scale) { | 101 if (need_scale) { |
102 matrix.preScale(1.f / decoded_image.scale_adjustment().width(), | 102 matrix.preScale(1.f / decoded_image.scale_adjustment().width(), |
103 1.f / decoded_image.scale_adjustment().height()); | 103 1.f / decoded_image.scale_adjustment().height()); |
104 } | 104 } |
105 SkPaint scratch_paint = paint; | 105 PaintFlags scratch_paint = paint; |
106 scratch_paint.setShader( | 106 scratch_paint.setShader( |
107 decoded_image.image()->makeShader(xy[0], xy[1], &matrix)); | 107 decoded_image.image()->makeShader(xy[0], xy[1], &matrix)); |
108 return ScopedImagePaint(std::move(scoped_lock), std::move(scratch_paint)); | 108 return ScopedImagePaint(std::move(scoped_lock), std::move(scratch_paint)); |
109 } | 109 } |
110 | 110 |
111 const SkPaint& paint() { return paint_; } | 111 const PaintFlags& paint() { return paint_; } |
112 | 112 |
113 private: | 113 private: |
114 ScopedImagePaint(ScopedDecodedImageLock lock, SkPaint paint) | 114 ScopedImagePaint(ScopedDecodedImageLock lock, PaintFlags paint) |
115 : lock_(std::move(lock)), paint_(std::move(paint)) {} | 115 : lock_(std::move(lock)), paint_(std::move(paint)) {} |
116 | 116 |
117 ScopedDecodedImageLock lock_; | 117 ScopedDecodedImageLock lock_; |
118 SkPaint paint_; | 118 PaintFlags paint_; |
119 }; | 119 }; |
120 | 120 |
121 } // namespace | 121 } // namespace |
122 | 122 |
123 ImageHijackCanvas::ImageHijackCanvas(int width, | 123 ImageHijackCanvas::ImageHijackCanvas(int width, |
124 int height, | 124 int height, |
125 ImageDecodeCache* image_decode_cache) | 125 ImageDecodeCache* image_decode_cache) |
126 : SkNWayCanvas(width, height), image_decode_cache_(image_decode_cache) {} | 126 : PaintCanvasPassThrough(width, height), |
| 127 image_decode_cache_(image_decode_cache) {} |
127 | 128 |
128 void ImageHijackCanvas::onDrawPicture(const SkPicture* picture, | 129 void ImageHijackCanvas::onDrawPicture(const PaintRecord* picture, |
129 const SkMatrix* matrix, | 130 const SkMatrix* matrix, |
130 const SkPaint* paint) { | 131 const PaintFlags* paint) { |
131 // Ensure that pictures are unpacked by this canvas, instead of being | 132 // Ensure that pictures are unpacked by this canvas, instead of being |
132 // forwarded to the raster canvas. | 133 // forwarded to the raster canvas. |
133 SkCanvas::onDrawPicture(picture, matrix, paint); | 134 PaintCanvas::onDrawPicture(picture, matrix, paint); |
134 } | 135 } |
135 | 136 |
136 void ImageHijackCanvas::onDrawImage(const SkImage* image, | 137 void ImageHijackCanvas::onDrawImage(const SkImage* image, |
137 SkScalar x, | 138 SkScalar x, |
138 SkScalar y, | 139 SkScalar y, |
139 const SkPaint* paint) { | 140 const PaintFlags* paint) { |
140 if (!image->isLazyGenerated()) { | 141 if (!image->isLazyGenerated()) { |
141 SkNWayCanvas::onDrawImage(image, x, y, paint); | 142 PaintCanvasPassThrough::onDrawImage(image, x, y, paint); |
142 return; | 143 return; |
143 } | 144 } |
144 | 145 |
145 SkMatrix ctm = getTotalMatrix(); | 146 SkMatrix ctm = getTotalMatrix(); |
146 | 147 |
147 ScopedDecodedImageLock scoped_lock( | 148 ScopedDecodedImageLock scoped_lock( |
148 image_decode_cache_, sk_ref_sp(image), | 149 image_decode_cache_, sk_ref_sp(image), |
149 SkRect::MakeIWH(image->width(), image->height()), ctm, paint); | 150 SkRect::MakeIWH(image->width(), image->height()), ctm, paint); |
150 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | 151 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); |
151 if (!decoded_image.image()) | 152 if (!decoded_image.image()) |
152 return; | 153 return; |
153 | 154 |
154 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width())); | 155 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width())); |
155 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height())); | 156 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height())); |
156 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | 157 const PaintFlags* decoded_paint = scoped_lock.decoded_paint(); |
157 | 158 |
158 bool need_scale = !decoded_image.is_scale_adjustment_identity(); | 159 bool need_scale = !decoded_image.is_scale_adjustment_identity(); |
159 if (need_scale) { | 160 if (need_scale) { |
160 SkNWayCanvas::save(); | 161 PaintCanvasPassThrough::save(); |
161 SkNWayCanvas::scale(1.f / (decoded_image.scale_adjustment().width()), | 162 PaintCanvasPassThrough::scale( |
162 1.f / (decoded_image.scale_adjustment().height())); | 163 1.f / (decoded_image.scale_adjustment().width()), |
| 164 1.f / (decoded_image.scale_adjustment().height())); |
163 } | 165 } |
164 SkNWayCanvas::onDrawImage(decoded_image.image().get(), x, y, decoded_paint); | 166 PaintCanvasPassThrough::onDrawImage(decoded_image.image().get(), x, y, |
| 167 decoded_paint); |
165 if (need_scale) | 168 if (need_scale) |
166 SkNWayCanvas::restore(); | 169 PaintCanvasPassThrough::restore(); |
167 } | 170 } |
168 | 171 |
169 void ImageHijackCanvas::onDrawImageRect(const SkImage* image, | 172 void ImageHijackCanvas::onDrawImageRect(const SkImage* image, |
170 const SkRect* src, | 173 const SkRect* src, |
171 const SkRect& dst, | 174 const SkRect& dst, |
172 const SkPaint* paint, | 175 const PaintFlags* paint, |
173 SrcRectConstraint constraint) { | 176 SrcRectConstraint constraint) { |
174 if (!image->isLazyGenerated()) { | 177 if (!image->isLazyGenerated()) { |
175 SkNWayCanvas::onDrawImageRect(image, src, dst, paint, constraint); | 178 PaintCanvasPassThrough::onDrawImageRect(image, src, dst, paint, constraint); |
176 return; | 179 return; |
177 } | 180 } |
178 | 181 |
179 SkRect src_storage; | 182 SkRect src_storage; |
180 if (!src) { | 183 if (!src) { |
181 src_storage = SkRect::MakeIWH(image->width(), image->height()); | 184 src_storage = SkRect::MakeIWH(image->width(), image->height()); |
182 src = &src_storage; | 185 src = &src_storage; |
183 } | 186 } |
184 SkMatrix matrix; | 187 SkMatrix matrix; |
185 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); | 188 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); |
186 matrix.postConcat(getTotalMatrix()); | 189 matrix.postConcat(getTotalMatrix()); |
187 | 190 |
188 ScopedDecodedImageLock scoped_lock(image_decode_cache_, sk_ref_sp(image), | 191 ScopedDecodedImageLock scoped_lock(image_decode_cache_, sk_ref_sp(image), |
189 *src, matrix, paint); | 192 *src, matrix, paint); |
190 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); | 193 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); |
191 if (!decoded_image.image()) | 194 if (!decoded_image.image()) |
192 return; | 195 return; |
193 | 196 |
194 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); | 197 const PaintFlags* decoded_paint = scoped_lock.decoded_paint(); |
195 | 198 |
196 SkRect adjusted_src = | 199 SkRect adjusted_src = |
197 src->makeOffset(decoded_image.src_rect_offset().width(), | 200 src->makeOffset(decoded_image.src_rect_offset().width(), |
198 decoded_image.src_rect_offset().height()); | 201 decoded_image.src_rect_offset().height()); |
199 if (!decoded_image.is_scale_adjustment_identity()) { | 202 if (!decoded_image.is_scale_adjustment_identity()) { |
200 float x_scale = decoded_image.scale_adjustment().width(); | 203 float x_scale = decoded_image.scale_adjustment().width(); |
201 float y_scale = decoded_image.scale_adjustment().height(); | 204 float y_scale = decoded_image.scale_adjustment().height(); |
202 adjusted_src = SkRect::MakeXYWH( | 205 adjusted_src = SkRect::MakeXYWH( |
203 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale, | 206 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale, |
204 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale); | 207 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale); |
205 } | 208 } |
206 SkNWayCanvas::onDrawImageRect(decoded_image.image().get(), &adjusted_src, dst, | 209 PaintCanvasPassThrough::onDrawImageRect(decoded_image.image().get(), |
207 decoded_paint, constraint); | 210 &adjusted_src, dst, decoded_paint, |
| 211 constraint); |
208 } | 212 } |
209 | 213 |
210 void ImageHijackCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) { | 214 void ImageHijackCanvas::onDrawRect(const SkRect& r, const PaintFlags& paint) { |
211 base::Optional<ScopedImagePaint> image_paint = | 215 base::Optional<ScopedImagePaint> image_paint = |
212 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); | 216 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); |
213 if (!image_paint.has_value()) { | 217 if (!image_paint.has_value()) { |
214 SkNWayCanvas::onDrawRect(r, paint); | 218 PaintCanvasPassThrough::onDrawRect(r, paint); |
215 return; | 219 return; |
216 } | 220 } |
217 SkNWayCanvas::onDrawRect(r, image_paint.value().paint()); | 221 PaintCanvasPassThrough::onDrawRect(r, image_paint.value().paint()); |
218 } | 222 } |
219 | 223 |
220 void ImageHijackCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { | 224 void ImageHijackCanvas::onDrawPath(const SkPath& path, |
| 225 const PaintFlags& paint) { |
221 base::Optional<ScopedImagePaint> image_paint = | 226 base::Optional<ScopedImagePaint> image_paint = |
222 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); | 227 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); |
223 if (!image_paint.has_value()) { | 228 if (!image_paint.has_value()) { |
224 SkNWayCanvas::onDrawPath(path, paint); | 229 PaintCanvasPassThrough::onDrawPath(path, paint); |
225 return; | 230 return; |
226 } | 231 } |
227 SkNWayCanvas::onDrawPath(path, image_paint.value().paint()); | 232 PaintCanvasPassThrough::onDrawPath(path, image_paint.value().paint()); |
228 } | 233 } |
229 | 234 |
230 void ImageHijackCanvas::onDrawOval(const SkRect& r, const SkPaint& paint) { | 235 void ImageHijackCanvas::onDrawOval(const SkRect& r, const PaintFlags& paint) { |
231 base::Optional<ScopedImagePaint> image_paint = | 236 base::Optional<ScopedImagePaint> image_paint = |
232 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); | 237 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); |
233 if (!image_paint.has_value()) { | 238 if (!image_paint.has_value()) { |
234 SkNWayCanvas::onDrawOval(r, paint); | 239 PaintCanvasPassThrough::onDrawOval(r, paint); |
235 return; | 240 return; |
236 } | 241 } |
237 SkNWayCanvas::onDrawOval(r, image_paint.value().paint()); | 242 PaintCanvasPassThrough::onDrawOval(r, image_paint.value().paint()); |
238 } | 243 } |
239 | 244 |
240 void ImageHijackCanvas::onDrawArc(const SkRect& r, | 245 void ImageHijackCanvas::onDrawArc(const SkRect& r, |
241 SkScalar start_angle, | 246 SkScalar start_angle, |
242 SkScalar sweep_angle, | 247 SkScalar sweep_angle, |
243 bool use_center, | 248 bool use_center, |
244 const SkPaint& paint) { | 249 const PaintFlags& paint) { |
245 base::Optional<ScopedImagePaint> image_paint = | 250 base::Optional<ScopedImagePaint> image_paint = |
246 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); | 251 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); |
247 if (!image_paint.has_value()) { | 252 if (!image_paint.has_value()) { |
248 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center, paint); | 253 PaintCanvasPassThrough::onDrawArc(r, start_angle, sweep_angle, use_center, |
| 254 paint); |
249 return; | 255 return; |
250 } | 256 } |
251 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center, | 257 PaintCanvasPassThrough::onDrawArc(r, start_angle, sweep_angle, use_center, |
252 image_paint.value().paint()); | 258 image_paint.value().paint()); |
253 } | 259 } |
254 | 260 |
255 void ImageHijackCanvas::onDrawRRect(const SkRRect& rr, const SkPaint& paint) { | 261 void ImageHijackCanvas::onDrawRRect(const SkRRect& rr, |
| 262 const PaintFlags& paint) { |
256 base::Optional<ScopedImagePaint> image_paint = | 263 base::Optional<ScopedImagePaint> image_paint = |
257 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); | 264 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); |
258 if (!image_paint.has_value()) { | 265 if (!image_paint.has_value()) { |
259 SkNWayCanvas::onDrawRRect(rr, paint); | 266 PaintCanvasPassThrough::onDrawRRect(rr, paint); |
260 return; | 267 return; |
261 } | 268 } |
262 SkNWayCanvas::onDrawRRect(rr, image_paint.value().paint()); | 269 PaintCanvasPassThrough::onDrawRRect(rr, image_paint.value().paint()); |
263 } | 270 } |
264 | 271 |
265 void ImageHijackCanvas::onDrawImageNine(const SkImage* image, | 272 void ImageHijackCanvas::onDrawImageNine(const SkImage* image, |
266 const SkIRect& center, | 273 const SkIRect& center, |
267 const SkRect& dst, | 274 const SkRect& dst, |
268 const SkPaint* paint) { | 275 const PaintFlags* paint) { |
269 // No cc embedder issues image nine calls. | 276 // No cc embedder issues image nine calls. |
270 NOTREACHED(); | 277 NOTREACHED(); |
271 } | 278 } |
272 | 279 |
273 } // namespace cc | 280 } // namespace cc |
OLD | NEW |