blob: e2149d1e4a6917d74ff1c4634e5b1aa4e7896f97 [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 Craik058fc642014-07-23 18:19:28 -070026 : mDirtyClip(false)
27 , mWidth(-1)
28 , mHeight(-1)
29 , mSaveCount(1)
Tom Hudson984162f2014-10-10 13:38:16 -040030 , mCanvas(renderer)
John Reckd9ee5502015-10-06 10:06:37 -070031 , mSnapshot(&mFirstSnapshot) {
32}
Tom Hudson984162f2014-10-10 13:38:16 -040033
John Reckd9ee5502015-10-06 10:06:37 -070034CanvasState::~CanvasState() {
35 // First call freeSnapshot on all but mFirstSnapshot
36 // to invoke all the dtors
37 freeAllSnapshots();
38
39 // Now actually release the memory
40 while (mSnapshotPool) {
41 void* temp = mSnapshotPool;
42 mSnapshotPool = mSnapshotPool->previous;
43 free(temp);
44 }
Chris Craik14e51302013-12-30 15:32:54 -080045}
46
Chris Craike4db79d2015-12-22 16:32:23 -080047void 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();
53 }
54
55 freeAllSnapshots();
Florin Malitaeecff562015-12-21 10:43:01 -050056 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
Chris Craike4db79d2015-12-22 16:32:23 -080057 mSnapshot->setRelativeLightCenter(Vector3());
58 mSaveCount = 1;
59}
60
Chris Craik64e445b2015-09-02 14:23:49 -070061void CanvasState::initializeSaveStack(
62 int viewportWidth, int viewportHeight,
63 float clipLeft, float clipTop,
Chris Craik69e5adf2014-08-14 13:34:01 -070064 float clipRight, float clipBottom, const Vector3& lightCenter) {
Chris Craik64e445b2015-09-02 14:23:49 -070065 if (mWidth != viewportWidth || mHeight != viewportHeight) {
66 mWidth = viewportWidth;
67 mHeight = viewportHeight;
John Reckd9ee5502015-10-06 10:06:37 -070068 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
Chris Craik64e445b2015-09-02 14:23:49 -070069 mCanvas.onViewportInitialized();
70 }
71
John Reckd9ee5502015-10-06 10:06:37 -070072 freeAllSnapshots();
Florin Malitaeecff562015-12-21 10:43:01 -050073 mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip);
Chris Craik14e51302013-12-30 15:32:54 -080074 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
Chris Craik6b109c72015-02-27 10:55:28 -080075 mSnapshot->fbo = mCanvas.getTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070076 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080077 mSaveCount = 1;
78}
79
John Reckd9ee5502015-10-06 10:06:37 -070080Snapshot* CanvasState::allocSnapshot(Snapshot* previous, int savecount) {
81 void* memory;
82 if (mSnapshotPool) {
83 memory = mSnapshotPool;
84 mSnapshotPool = mSnapshotPool->previous;
85 mSnapshotPoolCount--;
86 } else {
87 memory = malloc(sizeof(Snapshot));
88 }
89 return new (memory) Snapshot(previous, savecount);
90}
91
92void 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);
97 } else {
98 snapshot->previous = mSnapshotPool;
99 mSnapshotPool = snapshot;
100 mSnapshotPoolCount++;
101 }
102}
103
104void CanvasState::freeAllSnapshots() {
105 while (mSnapshot != &mFirstSnapshot) {
106 Snapshot* temp = mSnapshot;
107 mSnapshot = mSnapshot->previous;
108 freeSnapshot(temp);
109 }
110}
111
Chris Craik14e51302013-12-30 15:32:54 -0800112///////////////////////////////////////////////////////////////////////////////
113// Save (layer)
114///////////////////////////////////////////////////////////////////////////////
115
116/**
Tom Hudson984162f2014-10-10 13:38:16 -0400117 * Guaranteed to save without side-effects
Chris Craik14e51302013-12-30 15:32:54 -0800118 *
Tom Hudson984162f2014-10-10 13:38:16 -0400119 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
Chris Craik14e51302013-12-30 15:32:54 -0800120 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
121 */
Tom Hudson984162f2014-10-10 13:38:16 -0400122int CanvasState::saveSnapshot(int flags) {
John Reckd9ee5502015-10-06 10:06:37 -0700123 mSnapshot = allocSnapshot(mSnapshot, flags);
Chris Craik14e51302013-12-30 15:32:54 -0800124 return mSaveCount++;
125}
126
Tom Hudson984162f2014-10-10 13:38:16 -0400127int CanvasState::save(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -0800128 return saveSnapshot(flags);
129}
130
131/**
Tom Hudson984162f2014-10-10 13:38:16 -0400132 * Guaranteed to restore without side-effects.
Chris Craik14e51302013-12-30 15:32:54 -0800133 */
Tom Hudson984162f2014-10-10 13:38:16 -0400134void CanvasState::restoreSnapshot() {
John Reckd9ee5502015-10-06 10:06:37 -0700135 Snapshot* toRemove = mSnapshot;
136 Snapshot* toRestore = mSnapshot->previous;
Chris Craik14e51302013-12-30 15:32:54 -0800137
138 mSaveCount--;
139 mSnapshot = toRestore;
140
141 // subclass handles restore implementation
Tom Hudson984162f2014-10-10 13:38:16 -0400142 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
John Reckd9ee5502015-10-06 10:06:37 -0700143
144 freeSnapshot(toRemove);
Chris Craik14e51302013-12-30 15:32:54 -0800145}
146
Tom Hudson984162f2014-10-10 13:38:16 -0400147void CanvasState::restore() {
Chris Craik14e51302013-12-30 15:32:54 -0800148 if (mSaveCount > 1) {
149 restoreSnapshot();
150 }
151}
152
Tom Hudson984162f2014-10-10 13:38:16 -0400153void CanvasState::restoreToCount(int saveCount) {
Chris Craik14e51302013-12-30 15:32:54 -0800154 if (saveCount < 1) saveCount = 1;
155
156 while (mSaveCount > saveCount) {
157 restoreSnapshot();
158 }
159}
160
161///////////////////////////////////////////////////////////////////////////////
162// Matrix
163///////////////////////////////////////////////////////////////////////////////
164
Tom Hudson984162f2014-10-10 13:38:16 -0400165void CanvasState::getMatrix(SkMatrix* matrix) const {
Chris Craik14e51302013-12-30 15:32:54 -0800166 mSnapshot->transform->copyTo(*matrix);
167}
168
Tom Hudson984162f2014-10-10 13:38:16 -0400169void CanvasState::translate(float dx, float dy, float dz) {
Chris Craik14e51302013-12-30 15:32:54 -0800170 mSnapshot->transform->translate(dx, dy, dz);
171}
172
Tom Hudson984162f2014-10-10 13:38:16 -0400173void CanvasState::rotate(float degrees) {
Chris Craik14e51302013-12-30 15:32:54 -0800174 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
175}
176
Tom Hudson984162f2014-10-10 13:38:16 -0400177void CanvasState::scale(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800178 mSnapshot->transform->scale(sx, sy, 1.0f);
179}
180
Tom Hudson984162f2014-10-10 13:38:16 -0400181void CanvasState::skew(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800182 mSnapshot->transform->skew(sx, sy);
183}
184
Tom Hudson984162f2014-10-10 13:38:16 -0400185void CanvasState::setMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500186 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800187}
188
Tom Hudson984162f2014-10-10 13:38:16 -0400189void CanvasState::setMatrix(const Matrix4& matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -0700190 *(mSnapshot->transform) = matrix;
Chris Craik14e51302013-12-30 15:32:54 -0800191}
192
Tom Hudson984162f2014-10-10 13:38:16 -0400193void CanvasState::concatMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500194 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800195 mSnapshot->transform->multiply(transform);
196}
197
Tom Hudson984162f2014-10-10 13:38:16 -0400198void CanvasState::concatMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800199 mSnapshot->transform->multiply(matrix);
200}
201
202///////////////////////////////////////////////////////////////////////////////
203// Clip
204///////////////////////////////////////////////////////////////////////////////
205
Tom Hudson984162f2014-10-10 13:38:16 -0400206bool CanvasState::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Chris Craika2a70722015-12-17 12:58:24 -0800207 mSnapshot->clip(Rect(left, top, right, bottom), op);
Chris Craik4d3e7042015-08-20 12:54:25 -0700208 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800209 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800210}
211
Tom Hudson984162f2014-10-10 13:38:16 -0400212bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700213 mSnapshot->clipPath(*path, op);
214 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800215 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800216}
217
Tom Hudson984162f2014-10-10 13:38:16 -0400218bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700219 mSnapshot->clipRegionTransformed(*region, op);
220 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800221 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800222}
Chris Craik14e51302013-12-30 15:32:54 -0800223
Tom Hudson984162f2014-10-10 13:38:16 -0400224void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700225 Rect bounds;
226 float radius;
227 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
228
Chris Craik79d26c72014-08-21 12:26:16 -0700229 bool outlineIsRounded = MathUtils::isPositive(radius);
230 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700231 // 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);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700233 }
Chris Craik79d26c72014-08-21 12:26:16 -0700234 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700235 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700236 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700237}
238
Tom Hudson984162f2014-10-10 13:38:16 -0400239void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
Chris Craike83cbd42014-09-03 17:52:24 -0700240 const Rect& rect, float radius, bool highPriority) {
241 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700242}
243
Chris Craikfca52b752015-04-28 11:45:59 -0700244void CanvasState::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
245 mSnapshot->setProjectionPathMask(allocator, path);
246}
Chris Craikaf4d04c2014-07-29 12:50:14 -0700247
Chris Craik14e51302013-12-30 15:32:54 -0800248///////////////////////////////////////////////////////////////////////////////
249// Quick Rejection
250///////////////////////////////////////////////////////////////////////////////
251
252/**
253 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
254 * the clipRect. Does not modify the scissor.
255 *
256 * @param clipRequired if not null, will be set to true if element intersects clip
257 * (and wasn't rejected)
258 *
259 * @param snapOut if set, the geometry will be treated as having an AA ramp.
260 * See Rect::snapGeometryToPixelBoundaries()
261 */
Tom Hudson984162f2014-10-10 13:38:16 -0400262bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700263 float right, float bottom,
264 bool* clipRequired, bool* roundRectClipRequired,
265 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800266 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
267 return true;
268 }
269
270 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800271 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800272 r.snapGeometryToPixelBoundaries(snapOut);
273
Chris Craik6fe991e52015-10-20 09:39:42 -0700274 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800275 clipRect.snapToPixelBoundaries();
276
277 if (!clipRect.intersects(r)) return true;
278
279 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700280 if (clipRequired) {
281 *clipRequired = !clipRect.contains(r);
282 }
283
284 // round rect clip is required if RR clip exists, and geometry intersects its corners
285 if (roundRectClipRequired) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800286 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700287 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
288 }
Chris Craik14e51302013-12-30 15:32:54 -0800289 return false;
290}
291
Tom Hudson984162f2014-10-10 13:38:16 -0400292bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800293 float right, float bottom) const {
294 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
295 return true;
296 }
297
298 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800299 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800300 r.roundOut(); // rounded out to be conservative
301
Chris Craik6fe991e52015-10-20 09:39:42 -0700302 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800303 clipRect.snapToPixelBoundaries();
304
305 if (!clipRect.intersects(r)) return true;
306
307 return false;
308}
309
Tom Hudson984162f2014-10-10 13:38:16 -0400310} // namespace uirenderer
311} // namespace android