aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/record/SkRecordDraw.cpp
blob: f0d3772e9fbbc426d57b100481a09ab39234154b (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
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkRecordDraw.h"

namespace {

// This is an SkRecord visitor that will draw that SkRecord to an SkCanvas.
class Draw : SkNoncopyable {
public:
    explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(false) {}

    unsigned index() const { return fIndex; }
    void next() { ++fIndex; }

    template <typename T> void operator()(const T& r) {
        if (!this->canSkip(r)) {
            this->draw(r);
            this->updateClip<T>();
        }
    }

private:
    // Can we skip this command right now?
    template <typename T> bool canSkip(const T&) const {
        // We can skip most commands if the clip is empty.  Exceptions are specialized below.
        return fClipEmpty;
    }

    // No base case, so we'll be compile-time checked that we implemented all possibilities below.
    template <typename T> void draw(const T&);

    // Update fClipEmpty if necessary.
    template <typename T> void updateClip() {
        // Most commands don't change the clip.  Exceptions are specialized below.
    }

    SkCanvas* fCanvas;
    unsigned fIndex;
    bool fClipEmpty;
};

// These commands may change the clip.
#define UPDATE_CLIP(T) template <> void Draw::updateClip<SkRecords::T>() \
    { fClipEmpty = fCanvas->isClipEmpty(); }
UPDATE_CLIP(Restore);
UPDATE_CLIP(SaveLayer);
UPDATE_CLIP(ClipPath);
UPDATE_CLIP(ClipRRect);
UPDATE_CLIP(ClipRect);
UPDATE_CLIP(ClipRegion);
#undef UPDATE_CLIP

// These commands must always run.
#define CAN_SKIP(T) template <> bool Draw::canSkip(const SkRecords::T&) const { return false; }
CAN_SKIP(Restore);
CAN_SKIP(Save);
CAN_SKIP(SaveLayer);
CAN_SKIP(Clear);
CAN_SKIP(PushCull);
CAN_SKIP(PopCull);
#undef CAN_SKIP

// We can skip these commands if they're intersecting with a clip that's already empty.
#define CAN_SKIP(T) template <> bool Draw::canSkip(const SkRecords::T& r) const \
    { return fClipEmpty && SkRegion::kIntersect_Op == r.op; }
CAN_SKIP(ClipPath);
CAN_SKIP(ClipRRect);
CAN_SKIP(ClipRect);
CAN_SKIP(ClipRegion);
#undef CAN_SKIP

static bool can_skip_text(const SkCanvas& c, const SkPaint& p, SkScalar minY, SkScalar maxY) {
    // If we're drawing vertical text, none of the checks we're about to do make any sense.
    // We use canComputeFastBounds as a proxy for "is this text going to be rectangular?".
    if (p.isVerticalText() || !p.canComputeFastBounds()) {
        return false;
    }

    // Rather than checking the top and bottom font metrics, we guess.  Actually looking up the top
    // and bottom metrics is slow, and this overapproximation should be good enough.
    const SkScalar buffer = p.getTextSize() * 1.5f;
    SkDEBUGCODE(SkPaint::FontMetrics metrics;)
    SkDEBUGCODE(p.getFontMetrics(&metrics);)
    SkASSERT(-buffer <= metrics.fTop);
    SkASSERT(+buffer >= metrics.fBottom);
    return c.quickRejectY(minY - buffer, maxY + buffer);
}

template <> bool Draw::canSkip(const SkRecords::DrawPosTextH& r) const {
    return fClipEmpty || can_skip_text(*fCanvas, r.paint, r.y, r.y);
}

template <> bool Draw::canSkip(const SkRecords::DrawPosText& r) const {
    if (fClipEmpty) {
        return true;
    }

    // TODO(mtklein): may want to move this minY/maxY calculation into a one-time pass
    const unsigned points = r.paint.countText(r.text, r.byteLength);
    if (points == 0) {
        return true;
    }
    SkScalar minY = SK_ScalarInfinity, maxY = SK_ScalarNegativeInfinity;
    for (unsigned i = 0; i < points; i++) {
        minY = SkTMin(minY, r.pos[i].fY);
        maxY = SkTMax(maxY, r.pos[i].fY);
    }

    return can_skip_text(*fCanvas, r.paint, minY, maxY);
}

#define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; }
DRAW(Restore, restore());
DRAW(Save, save(r.flags));
DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
DRAW(PopCull, popCull());
DRAW(Clear, clear(r.color));
DRAW(Concat, concat(r.matrix));
DRAW(SetMatrix, setMatrix(r.matrix));

DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
DRAW(ClipRegion, clipRegion(r.region, r.op));

DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
DRAW(DrawOval, drawOval(r.oval, r.paint));
DRAW(DrawPaint, drawPaint(r.paint));
DRAW(DrawPath, drawPath(r.path, r.paint));
DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
DRAW(DrawRect, drawRect(r.rect, r.paint));
DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
                                r.xmode.get(), r.indices, r.indexCount, r.paint));
#undef DRAW

// PushCull is a bit of a oddball.  We might be able to just skip until just past its popCull.
template <> void Draw::draw(const SkRecords::PushCull& r) {
    if (r.popOffset != SkRecords::kUnsetPopOffset && fCanvas->quickReject(r.rect)) {
        fIndex += r.popOffset;
    } else {
        fCanvas->pushCull(r.rect);
    }
}

}  // namespace

void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
    for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
        record.visit(draw.index(), draw);
    }
}