blob: ebedf5338aaa0b5cdec28cd38f63d18865c53353 [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 Guyf607bdc2010-09-10 19:20:06 -070046 Snapshot(): flags(0), previous(NULL), layer(NULL) {
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 Guyf607bdc2010-09-10 19:20:06 -070056 flags(0), previous(s), layer(NULL) {
Romain Guy8aef54f2010-09-01 15:13:49 -070057 if (saveFlags & SkCanvas::kMatrix_SaveFlag) {
58 mTransformRoot.load(*s->transform);
59 transform = &mTransformRoot;
60 } else {
61 transform = s->transform;
62 }
63
64 if (saveFlags & SkCanvas::kClip_SaveFlag) {
65 mClipRectRoot.set(*s->clipRect);
66 clipRect = &mClipRectRoot;
67 } else {
68 clipRect = s->clipRect;
69 }
70
Romain Guyb82da652010-07-30 11:36:12 -070071 if ((s->flags & Snapshot::kFlagClipSet) &&
72 !(s->flags & Snapshot::kFlagDirtyLocalClip)) {
Romain Guy8aef54f2010-09-01 15:13:49 -070073 mLocalClip.set(s->mLocalClip);
Romain Guyb82da652010-07-30 11:36:12 -070074 } else {
75 flags |= Snapshot::kFlagDirtyLocalClip;
76 }
Romain Guy5cbbce52010-06-27 22:59:20 -070077 }
78
79 /**
80 * Various flags set on #flags.
81 */
82 enum Flags {
83 /**
84 * Indicates that the clip region was modified. When this
85 * snapshot is restored so must the clip.
86 */
87 kFlagClipSet = 0x1,
88 /**
Romain Guy5cbbce52010-06-27 22:59:20 -070089 * Indicates that this snapshot was created when saving
90 * a new layer.
91 */
Romain Guy079ba2c2010-07-16 14:12:24 -070092 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070093 /**
Romain Guy09147fb2010-07-22 13:08:20 -070094 * Indicates that the local clip should be recomputed.
95 */
Romain Guyf607bdc2010-09-10 19:20:06 -070096 kFlagDirtyLocalClip = 0x4,
Romain Guy5cbbce52010-06-27 22:59:20 -070097 };
98
99 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700100 * Modifies the current clip with the new clip rectangle and
101 * the specified operation. The specified rectangle is transformed
102 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700103 */
Romain Guyf607bdc2010-09-10 19:20:06 -0700104 bool clip(float left, float top, float right, float bottom,
105 SkRegion::Op op = SkRegion::kIntersect_Op) {
Romain Guyaf28b512010-08-12 14:34:44 -0700106 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700107 transform->mapRect(r);
Romain Guyf607bdc2010-09-10 19:20:06 -0700108 return clipTransformed(r, op);
109 }
110
111 /**
112 * Modifies the current clip with the new clip rectangle and
113 * the specified operation. The specified rectangle is considered
114 * already transformed.
115 */
116 bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op) {
117 bool clipped = false;
Romain Guy079ba2c2010-07-16 14:12:24 -0700118
119 switch (op) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700120 case SkRegion::kDifference_Op:
121 break;
122 case SkRegion::kIntersect_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700123 clipped = clipRect->intersect(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700124 break;
125 case SkRegion::kUnion_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700126 clipped = clipRect->unionWith(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700127 break;
128 case SkRegion::kXOR_Op:
129 break;
130 case SkRegion::kReverseDifference_Op:
131 break;
132 case SkRegion::kReplace_Op:
Romain Guy8aef54f2010-09-01 15:13:49 -0700133 clipRect->set(r);
Romain Guy7fac2e12010-07-16 17:10:13 -0700134 clipped = true;
135 break;
Romain Guy5cbbce52010-06-27 22:59:20 -0700136 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700137
138 if (clipped) {
Romain Guy09147fb2010-07-22 13:08:20 -0700139 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700140 }
141
Romain Guy3d58c032010-07-14 16:34:53 -0700142 return clipped;
Romain Guy5cbbce52010-06-27 22:59:20 -0700143 }
144
145 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700146 * Sets the current clip.
147 */
148 void setClip(float left, float top, float right, float bottom) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700149 clipRect->set(left, top, right, bottom);
Romain Guy09147fb2010-07-22 13:08:20 -0700150 flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700151 }
152
153 const Rect& getLocalClip() {
Romain Guy09147fb2010-07-22 13:08:20 -0700154 if (flags & Snapshot::kFlagDirtyLocalClip) {
155 mat4 inverse;
Romain Guy8aef54f2010-09-01 15:13:49 -0700156 inverse.loadInverse(*transform);
Romain Guy959c91f2010-08-11 19:35:53 -0700157
Romain Guy8aef54f2010-09-01 15:13:49 -0700158 mLocalClip.set(*clipRect);
159 inverse.mapRect(mLocalClip);
Romain Guy959c91f2010-08-11 19:35:53 -0700160
Romain Guy09147fb2010-07-22 13:08:20 -0700161 flags &= ~Snapshot::kFlagDirtyLocalClip;
162 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700163 return mLocalClip;
164 }
165
Romain Guy8b55f372010-08-18 17:10:07 -0700166 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700167 * Dirty flags.
168 */
169 int flags;
170
171 /**
172 * Previous snapshot.
173 */
174 sp<Snapshot> previous;
175
176 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700177 * Only set when the flag kFlagIsLayer is set.
178 */
Romain Guydda57022010-07-06 11:39:32 -0700179 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700180
Romain Guy8aef54f2010-09-01 15:13:49 -0700181 /**
182 * Local transformation. Holds the current translation, scale and
183 * rotation values.
184 */
185 mat4* transform;
186
187 /**
188 * Current clip region. The clip is stored in canvas-space coordinates,
189 * (screen-space coordinates in the regular case.)
190 */
191 Rect* clipRect;
192
Romain Guy5cbbce52010-06-27 22:59:20 -0700193private:
Romain Guy8aef54f2010-09-01 15:13:49 -0700194 mat4 mTransformRoot;
195 Rect mClipRectRoot;
196 Rect mLocalClip;
Romain Guy079ba2c2010-07-16 14:12:24 -0700197
Romain Guy5cbbce52010-06-27 22:59:20 -0700198}; // class Snapshot
199
200}; // namespace uirenderer
201}; // namespace android
202
203#endif // ANDROID_UI_SNAPSHOT_H