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

Side by Side Diff: cc/paint/paint_op_buffer.cc

Issue 2768143002: Back PaintRecord with PaintOpBuffer instead of SkPicture (Closed)
Patch Set: Remove unneeded expectation Created 3 years, 8 months 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
(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 pictures 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 +
vmpstr 2017/04/12 21:47:12 Magic!
mtklein_C 2017/04/12 22:32:00 Sure, but if you want real magic, write this as +1
enne (OOO) 2017/04/12 22:41:30 Oh unary plus, I always forget you.
140 static_assert(kNumOpTypes == TYPES(M) 0, "Missing op in list");
141 #undef M
142
143 typedef void (*RasterFunction)(const PaintOp* op,
vmpstr 2017/04/12 21:47:12 using RasterFunction = ...
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 typedef void (*RasterAlphaFunction)(const PaintOp* op,
vmpstr 2017/04/12 21:47:12 using
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 typedef void (*VoidFunction)(PaintOp* op);
vmpstr 2017/04/12 21:47:12 using
169 #define M(T) \
170 !std::is_trivially_destructible<T>::value \
171 ? [](PaintOp* op) { static_cast<T*>(op)->~T(); } \
172 : static_cast<VoidFunction>(nullptr),
vmpstr 2017/04/12 21:47:12 Maybe this can be [](PaintOp* op) {} and not have
enne (OOO) 2017/04/12 22:41:30 Left a TODO to evaluate the performance of this at
mtklein_C 2017/04/12 23:17:06 I have been thinking about doing the same for SkLi
173 static const VoidFunction g_destructor_functions[kNumOpTypes] = {TYPES(M)};
174 #undef M
175
176 #define M(T) T::kIsDrawOp,
177 static bool g_is_draw_op[kNumOpTypes] = {TYPES(M)};
178 #undef M
179
180 #define M(T) \
181 static_assert(sizeof(T) <= sizeof(LargestPaintOp), \
182 #T " must be no bigger than LargestPaintOp");
183 TYPES(M);
184 #undef M
185
186 #undef TYPES
187
188 SkRect PaintOp::kUnsetRect = {SK_ScalarInfinity, 0, 0, 0};
189
190 void AnnotateOp::Raster(SkCanvas* canvas) const {
191 switch (annotation_type) {
192 case PaintCanvas::AnnotationType::URL:
193 SkAnnotateRectWithURL(canvas, rect, data.get());
194 break;
195 case PaintCanvas::AnnotationType::LINK_TO_DESTINATION:
196 SkAnnotateLinkToDestination(canvas, rect, data.get());
197 break;
198 case PaintCanvas::AnnotationType::NAMED_DESTINATION: {
199 SkPoint point = SkPoint::Make(rect.x(), rect.y());
200 SkAnnotateNamedDestination(canvas, point, data.get());
201 break;
202 }
203 }
204 }
205
206 void ClipPathOp::Raster(SkCanvas* canvas) const {
207 canvas->clipPath(path, op, antialias);
208 }
209
210 void ClipRectOp::Raster(SkCanvas* canvas) const {
211 canvas->clipRect(rect, op, antialias);
212 }
213
214 void ClipRRectOp::Raster(SkCanvas* canvas) const {
215 canvas->clipRRect(rrect, op, antialias);
216 }
217
218 void ConcatOp::Raster(SkCanvas* canvas) const {
219 canvas->concat(matrix);
220 }
221
222 void DrawArcOp::RasterWithFlags(SkCanvas* canvas,
223 const PaintFlags& flags) const {
224 canvas->drawArc(oval, start_angle, sweep_angle, use_center, ToSkPaint(flags));
225 }
226
227 void DrawCircleOp::RasterWithFlags(SkCanvas* canvas,
228 const PaintFlags& flags) const {
229 canvas->drawCircle(cx, cy, radius, ToSkPaint(flags));
230 }
231
232 void DrawColorOp::Raster(SkCanvas* canvas) const {
233 canvas->drawColor(color, mode);
234 }
235
236 void DrawDisplayItemListOp::Raster(SkCanvas* canvas) const {
237 list->Raster(canvas, nullptr);
238 }
239
240 void DrawDRRectOp::RasterWithFlags(SkCanvas* canvas,
241 const PaintFlags& flags) const {
242 canvas->drawDRRect(outer, inner, ToSkPaint(flags));
243 }
244
245 void DrawImageOp::RasterWithFlags(SkCanvas* canvas,
246 const PaintFlags& flags) const {
247 canvas->drawImage(image.get(), left, top, ToSkPaint(&flags));
248 }
249
250 void DrawImageRectOp::RasterWithFlags(SkCanvas* canvas,
251 const PaintFlags& flags) const {
252 // TODO(enne): Probably PaintCanvas should just use the skia enum directly.
253 SkCanvas::SrcRectConstraint skconstraint =
254 static_cast<SkCanvas::SrcRectConstraint>(constraint);
255 canvas->drawImageRect(image.get(), src, dst, ToSkPaint(&flags), skconstraint);
256 }
257
258 void DrawIRectOp::RasterWithFlags(SkCanvas* canvas,
259 const PaintFlags& flags) const {
260 canvas->drawIRect(rect, ToSkPaint(flags));
261 }
262
263 void DrawLineOp::RasterWithFlags(SkCanvas* canvas,
264 const PaintFlags& flags) const {
265 canvas->drawLine(x0, y0, x1, y1, ToSkPaint(flags));
266 }
267
268 void DrawOvalOp::RasterWithFlags(SkCanvas* canvas,
269 const PaintFlags& flags) const {
270 canvas->drawOval(oval, ToSkPaint(flags));
271 }
272
273 void DrawPathOp::RasterWithFlags(SkCanvas* canvas,
274 const PaintFlags& flags) const {
275 canvas->drawPath(path, ToSkPaint(flags));
276 }
277
278 void DrawPosTextOp::RasterWithFlags(SkCanvas* canvas,
279 const PaintFlags& flags) const {
280 canvas->drawPosText(paint_op_data(this), bytes, paint_op_array<SkPoint>(this),
281 ToSkPaint(flags));
282 }
283
284 void DrawRecordOp::Raster(SkCanvas* canvas) const {
285 record->playback(canvas);
286 }
287
288 void DrawRectOp::RasterWithFlags(SkCanvas* canvas,
289 const PaintFlags& flags) const {
290 canvas->drawRect(rect, ToSkPaint(flags));
291 }
292
293 void DrawRRectOp::RasterWithFlags(SkCanvas* canvas,
294 const PaintFlags& flags) const {
295 canvas->drawRRect(rrect, ToSkPaint(flags));
296 }
297
298 void DrawTextOp::RasterWithFlags(SkCanvas* canvas,
299 const PaintFlags& flags) const {
300 canvas->drawText(paint_op_data(this), bytes, x, y, ToSkPaint(flags));
301 }
302
303 void DrawTextBlobOp::RasterWithFlags(SkCanvas* canvas,
304 const PaintFlags& flags) const {
305 canvas->drawTextBlob(blob.get(), x, y, ToSkPaint(flags));
306 }
307
308 void RestoreOp::Raster(SkCanvas* canvas) const {
309 canvas->restore();
310 }
311
312 void RotateOp::Raster(SkCanvas* canvas) const {
313 canvas->rotate(degrees);
314 }
315
316 void SaveOp::Raster(SkCanvas* canvas) const {
317 canvas->save();
318 }
319
320 void SaveLayerOp::RasterWithFlags(SkCanvas* canvas,
321 const PaintFlags& flags) const {
322 // See PaintOp::kUnsetRect
323 bool unset = bounds.left() == SK_ScalarInfinity;
324
325 canvas->saveLayer(unset ? nullptr : &bounds, ToSkPaint(&flags));
326 }
327
328 void SaveLayerAlphaOp::Raster(SkCanvas* canvas) const {
329 // See PaintOp::kUnsetRect
330 bool unset = bounds.left() == SK_ScalarInfinity;
331 canvas->saveLayerAlpha(unset ? nullptr : &bounds, alpha);
332 }
333
334 void ScaleOp::Raster(SkCanvas* canvas) const {
335 canvas->scale(sx, sy);
336 }
337
338 void SetMatrixOp::Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const {
339 canvas->setMatrix(SkMatrix::Concat(original_ctm, matrix));
340 }
341
342 void TranslateOp::Raster(SkCanvas* canvas) const {
343 canvas->translate(dx, dy);
344 }
345
346 bool PaintOp::IsDrawOp() const {
347 return g_is_draw_op[type];
348 }
349
350 void PaintOp::Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const {
351 g_raster_functions[type](this, canvas, original_ctm);
352 }
353
354 void PaintOp::RasterWithAlpha(SkCanvas* canvas, uint8_t alpha) const {
355 g_raster_alpha_functions[type](this, canvas, alpha);
356 }
357
358 DrawDisplayItemListOp::DrawDisplayItemListOp(
359 scoped_refptr<DisplayItemList> list)
360 : list(list) {}
361
362 size_t DrawDisplayItemListOp::AdditionalBytesUsed() const {
363 return list->ApproximateMemoryUsage();
364 }
365
366 int ClipPathOp::CountSlowPaths() const {
367 return antialias && !path.isConvex() ? 1 : 0;
368 }
369
370 int DrawLineOp::CountSlowPaths() const {
371 if (const SkPathEffect* effect = flags.getPathEffect()) {
372 SkPathEffect::DashInfo info;
373 SkPathEffect::DashType dashType = effect->asADash(&info);
374 if (flags.getStrokeCap() != PaintFlags::kRound_Cap &&
375 dashType == SkPathEffect::kDash_DashType && info.fCount == 2) {
376 // The PaintFlags will count this as 1, so uncount that here as
377 // this kind of line is special cased and not slow.
378 return -1;
379 }
380 }
381 return 0;
382 }
383
384 int DrawPathOp::CountSlowPaths() const {
385 // TODO(enne): It's not the best to have these Skia details exposed here,
386 // but hopefully the veto is shortlived and this can go away.
387 if (flags.isAntiAlias() && !path.isConvex()) {
388 PaintFlags::Style paintStyle = flags.getStyle();
389 const SkRect& pathBounds = path.getBounds();
390 if (paintStyle == PaintFlags::kStroke_Style &&
vmpstr 2017/04/12 21:47:12 nit: this is kind of a weird construct. Why this i
enne (OOO) 2017/04/12 22:41:30 This comes from SkPathCounter. I think there's th
391 flags.getStrokeWidth() == 0) {
392 // AA hairline concave path is not slow.
393 } else if (paintStyle == PaintFlags::kFill_Style &&
394 pathBounds.width() < 64.f && pathBounds.height() < 64.f &&
395 !path.isVolatile()) {
396 // AADF eligible concave path is not slow.
397 } else {
398 return 1;
399 }
400 }
401 return 0;
402 }
403
404 AnnotateOp::AnnotateOp(PaintCanvas::AnnotationType annotation_type,
405 const SkRect& rect,
406 sk_sp<SkData> data)
407 : annotation_type(annotation_type), rect(rect), data(std::move(data)) {}
408
409 AnnotateOp::~AnnotateOp() = default;
410
411 DrawDisplayItemListOp::~DrawDisplayItemListOp() = default;
412
413 DrawImageOp::DrawImageOp(sk_sp<const SkImage> image,
414 SkScalar left,
415 SkScalar top,
416 const PaintFlags* flags)
417 : image(std::move(image)), left(left), top(top) {
418 if (flags)
419 this->flags = *flags;
420 }
421
422 DrawImageOp::~DrawImageOp() = default;
423
424 DrawImageRectOp::DrawImageRectOp(sk_sp<const SkImage> image,
425 const SkRect& src,
426 const SkRect& dst,
427 const PaintFlags* flags,
428 PaintCanvas::SrcRectConstraint constraint)
429 : image(std::move(image)), src(src), dst(dst), constraint(constraint) {
430 if (flags)
vmpstr 2017/04/12 21:47:12 You have this pattern a few places. I don't really
mtklein_C 2017/04/12 22:32:00 (No, it's probably better to put it in the initial
enne (OOO) 2017/04/12 22:41:30 Sure, can do.
431 this->flags = *flags;
432 }
433
434 DrawImageRectOp::~DrawImageRectOp() = default;
435
436 DrawPosTextOp::DrawPosTextOp(size_t bytes,
437 size_t count,
438 const PaintFlags& flags)
439 : PaintOpWithDataArray(bytes, count), flags(flags) {}
440
441 DrawPosTextOp::~DrawPosTextOp() = default;
442
443 DrawRecordOp::DrawRecordOp(sk_sp<const PaintRecord> record)
444 : record(std::move(record)) {}
445
446 DrawRecordOp::~DrawRecordOp() = default;
447
448 size_t DrawRecordOp::AdditionalBytesUsed() const {
449 return record->approximateBytesUsed();
450 }
451
452 DrawTextBlobOp::DrawTextBlobOp(sk_sp<SkTextBlob> blob,
453 SkScalar x,
454 SkScalar y,
455 const PaintFlags& flags)
456 : blob(std::move(blob)), x(x), y(y), flags(flags) {}
457
458 DrawTextBlobOp::~DrawTextBlobOp() = default;
459
460 PaintOpBuffer::PaintOpBuffer() : cull_rect_(SkRect::MakeEmpty()) {}
461
462 PaintOpBuffer::PaintOpBuffer(const SkRect& cull_rect) : cull_rect_(cull_rect) {}
463
464 PaintOpBuffer::~PaintOpBuffer() {
465 Reset();
466 }
467
468 void PaintOpBuffer::Reset() {
469 for (auto* op : Iterator(this)) {
470 auto func = g_destructor_functions[op->type];
471 if (func)
472 func(op);
473 }
474
475 // Leave data_ allocated, reserved_ unchanged.
476 used_ = 0;
477 op_count_ = 0;
478 num_slow_paths_ = 0;
479 }
480
481 void PaintOpBuffer::playback(SkCanvas* canvas) const {
482 // TODO(enne): a PaintRecord that contains a SetMatrix assumes that the
483 // SetMatrix is local to that PaintRecord itself. Said differently, if you
484 // translate(x, y), then draw a paint record with a SetMatrix(identity),
485 // the translation should be preserved instead of clobbering the top level
486 // transform. This could probably be done more efficiently.
487 SkMatrix original = canvas->getTotalMatrix();
488
489 for (Iterator iter(this); iter; ++iter) {
490 // Optimize out save/restores or save/draw/restore that can be a single
491 // draw. See also: similar code in SkRecordOpts and cc's DisplayItemList.
492 // TODO(enne): consider making this recursive?
493 const PaintOp* op = *iter;
494 if (op->GetType() == PaintOpType::SaveLayerAlpha) {
495 const PaintOp* second = iter.peek1();
496 if (second) {
497 if (second->GetType() == PaintOpType::Restore) {
498 ++iter;
499 continue;
500 }
501 if (second->IsDrawOp()) {
502 const PaintOp* third = iter.peek2();
503 if (third && third->GetType() == PaintOpType::Restore) {
504 const SaveLayerAlphaOp* save_op =
505 static_cast<const SaveLayerAlphaOp*>(op);
506 second->RasterWithAlpha(canvas, save_op->alpha);
507 ++iter;
508 ++iter;
509 continue;
510 }
511 }
512 }
513 }
514 // TODO(enne): skip SaveLayer followed by restore with nothing in
515 // between, however SaveLayer with image filters on it (or maybe
516 // other PaintFlags options) are not a noop. Figure out what these
517 // are so we can skip them correctly.
518
519 op->Raster(canvas, original);
520 }
521 }
522
523 void PaintOpBuffer::playback(SkCanvas* canvas,
524 SkPicture::AbortCallback* callback) const {
525 // The abort callback is only used for analysis, in general, so
526 // this playback code can be more straightforward and not do the
527 // optimizations in the other function.
528 if (!callback) {
529 playback(canvas);
530 return;
531 }
532
533 SkMatrix original = canvas->getTotalMatrix();
534
535 // TODO(enne): ideally callers would just iterate themselves and we
536 // can remove the entire notion of an abort callback.
vmpstr 2017/04/12 21:47:12 +1
537 for (auto* op : Iterator(this)) {
538 op->Raster(canvas, original);
539 if (callback && callback->abort())
540 return;
541 }
542 }
543
544 void PaintOpBuffer::Compact() {
vmpstr 2017/04/12 21:47:12 Maybe ShrinkToFit (it's kind of a common name I th
545 if (!used_ || used_ == reserved_)
546 return;
547 data_.realloc(used_);
548 reserved_ = used_;
549 }
550
551 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698