blob: 7d299f09b131cd9f7a582bcef9fe7c5dbc53ad96 [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
23namespace android {
24namespace uirenderer {
25
26StatefulBaseRenderer::StatefulBaseRenderer() :
Chris Craikd6b65f62014-01-01 14:45:21 -080027 mDirtyClip(false), mWidth(-1), mHeight(-1),
Chris Craik14e51302013-12-30 15:32:54 -080028 mSaveCount(1), mFirstSnapshot(new Snapshot), mSnapshot(mFirstSnapshot) {
29}
30
31void StatefulBaseRenderer::initializeSaveStack(float clipLeft, float clipTop,
32 float clipRight, float clipBottom) {
33 mSnapshot = new Snapshot(mFirstSnapshot,
34 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
35 mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
36 mSnapshot->fbo = getTargetFbo();
37 mSaveCount = 1;
38}
39
40void StatefulBaseRenderer::initializeViewport(int width, int height) {
41 mWidth = width;
42 mHeight = height;
Chris Craika64a2be2014-05-14 14:17:01 -070043 mFirstSnapshot->initializeViewport(width, height);
Chris Craik14e51302013-12-30 15:32:54 -080044}
45
46///////////////////////////////////////////////////////////////////////////////
47// Save (layer)
48///////////////////////////////////////////////////////////////////////////////
49
50/**
51 * Non-virtual implementation of save, guaranteed to save without side-effects
52 *
53 * The approach here and in restoreSnapshot(), allows subclasses to directly manipulate the save
54 * stack, and ensures restoreToCount() doesn't call back into subclass overrides.
55 */
56int StatefulBaseRenderer::saveSnapshot(int flags) {
57 mSnapshot = new Snapshot(mSnapshot, flags);
58 return mSaveCount++;
59}
60
61int StatefulBaseRenderer::save(int flags) {
62 return saveSnapshot(flags);
63}
64
65/**
66 * Non-virtual implementation of restore, guaranteed to restore without side-effects.
67 */
68void StatefulBaseRenderer::restoreSnapshot() {
69 sp<Snapshot> toRemove = mSnapshot;
70 sp<Snapshot> toRestore = mSnapshot->previous;
71
72 mSaveCount--;
73 mSnapshot = toRestore;
74
75 // subclass handles restore implementation
76 onSnapshotRestored(*toRemove, *toRestore);
77}
78
79void StatefulBaseRenderer::restore() {
80 if (mSaveCount > 1) {
81 restoreSnapshot();
82 }
83}
84
85void StatefulBaseRenderer::restoreToCount(int saveCount) {
86 if (saveCount < 1) saveCount = 1;
87
88 while (mSaveCount > saveCount) {
89 restoreSnapshot();
90 }
91}
92
93///////////////////////////////////////////////////////////////////////////////
94// Matrix
95///////////////////////////////////////////////////////////////////////////////
96
97void StatefulBaseRenderer::getMatrix(SkMatrix* matrix) const {
98 mSnapshot->transform->copyTo(*matrix);
99}
100
101void StatefulBaseRenderer::translate(float dx, float dy, float dz) {
102 mSnapshot->transform->translate(dx, dy, dz);
103}
104
105void StatefulBaseRenderer::rotate(float degrees) {
106 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
107}
108
109void StatefulBaseRenderer::scale(float sx, float sy) {
110 mSnapshot->transform->scale(sx, sy, 1.0f);
111}
112
113void StatefulBaseRenderer::skew(float sx, float sy) {
114 mSnapshot->transform->skew(sx, sy);
115}
116
Chris Craikd218a922014-01-02 17:13:34 -0800117void StatefulBaseRenderer::setMatrix(const SkMatrix* matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800118 if (matrix) {
119 mSnapshot->transform->load(*matrix);
120 } else {
121 mSnapshot->transform->loadIdentity();
122 }
123}
124
125void StatefulBaseRenderer::setMatrix(const Matrix4& matrix) {
126 mSnapshot->transform->load(matrix);
127}
128
Chris Craikd218a922014-01-02 17:13:34 -0800129void StatefulBaseRenderer::concatMatrix(const SkMatrix* matrix) {
Chris Craik14e51302013-12-30 15:32:54 -0800130 mat4 transform(*matrix);
131 mSnapshot->transform->multiply(transform);
132}
133
134void StatefulBaseRenderer::concatMatrix(const Matrix4& matrix) {
135 mSnapshot->transform->multiply(matrix);
136}
137
138///////////////////////////////////////////////////////////////////////////////
139// Clip
140///////////////////////////////////////////////////////////////////////////////
141
Chris Craikd6b65f62014-01-01 14:45:21 -0800142bool StatefulBaseRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
143 if (CC_LIKELY(currentTransform()->rectToRect())) {
144 mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op);
145 return !mSnapshot->clipRect->isEmpty();
146 }
147
148 SkPath path;
149 path.addRect(left, top, right, bottom);
150
151 return StatefulBaseRenderer::clipPath(&path, op);
152}
153
Chris Craikd218a922014-01-02 17:13:34 -0800154bool StatefulBaseRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800155 SkMatrix transform;
156 currentTransform()->copyTo(transform);
157
158 SkPath transformed;
159 path->transform(transform, &transformed);
160
161 SkRegion clip;
162 if (!mSnapshot->previous->clipRegion->isEmpty()) {
163 clip.setRegion(*mSnapshot->previous->clipRegion);
164 } else {
165 if (mSnapshot->previous == firstSnapshot()) {
166 clip.setRect(0, 0, getWidth(), getHeight());
167 } else {
168 Rect* bounds = mSnapshot->previous->clipRect;
169 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
170 }
171 }
172
173 SkRegion region;
174 region.setPath(transformed, clip);
175
176 mDirtyClip |= mSnapshot->clipRegionTransformed(region, op);
177 return !mSnapshot->clipRect->isEmpty();
178}
179
Chris Craikd218a922014-01-02 17:13:34 -0800180bool StatefulBaseRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800181 mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op);
182 return !mSnapshot->clipRect->isEmpty();
183}
Chris Craik14e51302013-12-30 15:32:54 -0800184
Chris Craikdeeda3d2014-05-05 19:09:33 -0700185void StatefulBaseRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
186 mSnapshot->setClippingOutline(allocator, outline);
187}
188
Chris Craik14e51302013-12-30 15:32:54 -0800189///////////////////////////////////////////////////////////////////////////////
190// Quick Rejection
191///////////////////////////////////////////////////////////////////////////////
192
193/**
194 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
195 * the clipRect. Does not modify the scissor.
196 *
197 * @param clipRequired if not null, will be set to true if element intersects clip
198 * (and wasn't rejected)
199 *
200 * @param snapOut if set, the geometry will be treated as having an AA ramp.
201 * See Rect::snapGeometryToPixelBoundaries()
202 */
203bool StatefulBaseRenderer::calculateQuickRejectForScissor(float left, float top,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700204 float right, float bottom,
205 bool* clipRequired, bool* roundRectClipRequired,
206 bool snapOut) const {
Chris Craik14e51302013-12-30 15:32:54 -0800207 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
208 return true;
209 }
210
211 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800212 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800213 r.snapGeometryToPixelBoundaries(snapOut);
214
Chris Craikd6b65f62014-01-01 14:45:21 -0800215 Rect clipRect(*currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800216 clipRect.snapToPixelBoundaries();
217
218 if (!clipRect.intersects(r)) return true;
219
220 // clip is required if geometry intersects clip rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700221 if (clipRequired) {
222 *clipRequired = !clipRect.contains(r);
223 }
224
225 // round rect clip is required if RR clip exists, and geometry intersects its corners
226 if (roundRectClipRequired) {
227 *roundRectClipRequired = mSnapshot->roundRectClipState != NULL
228 && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
229 }
Chris Craik14e51302013-12-30 15:32:54 -0800230 return false;
231}
232
233/**
234 * Returns false if drawing won't be clipped out.
235 *
236 * Makes the decision conservatively, by rounding out the mapped rect before comparing with the
237 * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
238 * rejection is still desired.
239 *
240 * This function, unlike quickRejectSetupScissor, should be used where precise geometry information
241 * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
242 * rejection where precise rejection isn't important, or precise information isn't available.
243 */
244bool StatefulBaseRenderer::quickRejectConservative(float left, float top,
245 float right, float bottom) const {
246 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
247 return true;
248 }
249
250 Rect r(left, top, right, bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800251 currentTransform()->mapRect(r);
Chris Craik14e51302013-12-30 15:32:54 -0800252 r.roundOut(); // rounded out to be conservative
253
Chris Craikd6b65f62014-01-01 14:45:21 -0800254 Rect clipRect(*currentClipRect());
Chris Craik14e51302013-12-30 15:32:54 -0800255 clipRect.snapToPixelBoundaries();
256
257 if (!clipRect.intersects(r)) return true;
258
259 return false;
260}
261
262}; // namespace uirenderer
263}; // namespace android