Snapshot.h revision f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640
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_UI_SNAPSHOT_H
18#define ANDROID_UI_SNAPSHOT_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <SkXfermode.h>
24
25#include <utils/RefBase.h>
26
27#include "Matrix.h"
28#include "Rect.h"
29
30namespace android {
31namespace uirenderer {
32
33/**
34 * A snapshot holds information about the current state of the rendering
35 * surface. A snapshot is usually created whenever the user calls save()
36 * and discarded when the user calls restore(). Once a snapshot is created,
37 * it can hold information for deferred rendering.
38 *
39 * Each snapshot has a link to a previous snapshot, indicating the previous
40 * state of the renderer.
41 */
42class Snapshot: public LightRefBase<Snapshot> {
43public:
44    Snapshot() {
45    }
46
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     */
53    Snapshot(const sp<Snapshot> s):
54            height(s->height),
55            transform(s->transform),
56            clipRect(s->clipRect),
57            flags(kFlagDirtyTransform),
58            previous(s),
59            layer(0.0f, 0.0f, 0.0f, 0.0f),
60            texture(0),
61            fbo(0),
62            alpha(255) {
63    }
64
65    /**
66     * Various flags set on #flags.
67     */
68    enum Flags {
69        /**
70         * Indicates that the clip region was modified. When this
71         * snapshot is restored so must the clip.
72         */
73        kFlagClipSet = 0x1,
74        /**
75         * Indicates that the snapshot holds new transform
76         * information.
77         */
78        kFlagDirtyTransform = 0x2,
79        /**
80         * Indicates that this snapshot was created when saving
81         * a new layer.
82         */
83        kFlagIsLayer = 0x4,
84        /**
85         * Indicates that this snapshot has changed the ortho matrix.
86         */
87        kFlagDirtyOrtho = 0x8,
88    };
89
90    /**
91     * Returns the current clip region mapped by the current transform.
92     */
93    const Rect& getMappedClip() {
94        if (flags & kFlagDirtyTransform) {
95            flags &= ~kFlagDirtyTransform;
96            mappedClip.set(clipRect);
97            transform.mapRect(mappedClip);
98        }
99        return mappedClip;
100    }
101
102    /**
103     * Height of the framebuffer the snapshot is rendering into.
104     */
105    int height;
106
107    /**
108     * Local transformation. Holds the current translation, scale and
109     * rotation values.
110     */
111    mat4 transform;
112
113    /**
114     * Current clip region.
115     */
116    Rect clipRect;
117
118    /**
119     * Dirty flags.
120     */
121    int flags;
122
123    /**
124     * Previous snapshot.
125     */
126    sp<Snapshot> previous;
127
128    /**
129     * Coordinates of the layer corresponding to this snapshot.
130     * Only set when the flag kFlagIsLayer is set.
131     */
132    Rect layer;
133    /**
134     * Name of the texture used to render the layer.
135     * Only set when the flag kFlagIsLayer is set.
136     */
137    GLuint texture;
138    /**
139     * Name of the FBO used to render the layer.
140     * Only set when the flag kFlagIsLayer is set.
141     */
142    GLuint fbo;
143    /**
144     * Opacity of the layer.
145     * Only set when the flag kFlagIsLayer is set.
146     */
147    float alpha;
148    /**
149     * Blending mode of the layer.
150     * Only set when the flag kFlagIsLayer is set.
151     */
152    SkXfermode::Mode mode;
153
154    /**
155     * Contains the previous ortho matrix.
156     */
157    float orthoMatrix[16];
158
159private:
160    // Clipping rectangle mapped with the transform
161    Rect mappedClip;
162}; // class Snapshot
163
164}; // namespace uirenderer
165}; // namespace android
166
167#endif // ANDROID_UI_SNAPSHOT_H
168