1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkMatrixClipStateMgr.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,414 @@ 1.4 +/* 1.5 + * Copyright 2014 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 +#ifndef SkMatrixClipStateMgr_DEFINED 1.11 +#define SkMatrixClipStateMgr_DEFINED 1.12 + 1.13 +#include "SkCanvas.h" 1.14 +#include "SkMatrix.h" 1.15 +#include "SkRegion.h" 1.16 +#include "SkRRect.h" 1.17 +#include "SkTypes.h" 1.18 +#include "SkTDArray.h" 1.19 + 1.20 +class SkPictureRecord; 1.21 +class SkWriter32; 1.22 + 1.23 +// The SkMatrixClipStateMgr collapses the matrix/clip state of an SkPicture into 1.24 +// a series of save/restore blocks of consistent matrix clip state, e.g.: 1.25 +// 1.26 +// save 1.27 +// clip(s) 1.28 +// concat 1.29 +// ... draw ops ... 1.30 +// restore 1.31 +// 1.32 +// SaveLayers simply add another level, e.g.: 1.33 +// 1.34 +// save 1.35 +// clip(s) 1.36 +// concat 1.37 +// ... draw ops ... 1.38 +// saveLayer 1.39 +// save 1.40 +// clip(s) 1.41 +// concat 1.42 +// ... draw ops ... 1.43 +// restore 1.44 +// restore 1.45 +// restore 1.46 +// 1.47 +// As a side effect of this process all saves and saveLayers will become 1.48 +// kMatrixClip_SaveFlag style saves/saveLayers. 1.49 + 1.50 +// The SkMatrixClipStateMgr works by intercepting all the save*, restore, clip*, 1.51 +// and matrix calls sent to SkCanvas in order to track the current matrix/clip 1.52 +// state. All the other canvas calls get funnelled into a generic "call" entry 1.53 +// point that signals that a state block is required. 1.54 +class SkMatrixClipStateMgr { 1.55 +public: 1.56 + static const int32_t kIdentityWideOpenStateID = 0; 1.57 + static const int kIdentityMatID = 0; 1.58 + 1.59 + class MatrixClipState : public SkNoncopyable { 1.60 + public: 1.61 + class MatrixInfo { 1.62 + public: 1.63 + void reset() { 1.64 + fMatrixID = kIdentityMatID; 1.65 + fMatrix.reset(); 1.66 + } 1.67 + 1.68 + bool preTranslate(SkScalar dx, SkScalar dy) { 1.69 + fMatrixID = -1; 1.70 + return fMatrix.preTranslate(dx, dy); 1.71 + } 1.72 + 1.73 + bool preScale(SkScalar sx, SkScalar sy) { 1.74 + fMatrixID = -1; 1.75 + return fMatrix.preScale(sx, sy); 1.76 + } 1.77 + 1.78 + bool preRotate(SkScalar degrees) { 1.79 + fMatrixID = -1; 1.80 + return fMatrix.preRotate(degrees); 1.81 + } 1.82 + 1.83 + bool preSkew(SkScalar sx, SkScalar sy) { 1.84 + fMatrixID = -1; 1.85 + return fMatrix.preSkew(sx, sy); 1.86 + } 1.87 + 1.88 + bool preConcat(const SkMatrix& matrix) { 1.89 + fMatrixID = -1; 1.90 + return fMatrix.preConcat(matrix); 1.91 + } 1.92 + 1.93 + void setMatrix(const SkMatrix& matrix) { 1.94 + fMatrixID = -1; 1.95 + fMatrix = matrix; 1.96 + } 1.97 + 1.98 + int getID(SkMatrixClipStateMgr* mgr) { 1.99 + if (fMatrixID >= 0) { 1.100 + return fMatrixID; 1.101 + } 1.102 + 1.103 + fMatrixID = mgr->addMatToDict(fMatrix); 1.104 + return fMatrixID; 1.105 + } 1.106 + 1.107 + private: 1.108 + SkMatrix fMatrix; 1.109 + int fMatrixID; 1.110 + 1.111 + typedef SkNoncopyable INHERITED; 1.112 + }; 1.113 + 1.114 + class ClipInfo : public SkNoncopyable { 1.115 + public: 1.116 + ClipInfo() {} 1.117 + 1.118 + bool clipRect(const SkRect& rect, 1.119 + SkRegion::Op op, 1.120 + bool doAA, 1.121 + int matrixID) { 1.122 + ClipOp* newClip = fClips.append(); 1.123 + newClip->fClipType = kRect_ClipType; 1.124 + newClip->fGeom.fRRect.setRect(rect); // storing the clipRect in the RRect 1.125 + newClip->fOp = op; 1.126 + newClip->fDoAA = doAA; 1.127 + newClip->fMatrixID = matrixID; 1.128 + return false; 1.129 + } 1.130 + 1.131 + bool clipRRect(const SkRRect& rrect, 1.132 + SkRegion::Op op, 1.133 + bool doAA, 1.134 + int matrixID) { 1.135 + ClipOp* newClip = fClips.append(); 1.136 + newClip->fClipType = kRRect_ClipType; 1.137 + newClip->fGeom.fRRect = rrect; 1.138 + newClip->fOp = op; 1.139 + newClip->fDoAA = doAA; 1.140 + newClip->fMatrixID = matrixID; 1.141 + return false; 1.142 + } 1.143 + 1.144 + bool clipPath(SkPictureRecord* picRecord, 1.145 + const SkPath& path, 1.146 + SkRegion::Op op, 1.147 + bool doAA, 1.148 + int matrixID); 1.149 + bool clipRegion(SkPictureRecord* picRecord, 1.150 + int regionID, 1.151 + SkRegion::Op op, 1.152 + int matrixID); 1.153 + void writeClip(int* curMatID, SkMatrixClipStateMgr* mgr); 1.154 + 1.155 + SkDEBUGCODE(int numClips() const { return fClips.count(); }) 1.156 + 1.157 + private: 1.158 + enum ClipType { 1.159 + kRect_ClipType, 1.160 + kRRect_ClipType, 1.161 + kPath_ClipType, 1.162 + kRegion_ClipType 1.163 + }; 1.164 + 1.165 + class ClipOp { 1.166 + public: 1.167 + ClipType fClipType; 1.168 + 1.169 + union { 1.170 + SkRRect fRRect; // also stores clip rect 1.171 + int fPathID; 1.172 + int fRegionID; 1.173 + } fGeom; 1.174 + 1.175 + bool fDoAA; 1.176 + SkRegion::Op fOp; 1.177 + 1.178 + // The CTM in effect when this clip call was issued 1.179 + int fMatrixID; 1.180 + }; 1.181 + 1.182 + SkTDArray<ClipOp> fClips; 1.183 + 1.184 + typedef SkNoncopyable INHERITED; 1.185 + }; 1.186 + 1.187 + MatrixClipState(MatrixClipState* prev, int flags) 1.188 + : fPrev(prev) 1.189 + { 1.190 + fHasOpen = false; 1.191 + 1.192 + if (NULL == prev) { 1.193 + fLayerID = 0; 1.194 + 1.195 + fMatrixInfoStorage.reset(); 1.196 + fMatrixInfo = &fMatrixInfoStorage; 1.197 + fClipInfo = &fClipInfoStorage; // ctor handles init of fClipInfoStorage 1.198 + 1.199 + // The identity/wide-open-clip state is current by default 1.200 + fMCStateID = kIdentityWideOpenStateID; 1.201 +#ifdef SK_DEBUG 1.202 + fExpectedDepth = 1; 1.203 +#endif 1.204 + } 1.205 + else { 1.206 + fLayerID = prev->fLayerID; 1.207 + 1.208 + if (flags & SkCanvas::kMatrix_SaveFlag) { 1.209 + fMatrixInfoStorage = *prev->fMatrixInfo; 1.210 + fMatrixInfo = &fMatrixInfoStorage; 1.211 + } else { 1.212 + fMatrixInfo = prev->fMatrixInfo; 1.213 + } 1.214 + 1.215 + if (flags & SkCanvas::kClip_SaveFlag) { 1.216 + // We don't copy the ClipOps of the previous clip states 1.217 + fClipInfo = &fClipInfoStorage; 1.218 + } else { 1.219 + fClipInfo = prev->fClipInfo; 1.220 + } 1.221 + 1.222 + // Initially a new save/saveLayer represents the same MC state 1.223 + // as its predecessor. 1.224 + fMCStateID = prev->fMCStateID; 1.225 +#ifdef SK_DEBUG 1.226 + fExpectedDepth = prev->fExpectedDepth; 1.227 +#endif 1.228 + } 1.229 + 1.230 + fIsSaveLayer = false; 1.231 + } 1.232 + 1.233 + MatrixInfo* fMatrixInfo; 1.234 + MatrixInfo fMatrixInfoStorage; 1.235 + 1.236 + ClipInfo* fClipInfo; 1.237 + ClipInfo fClipInfoStorage; 1.238 + 1.239 + // Tracks the current depth of saveLayers to support the isDrawingToLayer call 1.240 + int fLayerID; 1.241 + // Does this MC state represent a saveLayer call? 1.242 + bool fIsSaveLayer; 1.243 + 1.244 + // The next field is only valid when fIsSaveLayer is set. 1.245 + SkTDArray<int>* fSavedSkipOffsets; 1.246 + 1.247 + // Does the MC state have an open block in the skp? 1.248 + bool fHasOpen; 1.249 + 1.250 + MatrixClipState* fPrev; 1.251 + 1.252 +#ifdef SK_DEBUG 1.253 + int fExpectedDepth; // debugging aid 1.254 +#endif 1.255 + 1.256 + int32_t fMCStateID; 1.257 + }; 1.258 + 1.259 + enum CallType { 1.260 + kMatrix_CallType, 1.261 + kClip_CallType, 1.262 + kOther_CallType 1.263 + }; 1.264 + 1.265 + SkMatrixClipStateMgr(); 1.266 + ~SkMatrixClipStateMgr(); 1.267 + 1.268 + void init(SkPictureRecord* picRecord) { 1.269 + // Note: we're not taking a ref here. It is expected that the SkMatrixClipStateMgr 1.270 + // is owned by the SkPictureRecord object 1.271 + fPicRecord = picRecord; 1.272 + } 1.273 + 1.274 + SkPictureRecord* getPicRecord() { return fPicRecord; } 1.275 + 1.276 + // TODO: need to override canvas' getSaveCount. Right now we pass the 1.277 + // save* and restore calls on to the base SkCanvas in SkPictureRecord but 1.278 + // this duplicates effort. 1.279 + int getSaveCount() const { return fMatrixClipStack.count(); } 1.280 + 1.281 + int save(SkCanvas::SaveFlags flags); 1.282 + 1.283 + int saveLayer(const SkRect* bounds, const SkPaint* paint, SkCanvas::SaveFlags flags); 1.284 + 1.285 + bool isDrawingToLayer() const { 1.286 + return fCurMCState->fLayerID > 0; 1.287 + } 1.288 + 1.289 + void restore(); 1.290 + 1.291 + bool translate(SkScalar dx, SkScalar dy) { 1.292 + this->call(kMatrix_CallType); 1.293 + return fCurMCState->fMatrixInfo->preTranslate(dx, dy); 1.294 + } 1.295 + 1.296 + bool scale(SkScalar sx, SkScalar sy) { 1.297 + this->call(kMatrix_CallType); 1.298 + return fCurMCState->fMatrixInfo->preScale(sx, sy); 1.299 + } 1.300 + 1.301 + bool rotate(SkScalar degrees) { 1.302 + this->call(kMatrix_CallType); 1.303 + return fCurMCState->fMatrixInfo->preRotate(degrees); 1.304 + } 1.305 + 1.306 + bool skew(SkScalar sx, SkScalar sy) { 1.307 + this->call(kMatrix_CallType); 1.308 + return fCurMCState->fMatrixInfo->preSkew(sx, sy); 1.309 + } 1.310 + 1.311 + bool concat(const SkMatrix& matrix) { 1.312 + this->call(kMatrix_CallType); 1.313 + return fCurMCState->fMatrixInfo->preConcat(matrix); 1.314 + } 1.315 + 1.316 + void setMatrix(const SkMatrix& matrix) { 1.317 + this->call(kMatrix_CallType); 1.318 + fCurMCState->fMatrixInfo->setMatrix(matrix); 1.319 + } 1.320 + 1.321 + bool clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { 1.322 + this->call(SkMatrixClipStateMgr::kClip_CallType); 1.323 + return fCurMCState->fClipInfo->clipRect(rect, op, doAA, 1.324 + fCurMCState->fMatrixInfo->getID(this)); 1.325 + } 1.326 + 1.327 + bool clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { 1.328 + this->call(SkMatrixClipStateMgr::kClip_CallType); 1.329 + return fCurMCState->fClipInfo->clipRRect(rrect, op, doAA, 1.330 + fCurMCState->fMatrixInfo->getID(this)); 1.331 + } 1.332 + 1.333 + bool clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { 1.334 + this->call(SkMatrixClipStateMgr::kClip_CallType); 1.335 + return fCurMCState->fClipInfo->clipPath(fPicRecord, path, op, doAA, 1.336 + fCurMCState->fMatrixInfo->getID(this)); 1.337 + } 1.338 + 1.339 + bool clipRegion(const SkRegion& region, SkRegion::Op op) { 1.340 + this->call(SkMatrixClipStateMgr::kClip_CallType); 1.341 + int regionID = this->addRegionToDict(region); 1.342 + return fCurMCState->fClipInfo->clipRegion(fPicRecord, regionID, op, 1.343 + fCurMCState->fMatrixInfo->getID(this)); 1.344 + } 1.345 + 1.346 + bool call(CallType callType); 1.347 + 1.348 + void fillInSkips(SkWriter32* writer, int32_t restoreOffset); 1.349 + 1.350 + void finish(); 1.351 + 1.352 +protected: 1.353 + SkPictureRecord* fPicRecord; 1.354 + 1.355 + uint32_t fMatrixClipStackStorage[43]; // sized to fit 2 clip states 1.356 + SkDeque fMatrixClipStack; 1.357 + MatrixClipState* fCurMCState; 1.358 + 1.359 + // This dictionary doesn't actually de-duplicate the matrices (except for the 1.360 + // identity matrix). It merely stores the matrices and allows them to be looked 1.361 + // up by ID later. The de-duplication mainly falls upon the matrix/clip stack 1.362 + // which stores the ID so a revisited clip/matrix (via popping the stack) will 1.363 + // use the same ID. 1.364 + SkTDArray<SkMatrix> fMatrixDict; 1.365 + 1.366 + SkTDArray<SkRegion*> fRegionDict; 1.367 + 1.368 + // The MCStateID of the state currently in effect in the byte stream. 0 if none. 1.369 + int32_t fCurOpenStateID; 1.370 + // The skip offsets for the current open state. These are the locations in the 1.371 + // skp that must be filled in when the current open state is closed. These are 1.372 + // here rather then distributed across the MatrixClipState's because saveLayers 1.373 + // can cause MC states to be nested. 1.374 + SkTDArray<int32_t> *fSkipOffsets; 1.375 + 1.376 + SkDEBUGCODE(void validate();) 1.377 + 1.378 + int MCStackPush(SkCanvas::SaveFlags flags); 1.379 + 1.380 + void addClipOffset(int offset) { 1.381 + SkASSERT(NULL != fSkipOffsets); 1.382 + SkASSERT(kIdentityWideOpenStateID != fCurOpenStateID); 1.383 + SkASSERT(fCurMCState->fHasOpen); 1.384 + SkASSERT(!fCurMCState->fIsSaveLayer); 1.385 + 1.386 + *fSkipOffsets->append() = offset; 1.387 + } 1.388 + 1.389 + void writeDeltaMat(int currentMatID, int desiredMatID); 1.390 + static int32_t NewMCStateID(); 1.391 + 1.392 + int addRegionToDict(const SkRegion& region); 1.393 + const SkRegion* lookupRegion(int index) { 1.394 + SkASSERT(index >= 0 && index < fRegionDict.count()); 1.395 + return fRegionDict[index]; 1.396 + } 1.397 + 1.398 + // TODO: add stats to check if the dictionary really does 1.399 + // reduce the size of the SkPicture. 1.400 + int addMatToDict(const SkMatrix& mat); 1.401 + const SkMatrix& lookupMat(int index) { 1.402 + SkASSERT(index >= 0 && index < fMatrixDict.count()); 1.403 + return fMatrixDict[index]; 1.404 + } 1.405 + 1.406 + bool isNestingMCState(int stateID); 1.407 + 1.408 +#ifdef SK_DEBUG 1.409 + int fActualDepth; 1.410 +#endif 1.411 + 1.412 + // save layers are nested within a specific MC state. This stack tracks 1.413 + // the nesting MC state's ID as save layers are pushed and popped. 1.414 + SkTDArray<int> fStateIDStack; 1.415 +}; 1.416 + 1.417 +#endif