blob: 6f19275d0603f4cd286f553743c20b30aeab800b [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
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Constructors
28///////////////////////////////////////////////////////////////////////////////
29
Chris Craike4aa95e2014-05-08 13:57:05 -070030Snapshot::Snapshot()
31 : flags(0)
32 , previous(NULL)
33 , layer(NULL)
34 , fbo(0)
35 , invisible(false)
36 , empty(false)
Chris Craikdeeda3d2014-05-05 19:09:33 -070037 , alpha(1.0f)
38 , roundRectClipState(NULL) {
Romain Guyada4d532012-02-02 17:31:16 -080039 transform = &mTransformRoot;
40 clipRect = &mClipRectRoot;
41 region = NULL;
Romain Guy8ce00302013-01-15 18:51:42 -080042 clipRegion = &mClipRegionRoot;
Romain Guyada4d532012-02-02 17:31:16 -080043}
44
45/**
46 * Copies the specified snapshot/ The specified snapshot is stored as
47 * the previous snapshot.
48 */
Chris Craike4aa95e2014-05-08 13:57:05 -070049Snapshot::Snapshot(const sp<Snapshot>& s, int saveFlags)
50 : flags(0)
51 , previous(s)
52 , layer(s->layer)
53 , fbo(s->fbo)
54 , invisible(s->invisible)
55 , empty(false)
Chris Craika64a2be2014-05-14 14:17:01 -070056 , alpha(s->alpha)
Chris Craikdeeda3d2014-05-05 19:09:33 -070057 , roundRectClipState(s->roundRectClipState)
Chris Craika64a2be2014-05-14 14:17:01 -070058 , mViewportData(s->mViewportData) {
Romain Guyada4d532012-02-02 17:31:16 -080059 if (saveFlags & SkCanvas::kMatrix_SaveFlag) {
60 mTransformRoot.load(*s->transform);
61 transform = &mTransformRoot;
62 } else {
63 transform = s->transform;
64 }
65
66 if (saveFlags & SkCanvas::kClip_SaveFlag) {
67 mClipRectRoot.set(*s->clipRect);
68 clipRect = &mClipRectRoot;
Romain Guy8ce00302013-01-15 18:51:42 -080069 if (!s->clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070070 mClipRegionRoot.op(*s->clipRegion, SkRegion::kUnion_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -080071 }
Romain Guy8ce00302013-01-15 18:51:42 -080072 clipRegion = &mClipRegionRoot;
Romain Guyada4d532012-02-02 17:31:16 -080073 } else {
74 clipRect = s->clipRect;
Romain Guy967e2bf2012-02-07 17:04:34 -080075 clipRegion = s->clipRegion;
Romain Guyada4d532012-02-02 17:31:16 -080076 }
77
78 if (s->flags & Snapshot::kFlagFboTarget) {
79 flags |= Snapshot::kFlagFboTarget;
80 region = s->region;
81 } else {
82 region = NULL;
83 }
84}
85
86///////////////////////////////////////////////////////////////////////////////
87// Clipping
88///////////////////////////////////////////////////////////////////////////////
89
Romain Guy967e2bf2012-02-07 17:04:34 -080090void Snapshot::ensureClipRegion() {
Romain Guy8ce00302013-01-15 18:51:42 -080091 if (clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070092 clipRegion->setRect(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Romain Guy967e2bf2012-02-07 17:04:34 -080093 }
Romain Guy967e2bf2012-02-07 17:04:34 -080094}
95
96void Snapshot::copyClipRectFromRegion() {
Romain Guy967e2bf2012-02-07 17:04:34 -080097 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070098 const SkIRect& bounds = clipRegion->getBounds();
99 clipRect->set(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
Romain Guy967e2bf2012-02-07 17:04:34 -0800100
101 if (clipRegion->isRect()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700102 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800103 }
104 } else {
105 clipRect->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800106 }
Romain Guy967e2bf2012-02-07 17:04:34 -0800107}
108
Romain Guy0baaac52012-08-31 20:31:01 -0700109bool Snapshot::clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy0baaac52012-08-31 20:31:01 -0700110 SkIRect tmp;
111 tmp.set(left, top, right, bottom);
112 clipRegion->op(tmp, op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800113 copyClipRectFromRegion();
114 return true;
Romain Guy8ce00302013-01-15 18:51:42 -0800115}
116
117bool Snapshot::clipRegionTransformed(const SkRegion& region, SkRegion::Op op) {
118 ensureClipRegion();
119 clipRegion->op(region, op);
120 copyClipRectFromRegion();
121 flags |= Snapshot::kFlagClipSet;
122 return true;
Romain Guy967e2bf2012-02-07 17:04:34 -0800123}
124
Romain Guyada4d532012-02-02 17:31:16 -0800125bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::Op op) {
126 Rect r(left, top, right, bottom);
127 transform->mapRect(r);
128 return clipTransformed(r, op);
129}
130
131bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) {
132 bool clipped = false;
133
Romain Guyada4d532012-02-02 17:31:16 -0800134 switch (op) {
Romain Guy967e2bf2012-02-07 17:04:34 -0800135 case SkRegion::kIntersect_Op: {
Romain Guy8ce00302013-01-15 18:51:42 -0800136 if (CC_UNLIKELY(!clipRegion->isEmpty())) {
Romain Guy735738c2012-12-03 12:34:51 -0800137 ensureClipRegion();
Romain Guy0baaac52012-08-31 20:31:01 -0700138 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, SkRegion::kIntersect_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800139 } else {
140 clipped = clipRect->intersect(r);
141 if (!clipped) {
142 clipRect->setEmpty();
143 clipped = true;
144 }
Romain Guyada4d532012-02-02 17:31:16 -0800145 }
146 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800147 }
Romain Guy967e2bf2012-02-07 17:04:34 -0800148 case SkRegion::kReplace_Op: {
149 setClip(r.left, r.top, r.right, r.bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800150 clipped = true;
151 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800152 }
Romain Guy0baaac52012-08-31 20:31:01 -0700153 default: {
154 ensureClipRegion();
155 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, op);
156 break;
157 }
Romain Guyada4d532012-02-02 17:31:16 -0800158 }
159
160 if (clipped) {
161 flags |= Snapshot::kFlagClipSet;
162 }
163
164 return clipped;
165}
166
167void Snapshot::setClip(float left, float top, float right, float bottom) {
168 clipRect->set(left, top, right, bottom);
Romain Guy8ce00302013-01-15 18:51:42 -0800169 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700170 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800171 }
Romain Guyada4d532012-02-02 17:31:16 -0800172 flags |= Snapshot::kFlagClipSet;
173}
174
Romain Guya3dc55f2012-09-28 13:55:44 -0700175bool Snapshot::hasPerspectiveTransform() const {
176 return transform->isPerspective();
177}
178
Romain Guyada4d532012-02-02 17:31:16 -0800179const Rect& Snapshot::getLocalClip() {
180 mat4 inverse;
181 inverse.loadInverse(*transform);
182
183 mLocalClip.set(*clipRect);
184 inverse.mapRect(mLocalClip);
185
186 return mLocalClip;
187}
188
189void Snapshot::resetClip(float left, float top, float right, float bottom) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800190 // TODO: This is incorrect, when we start rendering into a new layer,
191 // we may have to modify the previous snapshot's clip rect and clip
192 // region if the previous restore() call did not restore the clip
Romain Guyada4d532012-02-02 17:31:16 -0800193 clipRect = &mClipRectRoot;
Romain Guy3c099c42013-02-06 15:28:04 -0800194 clipRegion = &mClipRegionRoot;
Romain Guy967e2bf2012-02-07 17:04:34 -0800195 setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800196}
197
198///////////////////////////////////////////////////////////////////////////////
199// Transforms
200///////////////////////////////////////////////////////////////////////////////
201
202void Snapshot::resetTransform(float x, float y, float z) {
203 transform = &mTransformRoot;
204 transform->loadTranslate(x, y, z);
205}
206
207///////////////////////////////////////////////////////////////////////////////
Chris Craikaf4d04c2014-07-29 12:50:14 -0700208// Clipping round rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700209///////////////////////////////////////////////////////////////////////////////
210
Chris Craikaf4d04c2014-07-29 12:50:14 -0700211void Snapshot::setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds, float radius) {
212 if (bounds.isEmpty()) {
213 clipRect->setEmpty();
214 return;
215 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700216
217 RoundRectClipState* state = new (allocator) RoundRectClipState;
218
219 // store the inverse drawing matrix
Chris Craikaf4d04c2014-07-29 12:50:14 -0700220 Matrix4 roundRectDrawingMatrix;
221 roundRectDrawingMatrix.load(getOrthoMatrix());
222 roundRectDrawingMatrix.multiply(*transform);
223 state->matrix.loadInverse(roundRectDrawingMatrix);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700224
225 // compute area under rounded corners - only draws overlapping these rects need to be clipped
226 for (int i = 0 ; i < 4; i++) {
227 state->dangerRects[i] = bounds;
228 }
229 state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius;
230 state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius;
231 state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius;
232 state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius;
233 for (int i = 0; i < 4; i++) {
234 transform->mapRect(state->dangerRects[i]);
235
236 // round danger rects out as though they are AA geometry (since they essentially are)
237 state->dangerRects[i].snapGeometryToPixelBoundaries(true);
238 }
239
240 // store RR area
Chris Craikaf4d04c2014-07-29 12:50:14 -0700241 state->innerRect = bounds;
242 state->innerRect.inset(radius);
243 state->radius = radius;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700244
245 // store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info
246 roundRectClipState = state;
247}
248
249///////////////////////////////////////////////////////////////////////////////
Romain Guyada4d532012-02-02 17:31:16 -0800250// Queries
251///////////////////////////////////////////////////////////////////////////////
252
253bool Snapshot::isIgnored() const {
254 return invisible || empty;
255}
256
Chris Craik5f803622013-03-21 14:39:04 -0700257void Snapshot::dump() const {
258 ALOGD("Snapshot %p, flags %x, prev %p, height %d, ignored %d, hasComplexClip %d",
Chris Craika64a2be2014-05-14 14:17:01 -0700259 this, flags, previous.get(), getViewportHeight(), isIgnored(), clipRegion && !clipRegion->isEmpty());
Chris Craik5f803622013-03-21 14:39:04 -0700260 ALOGD(" ClipRect (at %p) %.1f %.1f %.1f %.1f",
261 clipRect, clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
262 ALOGD(" Transform (at %p):", transform);
263 transform->dump();
264}
265
Romain Guyada4d532012-02-02 17:31:16 -0800266}; // namespace uirenderer
267}; // namespace android