1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
cc / paint / record_paint_canvas.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/paint/record_paint_canvas.h"
#include <limits>
#include <utility>
#include "base/containers/span.h"
#include "cc/paint/paint_filter.h"
#include "cc/paint/paint_flags.h"
#include "cc/paint/paint_image_builder.h"
#include "cc/paint/paint_op.h"
#include "cc/paint/paint_record.h"
#include "cc/paint/paint_recorder.h"
#include "cc/paint/skottie_frame_data.h"
#include "cc/paint/skottie_wrapper.h"
#include "third_party/skia/include/core/SkAnnotation.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkTextBlob.h"
#include "third_party/skia/include/utils/SkNWayCanvas.h"
namespace cc {
RecordPaintCanvas::RecordPaintCanvas() = default;
RecordPaintCanvas::~RecordPaintCanvas() = default;
PaintRecord RecordPaintCanvas::ReleaseAsRecord() {
// Some users expect that their saves are automatically closed for them.
// Maybe we could remove this assumption and just have callers do it.
restoreToCount(1);
needs_flush_ = false;
return buffer_.ReleaseAsRecord();
}
void RecordPaintCanvas::DisableLineDrawingAsPaths() {
maybe_draw_lines_as_paths_ = false;
draw_path_count_ = draw_line_count_ = 0;
}
PaintRecord RecordPaintCanvas::CopyAsRecord() {
needs_flush_ = false;
return buffer_.DeepCopyAsRecord();
}
template <typename T, typename... Args>
void RecordPaintCanvas::push(Args&&... args) {
#if DCHECK_IS_ON()
// The following check fails if client code does not check and handle
// NeedsFlush() before issuing draw calls.
// Note: restore ops are tolerated when flushes are requested since they are
// often necessary in order to bring the canvas to a flushable state.
// SetNodeId ops are also tolerated because they may be inserted just before
// flushing.
DCHECK(disable_flush_check_scope_ || !needs_flush_ ||
(std::is_same<T, RestoreOp>::value) ||
(std::is_same<T, SetNodeIdOp>::value));
#endif
buffer_.push<T>(std::forward<Args>(args)...);
}
void* RecordPaintCanvas::accessTopLayerPixels(SkImageInfo* info,
size_t* rowBytes,
SkIPoint* origin) {
// Modifications to the underlying pixels cannot be saved.
return nullptr;
}
void RecordPaintCanvas::flush() {
// RecordPaintCanvas is unable to flush its own recording into the graphics
// pipeline. So instead we make note of the flush request so that it can be
// handled by code that owns the recording.
//
// Note: The value of needs_flush_ never gets reset until the end of
// recording. That is because flushing a recording implies ReleaseAsRecord()
// and starting a new recording.
needs_flush_ = true;
}
bool RecordPaintCanvas::NeedsFlush() const {
return needs_flush_;
}
int RecordPaintCanvas::save() {
push<SaveOp>();
return save_count_++;
}
int RecordPaintCanvas::saveLayer(const PaintFlags& flags) {
push<SaveLayerOp>(flags);
return save_count_++;
}
int RecordPaintCanvas::saveLayer(const SkRect& bounds,
const PaintFlags& flags) {
push<SaveLayerOp>(bounds, flags);
return save_count_++;
}
int RecordPaintCanvas::saveLayerAlphaf(float alpha) {
push<SaveLayerAlphaOp>(alpha);
return save_count_++;
}
int RecordPaintCanvas::saveLayerAlphaf(const SkRect& bounds, float alpha) {
push<SaveLayerAlphaOp>(bounds, alpha);
return save_count_++;
}
int RecordPaintCanvas::saveLayerFilters(
base::span<const sk_sp<PaintFilter>> filters,
const PaintFlags& flags) {
push<SaveLayerFiltersOp>(filters, flags);
return save_count_++;
}
void RecordPaintCanvas::restore() {
push<RestoreOp>();
--save_count_;
DCHECK_GE(save_count_, 1);
}
int RecordPaintCanvas::getSaveCount() const {
return save_count_;
}
void RecordPaintCanvas::restoreToCount(int save_count) {
DCHECK_GE(save_count, 1);
int diff = getSaveCount() - save_count;
DCHECK_GE(diff, 0);
for (int i = 0; i < diff; ++i)
restore();
}
void RecordPaintCanvas::translate(SkScalar dx, SkScalar dy) {
push<TranslateOp>(dx, dy);
}
void RecordPaintCanvas::scale(SkScalar sx, SkScalar sy) {
push<ScaleOp>(sx, sy);
}
void RecordPaintCanvas::rotate(SkScalar degrees) {
push<RotateOp>(degrees);
}
void RecordPaintCanvas::concat(const SkM44& matrix) {
push<ConcatOp>(matrix);
}
void RecordPaintCanvas::setMatrix(const SkM44& matrix) {
push<SetMatrixOp>(matrix);
}
void RecordPaintCanvas::clipRect(const SkRect& rect,
SkClipOp op,
bool antialias) {
push<ClipRectOp>(rect, op, antialias);
}
void RecordPaintCanvas::clipRRect(const SkRRect& rrect,
SkClipOp op,
bool antialias) {
if (rrect.isRect()) {
clipRect(rrect.getBounds(), op, antialias);
return;
}
clipRRectInternal(rrect, op, antialias);
}
void RecordPaintCanvas::clipRRectInternal(const SkRRect& rrect,
SkClipOp op,
bool antialias) {
push<ClipRRectOp>(rrect, op, antialias);
}
void RecordPaintCanvas::clipPath(const SkPath& path,
SkClipOp op,
bool antialias,
UsePaintCache use_paint_cache) {
if (!path.isInverseFillType()) {
SkRect rect;
if (path.isRect(&rect)) {
clipRect(rect, op, antialias);
return;
}
SkRRect rrect;
if (path.isOval(&rect)) {
rrect.setOval(rect);
clipRRect(rrect, op, antialias);
return;
}
if (path.isRRect(&rrect)) {
clipRRect(rrect, op, antialias);
return;
}
}
clipPathInternal(path, op, antialias, use_paint_cache);
}
void RecordPaintCanvas::clipPathInternal(const SkPath& path,
SkClipOp op,
bool antialias,
UsePaintCache use_paint_cache) {
push<ClipPathOp>(path, op, antialias, use_paint_cache);
}
SkImageInfo RecordPaintCanvas::imageInfo() const {
NOTREACHED();
}
bool RecordPaintCanvas::getLocalClipBounds(SkRect* bounds) const {
NOTREACHED();
}
bool RecordPaintCanvas::getDeviceClipBounds(SkIRect* bounds) const {
NOTREACHED();
}
SkM44 RecordPaintCanvas::getLocalToDevice() const {
NOTREACHED();
}
void RecordPaintCanvas::drawColor(SkColor4f color, SkBlendMode mode) {
push<DrawColorOp>(color, mode);
}
void RecordPaintCanvas::clear(SkColor4f color) {
push<DrawColorOp>(color, SkBlendMode::kSrc);
}
void RecordPaintCanvas::drawLine(SkScalar x0,
SkScalar y0,
SkScalar x1,
SkScalar y1,
const PaintFlags& flags) {
if (maybe_draw_lines_as_paths_ &&
draw_line_count_ != std::numeric_limits<uint32_t>::max()) {
++draw_line_count_;
// If a bunch of paths have been drawn, only switch to drawing lines
// after a number of lines have been drawn.
if (draw_line_count_ > 4) {
draw_path_count_ = 0;
}
}
// TODO(crbug.com/5524058): investigate if it makes sense to add support for
// draw_path_count > 4 to the lite op.
if (draw_path_count_ <= 4 && AreLiteOpsEnabled() &&
flags.CanConvertToCorePaintFlags()) {
push<DrawLineLiteOp>(x0, y0, x1, y1, flags.ToCorePaintFlags());
return;
}
// Render lines as paths if there have been a number of drawPaths() recently.
// See description in header for more details.
push<DrawLineOp>(x0, y0, x1, y1, flags, draw_path_count_ > 4);
}
void RecordPaintCanvas::drawArc(const SkRect& oval,
SkScalar start_angle_degrees,
SkScalar sweep_angle_degrees,
const PaintFlags& flags) {
if (AreLiteOpsEnabled() && flags.CanConvertToCorePaintFlags()) {
push<DrawArcLiteOp>(oval, start_angle_degrees, sweep_angle_degrees,
flags.ToCorePaintFlags());
return;
}
push<DrawArcOp>(oval, start_angle_degrees, sweep_angle_degrees, flags);
}
void RecordPaintCanvas::drawRect(const SkRect& rect, const PaintFlags& flags) {
push<DrawRectOp>(rect, flags);
}
void RecordPaintCanvas::drawIRect(const SkIRect& rect,
const PaintFlags& flags) {
push<DrawIRectOp>(rect, flags);
}
void RecordPaintCanvas::drawOval(const SkRect& oval, const PaintFlags& flags) {
push<DrawOvalOp>(oval, flags);
}
void RecordPaintCanvas::drawRRect(const SkRRect& rrect,
const PaintFlags& flags) {
push<DrawRRectOp>(rrect, flags);
}
void RecordPaintCanvas::drawDRRect(const SkRRect& outer,
const SkRRect& inner,
const PaintFlags& flags) {
if (outer.isEmpty())
return;
if (inner.isEmpty()) {
drawRRect(outer, flags);
return;
}
push<DrawDRRectOp>(outer, inner, flags);
}
void RecordPaintCanvas::drawRoundRect(const SkRect& rect,
SkScalar rx,
SkScalar ry,
const PaintFlags& flags) {
// TODO(enne): move this into base class?
if (rx > 0 && ry > 0) {
SkRRect rrect;
rrect.setRectXY(rect, rx, ry);
drawRRect(rrect, flags);
} else {
drawRect(rect, flags);
}
}
void RecordPaintCanvas::drawPath(const SkPath& path,
const PaintFlags& flags,
UsePaintCache use_paint_cache) {
if (maybe_draw_lines_as_paths_ &&
draw_path_count_ != std::numeric_limits<uint32_t>::max()) {
++draw_path_count_;
if (draw_path_count_ > 4) {
draw_line_count_ = 0;
}
}
push<DrawPathOp>(path, flags, use_paint_cache);
}
void RecordPaintCanvas::drawImage(const PaintImage& image,
SkScalar left,
SkScalar top,
const SkSamplingOptions& sampling,
const PaintFlags* flags) {
DCHECK(!image.IsPaintWorklet());
push<DrawImageOp>(image, left, top, sampling, flags);
}
void RecordPaintCanvas::drawImageRect(const PaintImage& image,
const SkRect& src,
const SkRect& dst,
const SkSamplingOptions& sampling,
const PaintFlags* flags,
SkCanvas::SrcRectConstraint constraint) {
push<DrawImageRectOp>(image, src, dst, sampling, flags, constraint);
}
void RecordPaintCanvas::drawVertices(
scoped_refptr<RefCountedBuffer<SkPoint>> vertices,
scoped_refptr<RefCountedBuffer<SkPoint>> uvs,
scoped_refptr<RefCountedBuffer<uint16_t>> indices,
const PaintFlags& flags) {
push<DrawVerticesOp>(std::move(vertices), std::move(uvs), std::move(indices),
flags);
}
void RecordPaintCanvas::drawSkottie(scoped_refptr<SkottieWrapper> skottie,
const SkRect& dst,
float t,
SkottieFrameDataMap images,
const SkottieColorMap& color_map,
SkottieTextPropertyValueMap text_map) {
push<DrawSkottieOp>(std::move(skottie), dst, t, std::move(images), color_map,
std::move(text_map));
}
void RecordPaintCanvas::drawTextBlob(sk_sp<SkTextBlob> blob,
SkScalar x,
SkScalar y,
const PaintFlags& flags) {
push<DrawTextBlobOp>(std::move(blob), x, y, flags);
}
void RecordPaintCanvas::drawTextBlob(sk_sp<SkTextBlob> blob,
SkScalar x,
SkScalar y,
NodeId node_id,
const PaintFlags& flags) {
push<DrawTextBlobOp>(std::move(blob), x, y, node_id, flags);
}
void RecordPaintCanvas::drawPicture(PaintRecord record) {
// TODO(enne): If this is small, maybe flatten it?
push<DrawRecordOp>(std::move(record));
}
void RecordPaintCanvas::drawPicture(PaintRecord record, bool local_ctm) {
// TODO(enne): If this is small, maybe flatten it?
push<DrawRecordOp>(std::move(record), local_ctm);
}
void RecordPaintCanvas::Annotate(AnnotationType type,
const SkRect& rect,
sk_sp<SkData> data) {
push<AnnotateOp>(type, rect, data);
}
void RecordPaintCanvas::recordCustomData(uint32_t id) {
push<CustomDataOp>(id);
}
void RecordPaintCanvas::setNodeId(int node_id) {
push<SetNodeIdOp>(node_id);
}
InspectableRecordPaintCanvas::InspectableRecordPaintCanvas(
const gfx::Size& size)
: canvas_(size.width(), size.height()) {}
InspectableRecordPaintCanvas::InspectableRecordPaintCanvas(
CreateChildCanvasTag,
const InspectableRecordPaintCanvas& parent)
: canvas_(SkIRect::MakeSize(parent.imageInfo().dimensions())) {
canvas_.setMatrix(parent.canvas_.getLocalToDevice());
}
InspectableRecordPaintCanvas::~InspectableRecordPaintCanvas() = default;
int InspectableRecordPaintCanvas::save() {
device_clip_bounds_.reset();
return CheckSaveCount(RecordPaintCanvas::save(), canvas_.save());
}
int InspectableRecordPaintCanvas::saveLayer(const PaintFlags& flags) {
SkPaint paint = flags.ToSkPaint();
device_clip_bounds_.reset();
return CheckSaveCount(RecordPaintCanvas::saveLayer(flags),
canvas_.saveLayer(nullptr, &paint));
}
int InspectableRecordPaintCanvas::saveLayer(const SkRect& bounds,
const PaintFlags& flags) {
SkPaint paint = flags.ToSkPaint();
device_clip_bounds_.reset();
return CheckSaveCount(RecordPaintCanvas::saveLayer(bounds, flags),
canvas_.saveLayer(&bounds, &paint));
}
int InspectableRecordPaintCanvas::saveLayerAlphaf(float alpha) {
device_clip_bounds_.reset();
return CheckSaveCount(RecordPaintCanvas::saveLayerAlphaf(alpha),
canvas_.saveLayerAlphaf(nullptr, alpha));
}
int InspectableRecordPaintCanvas::saveLayerAlphaf(const SkRect& bounds,
float alpha) {
device_clip_bounds_.reset();
return CheckSaveCount(RecordPaintCanvas::saveLayerAlphaf(bounds, alpha),
canvas_.saveLayerAlphaf(&bounds, alpha));
}
int InspectableRecordPaintCanvas::saveLayerFilters(
base::span<const sk_sp<PaintFilter>> filters,
const PaintFlags& flags) {
SkPaint paint = flags.ToSkPaint();
device_clip_bounds_.reset();
return CheckSaveCount(RecordPaintCanvas::saveLayerFilters(filters, flags),
// Don't bother copying the filter span, filters don't
// impact the current clip or CTM.
canvas_.saveLayer(/*bounds=*/nullptr, &paint));
}
void InspectableRecordPaintCanvas::restore() {
RecordPaintCanvas::restore();
canvas_.restore();
device_clip_bounds_.reset();
DCHECK_EQ(getSaveCount(), canvas_.getSaveCount());
}
int InspectableRecordPaintCanvas::CheckSaveCount(int super_prev_save_count,
int canvas_prev_save_count) {
DCHECK_EQ(super_prev_save_count, canvas_prev_save_count);
DCHECK_EQ(getSaveCount(), canvas_.getSaveCount());
return super_prev_save_count;
}
void InspectableRecordPaintCanvas::translate(SkScalar dx, SkScalar dy) {
RecordPaintCanvas::translate(dx, dy);
canvas_.translate(dx, dy);
device_clip_bounds_.reset();
}
void InspectableRecordPaintCanvas::scale(SkScalar sx, SkScalar sy) {
RecordPaintCanvas::scale(sx, sy);
canvas_.scale(sx, sy);
device_clip_bounds_.reset();
}
void InspectableRecordPaintCanvas::rotate(SkScalar degrees) {
RecordPaintCanvas::rotate(degrees);
canvas_.rotate(degrees);
device_clip_bounds_.reset();
}
void InspectableRecordPaintCanvas::concat(const SkM44& matrix) {
RecordPaintCanvas::concat(matrix);
canvas_.concat(matrix);
device_clip_bounds_.reset();
}
void InspectableRecordPaintCanvas::setMatrix(const SkM44& matrix) {
RecordPaintCanvas::setMatrix(matrix);
canvas_.setMatrix(matrix);
device_clip_bounds_.reset();
}
void InspectableRecordPaintCanvas::clipRect(const SkRect& rect,
SkClipOp op,
bool antialias) {
RecordPaintCanvas::clipRect(rect, op, antialias);
canvas_.clipRect(rect, op, antialias);
device_clip_bounds_.reset();
}
void InspectableRecordPaintCanvas::clipRRectInternal(const SkRRect& rrect,
SkClipOp op,
bool antialias) {
RecordPaintCanvas::clipRRectInternal(rrect, op, antialias);
canvas_.clipRRect(rrect, op, antialias);
device_clip_bounds_.reset();
}
void InspectableRecordPaintCanvas::clipPathInternal(
const SkPath& path,
SkClipOp op,
bool antialias,
UsePaintCache use_paint_cache) {
RecordPaintCanvas::clipPathInternal(path, op, antialias, use_paint_cache);
canvas_.clipPath(path, op, antialias);
device_clip_bounds_.reset();
}
SkImageInfo InspectableRecordPaintCanvas::imageInfo() const {
return canvas_.imageInfo();
}
bool InspectableRecordPaintCanvas::getLocalClipBounds(SkRect* bounds) const {
return canvas_.getLocalClipBounds(bounds);
}
bool InspectableRecordPaintCanvas::getDeviceClipBounds(SkIRect* bounds) const {
if (device_clip_bounds_) {
*bounds = *device_clip_bounds_;
return true;
}
if (canvas_.getDeviceClipBounds(bounds)) {
device_clip_bounds_.emplace(*bounds);
return true;
}
return false;
}
SkM44 InspectableRecordPaintCanvas::getLocalToDevice() const {
return canvas_.getLocalToDevice();
}
} // namespace cc