blob: e22b0d3084abd1c35548e9d2b3ae28a750ef3db2 [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 Craik797b95b2014-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) {
Rob Tsuk487a92c2015-01-06 13:22:54 -0800158 mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op);
159 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800160}
161
Tom Hudson984162f2014-10-10 13:38:16 -0400162bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
Rob Tsuk487a92c2015-01-06 13:22:54 -0800163 mDirtyClip |= mSnapshot->clipPath(*path, op);
164 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800165}
166
Tom Hudson984162f2014-10-10 13:38:16 -0400167bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800168 mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800169 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800170}
Chris Craik14e51302013-12-30 15:32:54 -0800171
Tom Hudson984162f2014-10-10 13:38:16 -0400172void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700173 Rect bounds;
174 float radius;
175 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
176
Chris Craik79d26c72014-08-21 12:26:16 -0700177 bool outlineIsRounded = MathUtils::isPositive(radius);
178 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700179 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
180 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700181 }
Chris Craik79d26c72014-08-21 12:26:16 -0700182 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700183 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700184 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700185}
186
Tom Hudson984162f2014-10-10 13:38:16 -0400187void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
Chris Craike83cbd42014-09-03 17:52:24 -0700188 const Rect& rect, float radius, bool highPriority) {
189 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700190}
191
Chris Craikfca52b752015-04-28 11:45:59 -0700192void CanvasState::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
193 mSnapshot->setProjectionPathMask(allocator, path);
194}
Chris Craikaf4d04c2014-07-29 12:50:14 -0700195
Chris Craik14e51302013-12-30 15:32:54 -0800196///////////////////////////////////////////////////////////////////////////////
197// Quick Rejection
198///////////////////////////////////////////////////////////////////////////////
199
200/**
201 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
202 * the clipRect. Does not modify the scissor.
203 *
204 * @param clipRequired if not null, will be set to true if element intersects clip
205 * (and wasn't rejected)
206 *
207 * @param snapOut if set, the geometry will be treated as having an AA ramp.
208 * See Rect::snapGeometryToPixelBoundaries()
209 */
Tom Hudson984162f2014-10-10 13:38:16 -0400210bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700211 float right, float bottom,
212 bool* clipRequired, bool* roundRectClipRequired,
213 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800214 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
215 return true;
216 }
217
218 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800219 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800220 r.snapGeometryToPixelBoundaries(snapOut);
221
Rob Tsuk487a92c2015-01-06 13:22:54 -0800222 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800223 clipRect.snapToPixelBoundaries();
224
225 if (!clipRect.intersects(r)) return true;
226
227 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700228 if (clipRequired) {
229 *clipRequired = !clipRect.contains(r);
230 }
231
232 // round rect clip is required if RR clip exists, and geometry intersects its corners
233 if (roundRectClipRequired) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800234 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700235 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
236 }
Chris Craik14e51302013-12-30 15:32:54 -0800237 return false;
238}
239
Tom Hudson984162f2014-10-10 13:38:16 -0400240bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800241 float right, float bottom) const {
242 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
243 return true;
244 }
245
246 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800247 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800248 r.roundOut(); // rounded out to be conservative
249
Rob Tsuk487a92c2015-01-06 13:22:54 -0800250 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800251 clipRect.snapToPixelBoundaries();
252
253 if (!clipRect.intersects(r)) return true;
254
255 return false;
256}
257
Tom Hudson984162f2014-10-10 13:38:16 -0400258} // namespace uirenderer
259} // namespace android