OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/debug/trace_event.h" | 5 #include "base/debug/trace_event.h" |
6 #include "skia/ext/analysis_canvas.h" | 6 #include "skia/ext/analysis_canvas.h" |
7 #include "third_party/skia/include/core/SkDevice.h" | 7 #include "third_party/skia/include/core/SkDevice.h" |
8 #include "third_party/skia/include/core/SkDraw.h" | 8 #include "third_party/skia/include/core/SkDraw.h" |
9 #include "third_party/skia/include/core/SkRRect.h" | 9 #include "third_party/skia/include/core/SkRRect.h" |
10 #include "third_party/skia/include/core/SkShader.h" | |
10 #include "third_party/skia/src/core/SkRasterClip.h" | 11 #include "third_party/skia/src/core/SkRasterClip.h" |
11 #include "ui/gfx/rect_conversions.h" | 12 #include "ui/gfx/rect_conversions.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // FIXME: Arbitrary number. Requires tuning & experimentation. | 16 // FIXME: Arbitrary number. Requires tuning & experimentation. |
16 // Probably requires per-platform tuning; N10 average draw call takes | 17 // Probably requires per-platform tuning; N10 average draw call takes |
17 // 25x as long as Z620. | 18 // 25x as long as Z620. |
18 const int gPictureCostThreshold = 1000; | 19 const int gPictureCostThreshold = 1000; |
19 | 20 |
21 // URI label for a lazily decoded SkPixelRef. | |
22 const char labelLazyDecoded[] = "lazy"; | |
Justin Novosad
2013/03/11 15:26:11
This string constant should only be defined in one
| |
23 | |
20 static bool isSolidColorPaint(const SkPaint& paint) { | 24 static bool isSolidColorPaint(const SkPaint& paint) { |
21 SkXfermode::Mode xferMode; | 25 SkXfermode::Mode xferMode; |
22 | 26 |
23 // getXfermode can return a NULL, but that is handled | 27 // getXfermode can return a NULL, but that is handled |
24 // gracefully by AsMode (NULL turns into kSrcOver mode). | 28 // gracefully by AsMode (NULL turns into kSrcOver mode). |
25 SkXfermode::AsMode(paint.getXfermode(), &xferMode); | 29 SkXfermode::AsMode(paint.getXfermode(), &xferMode); |
26 | 30 |
27 // Paint is solid color if the following holds: | 31 // Paint is solid color if the following holds: |
28 // - Alpha is 1.0, style is fill, and there are no special effects | 32 // - Alpha is 1.0, style is fill, and there are no special effects |
29 // - Xfer mode is either kSrc or kSrcOver (kSrcOver is equivalent | 33 // - Xfer mode is either kSrc or kSrcOver (kSrcOver is equivalent |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 if (isForcedNotSolid_) | 108 if (isForcedNotSolid_) |
105 isSolidColor_ = false; | 109 isSolidColor_ = false; |
106 } | 110 } |
107 | 111 |
108 void AnalysisDevice::setForceNotTransparent(bool flag) { | 112 void AnalysisDevice::setForceNotTransparent(bool flag) { |
109 isForcedNotTransparent_ = flag; | 113 isForcedNotTransparent_ = flag; |
110 if (isForcedNotTransparent_) | 114 if (isForcedNotTransparent_) |
111 isTransparent_ = false; | 115 isTransparent_ = false; |
112 } | 116 } |
113 | 117 |
118 void AnalysisDevice::addPixelRefIfLazy(SkPixelRef* pixelRef) { | |
119 uint32_t genID = pixelRef->getGenerationID(); | |
120 | |
121 // If this ID exists (whether it is lazy pixel ref or not), | |
122 // we can return early. | |
123 std::pair<std::set<uint32_t>::iterator, bool> result = | |
Justin Novosad
2013/03/11 15:26:11
"result" is a bad name for a variable that is not
| |
124 existingPixelRefIDs_.insert(genID); | |
125 if (!result.second) | |
126 return; | |
127 | |
128 if (pixelRef->getURI() && | |
129 !strncmp(pixelRef->getURI(), labelLazyDecoded, 4)) { | |
130 lazyPixelRefs_.push_back(static_cast<skia::LazyPixelRef*>(pixelRef)); | |
131 } | |
132 } | |
133 | |
134 void AnalysisDevice::addBitmap(const SkBitmap& bitmap) { | |
Justin Novosad
2013/03/11 15:26:11
Needs unit testing. Test should verify:
a) lazy vs
| |
135 addPixelRefIfLazy(bitmap.pixelRef()); | |
136 } | |
137 | |
138 void AnalysisDevice::addBitmapFromPaint(const SkPaint& paint) { | |
139 SkShader* shader = paint.getShader(); | |
140 if (shader) { | |
141 SkBitmap bitmap; | |
142 // Check whether the shader is a gradient in order to short-circuit | |
143 // call to asABitmap to prevent generation of bitmaps from | |
144 // gradient shaders, which implement asABitmap. | |
145 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && | |
146 shader->asABitmap(&bitmap, NULL, NULL)) { | |
147 addPixelRefIfLazy(bitmap.pixelRef()); | |
148 } | |
149 } | |
150 } | |
151 | |
152 void AnalysisDevice::consumeLazyPixelRefs(std::list<LazyPixelRef*>& pixelRefs) { | |
153 DCHECK(pixelRefs.empty()); | |
154 lazyPixelRefs_.swap(pixelRefs); | |
155 } | |
156 | |
114 void AnalysisDevice::clear(SkColor color) { | 157 void AnalysisDevice::clear(SkColor color) { |
115 ++estimatedCost_; | 158 ++estimatedCost_; |
116 | 159 |
117 isTransparent_ = (!isForcedNotTransparent_ && SkColorGetA(color) == 0); | 160 isTransparent_ = (!isForcedNotTransparent_ && SkColorGetA(color) == 0); |
118 | 161 |
119 if (!isForcedNotSolid_ && SkColorGetA(color) == 255) { | 162 if (!isForcedNotSolid_ && SkColorGetA(color) == 255) { |
120 isSolidColor_ = true; | 163 isSolidColor_ = true; |
121 color_ = color; | 164 color_ = color; |
122 } | 165 } |
123 else { | 166 else { |
124 isSolidColor_ = false; | 167 isSolidColor_ = false; |
125 } | 168 } |
126 } | 169 } |
127 | 170 |
128 void AnalysisDevice::drawPaint(const SkDraw&, const SkPaint& paint) { | 171 void AnalysisDevice::drawPaint(const SkDraw&, const SkPaint& paint) { |
129 ++estimatedCost_; | 172 ++estimatedCost_; |
130 isSolidColor_ = false; | 173 isSolidColor_ = false; |
131 isTransparent_ = false; | 174 isTransparent_ = false; |
175 addBitmapFromPaint(paint); | |
132 } | 176 } |
133 | 177 |
134 void AnalysisDevice::drawPoints(const SkDraw&, SkCanvas::PointMode mode, | 178 void AnalysisDevice::drawPoints(const SkDraw&, SkCanvas::PointMode mode, |
135 size_t count, const SkPoint[], | 179 size_t count, const SkPoint[], |
136 const SkPaint& paint) { | 180 const SkPaint& paint) { |
137 ++estimatedCost_; | 181 ++estimatedCost_; |
138 isSolidColor_ = false; | 182 isSolidColor_ = false; |
139 isTransparent_ = false; | 183 isTransparent_ = false; |
184 addBitmapFromPaint(paint); | |
140 } | 185 } |
141 | 186 |
142 void AnalysisDevice::drawRect(const SkDraw& draw, const SkRect& rect, | 187 void AnalysisDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
143 const SkPaint& paint) { | 188 const SkPaint& paint) { |
144 | 189 |
145 // FIXME: if there's a pending image decode & resize, more expensive | 190 // FIXME: if there's a pending image decode & resize, more expensive |
146 if (paint.getMaskFilter()) { | 191 if (paint.getMaskFilter()) { |
147 estimatedCost_ += 300; | 192 estimatedCost_ += 300; |
148 } | 193 } |
149 ++estimatedCost_; | 194 ++estimatedCost_; |
150 | 195 addBitmapFromPaint(paint); |
151 bool doesCoverCanvas = isFullQuad(draw, | 196 bool doesCoverCanvas = isFullQuad(draw, |
152 SkRect::MakeWH(width(), height()), | 197 SkRect::MakeWH(width(), height()), |
153 rect); | 198 rect); |
154 | 199 |
155 SkXfermode::Mode xferMode; | 200 SkXfermode::Mode xferMode; |
156 SkXfermode::AsMode(paint.getXfermode(), &xferMode); | 201 SkXfermode::AsMode(paint.getXfermode(), &xferMode); |
157 | 202 |
158 // This canvas will become transparent if the following holds: | 203 // This canvas will become transparent if the following holds: |
159 // - The quad is a full tile quad | 204 // - The quad is a full tile quad |
160 // - We're not in "forced not transparent" mode | 205 // - We're not in "forced not transparent" mode |
(...skipping 27 matching lines...) Expand all Loading... | |
188 else { | 233 else { |
189 isSolidColor_ = false; | 234 isSolidColor_ = false; |
190 } | 235 } |
191 } | 236 } |
192 | 237 |
193 void AnalysisDevice::drawOval(const SkDraw&, const SkRect& oval, | 238 void AnalysisDevice::drawOval(const SkDraw&, const SkRect& oval, |
194 const SkPaint& paint) { | 239 const SkPaint& paint) { |
195 ++estimatedCost_; | 240 ++estimatedCost_; |
196 isSolidColor_ = false; | 241 isSolidColor_ = false; |
197 isTransparent_ = false; | 242 isTransparent_ = false; |
243 addBitmapFromPaint(paint); | |
198 } | 244 } |
199 | 245 |
200 void AnalysisDevice::drawPath(const SkDraw&, const SkPath& path, | 246 void AnalysisDevice::drawPath(const SkDraw&, const SkPath& path, |
201 const SkPaint& paint, | 247 const SkPaint& paint, |
202 const SkMatrix* prePathMatrix , | 248 const SkMatrix* prePathMatrix , |
203 bool pathIsMutable ) { | 249 bool pathIsMutable ) { |
204 // On Z620, every antialiased path costs us about 300us. | 250 // On Z620, every antialiased path costs us about 300us. |
205 // We've only seen this in practice on filled paths, but | 251 // We've only seen this in practice on filled paths, but |
206 // we expect it to apply to all path stroking modes. | 252 // we expect it to apply to all path stroking modes. |
207 if (paint.getMaskFilter()) { | 253 if (paint.getMaskFilter()) { |
208 estimatedCost_ += 300; | 254 estimatedCost_ += 300; |
209 } | 255 } |
210 ++estimatedCost_; | 256 ++estimatedCost_; |
211 isSolidColor_ = false; | 257 isSolidColor_ = false; |
212 isTransparent_ = false; | 258 isTransparent_ = false; |
259 addBitmapFromPaint(paint); | |
213 } | 260 } |
214 | 261 |
215 void AnalysisDevice::drawBitmap(const SkDraw&, const SkBitmap& bitmap, | 262 void AnalysisDevice::drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
216 const SkIRect* srcRectOrNull, | 263 const SkIRect* srcRectOrNull, |
217 const SkMatrix& matrix, const SkPaint& paint) { | 264 const SkMatrix& matrix, const SkPaint& paint) { |
218 ++estimatedCost_; | 265 ++estimatedCost_; |
219 isSolidColor_ = false; | 266 isSolidColor_ = false; |
220 isTransparent_ = false; | 267 isTransparent_ = false; |
268 addBitmap(bitmap); | |
221 } | 269 } |
222 | 270 |
223 void AnalysisDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap, | 271 void AnalysisDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap, |
224 int x, int y, const SkPaint& paint) { | 272 int x, int y, const SkPaint& paint) { |
225 ++estimatedCost_; | 273 ++estimatedCost_; |
226 isSolidColor_ = false; | 274 isSolidColor_ = false; |
227 isTransparent_ = false; | 275 isTransparent_ = false; |
276 addBitmap(bitmap); | |
228 } | 277 } |
229 | 278 |
230 void AnalysisDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap&, | 279 void AnalysisDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
231 const SkRect* srcOrNull, const SkRect& dst, | 280 const SkRect* srcOrNull, const SkRect& dst, |
232 const SkPaint& paint) { | 281 const SkPaint& paint) { |
233 ++estimatedCost_; | 282 ++estimatedCost_; |
234 | 283 |
235 // Call drawRect to determine transparency, | 284 // Call drawRect to determine transparency, |
236 // but reset solid color to false. | 285 // but reset solid color to false. |
237 drawRect(draw, dst, paint); | 286 drawRect(draw, dst, paint); |
238 isSolidColor_ = false; | 287 isSolidColor_ = false; |
288 addBitmap(bitmap); | |
239 } | 289 } |
240 | 290 |
241 | 291 |
242 void AnalysisDevice::drawText(const SkDraw&, const void* text, size_t len, | 292 void AnalysisDevice::drawText(const SkDraw&, const void* text, size_t len, |
243 SkScalar x, SkScalar y, const SkPaint& paint) { | 293 SkScalar x, SkScalar y, const SkPaint& paint) { |
244 ++estimatedCost_; | 294 ++estimatedCost_; |
245 isSolidColor_ = false; | 295 isSolidColor_ = false; |
246 isTransparent_ = false; | 296 isTransparent_ = false; |
297 addBitmapFromPaint(paint); | |
247 } | 298 } |
248 | 299 |
249 void AnalysisDevice::drawPosText(const SkDraw& draw, const void* text, size_t le n, | 300 void AnalysisDevice::drawPosText(const SkDraw& draw, const void* text, size_t le n, |
250 const SkScalar pos[], SkScalar constY, | 301 const SkScalar pos[], SkScalar constY, |
251 int scalarsPerPos, const SkPaint& paint) { | 302 int scalarsPerPos, const SkPaint& paint) { |
252 // FIXME: On Z620, every glyph cache miss costs us about 10us. | 303 // FIXME: On Z620, every glyph cache miss costs us about 10us. |
253 // We don't have a good mechanism for predicting glyph cache misses. | 304 // We don't have a good mechanism for predicting glyph cache misses. |
254 ++estimatedCost_; | 305 ++estimatedCost_; |
255 isSolidColor_ = false; | 306 isSolidColor_ = false; |
256 isTransparent_ = false; | 307 isTransparent_ = false; |
308 addBitmapFromPaint(paint); | |
257 } | 309 } |
258 | 310 |
259 void AnalysisDevice::drawTextOnPath(const SkDraw&, const void* text, size_t len, | 311 void AnalysisDevice::drawTextOnPath(const SkDraw&, const void* text, size_t len, |
260 const SkPath& path, const SkMatrix* matrix, | 312 const SkPath& path, const SkMatrix* matrix, |
261 const SkPaint& paint) { | 313 const SkPaint& paint) { |
262 ++estimatedCost_; | 314 ++estimatedCost_; |
263 isSolidColor_ = false; | 315 isSolidColor_ = false; |
264 isTransparent_ = false; | 316 isTransparent_ = false; |
317 addBitmapFromPaint(paint); | |
265 } | 318 } |
266 | 319 |
267 #ifdef SK_BUILD_FOR_ANDROID | 320 #ifdef SK_BUILD_FOR_ANDROID |
268 void AnalysisDevice::drawPosTextOnPath(const SkDraw& draw, const void* text, | 321 void AnalysisDevice::drawPosTextOnPath(const SkDraw& draw, const void* text, |
269 size_t len, | 322 size_t len, |
270 const SkPoint pos[], const SkPaint& paint, | 323 const SkPoint pos[], const SkPaint& paint, |
271 const SkPath& path, const SkMatrix* matrix) { | 324 const SkPath& path, const SkMatrix* matrix) { |
272 ++estimatedCost_; | 325 ++estimatedCost_; |
273 isSolidColor_ = false; | 326 isSolidColor_ = false; |
274 isTransparent_ = false; | 327 isTransparent_ = false; |
328 addBitmapFromPaint(paint); | |
275 } | 329 } |
276 #endif | 330 #endif |
277 | 331 |
278 void AnalysisDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode, | 332 void AnalysisDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode, |
279 int vertexCount, | 333 int vertexCount, |
280 const SkPoint verts[], const SkPoint texs[], | 334 const SkPoint verts[], const SkPoint texs[], |
281 const SkColor colors[], SkXfermode* xmode, | 335 const SkColor colors[], SkXfermode* xmode, |
282 const uint16_t indices[], int indexCount, | 336 const uint16_t indices[], int indexCount, |
283 const SkPaint& paint) { | 337 const SkPaint& paint) { |
284 ++estimatedCost_; | 338 ++estimatedCost_; |
285 isSolidColor_ = false; | 339 isSolidColor_ = false; |
286 isTransparent_ = false; | 340 isTransparent_ = false; |
341 addBitmapFromPaint(paint); | |
287 } | 342 } |
288 | 343 |
289 void AnalysisDevice::drawDevice(const SkDraw&, SkDevice*, int x, int y, | 344 void AnalysisDevice::drawDevice(const SkDraw&, SkDevice*, int x, int y, |
290 const SkPaint&) { | 345 const SkPaint&) { |
291 ++estimatedCost_; | 346 ++estimatedCost_; |
292 isSolidColor_ = false; | 347 isSolidColor_ = false; |
293 isTransparent_ = false; | 348 isTransparent_ = false; |
294 } | 349 } |
295 | 350 |
296 | 351 |
(...skipping 19 matching lines...) Expand all Loading... | |
316 } | 371 } |
317 | 372 |
318 bool AnalysisCanvas::isTransparent() const { | 373 bool AnalysisCanvas::isTransparent() const { |
319 return (static_cast<AnalysisDevice*>(getDevice()))->isTransparent(); | 374 return (static_cast<AnalysisDevice*>(getDevice()))->isTransparent(); |
320 } | 375 } |
321 | 376 |
322 int AnalysisCanvas::getEstimatedCost() const { | 377 int AnalysisCanvas::getEstimatedCost() const { |
323 return (static_cast<AnalysisDevice*>(getDevice()))->getEstimatedCost(); | 378 return (static_cast<AnalysisDevice*>(getDevice()))->getEstimatedCost(); |
324 } | 379 } |
325 | 380 |
381 void AnalysisCanvas::consumeLazyPixelRefs(std::list<LazyPixelRef*>& pixelRefs) { | |
382 static_cast<AnalysisDevice*>(getDevice())->consumeLazyPixelRefs(pixelRefs); | |
383 } | |
384 | |
326 bool AnalysisCanvas::clipRect(const SkRect& rect, SkRegion::Op op, | 385 bool AnalysisCanvas::clipRect(const SkRect& rect, SkRegion::Op op, |
327 bool doAA) { | 386 bool doAA) { |
328 return INHERITED::clipRect(rect, op, doAA); | 387 return INHERITED::clipRect(rect, op, doAA); |
329 } | 388 } |
330 | 389 |
331 bool AnalysisCanvas::clipPath(const SkPath& path, SkRegion::Op op, | 390 bool AnalysisCanvas::clipPath(const SkPath& path, SkRegion::Op op, |
332 bool doAA) { | 391 bool doAA) { |
333 // clipPaths can make our calls to isFullQuad invalid (ie have false | 392 // clipPaths can make our calls to isFullQuad invalid (ie have false |
334 // positives). As a precaution, force the setting to be non-solid | 393 // positives). As a precaution, force the setting to be non-solid |
335 // and non-transparent until we pop this | 394 // and non-transparent until we pop this |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 if (savedStackSize_ < forceNotTransparentStackLevel_) { | 477 if (savedStackSize_ < forceNotTransparentStackLevel_) { |
419 (static_cast<AnalysisDevice*>(getDevice()))->setForceNotTransparent(false) ; | 478 (static_cast<AnalysisDevice*>(getDevice()))->setForceNotTransparent(false) ; |
420 forceNotTransparentStackLevel_ = kNoLayer; | 479 forceNotTransparentStackLevel_ = kNoLayer; |
421 } | 480 } |
422 } | 481 } |
423 } | 482 } |
424 | 483 |
425 } // namespace skia | 484 } // namespace skia |
426 | 485 |
427 | 486 |
OLD | NEW |