blob: cf2726b5f5307e0898073867c6feb61dca93755c [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
17#include <SkCanvas.h>
18
Tom Hudson984162f2014-10-10 13:38:16 -040019#include "CanvasState.h"
Chris Craikaf4d04c2014-07-29 12:50:14 -070020#include "utils/MathUtils.h"
21
Chris Craik14e51302013-12-30 15:32:54 -080022namespace android {
23namespace uirenderer {
24
Tom Hudson984162f2014-10-10 13:38:16 -040025
26CanvasState::CanvasState(CanvasStateClient& renderer)
Chris Craik058fc642014-07-23 18:19:28 -070027 : mDirtyClip(false)
28 , mWidth(-1)
29 , mHeight(-1)
30 , mSaveCount(1)
Tom Hudson984162f2014-10-10 13:38:16 -040031 , mCanvas(renderer)
John Reckd9ee5502015-10-06 10:06:37 -070032 , mSnapshot(&mFirstSnapshot) {
33}
Tom Hudson984162f2014-10-10 13:38:16 -040034
John Reckd9ee5502015-10-06 10:06:37 -070035CanvasState::~CanvasState() {
36 // First call freeSnapshot on all but mFirstSnapshot
37 // to invoke all the dtors
38 freeAllSnapshots();
39
40 // Now actually release the memory
41 while (mSnapshotPool) {
42 void* temp = mSnapshotPool;
43 mSnapshotPool = mSnapshotPool->previous;
44 free(temp);
45 }
Chris Craik14e51302013-12-30 15:32:54 -080046}
47
Chris Craike4db79d2015-12-22 16:32:23 -080048void CanvasState::initializeRecordingSaveStack(int viewportWidth, int viewportHeight) {
49 if (mWidth != viewportWidth || mHeight != viewportHeight) {
50 mWidth = viewportWidth;
51 mHeight = viewportHeight;
52 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
53 mCanvas.onViewportInitialized();
54 }
55
56 freeAllSnapshots();
57 mSnapshot = allocSnapshot(&mFirstSnapshot,
58 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
59 mSnapshot->setRelativeLightCenter(Vector3());
60 mSaveCount = 1;
61}
62
Chris Craik64e445b2015-09-02 14:23:49 -070063void CanvasState::initializeSaveStack(
64 int viewportWidth, int viewportHeight,
65 float clipLeft, float clipTop,
Chris Craik69e5adf2014-08-14 13:34:01 -070066 float clipRight, float clipBottom, const Vector3& lightCenter) {
Chris Craik64e445b2015-09-02 14:23:49 -070067 if (mWidth != viewportWidth || mHeight != viewportHeight) {
68 mWidth = viewportWidth;
69 mHeight = viewportHeight;
John Reckd9ee5502015-10-06 10:06:37 -070070 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
Chris Craik64e445b2015-09-02 14:23:49 -070071 mCanvas.onViewportInitialized();
72 }
73
John Reckd9ee5502015-10-06 10:06:37 -070074 freeAllSnapshots();
75 mSnapshot = allocSnapshot(&mFirstSnapshot,
Chris Craik14e51302013-12-30 15:32:54 -080076 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
77 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
Chris Craik6b109c72015-02-27 10:55:28 -080078 mSnapshot->fbo = mCanvas.getTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070079 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080080 mSaveCount = 1;
81}
82
John Reckd9ee5502015-10-06 10:06:37 -070083Snapshot* CanvasState::allocSnapshot(Snapshot* previous, int savecount) {
84 void* memory;
85 if (mSnapshotPool) {
86 memory = mSnapshotPool;
87 mSnapshotPool = mSnapshotPool->previous;
88 mSnapshotPoolCount--;
89 } else {
90 memory = malloc(sizeof(Snapshot));
91 }
92 return new (memory) Snapshot(previous, savecount);
93}
94
95void CanvasState::freeSnapshot(Snapshot* snapshot) {
96 snapshot->~Snapshot();
97 // Arbitrary number, just don't let this grown unbounded
98 if (mSnapshotPoolCount > 10) {
99 free((void*) snapshot);
100 } else {
101 snapshot->previous = mSnapshotPool;
102 mSnapshotPool = snapshot;
103 mSnapshotPoolCount++;
104 }
105}
106
107void CanvasState::freeAllSnapshots() {
108 while (mSnapshot != &mFirstSnapshot) {
109 Snapshot* temp = mSnapshot;
110 mSnapshot = mSnapshot->previous;
111 freeSnapshot(temp);
112 }
113}
114
Chris Craik14e51302013-12-30 15:32:54 -0800115///////////////////////////////////////////////////////////////////////////////
116// Save (layer)
117///////////////////////////////////////////////////////////////////////////////
118
119/**
Tom Hudson984162f2014-10-10 13:38:16 -0400120 * Guaranteed to save without side-effects
Chris Craik14e51302013-12-30 15:32:54 -0800121 *
Tom Hudson984162f2014-10-10 13:38:16 -0400122 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
Chris Craik14e51302013-12-30 15:32:54 -0800123 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
124 */
Tom Hudson984162f2014-10-10 13:38:16 -0400125int CanvasState::saveSnapshot(int flags) {
John Reckd9ee5502015-10-06 10:06:37 -0700126 mSnapshot = allocSnapshot(mSnapshot, flags);
Chris Craik14e51302013-12-30 15:32:54 -0800127 return mSaveCount++;
128}
129
Tom Hudson984162f2014-10-10 13:38:16 -0400130int CanvasState::save(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -0800131 return saveSnapshot(flags);
132}
133
134/**
Tom Hudson984162f2014-10-10 13:38:16 -0400135 * Guaranteed to restore without side-effects.
Chris Craik14e51302013-12-30 15:32:54 -0800136 */
Tom Hudson984162f2014-10-10 13:38:16 -0400137void CanvasState::restoreSnapshot() {
John Reckd9ee5502015-10-06 10:06:37 -0700138 Snapshot* toRemove = mSnapshot;
139 Snapshot* toRestore = mSnapshot->previous;
Chris Craik14e51302013-12-30 15:32:54 -0800140
141 mSaveCount--;
142 mSnapshot = toRestore;
143
144 // subclass handles restore implementation
Tom Hudson984162f2014-10-10 13:38:16 -0400145 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
John Reckd9ee5502015-10-06 10:06:37 -0700146
147 freeSnapshot(toRemove);
Chris Craik14e51302013-12-30 15:32:54 -0800148}
149
Tom Hudson984162f2014-10-10 13:38:16 -0400150void CanvasState::restore() {
Chris Craik14e51302013-12-30 15:32:54 -0800151 if (mSaveCount > 1) {
152 restoreSnapshot();
153 }
154}
155
Tom Hudson984162f2014-10-10 13:38:16 -0400156void CanvasState::restoreToCount(int saveCount) {
Chris Craik14e51302013-12-30 15:32:54 -0800157 if (saveCount < 1) saveCount = 1;
158
159 while (mSaveCount > saveCount) {
160 restoreSnapshot();
161 }
162}
163
164///////////////////////////////////////////////////////////////////////////////
165// Matrix
166///////////////////////////////////////////////////////////////////////////////
167
Tom Hudson984162f2014-10-10 13:38:16 -0400168void CanvasState::getMatrix(SkMatrix* matrix) const {
Chris Craik14e51302013-12-30 15:32:54 -0800169 mSnapshot->transform->copyTo(*matrix);
170}
171
Tom Hudson984162f2014-10-10 13:38:16 -0400172void CanvasState::translate(float dx, float dy, float dz) {
Chris Craik14e51302013-12-30 15:32:54 -0800173 mSnapshot->transform->translate(dx, dy, dz);
174}
175
Tom Hudson984162f2014-10-10 13:38:16 -0400176void CanvasState::rotate(float degrees) {
Chris Craik14e51302013-12-30 15:32:54 -0800177 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
178}
179
Tom Hudson984162f2014-10-10 13:38:16 -0400180void CanvasState::scale(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800181 mSnapshot->transform->scale(sx, sy, 1.0f);
182}
183
Tom Hudson984162f2014-10-10 13:38:16 -0400184void CanvasState::skew(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800185 mSnapshot->transform->skew(sx, sy);
186}
187
Tom Hudson984162f2014-10-10 13:38:16 -0400188void CanvasState::setMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500189 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800190}
191
Tom Hudson984162f2014-10-10 13:38:16 -0400192void CanvasState::setMatrix(const Matrix4& matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -0700193 *(mSnapshot->transform) = matrix;
Chris Craik14e51302013-12-30 15:32:54 -0800194}
195
Tom Hudson984162f2014-10-10 13:38:16 -0400196void CanvasState::concatMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500197 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800198 mSnapshot->transform->multiply(transform);
199}
200
Tom Hudson984162f2014-10-10 13:38:16 -0400201void CanvasState::concatMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800202 mSnapshot->transform->multiply(matrix);
203}
204
205///////////////////////////////////////////////////////////////////////////////
206// Clip
207///////////////////////////////////////////////////////////////////////////////
208
Tom Hudson984162f2014-10-10 13:38:16 -0400209bool CanvasState::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Chris Craika2a70722015-12-17 12:58:24 -0800210 mSnapshot->clip(Rect(left, top, right, bottom), op);
Chris Craik4d3e7042015-08-20 12:54:25 -0700211 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800212 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800213}
214
Tom Hudson984162f2014-10-10 13:38:16 -0400215bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700216 mSnapshot->clipPath(*path, op);
217 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800218 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800219}
220
Tom Hudson984162f2014-10-10 13:38:16 -0400221bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700222 mSnapshot->clipRegionTransformed(*region, op);
223 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800224 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800225}
Chris Craik14e51302013-12-30 15:32:54 -0800226
Tom Hudson984162f2014-10-10 13:38:16 -0400227void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700228 Rect bounds;
229 float radius;
230 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
231
Chris Craik79d26c72014-08-21 12:26:16 -0700232 bool outlineIsRounded = MathUtils::isPositive(radius);
233 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700234 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
235 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700236 }
Chris Craik79d26c72014-08-21 12:26:16 -0700237 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700238 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700239 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700240}
241
Tom Hudson984162f2014-10-10 13:38:16 -0400242void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
Chris Craike83cbd42014-09-03 17:52:24 -0700243 const Rect& rect, float radius, bool highPriority) {
244 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700245}
246
Chris Craikfca52b752015-04-28 11:45:59 -0700247void CanvasState::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
248 mSnapshot->setProjectionPathMask(allocator, path);
249}
Chris Craikaf4d04c2014-07-29 12:50:14 -0700250
Chris Craik14e51302013-12-30 15:32:54 -0800251///////////////////////////////////////////////////////////////////////////////
252// Quick Rejection
253///////////////////////////////////////////////////////////////////////////////
254
255/**
256 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
257 * the clipRect. Does not modify the scissor.
258 *
259 * @param clipRequired if not null, will be set to true if element intersects clip
260 * (and wasn't rejected)
261 *
262 * @param snapOut if set, the geometry will be treated as having an AA ramp.
263 * See Rect::snapGeometryToPixelBoundaries()
264 */
Tom Hudson984162f2014-10-10 13:38:16 -0400265bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700266 float right, float bottom,
267 bool* clipRequired, bool* roundRectClipRequired,
268 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800269 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
270 return true;
271 }
272
273 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800274 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800275 r.snapGeometryToPixelBoundaries(snapOut);
276
Chris Craik6fe991e52015-10-20 09:39:42 -0700277 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800278 clipRect.snapToPixelBoundaries();
279
280 if (!clipRect.intersects(r)) return true;
281
282 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700283 if (clipRequired) {
284 *clipRequired = !clipRect.contains(r);
285 }
286
287 // round rect clip is required if RR clip exists, and geometry intersects its corners
288 if (roundRectClipRequired) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800289 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700290 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
291 }
Chris Craik14e51302013-12-30 15:32:54 -0800292 return false;
293}
294
Tom Hudson984162f2014-10-10 13:38:16 -0400295bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800296 float right, float bottom) const {
297 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
298 return true;
299 }
300
301 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800302 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800303 r.roundOut(); // rounded out to be conservative
304
Chris Craik6fe991e52015-10-20 09:39:42 -0700305 Rect clipRect(currentRenderTargetClip());
Chris Craik14e51302013-12-30 15:32:54 -0800306 clipRect.snapToPixelBoundaries();
307
308 if (!clipRect.intersects(r)) return true;
309
310 return false;
311}
312
Tom Hudson984162f2014-10-10 13:38:16 -0400313} // namespace uirenderer
314} // namespace android