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

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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
michael@0 8 #include "SkMatrixClipStateMgr.h"
michael@0 9 #include "SkPictureRecord.h"
michael@0 10
michael@0 11 bool SkMatrixClipStateMgr::MatrixClipState::ClipInfo::clipPath(SkPictureRecord* picRecord,
michael@0 12 const SkPath& path,
michael@0 13 SkRegion::Op op,
michael@0 14 bool doAA,
michael@0 15 int matrixID) {
michael@0 16 int pathID = picRecord->addPathToHeap(path);
michael@0 17
michael@0 18 ClipOp* newClip = fClips.append();
michael@0 19 newClip->fClipType = kPath_ClipType;
michael@0 20 newClip->fGeom.fPathID = pathID;
michael@0 21 newClip->fOp = op;
michael@0 22 newClip->fDoAA = doAA;
michael@0 23 newClip->fMatrixID = matrixID;
michael@0 24 return false;
michael@0 25 }
michael@0 26
michael@0 27 bool SkMatrixClipStateMgr::MatrixClipState::ClipInfo::clipRegion(SkPictureRecord* picRecord,
michael@0 28 int regionID,
michael@0 29 SkRegion::Op op,
michael@0 30 int matrixID) {
michael@0 31 ClipOp* newClip = fClips.append();
michael@0 32 newClip->fClipType = kRegion_ClipType;
michael@0 33 newClip->fGeom.fRegionID = regionID;
michael@0 34 newClip->fOp = op;
michael@0 35 newClip->fDoAA = true; // not necessary but sanity preserving
michael@0 36 newClip->fMatrixID = matrixID;
michael@0 37 return false;
michael@0 38 }
michael@0 39
michael@0 40 void SkMatrixClipStateMgr::writeDeltaMat(int currentMatID, int desiredMatID) {
michael@0 41 const SkMatrix& current = this->lookupMat(currentMatID);
michael@0 42 const SkMatrix& desired = this->lookupMat(desiredMatID);
michael@0 43
michael@0 44 SkMatrix delta;
michael@0 45 bool result = current.invert(&delta);
michael@0 46 if (result) {
michael@0 47 delta.preConcat(desired);
michael@0 48 }
michael@0 49 fPicRecord->recordConcat(delta);
michael@0 50 }
michael@0 51
michael@0 52 // Note: this only writes out the clips for the current save state. To get the
michael@0 53 // entire clip stack requires iterating of the entire matrix/clip stack.
michael@0 54 void SkMatrixClipStateMgr::MatrixClipState::ClipInfo::writeClip(int* curMatID,
michael@0 55 SkMatrixClipStateMgr* mgr) {
michael@0 56 for (int i = 0; i < fClips.count(); ++i) {
michael@0 57 ClipOp& curClip = fClips[i];
michael@0 58
michael@0 59 // TODO: use the matrix ID to skip writing the identity matrix
michael@0 60 // over and over, i.e.:
michael@0 61 // if (*curMatID != curClip.fMatrixID) {
michael@0 62 // mgr->writeDeltaMat...
michael@0 63 // *curMatID...
michael@0 64 // }
michael@0 65 // Right now this optimization would throw off the testing harness.
michael@0 66 // TODO: right now we're writing out the delta matrix from the prior
michael@0 67 // matrix state. This is a side-effect of writing out the entire
michael@0 68 // clip stack and should be resolved when that is fixed.
michael@0 69 mgr->writeDeltaMat(*curMatID, curClip.fMatrixID);
michael@0 70 *curMatID = curClip.fMatrixID;
michael@0 71
michael@0 72 int offset = 0;
michael@0 73
michael@0 74 switch (curClip.fClipType) {
michael@0 75 case kRect_ClipType:
michael@0 76 offset = mgr->getPicRecord()->recordClipRect(curClip.fGeom.fRRect.rect(),
michael@0 77 curClip.fOp, curClip.fDoAA);
michael@0 78 break;
michael@0 79 case kRRect_ClipType:
michael@0 80 offset = mgr->getPicRecord()->recordClipRRect(curClip.fGeom.fRRect, curClip.fOp,
michael@0 81 curClip.fDoAA);
michael@0 82 break;
michael@0 83 case kPath_ClipType:
michael@0 84 offset = mgr->getPicRecord()->recordClipPath(curClip.fGeom.fPathID, curClip.fOp,
michael@0 85 curClip.fDoAA);
michael@0 86 break;
michael@0 87 case kRegion_ClipType: {
michael@0 88 const SkRegion* region = mgr->lookupRegion(curClip.fGeom.fRegionID);
michael@0 89 offset = mgr->getPicRecord()->recordClipRegion(*region, curClip.fOp);
michael@0 90 break;
michael@0 91 }
michael@0 92 default:
michael@0 93 SkASSERT(0);
michael@0 94 }
michael@0 95
michael@0 96 mgr->addClipOffset(offset);
michael@0 97 }
michael@0 98 }
michael@0 99
michael@0 100 SkMatrixClipStateMgr::SkMatrixClipStateMgr()
michael@0 101 : fPicRecord(NULL)
michael@0 102 , fMatrixClipStack(sizeof(MatrixClipState),
michael@0 103 fMatrixClipStackStorage,
michael@0 104 sizeof(fMatrixClipStackStorage))
michael@0 105 , fCurOpenStateID(kIdentityWideOpenStateID) {
michael@0 106
michael@0 107 fSkipOffsets = SkNEW(SkTDArray<int>);
michael@0 108
michael@0 109 // The first slot in the matrix dictionary is reserved for the identity matrix
michael@0 110 fMatrixDict.append()->reset();
michael@0 111
michael@0 112 fCurMCState = (MatrixClipState*)fMatrixClipStack.push_back();
michael@0 113 new (fCurMCState) MatrixClipState(NULL, 0); // balanced in restore()
michael@0 114
michael@0 115 #ifdef SK_DEBUG
michael@0 116 fActualDepth = 0;
michael@0 117 #endif
michael@0 118 }
michael@0 119
michael@0 120 SkMatrixClipStateMgr::~SkMatrixClipStateMgr() {
michael@0 121 for (int i = 0; i < fRegionDict.count(); ++i) {
michael@0 122 SkDELETE(fRegionDict[i]);
michael@0 123 }
michael@0 124
michael@0 125 SkDELETE(fSkipOffsets);
michael@0 126 }
michael@0 127
michael@0 128
michael@0 129 int SkMatrixClipStateMgr::MCStackPush(SkCanvas::SaveFlags flags) {
michael@0 130 MatrixClipState* newTop = (MatrixClipState*)fMatrixClipStack.push_back();
michael@0 131 new (newTop) MatrixClipState(fCurMCState, flags); // balanced in restore()
michael@0 132 fCurMCState = newTop;
michael@0 133
michael@0 134 SkDEBUGCODE(this->validate();)
michael@0 135
michael@0 136 return fMatrixClipStack.count();
michael@0 137 }
michael@0 138
michael@0 139 int SkMatrixClipStateMgr::save(SkCanvas::SaveFlags flags) {
michael@0 140 SkDEBUGCODE(this->validate();)
michael@0 141
michael@0 142 return this->MCStackPush(flags);
michael@0 143 }
michael@0 144
michael@0 145 int SkMatrixClipStateMgr::saveLayer(const SkRect* bounds, const SkPaint* paint,
michael@0 146 SkCanvas::SaveFlags flags) {
michael@0 147 #ifdef SK_DEBUG
michael@0 148 if (fCurMCState->fIsSaveLayer) {
michael@0 149 SkASSERT(0 == fSkipOffsets->count());
michael@0 150 }
michael@0 151 #endif
michael@0 152
michael@0 153 // Since the saveLayer call draws something we need to potentially dump
michael@0 154 // out the MC state
michael@0 155 SkDEBUGCODE(bool saved =) this->call(kOther_CallType);
michael@0 156
michael@0 157 int result = this->MCStackPush(flags);
michael@0 158 ++fCurMCState->fLayerID;
michael@0 159 fCurMCState->fIsSaveLayer = true;
michael@0 160
michael@0 161 #ifdef SK_DEBUG
michael@0 162 if (saved) {
michael@0 163 fCurMCState->fExpectedDepth++; // 1 for nesting save
michael@0 164 }
michael@0 165 fCurMCState->fExpectedDepth++; // 1 for saveLayer
michael@0 166 #endif
michael@0 167
michael@0 168 *fStateIDStack.append() = fCurOpenStateID;
michael@0 169 fCurMCState->fSavedSkipOffsets = fSkipOffsets;
michael@0 170
michael@0 171 // TODO: recycle these rather then new & deleting them on every saveLayer/
michael@0 172 // restore
michael@0 173 fSkipOffsets = SkNEW(SkTDArray<int>);
michael@0 174
michael@0 175 fPicRecord->recordSaveLayer(bounds, paint,
michael@0 176 (SkCanvas::SaveFlags)(flags| SkCanvas::kMatrixClip_SaveFlag));
michael@0 177 #ifdef SK_DEBUG
michael@0 178 fActualDepth++;
michael@0 179 #endif
michael@0 180 return result;
michael@0 181 }
michael@0 182
michael@0 183 void SkMatrixClipStateMgr::restore() {
michael@0 184 SkDEBUGCODE(this->validate();)
michael@0 185
michael@0 186 if (fCurMCState->fIsSaveLayer) {
michael@0 187 if (fCurMCState->fHasOpen) {
michael@0 188 fCurMCState->fHasOpen = false;
michael@0 189 fPicRecord->recordRestore(); // Close the open block inside the saveLayer
michael@0 190 #ifdef SK_DEBUG
michael@0 191 SkASSERT(fActualDepth > 0);
michael@0 192 fActualDepth--;
michael@0 193 #endif
michael@0 194 } else {
michael@0 195 SkASSERT(0 == fSkipOffsets->count());
michael@0 196 }
michael@0 197
michael@0 198 // The saveLayer's don't carry any matrix or clip state in the
michael@0 199 // new scheme so make sure the saveLayer's recordRestore doesn't
michael@0 200 // try to finalize them (i.e., fill in their skip offsets).
michael@0 201 fPicRecord->recordRestore(false); // close of saveLayer
michael@0 202 #ifdef SK_DEBUG
michael@0 203 SkASSERT(fActualDepth > 0);
michael@0 204 fActualDepth--;
michael@0 205 #endif
michael@0 206
michael@0 207 SkASSERT(fStateIDStack.count() >= 1);
michael@0 208 fCurOpenStateID = fStateIDStack[fStateIDStack.count()-1];
michael@0 209 fStateIDStack.pop();
michael@0 210
michael@0 211 SkASSERT(0 == fSkipOffsets->count());
michael@0 212 SkASSERT(NULL != fCurMCState->fSavedSkipOffsets);
michael@0 213
michael@0 214 SkDELETE(fSkipOffsets);
michael@0 215 fSkipOffsets = fCurMCState->fSavedSkipOffsets;
michael@0 216 }
michael@0 217
michael@0 218 bool prevHadOpen = fCurMCState->fHasOpen;
michael@0 219 bool prevWasSaveLayer = fCurMCState->fIsSaveLayer;
michael@0 220
michael@0 221 fCurMCState->~MatrixClipState(); // balanced in save()
michael@0 222 fMatrixClipStack.pop_back();
michael@0 223 fCurMCState = (MatrixClipState*)fMatrixClipStack.back();
michael@0 224
michael@0 225 if (!prevWasSaveLayer) {
michael@0 226 fCurMCState->fHasOpen = prevHadOpen;
michael@0 227 }
michael@0 228
michael@0 229 if (fCurMCState->fIsSaveLayer) {
michael@0 230 if (0 != fSkipOffsets->count()) {
michael@0 231 SkASSERT(fCurMCState->fHasOpen);
michael@0 232 }
michael@0 233 }
michael@0 234
michael@0 235 SkDEBUGCODE(this->validate();)
michael@0 236 }
michael@0 237
michael@0 238 // kIdentityWideOpenStateID (0) is reserved for the identity/wide-open clip state
michael@0 239 int32_t SkMatrixClipStateMgr::NewMCStateID() {
michael@0 240 // TODO: guard against wrap around
michael@0 241 // TODO: make uint32_t
michael@0 242 static int32_t gMCStateID = kIdentityWideOpenStateID;
michael@0 243 ++gMCStateID;
michael@0 244 return gMCStateID;
michael@0 245 }
michael@0 246
michael@0 247 bool SkMatrixClipStateMgr::isNestingMCState(int stateID) {
michael@0 248 return fStateIDStack.count() > 0 && fStateIDStack[fStateIDStack.count()-1] == fCurOpenStateID;
michael@0 249 }
michael@0 250
michael@0 251 bool SkMatrixClipStateMgr::call(CallType callType) {
michael@0 252 SkDEBUGCODE(this->validate();)
michael@0 253
michael@0 254 if (kMatrix_CallType == callType || kClip_CallType == callType) {
michael@0 255 fCurMCState->fMCStateID = NewMCStateID();
michael@0 256 SkDEBUGCODE(this->validate();)
michael@0 257 return false;
michael@0 258 }
michael@0 259
michael@0 260 SkASSERT(kOther_CallType == callType);
michael@0 261
michael@0 262 if (fCurMCState->fMCStateID == fCurOpenStateID) {
michael@0 263 // Required MC state is already active one - nothing to do
michael@0 264 SkDEBUGCODE(this->validate();)
michael@0 265 return false;
michael@0 266 }
michael@0 267
michael@0 268 if (kIdentityWideOpenStateID != fCurOpenStateID &&
michael@0 269 !this->isNestingMCState(fCurOpenStateID)) {
michael@0 270 // Don't write a restore if the open state is one in which a saveLayer
michael@0 271 // is nested. The save after the saveLayer's restore will close it.
michael@0 272 fPicRecord->recordRestore(); // Close the open block
michael@0 273 fCurMCState->fHasOpen = false;
michael@0 274 #ifdef SK_DEBUG
michael@0 275 SkASSERT(fActualDepth > 0);
michael@0 276 fActualDepth--;
michael@0 277 #endif
michael@0 278 }
michael@0 279
michael@0 280 // Install the required MC state as the active one
michael@0 281 fCurOpenStateID = fCurMCState->fMCStateID;
michael@0 282
michael@0 283 if (kIdentityWideOpenStateID == fCurOpenStateID) {
michael@0 284 SkASSERT(0 == fActualDepth);
michael@0 285 SkASSERT(!fCurMCState->fHasOpen);
michael@0 286 SkASSERT(0 == fSkipOffsets->count());
michael@0 287 return false;
michael@0 288 }
michael@0 289
michael@0 290 SkASSERT(!fCurMCState->fHasOpen);
michael@0 291 SkASSERT(0 == fSkipOffsets->count());
michael@0 292 fCurMCState->fHasOpen = true;
michael@0 293 fPicRecord->recordSave(SkCanvas::kMatrixClip_SaveFlag);
michael@0 294 #ifdef SK_DEBUG
michael@0 295 fActualDepth++;
michael@0 296 SkASSERT(fActualDepth == fCurMCState->fExpectedDepth);
michael@0 297 #endif
michael@0 298
michael@0 299 // write out clips
michael@0 300 SkDeque::Iter iter(fMatrixClipStack, SkDeque::Iter::kBack_IterStart);
michael@0 301 const MatrixClipState* state;
michael@0 302 // Loop back across the MC states until the last saveLayer. The MC
michael@0 303 // state in front of the saveLayer has already been written out.
michael@0 304 for (state = (const MatrixClipState*) iter.prev();
michael@0 305 state != NULL;
michael@0 306 state = (const MatrixClipState*) iter.prev()) {
michael@0 307 if (state->fIsSaveLayer) {
michael@0 308 break;
michael@0 309 }
michael@0 310 }
michael@0 311
michael@0 312 int curMatID;
michael@0 313
michael@0 314 if (NULL == state) {
michael@0 315 // There was no saveLayer in the MC stack so we need to output them all
michael@0 316 iter.reset(fMatrixClipStack, SkDeque::Iter::kFront_IterStart);
michael@0 317 state = (const MatrixClipState*) iter.next();
michael@0 318 curMatID = kIdentityMatID;
michael@0 319 } else {
michael@0 320 // SkDeque's iterators actually return the previous location so we
michael@0 321 // need to reverse and go forward one to get back on track.
michael@0 322 iter.next();
michael@0 323 SkDEBUGCODE(const MatrixClipState* test = (const MatrixClipState*)) iter.next();
michael@0 324 SkASSERT(test == state);
michael@0 325
michael@0 326 curMatID = state->fMatrixInfo->getID(this);
michael@0 327
michael@0 328 // TODO: this assumes that, in the case of Save|SaveLayer when the SaveLayer
michael@0 329 // doesn't save the clip, that the SaveLayer doesn't add any additional clip state.
michael@0 330 // This assumption will be removed when we explicitly store the clip state in
michael@0 331 // self-contained objects. It is valid for the small set of skps.
michael@0 332 if (NULL != state->fPrev && state->fClipInfo == state->fPrev->fClipInfo) {
michael@0 333 // By the above assumption the SaveLayer's MC state has already been
michael@0 334 // written out by the prior Save so don't output it again.
michael@0 335 state = (const MatrixClipState*) iter.next();
michael@0 336 }
michael@0 337 }
michael@0 338
michael@0 339 for ( ; state != NULL; state = (const MatrixClipState*) iter.next()) {
michael@0 340 state->fClipInfo->writeClip(&curMatID, this);
michael@0 341 }
michael@0 342
michael@0 343 // write out matrix
michael@0 344 // TODO: this test isn't quite right. It should be:
michael@0 345 // if (curMatID != fCurMCState->fMatrixInfo->getID(this)) {
michael@0 346 // but right now the testing harness always expects a matrix if
michael@0 347 // the matrices are non-I
michael@0 348 if (kIdentityMatID != fCurMCState->fMatrixInfo->getID(this)) {
michael@0 349 // TODO: writing out the delta matrix here is an artifact of the writing
michael@0 350 // out of the entire clip stack (with its matrices). Ultimately we will
michael@0 351 // write out the CTM here when the clip state is collapsed to a single path.
michael@0 352 this->writeDeltaMat(curMatID, fCurMCState->fMatrixInfo->getID(this));
michael@0 353 }
michael@0 354
michael@0 355 SkDEBUGCODE(this->validate();)
michael@0 356 return true;
michael@0 357 }
michael@0 358
michael@0 359 // Fill in the skip offsets for all the clips written in the current block
michael@0 360 void SkMatrixClipStateMgr::fillInSkips(SkWriter32* writer, int32_t restoreOffset) {
michael@0 361 for (int i = 0; i < fSkipOffsets->count(); ++i) {
michael@0 362 SkDEBUGCODE(int32_t peek = writer->readTAt<int32_t>((*fSkipOffsets)[i]);)
michael@0 363 SkASSERT(-1 == peek);
michael@0 364 writer->overwriteTAt<int32_t>((*fSkipOffsets)[i], restoreOffset);
michael@0 365 }
michael@0 366
michael@0 367 fSkipOffsets->rewind();
michael@0 368 SkASSERT(0 == fSkipOffsets->count());
michael@0 369 }
michael@0 370
michael@0 371 void SkMatrixClipStateMgr::finish() {
michael@0 372 if (kIdentityWideOpenStateID != fCurOpenStateID) {
michael@0 373 fPicRecord->recordRestore(); // Close the open block
michael@0 374 fCurMCState->fHasOpen = false;
michael@0 375 #ifdef SK_DEBUG
michael@0 376 SkASSERT(fActualDepth > 0);
michael@0 377 fActualDepth--;
michael@0 378 #endif
michael@0 379 fCurOpenStateID = kIdentityWideOpenStateID;
michael@0 380 SkASSERT(!fCurMCState->fHasOpen);
michael@0 381 }
michael@0 382 }
michael@0 383
michael@0 384 #ifdef SK_DEBUG
michael@0 385 void SkMatrixClipStateMgr::validate() {
michael@0 386 if (fCurOpenStateID == fCurMCState->fMCStateID && !this->isNestingMCState(fCurOpenStateID)) {
michael@0 387 // The current state is the active one so it should have a skip
michael@0 388 // offset for each clip
michael@0 389 SkDeque::Iter iter(fMatrixClipStack, SkDeque::Iter::kBack_IterStart);
michael@0 390 int clipCount = 0;
michael@0 391 for (const MatrixClipState* state = (const MatrixClipState*) iter.prev();
michael@0 392 state != NULL;
michael@0 393 state = (const MatrixClipState*) iter.prev()) {
michael@0 394 if (NULL == state->fPrev || state->fPrev->fClipInfo != state->fClipInfo) {
michael@0 395 clipCount += state->fClipInfo->numClips();
michael@0 396 }
michael@0 397 if (state->fIsSaveLayer) {
michael@0 398 break;
michael@0 399 }
michael@0 400 }
michael@0 401
michael@0 402 SkASSERT(fSkipOffsets->count() == clipCount);
michael@0 403 }
michael@0 404 }
michael@0 405 #endif
michael@0 406
michael@0 407 int SkMatrixClipStateMgr::addRegionToDict(const SkRegion& region) {
michael@0 408 int index = fRegionDict.count();
michael@0 409 *fRegionDict.append() = SkNEW(SkRegion(region));
michael@0 410 return index;
michael@0 411 }
michael@0 412
michael@0 413 int SkMatrixClipStateMgr::addMatToDict(const SkMatrix& mat) {
michael@0 414 if (mat.isIdentity()) {
michael@0 415 return kIdentityMatID;
michael@0 416 }
michael@0 417
michael@0 418 *fMatrixDict.append() = mat;
michael@0 419 return fMatrixDict.count()-1;
michael@0 420 }

mercurial