3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkPictureStateTree.h"
12 SkPictureStateTree::SkPictureStateTree()
14 , fLastRestoredNode(NULL)
15 , fStateStack(sizeof(Draw), 16) {
18 fRoot.fMatrix = &fRootMatrix;
19 fRoot.fFlags = Node::kSave_Flag;
22 fCurrentState.fNode = &fRoot;
23 fCurrentState.fMatrix = &fRootMatrix;
24 *static_cast<Draw*>(fStateStack.push_back()) = fCurrentState;
27 SkPictureStateTree::~SkPictureStateTree() {
30 SkPictureStateTree::Draw* SkPictureStateTree::appendDraw(size_t offset) {
31 Draw* draw = static_cast<Draw*>(fAlloc.allocThrow(sizeof(Draw)));
32 *draw = fCurrentState;
33 draw->fOffset = SkToU32(offset);
37 void SkPictureStateTree::appendSave() {
38 *static_cast<Draw*>(fStateStack.push_back()) = fCurrentState;
39 fCurrentState.fNode->fFlags |= Node::kSave_Flag;
42 void SkPictureStateTree::appendSaveLayer(size_t offset) {
43 *static_cast<Draw*>(fStateStack.push_back()) = fCurrentState;
44 this->appendNode(offset);
45 fCurrentState.fNode->fFlags |= Node::kSaveLayer_Flag;
48 void SkPictureStateTree::saveCollapsed() {
49 SkASSERT(NULL != fLastRestoredNode);
50 SkASSERT(SkToBool(fLastRestoredNode->fFlags & \
51 (Node::kSaveLayer_Flag | Node::kSave_Flag)));
52 SkASSERT(fLastRestoredNode->fParent == fCurrentState.fNode);
53 // The structure of the tree is not modified here. We just turn off
54 // the save or saveLayer flag to prevent the iterator from making state
55 // changing calls on the playback canvas when traversing a save or
56 // saveLayerNode node.
57 fLastRestoredNode->fFlags = 0;
60 void SkPictureStateTree::appendRestore() {
61 fLastRestoredNode = fCurrentState.fNode;
62 fCurrentState = *static_cast<Draw*>(fStateStack.back());
63 fStateStack.pop_back();
66 void SkPictureStateTree::appendTransform(const SkMatrix& trans) {
67 SkMatrix* m = static_cast<SkMatrix*>(fAlloc.allocThrow(sizeof(SkMatrix)));
69 fCurrentState.fMatrix = m;
72 void SkPictureStateTree::appendClip(size_t offset) {
73 this->appendNode(offset);
76 SkPictureStateTree::Iterator SkPictureStateTree::getIterator(const SkTDArray<void*>& draws,
78 return Iterator(draws, canvas, &fRoot);
81 void SkPictureStateTree::appendNode(size_t offset) {
82 Node* n = static_cast<Node*>(fAlloc.allocThrow(sizeof(Node)));
83 n->fOffset = SkToU32(offset);
85 n->fParent = fCurrentState.fNode;
86 n->fLevel = fCurrentState.fNode->fLevel + 1;
87 n->fMatrix = fCurrentState.fMatrix;
88 fCurrentState.fNode = n;
91 SkPictureStateTree::Iterator::Iterator(const SkTDArray<void*>& draws, SkCanvas* canvas, Node* root)
95 , fPlaybackMatrix(canvas->getTotalMatrix())
96 , fCurrentMatrix(NULL)
102 void SkPictureStateTree::Iterator::setCurrentMatrix(const SkMatrix* matrix) {
103 SkASSERT(NULL != matrix);
105 if (matrix == fCurrentMatrix) {
109 // The matrix is in recording space, but we also inherit
110 // a playback matrix from out target canvas.
111 SkMatrix m = *matrix;
112 m.postConcat(fPlaybackMatrix);
113 fCanvas->setMatrix(m);
114 fCurrentMatrix = matrix;
117 uint32_t SkPictureStateTree::Iterator::finish() {
118 if (fCurrentNode->fFlags & Node::kSaveLayer_Flag) {
122 for (fCurrentNode = fCurrentNode->fParent; fCurrentNode;
123 fCurrentNode = fCurrentNode->fParent) {
124 // Note: we call restore() twice when both flags are set.
125 if (fCurrentNode->fFlags & Node::kSave_Flag) {
128 if (fCurrentNode->fFlags & Node::kSaveLayer_Flag) {
133 fCanvas->setMatrix(fPlaybackMatrix);
134 fCurrentMatrix = NULL;
135 return kDrawComplete;
138 uint32_t SkPictureStateTree::Iterator::nextDraw() {
139 SkASSERT(this->isValid());
140 if (fPlaybackIndex >= fDraws->count()) {
141 return this->finish();
144 Draw* draw = static_cast<Draw*>((*fDraws)[fPlaybackIndex]);
145 Node* targetNode = draw->fNode;
152 if (fCurrentNode != targetNode) {
153 // If we're not at the target and we don't have a list of nodes to get there, we need to
154 // figure out the path from our current node, to the target
155 if (fNodes.count() == 0) {
156 // Trace back up to a common ancestor, restoring to get our current state to match that
157 // of the ancestor, and saving a list of nodes whose state we need to apply to get to
158 // the target (we can restore up to the ancestor immediately, but we'll need to return
159 // an offset for each node on the way down to the target, to apply the desired clips and
160 // saveLayers, so it may take several draw() calls before the next draw actually occurs)
161 Node* tmp = fCurrentNode;
162 Node* ancestor = targetNode;
163 while (tmp != ancestor) {
164 uint16_t currentLevel = tmp->fLevel;
165 uint16_t targetLevel = ancestor->fLevel;
166 if (currentLevel >= targetLevel) {
167 if (tmp != fCurrentNode && tmp->fFlags & Node::kSave_Flag) {
169 // restore() may change the matrix, so we need to reapply.
170 fCurrentMatrix = NULL;
172 if (tmp->fFlags & Node::kSaveLayer_Flag) {
174 // restore() may change the matrix, so we need to reapply.
175 fCurrentMatrix = NULL;
179 if (currentLevel <= targetLevel) {
180 fNodes.push(ancestor);
181 ancestor = ancestor->fParent;
185 if (ancestor->fFlags & Node::kSave_Flag) {
186 if (fCurrentNode != ancestor) {
188 // restore() may change the matrix, so we need to reapply.
189 fCurrentMatrix = NULL;
191 if (targetNode != ancestor) {
195 fCurrentNode = ancestor;
198 // If we're not at the target node yet, we'll need to return an offset to make the caller
199 // apply the next clip or saveLayer.
200 if (fCurrentNode != targetNode) {
201 uint32_t offset = fNodes.top()->fOffset;
202 fCurrentNode = fNodes.top();
203 fSave = fCurrentNode != targetNode && fCurrentNode->fFlags & Node::kSave_Flag;
205 this->setCurrentMatrix(fCurrentNode->fMatrix);
210 // If we got this far, the clip/saveLayer state is all set, so we can proceed to set the matrix
211 // for the draw, and return its offset.
212 this->setCurrentMatrix(draw->fMatrix);
215 return draw->fOffset;