robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #ifndef SkMatrixClipStateMgr_DEFINED |
| 8 | #define SkMatrixClipStateMgr_DEFINED |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkMatrix.h" |
| 12 | #include "SkRegion.h" |
| 13 | #include "SkRRect.h" |
| 14 | #include "SkTypes.h" |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 15 | #include "SkTDArray.h" |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 16 | |
| 17 | class SkPictureRecord; |
| 18 | class SkWriter32; |
| 19 | |
| 20 | // The SkMatrixClipStateMgr collapses the matrix/clip state of an SkPicture into |
| 21 | // a series of save/restore blocks of consistent matrix clip state, e.g.: |
| 22 | // |
| 23 | // save |
| 24 | // clip(s) |
| 25 | // concat |
| 26 | // ... draw ops ... |
| 27 | // restore |
| 28 | // |
| 29 | // SaveLayers simply add another level, e.g.: |
| 30 | // |
| 31 | // save |
| 32 | // clip(s) |
| 33 | // concat |
| 34 | // ... draw ops ... |
| 35 | // saveLayer |
| 36 | // save |
| 37 | // clip(s) |
| 38 | // concat |
| 39 | // ... draw ops ... |
| 40 | // restore |
| 41 | // restore |
| 42 | // restore |
| 43 | // |
| 44 | // As a side effect of this process all saves and saveLayers will become |
| 45 | // kMatrixClip_SaveFlag style saves/saveLayers. |
| 46 | |
| 47 | // The SkMatrixClipStateMgr works by intercepting all the save*, restore, clip*, |
| 48 | // and matrix calls sent to SkCanvas in order to track the current matrix/clip |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 49 | // state. All the other canvas calls get funnelled into a generic "call" entry |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 50 | // point that signals that a state block is required. |
| 51 | class SkMatrixClipStateMgr { |
| 52 | public: |
| 53 | static const int32_t kIdentityWideOpenStateID = 0; |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 54 | static const int kIdentityMatID = 0; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 56 | class MatrixClipState : SkNoncopyable { |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 57 | public: |
| 58 | class MatrixInfo { |
| 59 | public: |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 60 | void reset() { |
| 61 | fMatrixID = kIdentityMatID; |
| 62 | fMatrix.reset(); |
| 63 | } |
| 64 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 65 | void preTranslate(SkScalar dx, SkScalar dy) { |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 66 | fMatrixID = -1; |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 67 | fMatrix.preTranslate(dx, dy); |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 68 | } |
| 69 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 70 | void preScale(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 71 | fMatrixID = -1; |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 72 | fMatrix.preScale(sx, sy); |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 73 | } |
| 74 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 75 | void preRotate(SkScalar degrees) { |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 76 | fMatrixID = -1; |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 77 | fMatrix.preRotate(degrees); |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 78 | } |
| 79 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 80 | void preSkew(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 81 | fMatrixID = -1; |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 82 | fMatrix.preSkew(sx, sy); |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 83 | } |
| 84 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 85 | void preConcat(const SkMatrix& matrix) { |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 86 | fMatrixID = -1; |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 87 | fMatrix.preConcat(matrix); |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void setMatrix(const SkMatrix& matrix) { |
| 91 | fMatrixID = -1; |
| 92 | fMatrix = matrix; |
| 93 | } |
| 94 | |
| 95 | int getID(SkMatrixClipStateMgr* mgr) { |
| 96 | if (fMatrixID >= 0) { |
| 97 | return fMatrixID; |
| 98 | } |
| 99 | |
| 100 | fMatrixID = mgr->addMatToDict(fMatrix); |
| 101 | return fMatrixID; |
| 102 | } |
| 103 | |
| 104 | private: |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 105 | SkMatrix fMatrix; |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 106 | int fMatrixID; |
commit-bot@chromium.org | 6c5f1fd | 2014-02-24 17:24:15 +0000 | [diff] [blame] | 107 | |
| 108 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 111 | class ClipInfo : SkNoncopyable { |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 112 | public: |
| 113 | ClipInfo() {} |
| 114 | |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 115 | bool clipRect(const SkRect& rect, |
| 116 | SkRegion::Op op, |
| 117 | bool doAA, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 118 | int matrixID) { |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 119 | ClipOp* newClip = fClips.append(); |
| 120 | newClip->fClipType = kRect_ClipType; |
| 121 | newClip->fGeom.fRRect.setRect(rect); // storing the clipRect in the RRect |
| 122 | newClip->fOp = op; |
| 123 | newClip->fDoAA = doAA; |
| 124 | newClip->fMatrixID = matrixID; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 128 | bool clipRRect(const SkRRect& rrect, |
| 129 | SkRegion::Op op, |
| 130 | bool doAA, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 131 | int matrixID) { |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 132 | ClipOp* newClip = fClips.append(); |
| 133 | newClip->fClipType = kRRect_ClipType; |
| 134 | newClip->fGeom.fRRect = rrect; |
| 135 | newClip->fOp = op; |
| 136 | newClip->fDoAA = doAA; |
| 137 | newClip->fMatrixID = matrixID; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 141 | bool clipPath(SkPictureRecord* picRecord, |
| 142 | const SkPath& path, |
| 143 | SkRegion::Op op, |
| 144 | bool doAA, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 145 | int matrixID); |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 146 | bool clipRegion(SkPictureRecord* picRecord, |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 147 | int regionID, |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 148 | SkRegion::Op op, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 149 | int matrixID); |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 150 | void writeClip(int* curMatID, SkMatrixClipStateMgr* mgr); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 151 | |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 152 | SkDEBUGCODE(int numClips() const { return fClips.count(); }) |
| 153 | |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 154 | private: |
| 155 | enum ClipType { |
| 156 | kRect_ClipType, |
| 157 | kRRect_ClipType, |
| 158 | kPath_ClipType, |
| 159 | kRegion_ClipType |
| 160 | }; |
| 161 | |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 162 | class ClipOp { |
| 163 | public: |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 164 | ClipType fClipType; |
| 165 | |
| 166 | union { |
commit-bot@chromium.org | 6c5f1fd | 2014-02-24 17:24:15 +0000 | [diff] [blame] | 167 | SkRRect fRRect; // also stores clip rect |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 168 | int fPathID; |
| 169 | int fRegionID; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 170 | } fGeom; |
| 171 | |
| 172 | bool fDoAA; |
| 173 | SkRegion::Op fOp; |
| 174 | |
| 175 | // The CTM in effect when this clip call was issued |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 176 | int fMatrixID; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 179 | SkTDArray<ClipOp> fClips; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 180 | |
| 181 | typedef SkNoncopyable INHERITED; |
| 182 | }; |
| 183 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 184 | MatrixClipState(MatrixClipState* prev) |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 185 | : fPrev(prev) |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 186 | { |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 187 | fHasOpen = false; |
| 188 | |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 189 | if (NULL == prev) { |
| 190 | fLayerID = 0; |
| 191 | |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 192 | fMatrixInfoStorage.reset(); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 193 | fMatrixInfo = &fMatrixInfoStorage; |
| 194 | fClipInfo = &fClipInfoStorage; // ctor handles init of fClipInfoStorage |
| 195 | |
| 196 | // The identity/wide-open-clip state is current by default |
| 197 | fMCStateID = kIdentityWideOpenStateID; |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 198 | #ifdef SK_DEBUG |
| 199 | fExpectedDepth = 1; |
| 200 | #endif |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 201 | } |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 202 | else { |
| 203 | fLayerID = prev->fLayerID; |
| 204 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 205 | fMatrixInfoStorage = *prev->fMatrixInfo; |
| 206 | fMatrixInfo = &fMatrixInfoStorage; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 207 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 208 | // We don't copy the ClipOps of the previous clip states |
| 209 | fClipInfo = &fClipInfoStorage; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 210 | |
| 211 | // Initially a new save/saveLayer represents the same MC state |
| 212 | // as its predecessor. |
| 213 | fMCStateID = prev->fMCStateID; |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 214 | #ifdef SK_DEBUG |
| 215 | fExpectedDepth = prev->fExpectedDepth; |
| 216 | #endif |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | fIsSaveLayer = false; |
| 220 | } |
| 221 | |
| 222 | MatrixInfo* fMatrixInfo; |
| 223 | MatrixInfo fMatrixInfoStorage; |
| 224 | |
| 225 | ClipInfo* fClipInfo; |
| 226 | ClipInfo fClipInfoStorage; |
| 227 | |
| 228 | // Tracks the current depth of saveLayers to support the isDrawingToLayer call |
| 229 | int fLayerID; |
| 230 | // Does this MC state represent a saveLayer call? |
| 231 | bool fIsSaveLayer; |
| 232 | |
commit-bot@chromium.org | 6c5f1fd | 2014-02-24 17:24:15 +0000 | [diff] [blame] | 233 | // The next field is only valid when fIsSaveLayer is set. |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 234 | SkTDArray<int>* fSavedSkipOffsets; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 235 | |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 236 | // Does the MC state have an open block in the skp? |
| 237 | bool fHasOpen; |
| 238 | |
skia.committer@gmail.com | 7911ade | 2014-02-22 03:02:18 +0000 | [diff] [blame] | 239 | MatrixClipState* fPrev; |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 240 | |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 241 | #ifdef SK_DEBUG |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 242 | int fExpectedDepth; // debugging aid |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 243 | #endif |
| 244 | |
| 245 | int32_t fMCStateID; |
| 246 | }; |
| 247 | |
| 248 | enum CallType { |
| 249 | kMatrix_CallType, |
| 250 | kClip_CallType, |
| 251 | kOther_CallType |
| 252 | }; |
| 253 | |
| 254 | SkMatrixClipStateMgr(); |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 255 | ~SkMatrixClipStateMgr(); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 256 | |
| 257 | void init(SkPictureRecord* picRecord) { |
| 258 | // Note: we're not taking a ref here. It is expected that the SkMatrixClipStateMgr |
| 259 | // is owned by the SkPictureRecord object |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 260 | fPicRecord = picRecord; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 261 | } |
| 262 | |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 263 | SkPictureRecord* getPicRecord() { return fPicRecord; } |
| 264 | |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 265 | // TODO: need to override canvas' getSaveCount. Right now we pass the |
| 266 | // save* and restore calls on to the base SkCanvas in SkPictureRecord but |
| 267 | // this duplicates effort. |
| 268 | int getSaveCount() const { return fMatrixClipStack.count(); } |
| 269 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 270 | int save(); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 271 | |
| 272 | int saveLayer(const SkRect* bounds, const SkPaint* paint, SkCanvas::SaveFlags flags); |
| 273 | |
| 274 | bool isDrawingToLayer() const { |
| 275 | return fCurMCState->fLayerID > 0; |
| 276 | } |
| 277 | |
| 278 | void restore(); |
| 279 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 280 | void translate(SkScalar dx, SkScalar dy) { |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 281 | this->call(kMatrix_CallType); |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 282 | fCurMCState->fMatrixInfo->preTranslate(dx, dy); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 283 | } |
| 284 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 285 | void scale(SkScalar sx, SkScalar sy) { |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 286 | this->call(kMatrix_CallType); |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 287 | fCurMCState->fMatrixInfo->preScale(sx, sy); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 288 | } |
| 289 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 290 | void rotate(SkScalar degrees) { |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 291 | this->call(kMatrix_CallType); |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 292 | fCurMCState->fMatrixInfo->preRotate(degrees); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 293 | } |
| 294 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 295 | void skew(SkScalar sx, SkScalar sy) { |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 296 | this->call(kMatrix_CallType); |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 297 | fCurMCState->fMatrixInfo->preSkew(sx, sy); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 298 | } |
| 299 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 300 | void concat(const SkMatrix& matrix) { |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 301 | this->call(kMatrix_CallType); |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 302 | fCurMCState->fMatrixInfo->preConcat(matrix); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void setMatrix(const SkMatrix& matrix) { |
| 306 | this->call(kMatrix_CallType); |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 307 | fCurMCState->fMatrixInfo->setMatrix(matrix); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | bool clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
| 311 | this->call(SkMatrixClipStateMgr::kClip_CallType); |
| 312 | return fCurMCState->fClipInfo->clipRect(rect, op, doAA, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 313 | fCurMCState->fMatrixInfo->getID(this)); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | bool clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| 317 | this->call(SkMatrixClipStateMgr::kClip_CallType); |
| 318 | return fCurMCState->fClipInfo->clipRRect(rrect, op, doAA, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 319 | fCurMCState->fMatrixInfo->getID(this)); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | bool clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
| 323 | this->call(SkMatrixClipStateMgr::kClip_CallType); |
| 324 | return fCurMCState->fClipInfo->clipPath(fPicRecord, path, op, doAA, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 325 | fCurMCState->fMatrixInfo->getID(this)); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | bool clipRegion(const SkRegion& region, SkRegion::Op op) { |
| 329 | this->call(SkMatrixClipStateMgr::kClip_CallType); |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 330 | int regionID = this->addRegionToDict(region); |
| 331 | return fCurMCState->fClipInfo->clipRegion(fPicRecord, regionID, op, |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 332 | fCurMCState->fMatrixInfo->getID(this)); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | bool call(CallType callType); |
| 336 | |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 337 | void fillInSkips(SkWriter32* writer, int32_t restoreOffset); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 338 | |
| 339 | void finish(); |
| 340 | |
| 341 | protected: |
| 342 | SkPictureRecord* fPicRecord; |
| 343 | |
| 344 | uint32_t fMatrixClipStackStorage[43]; // sized to fit 2 clip states |
| 345 | SkDeque fMatrixClipStack; |
| 346 | MatrixClipState* fCurMCState; |
| 347 | |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 348 | // This dictionary doesn't actually de-duplicate the matrices (except for the |
| 349 | // identity matrix). It merely stores the matrices and allows them to be looked |
skia.committer@gmail.com | 22ef2c3 | 2014-02-13 03:01:57 +0000 | [diff] [blame] | 350 | // up by ID later. The de-duplication mainly falls upon the matrix/clip stack |
| 351 | // which stores the ID so a revisited clip/matrix (via popping the stack) will |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 352 | // use the same ID. |
| 353 | SkTDArray<SkMatrix> fMatrixDict; |
| 354 | |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 355 | SkTDArray<SkRegion*> fRegionDict; |
| 356 | |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 357 | // The MCStateID of the state currently in effect in the byte stream. 0 if none. |
| 358 | int32_t fCurOpenStateID; |
skia.committer@gmail.com | c05d285 | 2014-02-20 03:01:56 +0000 | [diff] [blame] | 359 | // The skip offsets for the current open state. These are the locations in the |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 360 | // skp that must be filled in when the current open state is closed. These are |
| 361 | // here rather then distributed across the MatrixClipState's because saveLayers |
| 362 | // can cause MC states to be nested. |
commit-bot@chromium.org | dcecb16 | 2014-04-22 17:54:29 +0000 | [diff] [blame] | 363 | SkTDArray<int32_t> *fSkipOffsets; // TODO: should we store u32 or size_t instead? |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 364 | |
| 365 | SkDEBUGCODE(void validate();) |
| 366 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 367 | int MCStackPush(); |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 368 | |
commit-bot@chromium.org | dcecb16 | 2014-04-22 17:54:29 +0000 | [diff] [blame] | 369 | void addClipOffset(size_t offset) { |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 370 | SkASSERT(NULL != fSkipOffsets); |
| 371 | SkASSERT(kIdentityWideOpenStateID != fCurOpenStateID); |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 372 | SkASSERT(fCurMCState->fHasOpen); |
| 373 | SkASSERT(!fCurMCState->fIsSaveLayer); |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 374 | |
commit-bot@chromium.org | dcecb16 | 2014-04-22 17:54:29 +0000 | [diff] [blame] | 375 | *fSkipOffsets->append() = SkToS32(offset); |
commit-bot@chromium.org | 92da60c | 2014-02-19 15:11:23 +0000 | [diff] [blame] | 376 | } |
| 377 | |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 378 | void writeDeltaMat(int currentMatID, int desiredMatID); |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 379 | static int32_t NewMCStateID(); |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 380 | |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 381 | int addRegionToDict(const SkRegion& region); |
| 382 | const SkRegion* lookupRegion(int index) { |
| 383 | SkASSERT(index >= 0 && index < fRegionDict.count()); |
skia.committer@gmail.com | 22ef2c3 | 2014-02-13 03:01:57 +0000 | [diff] [blame] | 384 | return fRegionDict[index]; |
commit-bot@chromium.org | 9a67a7e | 2014-02-12 23:22:15 +0000 | [diff] [blame] | 385 | } |
| 386 | |
commit-bot@chromium.org | c5dada8 | 2014-02-12 15:48:04 +0000 | [diff] [blame] | 387 | // TODO: add stats to check if the dictionary really does |
| 388 | // reduce the size of the SkPicture. |
| 389 | int addMatToDict(const SkMatrix& mat); |
| 390 | const SkMatrix& lookupMat(int index) { |
| 391 | SkASSERT(index >= 0 && index < fMatrixDict.count()); |
| 392 | return fMatrixDict[index]; |
| 393 | } |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 394 | |
commit-bot@chromium.org | 6c5f1fd | 2014-02-24 17:24:15 +0000 | [diff] [blame] | 395 | bool isNestingMCState(int stateID); |
robertphillips@google.com | fc4ded9 | 2014-02-21 17:46:37 +0000 | [diff] [blame] | 396 | |
| 397 | #ifdef SK_DEBUG |
| 398 | int fActualDepth; |
| 399 | #endif |
commit-bot@chromium.org | 6c5f1fd | 2014-02-24 17:24:15 +0000 | [diff] [blame] | 400 | |
| 401 | // save layers are nested within a specific MC state. This stack tracks |
| 402 | // the nesting MC state's ID as save layers are pushed and popped. |
| 403 | SkTDArray<int> fStateIDStack; |
robertphillips@google.com | 105a4a5 | 2014-02-11 15:10:40 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
robertphillips@google.com | 3cd6fdf | 2014-02-11 15:40:54 +0000 | [diff] [blame] | 406 | #endif |