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