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

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

Issue 2813303007: Revert of Back PaintRecord with PaintOpBuffer instead of SkPicture (Closed)
Patch Set: 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
« no previous file with comments | « cc/paint/paint_op_buffer.h ('k') | cc/paint/paint_op_buffer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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.sk_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.sk_image().get(), src, dst, ToSkPaint(&flags),
258 skconstraint);
259 }
260
261 void DrawIRectOp::RasterWithFlags(SkCanvas* canvas,
262 const PaintFlags& flags) const {
263 canvas->drawIRect(rect, ToSkPaint(flags));
264 }
265
266 void DrawLineOp::RasterWithFlags(SkCanvas* canvas,
267 const PaintFlags& flags) const {
268 canvas->drawLine(x0, y0, x1, y1, ToSkPaint(flags));
269 }
270
271 void DrawOvalOp::RasterWithFlags(SkCanvas* canvas,
272 const PaintFlags& flags) const {
273 canvas->drawOval(oval, ToSkPaint(flags));
274 }
275
276 void DrawPathOp::RasterWithFlags(SkCanvas* canvas,
277 const PaintFlags& flags) const {
278 canvas->drawPath(path, ToSkPaint(flags));
279 }
280
281 void DrawPosTextOp::RasterWithFlags(SkCanvas* canvas,
282 const PaintFlags& flags) const {
283 canvas->drawPosText(paint_op_data(this), bytes, paint_op_array<SkPoint>(this),
284 ToSkPaint(flags));
285 }
286
287 void DrawRecordOp::Raster(SkCanvas* canvas) const {
288 record->playback(canvas);
289 }
290
291 void DrawRectOp::RasterWithFlags(SkCanvas* canvas,
292 const PaintFlags& flags) const {
293 canvas->drawRect(rect, ToSkPaint(flags));
294 }
295
296 void DrawRRectOp::RasterWithFlags(SkCanvas* canvas,
297 const PaintFlags& flags) const {
298 canvas->drawRRect(rrect, ToSkPaint(flags));
299 }
300
301 void DrawTextOp::RasterWithFlags(SkCanvas* canvas,
302 const PaintFlags& flags) const {
303 canvas->drawText(paint_op_data(this), bytes, x, y, ToSkPaint(flags));
304 }
305
306 void DrawTextBlobOp::RasterWithFlags(SkCanvas* canvas,
307 const PaintFlags& flags) const {
308 canvas->drawTextBlob(blob.get(), x, y, ToSkPaint(flags));
309 }
310
311 void RestoreOp::Raster(SkCanvas* canvas) const {
312 canvas->restore();
313 }
314
315 void RotateOp::Raster(SkCanvas* canvas) const {
316 canvas->rotate(degrees);
317 }
318
319 void SaveOp::Raster(SkCanvas* canvas) const {
320 canvas->save();
321 }
322
323 void SaveLayerOp::RasterWithFlags(SkCanvas* canvas,
324 const PaintFlags& flags) const {
325 // See PaintOp::kUnsetRect
326 bool unset = bounds.left() == SK_ScalarInfinity;
327
328 canvas->saveLayer(unset ? nullptr : &bounds, ToSkPaint(&flags));
329 }
330
331 void SaveLayerAlphaOp::Raster(SkCanvas* canvas) const {
332 // See PaintOp::kUnsetRect
333 bool unset = bounds.left() == SK_ScalarInfinity;
334 canvas->saveLayerAlpha(unset ? nullptr : &bounds, alpha);
335 }
336
337 void ScaleOp::Raster(SkCanvas* canvas) const {
338 canvas->scale(sx, sy);
339 }
340
341 void SetMatrixOp::Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const {
342 canvas->setMatrix(SkMatrix::Concat(original_ctm, matrix));
343 }
344
345 void TranslateOp::Raster(SkCanvas* canvas) const {
346 canvas->translate(dx, dy);
347 }
348
349 bool PaintOp::IsDrawOp() const {
350 return g_is_draw_op[type];
351 }
352
353 void PaintOp::Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const {
354 g_raster_functions[type](this, canvas, original_ctm);
355 }
356
357 void PaintOp::RasterWithAlpha(SkCanvas* canvas, uint8_t alpha) const {
358 g_raster_alpha_functions[type](this, canvas, alpha);
359 }
360
361 DrawDisplayItemListOp::DrawDisplayItemListOp(
362 scoped_refptr<DisplayItemList> list)
363 : list(list) {}
364
365 size_t DrawDisplayItemListOp::AdditionalBytesUsed() const {
366 return list->ApproximateMemoryUsage();
367 }
368
369 int ClipPathOp::CountSlowPaths() const {
370 return antialias && !path.isConvex() ? 1 : 0;
371 }
372
373 int DrawLineOp::CountSlowPaths() const {
374 if (const SkPathEffect* effect = flags.getPathEffect()) {
375 SkPathEffect::DashInfo info;
376 SkPathEffect::DashType dashType = effect->asADash(&info);
377 if (flags.getStrokeCap() != PaintFlags::kRound_Cap &&
378 dashType == SkPathEffect::kDash_DashType && info.fCount == 2) {
379 // The PaintFlags will count this as 1, so uncount that here as
380 // this kind of line is special cased and not slow.
381 return -1;
382 }
383 }
384 return 0;
385 }
386
387 int DrawPathOp::CountSlowPaths() const {
388 // This logic is copied from SkPathCounter instead of attempting to expose
389 // that from Skia.
390 if (!flags.isAntiAlias() || path.isConvex())
391 return 0;
392
393 PaintFlags::Style paintStyle = flags.getStyle();
394 const SkRect& pathBounds = path.getBounds();
395 if (paintStyle == PaintFlags::kStroke_Style && flags.getStrokeWidth() == 0) {
396 // AA hairline concave path is not slow.
397 return 0;
398 } else if (paintStyle == PaintFlags::kFill_Style &&
399 pathBounds.width() < 64.f && pathBounds.height() < 64.f &&
400 !path.isVolatile()) {
401 // AADF eligible concave path is not slow.
402 return 0;
403 } else {
404 return 1;
405 }
406 }
407
408 AnnotateOp::AnnotateOp(PaintCanvas::AnnotationType annotation_type,
409 const SkRect& rect,
410 sk_sp<SkData> data)
411 : annotation_type(annotation_type), rect(rect), data(std::move(data)) {}
412
413 AnnotateOp::~AnnotateOp() = default;
414
415 DrawDisplayItemListOp::~DrawDisplayItemListOp() = default;
416
417 DrawImageOp::DrawImageOp(const PaintImage& image,
418 SkScalar left,
419 SkScalar top,
420 const PaintFlags* flags)
421 : image(image),
422 left(left),
423 top(top),
424 flags(flags ? *flags : PaintFlags()) {}
425
426 DrawImageOp::~DrawImageOp() = default;
427
428 DrawImageRectOp::DrawImageRectOp(const PaintImage& image,
429 const SkRect& src,
430 const SkRect& dst,
431 const PaintFlags* flags,
432 PaintCanvas::SrcRectConstraint constraint)
433 : image(image),
434 flags(flags ? *flags : PaintFlags()),
435 src(src),
436 dst(dst),
437 constraint(constraint) {}
438
439 DrawImageRectOp::~DrawImageRectOp() = default;
440
441 DrawPosTextOp::DrawPosTextOp(size_t bytes,
442 size_t count,
443 const PaintFlags& flags)
444 : PaintOpWithDataArray(bytes, count), flags(flags) {}
445
446 DrawPosTextOp::~DrawPosTextOp() = default;
447
448 DrawRecordOp::DrawRecordOp(sk_sp<const PaintRecord> record)
449 : record(std::move(record)) {}
450
451 DrawRecordOp::~DrawRecordOp() = default;
452
453 size_t DrawRecordOp::AdditionalBytesUsed() const {
454 return record->approximateBytesUsed();
455 }
456
457 DrawTextBlobOp::DrawTextBlobOp(sk_sp<SkTextBlob> blob,
458 SkScalar x,
459 SkScalar y,
460 const PaintFlags& flags)
461 : blob(std::move(blob)), x(x), y(y), flags(flags) {}
462
463 DrawTextBlobOp::~DrawTextBlobOp() = default;
464
465 PaintOpBuffer::PaintOpBuffer() : cull_rect_(SkRect::MakeEmpty()) {}
466
467 PaintOpBuffer::PaintOpBuffer(const SkRect& cull_rect) : cull_rect_(cull_rect) {}
468
469 PaintOpBuffer::~PaintOpBuffer() {
470 Reset();
471 }
472
473 void PaintOpBuffer::Reset() {
474 for (auto* op : Iterator(this)) {
475 auto func = g_destructor_functions[op->type];
476 if (func)
477 func(op);
478 }
479
480 // Leave data_ allocated, reserved_ unchanged.
481 used_ = 0;
482 op_count_ = 0;
483 num_slow_paths_ = 0;
484 }
485
486 void PaintOpBuffer::playback(SkCanvas* canvas) const {
487 // TODO(enne): a PaintRecord that contains a SetMatrix assumes that the
488 // SetMatrix is local to that PaintRecord itself. Said differently, if you
489 // translate(x, y), then draw a paint record with a SetMatrix(identity),
490 // the translation should be preserved instead of clobbering the top level
491 // transform. This could probably be done more efficiently.
492 SkMatrix original = canvas->getTotalMatrix();
493
494 for (Iterator iter(this); iter; ++iter) {
495 // Optimize out save/restores or save/draw/restore that can be a single
496 // draw. See also: similar code in SkRecordOpts and cc's DisplayItemList.
497 // TODO(enne): consider making this recursive?
498 const PaintOp* op = *iter;
499 if (op->GetType() == PaintOpType::SaveLayerAlpha) {
500 const PaintOp* second = iter.peek1();
501 if (second) {
502 if (second->GetType() == PaintOpType::Restore) {
503 ++iter;
504 continue;
505 }
506 if (second->IsDrawOp()) {
507 const PaintOp* third = iter.peek2();
508 if (third && third->GetType() == PaintOpType::Restore) {
509 const SaveLayerAlphaOp* save_op =
510 static_cast<const SaveLayerAlphaOp*>(op);
511 second->RasterWithAlpha(canvas, save_op->alpha);
512 ++iter;
513 ++iter;
514 continue;
515 }
516 }
517 }
518 }
519 // TODO(enne): skip SaveLayer followed by restore with nothing in
520 // between, however SaveLayer with image filters on it (or maybe
521 // other PaintFlags options) are not a noop. Figure out what these
522 // are so we can skip them correctly.
523
524 op->Raster(canvas, original);
525 }
526 }
527
528 void PaintOpBuffer::playback(SkCanvas* canvas,
529 SkPicture::AbortCallback* callback) const {
530 // The abort callback is only used for analysis, in general, so
531 // this playback code can be more straightforward and not do the
532 // optimizations in the other function.
533 if (!callback) {
534 playback(canvas);
535 return;
536 }
537
538 SkMatrix original = canvas->getTotalMatrix();
539
540 // TODO(enne): ideally callers would just iterate themselves and we
541 // can remove the entire notion of an abort callback.
542 for (auto* op : Iterator(this)) {
543 op->Raster(canvas, original);
544 if (callback && callback->abort())
545 return;
546 }
547 }
548
549 void PaintOpBuffer::ShrinkToFit() {
550 if (!used_ || used_ == reserved_)
551 return;
552 data_.realloc(used_);
553 reserved_ = used_;
554 }
555
556 } // namespace cc
OLDNEW
« no previous file with comments | « cc/paint/paint_op_buffer.h ('k') | cc/paint/paint_op_buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698