Snapshot.h revision 7fac2e18339f765320d759e8d4c090f92431959e
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 <utils/RefBase.h>
24
25#include <SkRegion.h>
26
27#include "Layer.h"
28#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:
45    Snapshot(): flags(0x0), previous(NULL), layer(NULL), fbo(0) { }
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(0x0),
58            previous(s),
59            layer(NULL),
60            fbo(s->fbo) {
61    }
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 this snapshot was created when saving
74         * a new layer.
75         */
76        kFlagIsLayer = 0x2,
77        /**
78         * Indicates that this snapshot has changed the ortho matrix.
79         */
80        kFlagDirtyOrtho = 0x4,
81    };
82
83    /**
84     * Intersects the current clip with the new clip rectangle.
85     */
86    bool clip(float left, float top, float right, float bottom, SkRegion::Op op) {
87        bool clipped = false;
88
89        Rect r(left, top, right, bottom);
90        transform.mapRect(r);
91
92        switch (op) {
93            case SkRegion::kDifference_Op:
94                break;
95            case SkRegion::kIntersect_Op:
96                clipped = clipRect.intersect(r);
97                break;
98            case SkRegion::kUnion_Op:
99                clipped = clipRect.unionWith(r);
100                break;
101            case SkRegion::kXOR_Op:
102                break;
103            case SkRegion::kReverseDifference_Op:
104                break;
105            case SkRegion::kReplace_Op:
106                clipRect.set(r);
107                clipped = true;
108                break;
109        }
110
111        if (clipped) {
112            flags |= Snapshot::kFlagClipSet;
113        }
114
115        return clipped;
116    }
117
118    /**
119     * Sets the current clip.
120     */
121    void setClip(float left, float top, float right, float bottom) {
122        clipRect.set(left, top, right, bottom);
123        flags |= Snapshot::kFlagClipSet;
124    }
125
126    const Rect& getLocalClip() {
127        mat4 inverse;
128        inverse.loadInverse(transform);
129        localClip.set(clipRect);
130        inverse.mapRect(localClip);
131        return localClip;
132    }
133
134    /**
135     * Height of the framebuffer the snapshot is rendering into.
136     */
137    int height;
138
139    /**
140     * Local transformation. Holds the current translation, scale and
141     * rotation values.
142     */
143    mat4 transform;
144
145    /**
146     * Current clip region. The clip is stored in canvas-space coordinates,
147     * (screen-space coordinates in the regular case.)
148     */
149    Rect clipRect;
150
151    /**
152     * Dirty flags.
153     */
154    int flags;
155
156    /**
157     * Previous snapshot.
158     */
159    sp<Snapshot> previous;
160
161    /**
162     * Only set when the flag kFlagIsLayer is set.
163     */
164    Layer* layer;
165    GLuint fbo;
166
167    /**
168     * Contains the previous ortho matrix.
169     */
170    mat4 orthoMatrix;
171
172private:
173    Rect localClip;
174
175}; // class Snapshot
176
177}; // namespace uirenderer
178}; // namespace android
179
180#endif // ANDROID_UI_SNAPSHOT_H
181