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

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

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

Powered by Google App Engine
This is Rietveld 408576698