OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/paint/paint_op_buffer.h" | |
6 | |
7 #include "cc/paint/display_item_list.h" | |
8 #include "cc/paint/paint_record.h" | |
9 #include "third_party/skia/include/core/SkAnnotation.h" | |
10 | |
11 namespace cc { | |
12 | |
13 #define TYPES(M) \ | |
14 M(AnnotateOp) \ | |
15 M(ClipPathOp) \ | |
16 M(ClipRectOp) \ | |
17 M(ClipRRectOp) \ | |
18 M(ConcatOp) \ | |
19 M(DrawArcOp) \ | |
20 M(DrawCircleOp) \ | |
21 M(DrawColorOp) \ | |
22 M(DrawDisplayItemListOp) \ | |
23 M(DrawDRRectOp) \ | |
24 M(DrawImageOp) \ | |
25 M(DrawImageRectOp) \ | |
26 M(DrawIRectOp) \ | |
27 M(DrawLineOp) \ | |
28 M(DrawOvalOp) \ | |
29 M(DrawPathOp) \ | |
30 M(DrawPosTextOp) \ | |
31 M(DrawRecordOp) \ | |
32 M(DrawRectOp) \ | |
33 M(DrawRRectOp) \ | |
34 M(DrawTextOp) \ | |
35 M(DrawTextBlobOp) \ | |
36 M(NoopOp) \ | |
37 M(RestoreOp) \ | |
38 M(RotateOp) \ | |
39 M(SaveOp) \ | |
40 M(SaveLayerOp) \ | |
41 M(SaveLayerAlphaOp) \ | |
42 M(ScaleOp) \ | |
43 M(SetMatrixOp) \ | |
44 M(TranslateOp) | |
45 | |
46 // Helper template to share common code for RasterWithAlpha when paint ops | |
47 // have or don't have PaintFlags. | |
48 template <typename T, bool HasFlags> | |
49 struct Rasterizer { | |
50 static void Raster(const T* op, | |
51 SkCanvas* canvas, | |
52 const SkMatrix& original_ctm) { | |
53 // Paint ops with kHasPaintFlags need to declare RasterWithPaintFlags | |
54 // otherwise, the paint op needs its own Raster function. Without its | |
55 // own, this becomes an infinite loop as PaintOp::Raster calls itself. | |
56 static_assert( | |
57 !std::is_same<decltype(&PaintOp::Raster), decltype(&T::Raster)>::value, | |
58 "No Raster function"); | |
59 | |
60 op->Raster(canvas); | |
61 } | |
62 static void RasterWithAlpha(const T* op, SkCanvas* canvas, uint8_t alpha) { | |
63 DCHECK(T::kIsDrawOp); | |
64 // TODO(enne): is it ok to just drop the bounds here? | |
65 canvas->saveLayerAlpha(nullptr, alpha); | |
66 op->Raster(canvas); | |
67 canvas->restore(); | |
68 } | |
69 }; | |
70 | |
71 template <typename T> | |
72 struct Rasterizer<T, true> { | |
73 static void Raster(const T* op, | |
74 SkCanvas* canvas, | |
75 const SkMatrix& original_ctm) { | |
76 op->RasterWithFlags(canvas, op->flags); | |
77 } | |
78 static void RasterWithAlpha(const T* op, SkCanvas* canvas, uint8_t alpha) { | |
79 DCHECK(T::kIsDrawOp); | |
80 SkMatrix unused_matrix; | |
81 if (alpha == 255) { | |
82 Raster(op, canvas, unused_matrix); | |
83 } else if (op->flags.SupportsFoldingAlpha()) { | |
84 PaintFlags flags = op->flags; | |
85 flags.setAlpha(SkMulDiv255Round(flags.getAlpha(), alpha)); | |
86 op->RasterWithFlags(canvas, flags); | |
87 } else { | |
88 canvas->saveLayerAlpha(nullptr, alpha); | |
89 op->RasterWithFlags(canvas, op->flags); | |
90 canvas->restore(); | |
91 } | |
92 } | |
93 }; | |
94 | |
95 template <> | |
96 struct Rasterizer<SetMatrixOp, false> { | |
97 static void Raster(const SetMatrixOp* op, | |
98 SkCanvas* canvas, | |
99 const SkMatrix& original_ctm) { | |
100 op->Raster(canvas, original_ctm); | |
101 } | |
102 static void RasterWithAlpha(const SetMatrixOp* op, | |
103 SkCanvas* canvas, | |
104 uint8_t alpha) { | |
105 NOTREACHED(); | |
106 } | |
107 }; | |
108 | |
109 template <> | |
110 struct Rasterizer<DrawRecordOp, false> { | |
111 static void Raster(const DrawRecordOp* op, | |
112 SkCanvas* canvas, | |
113 const SkMatrix& original_ctm) { | |
114 op->Raster(canvas); | |
115 } | |
116 static void RasterWithAlpha(const DrawRecordOp* op, | |
117 SkCanvas* canvas, | |
118 uint8_t alpha) { | |
119 // This "looking into records" optimization is done here instead of | |
120 // in the PaintOpBuffer::Raster function as DisplayItemList calls | |
121 // into RasterWithAlpha directly. | |
122 if (op->record->approximateOpCount() == 1) { | |
123 op->record->GetFirstOp()->RasterWithAlpha(canvas, alpha); | |
124 return; | |
125 } | |
126 | |
127 canvas->saveLayerAlpha(nullptr, alpha); | |
128 op->Raster(canvas); | |
129 canvas->restore(); | |
130 } | |
131 }; | |
132 | |
133 // TODO(enne): partially specialize RasterWithAlpha for draw color? | |
134 | |
135 static constexpr size_t kNumOpTypes = | |
136 static_cast<size_t>(PaintOpType::LastPaintOpType) + 1; | |
137 | |
138 // Verify that every op is in the TYPES macro. | |
139 #define M(T) +1 | |
140 static_assert(kNumOpTypes == TYPES(M), "Missing op in list"); | |
141 #undef M | |
142 | |
143 using RasterFunction = void (*)(const PaintOp* op, | |
144 SkCanvas* canvas, | |
145 const SkMatrix& original_ctm); | |
146 #define M(T) \ | |
147 [](const PaintOp* op, SkCanvas* canvas, const SkMatrix& original_ctm) { \ | |
148 Rasterizer<T, T::kHasPaintFlags>::Raster(static_cast<const T*>(op), \ | |
149 canvas, original_ctm); \ | |
150 }, | |
151 static const RasterFunction g_raster_functions[kNumOpTypes] = {TYPES(M)}; | |
152 #undef M | |
153 | |
154 using RasterAlphaFunction = void (*)(const PaintOp* op, | |
155 SkCanvas* canvas, | |
156 uint8_t alpha); | |
157 #define M(T) \ | |
158 T::kIsDrawOp ? \ | |
159 [](const PaintOp* op, SkCanvas* canvas, uint8_t alpha) { \ | |
160 Rasterizer<T, T::kHasPaintFlags>::RasterWithAlpha( \ | |
161 static_cast<const T*>(op), canvas, alpha); \ | |
162 } : static_cast<RasterAlphaFunction>(nullptr), | |
163 static const RasterAlphaFunction g_raster_alpha_functions[kNumOpTypes] = { | |
164 TYPES(M)}; | |
165 #undef M | |
166 | |
167 // Most state ops (matrix, clip, save, restore) have a trivial destructor. | |
168 // TODO(enne): evaluate if we need the nullptr optimization or if | |
169 // we even need to differentiate trivial destructors here. | |
170 using VoidFunction = void (*)(PaintOp* op); | |
171 #define M(T) \ | |
172 !std::is_trivially_destructible<T>::value \ | |
173 ? [](PaintOp* op) { static_cast<T*>(op)->~T(); } \ | |
174 : static_cast<VoidFunction>(nullptr), | |
175 static const VoidFunction g_destructor_functions[kNumOpTypes] = {TYPES(M)}; | |
176 #undef M | |
177 | |
178 #define M(T) T::kIsDrawOp, | |
179 static bool g_is_draw_op[kNumOpTypes] = {TYPES(M)}; | |
180 #undef M | |
181 | |
182 #define M(T) \ | |
183 static_assert(sizeof(T) <= sizeof(LargestPaintOp), \ | |
184 #T " must be no bigger than LargestPaintOp"); | |
185 TYPES(M); | |
186 #undef M | |
187 | |
188 #undef TYPES | |
189 | |
190 SkRect PaintOp::kUnsetRect = {SK_ScalarInfinity, 0, 0, 0}; | |
191 | |
192 void AnnotateOp::Raster(SkCanvas* canvas) const { | |
193 switch (annotation_type) { | |
194 case PaintCanvas::AnnotationType::URL: | |
195 SkAnnotateRectWithURL(canvas, rect, data.get()); | |
196 break; | |
197 case PaintCanvas::AnnotationType::LINK_TO_DESTINATION: | |
198 SkAnnotateLinkToDestination(canvas, rect, data.get()); | |
199 break; | |
200 case PaintCanvas::AnnotationType::NAMED_DESTINATION: { | |
201 SkPoint point = SkPoint::Make(rect.x(), rect.y()); | |
202 SkAnnotateNamedDestination(canvas, point, data.get()); | |
203 break; | |
204 } | |
205 } | |
206 } | |
207 | |
208 void ClipPathOp::Raster(SkCanvas* canvas) const { | |
209 canvas->clipPath(path, op, antialias); | |
210 } | |
211 | |
212 void ClipRectOp::Raster(SkCanvas* canvas) const { | |
213 canvas->clipRect(rect, op, antialias); | |
214 } | |
215 | |
216 void ClipRRectOp::Raster(SkCanvas* canvas) const { | |
217 canvas->clipRRect(rrect, op, antialias); | |
218 } | |
219 | |
220 void ConcatOp::Raster(SkCanvas* canvas) const { | |
221 canvas->concat(matrix); | |
222 } | |
223 | |
224 void DrawArcOp::RasterWithFlags(SkCanvas* canvas, | |
225 const PaintFlags& flags) const { | |
226 canvas->drawArc(oval, start_angle, sweep_angle, use_center, ToSkPaint(flags)); | |
227 } | |
228 | |
229 void DrawCircleOp::RasterWithFlags(SkCanvas* canvas, | |
230 const PaintFlags& flags) const { | |
231 canvas->drawCircle(cx, cy, radius, ToSkPaint(flags)); | |
232 } | |
233 | |
234 void DrawColorOp::Raster(SkCanvas* canvas) const { | |
235 canvas->drawColor(color, mode); | |
236 } | |
237 | |
238 void DrawDisplayItemListOp::Raster(SkCanvas* canvas) const { | |
239 list->Raster(canvas, nullptr); | |
240 } | |
241 | |
242 void DrawDRRectOp::RasterWithFlags(SkCanvas* canvas, | |
243 const PaintFlags& flags) const { | |
244 canvas->drawDRRect(outer, inner, ToSkPaint(flags)); | |
245 } | |
246 | |
247 void DrawImageOp::RasterWithFlags(SkCanvas* canvas, | |
248 const PaintFlags& flags) const { | |
249 canvas->drawImage(image.get(), left, top, ToSkPaint(&flags)); | |
250 } | |
251 | |
252 void DrawImageRectOp::RasterWithFlags(SkCanvas* canvas, | |
253 const PaintFlags& flags) const { | |
254 // TODO(enne): Probably PaintCanvas should just use the skia enum directly. | |
255 SkCanvas::SrcRectConstraint skconstraint = | |
256 static_cast<SkCanvas::SrcRectConstraint>(constraint); | |
257 canvas->drawImageRect(image.get(), src, dst, ToSkPaint(&flags), skconstraint); | |
258 } | |
259 | |
260 void DrawIRectOp::RasterWithFlags(SkCanvas* canvas, | |
261 const PaintFlags& flags) const { | |
262 canvas->drawIRect(rect, ToSkPaint(flags)); | |
263 } | |
264 | |
265 void DrawLineOp::RasterWithFlags(SkCanvas* canvas, | |
266 const PaintFlags& flags) const { | |
267 canvas->drawLine(x0, y0, x1, y1, ToSkPaint(flags)); | |
268 } | |
269 | |
270 void DrawOvalOp::RasterWithFlags(SkCanvas* canvas, | |
271 const PaintFlags& flags) const { | |
272 canvas->drawOval(oval, ToSkPaint(flags)); | |
273 } | |
274 | |
275 void DrawPathOp::RasterWithFlags(SkCanvas* canvas, | |
276 const PaintFlags& flags) const { | |
277 canvas->drawPath(path, ToSkPaint(flags)); | |
278 } | |
279 | |
280 void DrawPosTextOp::RasterWithFlags(SkCanvas* canvas, | |
281 const PaintFlags& flags) const { | |
282 canvas->drawPosText(paint_op_data(this), bytes, paint_op_array<SkPoint>(this), | |
283 ToSkPaint(flags)); | |
284 } | |
285 | |
286 void DrawRecordOp::Raster(SkCanvas* canvas) const { | |
287 record->playback(canvas); | |
288 } | |
289 | |
290 void DrawRectOp::RasterWithFlags(SkCanvas* canvas, | |
291 const PaintFlags& flags) const { | |
292 canvas->drawRect(rect, ToSkPaint(flags)); | |
293 } | |
294 | |
295 void DrawRRectOp::RasterWithFlags(SkCanvas* canvas, | |
296 const PaintFlags& flags) const { | |
297 canvas->drawRRect(rrect, ToSkPaint(flags)); | |
298 } | |
299 | |
300 void DrawTextOp::RasterWithFlags(SkCanvas* canvas, | |
301 const PaintFlags& flags) const { | |
302 canvas->drawText(paint_op_data(this), bytes, x, y, ToSkPaint(flags)); | |
303 } | |
304 | |
305 void DrawTextBlobOp::RasterWithFlags(SkCanvas* canvas, | |
306 const PaintFlags& flags) const { | |
307 canvas->drawTextBlob(blob.get(), x, y, ToSkPaint(flags)); | |
308 } | |
309 | |
310 void RestoreOp::Raster(SkCanvas* canvas) const { | |
311 canvas->restore(); | |
312 } | |
313 | |
314 void RotateOp::Raster(SkCanvas* canvas) const { | |
315 canvas->rotate(degrees); | |
316 } | |
317 | |
318 void SaveOp::Raster(SkCanvas* canvas) const { | |
319 canvas->save(); | |
320 } | |
321 | |
322 void SaveLayerOp::RasterWithFlags(SkCanvas* canvas, | |
323 const PaintFlags& flags) const { | |
324 // See PaintOp::kUnsetRect | |
325 bool unset = bounds.left() == SK_ScalarInfinity; | |
326 | |
327 canvas->saveLayer(unset ? nullptr : &bounds, ToSkPaint(&flags)); | |
328 } | |
329 | |
330 void SaveLayerAlphaOp::Raster(SkCanvas* canvas) const { | |
331 // See PaintOp::kUnsetRect | |
332 bool unset = bounds.left() == SK_ScalarInfinity; | |
333 canvas->saveLayerAlpha(unset ? nullptr : &bounds, alpha); | |
334 } | |
335 | |
336 void ScaleOp::Raster(SkCanvas* canvas) const { | |
337 canvas->scale(sx, sy); | |
338 } | |
339 | |
340 void SetMatrixOp::Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const { | |
341 canvas->setMatrix(SkMatrix::Concat(original_ctm, matrix)); | |
342 } | |
343 | |
344 void TranslateOp::Raster(SkCanvas* canvas) const { | |
345 canvas->translate(dx, dy); | |
346 } | |
347 | |
348 bool PaintOp::IsDrawOp() const { | |
349 return g_is_draw_op[type]; | |
350 } | |
351 | |
352 void PaintOp::Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const { | |
353 g_raster_functions[type](this, canvas, original_ctm); | |
354 } | |
355 | |
356 void PaintOp::RasterWithAlpha(SkCanvas* canvas, uint8_t alpha) const { | |
357 g_raster_alpha_functions[type](this, canvas, alpha); | |
358 } | |
359 | |
360 DrawDisplayItemListOp::DrawDisplayItemListOp( | |
361 scoped_refptr<DisplayItemList> list) | |
362 : list(list) {} | |
363 | |
364 size_t DrawDisplayItemListOp::AdditionalBytesUsed() const { | |
365 return list->ApproximateMemoryUsage(); | |
366 } | |
367 | |
368 int ClipPathOp::CountSlowPaths() const { | |
369 return antialias && !path.isConvex() ? 1 : 0; | |
370 } | |
371 | |
372 int DrawLineOp::CountSlowPaths() const { | |
373 if (const SkPathEffect* effect = flags.getPathEffect()) { | |
374 SkPathEffect::DashInfo info; | |
375 SkPathEffect::DashType dashType = effect->asADash(&info); | |
376 if (flags.getStrokeCap() != PaintFlags::kRound_Cap && | |
377 dashType == SkPathEffect::kDash_DashType && info.fCount == 2) { | |
378 // The PaintFlags will count this as 1, so uncount that here as | |
379 // this kind of line is special cased and not slow. | |
380 return -1; | |
381 } | |
382 } | |
383 return 0; | |
384 } | |
385 | |
386 int DrawPathOp::CountSlowPaths() const { | |
387 // This logic is copied from SkPathCounter instead of attempting to expose | |
388 // that from Skia. | |
389 if (!flags.isAntiAlias() || path.isConvex()) | |
390 return 0; | |
391 | |
392 PaintFlags::Style paintStyle = flags.getStyle(); | |
393 const SkRect& pathBounds = path.getBounds(); | |
394 if (paintStyle == PaintFlags::kStroke_Style && flags.getStrokeWidth() == 0) { | |
395 // AA hairline concave path is not slow. | |
396 return 0; | |
397 } else if (paintStyle == PaintFlags::kFill_Style && | |
398 pathBounds.width() < 64.f && pathBounds.height() < 64.f && | |
399 !path.isVolatile()) { | |
400 // AADF eligible concave path is not slow. | |
401 return 0; | |
402 } else { | |
403 return 1; | |
404 } | |
405 } | |
406 | |
407 AnnotateOp::AnnotateOp(PaintCanvas::AnnotationType annotation_type, | |
408 const SkRect& rect, | |
409 sk_sp<SkData> data) | |
410 : annotation_type(annotation_type), rect(rect), data(std::move(data)) {} | |
411 | |
412 AnnotateOp::~AnnotateOp() = default; | |
413 | |
414 DrawDisplayItemListOp::~DrawDisplayItemListOp() = default; | |
415 | |
416 DrawImageOp::DrawImageOp(sk_sp<const SkImage> image, | |
417 SkScalar left, | |
418 SkScalar top, | |
419 const PaintFlags* flags) | |
420 : image(std::move(image)), | |
421 left(left), | |
422 top(top), | |
423 flags(flags ? *flags : PaintFlags()) {} | |
424 | |
425 DrawImageOp::~DrawImageOp() = default; | |
426 | |
427 DrawImageRectOp::DrawImageRectOp(sk_sp<const SkImage> image, | |
428 const SkRect& src, | |
429 const SkRect& dst, | |
430 const PaintFlags* flags, | |
431 PaintCanvas::SrcRectConstraint constraint) | |
432 : image(std::move(image)), | |
433 flags(flags ? *flags : PaintFlags()), | |
434 src(src), | |
435 dst(dst), | |
436 constraint(constraint) {} | |
437 | |
438 DrawImageRectOp::~DrawImageRectOp() = default; | |
439 | |
440 DrawPosTextOp::DrawPosTextOp(size_t bytes, | |
441 size_t count, | |
442 const PaintFlags& flags) | |
443 : PaintOpWithDataArray(bytes, count), flags(flags) {} | |
444 | |
445 DrawPosTextOp::~DrawPosTextOp() = default; | |
446 | |
447 DrawRecordOp::DrawRecordOp(sk_sp<const PaintRecord> record) | |
448 : record(std::move(record)) {} | |
449 | |
450 DrawRecordOp::~DrawRecordOp() = default; | |
451 | |
452 size_t DrawRecordOp::AdditionalBytesUsed() const { | |
453 return record->approximateBytesUsed(); | |
454 } | |
455 | |
456 DrawTextBlobOp::DrawTextBlobOp(sk_sp<SkTextBlob> blob, | |
457 SkScalar x, | |
458 SkScalar y, | |
459 const PaintFlags& flags) | |
460 : blob(std::move(blob)), x(x), y(y), flags(flags) {} | |
461 | |
462 DrawTextBlobOp::~DrawTextBlobOp() = default; | |
463 | |
464 PaintOpBuffer::PaintOpBuffer() : cull_rect_(SkRect::MakeEmpty()) {} | |
465 | |
466 PaintOpBuffer::PaintOpBuffer(const SkRect& cull_rect) : cull_rect_(cull_rect) {} | |
467 | |
468 PaintOpBuffer::~PaintOpBuffer() { | |
469 Reset(); | |
470 } | |
471 | |
472 void PaintOpBuffer::Reset() { | |
473 for (auto* op : Iterator(this)) { | |
474 auto func = g_destructor_functions[op->type]; | |
475 if (func) | |
476 func(op); | |
477 } | |
478 | |
479 // Leave data_ allocated, reserved_ unchanged. | |
480 used_ = 0; | |
481 op_count_ = 0; | |
482 num_slow_paths_ = 0; | |
483 } | |
484 | |
485 void PaintOpBuffer::playback(SkCanvas* canvas) const { | |
486 // TODO(enne): a PaintRecord that contains a SetMatrix assumes that the | |
487 // SetMatrix is local to that PaintRecord itself. Said differently, if you | |
488 // translate(x, y), then draw a paint record with a SetMatrix(identity), | |
489 // the translation should be preserved instead of clobbering the top level | |
490 // transform. This could probably be done more efficiently. | |
491 SkMatrix original = canvas->getTotalMatrix(); | |
492 | |
493 for (Iterator iter(this); iter; ++iter) { | |
494 // Optimize out save/restores or save/draw/restore that can be a single | |
495 // draw. See also: similar code in SkRecordOpts and cc's DisplayItemList. | |
496 // TODO(enne): consider making this recursive? | |
497 const PaintOp* op = *iter; | |
498 if (op->GetType() == PaintOpType::SaveLayerAlpha) { | |
499 const PaintOp* second = iter.peek1(); | |
500 if (second) { | |
501 if (second->GetType() == PaintOpType::Restore) { | |
502 ++iter; | |
503 continue; | |
504 } | |
505 if (second->IsDrawOp()) { | |
506 const PaintOp* third = iter.peek2(); | |
507 if (third && third->GetType() == PaintOpType::Restore) { | |
508 const SaveLayerAlphaOp* save_op = | |
509 static_cast<const SaveLayerAlphaOp*>(op); | |
510 second->RasterWithAlpha(canvas, save_op->alpha); | |
511 ++iter; | |
512 ++iter; | |
513 continue; | |
514 } | |
515 } | |
516 } | |
517 } | |
518 // TODO(enne): skip SaveLayer followed by restore with nothing in | |
519 // between, however SaveLayer with image filters on it (or maybe | |
520 // other PaintFlags options) are not a noop. Figure out what these | |
521 // are so we can skip them correctly. | |
522 | |
523 op->Raster(canvas, original); | |
524 } | |
525 } | |
526 | |
527 void PaintOpBuffer::playback(SkCanvas* canvas, | |
528 SkPicture::AbortCallback* callback) const { | |
529 // The abort callback is only used for analysis, in general, so | |
530 // this playback code can be more straightforward and not do the | |
531 // optimizations in the other function. | |
532 if (!callback) { | |
533 playback(canvas); | |
534 return; | |
535 } | |
536 | |
537 SkMatrix original = canvas->getTotalMatrix(); | |
538 | |
539 // TODO(enne): ideally callers would just iterate themselves and we | |
540 // can remove the entire notion of an abort callback. | |
541 for (auto* op : Iterator(this)) { | |
542 op->Raster(canvas, original); | |
543 if (callback && callback->abort()) | |
544 return; | |
545 } | |
546 } | |
547 | |
548 void PaintOpBuffer::ShrinkToFit() { | |
549 if (!used_ || used_ == reserved_) | |
550 return; | |
551 data_.realloc(used_); | |
552 reserved_ = used_; | |
553 } | |
554 | |
555 } // namespace cc | |
OLD | NEW |