blob: 3d74b4c6bd44985a10f32eae2fac6d0c92407a6a [file] [log] [blame]
Romain Guy5cbbce52010-06-27 22:59:20 -07001/*
2 * Copyright (C) 2010 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#ifndef ANDROID_UI_SNAPSHOT_H
18#define ANDROID_UI_SNAPSHOT_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <utils/RefBase.h>
24
Romain Guy8aef54f2010-09-01 15:13:49 -070025#include <SkCanvas.h>
Romain Guy079ba2c2010-07-16 14:12:24 -070026#include <SkRegion.h>
27
Romain Guydda57022010-07-06 11:39:32 -070028#include "Layer.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070029#include "Matrix.h"
30#include "Rect.h"
31
32namespace android {
33namespace uirenderer {
34
35/**
36 * A snapshot holds information about the current state of the rendering
37 * surface. A snapshot is usually created whenever the user calls save()
38 * and discarded when the user calls restore(). Once a snapshot is created,
39 * it can hold information for deferred rendering.
40 *
41 * Each snapshot has a link to a previous snapshot, indicating the previous
42 * state of the renderer.
43 */
44class Snapshot: public LightRefBase<Snapshot> {
45public:
Romain Guyeb993562010-10-05 18:14:38 -070046 Snapshot(): flags(0), previous(NULL), layer(NULL), fbo(0) {
Romain Guy8aef54f2010-09-01 15:13:49 -070047 transform = &mTransformRoot;
48 clipRect = &mClipRectRoot;
49 }
Romain Guy5cbbce52010-06-27 22:59:20 -070050
51 /**
Romain Guy8aef54f2010-09-01 15:13:49 -070052 * Copies the specified snapshot/ The specified snapshot is stored as
53 * the previous snapshot.
Romain Guy5cbbce52010-06-27 22:59:20 -070054 */
Romain Guy8aef54f2010-09-01 15:13:49 -070055 Snapshot(const sp<Snapshot>& s, int saveFlags):
Romain Guyeb993562010-10-05 18:14:38 -070056 flags(0), previous(s), layer(NULL),
57 fbo(s->fbo), viewport(s->viewport), height(s->height) {
Romain Guy8aef54f2010-09-01 15:13:49 -070058 if (saveFlags & SkCanvas::kMatrix_SaveFlag) {
59 mTransformRoot.load(*s->transform);
60 transform = &mTransformRoot;
61 } else {
62 transform = s->transform;
63 }
64
65 if (saveFlags & SkCanvas::kClip_SaveFlag) {
66 mClipRectRoot.set(*s->clipRect);
67 clipRect = &mClipRectRoot;
68 } else {
69 clipRect = s->clipRect;
70 }
71
Romain Guyb82da652010-07-30 11:36:12 -070072 if ((s->flags & Snapshot::kFlagClipSet) &&
73 !(s->flags & Snapshot::kFlagDirtyLocalClip)) {
Romain Guy8aef54f2010-09-01 15:13:49 -070074 mLocalClip.set(s->mLocalClip);
Romain Guyb82da652010-07-30 11:36:12 -070075 } else {
76 flags |= Snapshot::kFlagDirtyLocalClip;
77 }
Romain Guy5cbbce52010-06-27 22:59:20 -070078 }
79
80 /**
81 * Various flags set on #flags.
82 */
83 enum Flags {
84 /**
85 * Indicates that the clip region was modified. When this
86 * snapshot is restored so must the clip.
87 */
88 kFlagClipSet = 0x1,
89 /**
Romain Guy5cbbce52010-06-27 22:59:20 -070090 * Indicates that this snapshot was created when saving
91 * a new layer.
92 */
Romain Guy079ba2c2010-07-16 14:12:24 -070093 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070094 /**
Romain Guyeb993562010-10-05 18:14:38 -070095 * Indicates that this snapshot is a special type of layer
96 * backed by an FBO. This flag only makes sense when the
97 * flag kFlagIsLayer is also set.
98 */
99 kFlagIsFboLayer = 0x4,
100 /**
Romain Guy09147fb2010-07-22 13:08:20 -0700101 * Indicates that the local clip should be recomputed.
102 */
Romain Guyeb993562010-10-05 18:14:38 -0700103 kFlagDirtyLocalClip = 0x8,
104 /**
105 * Indicates that this snapshot has changed the ortho matrix.
106 */
107 kFlagDirtyOrtho = 0x10,
Romain Guy5cbbce52010-06-27 22:59:20 -0700108 };
109
110 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700111 * Modifies the current clip with the new clip rectangle and
112 * the specified operation. The specified rectangle is transformed
113 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700114 */
Romain Guyf607bdc2010-09-10 19:20:06 -0700115 bool clip(float left, float top, float right, float bottom,
116 SkRegion::Op op = SkRegion::kIntersect_Op) {
Romain Guyaf28b512010-08-12 14:34:44 -0700117 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700118 transform->mapRect(r);
Romain Guyf607bdc2010-09-10 19:20:06 -0700119 return clipTransformed(r, op);
120 }
121
122 /**
123 * Modifies the current clip with the new clip rectangle and
124 * the specified operation. The specified rectangle is considered
125 * already transformed.
126 */
127 bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op) {
128 bool clipped = false;
Romain Guy079ba2c2010-07-16 14:12:24 -0700129
Romain Guy87a76572010-09-13 18:11:21 -0700130 // NOTE: The unimplemented operations require support for regions
131 // Supporting regions would require using a stencil buffer instead
132 // of the scissor. The stencil buffer itself is not too expensive
133 // (memory cost excluded) but on fillrate limited devices, managing
134 // the stencil might have a negative impact on the framerate.
Romain Guy079ba2c2010-07-16 14:12:24 -0700135 switch (op) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700136 case SkRegion::kDifference_Op:
137 break;
138 case SkRegion::kIntersect_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700139 clipped = clipRect->intersect(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700140 break;
141 case SkRegion::kUnion_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700142 clipped = clipRect->unionWith(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700143 break;
144 case SkRegion::kXOR_Op:
145 break;
146 case SkRegion::kReverseDifference_Op:
147 break;
148 case SkRegion::kReplace_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700149 clipRect->set(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700150 clipped = true;
151 break;
Romain Guy5cbbce52010-06-27 22:59:20 -0700152 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700153
154 if (clipped) {
Romain Guy3b3e4572010-10-04 14:18:24 -0700155 clipRect->snapToPixelBoundaries();
Romain Guy09147fb2010-07-22 13:08:20 -0700156 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700157 }
158
Romain Guy3d58c032010-07-14 16:34:53 -0700159 return clipped;
Romain Guy5cbbce52010-06-27 22:59:20 -0700160 }
161
162 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700163 * Sets the current clip.
164 */
165 void setClip(float left, float top, float right, float bottom) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700166 clipRect->set(left, top, right, bottom);
Romain Guy09147fb2010-07-22 13:08:20 -0700167 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700168 }
169
170 const Rect& getLocalClip() {
Romain Guy09147fb2010-07-22 13:08:20 -0700171 if (flags & Snapshot::kFlagDirtyLocalClip) {
172 mat4 inverse;
Romain Guy8aef54f2010-09-01 15:13:49 -0700173 inverse.loadInverse(*transform);
Romain Guy959c91f2010-08-11 19:35:53 -0700174
Romain Guy8aef54f2010-09-01 15:13:49 -0700175 mLocalClip.set(*clipRect);
176 inverse.mapRect(mLocalClip);
Romain Guy959c91f2010-08-11 19:35:53 -0700177
Romain Guy09147fb2010-07-22 13:08:20 -0700178 flags &= ~Snapshot::kFlagDirtyLocalClip;
179 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700180 return mLocalClip;
181 }
182
Romain Guyeb993562010-10-05 18:14:38 -0700183 void resetTransform(float x, float y, float z) {
184 transform = &mTransformRoot;
185 transform->loadTranslate(x, y, z);
186 }
187
188 void resetClip(float left, float top, float right, float bottom) {
189 clipRect = &mClipRectRoot;
190 clipRect->set(left, top, right, bottom);
191 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
192 }
193
Romain Guy8b55f372010-08-18 17:10:07 -0700194 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700195 * Dirty flags.
196 */
197 int flags;
198
199 /**
200 * Previous snapshot.
201 */
202 sp<Snapshot> previous;
203
204 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700205 * Only set when the flag kFlagIsLayer is set.
206 */
Romain Guydda57022010-07-06 11:39:32 -0700207 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700208
Romain Guy8aef54f2010-09-01 15:13:49 -0700209 /**
Romain Guyeb993562010-10-05 18:14:38 -0700210 * Only set when the flag kFlagIsFboLayer is set.
211 */
212 GLuint fbo;
213
214 /**
215 * Current viewport.
216 */
217 Rect viewport;
218
219 /**
220 * Height of the framebuffer the snapshot is rendering into.
221 */
222 int height;
223
224 /**
225 * Contains the previous ortho matrix.
226 */
227 mat4 orthoMatrix;
228
229 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700230 * Local transformation. Holds the current translation, scale and
231 * rotation values.
232 */
233 mat4* transform;
234
235 /**
236 * Current clip region. The clip is stored in canvas-space coordinates,
237 * (screen-space coordinates in the regular case.)
238 */
239 Rect* clipRect;
240
Romain Guy5cbbce52010-06-27 22:59:20 -0700241private:
Romain Guy8aef54f2010-09-01 15:13:49 -0700242 mat4 mTransformRoot;
243 Rect mClipRectRoot;
244 Rect mLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700245
Romain Guy5cbbce52010-06-27 22:59:20 -0700246}; // class Snapshot
247
248}; // namespace uirenderer
249}; // namespace android
250
251#endif // ANDROID_UI_SNAPSHOT_H