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