gfx/skia/trunk/src/core/SkMatrixClipStateMgr.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial