Layer.h revision ca79cf69d09efa0c327e9b1237d86a119aea5da7
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_LAYER_H
18#define ANDROID_HWUI_LAYER_H
19
20#include <sys/types.h>
21
22#include <GLES2/gl2.h>
23
24#include <ui/Region.h>
25
26#include <SkPaint.h>
27#include <SkXfermode.h>
28
29#include "Rect.h"
30#include "SkiaColorFilter.h"
31#include "Texture.h"
32#include "Vertex.h"
33
34namespace android {
35namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Layers
39///////////////////////////////////////////////////////////////////////////////
40
41// Forward declarations
42class OpenGLRenderer;
43class DisplayList;
44
45/**
46 * A layer has dimensions and is backed by an OpenGL texture or FBO.
47 */
48struct Layer {
49    Layer(const uint32_t layerWidth, const uint32_t layerHeight);
50    ~Layer();
51
52    void removeFbo();
53
54    /**
55     * Sets this layer's region to a rectangle. Computes the appropriate
56     * texture coordinates.
57     */
58    void setRegionAsRect() {
59        const android::Rect& bounds = region.getBounds();
60        regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
61               bounds.rightBottom().x, bounds.rightBottom().y);
62
63        const float texX = 1.0f / float(texture.width);
64        const float texY = 1.0f / float(texture.height);
65        const float height = layer.getHeight();
66        texCoords.set(
67               regionRect.left * texX, (height - regionRect.top) * texY,
68               regionRect.right * texX, (height - regionRect.bottom) * texY);
69
70        regionRect.translate(layer.left, layer.top);
71    }
72
73    void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
74            int left, int top, int right, int bottom) {
75        this->renderer = renderer;
76        this->displayList = displayList;
77        const Rect r(left, top, right, bottom);
78        dirtyRect.unionWith(r);
79        deferredUpdateScheduled = true;
80    }
81
82    inline uint32_t getWidth() {
83        return texture.width;
84    }
85
86    inline uint32_t getHeight() {
87        return texture.height;
88    }
89
90    void setSize(uint32_t width, uint32_t height) {
91        texture.width = width;
92        texture.height = height;
93    }
94
95    ANDROID_API void setPaint(SkPaint* paint);
96
97    inline void setBlend(bool blend) {
98        texture.blend = blend;
99    }
100
101    inline bool isBlend() {
102        return texture.blend;
103    }
104
105    inline void setAlpha(int alpha) {
106        this->alpha = alpha;
107    }
108
109    inline void setAlpha(int alpha, SkXfermode::Mode mode) {
110        this->alpha = alpha;
111        this->mode = mode;
112    }
113
114    inline int getAlpha() {
115        return alpha;
116    }
117
118    inline SkXfermode::Mode getMode() {
119        return mode;
120    }
121
122    inline void setEmpty(bool empty) {
123        this->empty = empty;
124    }
125
126    inline bool isEmpty() {
127        return empty;
128    }
129
130    inline void setFbo(GLuint fbo) {
131        this->fbo = fbo;
132    }
133
134    inline GLuint getFbo() {
135        return fbo;
136    }
137
138    inline GLuint getTexture() {
139        return texture.id;
140    }
141
142    inline GLenum getRenderTarget() {
143        return renderTarget;
144    }
145
146    inline void setRenderTarget(GLenum renderTarget) {
147        this->renderTarget = renderTarget;
148    }
149
150    void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
151        texture.setWrap(wrap, bindTexture, force, renderTarget);
152    }
153
154    void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
155        texture.setFilter(filter, bindTexture, force, renderTarget);
156    }
157
158    inline bool isCacheable() {
159        return cacheable;
160    }
161
162    inline void setCacheable(bool cacheable) {
163        this->cacheable = cacheable;
164    }
165
166    inline bool isDirty() {
167        return dirty;
168    }
169
170    inline void setDirty(bool dirty) {
171        this->dirty = dirty;
172    }
173
174    inline bool isTextureLayer() {
175        return textureLayer;
176    }
177
178    inline void setTextureLayer(bool textureLayer) {
179        this->textureLayer = textureLayer;
180    }
181
182    inline SkiaColorFilter* getColorFilter() {
183        return colorFilter;
184    }
185
186    ANDROID_API void setColorFilter(SkiaColorFilter* filter);
187
188    inline void bindTexture() {
189        if (texture.id) {
190            glBindTexture(renderTarget, texture.id);
191        }
192    }
193
194    inline void generateTexture() {
195        if (!texture.id) {
196            glGenTextures(1, &texture.id);
197        }
198    }
199
200    inline void deleteTexture() {
201        if (texture.id) {
202            glDeleteTextures(1, &texture.id);
203            texture.id = 0;
204        }
205    }
206
207    /**
208     * When the caller frees the texture itself, the caller
209     * must call this method to tell this layer that it lost
210     * the texture.
211     */
212    void clearTexture() {
213        texture.id = 0;
214    }
215
216    inline void deleteFbo() {
217        if (fbo) glDeleteFramebuffers(1, &fbo);
218    }
219
220    inline void allocateTexture(GLenum format, GLenum storage) {
221#if DEBUG_LAYERS
222        ALOGD("  Allocate layer: %dx%d", getWidth(), getHeight());
223#endif
224        glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL);
225    }
226
227    inline mat4& getTexTransform() {
228        return texTransform;
229    }
230
231    inline mat4& getTransform() {
232        return transform;
233    }
234
235    /**
236     * Bounds of the layer.
237     */
238    Rect layer;
239    /**
240     * Texture coordinates of the layer.
241     */
242    Rect texCoords;
243
244    /**
245     * Dirty region indicating what parts of the layer
246     * have been drawn.
247     */
248    Region region;
249    /**
250     * If the region is a rectangle, coordinates of the
251     * region are stored here.
252     */
253    Rect regionRect;
254
255    /**
256     * If the layer can be rendered as a mesh, this is non-null.
257     */
258    TextureVertex* mesh;
259    uint16_t* meshIndices;
260    GLsizei meshElementCount;
261
262    /**
263     * Used for deferred updates.
264     */
265    bool deferredUpdateScheduled;
266    OpenGLRenderer* renderer;
267    DisplayList* displayList;
268    Rect dirtyRect;
269    bool debugDrawUpdate;
270
271private:
272    /**
273     * Name of the FBO used to render the layer. If the name is 0
274     * this layer is not backed by an FBO, but a simple texture.
275     */
276    GLuint fbo;
277
278    /**
279     * Indicates whether this layer has been used already.
280     */
281    bool empty;
282
283    /**
284     * The texture backing this layer.
285     */
286    Texture texture;
287
288    /**
289     * If set to true (by default), the layer can be reused.
290     */
291    bool cacheable;
292
293    /**
294     * When set to true, this layer must be treated as a texture
295     * layer.
296     */
297    bool textureLayer;
298
299    /**
300     * When set to true, this layer is dirty and should be cleared
301     * before any rendering occurs.
302     */
303    bool dirty;
304
305    /**
306     * Indicates the render target.
307     */
308    GLenum renderTarget;
309
310    /**
311     * Color filter used to draw this layer. Optional.
312     */
313    SkiaColorFilter* colorFilter;
314
315    /**
316     * Opacity of the layer.
317     */
318    int alpha;
319    /**
320     * Blending mode of the layer.
321     */
322    SkXfermode::Mode mode;
323
324    /**
325     * Optional texture coordinates transform.
326     */
327    mat4 texTransform;
328
329    /**
330     * Optional transform.
331     */
332    mat4 transform;
333
334}; // struct Layer
335
336}; // namespace uirenderer
337}; // namespace android
338
339#endif // ANDROID_HWUI_LAYER_H
340