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

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

Issue 2514263002: Handle simple SkImageShaders (Closed)
Patch Set: fix unit test Created 4 years 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
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/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 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
(...skipping 11 matching lines...) Expand all
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
42 ScopedDecodedImageLock(ScopedDecodedImageLock&& from)
43 : image_decode_cache_(from.image_decode_cache_),
44 draw_image_(std::move(from.draw_image_)),
45 decoded_draw_image_(std::move(from.decoded_draw_image_)),
46 decoded_paint_(std::move(from.decoded_paint_)) {
47 from.image_decode_cache_ = nullptr;
48 }
49
41 ~ScopedDecodedImageLock() { 50 ~ScopedDecodedImageLock() {
42 image_decode_cache_->DrawWithImageFinished(draw_image_, 51 if (image_decode_cache_)
43 decoded_draw_image_); 52 image_decode_cache_->DrawWithImageFinished(draw_image_,
53 decoded_draw_image_);
44 } 54 }
45 55
46 const DecodedDrawImage& decoded_image() const { return decoded_draw_image_; } 56 const DecodedDrawImage& decoded_image() const { return decoded_draw_image_; }
47 const SkPaint* decoded_paint() const { 57 const SkPaint* decoded_paint() const {
48 return decoded_paint_ ? &decoded_paint_.value() : nullptr; 58 return decoded_paint_ ? &decoded_paint_.value() : nullptr;
49 } 59 }
50 60
51 private: 61 private:
52 ImageDecodeCache* image_decode_cache_; 62 ImageDecodeCache* image_decode_cache_;
53 DrawImage draw_image_; 63 DrawImage draw_image_;
54 DecodedDrawImage decoded_draw_image_; 64 DecodedDrawImage decoded_draw_image_;
55 base::Optional<SkPaint> decoded_paint_; 65 base::Optional<SkPaint> decoded_paint_;
56 }; 66 };
57 67
68 // Encapsulates a ScopedDecodedImageLock and an SkPaint. Use of this class
69 // ensures that the ScopedDecodedImageLock outlives the dependent SkPaint.
70 class ScopedImagePaint {
71 public:
72 // Tries to create a ScopedImagePaint for the provided SkPaint. If a
73 // the SkPaint does not contain an image that we support replacing,
74 // an empty base::Optional will be returned.
75 static base::Optional<ScopedImagePaint> TryCreate(
76 ImageDecodeCache* image_decode_cache,
77 const SkMatrix& ctm,
78 const SkPaint& paint) {
79 SkShader* shader = paint.getShader();
80 if (!shader)
81 return base::Optional<ScopedImagePaint>();
82
83 SkMatrix matrix;
84 SkShader::TileMode xy[2];
85 SkImage* image = shader->isAImage(&matrix, xy);
86 if (!image)
87 return base::Optional<ScopedImagePaint>();
88
89 SkMatrix total_image_matrix = matrix;
90 total_image_matrix.preConcat(ctm);
91
92 ScopedDecodedImageLock scoped_lock(
93 image_decode_cache, sk_ref_sp(image),
94 SkRect::MakeIWH(image->width(), image->height()), total_image_matrix,
95 &paint);
96 const DecodedDrawImage& decoded_image = scoped_lock.decoded_image();
97 if (!decoded_image.image())
98 return base::Optional<ScopedImagePaint>();
99
100 bool need_scale = !decoded_image.is_scale_adjustment_identity();
101 if (need_scale) {
102 matrix.preScale(1.f / decoded_image.scale_adjustment().width(),
103 1.f / decoded_image.scale_adjustment().height());
104 }
105 SkPaint scratch_paint = paint;
106 scratch_paint.setShader(
107 decoded_image.image()->makeShader(xy[0], xy[1], &matrix));
108 return ScopedImagePaint(std::move(scoped_lock), std::move(scratch_paint));
109 }
110
111 const SkPaint& paint() { return paint_; }
112
113 private:
114 ScopedImagePaint(ScopedDecodedImageLock lock, SkPaint paint)
115 : lock_(std::move(lock)), paint_(std::move(paint)) {}
116
117 ScopedDecodedImageLock lock_;
118 SkPaint paint_;
119 };
120
58 } // namespace 121 } // namespace
59 122
60 ImageHijackCanvas::ImageHijackCanvas(int width, 123 ImageHijackCanvas::ImageHijackCanvas(int width,
61 int height, 124 int height,
62 ImageDecodeCache* image_decode_cache) 125 ImageDecodeCache* image_decode_cache)
63 : SkNWayCanvas(width, height), image_decode_cache_(image_decode_cache) {} 126 : SkNWayCanvas(width, height), image_decode_cache_(image_decode_cache) {}
64 127
65 void ImageHijackCanvas::onDrawPicture(const SkPicture* picture, 128 void ImageHijackCanvas::onDrawPicture(const SkPicture* picture,
66 const SkMatrix* matrix, 129 const SkMatrix* matrix,
67 const SkPaint* paint) { 130 const SkPaint* paint) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 float x_scale = decoded_image.scale_adjustment().width(); 200 float x_scale = decoded_image.scale_adjustment().width();
138 float y_scale = decoded_image.scale_adjustment().height(); 201 float y_scale = decoded_image.scale_adjustment().height();
139 adjusted_src = SkRect::MakeXYWH( 202 adjusted_src = SkRect::MakeXYWH(
140 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale, 203 adjusted_src.x() * x_scale, adjusted_src.y() * y_scale,
141 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale); 204 adjusted_src.width() * x_scale, adjusted_src.height() * y_scale);
142 } 205 }
143 SkNWayCanvas::onDrawImageRect(decoded_image.image().get(), &adjusted_src, dst, 206 SkNWayCanvas::onDrawImageRect(decoded_image.image().get(), &adjusted_src, dst,
144 decoded_paint, constraint); 207 decoded_paint, constraint);
145 } 208 }
146 209
210 void ImageHijackCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) {
211 base::Optional<ScopedImagePaint> image_paint =
212 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint);
213 if (!image_paint.has_value()) {
214 SkNWayCanvas::onDrawRect(r, paint);
215 return;
216 }
217 SkNWayCanvas::onDrawRect(r, image_paint.value().paint());
218 }
219
220 void ImageHijackCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
221 base::Optional<ScopedImagePaint> image_paint =
222 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint);
223 if (!image_paint.has_value()) {
224 SkNWayCanvas::onDrawPath(path, paint);
225 return;
226 }
227 SkNWayCanvas::onDrawPath(path, image_paint.value().paint());
228 }
229
230 void ImageHijackCanvas::onDrawOval(const SkRect& r, const SkPaint& paint) {
231 base::Optional<ScopedImagePaint> image_paint =
232 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint);
233 if (!image_paint.has_value()) {
234 SkNWayCanvas::onDrawOval(r, paint);
235 return;
236 }
237 SkNWayCanvas::onDrawOval(r, image_paint.value().paint());
238 }
239
240 void ImageHijackCanvas::onDrawArc(const SkRect& r,
241 SkScalar start_angle,
242 SkScalar sweep_angle,
243 bool use_center,
244 const SkPaint& paint) {
245 base::Optional<ScopedImagePaint> image_paint =
246 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint);
247 if (!image_paint.has_value()) {
248 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center, paint);
249 return;
250 }
251 SkNWayCanvas::onDrawArc(r, start_angle, sweep_angle, use_center,
252 image_paint.value().paint());
253 }
254
255 void ImageHijackCanvas::onDrawRRect(const SkRRect& rr, const SkPaint& paint) {
256 base::Optional<ScopedImagePaint> image_paint =
257 ScopedImagePaint::TryCreate(image_decode_cache_, getTotalMatrix(), paint);
258 if (!image_paint.has_value()) {
259 SkNWayCanvas::onDrawRRect(rr, paint);
260 return;
261 }
262 SkNWayCanvas::onDrawRRect(rr, image_paint.value().paint());
263 }
264
147 void ImageHijackCanvas::onDrawImageNine(const SkImage* image, 265 void ImageHijackCanvas::onDrawImageNine(const SkImage* image,
148 const SkIRect& center, 266 const SkIRect& center,
149 const SkRect& dst, 267 const SkRect& dst,
150 const SkPaint* paint) { 268 const SkPaint* paint) {
151 // No cc embedder issues image nine calls. 269 // No cc embedder issues image nine calls.
152 NOTREACHED(); 270 NOTREACHED();
153 } 271 }
154 272
155 } // namespace cc 273 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698