blob: e307ad91885fcbdb0b9c833afa4bd9af51721c08 [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);
Chris Craik6b109c72015-02-27 10:55:28 -080046 mSnapshot->fbo = mCanvas.getTargetFbo();
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 Craik7c85c542015-08-19 15:10:24 -0700141 *(mSnapshot->transform) = matrix;
Chris Craik14e51302013-12-30 15:32:54 -0800142}
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 Craik4d3e7042015-08-20 12:54:25 -0700158 mSnapshot->clip(left, top, right, bottom, op);
159 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800160 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800161}
162
Tom Hudson984162f2014-10-10 13:38:16 -0400163bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700164 mSnapshot->clipPath(*path, op);
165 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800166 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800167}
168
Tom Hudson984162f2014-10-10 13:38:16 -0400169bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700170 mSnapshot->clipRegionTransformed(*region, op);
171 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800172 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800173}
Chris Craik14e51302013-12-30 15:32:54 -0800174
Tom Hudson984162f2014-10-10 13:38:16 -0400175void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700176 Rect bounds;
177 float radius;
178 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
179
Chris Craik79d26c72014-08-21 12:26:16 -0700180 bool outlineIsRounded = MathUtils::isPositive(radius);
181 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700182 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
183 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700184 }
Chris Craik79d26c72014-08-21 12:26:16 -0700185 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700186 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700187 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700188}
189
Tom Hudson984162f2014-10-10 13:38:16 -0400190void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
Chris Craike83cbd42014-09-03 17:52:24 -0700191 const Rect& rect, float radius, bool highPriority) {
192 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700193}
194
Chris Craikfca52b752015-04-28 11:45:59 -0700195void CanvasState::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
196 mSnapshot->setProjectionPathMask(allocator, path);
197}
Chris Craikaf4d04c2014-07-29 12:50:14 -0700198
Chris Craik14e51302013-12-30 15:32:54 -0800199///////////////////////////////////////////////////////////////////////////////
200// Quick Rejection
201///////////////////////////////////////////////////////////////////////////////
202
203/**
204 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
205 * the clipRect. Does not modify the scissor.
206 *
207 * @param clipRequired if not null, will be set to true if element intersects clip
208 * (and wasn't rejected)
209 *
210 * @param snapOut if set, the geometry will be treated as having an AA ramp.
211 * See Rect::snapGeometryToPixelBoundaries()
212 */
Tom Hudson984162f2014-10-10 13:38:16 -0400213bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700214 float right, float bottom,
215 bool* clipRequired, bool* roundRectClipRequired,
216 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800217 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
218 return true;
219 }
220
221 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800222 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800223 r.snapGeometryToPixelBoundaries(snapOut);
224
Rob Tsuk487a92c2015-01-06 13:22:54 -0800225 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800226 clipRect.snapToPixelBoundaries();
227
228 if (!clipRect.intersects(r)) return true;
229
230 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700231 if (clipRequired) {
232 *clipRequired = !clipRect.contains(r);
233 }
234
235 // round rect clip is required if RR clip exists, and geometry intersects its corners
236 if (roundRectClipRequired) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800237 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700238 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
239 }
Chris Craik14e51302013-12-30 15:32:54 -0800240 return false;
241}
242
Tom Hudson984162f2014-10-10 13:38:16 -0400243bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800244 float right, float bottom) const {
245 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
246 return true;
247 }
248
249 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800250 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800251 r.roundOut(); // rounded out to be conservative
252
Rob Tsuk487a92c2015-01-06 13:22:54 -0800253 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800254 clipRect.snapToPixelBoundaries();
255
256 if (!clipRect.intersects(r)) return true;
257
258 return false;
259}
260
Tom Hudson984162f2014-10-10 13:38:16 -0400261} // namespace uirenderer
262} // namespace android