michael@0: /* michael@0: * Copyright 2014 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkMatrixClipStateMgr.h" michael@0: #include "SkPictureRecord.h" michael@0: michael@0: bool SkMatrixClipStateMgr::MatrixClipState::ClipInfo::clipPath(SkPictureRecord* picRecord, michael@0: const SkPath& path, michael@0: SkRegion::Op op, michael@0: bool doAA, michael@0: int matrixID) { michael@0: int pathID = picRecord->addPathToHeap(path); michael@0: michael@0: ClipOp* newClip = fClips.append(); michael@0: newClip->fClipType = kPath_ClipType; michael@0: newClip->fGeom.fPathID = pathID; michael@0: newClip->fOp = op; michael@0: newClip->fDoAA = doAA; michael@0: newClip->fMatrixID = matrixID; michael@0: return false; michael@0: } michael@0: michael@0: bool SkMatrixClipStateMgr::MatrixClipState::ClipInfo::clipRegion(SkPictureRecord* picRecord, michael@0: int regionID, michael@0: SkRegion::Op op, michael@0: int matrixID) { michael@0: ClipOp* newClip = fClips.append(); michael@0: newClip->fClipType = kRegion_ClipType; michael@0: newClip->fGeom.fRegionID = regionID; michael@0: newClip->fOp = op; michael@0: newClip->fDoAA = true; // not necessary but sanity preserving michael@0: newClip->fMatrixID = matrixID; michael@0: return false; michael@0: } michael@0: michael@0: void SkMatrixClipStateMgr::writeDeltaMat(int currentMatID, int desiredMatID) { michael@0: const SkMatrix& current = this->lookupMat(currentMatID); michael@0: const SkMatrix& desired = this->lookupMat(desiredMatID); michael@0: michael@0: SkMatrix delta; michael@0: bool result = current.invert(&delta); michael@0: if (result) { michael@0: delta.preConcat(desired); michael@0: } michael@0: fPicRecord->recordConcat(delta); michael@0: } michael@0: michael@0: // Note: this only writes out the clips for the current save state. To get the michael@0: // entire clip stack requires iterating of the entire matrix/clip stack. michael@0: void SkMatrixClipStateMgr::MatrixClipState::ClipInfo::writeClip(int* curMatID, michael@0: SkMatrixClipStateMgr* mgr) { michael@0: for (int i = 0; i < fClips.count(); ++i) { michael@0: ClipOp& curClip = fClips[i]; michael@0: michael@0: // TODO: use the matrix ID to skip writing the identity matrix michael@0: // over and over, i.e.: michael@0: // if (*curMatID != curClip.fMatrixID) { michael@0: // mgr->writeDeltaMat... michael@0: // *curMatID... michael@0: // } michael@0: // Right now this optimization would throw off the testing harness. michael@0: // TODO: right now we're writing out the delta matrix from the prior michael@0: // matrix state. This is a side-effect of writing out the entire michael@0: // clip stack and should be resolved when that is fixed. michael@0: mgr->writeDeltaMat(*curMatID, curClip.fMatrixID); michael@0: *curMatID = curClip.fMatrixID; michael@0: michael@0: int offset = 0; michael@0: michael@0: switch (curClip.fClipType) { michael@0: case kRect_ClipType: michael@0: offset = mgr->getPicRecord()->recordClipRect(curClip.fGeom.fRRect.rect(), michael@0: curClip.fOp, curClip.fDoAA); michael@0: break; michael@0: case kRRect_ClipType: michael@0: offset = mgr->getPicRecord()->recordClipRRect(curClip.fGeom.fRRect, curClip.fOp, michael@0: curClip.fDoAA); michael@0: break; michael@0: case kPath_ClipType: michael@0: offset = mgr->getPicRecord()->recordClipPath(curClip.fGeom.fPathID, curClip.fOp, michael@0: curClip.fDoAA); michael@0: break; michael@0: case kRegion_ClipType: { michael@0: const SkRegion* region = mgr->lookupRegion(curClip.fGeom.fRegionID); michael@0: offset = mgr->getPicRecord()->recordClipRegion(*region, curClip.fOp); michael@0: break; michael@0: } michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: michael@0: mgr->addClipOffset(offset); michael@0: } michael@0: } michael@0: michael@0: SkMatrixClipStateMgr::SkMatrixClipStateMgr() michael@0: : fPicRecord(NULL) michael@0: , fMatrixClipStack(sizeof(MatrixClipState), michael@0: fMatrixClipStackStorage, michael@0: sizeof(fMatrixClipStackStorage)) michael@0: , fCurOpenStateID(kIdentityWideOpenStateID) { michael@0: michael@0: fSkipOffsets = SkNEW(SkTDArray); michael@0: michael@0: // The first slot in the matrix dictionary is reserved for the identity matrix michael@0: fMatrixDict.append()->reset(); michael@0: michael@0: fCurMCState = (MatrixClipState*)fMatrixClipStack.push_back(); michael@0: new (fCurMCState) MatrixClipState(NULL, 0); // balanced in restore() michael@0: michael@0: #ifdef SK_DEBUG michael@0: fActualDepth = 0; michael@0: #endif michael@0: } michael@0: michael@0: SkMatrixClipStateMgr::~SkMatrixClipStateMgr() { michael@0: for (int i = 0; i < fRegionDict.count(); ++i) { michael@0: SkDELETE(fRegionDict[i]); michael@0: } michael@0: michael@0: SkDELETE(fSkipOffsets); michael@0: } michael@0: michael@0: michael@0: int SkMatrixClipStateMgr::MCStackPush(SkCanvas::SaveFlags flags) { michael@0: MatrixClipState* newTop = (MatrixClipState*)fMatrixClipStack.push_back(); michael@0: new (newTop) MatrixClipState(fCurMCState, flags); // balanced in restore() michael@0: fCurMCState = newTop; michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: return fMatrixClipStack.count(); michael@0: } michael@0: michael@0: int SkMatrixClipStateMgr::save(SkCanvas::SaveFlags flags) { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: return this->MCStackPush(flags); michael@0: } michael@0: michael@0: int SkMatrixClipStateMgr::saveLayer(const SkRect* bounds, const SkPaint* paint, michael@0: SkCanvas::SaveFlags flags) { michael@0: #ifdef SK_DEBUG michael@0: if (fCurMCState->fIsSaveLayer) { michael@0: SkASSERT(0 == fSkipOffsets->count()); michael@0: } michael@0: #endif michael@0: michael@0: // Since the saveLayer call draws something we need to potentially dump michael@0: // out the MC state michael@0: SkDEBUGCODE(bool saved =) this->call(kOther_CallType); michael@0: michael@0: int result = this->MCStackPush(flags); michael@0: ++fCurMCState->fLayerID; michael@0: fCurMCState->fIsSaveLayer = true; michael@0: michael@0: #ifdef SK_DEBUG michael@0: if (saved) { michael@0: fCurMCState->fExpectedDepth++; // 1 for nesting save michael@0: } michael@0: fCurMCState->fExpectedDepth++; // 1 for saveLayer michael@0: #endif michael@0: michael@0: *fStateIDStack.append() = fCurOpenStateID; michael@0: fCurMCState->fSavedSkipOffsets = fSkipOffsets; michael@0: michael@0: // TODO: recycle these rather then new & deleting them on every saveLayer/ michael@0: // restore michael@0: fSkipOffsets = SkNEW(SkTDArray); michael@0: michael@0: fPicRecord->recordSaveLayer(bounds, paint, michael@0: (SkCanvas::SaveFlags)(flags| SkCanvas::kMatrixClip_SaveFlag)); michael@0: #ifdef SK_DEBUG michael@0: fActualDepth++; michael@0: #endif michael@0: return result; michael@0: } michael@0: michael@0: void SkMatrixClipStateMgr::restore() { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (fCurMCState->fIsSaveLayer) { michael@0: if (fCurMCState->fHasOpen) { michael@0: fCurMCState->fHasOpen = false; michael@0: fPicRecord->recordRestore(); // Close the open block inside the saveLayer michael@0: #ifdef SK_DEBUG michael@0: SkASSERT(fActualDepth > 0); michael@0: fActualDepth--; michael@0: #endif michael@0: } else { michael@0: SkASSERT(0 == fSkipOffsets->count()); michael@0: } michael@0: michael@0: // The saveLayer's don't carry any matrix or clip state in the michael@0: // new scheme so make sure the saveLayer's recordRestore doesn't michael@0: // try to finalize them (i.e., fill in their skip offsets). michael@0: fPicRecord->recordRestore(false); // close of saveLayer michael@0: #ifdef SK_DEBUG michael@0: SkASSERT(fActualDepth > 0); michael@0: fActualDepth--; michael@0: #endif michael@0: michael@0: SkASSERT(fStateIDStack.count() >= 1); michael@0: fCurOpenStateID = fStateIDStack[fStateIDStack.count()-1]; michael@0: fStateIDStack.pop(); michael@0: michael@0: SkASSERT(0 == fSkipOffsets->count()); michael@0: SkASSERT(NULL != fCurMCState->fSavedSkipOffsets); michael@0: michael@0: SkDELETE(fSkipOffsets); michael@0: fSkipOffsets = fCurMCState->fSavedSkipOffsets; michael@0: } michael@0: michael@0: bool prevHadOpen = fCurMCState->fHasOpen; michael@0: bool prevWasSaveLayer = fCurMCState->fIsSaveLayer; michael@0: michael@0: fCurMCState->~MatrixClipState(); // balanced in save() michael@0: fMatrixClipStack.pop_back(); michael@0: fCurMCState = (MatrixClipState*)fMatrixClipStack.back(); michael@0: michael@0: if (!prevWasSaveLayer) { michael@0: fCurMCState->fHasOpen = prevHadOpen; michael@0: } michael@0: michael@0: if (fCurMCState->fIsSaveLayer) { michael@0: if (0 != fSkipOffsets->count()) { michael@0: SkASSERT(fCurMCState->fHasOpen); michael@0: } michael@0: } michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: // kIdentityWideOpenStateID (0) is reserved for the identity/wide-open clip state michael@0: int32_t SkMatrixClipStateMgr::NewMCStateID() { michael@0: // TODO: guard against wrap around michael@0: // TODO: make uint32_t michael@0: static int32_t gMCStateID = kIdentityWideOpenStateID; michael@0: ++gMCStateID; michael@0: return gMCStateID; michael@0: } michael@0: michael@0: bool SkMatrixClipStateMgr::isNestingMCState(int stateID) { michael@0: return fStateIDStack.count() > 0 && fStateIDStack[fStateIDStack.count()-1] == fCurOpenStateID; michael@0: } michael@0: michael@0: bool SkMatrixClipStateMgr::call(CallType callType) { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (kMatrix_CallType == callType || kClip_CallType == callType) { michael@0: fCurMCState->fMCStateID = NewMCStateID(); michael@0: SkDEBUGCODE(this->validate();) michael@0: return false; michael@0: } michael@0: michael@0: SkASSERT(kOther_CallType == callType); michael@0: michael@0: if (fCurMCState->fMCStateID == fCurOpenStateID) { michael@0: // Required MC state is already active one - nothing to do michael@0: SkDEBUGCODE(this->validate();) michael@0: return false; michael@0: } michael@0: michael@0: if (kIdentityWideOpenStateID != fCurOpenStateID && michael@0: !this->isNestingMCState(fCurOpenStateID)) { michael@0: // Don't write a restore if the open state is one in which a saveLayer michael@0: // is nested. The save after the saveLayer's restore will close it. michael@0: fPicRecord->recordRestore(); // Close the open block michael@0: fCurMCState->fHasOpen = false; michael@0: #ifdef SK_DEBUG michael@0: SkASSERT(fActualDepth > 0); michael@0: fActualDepth--; michael@0: #endif michael@0: } michael@0: michael@0: // Install the required MC state as the active one michael@0: fCurOpenStateID = fCurMCState->fMCStateID; michael@0: michael@0: if (kIdentityWideOpenStateID == fCurOpenStateID) { michael@0: SkASSERT(0 == fActualDepth); michael@0: SkASSERT(!fCurMCState->fHasOpen); michael@0: SkASSERT(0 == fSkipOffsets->count()); michael@0: return false; michael@0: } michael@0: michael@0: SkASSERT(!fCurMCState->fHasOpen); michael@0: SkASSERT(0 == fSkipOffsets->count()); michael@0: fCurMCState->fHasOpen = true; michael@0: fPicRecord->recordSave(SkCanvas::kMatrixClip_SaveFlag); michael@0: #ifdef SK_DEBUG michael@0: fActualDepth++; michael@0: SkASSERT(fActualDepth == fCurMCState->fExpectedDepth); michael@0: #endif michael@0: michael@0: // write out clips michael@0: SkDeque::Iter iter(fMatrixClipStack, SkDeque::Iter::kBack_IterStart); michael@0: const MatrixClipState* state; michael@0: // Loop back across the MC states until the last saveLayer. The MC michael@0: // state in front of the saveLayer has already been written out. michael@0: for (state = (const MatrixClipState*) iter.prev(); michael@0: state != NULL; michael@0: state = (const MatrixClipState*) iter.prev()) { michael@0: if (state->fIsSaveLayer) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: int curMatID; michael@0: michael@0: if (NULL == state) { michael@0: // There was no saveLayer in the MC stack so we need to output them all michael@0: iter.reset(fMatrixClipStack, SkDeque::Iter::kFront_IterStart); michael@0: state = (const MatrixClipState*) iter.next(); michael@0: curMatID = kIdentityMatID; michael@0: } else { michael@0: // SkDeque's iterators actually return the previous location so we michael@0: // need to reverse and go forward one to get back on track. michael@0: iter.next(); michael@0: SkDEBUGCODE(const MatrixClipState* test = (const MatrixClipState*)) iter.next(); michael@0: SkASSERT(test == state); michael@0: michael@0: curMatID = state->fMatrixInfo->getID(this); michael@0: michael@0: // TODO: this assumes that, in the case of Save|SaveLayer when the SaveLayer michael@0: // doesn't save the clip, that the SaveLayer doesn't add any additional clip state. michael@0: // This assumption will be removed when we explicitly store the clip state in michael@0: // self-contained objects. It is valid for the small set of skps. michael@0: if (NULL != state->fPrev && state->fClipInfo == state->fPrev->fClipInfo) { michael@0: // By the above assumption the SaveLayer's MC state has already been michael@0: // written out by the prior Save so don't output it again. michael@0: state = (const MatrixClipState*) iter.next(); michael@0: } michael@0: } michael@0: michael@0: for ( ; state != NULL; state = (const MatrixClipState*) iter.next()) { michael@0: state->fClipInfo->writeClip(&curMatID, this); michael@0: } michael@0: michael@0: // write out matrix michael@0: // TODO: this test isn't quite right. It should be: michael@0: // if (curMatID != fCurMCState->fMatrixInfo->getID(this)) { michael@0: // but right now the testing harness always expects a matrix if michael@0: // the matrices are non-I michael@0: if (kIdentityMatID != fCurMCState->fMatrixInfo->getID(this)) { michael@0: // TODO: writing out the delta matrix here is an artifact of the writing michael@0: // out of the entire clip stack (with its matrices). Ultimately we will michael@0: // write out the CTM here when the clip state is collapsed to a single path. michael@0: this->writeDeltaMat(curMatID, fCurMCState->fMatrixInfo->getID(this)); michael@0: } michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: return true; michael@0: } michael@0: michael@0: // Fill in the skip offsets for all the clips written in the current block michael@0: void SkMatrixClipStateMgr::fillInSkips(SkWriter32* writer, int32_t restoreOffset) { michael@0: for (int i = 0; i < fSkipOffsets->count(); ++i) { michael@0: SkDEBUGCODE(int32_t peek = writer->readTAt((*fSkipOffsets)[i]);) michael@0: SkASSERT(-1 == peek); michael@0: writer->overwriteTAt((*fSkipOffsets)[i], restoreOffset); michael@0: } michael@0: michael@0: fSkipOffsets->rewind(); michael@0: SkASSERT(0 == fSkipOffsets->count()); michael@0: } michael@0: michael@0: void SkMatrixClipStateMgr::finish() { michael@0: if (kIdentityWideOpenStateID != fCurOpenStateID) { michael@0: fPicRecord->recordRestore(); // Close the open block michael@0: fCurMCState->fHasOpen = false; michael@0: #ifdef SK_DEBUG michael@0: SkASSERT(fActualDepth > 0); michael@0: fActualDepth--; michael@0: #endif michael@0: fCurOpenStateID = kIdentityWideOpenStateID; michael@0: SkASSERT(!fCurMCState->fHasOpen); michael@0: } michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkMatrixClipStateMgr::validate() { michael@0: if (fCurOpenStateID == fCurMCState->fMCStateID && !this->isNestingMCState(fCurOpenStateID)) { michael@0: // The current state is the active one so it should have a skip michael@0: // offset for each clip michael@0: SkDeque::Iter iter(fMatrixClipStack, SkDeque::Iter::kBack_IterStart); michael@0: int clipCount = 0; michael@0: for (const MatrixClipState* state = (const MatrixClipState*) iter.prev(); michael@0: state != NULL; michael@0: state = (const MatrixClipState*) iter.prev()) { michael@0: if (NULL == state->fPrev || state->fPrev->fClipInfo != state->fClipInfo) { michael@0: clipCount += state->fClipInfo->numClips(); michael@0: } michael@0: if (state->fIsSaveLayer) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: SkASSERT(fSkipOffsets->count() == clipCount); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: int SkMatrixClipStateMgr::addRegionToDict(const SkRegion& region) { michael@0: int index = fRegionDict.count(); michael@0: *fRegionDict.append() = SkNEW(SkRegion(region)); michael@0: return index; michael@0: } michael@0: michael@0: int SkMatrixClipStateMgr::addMatToDict(const SkMatrix& mat) { michael@0: if (mat.isIdentity()) { michael@0: return kIdentityMatID; michael@0: } michael@0: michael@0: *fMatrixDict.append() = mat; michael@0: return fMatrixDict.count()-1; michael@0: }