blob: 21b2bef16442da7c9e09779e6e95e5c73c21739b [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 Guy079ba2c2010-07-16 14:12:24 -070025#include <SkRegion.h>
26
Romain Guydda570202010-07-06 11:39:32 -070027#include "Layer.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070028#include "Matrix.h"
29#include "Rect.h"
30
31namespace android {
32namespace uirenderer {
33
34/**
35 * A snapshot holds information about the current state of the rendering
36 * surface. A snapshot is usually created whenever the user calls save()
37 * and discarded when the user calls restore(). Once a snapshot is created,
38 * it can hold information for deferred rendering.
39 *
40 * Each snapshot has a link to a previous snapshot, indicating the previous
41 * state of the renderer.
42 */
43class Snapshot: public LightRefBase<Snapshot> {
44public:
Romain Guy09147fb2010-07-22 13:08:20 -070045 Snapshot(): flags(0), previous(NULL), layer(NULL), fbo(0) { }
Romain Guy5cbbce52010-06-27 22:59:20 -070046
47 /**
48 * Copies the specified snapshot. Only the transform and clip rectangle
49 * are copied. The layer information is set to 0 and the transform is
50 * assumed to be dirty. The specified snapshot is stored as the previous
51 * snapshot.
52 */
Romain Guyae5575b2010-07-29 18:48:04 -070053 Snapshot(const sp<Snapshot>& s):
Romain Guyf86ef572010-07-01 11:05:42 -070054 height(s->height),
Romain Guy5cbbce52010-06-27 22:59:20 -070055 transform(s->transform),
56 clipRect(s->clipRect),
Romain Guy09147fb2010-07-22 13:08:20 -070057 flags(0),
Romain Guy5cbbce52010-06-27 22:59:20 -070058 previous(s),
Romain Guydda570202010-07-06 11:39:32 -070059 layer(NULL),
Romain Guy1d83e192010-08-17 11:37:00 -070060 fbo(s->fbo),
61 viewport(s->viewport) {
Romain Guyb82da652010-07-30 11:36:12 -070062 if ((s->flags & Snapshot::kFlagClipSet) &&
63 !(s->flags & Snapshot::kFlagDirtyLocalClip)) {
64 localClip.set(s->localClip);
65 } else {
66 flags |= Snapshot::kFlagDirtyLocalClip;
67 }
Romain Guy5cbbce52010-06-27 22:59:20 -070068 }
69
70 /**
71 * Various flags set on #flags.
72 */
73 enum Flags {
74 /**
75 * Indicates that the clip region was modified. When this
76 * snapshot is restored so must the clip.
77 */
78 kFlagClipSet = 0x1,
79 /**
Romain Guy5cbbce52010-06-27 22:59:20 -070080 * Indicates that this snapshot was created when saving
81 * a new layer.
82 */
Romain Guy079ba2c2010-07-16 14:12:24 -070083 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070084 /**
85 * Indicates that this snapshot has changed the ortho matrix.
86 */
Romain Guy079ba2c2010-07-16 14:12:24 -070087 kFlagDirtyOrtho = 0x4,
Romain Guy09147fb2010-07-22 13:08:20 -070088 /**
89 * Indicates that the local clip should be recomputed.
90 */
91 kFlagDirtyLocalClip = 0x8,
Romain Guy5cbbce52010-06-27 22:59:20 -070092 };
93
94 /**
Romain Guy3d58c032010-07-14 16:34:53 -070095 * Intersects the current clip with the new clip rectangle.
96 */
Romain Guy079ba2c2010-07-16 14:12:24 -070097 bool clip(float left, float top, float right, float bottom, SkRegion::Op op) {
98 bool clipped = false;
99
Romain Guyaf28b512010-08-12 14:34:44 -0700100 Rect r(left, top, right, bottom);
101 transform.mapRect(r);
Romain Guy079ba2c2010-07-16 14:12:24 -0700102
103 switch (op) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700104 case SkRegion::kDifference_Op:
105 break;
106 case SkRegion::kIntersect_Op:
107 clipped = clipRect.intersect(r);
108 break;
109 case SkRegion::kUnion_Op:
110 clipped = clipRect.unionWith(r);
111 break;
112 case SkRegion::kXOR_Op:
113 break;
114 case SkRegion::kReverseDifference_Op:
115 break;
116 case SkRegion::kReplace_Op:
117 clipRect.set(r);
118 clipped = true;
119 break;
Romain Guy5cbbce52010-06-27 22:59:20 -0700120 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700121
122 if (clipped) {
Romain Guy09147fb2010-07-22 13:08:20 -0700123 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700124 }
125
Romain Guy3d58c032010-07-14 16:34:53 -0700126 return clipped;
Romain Guy5cbbce52010-06-27 22:59:20 -0700127 }
128
129 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700130 * Sets the current clip.
131 */
132 void setClip(float left, float top, float right, float bottom) {
133 clipRect.set(left, top, right, bottom);
Romain Guy09147fb2010-07-22 13:08:20 -0700134 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700135 }
136
137 const Rect& getLocalClip() {
Romain Guy09147fb2010-07-22 13:08:20 -0700138 if (flags & Snapshot::kFlagDirtyLocalClip) {
139 mat4 inverse;
140 inverse.loadInverse(transform);
Romain Guy959c91f2010-08-11 19:35:53 -0700141
Romain Guyaf28b512010-08-12 14:34:44 -0700142 localClip.set(clipRect);
143 inverse.mapRect(localClip);
Romain Guy959c91f2010-08-11 19:35:53 -0700144
Romain Guy09147fb2010-07-22 13:08:20 -0700145 flags &= ~Snapshot::kFlagDirtyLocalClip;
146 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700147 return localClip;
Romain Guyd27977d2010-07-14 19:18:51 -0700148 }
149
150 /**
Romain Guyf86ef572010-07-01 11:05:42 -0700151 * Height of the framebuffer the snapshot is rendering into.
152 */
153 int height;
154
155 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700156 * Local transformation. Holds the current translation, scale and
157 * rotation values.
158 */
159 mat4 transform;
160
161 /**
Romain Guy079ba2c2010-07-16 14:12:24 -0700162 * Current clip region. The clip is stored in canvas-space coordinates,
163 * (screen-space coordinates in the regular case.)
Romain Guy5cbbce52010-06-27 22:59:20 -0700164 */
165 Rect clipRect;
166
167 /**
168 * Dirty flags.
169 */
170 int flags;
171
172 /**
173 * Previous snapshot.
174 */
175 sp<Snapshot> previous;
176
177 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700178 * Only set when the flag kFlagIsLayer is set.
179 */
Romain Guydda570202010-07-06 11:39:32 -0700180 Layer* layer;
Romain Guy5cbbce52010-06-27 22:59:20 -0700181 GLuint fbo;
Romain Guy5cbbce52010-06-27 22:59:20 -0700182
Romain Guyf86ef572010-07-01 11:05:42 -0700183 /**
Romain Guy1d83e192010-08-17 11:37:00 -0700184 * Current viewport.
185 */
186 Rect viewport;
187
188 /**
Romain Guyf86ef572010-07-01 11:05:42 -0700189 * Contains the previous ortho matrix.
190 */
Romain Guy260e1022010-07-12 14:41:06 -0700191 mat4 orthoMatrix;
Romain Guyf86ef572010-07-01 11:05:42 -0700192
Romain Guy5cbbce52010-06-27 22:59:20 -0700193private:
Romain Guy079ba2c2010-07-16 14:12:24 -0700194 Rect localClip;
195
Romain Guy5cbbce52010-06-27 22:59:20 -0700196}; // class Snapshot
197
198}; // namespace uirenderer
199}; // namespace android
200
201#endif // ANDROID_UI_SNAPSHOT_H