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

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

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

Powered by Google App Engine
This is Rietveld 408576698