blob: dbbf00d8bdec5c48970e8f5118acb8da35a13991 [file] [log] [blame]
Chris Craik14e51302013-12-30 15:32:54 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Tom Hudson984162f2014-10-10 13:38:16 -040017#include "CanvasState.h"
sergeyvdccca442016-03-21 15:38:21 -070018#include "hwui/Canvas.h"
Chris Craikaf4d04c2014-07-29 12:50:14 -070019#include "utils/MathUtils.h"
20
Chris Craik14e51302013-12-30 15:32:54 -080021namespace android {
22namespace uirenderer {
23
Tom Hudson984162f2014-10-10 13:38:16 -040024
25CanvasState::CanvasState(CanvasStateClient& renderer)
Chris Craik5e00c7c2016-07-06 16:10:09 -070026 : mWidth(-1)
Chris Craik058fc642014-07-23 18:19:28 -070027 , mHeight(-1)
28 , mSaveCount(1)
Tom Hudson984162f2014-10-10 13:38:16 -040029 , mCanvas(renderer)
John Reckd9ee5502015-10-06 10:06:37 -070030 , mSnapshot(&mFirstSnapshot) {
31}
Tom Hudson984162f2014-10-10 13:38:16 -040032
John Reckd9ee5502015-10-06 10:06:37 -070033CanvasState::~CanvasState() {
34 // First call freeSnapshot on all but mFirstSnapshot
35 // to invoke all the dtors
36 freeAllSnapshots();
37
38 // Now actually release the memory
39 while (mSnapshotPool) {
40 void* temp = mSnapshotPool;
41 mSnapshotPool = mSnapshotPool->previous;
42 free(temp);
43 }
Chris Craik14e51302013-12-30 15:32:54 -080044}
45
Chris Craike4db79d2015-12-22 16:32:23 -080046void CanvasState::initializeRecordingSaveStack(int viewportWidth, int viewportHeight) {
47 if (mWidth != viewportWidth || mHeight != viewportHeight) {
48 mWidth = viewportWidth;
49 mHeight = viewportHeight;
50 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
51 mCanvas.onViewportInitialized();
52 }
53
54 freeAllSnapshots();
Florin Malitaeecff562015-12-21 10:43:01 -050055 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
Chris Craike4db79d2015-12-22 16:32:23 -080056 mSnapshot->setRelativeLightCenter(Vector3());
57 mSaveCount = 1;
58}
59
Chris Craik64e445b2015-09-02 14:23:49 -070060void CanvasState::initializeSaveStack(
61 int viewportWidth, int viewportHeight,
62 float clipLeft, float clipTop,
Chris Craik69e5adf2014-08-14 13:34:01 -070063 float clipRight, float clipBottom, const Vector3& lightCenter) {
Chris Craik64e445b2015-09-02 14:23:49 -070064 if (mWidth != viewportWidth || mHeight != viewportHeight) {
65 mWidth = viewportWidth;
66 mHeight = viewportHeight;
John Reckd9ee5502015-10-06 10:06:37 -070067 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
Chris Craik64e445b2015-09-02 14:23:49 -070068 mCanvas.onViewportInitialized();
69 }
70
John Reckd9ee5502015-10-06 10:06:37 -070071 freeAllSnapshots();
Florin Malitaeecff562015-12-21 10:43:01 -050072 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
Chris Craik14e51302013-12-30 15:32:54 -080073 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
Chris Craik6b109c72015-02-27 10:55:28 -080074 mSnapshot->fbo = mCanvas.getTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070075 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080076 mSaveCount = 1;
77}
78
John Reckd9ee5502015-10-06 10:06:37 -070079Snapshot* CanvasState::allocSnapshot(Snapshot* previous, int savecount) {
80 void* memory;
81 if (mSnapshotPool) {
82 memory = mSnapshotPool;
83 mSnapshotPool = mSnapshotPool->previous;
84 mSnapshotPoolCount--;
85 } else {
86 memory = malloc(sizeof(Snapshot));
87 }
88 return new (memory) Snapshot(previous, savecount);
89}
90
91void CanvasState::freeSnapshot(Snapshot* snapshot) {
92 snapshot->~Snapshot();
93 // Arbitrary number, just don't let this grown unbounded
94 if (mSnapshotPoolCount > 10) {
95 free((void*) snapshot);
96 } else {
97 snapshot->previous = mSnapshotPool;
98 mSnapshotPool = snapshot;
99 mSnapshotPoolCount++;
100 }
101}
102
103void CanvasState::freeAllSnapshots() {
104 while (mSnapshot != &mFirstSnapshot) {
105 Snapshot* temp = mSnapshot;
106 mSnapshot = mSnapshot->previous;
107 freeSnapshot(temp);
108 }
109}
110
Chris Craik14e51302013-12-30 15:32:54 -0800111///////////////////////////////////////////////////////////////////////////////
112// Save (layer)
113///////////////////////////////////////////////////////////////////////////////
114
115/**
Tom Hudson984162f2014-10-10 13:38:16 -0400116 * Guaranteed to save without side-effects
Chris Craik14e51302013-12-30 15:32:54 -0800117 *
Tom Hudson984162f2014-10-10 13:38:16 -0400118 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
Chris Craik14e51302013-12-30 15:32:54 -0800119 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
120 */
Tom Hudson984162f2014-10-10 13:38:16 -0400121int CanvasState::saveSnapshot(int flags) {
John Reckd9ee5502015-10-06 10:06:37 -0700122 mSnapshot = allocSnapshot(mSnapshot, flags);
Chris Craik14e51302013-12-30 15:32:54 -0800123 return mSaveCount++;
124}
125
Tom Hudson984162f2014-10-10 13:38:16 -0400126int CanvasState::save(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -0800127 return saveSnapshot(flags);
128}
129
130/**
Tom Hudson984162f2014-10-10 13:38:16 -0400131 * Guaranteed to restore without side-effects.
Chris Craik14e51302013-12-30 15:32:54 -0800132 */
Tom Hudson984162f2014-10-10 13:38:16 -0400133void CanvasState::restoreSnapshot() {
John Reckd9ee5502015-10-06 10:06:37 -0700134 Snapshot* toRemove = mSnapshot;
135 Snapshot* toRestore = mSnapshot->previous;
Chris Craik14e51302013-12-30 15:32:54 -0800136
137 mSaveCount--;
138 mSnapshot = toRestore;
139
140 // subclass handles restore implementation
Tom Hudson984162f2014-10-10 13:38:16 -0400141 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
John Reckd9ee5502015-10-06 10:06:37 -0700142
143 freeSnapshot(toRemove);
Chris Craik14e51302013-12-30 15:32:54 -0800144}
145
Tom Hudson984162f2014-10-10 13:38:16 -0400146void CanvasState::restore() {
Chris Craik14e51302013-12-30 15:32:54 -0800147 if (mSaveCount > 1) {
148 restoreSnapshot();
149 }
150}
151
Tom Hudson984162f2014-10-10 13:38:16 -0400152void CanvasState::restoreToCount(int saveCount) {
Chris Craik14e51302013-12-30 15:32:54 -0800153 if (saveCount < 1) saveCount = 1;
154
155 while (mSaveCount > saveCount) {
156 restoreSnapshot();
157 }
158}
159
160///////////////////////////////////////////////////////////////////////////////
161// Matrix
162///////////////////////////////////////////////////////////////////////////////
163
Tom Hudson984162f2014-10-10 13:38:16 -0400164void CanvasState::getMatrix(SkMatrix* matrix) const {
Chris Craik14e51302013-12-30 15:32:54 -0800165 mSnapshot->transform->copyTo(*matrix);
166}
167
Tom Hudson984162f2014-10-10 13:38:16 -0400168void CanvasState::translate(float dx, float dy, float dz) {
Chris Craik14e51302013-12-30 15:32:54 -0800169 mSnapshot->transform->translate(dx, dy, dz);
170}
171
Tom Hudson984162f2014-10-10 13:38:16 -0400172void CanvasState::rotate(float degrees) {
Chris Craik14e51302013-12-30 15:32:54 -0800173 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
174}
175
Tom Hudson984162f2014-10-10 13:38:16 -0400176void CanvasState::scale(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800177 mSnapshot->transform->scale(sx, sy, 1.0f);
178}
179
Tom Hudson984162f2014-10-10 13:38:16 -0400180void CanvasState::skew(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800181 mSnapshot->transform->skew(sx, sy);
182}
183
Tom Hudson984162f2014-10-10 13:38:16 -0400184void CanvasState::setMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500185 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800186}
187
Tom Hudson984162f2014-10-10 13:38:16 -0400188void CanvasState::setMatrix(const Matrix4& matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -0700189 *(mSnapshot->transform) = matrix;
Chris Craik14e51302013-12-30 15:32:54 -0800190}
191
Tom Hudson984162f2014-10-10 13:38:16 -0400192void CanvasState::concatMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500193 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800194 mSnapshot->transform->multiply(transform);
195}
196
Tom Hudson984162f2014-10-10 13:38:16 -0400197void CanvasState::concatMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800198 mSnapshot->transform->multiply(matrix);
199}
200
201///////////////////////////////////////////////////////////////////////////////
202// Clip
203///////////////////////////////////////////////////////////////////////////////
204
Mike Reed6e49c9f2016-12-02 15:36:59 -0500205bool CanvasState::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Chris Craika2a70722015-12-17 12:58:24 -0800206 mSnapshot->clip(Rect(left, top, right, bottom), op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800207 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800208}
209
Mike Reed6e49c9f2016-12-02 15:36:59 -0500210bool CanvasState::clipPath(const SkPath* path, SkClipOp op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700211 mSnapshot->clipPath(*path, op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800212 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800213}
214
Mike Reed6e49c9f2016-12-02 15:36:59 -0500215bool CanvasState::clipRegion(const SkRegion* region, SkClipOp op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700216 mSnapshot->clipRegionTransformed(*region, op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800217 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800218}
Chris Craik14e51302013-12-30 15:32:54 -0800219
Tom Hudson984162f2014-10-10 13:38:16 -0400220void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700221 Rect bounds;
222 float radius;
223 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
224
Chris Craik79d26c72014-08-21 12:26:16 -0700225 bool outlineIsRounded = MathUtils::isPositive(radius);
226 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700227 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
Mike Reed6c67f1d2016-12-14 10:29:54 -0500228 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkClipOp::kIntersect);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700229 }
Chris Craik79d26c72014-08-21 12:26:16 -0700230 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700231 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700232 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700233}
234
Chris Craik14e51302013-12-30 15:32:54 -0800235///////////////////////////////////////////////////////////////////////////////
236// Quick Rejection
237///////////////////////////////////////////////////////////////////////////////
238
239/**
240 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
241 * the clipRect. Does not modify the scissor.
242 *
243 * @param clipRequired if not null, will be set to true if element intersects clip
244 * (and wasn't rejected)
245 *
246 * @param snapOut if set, the geometry will be treated as having an AA ramp.
247 * See Rect::snapGeometryToPixelBoundaries()
248 */
Tom Hudson984162f2014-10-10 13:38:16 -0400249bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700250 float right, float bottom,
251 bool* clipRequired, bool* roundRectClipRequired,
252 bool snapOut) const {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700253 if (bottom <= top || right <= left) {
Chris Craik14e51302013-12-30 15:32:54 -0800254 return true;
255 }
256
257 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800258 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800259 r.snapGeometryToPixelBoundaries(snapOut);
260
Chris Craik6fe991e52015-10-20 09:39:42 -0700261 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800262 clipRect.snapToPixelBoundaries();
263
264 if (!clipRect.intersects(r)) return true;
265
266 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700267 if (clipRequired) {
268 *clipRequired = !clipRect.contains(r);
269 }
270
271 // round rect clip is required if RR clip exists, and geometry intersects its corners
272 if (roundRectClipRequired) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800273 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700274 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
275 }
Chris Craik14e51302013-12-30 15:32:54 -0800276 return false;
277}
278
Tom Hudson984162f2014-10-10 13:38:16 -0400279bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800280 float right, float bottom) const {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700281 if (bottom <= top || right <= left) {
Chris Craik14e51302013-12-30 15:32:54 -0800282 return true;
283 }
284
285 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800286 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800287 r.roundOut(); // rounded out to be conservative
288
Chris Craik6fe991e52015-10-20 09:39:42 -0700289 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800290 clipRect.snapToPixelBoundaries();
291
292 if (!clipRect.intersects(r)) return true;
293
294 return false;
295}
296
Tom Hudson984162f2014-10-10 13:38:16 -0400297} // namespace uirenderer
298} // namespace android