blob: da4824398c9343bc562d522e497514f0aa3312da [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 Guy8aef54f2010-09-01 15:13:49 -070046 Snapshot(): invisible(false), flags(0), previous(NULL), layer(NULL), fbo(0) {
47 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 Guyf86ef572010-07-01 11:05:42 -070056 height(s->height),
Romain Guy8b55f372010-08-18 17:10:07 -070057 invisible(s->invisible),
Romain Guy09147fb2010-07-22 13:08:20 -070058 flags(0),
Romain Guy5cbbce52010-06-27 22:59:20 -070059 previous(s),
Romain Guydda57022010-07-06 11:39:32 -070060 layer(NULL),
Romain Guy1d83e192010-08-17 11:37:00 -070061 fbo(s->fbo),
62 viewport(s->viewport) {
Romain Guy8aef54f2010-09-01 15:13:49 -070063 if (saveFlags & SkCanvas::kMatrix_SaveFlag) {
64 mTransformRoot.load(*s->transform);
65 transform = &mTransformRoot;
66 } else {
67 transform = s->transform;
68 }
69
70 if (saveFlags & SkCanvas::kClip_SaveFlag) {
71 mClipRectRoot.set(*s->clipRect);
72 clipRect = &mClipRectRoot;
73 } else {
74 clipRect = s->clipRect;
75 }
76
Romain Guyb82da652010-07-30 11:36:12 -070077 if ((s->flags & Snapshot::kFlagClipSet) &&
78 !(s->flags & Snapshot::kFlagDirtyLocalClip)) {
Romain Guy8aef54f2010-09-01 15:13:49 -070079 mLocalClip.set(s->mLocalClip);
Romain Guyb82da652010-07-30 11:36:12 -070080 } else {
81 flags |= Snapshot::kFlagDirtyLocalClip;
82 }
Romain Guy5cbbce52010-06-27 22:59:20 -070083 }
84
85 /**
86 * Various flags set on #flags.
87 */
88 enum Flags {
89 /**
90 * Indicates that the clip region was modified. When this
91 * snapshot is restored so must the clip.
92 */
93 kFlagClipSet = 0x1,
94 /**
Romain Guy5cbbce52010-06-27 22:59:20 -070095 * Indicates that this snapshot was created when saving
96 * a new layer.
97 */
Romain Guy079ba2c2010-07-16 14:12:24 -070098 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070099 /**
100 * Indicates that this snapshot has changed the ortho matrix.
101 */
Romain Guy079ba2c2010-07-16 14:12:24 -0700102 kFlagDirtyOrtho = 0x4,
Romain Guy09147fb2010-07-22 13:08:20 -0700103 /**
104 * Indicates that the local clip should be recomputed.
105 */
106 kFlagDirtyLocalClip = 0x8,
Romain Guy5cbbce52010-06-27 22:59:20 -0700107 };
108
109 /**
Romain Guy3d58c032010-07-14 16:34:53 -0700110 * Intersects the current clip with the new clip rectangle.
111 */
Romain Guy079ba2c2010-07-16 14:12:24 -0700112 bool clip(float left, float top, float right, float bottom, SkRegion::Op op) {
113 bool clipped = false;
114
Romain Guyaf28b512010-08-12 14:34:44 -0700115 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700116 transform->mapRect(r);
Romain Guy079ba2c2010-07-16 14:12:24 -0700117
118 switch (op) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700119 case SkRegion::kDifference_Op:
120 break;
121 case SkRegion::kIntersect_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700122 clipped = clipRect->intersect(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700123 break;
124 case SkRegion::kUnion_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700125 clipped = clipRect->unionWith(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700126 break;
127 case SkRegion::kXOR_Op:
128 break;
129 case SkRegion::kReverseDifference_Op:
130 break;
131 case SkRegion::kReplace_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700132 clipRect->set(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700133 clipped = true;
134 break;
Romain Guy5cbbce52010-06-27 22:59:20 -0700135 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700136
137 if (clipped) {
Romain Guy09147fb2010-07-22 13:08:20 -0700138 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700139 }
140
Romain Guy3d58c032010-07-14 16:34:53 -0700141 return clipped;
Romain Guy5cbbce52010-06-27 22:59:20 -0700142 }
143
144 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700145 * Sets the current clip.
146 */
147 void setClip(float left, float top, float right, float bottom) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700148 clipRect->set(left, top, right, bottom);
Romain Guy09147fb2010-07-22 13:08:20 -0700149 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700150 }
151
152 const Rect& getLocalClip() {
Romain Guy09147fb2010-07-22 13:08:20 -0700153 if (flags & Snapshot::kFlagDirtyLocalClip) {
154 mat4 inverse;
Romain Guy8aef54f2010-09-01 15:13:49 -0700155 inverse.loadInverse(*transform);
Romain Guy959c91f2010-08-11 19:35:53 -0700156
Romain Guy8aef54f2010-09-01 15:13:49 -0700157 mLocalClip.set(*clipRect);
158 inverse.mapRect(mLocalClip);
Romain Guy959c91f2010-08-11 19:35:53 -0700159
Romain Guy09147fb2010-07-22 13:08:20 -0700160 flags &= ~Snapshot::kFlagDirtyLocalClip;
161 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700162 return mLocalClip;
163 }
164
165 // TODO: Temporary
166 void resetTransform(float x, float y, float z) {
167 transform = &mTransformRoot;
168 transform->loadTranslate(x, y, z);
169 }
170
171 // TODO: Temporary
172 void resetClip(float left, float top, float right, float bottom) {
173 clipRect = &mClipRectRoot;
174 clipRect->set(left, top, right, bottom);
175 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guyd27977d2010-07-14 19:18:51 -0700176 }
177
178 /**
Romain Guyf86ef572010-07-01 11:05:42 -0700179 * Height of the framebuffer the snapshot is rendering into.
180 */
181 int height;
182
183 /**
Romain Guy8b55f372010-08-18 17:10:07 -0700184 * If true, the layer won't be rendered.
185 */
186 bool invisible;
187
188 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700189 * Dirty flags.
190 */
191 int flags;
192
193 /**
194 * Previous snapshot.
195 */
196 sp<Snapshot> previous;
197
198 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700199 * Only set when the flag kFlagIsLayer is set.
200 */
Romain Guydda57022010-07-06 11:39:32 -0700201 Layer* layer;
Romain Guy5cbbce52010-06-27 22:59:20 -0700202 GLuint fbo;
Romain Guy5cbbce52010-06-27 22:59:20 -0700203
Romain Guyf86ef572010-07-01 11:05:42 -0700204 /**
Romain Guy1d83e192010-08-17 11:37:00 -0700205 * Current viewport.
206 */
207 Rect viewport;
208
209 /**
Romain Guyf86ef572010-07-01 11:05:42 -0700210 * Contains the previous ortho matrix.
211 */
Romain Guy260e1022010-07-12 14:41:06 -0700212 mat4 orthoMatrix;
Romain Guyf86ef572010-07-01 11:05:42 -0700213
Romain Guy8aef54f2010-09-01 15:13:49 -0700214 /**
215 * Local transformation. Holds the current translation, scale and
216 * rotation values.
217 */
218 mat4* transform;
219
220 /**
221 * Current clip region. The clip is stored in canvas-space coordinates,
222 * (screen-space coordinates in the regular case.)
223 */
224 Rect* clipRect;
225
Romain Guy5cbbce52010-06-27 22:59:20 -0700226private:
Romain Guy8aef54f2010-09-01 15:13:49 -0700227 mat4 mTransformRoot;
228 Rect mClipRectRoot;
229 Rect mLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700230
Romain Guy5cbbce52010-06-27 22:59:20 -0700231}; // class Snapshot
232
233}; // namespace uirenderer
234}; // namespace android
235
236#endif // ANDROID_UI_SNAPSHOT_H