OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/discardable_image_map.h" | 5 #include "cc/playback/discardable_image_map.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <limits> | 10 #include <limits> |
11 | 11 |
12 #include "base/containers/adapters.h" | 12 #include "base/containers/adapters.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "cc/base/math_util.h" | 14 #include "cc/base/math_util.h" |
| 15 #include "cc/paint/paint_canvas.h" |
| 16 #include "cc/paint/paint_flags.h" |
15 #include "cc/playback/display_item_list.h" | 17 #include "cc/playback/display_item_list.h" |
16 #include "third_party/skia/include/core/SkPath.h" | 18 #include "third_party/skia/include/core/SkPath.h" |
17 #include "third_party/skia/include/utils/SkNWayCanvas.h" | |
18 #include "ui/gfx/geometry/rect_conversions.h" | 19 #include "ui/gfx/geometry/rect_conversions.h" |
19 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
20 | 21 |
21 namespace cc { | 22 namespace cc { |
22 | 23 |
23 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { | 24 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { |
24 SkRect dst; | 25 SkRect dst; |
25 matrix.mapRect(&dst, src); | 26 matrix.mapRect(&dst, src); |
26 return dst; | 27 return dst; |
27 } | 28 } |
(...skipping 22 matching lines...) Expand all Loading... |
50 max_size.width() - bounds_rect.x())); | 51 max_size.width() - bounds_rect.x())); |
51 bounds_rect.set_height(std::min(bounds_rect.height() + y_offset_if_negative, | 52 bounds_rect.set_height(std::min(bounds_rect.height() + y_offset_if_negative, |
52 max_size.height() - bounds_rect.y())); | 53 max_size.height() - bounds_rect.y())); |
53 return gfx::ToEnclosingRect(bounds_rect); | 54 return gfx::ToEnclosingRect(bounds_rect); |
54 } | 55 } |
55 | 56 |
56 namespace { | 57 namespace { |
57 | 58 |
58 // We're using an NWay canvas with no added canvases, so in effect | 59 // We're using an NWay canvas with no added canvases, so in effect |
59 // non-overridden functions are no-ops. | 60 // non-overridden functions are no-ops. |
60 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { | 61 class DiscardableImagesMetadataCanvas : public PaintCanvasNoDraw { |
61 public: | 62 public: |
62 DiscardableImagesMetadataCanvas( | 63 DiscardableImagesMetadataCanvas( |
63 int width, | 64 int width, |
64 int height, | 65 int height, |
65 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) | 66 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) |
66 : SkNWayCanvas(width, height), | 67 : PaintCanvasNoDraw(width, height), |
67 image_set_(image_set), | 68 image_set_(image_set), |
68 canvas_bounds_(SkRect::MakeIWH(width, height)), | 69 canvas_bounds_(SkRect::MakeIWH(width, height)), |
69 canvas_size_(width, height) {} | 70 canvas_size_(width, height) {} |
70 | 71 |
71 protected: | 72 protected: |
72 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward | 73 // we need to "undo" the behavior of PaintCanvasNoDraw, which will try to |
73 // it. | 74 // forward it. |
74 void onDrawPicture(const SkPicture* picture, | 75 void onDrawPicture(const PaintRecord* picture, |
75 const SkMatrix* matrix, | 76 const SkMatrix* matrix, |
76 const SkPaint* paint) override { | 77 const PaintFlags* paint) override { |
77 SkCanvas::onDrawPicture(picture, matrix, paint); | 78 PaintCanvas::onDrawPicture(picture, matrix, paint); |
78 } | 79 } |
79 | 80 |
80 void onDrawImage(const SkImage* image, | 81 void onDrawImage(const SkImage* image, |
81 SkScalar x, | 82 SkScalar x, |
82 SkScalar y, | 83 SkScalar y, |
83 const SkPaint* paint) override { | 84 const PaintFlags* paint) override { |
84 const SkMatrix& ctm = getTotalMatrix(); | 85 const SkMatrix& ctm = getTotalMatrix(); |
85 AddImage( | 86 AddImage( |
86 sk_ref_sp(image), SkRect::MakeIWH(image->width(), image->height()), | 87 sk_ref_sp(image), SkRect::MakeIWH(image->width(), image->height()), |
87 MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())), | 88 MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())), |
88 ctm, paint); | 89 ctm, paint); |
89 } | 90 } |
90 | 91 |
91 void onDrawImageRect(const SkImage* image, | 92 void onDrawImageRect(const SkImage* image, |
92 const SkRect* src, | 93 const SkRect* src, |
93 const SkRect& dst, | 94 const SkRect& dst, |
94 const SkPaint* paint, | 95 const PaintFlags* paint, |
95 SrcRectConstraint) override { | 96 SrcRectConstraint) override { |
96 const SkMatrix& ctm = getTotalMatrix(); | 97 const SkMatrix& ctm = getTotalMatrix(); |
97 SkRect src_storage; | 98 SkRect src_storage; |
98 if (!src) { | 99 if (!src) { |
99 src_storage = SkRect::MakeIWH(image->width(), image->height()); | 100 src_storage = SkRect::MakeIWH(image->width(), image->height()); |
100 src = &src_storage; | 101 src = &src_storage; |
101 } | 102 } |
102 SkMatrix matrix; | 103 SkMatrix matrix; |
103 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); | 104 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); |
104 matrix.preConcat(ctm); | 105 matrix.preConcat(ctm); |
105 AddImage(sk_ref_sp(image), *src, MapRect(ctm, dst), matrix, paint); | 106 AddImage(sk_ref_sp(image), *src, MapRect(ctm, dst), matrix, paint); |
106 } | 107 } |
107 | 108 |
108 void onDrawImageNine(const SkImage* image, | 109 void onDrawImageNine(const SkImage* image, |
109 const SkIRect& center, | 110 const SkIRect& center, |
110 const SkRect& dst, | 111 const SkRect& dst, |
111 const SkPaint* paint) override { | 112 const PaintFlags* paint) override { |
112 // No cc embedder issues image nine calls. | 113 // No cc embedder issues image nine calls. |
113 NOTREACHED(); | 114 NOTREACHED(); |
114 } | 115 } |
115 | 116 |
116 void onDrawRect(const SkRect& r, const SkPaint& paint) override { | 117 void onDrawRect(const SkRect& r, const PaintFlags& paint) override { |
117 AddPaintImage(r, paint); | 118 AddPaintImage(r, paint); |
118 } | 119 } |
119 | 120 |
120 void onDrawPath(const SkPath& path, const SkPaint& paint) override { | 121 void onDrawPath(const SkPath& path, const PaintFlags& paint) override { |
121 AddPaintImage(path.getBounds(), paint); | 122 AddPaintImage(path.getBounds(), paint); |
122 } | 123 } |
123 | 124 |
124 void onDrawOval(const SkRect& r, const SkPaint& paint) override { | 125 void onDrawOval(const SkRect& r, const PaintFlags& paint) override { |
125 AddPaintImage(r, paint); | 126 AddPaintImage(r, paint); |
126 } | 127 } |
127 | 128 |
128 void onDrawArc(const SkRect& r, | 129 void onDrawArc(const SkRect& r, |
129 SkScalar start_angle, | 130 SkScalar start_angle, |
130 SkScalar sweep_angle, | 131 SkScalar sweep_angle, |
131 bool use_center, | 132 bool use_center, |
132 const SkPaint& paint) override { | 133 const PaintFlags& paint) override { |
133 AddPaintImage(r, paint); | 134 AddPaintImage(r, paint); |
134 } | 135 } |
135 | 136 |
136 void onDrawRRect(const SkRRect& rr, const SkPaint& paint) override { | 137 void onDrawRRect(const SkRRect& rr, const PaintFlags& paint) override { |
137 AddPaintImage(rr.rect(), paint); | 138 AddPaintImage(rr.rect(), paint); |
138 } | 139 } |
139 | 140 |
140 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override { | 141 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override { |
141 saved_paints_.push_back(*rec.fPaint); | 142 saved_paints_.push_back(*rec.fPaint); |
142 return SkNWayCanvas::getSaveLayerStrategy(rec); | 143 return PaintCanvasNoDraw::getSaveLayerStrategy(rec); |
143 } | 144 } |
144 | 145 |
145 void willSave() override { | 146 void willSave() override { |
146 saved_paints_.push_back(SkPaint()); | 147 saved_paints_.push_back(PaintFlags()); |
147 return SkNWayCanvas::willSave(); | 148 return PaintCanvasNoDraw::willSave(); |
148 } | 149 } |
149 | 150 |
150 void willRestore() override { | 151 void willRestore() override { |
151 DCHECK_GT(saved_paints_.size(), 0u); | 152 DCHECK_GT(saved_paints_.size(), 0u); |
152 saved_paints_.pop_back(); | 153 saved_paints_.pop_back(); |
153 SkNWayCanvas::willRestore(); | 154 PaintCanvasNoDraw::willRestore(); |
154 } | 155 } |
155 | 156 |
156 private: | 157 private: |
157 bool ComputePaintBounds(const SkRect& rect, | 158 bool ComputePaintBounds(const SkRect& rect, |
158 const SkPaint* current_paint, | 159 const PaintFlags* current_paint, |
159 SkRect* paint_bounds) { | 160 SkRect* paint_bounds) { |
160 *paint_bounds = rect; | 161 *paint_bounds = rect; |
161 if (current_paint) { | 162 if (current_paint) { |
162 if (!current_paint->canComputeFastBounds()) | 163 if (!current_paint->canComputeFastBounds()) |
163 return false; | 164 return false; |
164 *paint_bounds = | 165 *paint_bounds = |
165 current_paint->computeFastBounds(*paint_bounds, paint_bounds); | 166 current_paint->computeFastBounds(*paint_bounds, paint_bounds); |
166 } | 167 } |
167 | 168 |
168 for (const auto& paint : base::Reversed(saved_paints_)) { | 169 for (const auto& paint : base::Reversed(saved_paints_)) { |
169 if (!paint.canComputeFastBounds()) | 170 if (!paint.canComputeFastBounds()) |
170 return false; | 171 return false; |
171 *paint_bounds = paint.computeFastBounds(*paint_bounds, paint_bounds); | 172 *paint_bounds = paint.computeFastBounds(*paint_bounds, paint_bounds); |
172 } | 173 } |
173 return true; | 174 return true; |
174 } | 175 } |
175 | 176 |
176 void AddImage(sk_sp<const SkImage> image, | 177 void AddImage(sk_sp<const SkImage> image, |
177 const SkRect& src_rect, | 178 const SkRect& src_rect, |
178 const SkRect& rect, | 179 const SkRect& rect, |
179 const SkMatrix& matrix, | 180 const SkMatrix& matrix, |
180 const SkPaint* paint) { | 181 const PaintFlags* paint) { |
181 if (!image->isLazyGenerated()) | 182 if (!image->isLazyGenerated()) |
182 return; | 183 return; |
183 | 184 |
184 SkRect paint_rect; | 185 SkRect paint_rect; |
185 bool computed_paint_bounds = ComputePaintBounds(rect, paint, &paint_rect); | 186 bool computed_paint_bounds = ComputePaintBounds(rect, paint, &paint_rect); |
186 if (!computed_paint_bounds) { | 187 if (!computed_paint_bounds) { |
187 // TODO(vmpstr): UMA this case. | 188 // TODO(vmpstr): UMA this case. |
188 paint_rect = canvas_bounds_; | 189 paint_rect = canvas_bounds_; |
189 } | 190 } |
190 | 191 |
191 if (!paint_rect.intersects(canvas_bounds_)) | 192 if (!paint_rect.intersects(canvas_bounds_)) |
192 return; | 193 return; |
193 | 194 |
194 SkFilterQuality filter_quality = kNone_SkFilterQuality; | 195 SkFilterQuality filter_quality = kNone_SkFilterQuality; |
195 if (paint) { | 196 if (paint) { |
196 filter_quality = paint->getFilterQuality(); | 197 filter_quality = paint->getFilterQuality(); |
197 } | 198 } |
198 | 199 |
199 SkIRect src_irect; | 200 SkIRect src_irect; |
200 src_rect.roundOut(&src_irect); | 201 src_rect.roundOut(&src_irect); |
201 image_set_->push_back(std::make_pair( | 202 image_set_->push_back(std::make_pair( |
202 DrawImage(std::move(image), src_irect, filter_quality, matrix), | 203 DrawImage(std::move(image), src_irect, filter_quality, matrix), |
203 SafeClampPaintRectToSize(paint_rect, canvas_size_))); | 204 SafeClampPaintRectToSize(paint_rect, canvas_size_))); |
204 } | 205 } |
205 | 206 |
206 // Currently this function only handles extracting images from SkImageShaders | 207 // Currently this function only handles extracting images from PaintShaders |
207 // embedded in SkPaints. Other embedded image cases, such as SkPictures, | 208 // embedded in PaintFlags. Other embedded image cases, such as PaintRecords, |
208 // are not yet handled. | 209 // are not yet handled. |
209 void AddPaintImage(const SkRect& rect, const SkPaint& paint) { | 210 void AddPaintImage(const SkRect& rect, const PaintFlags& paint) { |
210 SkShader* shader = paint.getShader(); | 211 SkShader* shader = paint.getShader(); |
211 if (shader) { | 212 if (shader) { |
212 SkMatrix matrix; | 213 SkMatrix matrix; |
213 SkShader::TileMode xy[2]; | 214 SkShader::TileMode xy[2]; |
214 SkImage* image = shader->isAImage(&matrix, xy); | 215 SkImage* image = shader->isAImage(&matrix, xy); |
215 if (image) { | 216 if (image) { |
216 const SkMatrix& ctm = getTotalMatrix(); | 217 const SkMatrix& ctm = getTotalMatrix(); |
217 matrix.postConcat(ctm); | 218 matrix.postConcat(ctm); |
218 // TODO(ericrk): Handle cases where we only need a sub-rect from the | 219 // TODO(ericrk): Handle cases where we only need a sub-rect from the |
219 // image. crbug.com/671821 | 220 // image. crbug.com/671821 |
220 AddImage(sk_ref_sp(image), SkRect::MakeFromIRect(image->bounds()), | 221 AddImage(sk_ref_sp(image), SkRect::MakeFromIRect(image->bounds()), |
221 MapRect(ctm, rect), matrix, &paint); | 222 MapRect(ctm, rect), matrix, &paint); |
222 } | 223 } |
223 } | 224 } |
224 } | 225 } |
225 | 226 |
226 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; | 227 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; |
227 const SkRect canvas_bounds_; | 228 const SkRect canvas_bounds_; |
228 const gfx::Size canvas_size_; | 229 const gfx::Size canvas_size_; |
229 std::vector<SkPaint> saved_paints_; | 230 std::vector<PaintFlags> saved_paints_; |
230 }; | 231 }; |
231 | 232 |
232 } // namespace | 233 } // namespace |
233 | 234 |
234 DiscardableImageMap::DiscardableImageMap() {} | 235 DiscardableImageMap::DiscardableImageMap() {} |
235 | 236 |
236 DiscardableImageMap::~DiscardableImageMap() {} | 237 DiscardableImageMap::~DiscardableImageMap() {} |
237 | 238 |
238 std::unique_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( | 239 std::unique_ptr<PaintCanvas> DiscardableImageMap::BeginGeneratingMetadata( |
239 const gfx::Size& bounds) { | 240 const gfx::Size& bounds) { |
240 DCHECK(all_images_.empty()); | 241 DCHECK(all_images_.empty()); |
241 return base::MakeUnique<DiscardableImagesMetadataCanvas>( | 242 return base::MakeUnique<DiscardableImagesMetadataCanvas>( |
242 bounds.width(), bounds.height(), &all_images_); | 243 bounds.width(), bounds.height(), &all_images_); |
243 } | 244 } |
244 | 245 |
245 void DiscardableImageMap::EndGeneratingMetadata() { | 246 void DiscardableImageMap::EndGeneratingMetadata() { |
246 images_rtree_.Build(all_images_, | 247 images_rtree_.Build(all_images_, |
247 [](const std::pair<DrawImage, gfx::Rect>& image) { | 248 [](const std::pair<DrawImage, gfx::Rect>& image) { |
248 return image.second; | 249 return image.second; |
(...skipping 14 matching lines...) Expand all Loading... |
263 DiscardableImageMap* image_map, | 264 DiscardableImageMap* image_map, |
264 const gfx::Size& bounds) | 265 const gfx::Size& bounds) |
265 : image_map_(image_map), | 266 : image_map_(image_map), |
266 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} | 267 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} |
267 | 268 |
268 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { | 269 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { |
269 image_map_->EndGeneratingMetadata(); | 270 image_map_->EndGeneratingMetadata(); |
270 } | 271 } |
271 | 272 |
272 } // namespace cc | 273 } // namespace cc |
OLD | NEW |