Layer.h revision 9fc27819d75e24ad63d7b383d80f5cb66a577a0d
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 <SkXfermode.h>
27
28#include "Rect.h"
29#include "SkiaColorFilter.h"
30#include "Vertex.h"
31
32namespace android {
33namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Layers
37///////////////////////////////////////////////////////////////////////////////
38
39/**
40 * A layer has dimensions and is backed by an OpenGL texture or FBO.
41 */
42struct Layer {
43    Layer(const uint32_t layerWidth, const uint32_t layerHeight):
44            width(layerWidth), height(layerHeight) {
45        mesh = NULL;
46        meshIndices = NULL;
47        meshElementCount = 0;
48    }
49
50    ~Layer() {
51        if (mesh) delete mesh;
52        if (meshIndices) delete meshIndices;
53    }
54
55    /**
56     * Sets this layer's region to a rectangle. Computes the appropriate
57     * texture coordinates.
58     */
59    void setRegionAsRect() {
60        const android::Rect& bounds = region.getBounds();
61        regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
62               bounds.rightBottom().x, bounds.rightBottom().y);
63
64        const float texX = 1.0f / float(width);
65        const float texY = 1.0f / float(height);
66        const float height = layer.getHeight();
67        texCoords.set(
68               regionRect.left * texX, (height - regionRect.top) * texY,
69               regionRect.right * texX, (height - regionRect.bottom) * texY);
70    }
71
72    /**
73     * Bounds of the layer.
74     */
75    Rect layer;
76    /**
77     * Texture coordinates of the layer.
78     */
79    Rect texCoords;
80
81    /**
82     * Name of the FBO used to render the layer. If the name is 0
83     * this layer is not backed by an FBO, but a simple texture.
84     */
85    GLuint fbo;
86
87    /**
88     * Opacity of the layer.
89     */
90    int alpha;
91    /**
92     * Blending mode of the layer.
93     */
94    SkXfermode::Mode mode;
95    /**
96     * Indicates whether this layer should be blended.
97     */
98    bool blend;
99
100    /**
101     * Indicates whether this layer has been used already.
102     */
103    bool empty;
104
105    /**
106     * Name of the texture used to render the layer.
107     */
108    GLuint texture;
109    /**
110     * Width of the layer texture.
111     */
112    uint32_t width;
113    /**
114     * Height of the layer texture.
115     */
116    uint32_t height;
117
118    /**
119     * Dirty region indicating what parts of the layer
120     * have been drawn.
121     */
122    Region region;
123    /**
124     * If the region is a rectangle, coordinates of the
125     * region are stored here.
126     */
127    Rect regionRect;
128
129    /**
130     * Color filter used to draw this layer. Optional.
131     */
132    SkiaColorFilter* colorFilter;
133
134    /**
135     * If the layer can be rendered as a mesh, this is non-null.
136     */
137    TextureVertex* mesh;
138    uint16_t* meshIndices;
139    GLsizei meshElementCount;
140}; // struct Layer
141
142}; // namespace uirenderer
143}; // namespace android
144
145#endif // ANDROID_HWUI_LAYER_H
146