2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkMatrixClipStateMgr.h"
9 #include "SkPictureRecord.h"
11 bool SkMatrixClipStateMgr::MatrixClipState::ClipInfo::clipPath(SkPictureRecord* picRecord,
16 int pathID = picRecord->addPathToHeap(path);
18 ClipOp* newClip = fClips.append();
19 newClip->fClipType = kPath_ClipType;
20 newClip->fGeom.fPathID = pathID;
22 newClip->fDoAA = doAA;
23 newClip->fMatrixID = matrixID;
27 bool SkMatrixClipStateMgr::MatrixClipState::ClipInfo::clipRegion(SkPictureRecord* picRecord,
31 ClipOp* newClip = fClips.append();
32 newClip->fClipType = kRegion_ClipType;
33 newClip->fGeom.fRegionID = regionID;
35 newClip->fDoAA = true; // not necessary but sanity preserving
36 newClip->fMatrixID = matrixID;
40 void SkMatrixClipStateMgr::writeDeltaMat(int currentMatID, int desiredMatID) {
41 const SkMatrix& current = this->lookupMat(currentMatID);
42 const SkMatrix& desired = this->lookupMat(desiredMatID);
45 bool result = current.invert(&delta);
47 delta.preConcat(desired);
49 fPicRecord->recordConcat(delta);
52 // Note: this only writes out the clips for the current save state. To get the
53 // entire clip stack requires iterating of the entire matrix/clip stack.
54 void SkMatrixClipStateMgr::MatrixClipState::ClipInfo::writeClip(int* curMatID,
55 SkMatrixClipStateMgr* mgr) {
56 for (int i = 0; i < fClips.count(); ++i) {
57 ClipOp& curClip = fClips[i];
59 // TODO: use the matrix ID to skip writing the identity matrix
60 // over and over, i.e.:
61 // if (*curMatID != curClip.fMatrixID) {
62 // mgr->writeDeltaMat...
65 // Right now this optimization would throw off the testing harness.
66 // TODO: right now we're writing out the delta matrix from the prior
67 // matrix state. This is a side-effect of writing out the entire
68 // clip stack and should be resolved when that is fixed.
69 mgr->writeDeltaMat(*curMatID, curClip.fMatrixID);
70 *curMatID = curClip.fMatrixID;
74 switch (curClip.fClipType) {
76 offset = mgr->getPicRecord()->recordClipRect(curClip.fGeom.fRRect.rect(),
77 curClip.fOp, curClip.fDoAA);
80 offset = mgr->getPicRecord()->recordClipRRect(curClip.fGeom.fRRect, curClip.fOp,
84 offset = mgr->getPicRecord()->recordClipPath(curClip.fGeom.fPathID, curClip.fOp,
87 case kRegion_ClipType: {
88 const SkRegion* region = mgr->lookupRegion(curClip.fGeom.fRegionID);
89 offset = mgr->getPicRecord()->recordClipRegion(*region, curClip.fOp);
96 mgr->addClipOffset(offset);
100 SkMatrixClipStateMgr::SkMatrixClipStateMgr()
102 , fMatrixClipStack(sizeof(MatrixClipState),
103 fMatrixClipStackStorage,
104 sizeof(fMatrixClipStackStorage))
105 , fCurOpenStateID(kIdentityWideOpenStateID) {
107 fSkipOffsets = SkNEW(SkTDArray<int>);
109 // The first slot in the matrix dictionary is reserved for the identity matrix
110 fMatrixDict.append()->reset();
112 fCurMCState = (MatrixClipState*)fMatrixClipStack.push_back();
113 new (fCurMCState) MatrixClipState(NULL, 0); // balanced in restore()
120 SkMatrixClipStateMgr::~SkMatrixClipStateMgr() {
121 for (int i = 0; i < fRegionDict.count(); ++i) {
122 SkDELETE(fRegionDict[i]);
125 SkDELETE(fSkipOffsets);
129 int SkMatrixClipStateMgr::MCStackPush(SkCanvas::SaveFlags flags) {
130 MatrixClipState* newTop = (MatrixClipState*)fMatrixClipStack.push_back();
131 new (newTop) MatrixClipState(fCurMCState, flags); // balanced in restore()
132 fCurMCState = newTop;
134 SkDEBUGCODE(this->validate();)
136 return fMatrixClipStack.count();
139 int SkMatrixClipStateMgr::save(SkCanvas::SaveFlags flags) {
140 SkDEBUGCODE(this->validate();)
142 return this->MCStackPush(flags);
145 int SkMatrixClipStateMgr::saveLayer(const SkRect* bounds, const SkPaint* paint,
146 SkCanvas::SaveFlags flags) {
148 if (fCurMCState->fIsSaveLayer) {
149 SkASSERT(0 == fSkipOffsets->count());
153 // Since the saveLayer call draws something we need to potentially dump
155 SkDEBUGCODE(bool saved =) this->call(kOther_CallType);
157 int result = this->MCStackPush(flags);
158 ++fCurMCState->fLayerID;
159 fCurMCState->fIsSaveLayer = true;
163 fCurMCState->fExpectedDepth++; // 1 for nesting save
165 fCurMCState->fExpectedDepth++; // 1 for saveLayer
168 *fStateIDStack.append() = fCurOpenStateID;
169 fCurMCState->fSavedSkipOffsets = fSkipOffsets;
171 // TODO: recycle these rather then new & deleting them on every saveLayer/
173 fSkipOffsets = SkNEW(SkTDArray<int>);
175 fPicRecord->recordSaveLayer(bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
182 void SkMatrixClipStateMgr::restore() {
183 SkDEBUGCODE(this->validate();)
185 if (fCurMCState->fIsSaveLayer) {
186 if (fCurMCState->fHasOpen) {
187 fCurMCState->fHasOpen = false;
188 fPicRecord->recordRestore(); // Close the open block inside the saveLayer
190 SkASSERT(fActualDepth > 0);
194 SkASSERT(0 == fSkipOffsets->count());
197 // The saveLayer's don't carry any matrix or clip state in the
198 // new scheme so make sure the saveLayer's recordRestore doesn't
199 // try to finalize them (i.e., fill in their skip offsets).
200 fPicRecord->recordRestore(false); // close of saveLayer
202 SkASSERT(fActualDepth > 0);
206 SkASSERT(fStateIDStack.count() >= 1);
207 fCurOpenStateID = fStateIDStack[fStateIDStack.count()-1];
210 SkASSERT(0 == fSkipOffsets->count());
211 SkASSERT(NULL != fCurMCState->fSavedSkipOffsets);
213 SkDELETE(fSkipOffsets);
214 fSkipOffsets = fCurMCState->fSavedSkipOffsets;
217 bool prevHadOpen = fCurMCState->fHasOpen;
218 bool prevWasSaveLayer = fCurMCState->fIsSaveLayer;
220 fCurMCState->~MatrixClipState(); // balanced in save()
221 fMatrixClipStack.pop_back();
222 fCurMCState = (MatrixClipState*)fMatrixClipStack.back();
224 if (!prevWasSaveLayer) {
225 fCurMCState->fHasOpen = prevHadOpen;
228 if (fCurMCState->fIsSaveLayer) {
229 if (0 != fSkipOffsets->count()) {
230 SkASSERT(fCurMCState->fHasOpen);
234 SkDEBUGCODE(this->validate();)
237 // kIdentityWideOpenStateID (0) is reserved for the identity/wide-open clip state
238 int32_t SkMatrixClipStateMgr::NewMCStateID() {
239 // TODO: guard against wrap around
240 // TODO: make uint32_t
241 static int32_t gMCStateID = kIdentityWideOpenStateID;
246 bool SkMatrixClipStateMgr::isNestingMCState(int stateID) {
247 return fStateIDStack.count() > 0 && fStateIDStack[fStateIDStack.count()-1] == fCurOpenStateID;
250 bool SkMatrixClipStateMgr::call(CallType callType) {
251 SkDEBUGCODE(this->validate();)
253 if (kMatrix_CallType == callType || kClip_CallType == callType) {
254 fCurMCState->fMCStateID = NewMCStateID();
255 SkDEBUGCODE(this->validate();)
259 SkASSERT(kOther_CallType == callType);
261 if (fCurMCState->fMCStateID == fCurOpenStateID) {
262 // Required MC state is already active one - nothing to do
263 SkDEBUGCODE(this->validate();)
267 if (kIdentityWideOpenStateID != fCurOpenStateID &&
268 !this->isNestingMCState(fCurOpenStateID)) {
269 // Don't write a restore if the open state is one in which a saveLayer
270 // is nested. The save after the saveLayer's restore will close it.
271 fPicRecord->recordRestore(); // Close the open block
272 fCurMCState->fHasOpen = false;
274 SkASSERT(fActualDepth > 0);
279 // Install the required MC state as the active one
280 fCurOpenStateID = fCurMCState->fMCStateID;
282 if (kIdentityWideOpenStateID == fCurOpenStateID) {
283 SkASSERT(0 == fActualDepth);
284 SkASSERT(!fCurMCState->fHasOpen);
285 SkASSERT(0 == fSkipOffsets->count());
289 SkASSERT(!fCurMCState->fHasOpen);
290 SkASSERT(0 == fSkipOffsets->count());
291 fCurMCState->fHasOpen = true;
292 fPicRecord->recordSave(SkCanvas::kMatrixClip_SaveFlag);
295 SkASSERT(fActualDepth == fCurMCState->fExpectedDepth);
299 SkDeque::Iter iter(fMatrixClipStack, SkDeque::Iter::kBack_IterStart);
300 const MatrixClipState* state;
301 // Loop back across the MC states until the last saveLayer. The MC
302 // state in front of the saveLayer has already been written out.
303 for (state = (const MatrixClipState*) iter.prev();
305 state = (const MatrixClipState*) iter.prev()) {
306 if (state->fIsSaveLayer) {
314 // There was no saveLayer in the MC stack so we need to output them all
315 iter.reset(fMatrixClipStack, SkDeque::Iter::kFront_IterStart);
316 state = (const MatrixClipState*) iter.next();
317 curMatID = kIdentityMatID;
319 // SkDeque's iterators actually return the previous location so we
320 // need to reverse and go forward one to get back on track.
322 SkDEBUGCODE(const MatrixClipState* test = (const MatrixClipState*)) iter.next();
323 SkASSERT(test == state);
325 curMatID = state->fMatrixInfo->getID(this);
327 // TODO: this assumes that, in the case of Save|SaveLayer when the SaveLayer
328 // doesn't save the clip, that the SaveLayer doesn't add any additional clip state.
329 // This assumption will be removed when we explicitly store the clip state in
330 // self-contained objects. It is valid for the small set of skps.
331 if (NULL != state->fPrev && state->fClipInfo == state->fPrev->fClipInfo) {
332 // By the above assumption the SaveLayer's MC state has already been
333 // written out by the prior Save so don't output it again.
334 state = (const MatrixClipState*) iter.next();
338 for ( ; state != NULL; state = (const MatrixClipState*) iter.next()) {
339 state->fClipInfo->writeClip(&curMatID, this);
343 // TODO: this test isn't quite right. It should be:
344 // if (curMatID != fCurMCState->fMatrixInfo->getID(this)) {
345 // but right now the testing harness always expects a matrix if
346 // the matrices are non-I
347 if (kIdentityMatID != fCurMCState->fMatrixInfo->getID(this)) {
348 // TODO: writing out the delta matrix here is an artifact of the writing
349 // out of the entire clip stack (with its matrices). Ultimately we will
350 // write out the CTM here when the clip state is collapsed to a single path.
351 this->writeDeltaMat(curMatID, fCurMCState->fMatrixInfo->getID(this));
354 SkDEBUGCODE(this->validate();)
358 // Fill in the skip offsets for all the clips written in the current block
359 void SkMatrixClipStateMgr::fillInSkips(SkWriter32* writer, int32_t restoreOffset) {
360 for (int i = 0; i < fSkipOffsets->count(); ++i) {
361 SkDEBUGCODE(int32_t peek = writer->readTAt<int32_t>((*fSkipOffsets)[i]);)
362 SkASSERT(-1 == peek);
363 writer->overwriteTAt<int32_t>((*fSkipOffsets)[i], restoreOffset);
366 fSkipOffsets->rewind();
367 SkASSERT(0 == fSkipOffsets->count());
370 void SkMatrixClipStateMgr::finish() {
371 if (kIdentityWideOpenStateID != fCurOpenStateID) {
372 fPicRecord->recordRestore(); // Close the open block
373 fCurMCState->fHasOpen = false;
375 SkASSERT(fActualDepth > 0);
378 fCurOpenStateID = kIdentityWideOpenStateID;
379 SkASSERT(!fCurMCState->fHasOpen);
384 void SkMatrixClipStateMgr::validate() {
385 if (fCurOpenStateID == fCurMCState->fMCStateID && !this->isNestingMCState(fCurOpenStateID)) {
386 // The current state is the active one so it should have a skip
387 // offset for each clip
388 SkDeque::Iter iter(fMatrixClipStack, SkDeque::Iter::kBack_IterStart);
390 for (const MatrixClipState* state = (const MatrixClipState*) iter.prev();
392 state = (const MatrixClipState*) iter.prev()) {
393 if (NULL == state->fPrev || state->fPrev->fClipInfo != state->fClipInfo) {
394 clipCount += state->fClipInfo->numClips();
396 if (state->fIsSaveLayer) {
401 SkASSERT(fSkipOffsets->count() == clipCount);
406 int SkMatrixClipStateMgr::addRegionToDict(const SkRegion& region) {
407 int index = fRegionDict.count();
408 *fRegionDict.append() = SkNEW(SkRegion(region));
412 int SkMatrixClipStateMgr::addMatToDict(const SkMatrix& mat) {
413 if (mat.isIdentity()) {
414 return kIdentityMatID;
417 *fMatrixDict.append() = mat;
418 return fMatrixDict.count()-1;