Layer.h revision e18264b079481a244b30e3f71012c53bbd861f92
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 <cutils/compiler.h>
21#include <sys/types.h>
22
23#include <GLES2/gl2.h>
24
25#include <ui/Region.h>
26
27#include <SkPaint.h>
28#include <SkXfermode.h>
29
30#include "Matrix.h"
31#include "Rect.h"
32#include "RenderBuffer.h"
33#include "Texture.h"
34#include "Vertex.h"
35
36namespace android {
37namespace uirenderer {
38
39///////////////////////////////////////////////////////////////////////////////
40// Layers
41///////////////////////////////////////////////////////////////////////////////
42
43// Forward declarations
44class Caches;
45class OpenGLRenderer;
46class RenderNode;
47class DeferredDisplayList;
48class DeferStateStruct;
49
50/**
51 * A layer has dimensions and is backed by an OpenGL texture or FBO.
52 */
53class Layer {
54public:
55    Layer(const uint32_t layerWidth, const uint32_t layerHeight);
56    ~Layer();
57
58    static uint32_t computeIdealWidth(uint32_t layerWidth);
59    static uint32_t computeIdealHeight(uint32_t layerHeight);
60
61    /**
62     * Calling this method will remove (either by recycling or
63     * destroying) the associated FBO, if present, and any render
64     * buffer (stencil for instance.)
65     */
66    void removeFbo(bool flush = true);
67
68    /**
69     * Sets this layer's region to a rectangle. Computes the appropriate
70     * texture coordinates.
71     */
72    void setRegionAsRect() {
73        const android::Rect& bounds = region.getBounds();
74        regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
75               bounds.rightBottom().x, bounds.rightBottom().y);
76
77        const float texX = 1.0f / float(texture.width);
78        const float texY = 1.0f / float(texture.height);
79        const float height = layer.getHeight();
80        texCoords.set(
81               regionRect.left * texX, (height - regionRect.top) * texY,
82               regionRect.right * texX, (height - regionRect.bottom) * texY);
83
84        regionRect.translate(layer.left, layer.top);
85    }
86
87    void updateDeferred(OpenGLRenderer* renderer, RenderNode* displayList,
88            int left, int top, int right, int bottom) {
89        this->renderer = renderer;
90        this->displayList = displayList;
91        const Rect r(left, top, right, bottom);
92        dirtyRect.unionWith(r);
93        deferredUpdateScheduled = true;
94    }
95
96    inline uint32_t getWidth() const {
97        return texture.width;
98    }
99
100    inline uint32_t getHeight() const {
101        return texture.height;
102    }
103
104    /**
105     * Resize the layer and its texture if needed.
106     *
107     * @param width The new width of the layer
108     * @param height The new height of the layer
109     *
110     * @return True if the layer was resized or nothing happened, false if
111     *         a failure occurred during the resizing operation
112     */
113    bool resize(const uint32_t width, const uint32_t height);
114
115    void setSize(uint32_t width, uint32_t height) {
116        texture.width = width;
117        texture.height = height;
118    }
119
120    ANDROID_API void setPaint(const SkPaint* paint);
121
122    inline void setBlend(bool blend) {
123        texture.blend = blend;
124    }
125
126    inline bool isBlend() const {
127        return texture.blend;
128    }
129
130    inline void setForceFilter(bool forceFilter) {
131        this->forceFilter = forceFilter;
132    }
133
134    inline bool getForceFilter() const {
135        return forceFilter;
136    }
137
138    inline void setAlpha(int alpha) {
139        this->alpha = alpha;
140    }
141
142    inline void setAlpha(int alpha, SkXfermode::Mode mode) {
143        this->alpha = alpha;
144        this->mode = mode;
145    }
146
147    inline int getAlpha() const {
148        return alpha;
149    }
150
151    inline SkXfermode::Mode getMode() const {
152        return mode;
153    }
154
155    inline void setEmpty(bool empty) {
156        this->empty = empty;
157    }
158
159    inline bool isEmpty() const {
160        return empty;
161    }
162
163    inline void setFbo(GLuint fbo) {
164        this->fbo = fbo;
165    }
166
167    inline GLuint getFbo() const {
168        return fbo;
169    }
170
171    inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
172        if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
173            this->stencil = renderBuffer;
174            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
175                    GL_RENDERBUFFER, stencil->getName());
176        } else {
177            ALOGE("The specified render buffer is not a stencil buffer");
178        }
179    }
180
181    inline RenderBuffer* getStencilRenderBuffer() const {
182        return stencil;
183    }
184
185    inline GLuint getTexture() const {
186        return texture.id;
187    }
188
189    inline GLenum getRenderTarget() const {
190        return renderTarget;
191    }
192
193    inline void setRenderTarget(GLenum renderTarget) {
194        this->renderTarget = renderTarget;
195    }
196
197    void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
198        texture.setWrap(wrap, bindTexture, force, renderTarget);
199    }
200
201    void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
202        texture.setFilter(filter, bindTexture, force, renderTarget);
203    }
204
205    inline bool isCacheable() const {
206        return cacheable;
207    }
208
209    inline void setCacheable(bool cacheable) {
210        this->cacheable = cacheable;
211    }
212
213    inline bool isDirty() const {
214        return dirty;
215    }
216
217    inline void setDirty(bool dirty) {
218        this->dirty = dirty;
219    }
220
221    inline bool isTextureLayer() const {
222        return textureLayer;
223    }
224
225    inline void setTextureLayer(bool textureLayer) {
226        this->textureLayer = textureLayer;
227    }
228
229    inline SkColorFilter* getColorFilter() const {
230        return colorFilter;
231    }
232
233    ANDROID_API void setColorFilter(SkColorFilter* filter);
234
235    void bindStencilRenderBuffer() const;
236
237    void bindTexture() const;
238    void generateTexture();
239    void allocateTexture();
240    void deleteTexture();
241
242    /**
243     * When the caller frees the texture itself, the caller
244     * must call this method to tell this layer that it lost
245     * the texture.
246     */
247    ANDROID_API void clearTexture();
248
249    inline mat4& getTexTransform() {
250        return texTransform;
251    }
252
253    inline mat4& getTransform() {
254        return transform;
255    }
256
257    void defer();
258    void cancelDefer();
259    void flush();
260    void render();
261
262    /**
263     * Bounds of the layer.
264     */
265    Rect layer;
266    /**
267     * Texture coordinates of the layer.
268     */
269    Rect texCoords;
270    /**
271     * Clipping rectangle.
272     */
273    Rect clipRect;
274
275    /**
276     * Dirty region indicating what parts of the layer
277     * have been drawn.
278     */
279    Region region;
280    /**
281     * If the region is a rectangle, coordinates of the
282     * region are stored here.
283     */
284    Rect regionRect;
285
286    /**
287     * If the layer can be rendered as a mesh, this is non-null.
288     */
289    TextureVertex* mesh;
290    GLsizei meshElementCount;
291
292    /**
293     * Used for deferred updates.
294     */
295    bool deferredUpdateScheduled;
296    OpenGLRenderer* renderer;
297    RenderNode* displayList;
298    Rect dirtyRect;
299    bool debugDrawUpdate;
300    bool hasDrawnSinceUpdate;
301
302private:
303    Caches& caches;
304
305    /**
306     * Name of the FBO used to render the layer. If the name is 0
307     * this layer is not backed by an FBO, but a simple texture.
308     */
309    GLuint fbo;
310
311    /**
312     * The render buffer used as the stencil buffer.
313     */
314    RenderBuffer* stencil;
315
316    /**
317     * Indicates whether this layer has been used already.
318     */
319    bool empty;
320
321    /**
322     * The texture backing this layer.
323     */
324    Texture texture;
325
326    /**
327     * If set to true (by default), the layer can be reused.
328     */
329    bool cacheable;
330
331    /**
332     * When set to true, this layer must be treated as a texture
333     * layer.
334     */
335    bool textureLayer;
336
337    /**
338     * When set to true, this layer is dirty and should be cleared
339     * before any rendering occurs.
340     */
341    bool dirty;
342
343    /**
344     * Indicates the render target.
345     */
346    GLenum renderTarget;
347
348    /**
349     * Color filter used to draw this layer. Optional.
350     */
351    SkColorFilter* colorFilter;
352
353    /**
354     * Indicates raster data backing the layer is scaled, requiring filtration.
355     */
356    bool forceFilter;
357
358    /**
359     * Opacity of the layer.
360     */
361    int alpha;
362
363    /**
364     * Blending mode of the layer.
365     */
366    SkXfermode::Mode mode;
367
368    /**
369     * Optional texture coordinates transform.
370     */
371    mat4 texTransform;
372
373    /**
374     * Optional transform.
375     */
376    mat4 transform;
377
378    /**
379     * Used to defer display lists when the layer is updated with a
380     * display list.
381     */
382    DeferredDisplayList* deferredList;
383
384}; // struct Layer
385
386}; // namespace uirenderer
387}; // namespace android
388
389#endif // ANDROID_HWUI_LAYER_H
390