aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkMatrixClipStateMgr.h
blob: 4b018c4e4ac25ef24746fc071d553c5d37f7bd64 (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
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkMatrixClipStateMgr_DEFINED
#define SkMatrixClipStateMgr_DEFINED

#include "SkCanvas.h"
#include "SkMatrix.h"
#include "SkRegion.h"
#include "SkRRect.h"
#include "SkTypes.h"
#include "SkTArray.h"

class SkPictureRecord;
class SkWriter32;

// The SkMatrixClipStateMgr collapses the matrix/clip state of an SkPicture into
// a series of save/restore blocks of consistent matrix clip state, e.g.:
//
//   save
//     clip(s)
//     concat
//     ... draw ops ...
//   restore
//
// SaveLayers simply add another level, e.g.:
//
//   save
//     clip(s)
//     concat
//     ... draw ops ...
//     saveLayer
//       save
//         clip(s)
//         concat
//         ... draw ops ...
//       restore
//     restore
//   restore
//
// As a side effect of this process all saves and saveLayers will become
// kMatrixClip_SaveFlag style saves/saveLayers.

// The SkMatrixClipStateMgr works by intercepting all the save*, restore, clip*,
// and matrix calls sent to SkCanvas in order to track the current matrix/clip
// state. All the other canvas calls get funnelled into a generic "call" entry 
// point that signals that a state block is required.
class SkMatrixClipStateMgr {
public:
    static const int32_t kIdentityWideOpenStateID = 0;

    class MatrixClipState {
    public:
        class MatrixInfo {
        public:
            SkMatrix fMatrix;
            // TODO: add an internal dictionary and an ID here
        };

        class ClipInfo : public SkNoncopyable {
        public:
            ClipInfo() {}

            bool clipRect(const SkRect& rect, 
                          SkRegion::Op op, 
                          bool doAA, 
                          const SkMatrix& matrix) {
                ClipOp& newClip = fClips.push_back();
                newClip.fClipType = kRect_ClipType;
                newClip.fGeom.fRRect.setRect(rect);   // storing the clipRect in the RRect
                newClip.fOp = op;
                newClip.fDoAA = doAA;
                newClip.fMatrix = matrix;
                newClip.fOffset = kInvalidJumpOffset;
                return false;
            }

            bool clipRRect(const SkRRect& rrect, 
                           SkRegion::Op op, 
                           bool doAA, 
                           const SkMatrix& matrix) {
                ClipOp& newClip = fClips.push_back();
                newClip.fClipType = kRRect_ClipType;
                newClip.fGeom.fRRect = rrect;
                newClip.fOp = op;
                newClip.fDoAA = doAA;
                newClip.fMatrix = matrix;
                newClip.fOffset = kInvalidJumpOffset;
                return false;
            }

            bool clipPath(SkPictureRecord* picRecord, 
                          const SkPath& path, 
                          SkRegion::Op op, 
                          bool doAA, 
                          const SkMatrix& matrix);
            bool clipRegion(SkPictureRecord* picRecord, 
                            const SkRegion& region, 
                            SkRegion::Op op, 
                            const SkMatrix& matrix);
            void writeClip(SkMatrix* curMat, 
                           SkPictureRecord* picRecord,
                           bool* overrideFirstOp);
            void fillInSkips(SkWriter32* writer, int32_t restoreOffset);

#ifdef SK_DEBUG
            void checkOffsetNotEqual(int32_t offset) {
                for (int i = 0; i < fClips.count(); ++i) {
                    ClipOp& curClip = fClips[i];
                    SkASSERT(offset != curClip.fOffset);
                }
            }
#endif
        private:
            enum ClipType {
                kRect_ClipType,
                kRRect_ClipType,
                kPath_ClipType,
                kRegion_ClipType
            };

            static const int kInvalidJumpOffset = -1;

            class ClipOp {
            public:
                ClipOp() {}
                ~ClipOp() {
                    if (kRegion_ClipType == fClipType) {
                        SkDELETE(fGeom.fRegion);
                    }
                }

                ClipType     fClipType;

                union {
                    SkRRect         fRRect;        // also stores clipRect
                    int             fPathID;
                    // TODO: add internal dictionary of regions
                    // This parameter forces us to have a dtor and thus use
                    // SkTArray rather then SkTDArray!
                    const SkRegion* fRegion;
                } fGeom;

                bool         fDoAA;
                SkRegion::Op fOp;

                // The CTM in effect when this clip call was issued
                // TODO: add an internal dictionary and replace with ID
                SkMatrix     fMatrix;

                // The offset of this clipOp's "jump-to-offset" location in the skp.
                // -1 means the offset hasn't been written.
                int32_t      fOffset;
            };

            SkTArray<ClipOp> fClips;

            typedef SkNoncopyable INHERITED;
        };

        MatrixClipState(MatrixClipState* prev, int flags) 
#ifdef SK_DEBUG
            : fPrev(prev)
#endif
        {
            if (NULL == prev) {
                fLayerID = 0;

                fMatrixInfoStorage.fMatrix.reset();
                fMatrixInfo = &fMatrixInfoStorage;
                fClipInfo = &fClipInfoStorage;  // ctor handles init of fClipInfoStorage

                // The identity/wide-open-clip state is current by default
                fMCStateID = kIdentityWideOpenStateID;
            } 
            else {
                fLayerID = prev->fLayerID;

                if (flags & SkCanvas::kMatrix_SaveFlag) {
                    fMatrixInfoStorage = *prev->fMatrixInfo;
                    fMatrixInfo = &fMatrixInfoStorage;
                } else {
                    fMatrixInfo = prev->fMatrixInfo;
                }

                if (flags & SkCanvas::kClip_SaveFlag) {
                    // We don't copy the ClipOps of the previous clip states
                    fClipInfo = &fClipInfoStorage;
                } else {
                    fClipInfo = prev->fClipInfo;
                }

                // Initially a new save/saveLayer represents the same MC state
                // as its predecessor.
                fMCStateID = prev->fMCStateID;
            }

            fIsSaveLayer = false;
        }

        MatrixInfo*  fMatrixInfo;
        MatrixInfo   fMatrixInfoStorage;

        ClipInfo*    fClipInfo;
        ClipInfo     fClipInfoStorage;

        // Tracks the current depth of saveLayers to support the isDrawingToLayer call
        int          fLayerID;
        // Does this MC state represent a saveLayer call?
        bool         fIsSaveLayer;

        // The next two fields are only valid when fIsSaveLayer is set.
        int32_t      fSaveLayerBaseStateID;
        bool         fSaveLayerBracketed;

#ifdef SK_DEBUG
        MatrixClipState* fPrev; // debugging aid
#endif

        int32_t     fMCStateID;
    };

    enum CallType {
        kMatrix_CallType,
        kClip_CallType,
        kOther_CallType
    };

    SkMatrixClipStateMgr();

    void init(SkPictureRecord* picRecord) {
        // Note: we're not taking a ref here. It is expected that the SkMatrixClipStateMgr
        // is owned by the SkPictureRecord object
        fPicRecord = picRecord; 
    }

    // TODO: need to override canvas' getSaveCount. Right now we pass the
    // save* and restore calls on to the base SkCanvas in SkPictureRecord but
    // this duplicates effort.
    int getSaveCount() const { return fMatrixClipStack.count(); }

    int save(SkCanvas::SaveFlags flags);

    int saveLayer(const SkRect* bounds, const SkPaint* paint, SkCanvas::SaveFlags flags);

    bool isDrawingToLayer() const {
        return fCurMCState->fLayerID > 0;
    }

    void restore();

    bool translate(SkScalar dx, SkScalar dy) {
        this->call(kMatrix_CallType);
        return fCurMCState->fMatrixInfo->fMatrix.preTranslate(dx, dy);
    }

    bool scale(SkScalar sx, SkScalar sy) {
        this->call(kMatrix_CallType);
        return fCurMCState->fMatrixInfo->fMatrix.preScale(sx, sy);
    }

    bool rotate(SkScalar degrees) {
        this->call(kMatrix_CallType);
        return fCurMCState->fMatrixInfo->fMatrix.preRotate(degrees);
    }

    bool skew(SkScalar sx, SkScalar sy) {
        this->call(kMatrix_CallType);
        return fCurMCState->fMatrixInfo->fMatrix.preSkew(sx, sy);
    }

    bool concat(const SkMatrix& matrix) {
        this->call(kMatrix_CallType);
        return fCurMCState->fMatrixInfo->fMatrix.preConcat(matrix);
    }

    void setMatrix(const SkMatrix& matrix) {
        this->call(kMatrix_CallType);
        fCurMCState->fMatrixInfo->fMatrix = matrix;
    }

    bool clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
        this->call(SkMatrixClipStateMgr::kClip_CallType);
        return fCurMCState->fClipInfo->clipRect(rect, op, doAA,
                                                fCurMCState->fMatrixInfo->fMatrix);
    }

    bool clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
        this->call(SkMatrixClipStateMgr::kClip_CallType);
        return fCurMCState->fClipInfo->clipRRect(rrect, op, doAA,
                                                 fCurMCState->fMatrixInfo->fMatrix);
    }

    bool clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
        this->call(SkMatrixClipStateMgr::kClip_CallType);
        return fCurMCState->fClipInfo->clipPath(fPicRecord, path, op, doAA,
                                                fCurMCState->fMatrixInfo->fMatrix);
    }

    bool clipRegion(const SkRegion& region, SkRegion::Op op) {
        this->call(SkMatrixClipStateMgr::kClip_CallType);
        return fCurMCState->fClipInfo->clipRegion(fPicRecord, region, op,
                                                  fCurMCState->fMatrixInfo->fMatrix);
    }

    bool call(CallType callType);

    void fillInSkips(SkWriter32* writer, int32_t restoreOffset) {
        // Since we write out the entire clip stack at each block start we
        // need to update the skips for the entire stack each time too.
        SkDeque::F2BIter iter(fMatrixClipStack);

        for (const MatrixClipState* state = (const MatrixClipState*) iter.next();
             state != NULL;
             state = (const MatrixClipState*) iter.next()) {
            state->fClipInfo->fillInSkips(writer, restoreOffset);
        }
    }

    void finish();

protected:
    SkPictureRecord* fPicRecord;

    uint32_t         fMatrixClipStackStorage[43]; // sized to fit 2 clip states
    SkDeque          fMatrixClipStack;
    MatrixClipState* fCurMCState;

    // The MCStateID of the state currently in effect in the byte stream. 0 if none.
    int32_t          fCurOpenStateID;

    SkDEBUGCODE(void validate();)

    static void WriteDeltaMat(SkPictureRecord* picRecord,
                              const SkMatrix& current, 
                              const SkMatrix& desired);
    static int32_t   NewMCStateID();
};

#endif