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

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

Issue 2768143002: Back PaintRecord with PaintOpBuffer instead of SkPicture (Closed)
Patch Set: Fix win build 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 +
140 static_assert(kNumOpTypes == TYPES(M) 0, "Missing op in list");
141 #undef M
142
143 typedef void (*RasterFunction)(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 typedef void (*RasterAlphaFunction)(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 typedef void (*VoidFunction)(PaintOp* op);
169 #define M(T) \
170 !std::is_trivially_destructible<T>::value \
171 ? [](PaintOp* op) { static_cast<T*>(op)->~T(); } \
172 : static_cast<VoidFunction>(nullptr),
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 &&
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(data) {}
vmpstr 2017/04/06 19:16:12 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(image), src(src), dst(dst), constraint(constraint) {
vmpstr 2017/04/06 19:16:12 std::move(image)
430 if (flags)
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) : record(record) {}
vmpstr 2017/04/06 19:16:12 std::move(record)
444
445 DrawRecordOp::~DrawRecordOp() = default;
446
447 size_t DrawRecordOp::AdditionalBytesUsed() const {
448 return record->approximateBytesUsed();
449 }
450
451 DrawTextBlobOp::DrawTextBlobOp(sk_sp<SkTextBlob> blob,
452 SkScalar x,
453 SkScalar y,
454 const PaintFlags& flags)
455 : blob(blob), x(x), y(y), flags(flags) {}
vmpstr 2017/04/06 19:16:12 std::move(blob)
456
457 DrawTextBlobOp::~DrawTextBlobOp() = default;
458
459 PaintOpBuffer::PaintOpBuffer() : cull_rect_(SkRect::MakeEmpty()) {}
460
461 PaintOpBuffer::PaintOpBuffer(const SkRect& cull_rect) : cull_rect_(cull_rect) {}
462
463 PaintOpBuffer::~PaintOpBuffer() {
464 Reset();
465 }
466
467 void PaintOpBuffer::Reset() {
468 for (auto* op : Iterator(this)) {
469 auto func = g_destructor_functions[op->type];
470 if (func)
471 func(op);
472 }
473
474 // Leave data_ allocated, reserved_ unchanged.
475 used_ = 0;
476 op_count_ = 0;
477 num_slow_paths_ = 0;
478 }
479
480 void PaintOpBuffer::playback(SkCanvas* canvas) const {
481 // TODO(enne): a PaintRecord that contains a SetMatrix assumes that the
482 // SetMatrix is local to that PaintRecord itself. Said differently, if you
483 // translate(x, y), then draw a paint record with a SetMatrix(identity),
484 // the translation should be preserved instead of clobbering the top level
485 // transform. This could probably be done more efficiently.
486 SkMatrix original = canvas->getTotalMatrix();
487
488 for (Iterator iter(this); iter; ++iter) {
489 // Optimize out save/restores or save/draw/restore that can be a single
490 // draw. See also: similar code in SkRecordOpts and cc's DisplayItemList.
491 // TODO(enne): consider making this recursive?
492 const PaintOp* op = *iter;
493 if (op->GetType() == PaintOpType::SaveLayerAlpha) {
494 const PaintOp* second = iter.peek1();
vmpstr 2017/04/06 19:16:12 future thought: maybe peek can return an iterator,
enne (OOO) 2017/04/06 23:17:05 If it's ever the case that n > 2, I'll change this
495 if (second) {
496 if (second->GetType() == PaintOpType::Restore) {
497 ++iter;
498 continue;
499 }
500 if (second->IsDrawOp()) {
501 const PaintOp* third = iter.peek2();
502 if (third && third->GetType() == PaintOpType::Restore) {
503 const SaveLayerAlphaOp* save_op =
504 static_cast<const SaveLayerAlphaOp*>(op);
505 second->RasterWithAlpha(canvas, save_op->alpha);
506 ++iter;
507 ++iter;
508 continue;
509 }
510 }
511 }
512 }
513 // TODO(enne): skip SaveLayer followed by restore with nothing in
514 // between, however SaveLayer with image filters on it (or maybe
515 // other PaintFlags options) are not a noop. Figure out what these
516 // are so we can skip them correctly.
517
518 op->Raster(canvas, original);
519 }
520 }
521
522 void PaintOpBuffer::playback(SkCanvas* canvas,
523 SkPicture::AbortCallback* callback) const {
524 // The abort callback is only used for analysis, in general, so
525 // this playback code can be more straightforward and not do the
526 // optimizations in the other function.
527 if (!callback) {
528 playback(canvas);
529 return;
530 }
531
532 SkMatrix original = canvas->getTotalMatrix();
533
534 // TODO(enne): ideally callers would just iterate themselves and we
535 // can remove the entire notion of an abort callback.
536 for (auto* op : Iterator(this)) {
537 op->Raster(canvas, original);
538 if (callback && callback->abort())
539 return;
540 }
541 }
542
543 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698