blob: c128ca7751550c58e9ea3c0dfc276bb847de2211 [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
Chris Craik64e445b2015-09-02 14:23:49 -070037void CanvasState::initializeSaveStack(
38 int viewportWidth, int viewportHeight,
39 float clipLeft, float clipTop,
Chris Craik69e5adf2014-08-14 13:34:01 -070040 float clipRight, float clipBottom, const Vector3& lightCenter) {
Chris Craik64e445b2015-09-02 14:23:49 -070041 if (mWidth != viewportWidth || mHeight != viewportHeight) {
42 mWidth = viewportWidth;
43 mHeight = viewportHeight;
44 mFirstSnapshot->initializeViewport(viewportWidth, viewportHeight);
45 mCanvas.onViewportInitialized();
46 }
47
Chris Craik14e51302013-12-30 15:32:54 -080048 mSnapshot = new Snapshot(mFirstSnapshot,
49 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
50 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
Chris Craik6b109c72015-02-27 10:55:28 -080051 mSnapshot->fbo = mCanvas.getTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070052 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080053 mSaveCount = 1;
54}
55
Chris Craik14e51302013-12-30 15:32:54 -080056///////////////////////////////////////////////////////////////////////////////
57// Save (layer)
58///////////////////////////////////////////////////////////////////////////////
59
60/**
Tom Hudson984162f2014-10-10 13:38:16 -040061 * Guaranteed to save without side-effects
Chris Craik14e51302013-12-30 15:32:54 -080062 *
Tom Hudson984162f2014-10-10 13:38:16 -040063 * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save
Chris Craik14e51302013-12-30 15:32:54 -080064 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
65 */
Tom Hudson984162f2014-10-10 13:38:16 -040066int CanvasState::saveSnapshot(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -080067 mSnapshot = new Snapshot(mSnapshot, flags);
68 return mSaveCount++;
69}
70
Tom Hudson984162f2014-10-10 13:38:16 -040071int CanvasState::save(int flags) {
Chris Craik14e51302013-12-30 15:32:54 -080072 return saveSnapshot(flags);
73}
74
75/**
Tom Hudson984162f2014-10-10 13:38:16 -040076 * Guaranteed to restore without side-effects.
Chris Craik14e51302013-12-30 15:32:54 -080077 */
Tom Hudson984162f2014-10-10 13:38:16 -040078void CanvasState::restoreSnapshot() {
Chris Craik14e51302013-12-30 15:32:54 -080079 sp<Snapshot> toRemove = mSnapshot;
80 sp<Snapshot> toRestore = mSnapshot->previous;
81
82 mSaveCount--;
83 mSnapshot = toRestore;
84
85 // subclass handles restore implementation
Tom Hudson984162f2014-10-10 13:38:16 -040086 mCanvas.onSnapshotRestored(*toRemove, *toRestore);
Chris Craik14e51302013-12-30 15:32:54 -080087}
88
Tom Hudson984162f2014-10-10 13:38:16 -040089void CanvasState::restore() {
Chris Craik14e51302013-12-30 15:32:54 -080090 if (mSaveCount > 1) {
91 restoreSnapshot();
92 }
93}
94
Tom Hudson984162f2014-10-10 13:38:16 -040095void CanvasState::restoreToCount(int saveCount) {
Chris Craik14e51302013-12-30 15:32:54 -080096 if (saveCount < 1) saveCount = 1;
97
98 while (mSaveCount > saveCount) {
99 restoreSnapshot();
100 }
101}
102
103///////////////////////////////////////////////////////////////////////////////
104// Matrix
105///////////////////////////////////////////////////////////////////////////////
106
Tom Hudson984162f2014-10-10 13:38:16 -0400107void CanvasState::getMatrix(SkMatrix* matrix) const {
Chris Craik14e51302013-12-30 15:32:54 -0800108 mSnapshot->transform->copyTo(*matrix);
109}
110
Tom Hudson984162f2014-10-10 13:38:16 -0400111void CanvasState::translate(float dx, float dy, float dz) {
Chris Craik14e51302013-12-30 15:32:54 -0800112 mSnapshot->transform->translate(dx, dy, dz);
113}
114
Tom Hudson984162f2014-10-10 13:38:16 -0400115void CanvasState::rotate(float degrees) {
Chris Craik14e51302013-12-30 15:32:54 -0800116 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
117}
118
Tom Hudson984162f2014-10-10 13:38:16 -0400119void CanvasState::scale(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800120 mSnapshot->transform->scale(sx, sy, 1.0f);
121}
122
Tom Hudson984162f2014-10-10 13:38:16 -0400123void CanvasState::skew(float sx, float sy) {
Chris Craik14e51302013-12-30 15:32:54 -0800124 mSnapshot->transform->skew(sx, sy);
125}
126
Tom Hudson984162f2014-10-10 13:38:16 -0400127void CanvasState::setMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500128 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800129}
130
Tom Hudson984162f2014-10-10 13:38:16 -0400131void CanvasState::setMatrix(const Matrix4& matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -0700132 *(mSnapshot->transform) = matrix;
Chris Craik14e51302013-12-30 15:32:54 -0800133}
134
Tom Hudson984162f2014-10-10 13:38:16 -0400135void CanvasState::concatMatrix(const SkMatrix& matrix) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500136 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800137 mSnapshot->transform->multiply(transform);
138}
139
Tom Hudson984162f2014-10-10 13:38:16 -0400140void CanvasState::concatMatrix(const Matrix4& matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800141 mSnapshot->transform->multiply(matrix);
142}
143
144///////////////////////////////////////////////////////////////////////////////
145// Clip
146///////////////////////////////////////////////////////////////////////////////
147
Tom Hudson984162f2014-10-10 13:38:16 -0400148bool CanvasState::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700149 mSnapshot->clip(left, top, right, bottom, op);
150 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800151 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800152}
153
Tom Hudson984162f2014-10-10 13:38:16 -0400154bool CanvasState::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700155 mSnapshot->clipPath(*path, op);
156 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800157 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800158}
159
Tom Hudson984162f2014-10-10 13:38:16 -0400160bool CanvasState::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craik4d3e7042015-08-20 12:54:25 -0700161 mSnapshot->clipRegionTransformed(*region, op);
162 mDirtyClip = true;
Rob Tsuk487a92c2015-01-06 13:22:54 -0800163 return !mSnapshot->clipIsEmpty();
Chris Craikd6b65f62014-01-01 14:45:21 -0800164}
Chris Craik14e51302013-12-30 15:32:54 -0800165
Tom Hudson984162f2014-10-10 13:38:16 -0400166void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700167 Rect bounds;
168 float radius;
169 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
170
Chris Craik79d26c72014-08-21 12:26:16 -0700171 bool outlineIsRounded = MathUtils::isPositive(radius);
172 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700173 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
174 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700175 }
Chris Craik79d26c72014-08-21 12:26:16 -0700176 if (outlineIsRounded) {
Chris Craike83cbd42014-09-03 17:52:24 -0700177 setClippingRoundRect(allocator, bounds, radius, false);
Chris Craik79d26c72014-08-21 12:26:16 -0700178 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700179}
180
Tom Hudson984162f2014-10-10 13:38:16 -0400181void CanvasState::setClippingRoundRect(LinearAllocator& allocator,
Chris Craike83cbd42014-09-03 17:52:24 -0700182 const Rect& rect, float radius, bool highPriority) {
183 mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700184}
185
Chris Craikfca52b752015-04-28 11:45:59 -0700186void CanvasState::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) {
187 mSnapshot->setProjectionPathMask(allocator, path);
188}
Chris Craikaf4d04c2014-07-29 12:50:14 -0700189
Chris Craik14e51302013-12-30 15:32:54 -0800190///////////////////////////////////////////////////////////////////////////////
191// Quick Rejection
192///////////////////////////////////////////////////////////////////////////////
193
194/**
195 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
196 * the clipRect. Does not modify the scissor.
197 *
198 * @param clipRequired if not null, will be set to true if element intersects clip
199 * (and wasn't rejected)
200 *
201 * @param snapOut if set, the geometry will be treated as having an AA ramp.
202 * See Rect::snapGeometryToPixelBoundaries()
203 */
Tom Hudson984162f2014-10-10 13:38:16 -0400204bool CanvasState::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700205 float right, float bottom,
206 bool* clipRequired, bool* roundRectClipRequired,
207 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800208 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
209 return true;
210 }
211
212 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800213 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800214 r.snapGeometryToPixelBoundaries(snapOut);
215
Rob Tsuk487a92c2015-01-06 13:22:54 -0800216 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800217 clipRect.snapToPixelBoundaries();
218
219 if (!clipRect.intersects(r)) return true;
220
221 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700222 if (clipRequired) {
223 *clipRequired = !clipRect.contains(r);
224 }
225
226 // round rect clip is required if RR clip exists, and geometry intersects its corners
227 if (roundRectClipRequired) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800228 *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr
Chris Craikdeeda3d2014-05-05 19:09:33 -0700229 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
230 }
Chris Craik14e51302013-12-30 15:32:54 -0800231 return false;
232}
233
Tom Hudson984162f2014-10-10 13:38:16 -0400234bool CanvasState::quickRejectConservative(float left, float top,
Chris Craik14e51302013-12-30 15:32:54 -0800235 float right, float bottom) const {
236 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
237 return true;
238 }
239
240 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800241 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800242 r.roundOut(); // rounded out to be conservative
243
Rob Tsuk487a92c2015-01-06 13:22:54 -0800244 Rect clipRect(currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800245 clipRect.snapToPixelBoundaries();
246
247 if (!clipRect.intersects(r)) return true;
248
249 return false;
250}
251
Tom Hudson984162f2014-10-10 13:38:16 -0400252} // namespace uirenderer
253} // namespace android