StatefulBaseRenderer.cpp revision 139088228faa7f3c446af7387e017933998a5570
1/*
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#define LOG_TAG "OpenGLRenderer"
18
19#include <SkCanvas.h>
20
21#include "StatefulBaseRenderer.h"
22
23namespace android {
24namespace uirenderer {
25
26StatefulBaseRenderer::StatefulBaseRenderer() :
27        mDirtyClip(false), mWidth(-1), mHeight(-1),
28        mSaveCount(1), mFirstSnapshot(new Snapshot), mSnapshot(mFirstSnapshot),
29        mLightCenter(FLT_MIN, FLT_MIN, FLT_MIN), mLightRadius(FLT_MIN) {
30}
31
32void StatefulBaseRenderer::initializeSaveStack(float clipLeft, float clipTop,
33        float clipRight, float clipBottom) {
34    mSnapshot = new Snapshot(mFirstSnapshot,
35            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
36    mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
37    mSnapshot->fbo = getTargetFbo();
38    mSaveCount = 1;
39}
40
41void StatefulBaseRenderer::setViewport(int width, int height) {
42    mWidth = width;
43    mHeight = height;
44    mFirstSnapshot->initializeViewport(width, height);
45    onViewportInitialized();
46}
47
48void StatefulBaseRenderer::initializeLight(const Vector3& lightCenter, float lightRadius) {
49    mLightCenter = lightCenter;
50    mLightRadius = lightRadius;
51}
52
53///////////////////////////////////////////////////////////////////////////////
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
104void StatefulBaseRenderer::getMatrix(SkMatrix* matrix) const {
105    mSnapshot->transform->copyTo(*matrix);
106}
107
108void StatefulBaseRenderer::translate(float dx, float dy, float dz) {
109    mSnapshot->transform->translate(dx, dy, dz);
110}
111
112void StatefulBaseRenderer::rotate(float degrees) {
113    mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
114}
115
116void StatefulBaseRenderer::scale(float sx, float sy) {
117    mSnapshot->transform->scale(sx, sy, 1.0f);
118}
119
120void StatefulBaseRenderer::skew(float sx, float sy) {
121    mSnapshot->transform->skew(sx, sy);
122}
123
124void StatefulBaseRenderer::setMatrix(const SkMatrix& matrix) {
125    mSnapshot->transform->load(matrix);
126}
127
128void StatefulBaseRenderer::setMatrix(const Matrix4& matrix) {
129    mSnapshot->transform->load(matrix);
130}
131
132void StatefulBaseRenderer::concatMatrix(const SkMatrix& matrix) {
133    mat4 transform(matrix);
134    mSnapshot->transform->multiply(transform);
135}
136
137void StatefulBaseRenderer::concatMatrix(const Matrix4& matrix) {
138    mSnapshot->transform->multiply(matrix);
139}
140
141///////////////////////////////////////////////////////////////////////////////
142// Clip
143///////////////////////////////////////////////////////////////////////////////
144
145bool StatefulBaseRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
146    if (CC_LIKELY(currentTransform()->rectToRect())) {
147        mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op);
148        return !mSnapshot->clipRect->isEmpty();
149    }
150
151    SkPath path;
152    path.addRect(left, top, right, bottom);
153
154    return StatefulBaseRenderer::clipPath(&path, op);
155}
156
157bool StatefulBaseRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
158    SkMatrix transform;
159    currentTransform()->copyTo(transform);
160
161    SkPath transformed;
162    path->transform(transform, &transformed);
163
164    SkRegion clip;
165    if (!mSnapshot->previous->clipRegion->isEmpty()) {
166        clip.setRegion(*mSnapshot->previous->clipRegion);
167    } else {
168        if (mSnapshot->previous == firstSnapshot()) {
169            clip.setRect(0, 0, getWidth(), getHeight());
170        } else {
171            Rect* bounds = mSnapshot->previous->clipRect;
172            clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
173        }
174    }
175
176    SkRegion region;
177    region.setPath(transformed, clip);
178
179    mDirtyClip |= mSnapshot->clipRegionTransformed(region, op);
180    return !mSnapshot->clipRect->isEmpty();
181}
182
183bool StatefulBaseRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
184    mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op);
185    return !mSnapshot->clipRect->isEmpty();
186}
187
188void StatefulBaseRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
189    mSnapshot->setClippingOutline(allocator, outline);
190}
191
192///////////////////////////////////////////////////////////////////////////////
193// Quick Rejection
194///////////////////////////////////////////////////////////////////////////////
195
196/**
197 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
198 * the clipRect. Does not modify the scissor.
199 *
200 * @param clipRequired if not null, will be set to true if element intersects clip
201 *         (and wasn't rejected)
202 *
203 * @param snapOut if set, the geometry will be treated as having an AA ramp.
204 *         See Rect::snapGeometryToPixelBoundaries()
205 */
206bool StatefulBaseRenderer::calculateQuickRejectForScissor(float left, float top,
207        float right, float bottom,
208        bool* clipRequired, bool* roundRectClipRequired,
209        bool snapOut) const {
210    if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
211        return true;
212    }
213
214    Rect r(left, top, right, bottom);
215    currentTransform()->mapRect(r);
216    r.snapGeometryToPixelBoundaries(snapOut);
217
218    Rect clipRect(*currentClipRect());
219    clipRect.snapToPixelBoundaries();
220
221    if (!clipRect.intersects(r)) return true;
222
223    // clip is required if geometry intersects clip rect
224    if (clipRequired) {
225        *clipRequired = !clipRect.contains(r);
226    }
227
228    // round rect clip is required if RR clip exists, and geometry intersects its corners
229    if (roundRectClipRequired) {
230        *roundRectClipRequired = mSnapshot->roundRectClipState != NULL
231                && mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
232    }
233    return false;
234}
235
236/**
237 * Returns false if drawing won't be clipped out.
238 *
239 * Makes the decision conservatively, by rounding out the mapped rect before comparing with the
240 * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
241 * rejection is still desired.
242 *
243 * This function, unlike quickRejectSetupScissor, should be used where precise geometry information
244 * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
245 * rejection where precise rejection isn't important, or precise information isn't available.
246 */
247bool StatefulBaseRenderer::quickRejectConservative(float left, float top,
248        float right, float bottom) const {
249    if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
250        return true;
251    }
252
253    Rect r(left, top, right, bottom);
254    currentTransform()->mapRect(r);
255    r.roundOut(); // rounded out to be conservative
256
257    Rect clipRect(*currentClipRect());
258    clipRect.snapToPixelBoundaries();
259
260    if (!clipRect.intersects(r)) return true;
261
262    return false;
263}
264
265}; // namespace uirenderer
266}; // namespace android
267