blob: f2431771bd8b88f2938be004b5a911e09cf9aa28 [file] [log] [blame]
Romain Guydda57022010-07-06 11:39:32 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_LAYER_H
18#define ANDROID_HWUI_LAYER_H
Romain Guydda57022010-07-06 11:39:32 -070019
Romain Guyf7f93552010-07-08 19:17:03 -070020#include <sys/types.h>
21
Romain Guydda57022010-07-06 11:39:32 -070022#include <GLES2/gl2.h>
23
Romain Guy5b3b3522010-10-27 18:57:51 -070024#include <ui/Region.h>
25
Romain Guydda57022010-07-06 11:39:32 -070026#include <SkXfermode.h>
27
28#include "Rect.h"
Romain Guy171c5922011-01-06 10:04:23 -080029#include "SkiaColorFilter.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070030#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080031#include "Vertex.h"
Romain Guydda57022010-07-06 11:39:32 -070032
33namespace android {
34namespace uirenderer {
35
Romain Guy8550c4c2010-10-08 15:49:53 -070036///////////////////////////////////////////////////////////////////////////////
37// Layers
38///////////////////////////////////////////////////////////////////////////////
Romain Guydda57022010-07-06 11:39:32 -070039
Romain Guy2bf68f02012-03-02 13:37:47 -080040// Forward declarations
41class OpenGLRenderer;
42class DisplayList;
43
Romain Guydda57022010-07-06 11:39:32 -070044/**
Romain Guyeb993562010-10-05 18:14:38 -070045 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070046 */
47struct Layer {
Romain Guy9ace8f52011-07-07 20:50:11 -070048 Layer(const uint32_t layerWidth, const uint32_t layerHeight) {
Romain Guyf219da52011-01-16 12:54:25 -080049 mesh = NULL;
50 meshIndices = NULL;
51 meshElementCount = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -070052 cacheable = true;
53 textureLayer = false;
Romain Guy8f0095c2011-05-02 17:24:22 -070054 renderTarget = GL_TEXTURE_2D;
Romain Guy9ace8f52011-07-07 20:50:11 -070055 texture.width = layerWidth;
56 texture.height = layerHeight;
57 colorFilter = NULL;
Romain Guy2bf68f02012-03-02 13:37:47 -080058 deferredUpdateScheduled = false;
59 renderer = NULL;
60 displayList = NULL;
Romain Guyf219da52011-01-16 12:54:25 -080061 }
62
63 ~Layer() {
64 if (mesh) delete mesh;
65 if (meshIndices) delete meshIndices;
Romain Guy8550c4c2010-10-08 15:49:53 -070066 }
67
Romain Guydda57022010-07-06 11:39:32 -070068 /**
Romain Guy9fc27812011-04-27 14:21:41 -070069 * 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
Romain Guy9ace8f52011-07-07 20:50:11 -070077 const float texX = 1.0f / float(texture.width);
78 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070079 const float height = layer.getHeight();
80 texCoords.set(
81 regionRect.left * texX, (height - regionRect.top) * texY,
82 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070083
84 regionRect.translate(layer.left, layer.top);
85 }
86
Romain Guy2bf68f02012-03-02 13:37:47 -080087 void updateDeferred(OpenGLRenderer* renderer, DisplayList* 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
Romain Guy9ace8f52011-07-07 20:50:11 -070096 inline uint32_t getWidth() {
97 return texture.width;
98 }
99
100 inline uint32_t getHeight() {
101 return texture.height;
102 }
103
104 void setSize(uint32_t width, uint32_t height) {
105 texture.width = width;
106 texture.height = height;
107 }
108
109 inline void setBlend(bool blend) {
110 texture.blend = blend;
111 }
112
113 inline bool isBlend() {
114 return texture.blend;
115 }
116
117 inline void setAlpha(int alpha) {
118 this->alpha = alpha;
119 }
120
121 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
122 this->alpha = alpha;
123 this->mode = mode;
124 }
125
126 inline int getAlpha() {
127 return alpha;
128 }
129
130 inline SkXfermode::Mode getMode() {
131 return mode;
132 }
133
134 inline void setEmpty(bool empty) {
135 this->empty = empty;
136 }
137
138 inline bool isEmpty() {
139 return empty;
140 }
141
142 inline void setFbo(GLuint fbo) {
143 this->fbo = fbo;
144 }
145
146 inline GLuint getFbo() {
147 return fbo;
148 }
149
150 inline GLuint* getTexturePointer() {
151 return &texture.id;
152 }
153
154 inline GLuint getTexture() {
155 return texture.id;
156 }
157
158 inline GLenum getRenderTarget() {
159 return renderTarget;
160 }
161
162 inline void setRenderTarget(GLenum renderTarget) {
163 this->renderTarget = renderTarget;
164 }
165
Romain Guyd21b6e12011-11-30 20:21:23 -0800166 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
167 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700168 }
169
Romain Guyd21b6e12011-11-30 20:21:23 -0800170 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
171 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700172 }
173
174 inline bool isCacheable() {
175 return cacheable;
176 }
177
178 inline void setCacheable(bool cacheable) {
179 this->cacheable = cacheable;
180 }
181
182 inline bool isTextureLayer() {
183 return textureLayer;
184 }
185
186 inline void setTextureLayer(bool textureLayer) {
187 this->textureLayer = textureLayer;
188 }
189
190 inline SkiaColorFilter* getColorFilter() {
191 return colorFilter;
192 }
193
194 inline void setColorFilter(SkiaColorFilter* filter) {
195 colorFilter = filter;
196 }
197
198 inline void bindTexture() {
199 glBindTexture(renderTarget, texture.id);
200 }
201
202 inline void generateTexture() {
203 glGenTextures(1, &texture.id);
204 }
205
206 inline void deleteTexture() {
207 if (texture.id) glDeleteTextures(1, &texture.id);
208 }
209
Romain Guy912a7b32011-07-26 18:57:28 -0700210 inline void deleteFbo() {
211 if (fbo) glDeleteFramebuffers(1, &fbo);
212 }
213
Romain Guy9ace8f52011-07-07 20:50:11 -0700214 inline void allocateTexture(GLenum format, GLenum storage) {
215 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL);
216 }
217
218 inline mat4& getTexTransform() {
219 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700220 }
221
Romain Guy302a9df2011-08-16 13:55:02 -0700222 inline mat4& getTransform() {
223 return transform;
224 }
225
Romain Guy9fc27812011-04-27 14:21:41 -0700226 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700227 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700228 */
229 Rect layer;
230 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700231 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700232 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700233 Rect texCoords;
234
Romain Guydda57022010-07-06 11:39:32 -0700235 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700236 * Dirty region indicating what parts of the layer
237 * have been drawn.
238 */
239 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700240 /**
241 * If the region is a rectangle, coordinates of the
242 * region are stored here.
243 */
244 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800245
246 /**
Romain Guyf219da52011-01-16 12:54:25 -0800247 * If the layer can be rendered as a mesh, this is non-null.
248 */
249 TextureVertex* mesh;
250 uint16_t* meshIndices;
251 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700252
Romain Guy2bf68f02012-03-02 13:37:47 -0800253 /**
254 * Used for deferred updates.
255 */
256 bool deferredUpdateScheduled;
257 OpenGLRenderer* renderer;
258 DisplayList* displayList;
259 Rect dirtyRect;
260
Romain Guy9ace8f52011-07-07 20:50:11 -0700261private:
262 /**
263 * Name of the FBO used to render the layer. If the name is 0
264 * this layer is not backed by an FBO, but a simple texture.
265 */
266 GLuint fbo;
267
268 /**
269 * Indicates whether this layer has been used already.
270 */
271 bool empty;
272
273 /**
274 * The texture backing this layer.
275 */
276 Texture texture;
277
Romain Guyaa6c24c2011-04-28 18:40:04 -0700278 /**
279 * If set to true (by default), the layer can be reused.
280 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700281 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700282
283 /**
284 * When set to true, this layer must be treated as a texture
285 * layer.
286 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700287 bool textureLayer;
288
289 /**
290 * Indicates the render target.
291 */
292 GLenum renderTarget;
293
294 /**
295 * Color filter used to draw this layer. Optional.
296 */
297 SkiaColorFilter* colorFilter;
298
299 /**
300 * Opacity of the layer.
301 */
302 int alpha;
303 /**
304 * Blending mode of the layer.
305 */
306 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700307
308 /**
309 * Optional texture coordinates transform.
310 */
311 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700312
Romain Guy302a9df2011-08-16 13:55:02 -0700313 /**
314 * Optional transform.
315 */
316 mat4 transform;
317
Romain Guydda57022010-07-06 11:39:32 -0700318}; // struct Layer
319
320}; // namespace uirenderer
321}; // namespace android
322
Romain Guy5b3b3522010-10-27 18:57:51 -0700323#endif // ANDROID_HWUI_LAYER_H