2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "CanvasState.h"
19 #include "utils/MathUtils.h"
22 namespace uirenderer {
25 CanvasState::CanvasState(CanvasStateClient& renderer)
31 , mSnapshot(&mFirstSnapshot) {
34 CanvasState::~CanvasState() {
35 // First call freeSnapshot on all but mFirstSnapshot
36 // to invoke all the dtors
39 // Now actually release the memory
40 while (mSnapshotPool) {
41 void* temp = mSnapshotPool;
42 mSnapshotPool = mSnapshotPool->previous;
47 void CanvasState::initializeRecordingSaveStack(int viewportWidth, int viewportHeight) {
48 if (mWidth != viewportWidth || mHeight != viewportHeight) {
49 mWidth = viewportWidth;
50 mHeight = viewportHeight;
51 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
52 mCanvas.onViewportInitialized();
56 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
57 mSnapshot->setRelativeLightCenter(Vector3());
61 void CanvasState::initializeSaveStack(
62 int viewportWidth, int viewportHeight,
63 float clipLeft, float clipTop,
64 float clipRight, float clipBottom, const Vector3& lightCenter) {
65 if (mWidth != viewportWidth || mHeight != viewportHeight) {
66 mWidth = viewportWidth;
67 mHeight = viewportHeight;
68 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
69 mCanvas.onViewportInitialized();
73 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
74 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
75 mSnapshot->fbo = mCanvas.getTargetFbo();
76 mSnapshot->setRelativeLightCenter(lightCenter);
80 Snapshot* CanvasState::allocSnapshot(Snapshot* previous, int savecount) {
83 memory = mSnapshotPool;
84 mSnapshotPool = mSnapshotPool->previous;
87 memory = malloc(sizeof(Snapshot));
89 return new (memory) Snapshot(previous, savecount);
92 void CanvasState::freeSnapshot(Snapshot* snapshot) {
93 snapshot->~Snapshot();
94 // Arbitrary number, just don't let this grown unbounded
95 if (mSnapshotPoolCount > 10) {
96 free((void*) snapshot);
98 snapshot->previous = mSnapshotPool;
99 mSnapshotPool = snapshot;
100 mSnapshotPoolCount++;
104 void CanvasState::freeAllSnapshots() {
105 while (mSnapshot != &mFirstSnapshot) {
106 Snapshot* temp = mSnapshot;
107 mSnapshot = mSnapshot->previous;
112 ///////////////////////////////////////////////////////////////////////////////
114 ///////////////////////////////////////////////////////////////////////////////
117 * Guaranteed to save without side-effects
119 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
120 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
122 int CanvasState::saveSnapshot(int flags) {
123 mSnapshot = allocSnapshot(mSnapshot, flags);
127 int CanvasState::save(int flags) {
128 return saveSnapshot(flags);
132 * Guaranteed to restore without side-effects.
134 void CanvasState::restoreSnapshot() {
135 Snapshot* toRemove = mSnapshot;
136 Snapshot* toRestore = mSnapshot->previous;
139 mSnapshot = toRestore;
141 // subclass handles restore implementation
142 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
144 freeSnapshot(toRemove);
147 void CanvasState::restore() {
148 if (mSaveCount > 1) {
153 void CanvasState::restoreToCount(int saveCount) {
154 if (saveCount < 1) saveCount = 1;
156 while (mSaveCount > saveCount) {
161 ///////////////////////////////////////////////////////////////////////////////
163 ///////////////////////////////////////////////////////////////////////////////
165 void CanvasState::getMatrix(SkMatrix* matrix) const {
166 mSnapshot->transform->copyTo(*matrix);
169 void CanvasState::translate(float dx, float dy, float dz) {
170 mSnapshot->transform->translate(dx, dy, dz);
173 void CanvasState::rotate(float degrees) {
174 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
177 void CanvasState::scale(float sx, float sy) {
178 mSnapshot->transform->scale(sx, sy, 1.0f);
181 void CanvasState::skew(float sx, float sy) {
182 mSnapshot->transform->skew(sx, sy);
185 void CanvasState::setMatrix(const SkMatrix& matrix) {
186 mSnapshot->transform->load(matrix);
189 void CanvasState::setMatrix(const Matrix4& matrix) {
190 *(mSnapshot->transform) = matrix;
193 void CanvasState::concatMatrix(const SkMatrix& matrix) {
194 mat4 transform(matrix);
195 mSnapshot->transform->multiply(transform);
198 void CanvasState::concatMatrix(const Matrix4& matrix) {
199 mSnapshot->transform->multiply(matrix);
202 ///////////////////////////////////////////////////////////////////////////////
204 ///////////////////////////////////////////////////////////////////////////////
206 bool CanvasState::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
207 mSnapshot->clip(Rect(left, top, right, bottom), op);
209 return !mSnapshot->clipIsEmpty();
212 bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
213 mSnapshot->clipPath(*path, op);
215 return !mSnapshot->clipIsEmpty();
218 bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
219 mSnapshot->clipRegionTransformed(*region, op);
221 return !mSnapshot->clipIsEmpty();
224 void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
227 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
229 bool outlineIsRounded = MathUtils::isPositive(radius);
230 if (!outlineIsRounded || currentTransform()->isSimple()) {
231 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
232 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
234 if (outlineIsRounded) {
235 setClippingRoundRect(allocator, bounds, radius, false);
239 void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
240 const Rect& rect, float radius, bool highPriority) {
241 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
244 void CanvasState::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
245 mSnapshot->setProjectionPathMask(allocator, path);
248 ///////////////////////////////////////////////////////////////////////////////
250 ///////////////////////////////////////////////////////////////////////////////
253 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
254 * the clipRect. Does not modify the scissor.
256 * @param clipRequired if not null, will be set to true if element intersects clip
257 * (and wasn't rejected)
259 * @param snapOut if set, the geometry will be treated as having an AA ramp.
260 * See Rect::snapGeometryToPixelBoundaries()
262 bool CanvasState::calculateQuickRejectForScissor(float left, float top,
263 float right, float bottom,
264 bool* clipRequired, bool* roundRectClipRequired,
265 bool snapOut) const {
266 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
270 Rect r(left, top, right, bottom);
271 currentTransform()->mapRect(r);
272 r.snapGeometryToPixelBoundaries(snapOut);
274 Rect clipRect(currentRenderTargetClip());
275 clipRect.snapToPixelBoundaries();
277 if (!clipRect.intersects(r)) return true;
279 // clip is required if geometry intersects clip rect
281 *clipRequired = !clipRect.contains(r);
284 // round rect clip is required if RR clip exists, and geometry intersects its corners
285 if (roundRectClipRequired) {
286 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
287 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
292 bool CanvasState::quickRejectConservative(float left, float top,
293 float right, float bottom) const {
294 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
298 Rect r(left, top, right, bottom);
299 currentTransform()->mapRect(r);
300 r.roundOut(); // rounded out to be conservative
302 Rect clipRect(currentRenderTargetClip());
303 clipRect.snapToPixelBoundaries();
305 if (!clipRect.intersects(r)) return true;
310 } // namespace uirenderer
311 } // namespace android