blob: 6bfa2033f8fcf09645b064e492b0792f19545cc3 [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)
37 , height(0)
38 , alpha(1.0f) {
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)
56 , viewport(s->viewport)
57 , height(s->height)
58 , alpha(s->alpha) {
Romain Guyada4d532012-02-02 17:31:16 -080059
60 if (saveFlags & SkCanvas::kMatrix_SaveFlag) {
61 mTransformRoot.load(*s->transform);
62 transform = &mTransformRoot;
63 } else {
64 transform = s->transform;
65 }
66
67 if (saveFlags & SkCanvas::kClip_SaveFlag) {
68 mClipRectRoot.set(*s->clipRect);
69 clipRect = &mClipRectRoot;
Romain Guy8ce00302013-01-15 18:51:42 -080070 if (!s->clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070071 mClipRegionRoot.op(*s->clipRegion, SkRegion::kUnion_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -080072 }
Romain Guy8ce00302013-01-15 18:51:42 -080073 clipRegion = &mClipRegionRoot;
Romain Guyada4d532012-02-02 17:31:16 -080074 } else {
75 clipRect = s->clipRect;
Romain Guy967e2bf2012-02-07 17:04:34 -080076 clipRegion = s->clipRegion;
Romain Guyada4d532012-02-02 17:31:16 -080077 }
78
79 if (s->flags & Snapshot::kFlagFboTarget) {
80 flags |= Snapshot::kFlagFboTarget;
81 region = s->region;
82 } else {
83 region = NULL;
84 }
85}
86
87///////////////////////////////////////////////////////////////////////////////
88// Clipping
89///////////////////////////////////////////////////////////////////////////////
90
Romain Guy967e2bf2012-02-07 17:04:34 -080091void Snapshot::ensureClipRegion() {
Romain Guy8ce00302013-01-15 18:51:42 -080092 if (clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070093 clipRegion->setRect(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Romain Guy967e2bf2012-02-07 17:04:34 -080094 }
Romain Guy967e2bf2012-02-07 17:04:34 -080095}
96
97void Snapshot::copyClipRectFromRegion() {
Romain Guy967e2bf2012-02-07 17:04:34 -080098 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -070099 const SkIRect& bounds = clipRegion->getBounds();
100 clipRect->set(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
Romain Guy967e2bf2012-02-07 17:04:34 -0800101
102 if (clipRegion->isRect()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700103 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800104 }
105 } else {
106 clipRect->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800107 }
Romain Guy967e2bf2012-02-07 17:04:34 -0800108}
109
Romain Guy0baaac52012-08-31 20:31:01 -0700110bool Snapshot::clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy0baaac52012-08-31 20:31:01 -0700111 SkIRect tmp;
112 tmp.set(left, top, right, bottom);
113 clipRegion->op(tmp, op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800114 copyClipRectFromRegion();
115 return true;
Romain Guy8ce00302013-01-15 18:51:42 -0800116}
117
118bool Snapshot::clipRegionTransformed(const SkRegion& region, SkRegion::Op op) {
119 ensureClipRegion();
120 clipRegion->op(region, op);
121 copyClipRectFromRegion();
122 flags |= Snapshot::kFlagClipSet;
123 return true;
Romain Guy967e2bf2012-02-07 17:04:34 -0800124}
125
Romain Guyada4d532012-02-02 17:31:16 -0800126bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::Op op) {
127 Rect r(left, top, right, bottom);
128 transform->mapRect(r);
129 return clipTransformed(r, op);
130}
131
132bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) {
133 bool clipped = false;
134
Romain Guyada4d532012-02-02 17:31:16 -0800135 switch (op) {
Romain Guy967e2bf2012-02-07 17:04:34 -0800136 case SkRegion::kIntersect_Op: {
Romain Guy8ce00302013-01-15 18:51:42 -0800137 if (CC_UNLIKELY(!clipRegion->isEmpty())) {
Romain Guy735738c2012-12-03 12:34:51 -0800138 ensureClipRegion();
Romain Guy0baaac52012-08-31 20:31:01 -0700139 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, SkRegion::kIntersect_Op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800140 } else {
141 clipped = clipRect->intersect(r);
142 if (!clipped) {
143 clipRect->setEmpty();
144 clipped = true;
145 }
Romain Guyada4d532012-02-02 17:31:16 -0800146 }
147 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800148 }
Romain Guy967e2bf2012-02-07 17:04:34 -0800149 case SkRegion::kReplace_Op: {
150 setClip(r.left, r.top, r.right, r.bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800151 clipped = true;
152 break;
Romain Guy967e2bf2012-02-07 17:04:34 -0800153 }
Romain Guy0baaac52012-08-31 20:31:01 -0700154 default: {
155 ensureClipRegion();
156 clipped = clipRegionOp(r.left, r.top, r.right, r.bottom, op);
157 break;
158 }
Romain Guyada4d532012-02-02 17:31:16 -0800159 }
160
161 if (clipped) {
162 flags |= Snapshot::kFlagClipSet;
163 }
164
165 return clipped;
166}
167
168void Snapshot::setClip(float left, float top, float right, float bottom) {
169 clipRect->set(left, top, right, bottom);
Romain Guy8ce00302013-01-15 18:51:42 -0800170 if (!clipRegion->isEmpty()) {
Romain Guy0baaac52012-08-31 20:31:01 -0700171 clipRegion->setEmpty();
Romain Guy967e2bf2012-02-07 17:04:34 -0800172 }
Romain Guyada4d532012-02-02 17:31:16 -0800173 flags |= Snapshot::kFlagClipSet;
174}
175
Romain Guya3dc55f2012-09-28 13:55:44 -0700176bool Snapshot::hasPerspectiveTransform() const {
177 return transform->isPerspective();
178}
179
Romain Guyada4d532012-02-02 17:31:16 -0800180const Rect& Snapshot::getLocalClip() {
181 mat4 inverse;
182 inverse.loadInverse(*transform);
183
184 mLocalClip.set(*clipRect);
185 inverse.mapRect(mLocalClip);
186
187 return mLocalClip;
188}
189
190void Snapshot::resetClip(float left, float top, float right, float bottom) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800191 // TODO: This is incorrect, when we start rendering into a new layer,
192 // we may have to modify the previous snapshot's clip rect and clip
193 // region if the previous restore() call did not restore the clip
Romain Guyada4d532012-02-02 17:31:16 -0800194 clipRect = &mClipRectRoot;
Romain Guy3c099c42013-02-06 15:28:04 -0800195 clipRegion = &mClipRegionRoot;
Romain Guy967e2bf2012-02-07 17:04:34 -0800196 setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800197}
198
199///////////////////////////////////////////////////////////////////////////////
200// Transforms
201///////////////////////////////////////////////////////////////////////////////
202
203void Snapshot::resetTransform(float x, float y, float z) {
204 transform = &mTransformRoot;
205 transform->loadTranslate(x, y, z);
206}
207
208///////////////////////////////////////////////////////////////////////////////
209// Queries
210///////////////////////////////////////////////////////////////////////////////
211
212bool Snapshot::isIgnored() const {
213 return invisible || empty;
214}
215
Chris Craik5f803622013-03-21 14:39:04 -0700216void Snapshot::dump() const {
217 ALOGD("Snapshot %p, flags %x, prev %p, height %d, ignored %d, hasComplexClip %d",
218 this, flags, previous.get(), height, isIgnored(), clipRegion && !clipRegion->isEmpty());
219 ALOGD(" ClipRect (at %p) %.1f %.1f %.1f %.1f",
220 clipRect, clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
221 ALOGD(" Transform (at %p):", transform);
222 transform->dump();
223}
224
Romain Guyada4d532012-02-02 17:31:16 -0800225}; // namespace uirenderer
226}; // namespace android