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
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkDeferredCanvas.h"
#include "SkDrawable.h"
#include "SkPath.h"
#include "SkRRect.h"
#include "SkSurface.h"
#include "SkTextBlob.h"
bool SkDeferredCanvas::Rec::isConcat(SkMatrix* m) const {
switch (fType) {
case kTrans_Type:
m->setTranslate(fData.fTranslate.x(), fData.fTranslate.y());
return true;
case kScaleTrans_Type:
m->setScaleTranslate(fData.fScaleTrans.fScale.x(),
fData.fScaleTrans.fScale.y(),
fData.fScaleTrans.fTrans.x(),
fData.fScaleTrans.fTrans.y());
return true;
default:
break;
}
return false;
}
void SkDeferredCanvas::Rec::setConcat(const SkMatrix& m) {
SkASSERT(m.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask));
if (m.getType() <= SkMatrix::kTranslate_Mask) {
fType = kTrans_Type;
fData.fTranslate.set(m.getTranslateX(), m.getTranslateY());
} else {
fType = kScaleTrans_Type;
fData.fScaleTrans.fScale.set(m.getScaleX(), m.getScaleY());
fData.fScaleTrans.fTrans.set(m.getTranslateX(), m.getTranslateY());
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
SkDeferredCanvas::SkDeferredCanvas(SkCanvas* canvas)
: INHERITED(canvas->getBaseLayerSize().width(), canvas->getBaseLayerSize().height())
, fCanvas(canvas)
{}
SkDeferredCanvas::~SkDeferredCanvas() {}
void SkDeferredCanvas::push_save() {
Rec* r = fRecs.append();
r->fType = kSave_Type;
}
void SkDeferredCanvas::push_cliprect(const SkRect& bounds) {
int index = fRecs.count() - 1;
if (index >= 0 && fRecs[index].fType == kClipRect_Type) {
if (!fRecs[index].fData.fBounds.intersect(bounds)) {
fRecs[index].fData.fBounds.setEmpty();
}
} else {
Rec* r = fRecs.append();
r->fType = kClipRect_Type;
r->fData.fBounds = bounds;
}
}
bool SkDeferredCanvas::push_concat(const SkMatrix& mat) {
if (mat.getType() > (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
return false;
}
// At the moment, we don't know which ops can scale and which can also flip, so
// we reject negative scales for now
if (mat.getScaleX() < 0 || mat.getScaleY() < 0) {
return false;
}
int index = fRecs.count() - 1;
SkMatrix m;
if (index >= 0 && fRecs[index].isConcat(&m)) {
m.preConcat(mat);
fRecs[index].setConcat(m);
} else {
fRecs.append()->setConcat(mat);
}
return true;
}
void SkDeferredCanvas::emit(const Rec& rec) {
switch (rec.fType) {
case kSave_Type:
fCanvas->save();
this->INHERITED::willSave();
break;
case kClipRect_Type:
fCanvas->clipRect(rec.fData.fBounds);
this->INHERITED::onClipRect(rec.fData.fBounds,
SkRegion::kIntersect_Op, kHard_ClipEdgeStyle);
break;
case kTrans_Type:
case kScaleTrans_Type: {
SkMatrix mat;
rec.getConcat(&mat);
fCanvas->concat(mat);
this->INHERITED::didConcat(mat);
} break;
}
}
void SkDeferredCanvas::flush_le(int index) {
SkASSERT(index >= -1 && index < fRecs.count());
int count = index + 1;
for (int i = 0; i < count; ++i) {
this->emit(fRecs[i]);
}
fRecs.remove(0, count);
}
void SkDeferredCanvas::flush_all() {
this->flush_le(fRecs.count() - 1);
}
void SkDeferredCanvas::flush_before_saves() {
int i;
for (i = fRecs.count() - 1; i >= 0; --i) {
if (kSave_Type != fRecs[i].fType) {
break;
}
}
this->flush_le(i);
}
enum Flags {
kNoTranslate_Flag = 1 << 0,
kNoClip_Flag = 1 << 1,
kNoCull_Flag = 1 << 2,
kNoScale_Flag = 1 << 3,
};
void SkDeferredCanvas::flush_check(SkRect* bounds, const SkPaint* paint, unsigned flags) {
if (paint) {
if (paint->getShader() || paint->getImageFilter()) {
flags |= kNoTranslate_Flag | kNoScale_Flag;
}
// TODO: replace these with code to enlarge the bounds conservatively?
if (paint->getStyle() != SkPaint::kFill_Style || paint->getMaskFilter() ||
paint->getImageFilter() || paint->getPathEffect())
{
flags |= kNoCull_Flag | kNoScale_Flag | kNoClip_Flag;
}
if (paint->getLooper()) {
// to be conservative, we disable both, since embedded layers could have shaders
// or strokes etc.
flags |= kNoTranslate_Flag | kNoCull_Flag | kNoScale_Flag;
}
}
bool canClip = !(flags & kNoClip_Flag);
bool canTranslate = !(flags & kNoTranslate_Flag);
bool canCull = !(flags & kNoCull_Flag);
bool canScale = !(flags & kNoScale_Flag);
int i;
for (i = fRecs.count() - 1; i >= 0; --i) {
const Rec& rec = fRecs[i];
switch (rec.fType) {
case kSave_Type:
// continue to the next rec
break;
case kClipRect_Type:
if (!canCull) {
goto STOP;
}
if (canClip) {
if (!bounds->intersect(rec.fData.fBounds)) {
bounds->setEmpty();
return;
}
// continue to the next rec
} else {
if (!rec.fData.fBounds.contains(*bounds)) {
goto STOP;
}
// continue to the next rec
}
break;
case kTrans_Type:
if (canTranslate) {
bounds->offset(rec.fData.fTranslate.x(), rec.fData.fTranslate.y());
// continue to the next rec
} else {
goto STOP;
}
break;
case kScaleTrans_Type:
if (canScale) {
SkMatrix m;
rec.getConcat(&m);
m.mapRectScaleTranslate(bounds, *bounds);
} else {
goto STOP;
}
break;
}
}
STOP:
this->flush_le(i);
}
void SkDeferredCanvas::flush_translate(SkScalar* x, SkScalar* y, const SkRect& bounds,
const SkPaint* paint) {
SkRect tmp = bounds;
this->flush_check(&tmp, paint, kNoClip_Flag | kNoScale_Flag);
*x += tmp.x() - bounds.x();
*y += tmp.y() - bounds.y();
}
void SkDeferredCanvas::flush_translate(SkScalar* x, SkScalar* y, const SkPaint& paint) {
SkRect tmp = SkRect::MakeXYWH(*x, *y, 1, 1);
this->flush_check(&tmp, &paint, kNoClip_Flag | kNoCull_Flag | kNoScale_Flag);
*x = tmp.x();
*y = tmp.y();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void SkDeferredCanvas::willSave() {
this->push_save();
}
SkCanvas::SaveLayerStrategy SkDeferredCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
this->flush_all();
fCanvas->saveLayer(rec);
this->INHERITED::getSaveLayerStrategy(rec);
// No need for a layer.
return kNoLayer_SaveLayerStrategy;
}
void SkDeferredCanvas::willRestore() {
for (int i = fRecs.count() - 1; i >= 0; --i) {
if (kSave_Type == fRecs[i].fType) {
fRecs.setCount(i); // pop off everything here and later
return;
}
}
for (int i = 0; i < fRecs.count(); ++i) {
SkASSERT(kSave_Type != fRecs[i].fType);
}
fRecs.setCount(0);
fCanvas->restore();
this->INHERITED::willRestore();
}
void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
if (matrix.isIdentity()) {
return;
}
if (!this->push_concat(matrix)) {
this->flush_all();
fCanvas->concat(matrix);
this->INHERITED::didConcat(matrix);
}
}
void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
this->flush_all();
fCanvas->setMatrix(matrix);
this->INHERITED::didSetMatrix(matrix);
}
void SkDeferredCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
if (SkRegion::kIntersect_Op == op) {
this->push_cliprect(rect);
} else {
this->flush_all();
fCanvas->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
this->INHERITED::onClipRect(rect, op, edgeStyle);
}
}
void SkDeferredCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
this->flush_all();
fCanvas->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
this->INHERITED::onClipRRect(rrect, op, edgeStyle);
}
void SkDeferredCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
this->flush_all();
fCanvas->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
this->INHERITED::onClipPath(path, op, edgeStyle);
}
void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
this->flush_all();
fCanvas->clipRegion(deviceRgn, op);
this->INHERITED::onClipRegion(deviceRgn, op);
}
void SkDeferredCanvas::onDrawPaint(const SkPaint& paint) {
// TODO: Can we turn this into drawRect?
this->flush_all();
fCanvas->drawPaint(paint);
}
void SkDeferredCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
const SkPaint& paint) {
this->flush_all();
fCanvas->drawPoints(mode, count, pts, paint);
}
void SkDeferredCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
SkRect modRect = rect;
this->flush_check(&modRect, &paint);
fCanvas->drawRect(modRect, paint);
}
void SkDeferredCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
SkRect modRect = rect;
this->flush_check(&modRect, &paint, kNoClip_Flag);
fCanvas->drawOval(modRect, paint);
}
static SkRRect make_offset(const SkRRect& src, SkScalar dx, SkScalar dy) {
SkRRect dst = src;
dst.offset(dx, dy);
return dst;
}
void SkDeferredCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
SkRect modRect = rrect.getBounds();
this->flush_check(&modRect, &paint, kNoClip_Flag);
fCanvas->drawRRect(make_offset(rrect,
modRect.x() - rrect.getBounds().x(),
modRect.y() - rrect.getBounds().y()), paint);
}
void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
this->flush_all();
fCanvas->drawDRRect(outer, inner, paint);
}
void SkDeferredCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
if (path.isInverseFillType()) {
this->flush_before_saves();
} else {
SkRect modRect = path.getBounds();
this->flush_check(&modRect, &paint, kNoClip_Flag | kNoTranslate_Flag | kNoScale_Flag);
}
fCanvas->drawPath(path, paint);
}
void SkDeferredCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
const SkPaint* paint) {
const SkScalar w = SkIntToScalar(bitmap.width());
const SkScalar h = SkIntToScalar(bitmap.height());
SkRect bounds = SkRect::MakeXYWH(x, y, w, h);
this->flush_check(&bounds, paint, kNoClip_Flag);
if (bounds.width() == w && bounds.height() == h) {
fCanvas->drawBitmap(bitmap, bounds.x(), bounds.y(), paint);
} else {
fCanvas->drawBitmapRect(bitmap, bounds, paint);
}
}
void SkDeferredCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
const SkPaint* paint, SrcRectConstraint constraint) {
SkRect modRect = dst;
this->flush_check(&modRect, paint, kNoClip_Flag);
fCanvas->legacy_drawBitmapRect(bitmap, src, modRect, paint, constraint);
}
void SkDeferredCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
const SkRect& dst, const SkPaint* paint) {
SkRect modRect = dst;
this->flush_check(&modRect, paint, kNoClip_Flag);
fCanvas->drawBitmapNine(bitmap, center, modRect, paint);
}
void SkDeferredCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center,
const SkRect& dst, const SkPaint* paint) {
SkRect modRect = dst;
this->flush_check(&modRect, paint, kNoClip_Flag);
fCanvas->drawImageNine(image, center, modRect, paint);
}
void SkDeferredCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y,
const SkPaint* paint) {
const SkScalar w = SkIntToScalar(image->width());
const SkScalar h = SkIntToScalar(image->height());
SkRect bounds = SkRect::MakeXYWH(x, y, w, h);
this->flush_check(&bounds, paint, kNoClip_Flag);
if (bounds.width() == w && bounds.height() == h) {
fCanvas->drawImage(image, bounds.x(), bounds.y(), paint);
} else {
fCanvas->drawImageRect(image, bounds, paint);
}
}
void SkDeferredCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
const SkPaint* paint, SrcRectConstraint constraint) {
SkRect modRect = dst;
this->flush_check(&modRect, paint, kNoClip_Flag);
fCanvas->legacy_drawImageRect(image, src, modRect, paint, constraint);
}
void SkDeferredCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
const SkPaint& paint) {
this->flush_translate(&x, &y, paint);
fCanvas->drawText(text, byteLength, x, y, paint);
}
void SkDeferredCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
const SkPaint& paint) {
this->flush_before_saves();
fCanvas->drawPosText(text, byteLength, pos, paint);
}
void SkDeferredCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
SkScalar constY, const SkPaint& paint) {
this->flush_before_saves();
fCanvas->drawPosTextH(text, byteLength, xpos, constY, paint);
}
void SkDeferredCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
const SkMatrix* matrix, const SkPaint& paint) {
this->flush_before_saves();
fCanvas->drawTextOnPath(text, byteLength, path, matrix, paint);
}
void SkDeferredCanvas::onDrawTextRSXform(const void* text, size_t byteLength,
const SkRSXform xform[], const SkRect* cullRect,
const SkPaint& paint) {
if (cullRect) {
SkRect modRect = *cullRect;
// only allow culling
this->flush_check(&modRect, &paint, kNoClip_Flag | kNoScale_Flag | kNoTranslate_Flag);
} else {
this->flush_before_saves();
}
fCanvas->drawTextRSXform(text, byteLength, xform, cullRect, paint);
}
void SkDeferredCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
const SkPaint &paint) {
this->flush_translate(&x, &y, blob->bounds(), &paint);
fCanvas->drawTextBlob(blob, x, y, paint);
}
#include "SkPicture.h"
#include "SkCanvasPriv.h"
void SkDeferredCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
const SkPaint* paint) {
#if 1
SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
picture->playback(this);
#else
this->flush_before_saves();
fCanvas->drawPicture(picture, matrix, paint);
#endif
}
void SkDeferredCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
// TODO: investigate culling and applying concat to the matrix
#if 1
drawable->draw(this, matrix);
#else
this->flush_before_saves();
fCanvas->drawDrawable(drawable, matrix);
#endif
}
void SkDeferredCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[],
const SkRect rects[], const SkColor colors[],
int count, SkXfermode::Mode mode,
const SkRect* cull, const SkPaint* paint) {
this->flush_before_saves();
fCanvas->drawAtlas(image, xform, rects, colors, count, mode, cull, paint);
}
void SkDeferredCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
const SkPoint vertices[], const SkPoint texs[],
const SkColor colors[], SkXfermode* xmode,
const uint16_t indices[], int indexCount,
const SkPaint& paint) {
this->flush_before_saves();
fCanvas->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
indices, indexCount, paint);
}
void SkDeferredCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
const SkPoint texCoords[4], SkXfermode* xmode,
const SkPaint& paint) {
this->flush_before_saves();
fCanvas->drawPatch(cubics, colors, texCoords, xmode, paint);
}
void SkDeferredCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* data) {
this->flush_all();
fCanvas->drawAnnotation(rect, key, data);
}
#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
fCanvas->setDrawFilter(filter);
return this->INHERITED::setDrawFilter(filter);
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
sk_sp<SkSurface> SkDeferredCanvas::onNewSurface(const SkImageInfo& info,
const SkSurfaceProps& props) {
return fCanvas->makeSurface(info, &props);
}
SkISize SkDeferredCanvas::getBaseLayerSize() const { return fCanvas->getBaseLayerSize(); }
bool SkDeferredCanvas::getClipBounds(SkRect* bounds) const {
return fCanvas->getClipBounds(bounds);
}
bool SkDeferredCanvas::getClipDeviceBounds(SkIRect* bounds) const {
return fCanvas->getClipDeviceBounds(bounds);
}
bool SkDeferredCanvas::isClipEmpty() const { return fCanvas->isClipEmpty(); }
bool SkDeferredCanvas::isClipRect() const { return fCanvas->isClipRect(); }
bool SkDeferredCanvas::onPeekPixels(SkPixmap* pixmap) { return fCanvas->peekPixels(pixmap); }
bool SkDeferredCanvas::onAccessTopLayerPixels(SkPixmap* pixmap) {
SkImageInfo info;
size_t rowBytes;
SkIPoint* origin = nullptr;
void* addr = fCanvas->accessTopLayerPixels(&info, &rowBytes, origin);
if (addr) {
*pixmap = SkPixmap(info, addr, rowBytes);
return true;
}
return false;
}
SkImageInfo SkDeferredCanvas::onImageInfo() const { return fCanvas->imageInfo(); }
bool SkDeferredCanvas::onGetProps(SkSurfaceProps* props) const { return fCanvas->getProps(props); }
void SkDeferredCanvas::onFlush() {
this->flush_all();
return fCanvas->flush();
}
|