blob: 32fee324f488a0d09c27364bc628339bba879a7a [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 Guydda57022010-07-06 11:39:32 -070025#include "Layer.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070026#include "Matrix.h"
27#include "Rect.h"
28
29namespace android {
30namespace uirenderer {
31
32/**
33 * A snapshot holds information about the current state of the rendering
34 * surface. A snapshot is usually created whenever the user calls save()
35 * and discarded when the user calls restore(). Once a snapshot is created,
36 * it can hold information for deferred rendering.
37 *
38 * Each snapshot has a link to a previous snapshot, indicating the previous
39 * state of the renderer.
40 */
41class Snapshot: public LightRefBase<Snapshot> {
42public:
Romain Guydda57022010-07-06 11:39:32 -070043 Snapshot(): layer(NULL), fbo(0) { }
Romain Guy5cbbce52010-06-27 22:59:20 -070044
45 /**
46 * Copies the specified snapshot. Only the transform and clip rectangle
47 * are copied. The layer information is set to 0 and the transform is
48 * assumed to be dirty. The specified snapshot is stored as the previous
49 * snapshot.
50 */
51 Snapshot(const sp<Snapshot> s):
Romain Guyf86ef572010-07-01 11:05:42 -070052 height(s->height),
Romain Guy5cbbce52010-06-27 22:59:20 -070053 transform(s->transform),
54 clipRect(s->clipRect),
55 flags(kFlagDirtyTransform),
56 previous(s),
Romain Guydda57022010-07-06 11:39:32 -070057 layer(NULL),
58 fbo(s->fbo) {
Romain Guy3d58c032010-07-14 16:34:53 -070059 mappedClip.set(s->clipRect);
60 transform.mapRect(mappedClip);
Romain Guy5cbbce52010-06-27 22:59:20 -070061 }
62
63 /**
64 * Various flags set on #flags.
65 */
66 enum Flags {
67 /**
68 * Indicates that the clip region was modified. When this
69 * snapshot is restored so must the clip.
70 */
71 kFlagClipSet = 0x1,
72 /**
73 * Indicates that the snapshot holds new transform
74 * information.
75 */
76 kFlagDirtyTransform = 0x2,
77 /**
78 * Indicates that this snapshot was created when saving
79 * a new layer.
80 */
81 kFlagIsLayer = 0x4,
Romain Guyf86ef572010-07-01 11:05:42 -070082 /**
83 * Indicates that this snapshot has changed the ortho matrix.
84 */
85 kFlagDirtyOrtho = 0x8,
Romain Guy5cbbce52010-06-27 22:59:20 -070086 };
87
88 /**
89 * Returns the current clip region mapped by the current transform.
90 */
91 const Rect& getMappedClip() {
Romain Guy3d58c032010-07-14 16:34:53 -070092 return mappedClip;
93 }
94
95 /**
96 * Intersects the current clip with the new clip rectangle.
97 */
98 bool clip(float left, float top, float right, float bottom) {
99 bool clipped = clipRect.intersect(left, top, right, bottom);
Romain Guy5cbbce52010-06-27 22:59:20 -0700100 if (flags & kFlagDirtyTransform) {
101 flags &= ~kFlagDirtyTransform;
102 mappedClip.set(clipRect);
103 transform.mapRect(mappedClip);
104 }
Romain Guy3d58c032010-07-14 16:34:53 -0700105 return clipped;
Romain Guy5cbbce52010-06-27 22:59:20 -0700106 }
107
108 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700109 * Sets the current clip.
110 */
111 void setClip(float left, float top, float right, float bottom) {
112 clipRect.set(left, top, right, bottom);
113 if (flags & kFlagDirtyTransform) {
114 flags &= ~kFlagDirtyTransform;
115 mappedClip.set(clipRect);
116 transform.mapRect(mappedClip);
117 }
118 }
119
120 /**
Romain Guyf86ef572010-07-01 11:05:42 -0700121 * Height of the framebuffer the snapshot is rendering into.
122 */
123 int height;
124
125 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700126 * Local transformation. Holds the current translation, scale and
127 * rotation values.
128 */
129 mat4 transform;
130
131 /**
132 * Current clip region.
133 */
134 Rect clipRect;
135
136 /**
137 * Dirty flags.
138 */
139 int flags;
140
141 /**
142 * Previous snapshot.
143 */
144 sp<Snapshot> previous;
145
146 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700147 * Only set when the flag kFlagIsLayer is set.
148 */
Romain Guydda57022010-07-06 11:39:32 -0700149 Layer* layer;
Romain Guy5cbbce52010-06-27 22:59:20 -0700150 GLuint fbo;
Romain Guy5cbbce52010-06-27 22:59:20 -0700151
Romain Guyf86ef572010-07-01 11:05:42 -0700152 /**
153 * Contains the previous ortho matrix.
154 */
Romain Guy260e1022010-07-12 14:41:06 -0700155 mat4 orthoMatrix;
Romain Guyf86ef572010-07-01 11:05:42 -0700156
Romain Guy5cbbce52010-06-27 22:59:20 -0700157private:
158 // Clipping rectangle mapped with the transform
159 Rect mappedClip;
160}; // class Snapshot
161
162}; // namespace uirenderer
163}; // namespace android
164
165#endif // ANDROID_UI_SNAPSHOT_H