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

Side by Side Diff: cc/raster/image_hijack_canvas.cc

Issue 2797583002: cc: Add color space to image decode caches (Closed)
Patch Set: Remove dead code Created 3 years, 8 months 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
« no previous file with comments | « cc/raster/image_hijack_canvas.h ('k') | cc/raster/image_hijack_canvas_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/raster/image_hijack_canvas.h" 5 #include "cc/raster/image_hijack_canvas.h"
6 6
7 #include "base/optional.h" 7 #include "base/optional.h"
8 #include "base/trace_event/trace_event.h" 8 #include "base/trace_event/trace_event.h"
9 #include "cc/paint/discardable_image_map.h" 9 #include "cc/paint/discardable_image_map.h"
10 #include "cc/tiles/image_decode_cache.h" 10 #include "cc/tiles/image_decode_cache.h"
11 #include "third_party/skia/include/core/SkPath.h" 11 #include "third_party/skia/include/core/SkPath.h"
12 12
13 namespace cc { 13 namespace cc {
14 namespace { 14 namespace {
15 15
16 SkIRect RoundOutRect(const SkRect& rect) { 16 SkIRect RoundOutRect(const SkRect& rect) {
17 SkIRect result; 17 SkIRect result;
18 rect.roundOut(&result); 18 rect.roundOut(&result);
19 return result; 19 return result;
20 } 20 }
21 21
22 class ScopedDecodedImageLock { 22 class ScopedDecodedImageLock {
23 public: 23 public:
24 ScopedDecodedImageLock(ImageDecodeCache* image_decode_cache, 24 ScopedDecodedImageLock(ImageDecodeCache* image_decode_cache,
25 sk_sp<const SkImage> image, 25 sk_sp<const SkImage> image,
26 const SkRect& src_rect, 26 const SkRect& src_rect,
27 const SkMatrix& matrix, 27 const SkMatrix& matrix,
28 const SkPaint* paint) 28 const SkPaint* paint,
29 const gfx::ColorSpace& target_color_space)
29 : image_decode_cache_(image_decode_cache), 30 : image_decode_cache_(image_decode_cache),
30 draw_image_(std::move(image), 31 draw_image_(std::move(image),
31 RoundOutRect(src_rect), 32 RoundOutRect(src_rect),
32 paint ? paint->getFilterQuality() : kNone_SkFilterQuality, 33 paint ? paint->getFilterQuality() : kNone_SkFilterQuality,
33 matrix), 34 matrix,
35 target_color_space),
34 decoded_draw_image_( 36 decoded_draw_image_(
35 image_decode_cache_->GetDecodedImageForDraw(draw_image_)) { 37 image_decode_cache_->GetDecodedImageForDraw(draw_image_)) {
36 DCHECK(draw_image_.image()->isLazyGenerated()); 38 DCHECK(draw_image_.image()->isLazyGenerated());
37 if (paint) { 39 if (paint) {
38 decoded_paint_ = *paint; 40 decoded_paint_ = *paint;
39 decoded_paint_->setFilterQuality(decoded_draw_image_.filter_quality()); 41 decoded_paint_->setFilterQuality(decoded_draw_image_.filter_quality());
40 } 42 }
41 } 43 }
42 44
43 ScopedDecodedImageLock(ScopedDecodedImageLock&& from) 45 ScopedDecodedImageLock(ScopedDecodedImageLock&& from)
(...skipping 25 matching lines...) Expand all
69 // Encapsulates a ScopedDecodedImageLock and an SkPaint. Use of this class 71 // Encapsulates a ScopedDecodedImageLock and an SkPaint. Use of this class
70 // ensures that the ScopedDecodedImageLock outlives the dependent SkPaint. 72 // ensures that the ScopedDecodedImageLock outlives the dependent SkPaint.
71 class ScopedImagePaint { 73 class ScopedImagePaint {
72 public: 74 public:
73 // Tries to create a ScopedImagePaint for the provided SkPaint. If a 75 // Tries to create a ScopedImagePaint for the provided SkPaint. If a
74 // the SkPaint does not contain an image that we support replacing, 76 // the SkPaint does not contain an image that we support replacing,
75 // an empty base::Optional will be returned. 77 // an empty base::Optional will be returned.
76 static base::Optional<ScopedImagePaint> TryCreate( 78 static base::Optional<ScopedImagePaint> TryCreate(
77 ImageDecodeCache* image_decode_cache, 79 ImageDecodeCache* image_decode_cache,
78 const SkMatrix& ctm, 80 const SkMatrix& ctm,
79 const SkPaint& paint) { 81 const SkPaint& paint,
82 const gfx::ColorSpace& target_color_space) {
80 SkShader* shader = paint.getShader(); 83 SkShader* shader = paint.getShader();
81 if (!shader) 84 if (!shader)
82 return base::Optional<ScopedImagePaint>(); 85 return base::Optional<ScopedImagePaint>();
83 86
84 SkMatrix matrix; 87 SkMatrix matrix;
85 SkShader::TileMode xy[2]; 88 SkShader::TileMode xy[2];
86 SkImage* image = shader->isAImage(&matrix, xy); 89 SkImage* image = shader->isAImage(&matrix, xy);
87 if (!image || !image->isLazyGenerated()) 90 if (!image || !image->isLazyGenerated())
88 return base::Optional<ScopedImagePaint>(); 91 return base::Optional<ScopedImagePaint>();
89 92
90 SkMatrix total_image_matrix = matrix; 93 SkMatrix total_image_matrix = matrix;
91 total_image_matrix.preConcat(ctm); 94 total_image_matrix.preConcat(ctm);
92 95
93 ScopedDecodedImageLock scoped_lock( 96 ScopedDecodedImageLock scoped_lock(
94 image_decode_cache, sk_ref_sp(image), 97 image_decode_cache, sk_ref_sp(image),
95 SkRect::MakeIWH(image->width(), image->height()), total_image_matrix, 98 SkRect::MakeIWH(image->width(), image->height()), total_image_matrix,
96 &paint); 99 &paint, target_color_space);
97 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); 100 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
98 if (!decoded_image.image()) 101 if (!decoded_image.image())
99 return base::Optional<ScopedImagePaint>(); 102 return base::Optional<ScopedImagePaint>();
100 103
101 bool need_scale = !decoded_image.is_scale_adjustment_identity(); 104 bool need_scale = !decoded_image.is_scale_adjustment_identity();
102 if (need_scale) { 105 if (need_scale) {
103 matrix.preScale(1.f / decoded_image.scale_adjustment().width(), 106 matrix.preScale(1.f / decoded_image.scale_adjustment().width(),
104 1.f / decoded_image.scale_adjustment().height()); 107 1.f / decoded_image.scale_adjustment().height());
105 } 108 }
106 SkPaint scratch_paint = paint; 109 SkPaint scratch_paint = paint;
(...skipping 16 matching lines...) Expand all
123 const SkImage* GetImageInPaint(const SkPaint& paint) { 126 const SkImage* GetImageInPaint(const SkPaint& paint) {
124 SkShader* shader = paint.getShader(); 127 SkShader* shader = paint.getShader();
125 return shader ? shader->isAImage(nullptr, nullptr) : nullptr; 128 return shader ? shader->isAImage(nullptr, nullptr) : nullptr;
126 } 129 }
127 130
128 } // namespace 131 } // namespace
129 132
130 ImageHijackCanvas::ImageHijackCanvas(int width, 133 ImageHijackCanvas::ImageHijackCanvas(int width,
131 int height, 134 int height,
132 ImageDecodeCache* image_decode_cache, 135 ImageDecodeCache* image_decode_cache,
133 const ImageIdFlatSet* images_to_skip) 136 const ImageIdFlatSet* images_to_skip,
137 const gfx::ColorSpace& target_color_space)
134 : SkNWayCanvas(width, height), 138 : SkNWayCanvas(width, height),
135 image_decode_cache_(image_decode_cache), 139 image_decode_cache_(image_decode_cache),
136 images_to_skip_(images_to_skip) {} 140 images_to_skip_(images_to_skip),
141 target_color_space_(target_color_space) {}
137 142
138 void ImageHijackCanvas::onDrawPicture(const SkPicture* picture, 143 void ImageHijackCanvas::onDrawPicture(const SkPicture* picture,
139 const SkMatrix* matrix, 144 const SkMatrix* matrix,
140 const SkPaint* paint) { 145 const SkPaint* paint) {
141 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"), 146 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
142 "ImageHijackCanvas::onDrawPicture"); 147 "ImageHijackCanvas::onDrawPicture");
143 // Ensure that pictures are unpacked by this canvas, instead of being 148 // Ensure that pictures are unpacked by this canvas, instead of being
144 // forwarded to the raster canvas. 149 // forwarded to the raster canvas.
145 SkCanvas::onDrawPicture(picture, matrix, paint); 150 SkCanvas::onDrawPicture(picture, matrix, paint);
146 } 151 }
(...skipping 10 matching lines...) Expand all
157 return; 162 return;
158 } 163 }
159 164
160 if (ShouldSkipImage(image)) 165 if (ShouldSkipImage(image))
161 return; 166 return;
162 167
163 SkMatrix ctm = getTotalMatrix(); 168 SkMatrix ctm = getTotalMatrix();
164 169
165 ScopedDecodedImageLock scoped_lock( 170 ScopedDecodedImageLock scoped_lock(
166 image_decode_cache_, sk_ref_sp(image), 171 image_decode_cache_, sk_ref_sp(image),
167 SkRect::MakeIWH(image->width(), image->height()), ctm, paint); 172 SkRect::MakeIWH(image->width(), image->height()), ctm, paint,
173 target_color_space_);
168 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); 174 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
169 if (!decoded_image.image()) 175 if (!decoded_image.image())
170 return; 176 return;
171 177
172 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width())); 178 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().width()));
173 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height())); 179 DCHECK_EQ(0, static_cast<int>(decoded_image.src_rect_offset().height()));
174 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); 180 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
175 181
176 bool need_scale = !decoded_image.is_scale_adjustment_identity(); 182 bool need_scale = !decoded_image.is_scale_adjustment_identity();
177 if (need_scale) { 183 if (need_scale) {
(...skipping 25 matching lines...) Expand all
203 SkRect src_storage; 209 SkRect src_storage;
204 if (!src) { 210 if (!src) {
205 src_storage = SkRect::MakeIWH(image->width(), image->height()); 211 src_storage = SkRect::MakeIWH(image->width(), image->height());
206 src = &src_storage; 212 src = &src_storage;
207 } 213 }
208 SkMatrix matrix; 214 SkMatrix matrix;
209 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); 215 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit);
210 matrix.postConcat(getTotalMatrix()); 216 matrix.postConcat(getTotalMatrix());
211 217
212 ScopedDecodedImageLock scoped_lock(image_decode_cache_, sk_ref_sp(image), 218 ScopedDecodedImageLock scoped_lock(image_decode_cache_, sk_ref_sp(image),
213 *src, matrix, paint); 219 *src, matrix, paint, target_color_space_);
214 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image(); 220 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
215 if (!decoded_image.image()) 221 if (!decoded_image.image())
216 return; 222 return;
217 223
218 const SkPaint* decoded_paint = scoped_lock.decoded_paint(); 224 const SkPaint* decoded_paint = scoped_lock.decoded_paint();
219 225
220 SkRect adjusted_src = 226 SkRect adjusted_src =
221 src->makeOffset(decoded_image.src_rect_offset().width(), 227 src->makeOffset(decoded_image.src_rect_offset().width(),
222 decoded_image.src_rect_offset().height()); 228 decoded_image.src_rect_offset().height());
223 if (!decoded_image.is_scale_adjustment_identity()) { 229 if (!decoded_image.is_scale_adjustment_identity()) {
224 float x_scale = decoded_image.scale_adjustment().width(); 230 float x_scale = decoded_image.scale_adjustment().width();
225 float y_scale = decoded_image.scale_adjustment().height(); 231 float y_scale = decoded_image.scale_adjustment().height();
226 adjusted_src = SkRect::MakeXYWH( 232 adjusted_src = SkRect::MakeXYWH(
227 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale, 233 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale,
228 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale); 234 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale);
229 } 235 }
230 SkNWayCanvas::onDrawImageRect(decoded_image.image().get(), &adjusted_src, dst, 236 SkNWayCanvas::onDrawImageRect(decoded_image.image().get(), &adjusted_src, dst,
231 decoded_paint, constraint); 237 decoded_paint, constraint);
232 } 238 }
233 239
234 void ImageHijackCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) { 240 void ImageHijackCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) {
235 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"), 241 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
236 "ImageHijackCanvas::onDrawRect"); 242 "ImageHijackCanvas::onDrawRect");
237 if (ShouldSkipImageInPaint(paint)) 243 if (ShouldSkipImageInPaint(paint))
238 return; 244 return;
239 245
240 base::Optional<ScopedImagePaint> image_paint = 246 base::Optional<ScopedImagePaint> image_paint = ScopedImagePaint::TryCreate(
241 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); 247 image_decode_cache_, getTotalMatrix(), paint, target_color_space_);
242 if (!image_paint.has_value()) { 248 if (!image_paint.has_value()) {
243 SkNWayCanvas::onDrawRect(r, paint); 249 SkNWayCanvas::onDrawRect(r, paint);
244 return; 250 return;
245 } 251 }
246 SkNWayCanvas::onDrawRect(r, image_paint.value().paint()); 252 SkNWayCanvas::onDrawRect(r, image_paint.value().paint());
247 } 253 }
248 254
249 void ImageHijackCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { 255 void ImageHijackCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
250 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"), 256 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
251 "ImageHijackCanvas::onDrawPath"); 257 "ImageHijackCanvas::onDrawPath");
252 if (ShouldSkipImageInPaint(paint)) 258 if (ShouldSkipImageInPaint(paint))
253 return; 259 return;
254 260
255 base::Optional<ScopedImagePaint> image_paint = 261 base::Optional<ScopedImagePaint> image_paint = ScopedImagePaint::TryCreate(
256 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); 262 image_decode_cache_, getTotalMatrix(), paint, target_color_space_);
257 if (!image_paint.has_value()) { 263 if (!image_paint.has_value()) {
258 SkNWayCanvas::onDrawPath(path, paint); 264 SkNWayCanvas::onDrawPath(path, paint);
259 return; 265 return;
260 } 266 }
261 SkNWayCanvas::onDrawPath(path, image_paint.value().paint()); 267 SkNWayCanvas::onDrawPath(path, image_paint.value().paint());
262 } 268 }
263 269
264 void ImageHijackCanvas::onDrawOval(const SkRect& r, const SkPaint& paint) { 270 void ImageHijackCanvas::onDrawOval(const SkRect& r, const SkPaint& paint) {
265 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"), 271 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
266 "ImageHijackCanvas::onDrawOval"); 272 "ImageHijackCanvas::onDrawOval");
267 if (ShouldSkipImageInPaint(paint)) 273 if (ShouldSkipImageInPaint(paint))
268 return; 274 return;
269 275
270 base::Optional<ScopedImagePaint> image_paint = 276 base::Optional<ScopedImagePaint> image_paint = ScopedImagePaint::TryCreate(
271 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); 277 image_decode_cache_, getTotalMatrix(), paint, target_color_space_);
272 if (!image_paint.has_value()) { 278 if (!image_paint.has_value()) {
273 SkNWayCanvas::onDrawOval(r, paint); 279 SkNWayCanvas::onDrawOval(r, paint);
274 return; 280 return;
275 } 281 }
276 SkNWayCanvas::onDrawOval(r, image_paint.value().paint()); 282 SkNWayCanvas::onDrawOval(r, image_paint.value().paint());
277 } 283 }
278 284
279 void ImageHijackCanvas::onDrawArc(const SkRect& r, 285 void ImageHijackCanvas::onDrawArc(const SkRect& r,
280 SkScalar start_angle, 286 SkScalar start_angle,
281 SkScalar sweep_angle, 287 SkScalar sweep_angle,
282 bool use_center, 288 bool use_center,
283 const SkPaint& paint) { 289 const SkPaint& paint) {
284 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"), 290 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
285 "ImageHijackCanvas::onDrawArc"); 291 "ImageHijackCanvas::onDrawArc");
286 if (ShouldSkipImageInPaint(paint)) 292 if (ShouldSkipImageInPaint(paint))
287 return; 293 return;
288 294
289 base::Optional<ScopedImagePaint> image_paint = 295 base::Optional<ScopedImagePaint> image_paint = ScopedImagePaint::TryCreate(
290 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); 296 image_decode_cache_, getTotalMatrix(), paint, target_color_space_);
291 if (!image_paint.has_value()) { 297 if (!image_paint.has_value()) {
292 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center, paint); 298 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center, paint);
293 return; 299 return;
294 } 300 }
295 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center, 301 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center,
296 image_paint.value().paint()); 302 image_paint.value().paint());
297 } 303 }
298 304
299 void ImageHijackCanvas::onDrawRRect(const SkRRect& rr, const SkPaint& paint) { 305 void ImageHijackCanvas::onDrawRRect(const SkRRect& rr, const SkPaint& paint) {
300 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"), 306 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
301 "ImageHijackCanvas::onDrawRRect"); 307 "ImageHijackCanvas::onDrawRRect");
302 if (ShouldSkipImageInPaint(paint)) 308 if (ShouldSkipImageInPaint(paint))
303 return; 309 return;
304 310
305 base::Optional<ScopedImagePaint> image_paint = 311 base::Optional<ScopedImagePaint> image_paint = ScopedImagePaint::TryCreate(
306 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint); 312 image_decode_cache_, getTotalMatrix(), paint, target_color_space_);
307 if (!image_paint.has_value()) { 313 if (!image_paint.has_value()) {
308 SkNWayCanvas::onDrawRRect(rr, paint); 314 SkNWayCanvas::onDrawRRect(rr, paint);
309 return; 315 return;
310 } 316 }
311 SkNWayCanvas::onDrawRRect(rr, image_paint.value().paint()); 317 SkNWayCanvas::onDrawRRect(rr, image_paint.value().paint());
312 } 318 }
313 319
314 void ImageHijackCanvas::onDrawImageNine(const SkImage* image, 320 void ImageHijackCanvas::onDrawImageNine(const SkImage* image,
315 const SkIRect& center, 321 const SkIRect& center,
316 const SkRect& dst, 322 const SkRect& dst,
(...skipping 10 matching lines...) Expand all
327 image->uniqueID(), "skip", skip); 333 image->uniqueID(), "skip", skip);
328 return skip; 334 return skip;
329 } 335 }
330 336
331 bool ImageHijackCanvas::ShouldSkipImageInPaint(const SkPaint& paint) const { 337 bool ImageHijackCanvas::ShouldSkipImageInPaint(const SkPaint& paint) const {
332 const SkImage* image = GetImageInPaint(paint); 338 const SkImage* image = GetImageInPaint(paint);
333 return image ? ShouldSkipImage(image) : false; 339 return image ? ShouldSkipImage(image) : false;
334 } 340 }
335 341
336 } // namespace cc 342 } // namespace cc
OLDNEW
« no previous file with comments | « cc/raster/image_hijack_canvas.h ('k') | cc/raster/image_hijack_canvas_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698