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
cc / paint / skia_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/skia_paint_canvas.h"
#include <cstdint>
#include <cstring>
#include <utility>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/trace_event/trace_event.h"
#include "cc/paint/display_item_list.h"
#include "cc/paint/paint_filter.h"
#include "cc/paint/paint_flags.h"
#include "cc/paint/paint_op.h"
#include "cc/paint/paint_op_buffer.h"
#include "cc/paint/paint_recorder.h"
#include "cc/paint/scoped_raster_flags.h"
#include "cc/paint/skottie_wrapper.h"
#include "skia/ext/legacy_display_globals.h"
#include "third_party/skia/include/core/SkAnnotation.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPoint.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkTextBlob.h"
#include "third_party/skia/include/docs/SkPDFDocument.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/GrRecordingContext.h"
#include "third_party/skia/src/core/SkCanvasPriv.h"
namespace cc {
SkiaPaintCanvas::ContextFlushes::ContextFlushes()
: enable(false), max_draws_before_flush(-1) {}
SkiaPaintCanvas::SkiaPaintCanvas(SkCanvas* canvas,
ImageProvider* image_provider,
ContextFlushes context_flushes)
: canvas_(canvas),
image_provider_(image_provider),
context_flushes_(context_flushes) {}
SkiaPaintCanvas::SkiaPaintCanvas(const SkBitmap& bitmap,
ImageProvider* image_provider)
: canvas_(new SkCanvas(bitmap,
skia::LegacyDisplayGlobals::GetSkSurfaceProps())),
bitmap_(bitmap),
owned_(canvas_),
image_provider_(image_provider) {}
SkiaPaintCanvas::SkiaPaintCanvas(const SkBitmap& bitmap,
const SkSurfaceProps& props)
: canvas_(new SkCanvas(bitmap, props)), bitmap_(bitmap), owned_(canvas_) {}
SkiaPaintCanvas::~SkiaPaintCanvas() = default;
SkImageInfo SkiaPaintCanvas::imageInfo() const {
return canvas_->imageInfo();
}
void* SkiaPaintCanvas::accessTopLayerPixels(SkImageInfo* info,
size_t* rowBytes,
SkIPoint* origin) {
if (bitmap_.isNull() || bitmap_.isImmutable())
return nullptr;
return canvas_->accessTopLayerPixels(info, rowBytes, origin);
}
void SkiaPaintCanvas::flush() {
if (GrDirectContext* direct_context =
GrAsDirectContext(canvas_->recordingContext())) {
direct_context->flushAndSubmit();
}
}
bool SkiaPaintCanvas::NeedsFlush() const {
// Since flush() is always capable of flushing immediately with
// SkiaPaintCanvas, there is never any need for deferred flushing.
return false;
}
int SkiaPaintCanvas::save() {
return canvas_->save();
}
int SkiaPaintCanvas::saveLayer(const PaintFlags& flags) {
SkPaint paint = flags.ToSkPaint();
return canvas_->saveLayer(nullptr, &paint);
}
int SkiaPaintCanvas::saveLayer(const SkRect& bounds, const PaintFlags& flags) {
SkPaint paint = flags.ToSkPaint();
return canvas_->saveLayer(&bounds, &paint);
}
int SkiaPaintCanvas::saveLayerAlphaf(float alpha) {
return canvas_->saveLayerAlphaf(nullptr, alpha);
}
int SkiaPaintCanvas::saveLayerAlphaf(const SkRect& bounds, float alpha) {
return canvas_->saveLayerAlphaf(&bounds, alpha);
}
int SkiaPaintCanvas::saveLayerFilters(
base::span<const sk_sp<PaintFilter>> filters,
const PaintFlags& flags) {
SkPaint paint = flags.ToSkPaint();
return canvas_->saveLayer(SkCanvasPriv::ScaledBackdropLayer(
/*bounds=*/nullptr, &paint, /*backdrop=*/nullptr, /*backdropScale=*/1.0f,
/*saveLayerFlags=*/0, PaintFilter::ToSkImageFilters(filters)));
}
void SkiaPaintCanvas::restore() {
canvas_->restore();
}
int SkiaPaintCanvas::getSaveCount() const {
return canvas_->getSaveCount();
}
void SkiaPaintCanvas::restoreToCount(int save_count) {
canvas_->restoreToCount(save_count);
}
void SkiaPaintCanvas::translate(SkScalar dx, SkScalar dy) {
canvas_->translate(dx, dy);
}
void SkiaPaintCanvas::scale(SkScalar sx, SkScalar sy) {
canvas_->scale(sx, sy);
}
void SkiaPaintCanvas::rotate(SkScalar degrees) {
canvas_->rotate(degrees);
}
void SkiaPaintCanvas::setMatrix(const SkM44& matrix) {
canvas_->setMatrix(matrix);
}
void SkiaPaintCanvas::concat(const SkM44& matrix) {
canvas_->concat(matrix);
}
void SkiaPaintCanvas::clipRect(const SkRect& rect,
SkClipOp op,
bool do_anti_alias) {
canvas_->clipRect(rect, op, do_anti_alias);
}
void SkiaPaintCanvas::clipRRect(const SkRRect& rrect,
SkClipOp op,
bool do_anti_alias) {
canvas_->clipRRect(rrect, op, do_anti_alias);
}
void SkiaPaintCanvas::clipPath(const SkPath& path,
SkClipOp op,
bool do_anti_alias,
UsePaintCache) {
canvas_->clipPath(path, op, do_anti_alias);
}
bool SkiaPaintCanvas::getLocalClipBounds(SkRect* bounds) const {
return canvas_->getLocalClipBounds(bounds);
}
bool SkiaPaintCanvas::getDeviceClipBounds(SkIRect* bounds) const {
return canvas_->getDeviceClipBounds(bounds);
}
void SkiaPaintCanvas::drawColor(SkColor4f color, SkBlendMode mode) {
canvas_->drawColor(color, mode);
}
void SkiaPaintCanvas::clear(SkColor4f color) {
canvas_->clear(color);
}
void SkiaPaintCanvas::drawLine(SkScalar x0,
SkScalar y0,
SkScalar x1,
SkScalar y1,
const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_, [x0, y0, x1, y1](SkCanvas* c, const SkPaint& p) {
c->drawLine(x0, y0, x1, y1, p);
});
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawArc(const SkRect& oval,
SkScalar start_angle_degrees,
SkScalar sweep_angle_degrees,
const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags()) {
return;
}
DrawArcOp op(oval, start_angle_degrees, sweep_angle_degrees, flags);
op.RasterWithFlagsImpl(raster_flags.flags(), canvas_);
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawRect(const SkRect& rect, const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_,
[&rect](SkCanvas* c, const SkPaint& p) { c->drawRect(rect, p); });
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawIRect(const SkIRect& rect, const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_,
[&rect](SkCanvas* c, const SkPaint& p) { c->drawIRect(rect, p); });
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawOval(const SkRect& oval, const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_,
[&oval](SkCanvas* c, const SkPaint& p) { c->drawOval(oval, p); });
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawRRect(const SkRRect& rrect, const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_,
[&rrect](SkCanvas* c, const SkPaint& p) { c->drawRRect(rrect, p); });
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawDRRect(const SkRRect& outer,
const SkRRect& inner,
const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_, [&outer, &inner](SkCanvas* c, const SkPaint& p) {
c->drawDRRect(outer, inner, p);
});
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawRoundRect(const SkRect& rect,
SkScalar rx,
SkScalar ry,
const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_, [&rect, rx, ry](SkCanvas* c, const SkPaint& p) {
c->drawRoundRect(rect, rx, ry, p);
});
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawPath(const SkPath& path,
const PaintFlags& flags,
UsePaintCache) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(
canvas_,
[&path](SkCanvas* c, const SkPaint& p) { c->drawPath(path, p); });
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawImage(const PaintImage& image,
SkScalar left,
SkScalar top,
const SkSamplingOptions& sampling,
const PaintFlags* flags) {
DCHECK(!image.IsPaintWorklet());
std::optional<ScopedRasterFlags> scoped_flags;
if (flags) {
scoped_flags.emplace(flags, image_provider_, canvas_->getTotalMatrix(),
GetMaxTextureSize(), 1.0f);
if (!scoped_flags->flags())
return;
}
const PaintFlags* raster_flags = scoped_flags ? scoped_flags->flags() : flags;
PlaybackParams params(image_provider_, canvas_->getLocalToDevice());
DrawImageOp draw_image_op(image, left, top, sampling, nullptr);
DrawImageOp::RasterWithFlags(&draw_image_op, raster_flags, canvas_, params);
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawImageRect(const PaintImage& image,
const SkRect& src,
const SkRect& dst,
const SkSamplingOptions& sampling,
const PaintFlags* flags,
SkCanvas::SrcRectConstraint constraint) {
std::optional<ScopedRasterFlags> scoped_flags;
if (flags) {
scoped_flags.emplace(flags, image_provider_, canvas_->getTotalMatrix(),
GetMaxTextureSize(), 1.0f);
if (!scoped_flags->flags())
return;
}
const PaintFlags* raster_flags = scoped_flags ? scoped_flags->flags() : flags;
PlaybackParams params(image_provider_, canvas_->getLocalToDevice());
DrawImageRectOp draw_image_rect_op(image, src, dst, sampling, flags,
constraint);
DrawImageRectOp::RasterWithFlags(&draw_image_rect_op, raster_flags, canvas_,
params);
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawVertices(
scoped_refptr<RefCountedBuffer<SkPoint>> vertices,
scoped_refptr<RefCountedBuffer<SkPoint>> uvs,
scoped_refptr<RefCountedBuffer<uint16_t>> indices,
const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
/*alpha=*/1.0f);
DrawVerticesOp op(std::move(vertices), std::move(uvs), std::move(indices),
flags);
if (!raster_flags.flags() || !op.IsValid()) {
return;
}
PlaybackParams params(image_provider_);
DrawVerticesOp::RasterWithFlags(&op, raster_flags.flags(), canvas_, params);
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawSkottie(scoped_refptr<SkottieWrapper> skottie,
const SkRect& dst,
float t,
SkottieFrameDataMap images,
const SkottieColorMap& color_map,
SkottieTextPropertyValueMap text_map) {
if (!images.empty()) {
// This is not implemented solely because there's no use case yet. To
// implement, we could retrieve the underlying SkImage from each
// PaintImage in |images| here and call SkottieWrapper::SetImageForAsset().
NOTIMPLEMENTED()
<< "Rendering skottie frames with image assets directly to a "
"SkiaPaintCanvas is currently not supported.";
}
skottie->Draw(canvas_, t, dst, SkottieWrapper::FrameDataCallback(), color_map,
std::move(text_map));
}
void SkiaPaintCanvas::drawTextBlob(sk_sp<SkTextBlob> blob,
SkScalar x,
SkScalar y,
const PaintFlags& flags) {
ScopedRasterFlags raster_flags(&flags, image_provider_,
canvas_->getTotalMatrix(), GetMaxTextureSize(),
1.0f);
if (!raster_flags.flags())
return;
raster_flags.flags()->DrawToSk(canvas_,
[&blob, x, y](SkCanvas* c, const SkPaint& p) {
c->drawTextBlob(blob, x, y, p);
});
FlushAfterDrawIfNeeded();
}
void SkiaPaintCanvas::drawTextBlob(sk_sp<SkTextBlob> blob,
SkScalar x,
SkScalar y,
NodeId node_id,
const PaintFlags& flags) {
if (node_id)
SkPDF::SetNodeId(canvas_, node_id);
drawTextBlob(blob, x, y, flags);
if (node_id)
SkPDF::SetNodeId(canvas_, 0);
}
void SkiaPaintCanvas::drawPicture(PaintRecord record) {
drawPicture(std::move(record), PlaybackCallbacks::CustomDataRasterCallback(),
/*local_ctm=*/true);
}
void SkiaPaintCanvas::drawPicture(PaintRecord record, bool local_ctm) {
drawPicture(std::move(record), PlaybackCallbacks::CustomDataRasterCallback(),
local_ctm);
}
SkM44 SkiaPaintCanvas::getLocalToDevice() const {
return canvas_->getLocalToDevice();
}
void SkiaPaintCanvas::Annotate(AnnotationType type,
const SkRect& rect,
sk_sp<SkData> data) {
switch (type) {
case AnnotationType::kUrl:
SkAnnotateRectWithURL(canvas_, rect, data.get());
break;
case AnnotationType::kLinkToDestination:
SkAnnotateLinkToDestination(canvas_, rect, data.get());
break;
case AnnotationType::kNameDestination: {
SkPoint point = SkPoint::Make(rect.x(), rect.y());
SkAnnotateNamedDestination(canvas_, point, data.get());
break;
}
}
}
void SkiaPaintCanvas::setNodeId(int node_id) {
SkPDF::SetNodeId(canvas_, node_id);
}
void SkiaPaintCanvas::drawPicture(
PaintRecord record,
PlaybackCallbacks::CustomDataRasterCallback custom_raster_callback,
bool local_ctm) {
PlaybackCallbacks callbacks;
callbacks.custom_callback = custom_raster_callback;
if (context_flushes_.enable) {
callbacks.did_draw_op_callback = base::BindRepeating(
&SkiaPaintCanvas::FlushAfterDrawIfNeeded, base::Unretained(this));
}
PlaybackParams params(image_provider_, canvas_->getLocalToDevice(),
callbacks);
record.Playback(canvas_, params, local_ctm);
}
void SkiaPaintCanvas::FlushAfterDrawIfNeeded() {
if (!context_flushes_.enable)
return;
if (++num_of_ops_ > context_flushes_.max_draws_before_flush) {
num_of_ops_ = 0;
TRACE_EVENT0("cc",
"SkiaPaintCanvas::FlushAfterDrawIfNeeded::FlushGrContext");
if (GrDirectContext* direct_context =
GrAsDirectContext(canvas_->recordingContext())) {
direct_context->flushAndSubmit();
}
}
}
int SkiaPaintCanvas::GetMaxTextureSize() const {
auto* context = canvas_->recordingContext();
return context ? context->maxTextureSize() : 0;
}
} // namespace cc