blob: 9d2ccf17c299e80f7ed5b6d808e880c939ac9023 [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)
31 , mFirstSnapshot(new Snapshot)
Tom Hudson984162f2014-10-10 13:38:16 -040032 , mCanvas(renderer)
Chris Craik058fc642014-07-23 18:19:28 -070033 , mSnapshot(mFirstSnapshot) {
Tom Hudson984162f2014-10-10 13:38:16 -040034
Chris Craik14e51302013-12-30 15:32:54 -080035}
36
Tom Hudson984162f2014-10-10 13:38:16 -040037CanvasState::~CanvasState() {
38
39}
40
41void CanvasState::initializeSaveStack(float clipLeft, float clipTop,
Chris Craik69e5adf2014-08-14 13:34:01 -070042 float clipRight, float clipBottom, const Vector3& lightCenter) {
Chris Craik14e51302013-12-30 15:32:54 -080043 mSnapshot = new Snapshot(mFirstSnapshot,
44 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
45 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
Tom Hudson984162f2014-10-10 13:38:16 -040046 mSnapshot->fbo = mCanvas.onGetTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070047 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080048 mSaveCount = 1;
49}
50
Tom Hudson984162f2014-10-10 13:38:16 -040051void CanvasState::setViewport(int width, int height) {
Chris Craik14e51302013-12-30 15:32:54 -080052 mWidth = width;
53 mHeight = height;
Chris Craika64a2be2014-05-14 14:17:01 -070054 mFirstSnapshot->initializeViewport(width, height);
Tom Hudson984162f2014-10-10 13:38:16 -040055 mCanvas.onViewportInitialized();
Chris Craik284b2432014-09-18 16:05:35 -070056
57 // create a temporary 1st snapshot, so old snapshots are released,
58 // and viewport can be queried safely.
59 // TODO: remove, combine viewport + save stack initialization
60 mSnapshot = new Snapshot(mFirstSnapshot,
61 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
62 mSaveCount = 1;
Chris Craik797b95b22014-05-20 18:10:25 -070063}
64
Chris Craik14e51302013-12-30 15:32:54 -080065///////////////////////////////////////////////////////////////////////////////
66// Save (layer)
67///////////////////////////////////////////////////////////////////////////////
68
69/**
Tom Hudson984162f2014-10-10 13:38:16 -040070 * Guaranteed to save without side-effects
Chris Craik14e51302013-12-30 15:32:54 -080071 *
Tom Hudson984162f2014-10-10 13:38:16 -040072 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
Chris Craik14e51302013-12-30 15:32:54 -080073 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
74 */
Tom Hudson984162f2014-10-10 13:38:16 -040075int CanvasState::saveSnapshot(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -080076 mSnapshot = new Snapshot(mSnapshot, flags);
77 return mSaveCount++;
78}
79
Tom Hudson984162f2014-10-10 13:38:16 -040080int CanvasState::save(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -080081 return saveSnapshot(flags);
82}
83
84/**
Tom Hudson984162f2014-10-10 13:38:16 -040085 * Guaranteed to restore without side-effects.
Chris Craik14e51302013-12-30 15:32:54 -080086 */
Tom Hudson984162f2014-10-10 13:38:16 -040087void CanvasState::restoreSnapshot() {
Chris Craik14e51302013-12-30 15:32:54 -080088 sp<Snapshot> toRemove = mSnapshot;
89 sp<Snapshot> toRestore = mSnapshot->previous;
90
91 mSaveCount--;
92 mSnapshot = toRestore;
93
94 // subclass handles restore implementation
Tom Hudson984162f2014-10-10 13:38:16 -040095 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
Chris Craik14e51302013-12-30 15:32:54 -080096}
97
Tom Hudson984162f2014-10-10 13:38:16 -040098void CanvasState::restore() {
Chris Craik14e51302013-12-30 15:32:54 -080099 if (mSaveCount > 1) {
100 restoreSnapshot();
101 }
102}
103
Tom Hudson984162f2014-10-10 13:38:16 -0400104void CanvasState::restoreToCount(int saveCount) {
Chris Craik14e51302013-12-30 15:32:54 -0800105 if (saveCount < 1) saveCount = 1;
106
107 while (mSaveCount > saveCount) {
108 restoreSnapshot();
109 }
110}
111
112///////////////////////////////////////////////////////////////////////////////
113// Matrix
114///////////////////////////////////////////////////////////////////////////////
115
Tom Hudson984162f2014-10-10 13:38:16 -0400116void CanvasState::getMatrix(SkMatrix* matrix) const {
Chris Craik14e51302013-12-30 15:32:54 -0800117 mSnapshot->transform->copyTo(*matrix);
118}
119
Tom Hudson984162f2014-10-10 13:38:16 -0400120void CanvasState::translate(float dx, float dy, float dz) {
Chris Craik14e51302013-12-30 15:32:54 -0800121 mSnapshot->transform->translate(dx, dy, dz);
122}
123
Tom Hudson984162f2014-10-10 13:38:16 -0400124void CanvasState::rotate(float degrees) {
Chris Craik14e51302013-12-30 15:32:54 -0800125 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
126}
127
Tom Hudson984162f2014-10-10 13:38:16 -0400128void CanvasState::scale(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800129 mSnapshot->transform->scale(sx, sy, 1.0f);
130}
131
Tom Hudson984162f2014-10-10 13:38:16 -0400132void CanvasState::skew(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800133 mSnapshot->transform->skew(sx, sy);
134}
135
Tom Hudson984162f2014-10-10 13:38:16 -0400136void CanvasState::setMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500137 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800138}
139
Tom Hudson984162f2014-10-10 13:38:16 -0400140void CanvasState::setMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800141 mSnapshot->transform->load(matrix);
142}
143
Tom Hudson984162f2014-10-10 13:38:16 -0400144void CanvasState::concatMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500145 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800146 mSnapshot->transform->multiply(transform);
147}
148
Tom Hudson984162f2014-10-10 13:38:16 -0400149void CanvasState::concatMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800150 mSnapshot->transform->multiply(matrix);
151}
152
153///////////////////////////////////////////////////////////////////////////////
154// Clip
155///////////////////////////////////////////////////////////////////////////////
156
Tom Hudson984162f2014-10-10 13:38:16 -0400157bool CanvasState::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800158 if (CC_LIKELY(currentTransform()->rectToRect())) {
159 mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op);
160 return !mSnapshot->clipRect->isEmpty();
161 }
162
163 SkPath path;
164 path.addRect(left, top, right, bottom);
165
Tom Hudson984162f2014-10-10 13:38:16 -0400166 return CanvasState::clipPath(&path, op);
Chris Craikd6b65f62014-01-01 14:45:21 -0800167}
168
Tom Hudson984162f2014-10-10 13:38:16 -0400169bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800170 SkMatrix transform;
171 currentTransform()->copyTo(transform);
172
173 SkPath transformed;
174 path->transform(transform, &transformed);
175
176 SkRegion clip;
177 if (!mSnapshot->previous->clipRegion->isEmpty()) {
178 clip.setRegion(*mSnapshot->previous->clipRegion);
179 } else {
180 if (mSnapshot->previous == firstSnapshot()) {
181 clip.setRect(0, 0, getWidth(), getHeight());
182 } else {
183 Rect* bounds = mSnapshot->previous->clipRect;
184 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
185 }
186 }
187
188 SkRegion region;
189 region.setPath(transformed, clip);
190
Chris Craik62d307c2014-07-29 10:35:13 -0700191 // region is the transformed input path, masked by the previous clip
Chris Craikd6b65f62014-01-01 14:45:21 -0800192 mDirtyClip |= mSnapshot->clipRegionTransformed(region, op);
193 return !mSnapshot->clipRect->isEmpty();
194}
195
Tom Hudson984162f2014-10-10 13:38:16 -0400196bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800197 mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op);
198 return !mSnapshot->clipRect->isEmpty();
199}
Chris Craik14e51302013-12-30 15:32:54 -0800200
Tom Hudson984162f2014-10-10 13:38:16 -0400201void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700202 Rect bounds;
203 float radius;
204 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
205
Chris Craik79d26c72014-08-21 12:26:16 -0700206 bool outlineIsRounded = MathUtils::isPositive(radius);
207 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700208 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
209 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700210 }
Chris Craik79d26c72014-08-21 12:26:16 -0700211 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700212 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700213 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700214}
215
Tom Hudson984162f2014-10-10 13:38:16 -0400216void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
Chris Craike83cbd42014-09-03 17:52:24 -0700217 const Rect& rect, float radius, bool highPriority) {
218 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700219}
220
221
Chris Craik14e51302013-12-30 15:32:54 -0800222///////////////////////////////////////////////////////////////////////////////
223// Quick Rejection
224///////////////////////////////////////////////////////////////////////////////
225
226/**
227 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
228 * the clipRect. Does not modify the scissor.
229 *
230 * @param clipRequired if not null, will be set to true if element intersects clip
231 * (and wasn't rejected)
232 *
233 * @param snapOut if set, the geometry will be treated as having an AA ramp.
234 * See Rect::snapGeometryToPixelBoundaries()
235 */
Tom Hudson984162f2014-10-10 13:38:16 -0400236bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700237 float right, float bottom,
238 bool* clipRequired, bool* roundRectClipRequired,
239 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800240 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
241 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 Craikd6b65f62014-01-01 14:45:21 -0800248 Rect clipRect(*currentClipRect());
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) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800260 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700261 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
262 }
Chris Craik14e51302013-12-30 15:32:54 -0800263 return false;
264}
265
Tom Hudson984162f2014-10-10 13:38:16 -0400266bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800267 float right, float bottom) const {
268 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
269 return true;
270 }
271
272 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800273 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800274 r.roundOut(); // rounded out to be conservative
275
Chris Craikd6b65f62014-01-01 14:45:21 -0800276 Rect clipRect(*currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800277 clipRect.snapToPixelBoundaries();
278
279 if (!clipRect.intersects(r)) return true;
280
281 return false;
282}
283
Tom Hudson984162f2014-10-10 13:38:16 -0400284} // namespace uirenderer
285} // namespace android