blob: 80f7eca2cb9f7586c22f51cfd6181e221ae6619d [file] [log] [blame]
Romain Guyada4d532012-02-02 17:31:16 -08001/*
2 * Copyright (C) 2012 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 Craik5f803622013-03-21 14:39:04 -070017#define LOG_TAG "OpenGLRenderer"
18
Romain Guyada4d532012-02-02 17:31:16 -080019#include "Snapshot.h"
20
21#include <SkCanvas.h>
22
Chris Craikdeeda3d2014-05-05 19:09:33 -070023#include "utils/MathUtils.h"
24
Romain Guyada4d532012-02-02 17:31:16 -080025namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Constructors
30///////////////////////////////////////////////////////////////////////////////
31
Chris Craike4aa95e2014-05-08 13:57:05 -070032Snapshot::Snapshot()
33 : flags(0)
34 , previous(NULL)
35 , layer(NULL)
36 , fbo(0)
37 , invisible(false)
38 , empty(false)
Chris Craikdeeda3d2014-05-05 19:09:33 -070039 , alpha(1.0f)
40 , roundRectClipState(NULL) {
Romain Guyada4d532012-02-02 17:31:16 -080041 transform = &mTransformRoot;
42 clipRect = &mClipRectRoot;
43 region = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -080044 clipRegion = &mClipRegionRoot;
Romain Guyada4d532012-02-02 17:31:16 -080045}
46
47/**
48 * Copies the specified snapshot/ The specified snapshot is stored as
49 * the previous snapshot.
50 */
Chris Craike4aa95e2014-05-08 13:57:05 -070051Snapshot::Snapshot(const sp<Snapshot>& s, int saveFlags)
52 : flags(0)
53 , previous(s)
54 , layer(s->layer)
55 , fbo(s->fbo)
56 , invisible(s->invisible)
57 , empty(false)
Chris Craika64a2be2014-05-14 14:17:01 -070058 , alpha(s->alpha)
Chris Craikdeeda3d2014-05-05 19:09:33 -070059 , roundRectClipState(s->roundRectClipState)
Chris Craika64a2be2014-05-14 14:17:01 -070060 , mViewportData(s->mViewportData) {
Romain Guyada4d532012-02-02 17:31:16 -080061 if (saveFlags & SkCanvas::kMatrix_SaveFlag) {
62 mTransformRoot.load(*s->transform);
63 transform = &mTransformRoot;
64 } else {
65 transform = s->transform;
66 }
67
68 if (saveFlags & SkCanvas::kClip_SaveFlag) {
69 mClipRectRoot.set(*s->clipRect);
70 clipRect = &mClipRectRoot;
Romain Guy8ce00302013-01-15 18:51:42 -080071 if (!s->clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070072 mClipRegionRoot.op(*s->clipRegion, SkRegion::kUnion_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -080073 }
Romain Guy8ce00302013-01-15 18:51:42 -080074 clipRegion = &mClipRegionRoot;
Romain Guyada4d532012-02-02 17:31:16 -080075 } else {
76 clipRect = s->clipRect;
Romain Guy967e2bf2012-02-07 17:04:34 -080077 clipRegion = s->clipRegion;
Romain Guyada4d532012-02-02 17:31:16 -080078 }
79
80 if (s->flags & Snapshot::kFlagFboTarget) {
81 flags |= Snapshot::kFlagFboTarget;
82 region = s->region;
83 } else {
84 region = NULL;
85 }
86}
87
88///////////////////////////////////////////////////////////////////////////////
89// Clipping
90///////////////////////////////////////////////////////////////////////////////
91
Romain Guy967e2bf2012-02-07 17:04:34 -080092void Snapshot::ensureClipRegion() {
Romain Guy8ce00302013-01-15 18:51:42 -080093 if (clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070094 clipRegion->setRect(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Romain Guy967e2bf2012-02-07 17:04:34 -080095 }
Romain Guy967e2bf2012-02-07 17:04:34 -080096}
97
98void Snapshot::copyClipRectFromRegion() {
Romain Guy967e2bf2012-02-07 17:04:34 -080099 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700100 const SkIRect& bounds = clipRegion->getBounds();
101 clipRect->set(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
Romain Guy967e2bf2012-02-07 17:04:34 -0800102
103 if (clipRegion->isRect()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700104 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800105 }
106 } else {
107 clipRect->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800108 }
Romain Guy967e2bf2012-02-07 17:04:34 -0800109}
110
Romain Guy0baaac52012-08-31 20:31:01 -0700111bool Snapshot::clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy0baaac52012-08-31 20:31:01 -0700112 SkIRect tmp;
113 tmp.set(left, top, right, bottom);
114 clipRegion->op(tmp, op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800115 copyClipRectFromRegion();
116 return true;
Romain Guy8ce00302013-01-15 18:51:42 -0800117}
118
119bool Snapshot::clipRegionTransformed(const SkRegion& region, SkRegion::Op op) {
120 ensureClipRegion();
121 clipRegion->op(region, op);
122 copyClipRectFromRegion();
123 flags |= Snapshot::kFlagClipSet;
124 return true;
Romain Guy967e2bf2012-02-07 17:04:34 -0800125}
126
Romain Guyada4d532012-02-02 17:31:16 -0800127bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::Op op) {
128 Rect r(left, top, right, bottom);
129 transform->mapRect(r);
130 return clipTransformed(r, op);
131}
132
133bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) {
134 bool clipped = false;
135
Romain Guyada4d532012-02-02 17:31:16 -0800136 switch (op) {
Romain Guy967e2bf2012-02-07 17:04:34 -0800137 case SkRegion::kIntersect_Op: {
Romain Guy8ce00302013-01-15 18:51:42 -0800138 if (CC_UNLIKELY(!clipRegion->isEmpty())) {
Romain Guy735738c2012-12-03 12:34:51 -0800139 ensureClipRegion();
Romain Guy0baaac52012-08-31 20:31:01 -0700140 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, SkRegion::kIntersect_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800141 } else {
142 clipped = clipRect->intersect(r);
143 if (!clipped) {
144 clipRect->setEmpty();
145 clipped = true;
146 }
Romain Guyada4d532012-02-02 17:31:16 -0800147 }
148 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800149 }
Romain Guy967e2bf2012-02-07 17:04:34 -0800150 case SkRegion::kReplace_Op: {
151 setClip(r.left, r.top, r.right, r.bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800152 clipped = true;
153 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800154 }
Romain Guy0baaac52012-08-31 20:31:01 -0700155 default: {
156 ensureClipRegion();
157 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, op);
158 break;
159 }
Romain Guyada4d532012-02-02 17:31:16 -0800160 }
161
162 if (clipped) {
163 flags |= Snapshot::kFlagClipSet;
164 }
165
166 return clipped;
167}
168
169void Snapshot::setClip(float left, float top, float right, float bottom) {
170 clipRect->set(left, top, right, bottom);
Romain Guy8ce00302013-01-15 18:51:42 -0800171 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700172 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800173 }
Romain Guyada4d532012-02-02 17:31:16 -0800174 flags |= Snapshot::kFlagClipSet;
175}
176
Romain Guya3dc55f2012-09-28 13:55:44 -0700177bool Snapshot::hasPerspectiveTransform() const {
178 return transform->isPerspective();
179}
180
Romain Guyada4d532012-02-02 17:31:16 -0800181const Rect& Snapshot::getLocalClip() {
182 mat4 inverse;
183 inverse.loadInverse(*transform);
184
185 mLocalClip.set(*clipRect);
186 inverse.mapRect(mLocalClip);
187
188 return mLocalClip;
189}
190
191void Snapshot::resetClip(float left, float top, float right, float bottom) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800192 // TODO: This is incorrect, when we start rendering into a new layer,
193 // we may have to modify the previous snapshot's clip rect and clip
194 // region if the previous restore() call did not restore the clip
Romain Guyada4d532012-02-02 17:31:16 -0800195 clipRect = &mClipRectRoot;
Romain Guy3c099c42013-02-06 15:28:04 -0800196 clipRegion = &mClipRegionRoot;
Romain Guy967e2bf2012-02-07 17:04:34 -0800197 setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800198}
199
200///////////////////////////////////////////////////////////////////////////////
201// Transforms
202///////////////////////////////////////////////////////////////////////////////
203
204void Snapshot::resetTransform(float x, float y, float z) {
205 transform = &mTransformRoot;
206 transform->loadTranslate(x, y, z);
207}
208
209///////////////////////////////////////////////////////////////////////////////
Chris Craikdeeda3d2014-05-05 19:09:33 -0700210// Clipping outline
211///////////////////////////////////////////////////////////////////////////////
212
213void Snapshot::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
214 Rect bounds;
215 float radius;
216 if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
217
218 if (!MathUtils::isPositive(radius)) return; // leave clipping up to rect clipping
219
220 RoundRectClipState* state = new (allocator) RoundRectClipState;
221
222 // store the inverse drawing matrix
223 Matrix4 outlineDrawingMatrix;
224 outlineDrawingMatrix.load(getOrthoMatrix());
225 outlineDrawingMatrix.multiply(*transform);
226 state->matrix.loadInverse(outlineDrawingMatrix);
227
228 // compute area under rounded corners - only draws overlapping these rects need to be clipped
229 for (int i = 0 ; i < 4; i++) {
230 state->dangerRects[i] = bounds;
231 }
232 state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius;
233 state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius;
234 state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius;
235 state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius;
236 for (int i = 0; i < 4; i++) {
237 transform->mapRect(state->dangerRects[i]);
238
239 // round danger rects out as though they are AA geometry (since they essentially are)
240 state->dangerRects[i].snapGeometryToPixelBoundaries(true);
241 }
242
243 // store RR area
244 bounds.inset(radius);
245 state->outlineInnerRect = bounds;
246 state->outlineRadius = radius;
247
248 // store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info
249 roundRectClipState = state;
250}
251
252///////////////////////////////////////////////////////////////////////////////
Romain Guyada4d532012-02-02 17:31:16 -0800253// Queries
254///////////////////////////////////////////////////////////////////////////////
255
256bool Snapshot::isIgnored() const {
257 return invisible || empty;
258}
259
Chris Craik5f803622013-03-21 14:39:04 -0700260void Snapshot::dump() const {
261 ALOGD("Snapshot %p, flags %x, prev %p, height %d, ignored %d, hasComplexClip %d",
Chris Craika64a2be2014-05-14 14:17:01 -0700262 this, flags, previous.get(), getViewportHeight(), isIgnored(), clipRegion && !clipRegion->isEmpty());
Chris Craik5f803622013-03-21 14:39:04 -0700263 ALOGD(" ClipRect (at %p) %.1f %.1f %.1f %.1f",
264 clipRect, clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
265 ALOGD(" Transform (at %p):", transform);
266 transform->dump();
267}
268
Romain Guyada4d532012-02-02 17:31:16 -0800269}; // namespace uirenderer
270}; // namespace android