blob: dc41157d5778a62a94198bdd1757f715080519af [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
Chris Craikdeeda3d2014-05-05 19:09:33 -070017#define LOG_TAG "OpenGLRenderer"
18
Chris Craik14e51302013-12-30 15:32:54 -080019#include <SkCanvas.h>
20
21#include "StatefulBaseRenderer.h"
22
Chris Craikaf4d04c2014-07-29 12:50:14 -070023#include "utils/MathUtils.h"
24
Chris Craik14e51302013-12-30 15:32:54 -080025namespace android {
26namespace uirenderer {
27
Chris Craik058fc642014-07-23 18:19:28 -070028StatefulBaseRenderer::StatefulBaseRenderer()
29 : mDirtyClip(false)
30 , mWidth(-1)
31 , mHeight(-1)
32 , mSaveCount(1)
33 , mFirstSnapshot(new Snapshot)
34 , mSnapshot(mFirstSnapshot) {
Chris Craik14e51302013-12-30 15:32:54 -080035}
36
37void StatefulBaseRenderer::initializeSaveStack(float clipLeft, float clipTop,
38 float clipRight, float clipBottom) {
39 mSnapshot = new Snapshot(mFirstSnapshot,
40 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
41 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
42 mSnapshot->fbo = getTargetFbo();
43 mSaveCount = 1;
44}
45
Chris Craik797b95b2014-05-20 18:10:25 -070046void StatefulBaseRenderer::setViewport(int width, int height) {
Chris Craik14e51302013-12-30 15:32:54 -080047 mWidth = width;
48 mHeight = height;
Chris Craika64a2be2014-05-14 14:17:01 -070049 mFirstSnapshot->initializeViewport(width, height);
Chris Craik797b95b2014-05-20 18:10:25 -070050 onViewportInitialized();
51}
52
Chris Craik14e51302013-12-30 15:32:54 -080053///////////////////////////////////////////////////////////////////////////////
54// Save (layer)
55///////////////////////////////////////////////////////////////////////////////
56
57/**
58 * Non-virtual implementation of save, guaranteed to save without side-effects
59 *
60 * The approach here and in restoreSnapshot(), allows subclasses to directly manipulate the save
61 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
62 */
63int StatefulBaseRenderer::saveSnapshot(int flags) {
64 mSnapshot = new Snapshot(mSnapshot, flags);
65 return mSaveCount++;
66}
67
68int StatefulBaseRenderer::save(int flags) {
69 return saveSnapshot(flags);
70}
71
72/**
73 * Non-virtual implementation of restore, guaranteed to restore without side-effects.
74 */
75void StatefulBaseRenderer::restoreSnapshot() {
76 sp<Snapshot> toRemove = mSnapshot;
77 sp<Snapshot> toRestore = mSnapshot->previous;
78
79 mSaveCount--;
80 mSnapshot = toRestore;
81
82 // subclass handles restore implementation
83 onSnapshotRestored(*toRemove, *toRestore);
84}
85
86void StatefulBaseRenderer::restore() {
87 if (mSaveCount > 1) {
88 restoreSnapshot();
89 }
90}
91
92void StatefulBaseRenderer::restoreToCount(int saveCount) {
93 if (saveCount < 1) saveCount = 1;
94
95 while (mSaveCount > saveCount) {
96 restoreSnapshot();
97 }
98}
99
100///////////////////////////////////////////////////////////////////////////////
101// Matrix
102///////////////////////////////////////////////////////////////////////////////
103
Chris Craik05f3d6e2014-06-02 16:27:04 -0700104void StatefulBaseRenderer::getMatrix(Matrix4* matrix) const {
105 matrix->load(*(mSnapshot->transform));
106}
107
Chris Craik14e51302013-12-30 15:32:54 -0800108void StatefulBaseRenderer::getMatrix(SkMatrix* matrix) const {
109 mSnapshot->transform->copyTo(*matrix);
110}
111
112void StatefulBaseRenderer::translate(float dx, float dy, float dz) {
113 mSnapshot->transform->translate(dx, dy, dz);
114}
115
116void StatefulBaseRenderer::rotate(float degrees) {
117 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
118}
119
120void StatefulBaseRenderer::scale(float sx, float sy) {
121 mSnapshot->transform->scale(sx, sy, 1.0f);
122}
123
124void StatefulBaseRenderer::skew(float sx, float sy) {
125 mSnapshot->transform->skew(sx, sy);
126}
127
Derek Sollenberger13908822013-12-10 12:28:58 -0500128void StatefulBaseRenderer::setMatrix(const SkMatrix& matrix) {
129 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800130}
131
132void StatefulBaseRenderer::setMatrix(const Matrix4& matrix) {
133 mSnapshot->transform->load(matrix);
134}
135
Derek Sollenberger13908822013-12-10 12:28:58 -0500136void StatefulBaseRenderer::concatMatrix(const SkMatrix& matrix) {
137 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800138 mSnapshot->transform->multiply(transform);
139}
140
141void StatefulBaseRenderer::concatMatrix(const Matrix4& matrix) {
142 mSnapshot->transform->multiply(matrix);
143}
144
145///////////////////////////////////////////////////////////////////////////////
146// Clip
147///////////////////////////////////////////////////////////////////////////////
148
Chris Craikd6b65f62014-01-01 14:45:21 -0800149bool StatefulBaseRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
150 if (CC_LIKELY(currentTransform()->rectToRect())) {
151 mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op);
152 return !mSnapshot->clipRect->isEmpty();
153 }
154
155 SkPath path;
156 path.addRect(left, top, right, bottom);
157
158 return StatefulBaseRenderer::clipPath(&path, op);
159}
160
Chris Craikd218a922014-01-02 17:13:34 -0800161bool StatefulBaseRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800162 SkMatrix transform;
163 currentTransform()->copyTo(transform);
164
165 SkPath transformed;
166 path->transform(transform, &transformed);
167
168 SkRegion clip;
169 if (!mSnapshot->previous->clipRegion->isEmpty()) {
170 clip.setRegion(*mSnapshot->previous->clipRegion);
171 } else {
172 if (mSnapshot->previous == firstSnapshot()) {
173 clip.setRect(0, 0, getWidth(), getHeight());
174 } else {
175 Rect* bounds = mSnapshot->previous->clipRect;
176 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
177 }
178 }
179
180 SkRegion region;
181 region.setPath(transformed, clip);
182
Chris Craik62d307c2014-07-29 10:35:13 -0700183 // region is the transformed input path, masked by the previous clip
Chris Craikd6b65f62014-01-01 14:45:21 -0800184 mDirtyClip |= mSnapshot->clipRegionTransformed(region, op);
185 return !mSnapshot->clipRect->isEmpty();
186}
187
Chris Craikd218a922014-01-02 17:13:34 -0800188bool StatefulBaseRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800189 mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op);
190 return !mSnapshot->clipRect->isEmpty();
191}
Chris Craik14e51302013-12-30 15:32:54 -0800192
Chris Craikdeeda3d2014-05-05 19:09:33 -0700193void StatefulBaseRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700194 Rect bounds;
195 float radius;
196 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
197
198 if (!MathUtils::isPositive(radius)) {
199 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
200 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
201 return;
202 }
203 setClippingRoundRect(allocator, bounds, radius);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700204}
205
Chris Craikaf4d04c2014-07-29 12:50:14 -0700206void StatefulBaseRenderer::setClippingRoundRect(LinearAllocator& allocator,
207 const Rect& rect, float radius) {
208 mSnapshot->setClippingRoundRect(allocator, rect, radius);
209}
210
211
Chris Craik14e51302013-12-30 15:32:54 -0800212///////////////////////////////////////////////////////////////////////////////
213// Quick Rejection
214///////////////////////////////////////////////////////////////////////////////
215
216/**
217 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
218 * the clipRect. Does not modify the scissor.
219 *
220 * @param clipRequired if not null, will be set to true if element intersects clip
221 * (and wasn't rejected)
222 *
223 * @param snapOut if set, the geometry will be treated as having an AA ramp.
224 * See Rect::snapGeometryToPixelBoundaries()
225 */
226bool StatefulBaseRenderer::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700227 float right, float bottom,
228 bool* clipRequired, bool* roundRectClipRequired,
229 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800230 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
231 return true;
232 }
233
234 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800235 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800236 r.snapGeometryToPixelBoundaries(snapOut);
237
Chris Craikd6b65f62014-01-01 14:45:21 -0800238 Rect clipRect(*currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800239 clipRect.snapToPixelBoundaries();
240
241 if (!clipRect.intersects(r)) return true;
242
243 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700244 if (clipRequired) {
245 *clipRequired = !clipRect.contains(r);
246 }
247
248 // round rect clip is required if RR clip exists, and geometry intersects its corners
249 if (roundRectClipRequired) {
250 *roundRectClipRequired = mSnapshot->roundRectClipState != NULL
251 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
252 }
Chris Craik14e51302013-12-30 15:32:54 -0800253 return false;
254}
255
256/**
257 * Returns false if drawing won't be clipped out.
258 *
259 * Makes the decision conservatively, by rounding out the mapped rect before comparing with the
260 * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
261 * rejection is still desired.
262 *
263 * This function, unlike quickRejectSetupScissor, should be used where precise geometry information
264 * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
265 * rejection where precise rejection isn't important, or precise information isn't available.
266 */
267bool StatefulBaseRenderer::quickRejectConservative(float left, float top,
268 float right, float bottom) const {
269 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
270 return true;
271 }
272
273 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800274 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800275 r.roundOut(); // rounded out to be conservative
276
Chris Craikd6b65f62014-01-01 14:45:21 -0800277 Rect clipRect(*currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800278 clipRect.snapToPixelBoundaries();
279
280 if (!clipRect.intersects(r)) return true;
281
282 return false;
283}
284
285}; // namespace uirenderer
286}; // namespace android