aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/SkDeferredCanvas.cpp
blob: e9650508186ce5c438f8a704f63a6f5ea48ae34f (plain)
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

/*
 * Copyright 2011 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 "SkPaint.h"
#include "SkShader.h"
#include "SkColorFilter.h"
#include "SkDrawFilter.h"

namespace {

bool isPaintOpaque(const SkPaint* paint, 
                   const SkBitmap* bmpReplacesShader = NULL) {
    // TODO: SkXfermode should have a virtual isOpaque method, which would
    // make it possible to test modes that do not have a Coeff representation.

    if (!paint) {
        return bmpReplacesShader ? bmpReplacesShader->isOpaque() : true;
    }

    SkXfermode::Coeff srcCoeff, dstCoeff;
    if (SkXfermode::AsCoeff(paint->getXfermode(), &srcCoeff, &dstCoeff)){
        switch (dstCoeff) {
        case SkXfermode::kZero_Coeff:
            return true;
        case SkXfermode::kISA_Coeff:
            if (paint->getAlpha() != 255) {
                break;
            }
            if (bmpReplacesShader) {
                if (!bmpReplacesShader->isOpaque()) {
                    break;
                }
            } else if (paint->getShader() && !paint->getShader()->isOpaque()) {
                break;
            }
            if (paint->getColorFilter() && 
                ((paint->getColorFilter()->getFlags() &
                SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
                break;
            }
            return true;
        case SkXfermode::kSA_Coeff:
            if (paint->getAlpha() != 0) {
                break;
            }
            if (paint->getColorFilter() && 
                ((paint->getColorFilter()->getFlags() &
                SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
                break;
            }
            return true;
        case SkXfermode::kSC_Coeff:
            if (paint->getColor() != 0) { // all components must be 0
                break;
            }
            if (bmpReplacesShader || paint->getShader()) {
                break;
            }
            if (paint->getColorFilter() && (
                (paint->getColorFilter()->getFlags() &
                SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
                break;
            }
            return true;
        default:
            break;
        }
    }
    return false;
}

} // unnamed namespace

SkDeferredCanvas::SkDeferredCanvas() {
    init();
}

SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
    init();
    setDevice(device);
}

SkDeferredCanvas::SkDeferredCanvas(SkDevice* device, 
                                   DeviceContext* deviceContext) {
    init();
    setDevice(device);
    setDeviceContext(deviceContext);
}

void SkDeferredCanvas::init() {
    fDeferredDrawing = true; // On by default
}

void SkDeferredCanvas::validate() const {
    SkASSERT(getDevice());
}

SkCanvas* SkDeferredCanvas::drawingCanvas() const {
    validate();
    return fDeferredDrawing ? getDeferredDevice()->recordingCanvas() :
        getDeferredDevice()->immediateCanvas();
}

void SkDeferredCanvas::flushIfNeeded(const SkBitmap& bitmap) {
    validate();
    if (fDeferredDrawing) {
        getDeferredDevice()->flushIfNeeded(bitmap);
    }
}

SkDeferredCanvas::DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
    return static_cast<SkDeferredCanvas::DeferredDevice*>(getDevice());
}

void SkDeferredCanvas::setDeferredDrawing(bool val) {
    validate(); // Must set device before calling this method
    SkASSERT(drawingCanvas()->getSaveCount() == 1);
    if (val != fDeferredDrawing) {
        if (fDeferredDrawing) {
            // Going live.
            getDeferredDevice()->flushPending();
        }
        fDeferredDrawing = val;
    }
}

SkDeferredCanvas::~SkDeferredCanvas() {
}

SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
    INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
    return device;
}

SkDeferredCanvas::DeviceContext* SkDeferredCanvas::setDeviceContext(
    DeviceContext* deviceContext) {

    DeferredDevice* deferredDevice = getDeferredDevice();
    SkASSERT(deferredDevice);
    if (deferredDevice) {
        deferredDevice->setDeviceContext(deviceContext);
    }
    return deviceContext;
}

bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
                                   const SkPaint* paint) const {
    SkCanvas* canvas = drawingCanvas();
    SkISize canvasSize = getDeviceSize();
    if (rect) {
        if (!canvas->getTotalMatrix().rectStaysRect()) {
            return false; // conservative
        }

        SkRect transformedRect;
        canvas->getTotalMatrix().mapRect(&transformedRect, *rect);

        if (paint) {
            SkPaint::Style paintStyle = paint->getStyle();
            if (!(paintStyle == SkPaint::kFill_Style || 
                paintStyle == SkPaint::kStrokeAndFill_Style)) {
                return false;
            }
            if (paint->getMaskFilter() || paint->getLooper()
                || paint->getPathEffect() || paint->getImageFilter()) {
                return false; // conservative
            }
        }

        // The following test holds with AA enabled, and is conservative
        // by a 0.5 pixel margin with AA disabled
        if (transformedRect.fLeft > SkIntToScalar(0) || 
            transformedRect.fTop > SkIntToScalar(0) || 
            transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
            transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
            return false;
        }
    }

    switch (canvas->getClipType()) {
        case SkCanvas::kRect_ClipType :
            {
                SkIRect bounds;
                canvas->getClipDeviceBounds(&bounds);
                if (bounds.fLeft > 0 || bounds.fTop > 0 || 
                    bounds.fRight < canvasSize.fWidth || 
                    bounds.fBottom < canvasSize.fHeight)
                    return false;
            }
            break;
        case SkCanvas::kComplex_ClipType :
            return false; // conservative
        case SkCanvas::kEmpty_ClipType:
        default:
            break;
    };

    return true;
}

int SkDeferredCanvas::save(SaveFlags flags) {
    drawingCanvas()->save(flags);
    return this->INHERITED::save(flags);
}

int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
                                SaveFlags flags) {
    drawingCanvas()->saveLayer(bounds, paint, flags);
    int count = this->INHERITED::save(flags);
    this->clipRectBounds(bounds, flags, NULL);
    return count;
}

void SkDeferredCanvas::restore() {
    drawingCanvas()->restore();
    this->INHERITED::restore();
}

bool SkDeferredCanvas::isDrawingToLayer() const {
    return drawingCanvas()->isDrawingToLayer();
}

bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
    drawingCanvas()->translate(dx, dy);
    return this->INHERITED::translate(dx, dy);
}

bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
    drawingCanvas()->scale(sx, sy);
    return this->INHERITED::scale(sx, sy);
}

bool SkDeferredCanvas::rotate(SkScalar degrees) {
    drawingCanvas()->rotate(degrees);
    return this->INHERITED::rotate(degrees);
}

bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
    drawingCanvas()->skew(sx, sy);
    return this->INHERITED::skew(sx, sy);
}

bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
    drawingCanvas()->concat(matrix);
    return this->INHERITED::concat(matrix);
}

void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
    drawingCanvas()->setMatrix(matrix);
    this->INHERITED::setMatrix(matrix);
}

bool SkDeferredCanvas::clipRect(const SkRect& rect,
                                SkRegion::Op op,
                                bool doAntiAlias) {
    drawingCanvas()->clipRect(rect, op, doAntiAlias);
    return this->INHERITED::clipRect(rect, op, doAntiAlias);
}

bool SkDeferredCanvas::clipPath(const SkPath& path,
                                SkRegion::Op op,
                                bool doAntiAlias) {
    drawingCanvas()->clipPath(path, op, doAntiAlias);
    return this->INHERITED::clipPath(path, op, doAntiAlias);
}

bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
                                  SkRegion::Op op) {
    drawingCanvas()->clipRegion(deviceRgn, op);
    return this->INHERITED::clipRegion(deviceRgn, op);
}

void SkDeferredCanvas::clear(SkColor color) {
    // purge pending commands
    if (fDeferredDrawing) {
        getDeferredDevice()->contentsCleared();
    }

    drawingCanvas()->clear(color);
}

void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
    if (fDeferredDrawing && isFullFrame(NULL, &paint) && 
        isPaintOpaque(&paint)) {
        getDeferredDevice()->contentsCleared();
    }

    drawingCanvas()->drawPaint(paint);
}

void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
                                  const SkPoint pts[], const SkPaint& paint) {
    drawingCanvas()->drawPoints(mode, count, pts, paint);
}

void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
    if (fDeferredDrawing && isFullFrame(&rect, &paint) && 
        isPaintOpaque(&paint)) {
        getDeferredDevice()->contentsCleared();
    }

    drawingCanvas()->drawRect(rect, paint);
}

void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
    drawingCanvas()->drawPath(path, paint);
}

void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
                                  SkScalar top, const SkPaint* paint) {
    SkRect bitmapRect = SkRect::MakeXYWH(left, top,
        SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
    if (fDeferredDrawing && 
        isFullFrame(&bitmapRect, paint) &&
        isPaintOpaque(paint, &bitmap)) {
        getDeferredDevice()->contentsCleared();
    }

    drawingCanvas()->drawBitmap(bitmap, left, top, paint);
    flushIfNeeded(bitmap);
}

void SkDeferredCanvas::drawBitmapRect(const SkBitmap& bitmap, 
                                      const SkIRect* src,
                                      const SkRect& dst,
                                      const SkPaint* paint) {
    if (fDeferredDrawing && 
        isFullFrame(&dst, paint) &&
        isPaintOpaque(paint, &bitmap)) {
        getDeferredDevice()->contentsCleared();
    }

    drawingCanvas()->drawBitmapRect(bitmap, src,
                                    dst, paint);
    flushIfNeeded(bitmap);
}


void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
                                        const SkMatrix& m,
                                        const SkPaint* paint) {
    // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
    // covers canvas entirely and transformed bitmap covers canvas entirely
    drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
    flushIfNeeded(bitmap);
}

void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
                                      const SkIRect& center, const SkRect& dst,
                                      const SkPaint* paint) {
    // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
    // covers canvas entirely and dst covers canvas entirely
    drawingCanvas()->drawBitmapNine(bitmap, center,
                                    dst, paint);
    flushIfNeeded(bitmap);
}

void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
                                  const SkPaint* paint) {
    SkRect bitmapRect = SkRect::MakeXYWH(
        SkIntToScalar(left),
        SkIntToScalar(top), 
        SkIntToScalar(bitmap.width()),
        SkIntToScalar(bitmap.height()));
    if (fDeferredDrawing && 
        isFullFrame(&bitmapRect, paint) &&
        isPaintOpaque(paint, &bitmap)) {
        getDeferredDevice()->contentsCleared();
    }

    drawingCanvas()->drawSprite(bitmap, left, top,
                                paint);
    flushIfNeeded(bitmap);
}

void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
                                SkScalar x, SkScalar y, const SkPaint& paint) {
    drawingCanvas()->drawText(text, byteLength, x, y, paint);
}

void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
                                   const SkPoint pos[], const SkPaint& paint) {
    drawingCanvas()->drawPosText(text, byteLength, pos, paint);
}

void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
                                    const SkScalar xpos[], SkScalar constY,
                                    const SkPaint& paint) {
    drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
}

void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
                                      const SkPath& path,
                                      const SkMatrix* matrix,
                                      const SkPaint& paint) {
    drawingCanvas()->drawTextOnPath(text, byteLength,
                                    path, matrix,
                                    paint);
}

void SkDeferredCanvas::drawPicture(SkPicture& picture) {
    drawingCanvas()->drawPicture(picture);
}

void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
                                    const SkPoint vertices[],
                                    const SkPoint texs[],
                                    const SkColor colors[], SkXfermode* xmode,
                                    const uint16_t indices[], int indexCount,
                                    const SkPaint& paint) {
    drawingCanvas()->drawVertices(vmode, vertexCount,
                                  vertices, texs,
                                  colors, xmode,
                                  indices, indexCount,
                                  paint);
}

SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
    drawingCanvas()->setBounder(bounder);
    return INHERITED::setBounder(bounder);
}

SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
    drawingCanvas()->setDrawFilter(filter); 
    return INHERITED::setDrawFilter(filter);
}

SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
    return drawingCanvas();
}

// SkDeferredCanvas::DeferredDevice
//------------------------------------

SkDeferredCanvas::DeferredDevice::DeferredDevice(
    SkDevice* immediateDevice, DeviceContext* deviceContext) :
    SkDevice(SkBitmap::kNo_Config, immediateDevice->width(),
             immediateDevice->height(), immediateDevice->isOpaque())
    , fFreshFrame(true) {

    fDeviceContext = deviceContext;
    SkSafeRef(fDeviceContext);
    fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
    fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
    fRecordingCanvas = fPicture.beginRecording(fImmediateDevice->width(),
        fImmediateDevice->height(), 0);
}

SkDeferredCanvas::DeferredDevice::~DeferredDevice() {
    SkSafeUnref(fImmediateCanvas);
    SkSafeUnref(fDeviceContext);
}
    
void SkDeferredCanvas::DeferredDevice::setDeviceContext(
    DeviceContext* deviceContext) {
    SkRefCnt_SafeAssign(fDeviceContext, deviceContext);
}

void SkDeferredCanvas::DeferredDevice::contentsCleared() {
    if (!fRecordingCanvas->isDrawingToLayer()) {
        fFreshFrame = true;

        // TODO: find a way to transfer the state stack and layers
        // to the new recording canvas.  For now, purging only works
        // with an empty stack.
        if (fRecordingCanvas->getSaveCount() == 0) {

            // Save state that is trashed by the purge
            SkDrawFilter* drawFilter = fRecordingCanvas->getDrawFilter();
            SkSafeRef(drawFilter); // So that it survives the purge
            SkMatrix matrix = fRecordingCanvas->getTotalMatrix();
            SkRegion clipRegion = fRecordingCanvas->getTotalClip();

            // beginRecording creates a new recording canvas and discards the
            // old one, hence purging deferred draw ops.
            fRecordingCanvas = fPicture.beginRecording(
                fImmediateDevice->width(),
                fImmediateDevice->height(), 0);

            // Restore pre-purge state
            if (!clipRegion.isEmpty()) {
                fRecordingCanvas->clipRegion(clipRegion, 
                    SkRegion::kReplace_Op);
            }
            if (!matrix.isIdentity()) {
                fRecordingCanvas->setMatrix(matrix);
            }
            if (drawFilter) {
                fRecordingCanvas->setDrawFilter(drawFilter)->unref();
            }
        }
    }
}

bool SkDeferredCanvas::DeferredDevice::isFreshFrame() {
    bool ret = fFreshFrame;
    fFreshFrame = false;
    return ret;
}

void SkDeferredCanvas::DeferredDevice::flushPending() {
    if (fDeviceContext) {
        fDeviceContext->prepareForDraw();
    }
    fPicture.draw(fImmediateCanvas);
    fRecordingCanvas = fPicture.beginRecording(fImmediateDevice->width(), 
        fImmediateDevice->height(), 0);
}

void SkDeferredCanvas::DeferredDevice::flush() {
    flushPending();
    fImmediateCanvas->flush();
}

void SkDeferredCanvas::DeferredDevice::flushIfNeeded(const SkBitmap& bitmap) {
    if (bitmap.isImmutable()) {
        return; // safe to deffer without registering a dependency
    }

    // For now, drawing a writable bitmap triggers a flush
    // TODO: implement read-only semantics and auto buffer duplication on write
    // in SkBitmap/SkPixelRef, which will make deferral possible in this case.
    flushPending();
}

uint32_t SkDeferredCanvas::DeferredDevice::getDeviceCapabilities() { 
    return fImmediateDevice->getDeviceCapabilities();
}

int SkDeferredCanvas::DeferredDevice::width() const { 
    return fImmediateDevice->width();
}

int SkDeferredCanvas::DeferredDevice::height() const {
    return fImmediateDevice->height(); 
}

SkGpuRenderTarget* SkDeferredCanvas::DeferredDevice::accessRenderTarget() {
    flushPending();
    return fImmediateDevice->accessRenderTarget();
}

void SkDeferredCanvas::DeferredDevice::writePixels(const SkBitmap& bitmap,
    int x, int y, SkCanvas::Config8888 config8888) {

    if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
        (y + bitmap.height()) >= height()) {
        contentsCleared();
    }

    if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
        SkCanvas::kNative_Premul_Config8888 != config8888 &&
        kPMColorAlias != config8888) {
        //Special case config: no deferral
        flushPending();
        fImmediateDevice->writePixels(bitmap, x, y, config8888);
    }

    SkPaint paint;
    paint.setXfermodeMode(SkXfermode::kSrc_Mode);
    fRecordingCanvas->drawSprite(bitmap, x, y, &paint);
    flushIfNeeded(bitmap);
}

const SkBitmap& SkDeferredCanvas::DeferredDevice::onAccessBitmap(SkBitmap*) {
    flushPending();
    return fImmediateDevice->accessBitmap(false);
}

SkDevice* SkDeferredCanvas::DeferredDevice::onCreateCompatibleDevice(
    SkBitmap::Config config, int width, int height, bool isOpaque,
    Usage usage) {

    // Save layer usage not supported, and not required by SkDeferredCanvas.
    SkASSERT(usage != kSaveLayer_Usage);
    // Create a compatible non-deferred device.
    SkDevice* compatibleDevice = 
        fImmediateDevice->createCompatibleDevice(config, width, height, 
            isOpaque);
    return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fDeviceContext));
}

bool SkDeferredCanvas::DeferredDevice::onReadPixels(
    const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
    flushPending();
    return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
                                                   x, y, config8888);
}