blob: d18c4abde7f2e804426b177be153183652b2fc12 [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 -040024CanvasState::CanvasState(CanvasStateClient& renderer)
John Reck1bcacfd2017-11-03 10:12:19 -070025 : mWidth(-1), mHeight(-1), mSaveCount(1), mCanvas(renderer), mSnapshot(&mFirstSnapshot) {}
Tom Hudson984162f2014-10-10 13:38:16 -040026
John Reckd9ee5502015-10-06 10:06:37 -070027CanvasState::~CanvasState() {
28 // First call freeSnapshot on all but mFirstSnapshot
29 // to invoke all the dtors
30 freeAllSnapshots();
31
32 // Now actually release the memory
33 while (mSnapshotPool) {
34 void* temp = mSnapshotPool;
35 mSnapshotPool = mSnapshotPool->previous;
36 free(temp);
37 }
Chris Craik14e51302013-12-30 15:32:54 -080038}
39
Chris Craike4db79d2015-12-22 16:32:23 -080040void CanvasState::initializeRecordingSaveStack(int viewportWidth, int viewportHeight) {
41 if (mWidth != viewportWidth || mHeight != viewportHeight) {
42 mWidth = viewportWidth;
43 mHeight = viewportHeight;
44 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
45 mCanvas.onViewportInitialized();
46 }
47
48 freeAllSnapshots();
Florin Malitaeecff562015-12-21 10:43:01 -050049 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
Chris Craike4db79d2015-12-22 16:32:23 -080050 mSnapshot->setRelativeLightCenter(Vector3());
51 mSaveCount = 1;
52}
53
John Reck1bcacfd2017-11-03 10:12:19 -070054void CanvasState::initializeSaveStack(int viewportWidth, int viewportHeight, float clipLeft,
55 float clipTop, float clipRight, float clipBottom,
56 const Vector3& lightCenter) {
Chris Craik64e445b2015-09-02 14:23:49 -070057 if (mWidth != viewportWidth || mHeight != viewportHeight) {
58 mWidth = viewportWidth;
59 mHeight = viewportHeight;
John Reckd9ee5502015-10-06 10:06:37 -070060 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
Chris Craik64e445b2015-09-02 14:23:49 -070061 mCanvas.onViewportInitialized();
62 }
63
John Reckd9ee5502015-10-06 10:06:37 -070064 freeAllSnapshots();
Florin Malitaeecff562015-12-21 10:43:01 -050065 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
Chris Craik14e51302013-12-30 15:32:54 -080066 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
Chris Craik6b109c72015-02-27 10:55:28 -080067 mSnapshot->fbo = mCanvas.getTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070068 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080069 mSaveCount = 1;
70}
71
John Reckd9ee5502015-10-06 10:06:37 -070072Snapshot* CanvasState::allocSnapshot(Snapshot* previous, int savecount) {
73 void* memory;
74 if (mSnapshotPool) {
75 memory = mSnapshotPool;
76 mSnapshotPool = mSnapshotPool->previous;
77 mSnapshotPoolCount--;
78 } else {
79 memory = malloc(sizeof(Snapshot));
80 }
81 return new (memory) Snapshot(previous, savecount);
82}
83
84void CanvasState::freeSnapshot(Snapshot* snapshot) {
85 snapshot->~Snapshot();
86 // Arbitrary number, just don't let this grown unbounded
87 if (mSnapshotPoolCount > 10) {
John Reck1bcacfd2017-11-03 10:12:19 -070088 free((void*)snapshot);
John Reckd9ee5502015-10-06 10:06:37 -070089 } else {
90 snapshot->previous = mSnapshotPool;
91 mSnapshotPool = snapshot;
92 mSnapshotPoolCount++;
93 }
94}
95
96void CanvasState::freeAllSnapshots() {
97 while (mSnapshot != &mFirstSnapshot) {
98 Snapshot* temp = mSnapshot;
99 mSnapshot = mSnapshot->previous;
100 freeSnapshot(temp);
101 }
102}
103
Chris Craik14e51302013-12-30 15:32:54 -0800104///////////////////////////////////////////////////////////////////////////////
105// Save (layer)
106///////////////////////////////////////////////////////////////////////////////
107
108/**
Tom Hudson984162f2014-10-10 13:38:16 -0400109 * Guaranteed to save without side-effects
Chris Craik14e51302013-12-30 15:32:54 -0800110 *
Tom Hudson984162f2014-10-10 13:38:16 -0400111 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
Chris Craik14e51302013-12-30 15:32:54 -0800112 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
113 */
Tom Hudson984162f2014-10-10 13:38:16 -0400114int CanvasState::saveSnapshot(int flags) {
John Reckd9ee5502015-10-06 10:06:37 -0700115 mSnapshot = allocSnapshot(mSnapshot, flags);
Chris Craik14e51302013-12-30 15:32:54 -0800116 return mSaveCount++;
117}
118
Tom Hudson984162f2014-10-10 13:38:16 -0400119int CanvasState::save(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -0800120 return saveSnapshot(flags);
121}
122
123/**
Tom Hudson984162f2014-10-10 13:38:16 -0400124 * Guaranteed to restore without side-effects.
Chris Craik14e51302013-12-30 15:32:54 -0800125 */
Tom Hudson984162f2014-10-10 13:38:16 -0400126void CanvasState::restoreSnapshot() {
John Reckd9ee5502015-10-06 10:06:37 -0700127 Snapshot* toRemove = mSnapshot;
128 Snapshot* toRestore = mSnapshot->previous;
Chris Craik14e51302013-12-30 15:32:54 -0800129
130 mSaveCount--;
131 mSnapshot = toRestore;
132
133 // subclass handles restore implementation
Tom Hudson984162f2014-10-10 13:38:16 -0400134 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
John Reckd9ee5502015-10-06 10:06:37 -0700135
136 freeSnapshot(toRemove);
Chris Craik14e51302013-12-30 15:32:54 -0800137}
138
Tom Hudson984162f2014-10-10 13:38:16 -0400139void CanvasState::restore() {
Chris Craik14e51302013-12-30 15:32:54 -0800140 if (mSaveCount > 1) {
141 restoreSnapshot();
142 }
143}
144
Tom Hudson984162f2014-10-10 13:38:16 -0400145void CanvasState::restoreToCount(int saveCount) {
Chris Craik14e51302013-12-30 15:32:54 -0800146 if (saveCount < 1) saveCount = 1;
147
148 while (mSaveCount > saveCount) {
149 restoreSnapshot();
150 }
151}
152
153///////////////////////////////////////////////////////////////////////////////
154// Matrix
155///////////////////////////////////////////////////////////////////////////////
156
Tom Hudson984162f2014-10-10 13:38:16 -0400157void CanvasState::getMatrix(SkMatrix* matrix) const {
Chris Craik14e51302013-12-30 15:32:54 -0800158 mSnapshot->transform->copyTo(*matrix);
159}
160
Tom Hudson984162f2014-10-10 13:38:16 -0400161void CanvasState::translate(float dx, float dy, float dz) {
Chris Craik14e51302013-12-30 15:32:54 -0800162 mSnapshot->transform->translate(dx, dy, dz);
163}
164
Tom Hudson984162f2014-10-10 13:38:16 -0400165void CanvasState::rotate(float degrees) {
Chris Craik14e51302013-12-30 15:32:54 -0800166 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
167}
168
Tom Hudson984162f2014-10-10 13:38:16 -0400169void CanvasState::scale(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800170 mSnapshot->transform->scale(sx, sy, 1.0f);
171}
172
Tom Hudson984162f2014-10-10 13:38:16 -0400173void CanvasState::skew(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800174 mSnapshot->transform->skew(sx, sy);
175}
176
Tom Hudson984162f2014-10-10 13:38:16 -0400177void CanvasState::setMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500178 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800179}
180
Tom Hudson984162f2014-10-10 13:38:16 -0400181void CanvasState::setMatrix(const Matrix4& matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -0700182 *(mSnapshot->transform) = matrix;
Chris Craik14e51302013-12-30 15:32:54 -0800183}
184
Tom Hudson984162f2014-10-10 13:38:16 -0400185void CanvasState::concatMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500186 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800187 mSnapshot->transform->multiply(transform);
188}
189
Tom Hudson984162f2014-10-10 13:38:16 -0400190void CanvasState::concatMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800191 mSnapshot->transform->multiply(matrix);
192}
193
194///////////////////////////////////////////////////////////////////////////////
195// Clip
196///////////////////////////////////////////////////////////////////////////////
197
Mike Reed6e49c9f2016-12-02 15:36:59 -0500198bool CanvasState::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Chris Craika2a70722015-12-17 12:58:24 -0800199 mSnapshot->clip(Rect(left, top, right, bottom), op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800200 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800201}
202
Mike Reed6e49c9f2016-12-02 15:36:59 -0500203bool CanvasState::clipPath(const SkPath* path, SkClipOp op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700204 mSnapshot->clipPath(*path, op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800205 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800206}
207
Tom Hudson984162f2014-10-10 13:38:16 -0400208void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700209 Rect bounds;
210 float radius;
John Reck1bcacfd2017-11-03 10:12:19 -0700211 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
Chris Craikaf4d04c2014-07-29 12:50:14 -0700212
Chris Craik79d26c72014-08-21 12:26:16 -0700213 bool outlineIsRounded = MathUtils::isPositive(radius);
214 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700215 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
Mike Reed6c67f1d2016-12-14 10:29:54 -0500216 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkClipOp::kIntersect);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700217 }
Chris Craik79d26c72014-08-21 12:26:16 -0700218 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700219 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700220 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700221}
222
Chris Craik14e51302013-12-30 15:32:54 -0800223///////////////////////////////////////////////////////////////////////////////
224// Quick Rejection
225///////////////////////////////////////////////////////////////////////////////
226
227/**
228 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
229 * the clipRect. Does not modify the scissor.
230 *
231 * @param clipRequired if not null, will be set to true if element intersects clip
232 * (and wasn't rejected)
233 *
234 * @param snapOut if set, the geometry will be treated as having an AA ramp.
235 * See Rect::snapGeometryToPixelBoundaries()
236 */
John Reck1bcacfd2017-11-03 10:12:19 -0700237bool CanvasState::calculateQuickRejectForScissor(float left, float top, float right, float bottom,
238 bool* clipRequired, bool* roundRectClipRequired,
239 bool snapOut) const {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700240 if (bottom <= top || right <= left) {
Chris Craik14e51302013-12-30 15:32:54 -0800241 return true;
242 }
243
244 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800245 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800246 r.snapGeometryToPixelBoundaries(snapOut);
247
Chris Craik6fe991e52015-10-20 09:39:42 -0700248 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800249 clipRect.snapToPixelBoundaries();
250
251 if (!clipRect.intersects(r)) return true;
252
253 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700254 if (clipRequired) {
255 *clipRequired = !clipRect.contains(r);
256 }
257
258 // round rect clip is required if RR clip exists, and geometry intersects its corners
259 if (roundRectClipRequired) {
John Reck1bcacfd2017-11-03 10:12:19 -0700260 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr &&
261 mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700262 }
Chris Craik14e51302013-12-30 15:32:54 -0800263 return false;
264}
265
John Reck1bcacfd2017-11-03 10:12:19 -0700266bool CanvasState::quickRejectConservative(float left, float top, float right, float bottom) const {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700267 if (bottom <= top || right <= left) {
Chris Craik14e51302013-12-30 15:32:54 -0800268 return true;
269 }
270
271 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800272 currentTransform()->mapRect(r);
John Reck1bcacfd2017-11-03 10:12:19 -0700273 r.roundOut(); // rounded out to be conservative
Chris Craik14e51302013-12-30 15:32:54 -0800274
Chris Craik6fe991e52015-10-20 09:39:42 -0700275 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800276 clipRect.snapToPixelBoundaries();
277
278 if (!clipRect.intersects(r)) return true;
279
280 return false;
281}
282
John Reck1bcacfd2017-11-03 10:12:19 -0700283} // namespace uirenderer
284} // namespace android