blob: eca71c6e0e8d6c4cee398a221b243e7e9286bd29 [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 Craik64e445b2015-09-02 14:23:49 -070048void CanvasState::initializeSaveStack(
49 int viewportWidth, int viewportHeight,
50 float clipLeft, float clipTop,
Chris Craik69e5adf2014-08-14 13:34:01 -070051 float clipRight, float clipBottom, const Vector3& lightCenter) {
Chris Craik64e445b2015-09-02 14:23:49 -070052 if (mWidth != viewportWidth || mHeight != viewportHeight) {
53 mWidth = viewportWidth;
54 mHeight = viewportHeight;
John Reckd9ee5502015-10-06 10:06:37 -070055 mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight);
Chris Craik64e445b2015-09-02 14:23:49 -070056 mCanvas.onViewportInitialized();
57 }
58
John Reckd9ee5502015-10-06 10:06:37 -070059 freeAllSnapshots();
60 mSnapshot = allocSnapshot(&mFirstSnapshot,
Chris Craik14e51302013-12-30 15:32:54 -080061 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
62 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
Chris Craik6b109c72015-02-27 10:55:28 -080063 mSnapshot->fbo = mCanvas.getTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070064 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080065 mSaveCount = 1;
66}
67
John Reckd9ee5502015-10-06 10:06:37 -070068Snapshot* CanvasState::allocSnapshot(Snapshot* previous, int savecount) {
69 void* memory;
70 if (mSnapshotPool) {
71 memory = mSnapshotPool;
72 mSnapshotPool = mSnapshotPool->previous;
73 mSnapshotPoolCount--;
74 } else {
75 memory = malloc(sizeof(Snapshot));
76 }
77 return new (memory) Snapshot(previous, savecount);
78}
79
80void CanvasState::freeSnapshot(Snapshot* snapshot) {
81 snapshot->~Snapshot();
82 // Arbitrary number, just don't let this grown unbounded
83 if (mSnapshotPoolCount > 10) {
84 free((void*) snapshot);
85 } else {
86 snapshot->previous = mSnapshotPool;
87 mSnapshotPool = snapshot;
88 mSnapshotPoolCount++;
89 }
90}
91
92void CanvasState::freeAllSnapshots() {
93 while (mSnapshot != &mFirstSnapshot) {
94 Snapshot* temp = mSnapshot;
95 mSnapshot = mSnapshot->previous;
96 freeSnapshot(temp);
97 }
98}
99
Chris Craik14e51302013-12-30 15:32:54 -0800100///////////////////////////////////////////////////////////////////////////////
101// Save (layer)
102///////////////////////////////////////////////////////////////////////////////
103
104/**
Tom Hudson984162f2014-10-10 13:38:16 -0400105 * Guaranteed to save without side-effects
Chris Craik14e51302013-12-30 15:32:54 -0800106 *
Tom Hudson984162f2014-10-10 13:38:16 -0400107 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
Chris Craik14e51302013-12-30 15:32:54 -0800108 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
109 */
Tom Hudson984162f2014-10-10 13:38:16 -0400110int CanvasState::saveSnapshot(int flags) {
John Reckd9ee5502015-10-06 10:06:37 -0700111 mSnapshot = allocSnapshot(mSnapshot, flags);
Chris Craik14e51302013-12-30 15:32:54 -0800112 return mSaveCount++;
113}
114
Tom Hudson984162f2014-10-10 13:38:16 -0400115int CanvasState::save(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -0800116 return saveSnapshot(flags);
117}
118
119/**
Tom Hudson984162f2014-10-10 13:38:16 -0400120 * Guaranteed to restore without side-effects.
Chris Craik14e51302013-12-30 15:32:54 -0800121 */
Tom Hudson984162f2014-10-10 13:38:16 -0400122void CanvasState::restoreSnapshot() {
John Reckd9ee5502015-10-06 10:06:37 -0700123 Snapshot* toRemove = mSnapshot;
124 Snapshot* toRestore = mSnapshot->previous;
Chris Craik14e51302013-12-30 15:32:54 -0800125
126 mSaveCount--;
127 mSnapshot = toRestore;
128
129 // subclass handles restore implementation
Tom Hudson984162f2014-10-10 13:38:16 -0400130 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
John Reckd9ee5502015-10-06 10:06:37 -0700131
132 freeSnapshot(toRemove);
Chris Craik14e51302013-12-30 15:32:54 -0800133}
134
Tom Hudson984162f2014-10-10 13:38:16 -0400135void CanvasState::restore() {
Chris Craik14e51302013-12-30 15:32:54 -0800136 if (mSaveCount > 1) {
137 restoreSnapshot();
138 }
139}
140
Tom Hudson984162f2014-10-10 13:38:16 -0400141void CanvasState::restoreToCount(int saveCount) {
Chris Craik14e51302013-12-30 15:32:54 -0800142 if (saveCount < 1) saveCount = 1;
143
144 while (mSaveCount > saveCount) {
145 restoreSnapshot();
146 }
147}
148
149///////////////////////////////////////////////////////////////////////////////
150// Matrix
151///////////////////////////////////////////////////////////////////////////////
152
Tom Hudson984162f2014-10-10 13:38:16 -0400153void CanvasState::getMatrix(SkMatrix* matrix) const {
Chris Craik14e51302013-12-30 15:32:54 -0800154 mSnapshot->transform->copyTo(*matrix);
155}
156
Tom Hudson984162f2014-10-10 13:38:16 -0400157void CanvasState::translate(float dx, float dy, float dz) {
Chris Craik14e51302013-12-30 15:32:54 -0800158 mSnapshot->transform->translate(dx, dy, dz);
159}
160
Tom Hudson984162f2014-10-10 13:38:16 -0400161void CanvasState::rotate(float degrees) {
Chris Craik14e51302013-12-30 15:32:54 -0800162 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
163}
164
Tom Hudson984162f2014-10-10 13:38:16 -0400165void CanvasState::scale(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800166 mSnapshot->transform->scale(sx, sy, 1.0f);
167}
168
Tom Hudson984162f2014-10-10 13:38:16 -0400169void CanvasState::skew(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800170 mSnapshot->transform->skew(sx, sy);
171}
172
Tom Hudson984162f2014-10-10 13:38:16 -0400173void CanvasState::setMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500174 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800175}
176
Tom Hudson984162f2014-10-10 13:38:16 -0400177void CanvasState::setMatrix(const Matrix4& matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -0700178 *(mSnapshot->transform) = matrix;
Chris Craik14e51302013-12-30 15:32:54 -0800179}
180
Tom Hudson984162f2014-10-10 13:38:16 -0400181void CanvasState::concatMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500182 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800183 mSnapshot->transform->multiply(transform);
184}
185
Tom Hudson984162f2014-10-10 13:38:16 -0400186void CanvasState::concatMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800187 mSnapshot->transform->multiply(matrix);
188}
189
190///////////////////////////////////////////////////////////////////////////////
191// Clip
192///////////////////////////////////////////////////////////////////////////////
193
Tom Hudson984162f2014-10-10 13:38:16 -0400194bool CanvasState::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700195 mSnapshot->clip(left, top, right, bottom, op);
196 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800197 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800198}
199
Tom Hudson984162f2014-10-10 13:38:16 -0400200bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700201 mSnapshot->clipPath(*path, op);
202 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800203 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800204}
205
Tom Hudson984162f2014-10-10 13:38:16 -0400206bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700207 mSnapshot->clipRegionTransformed(*region, op);
208 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800209 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800210}
Chris Craik14e51302013-12-30 15:32:54 -0800211
Tom Hudson984162f2014-10-10 13:38:16 -0400212void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700213 Rect bounds;
214 float radius;
215 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
216
Chris Craik79d26c72014-08-21 12:26:16 -0700217 bool outlineIsRounded = MathUtils::isPositive(radius);
218 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700219 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
220 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700221 }
Chris Craik79d26c72014-08-21 12:26:16 -0700222 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700223 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700224 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700225}
226
Tom Hudson984162f2014-10-10 13:38:16 -0400227void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
Chris Craike83cbd42014-09-03 17:52:24 -0700228 const Rect& rect, float radius, bool highPriority) {
229 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700230}
231
Chris Craikfca52b752015-04-28 11:45:59 -0700232void CanvasState::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
233 mSnapshot->setProjectionPathMask(allocator, path);
234}
Chris Craikaf4d04c2014-07-29 12:50:14 -0700235
Chris Craik14e51302013-12-30 15:32:54 -0800236///////////////////////////////////////////////////////////////////////////////
237// Quick Rejection
238///////////////////////////////////////////////////////////////////////////////
239
240/**
241 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
242 * the clipRect. Does not modify the scissor.
243 *
244 * @param clipRequired if not null, will be set to true if element intersects clip
245 * (and wasn't rejected)
246 *
247 * @param snapOut if set, the geometry will be treated as having an AA ramp.
248 * See Rect::snapGeometryToPixelBoundaries()
249 */
Tom Hudson984162f2014-10-10 13:38:16 -0400250bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700251 float right, float bottom,
252 bool* clipRequired, bool* roundRectClipRequired,
253 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800254 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
255 return true;
256 }
257
258 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800259 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800260 r.snapGeometryToPixelBoundaries(snapOut);
261
Rob Tsuk487a92c2015-01-06 13:22:54 -0800262 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800263 clipRect.snapToPixelBoundaries();
264
265 if (!clipRect.intersects(r)) return true;
266
267 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700268 if (clipRequired) {
269 *clipRequired = !clipRect.contains(r);
270 }
271
272 // round rect clip is required if RR clip exists, and geometry intersects its corners
273 if (roundRectClipRequired) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800274 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700275 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
276 }
Chris Craik14e51302013-12-30 15:32:54 -0800277 return false;
278}
279
Tom Hudson984162f2014-10-10 13:38:16 -0400280bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800281 float right, float bottom) const {
282 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
283 return true;
284 }
285
286 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800287 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800288 r.roundOut(); // rounded out to be conservative
289
Rob Tsuk487a92c2015-01-06 13:22:54 -0800290 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800291 clipRect.snapToPixelBoundaries();
292
293 if (!clipRect.intersects(r)) return true;
294
295 return false;
296}
297
Tom Hudson984162f2014-10-10 13:38:16 -0400298} // namespace uirenderer
299} // namespace android