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