Snapshot.h revision 8ce00301a023eecaeb8891ce906f67b513ebb42a
1/*
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_HWUI_SNAPSHOT_H
18#define ANDROID_HWUI_SNAPSHOT_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <utils/RefBase.h>
24#include <ui/Region.h>
25
26#include <SkRegion.h>
27
28#include "Layer.h"
29#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:
46
47    Snapshot();
48    Snapshot(const sp<Snapshot>& s, int saveFlags);
49
50    /**
51     * Various flags set on ::flags.
52     */
53    enum Flags {
54        /**
55         * Indicates that the clip region was modified. When this
56         * snapshot is restored so must the clip.
57         */
58        kFlagClipSet = 0x1,
59        /**
60         * Indicates that this snapshot was created when saving
61         * a new layer.
62         */
63        kFlagIsLayer = 0x2,
64        /**
65         * Indicates that this snapshot is a special type of layer
66         * backed by an FBO. This flag only makes sense when the
67         * flag kFlagIsLayer is also set.
68         */
69        kFlagIsFboLayer = 0x4,
70        /**
71         * Indicates that this snapshot has changed the ortho matrix.
72         */
73        kFlagDirtyOrtho = 0x8,
74        /**
75         * Indicates that this snapshot or an ancestor snapshot is
76         * an FBO layer.
77         */
78        kFlagFboTarget = 0x10
79    };
80
81    /**
82     * Modifies the current clip with the new clip rectangle and
83     * the specified operation. The specified rectangle is transformed
84     * by this snapshot's trasnformation.
85     */
86    bool clip(float left, float top, float right, float bottom,
87            SkRegion::Op op = SkRegion::kIntersect_Op);
88
89    /**
90     * Modifies the current clip with the new clip rectangle and
91     * the specified operation. The specified rectangle is considered
92     * already transformed.
93     */
94    bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op);
95
96    /**
97     * Modifies the current clip with the specified region and operation.
98     * The specified region is considered already transformed.
99     */
100    bool clipRegionTransformed(const SkRegion& region, SkRegion::Op op);
101
102    /**
103     * Sets the current clip.
104     */
105    void setClip(float left, float top, float right, float bottom);
106
107    /**
108     * Returns the current clip in local coordinates. The clip rect is
109     * transformed by the inverse transform matrix.
110     */
111    const Rect& getLocalClip();
112
113    /**
114     * Resets the clip to the specified rect.
115     */
116    void resetClip(float left, float top, float right, float bottom);
117
118    /**
119     * Resets the current transform to a pure 3D translation.
120     */
121    void resetTransform(float x, float y, float z);
122
123    /**
124     * Indicates whether this snapshot should be ignored. A snapshot
125     * is typicalled ignored if its layer is invisible or empty.
126     */
127    bool isIgnored() const;
128
129    /**
130     * Indicates whether the current transform has perspective components.
131     */
132    bool hasPerspectiveTransform() const;
133
134    /**
135     * Dirty flags.
136     */
137    int flags;
138
139    /**
140     * Previous snapshot.
141     */
142    sp<Snapshot> previous;
143
144    /**
145     * A pointer to the currently active layer.
146     *
147     * This snapshot does not own the layer, this pointer must not be freed.
148     */
149    Layer* layer;
150
151    /**
152     * Target FBO used for rendering. Set to 0 when rendering directly
153     * into the framebuffer.
154     */
155    GLuint fbo;
156
157    /**
158     * Indicates that this snapshot is invisible and nothing should be drawn
159     * inside it. This flag is set only when the layer clips drawing to its
160     * bounds and is passed to subsequent snapshots.
161     */
162    bool invisible;
163
164    /**
165     * If set to true, the layer will not be composited. This is similar to
166     * invisible but this flag is not passed to subsequent snapshots.
167     */
168    bool empty;
169
170    /**
171     * Current viewport.
172     */
173    Rect viewport;
174
175    /**
176     * Height of the framebuffer the snapshot is rendering into.
177     */
178    int height;
179
180    /**
181     * Contains the previous ortho matrix.
182     */
183    mat4 orthoMatrix;
184
185    /**
186     * Local transformation. Holds the current translation, scale and
187     * rotation values.
188     *
189     * This is a reference to a matrix owned by this snapshot or another
190     *  snapshot. This pointer must not be freed. See ::mTransformRoot.
191     */
192    mat4* transform;
193
194    /**
195     * Current clip rect. The clip is stored in canvas-space coordinates,
196     * (screen-space coordinates in the regular case.)
197     *
198     * This is a reference to a rect owned by this snapshot or another
199     * snapshot. This pointer must not be freed. See ::mClipRectRoot.
200     */
201    Rect* clipRect;
202
203    /**
204     * Current clip region. The clip is stored in canvas-space coordinates,
205     * (screen-space coordinates in the regular case.)
206     *
207     * This is a reference to a region owned by this snapshot or another
208     * snapshot. This pointer must not be freed. See ::mClipRegionRoot.
209     */
210    SkRegion* clipRegion;
211
212    /**
213     * The ancestor layer's dirty region.
214     *
215     * This is a reference to a region owned by a layer. This pointer must
216     * not be freed.
217     */
218    Region* region;
219
220    /**
221     * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
222     * has translucent rendering in a non-overlapping View. This value will be used by
223     * the renderer to set the alpha in the current color being used for ensuing drawing
224     * operations. The value is inherited by child snapshots because the same value should
225     * be applied to descendents of the current DisplayList (for example, a TextView contains
226     * the base alpha value which should be applied to the child DisplayLists used for drawing
227     * the actual text).
228     */
229    float alpha;
230
231private:
232    void ensureClipRegion();
233    void copyClipRectFromRegion();
234
235    bool clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op);
236
237    mat4 mTransformRoot;
238    Rect mClipRectRoot;
239    Rect mLocalClip;
240
241    SkRegion mClipRegionRoot;
242
243}; // class Snapshot
244
245}; // namespace uirenderer
246}; // namespace android
247
248#endif // ANDROID_HWUI_SNAPSHOT_H
249