blob: bdac47b88ac25a065b923c0f028e84ea2aa03e6e [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,
Chris Craik69e5adf2014-08-14 13:34:01 -070038 float clipRight, float clipBottom, const Vector3& lightCenter) {
Chris Craik14e51302013-12-30 15:32:54 -080039 mSnapshot = new Snapshot(mFirstSnapshot,
40 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
41 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
42 mSnapshot->fbo = getTargetFbo();
Chris Craik69e5adf2014-08-14 13:34:01 -070043 mSnapshot->setRelativeLightCenter(lightCenter);
Chris Craik14e51302013-12-30 15:32:54 -080044 mSaveCount = 1;
45}
46
Chris Craik797b95b2014-05-20 18:10:25 -070047void StatefulBaseRenderer::setViewport(int width, int height) {
Chris Craik14e51302013-12-30 15:32:54 -080048 mWidth = width;
49 mHeight = height;
Chris Craika64a2be2014-05-14 14:17:01 -070050 mFirstSnapshot->initializeViewport(width, height);
Chris Craik797b95b2014-05-20 18:10:25 -070051 onViewportInitialized();
52}
53
Chris Craik14e51302013-12-30 15:32:54 -080054///////////////////////////////////////////////////////////////////////////////
55// Save (layer)
56///////////////////////////////////////////////////////////////////////////////
57
58/**
59 * Non-virtual implementation of save, guaranteed to save without side-effects
60 *
61 * The approach here and in restoreSnapshot(), allows subclasses to directly manipulate the save
62 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
63 */
64int StatefulBaseRenderer::saveSnapshot(int flags) {
65 mSnapshot = new Snapshot(mSnapshot, flags);
66 return mSaveCount++;
67}
68
69int StatefulBaseRenderer::save(int flags) {
70 return saveSnapshot(flags);
71}
72
73/**
74 * Non-virtual implementation of restore, guaranteed to restore without side-effects.
75 */
76void StatefulBaseRenderer::restoreSnapshot() {
77 sp<Snapshot> toRemove = mSnapshot;
78 sp<Snapshot> toRestore = mSnapshot->previous;
79
80 mSaveCount--;
81 mSnapshot = toRestore;
82
83 // subclass handles restore implementation
84 onSnapshotRestored(*toRemove, *toRestore);
85}
86
87void StatefulBaseRenderer::restore() {
88 if (mSaveCount > 1) {
89 restoreSnapshot();
90 }
91}
92
93void StatefulBaseRenderer::restoreToCount(int saveCount) {
94 if (saveCount < 1) saveCount = 1;
95
96 while (mSaveCount > saveCount) {
97 restoreSnapshot();
98 }
99}
100
101///////////////////////////////////////////////////////////////////////////////
102// Matrix
103///////////////////////////////////////////////////////////////////////////////
104
Chris Craik05f3d6e2014-06-02 16:27:04 -0700105void StatefulBaseRenderer::getMatrix(Matrix4* matrix) const {
106 matrix->load(*(mSnapshot->transform));
107}
108
Chris Craik14e51302013-12-30 15:32:54 -0800109void StatefulBaseRenderer::getMatrix(SkMatrix* matrix) const {
110 mSnapshot->transform->copyTo(*matrix);
111}
112
113void StatefulBaseRenderer::translate(float dx, float dy, float dz) {
114 mSnapshot->transform->translate(dx, dy, dz);
115}
116
117void StatefulBaseRenderer::rotate(float degrees) {
118 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
119}
120
121void StatefulBaseRenderer::scale(float sx, float sy) {
122 mSnapshot->transform->scale(sx, sy, 1.0f);
123}
124
125void StatefulBaseRenderer::skew(float sx, float sy) {
126 mSnapshot->transform->skew(sx, sy);
127}
128
Derek Sollenberger13908822013-12-10 12:28:58 -0500129void StatefulBaseRenderer::setMatrix(const SkMatrix& matrix) {
130 mSnapshot->transform->load(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800131}
132
133void StatefulBaseRenderer::setMatrix(const Matrix4& matrix) {
134 mSnapshot->transform->load(matrix);
135}
136
Derek Sollenberger13908822013-12-10 12:28:58 -0500137void StatefulBaseRenderer::concatMatrix(const SkMatrix& matrix) {
138 mat4 transform(matrix);
Chris Craik14e51302013-12-30 15:32:54 -0800139 mSnapshot->transform->multiply(transform);
140}
141
142void StatefulBaseRenderer::concatMatrix(const Matrix4& matrix) {
143 mSnapshot->transform->multiply(matrix);
144}
145
146///////////////////////////////////////////////////////////////////////////////
147// Clip
148///////////////////////////////////////////////////////////////////////////////
149
Chris Craikd6b65f62014-01-01 14:45:21 -0800150bool StatefulBaseRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
151 if (CC_LIKELY(currentTransform()->rectToRect())) {
152 mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op);
153 return !mSnapshot->clipRect->isEmpty();
154 }
155
156 SkPath path;
157 path.addRect(left, top, right, bottom);
158
159 return StatefulBaseRenderer::clipPath(&path, op);
160}
161
Chris Craikd218a922014-01-02 17:13:34 -0800162bool StatefulBaseRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800163 SkMatrix transform;
164 currentTransform()->copyTo(transform);
165
166 SkPath transformed;
167 path->transform(transform, &transformed);
168
169 SkRegion clip;
170 if (!mSnapshot->previous->clipRegion->isEmpty()) {
171 clip.setRegion(*mSnapshot->previous->clipRegion);
172 } else {
173 if (mSnapshot->previous == firstSnapshot()) {
174 clip.setRect(0, 0, getWidth(), getHeight());
175 } else {
176 Rect* bounds = mSnapshot->previous->clipRect;
177 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
178 }
179 }
180
181 SkRegion region;
182 region.setPath(transformed, clip);
183
Chris Craik62d307c2014-07-29 10:35:13 -0700184 // region is the transformed input path, masked by the previous clip
Chris Craikd6b65f62014-01-01 14:45:21 -0800185 mDirtyClip |= mSnapshot->clipRegionTransformed(region, op);
186 return !mSnapshot->clipRect->isEmpty();
187}
188
Chris Craikd218a922014-01-02 17:13:34 -0800189bool StatefulBaseRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800190 mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op);
191 return !mSnapshot->clipRect->isEmpty();
192}
Chris Craik14e51302013-12-30 15:32:54 -0800193
Chris Craikdeeda3d2014-05-05 19:09:33 -0700194void StatefulBaseRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700195 Rect bounds;
196 float radius;
197 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
198
Chris Craik79d26c72014-08-21 12:26:16 -0700199 bool outlineIsRounded = MathUtils::isPositive(radius);
200 if (!outlineIsRounded || currentTransform()->isSimple()) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700201 // TODO: consider storing this rect separately, so that this can't be replaced with clip ops
202 clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
Chris Craikaf4d04c2014-07-29 12:50:14 -0700203 }
Chris Craik79d26c72014-08-21 12:26:16 -0700204 if (outlineIsRounded) {
205 setClippingRoundRect(allocator, bounds, radius);
206 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700207}
208
Chris Craikaf4d04c2014-07-29 12:50:14 -0700209void StatefulBaseRenderer::setClippingRoundRect(LinearAllocator& allocator,
210 const Rect& rect, float radius) {
211 mSnapshot->setClippingRoundRect(allocator, rect, radius);
212}
213
214
Chris Craik14e51302013-12-30 15:32:54 -0800215///////////////////////////////////////////////////////////////////////////////
216// Quick Rejection
217///////////////////////////////////////////////////////////////////////////////
218
219/**
220 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
221 * the clipRect. Does not modify the scissor.
222 *
223 * @param clipRequired if not null, will be set to true if element intersects clip
224 * (and wasn't rejected)
225 *
226 * @param snapOut if set, the geometry will be treated as having an AA ramp.
227 * See Rect::snapGeometryToPixelBoundaries()
228 */
229bool StatefulBaseRenderer::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700230 float right, float bottom,
231 bool* clipRequired, bool* roundRectClipRequired,
232 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800233 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
234 return true;
235 }
236
237 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800238 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800239 r.snapGeometryToPixelBoundaries(snapOut);
240
Chris Craikd6b65f62014-01-01 14:45:21 -0800241 Rect clipRect(*currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800242 clipRect.snapToPixelBoundaries();
243
244 if (!clipRect.intersects(r)) return true;
245
246 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700247 if (clipRequired) {
248 *clipRequired = !clipRect.contains(r);
249 }
250
251 // round rect clip is required if RR clip exists, and geometry intersects its corners
252 if (roundRectClipRequired) {
253 *roundRectClipRequired = mSnapshot->roundRectClipState != NULL
254 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
255 }
Chris Craik14e51302013-12-30 15:32:54 -0800256 return false;
257}
258
259/**
260 * Returns false if drawing won't be clipped out.
261 *
262 * Makes the decision conservatively, by rounding out the mapped rect before comparing with the
263 * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
264 * rejection is still desired.
265 *
266 * This function, unlike quickRejectSetupScissor, should be used where precise geometry information
267 * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
268 * rejection where precise rejection isn't important, or precise information isn't available.
269 */
270bool StatefulBaseRenderer::quickRejectConservative(float left, float top,
271 float right, float bottom) const {
272 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
273 return true;
274 }
275
276 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800277 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800278 r.roundOut(); // rounded out to be conservative
279
Chris Craikd6b65f62014-01-01 14:45:21 -0800280 Rect clipRect(*currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800281 clipRect.snapToPixelBoundaries();
282
283 if (!clipRect.intersects(r)) return true;
284
285 return false;
286}
287
288}; // namespace uirenderer
289}; // namespace android