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