blob: 9ef48945df236ba8aed4a0d24e74020234da2f09 [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 {
Chet Haase603f6de2012-09-14 15:31:25 -070048 Layer(const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070049 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070050
Romain Guy8ce00302013-01-15 18:51:42 -080051 /**
52 * Calling this method will remove (either by recycling or
53 * destroying) the associated FBO, if present, and any render
54 * buffer (stencil for instance.)
55 */
56 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070057
Romain Guydda57022010-07-06 11:39:32 -070058 /**
Romain Guy9fc27812011-04-27 14:21:41 -070059 * Sets this layer's region to a rectangle. Computes the appropriate
60 * texture coordinates.
61 */
62 void setRegionAsRect() {
63 const android::Rect& bounds = region.getBounds();
64 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
65 bounds.rightBottom().x, bounds.rightBottom().y);
66
Romain Guy9ace8f52011-07-07 20:50:11 -070067 const float texX = 1.0f / float(texture.width);
68 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070069 const float height = layer.getHeight();
70 texCoords.set(
71 regionRect.left * texX, (height - regionRect.top) * texY,
72 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070073
74 regionRect.translate(layer.left, layer.top);
75 }
76
Romain Guy2bf68f02012-03-02 13:37:47 -080077 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
78 int left, int top, int right, int bottom) {
79 this->renderer = renderer;
80 this->displayList = displayList;
81 const Rect r(left, top, right, bottom);
82 dirtyRect.unionWith(r);
83 deferredUpdateScheduled = true;
84 }
85
Romain Guy9ace8f52011-07-07 20:50:11 -070086 inline uint32_t getWidth() {
87 return texture.width;
88 }
89
90 inline uint32_t getHeight() {
91 return texture.height;
92 }
93
94 void setSize(uint32_t width, uint32_t height) {
95 texture.width = width;
96 texture.height = height;
97 }
98
Chet Haased15ebf22012-09-05 11:40:29 -070099 ANDROID_API void setPaint(SkPaint* paint);
100
Romain Guy9ace8f52011-07-07 20:50:11 -0700101 inline void setBlend(bool blend) {
102 texture.blend = blend;
103 }
104
105 inline bool isBlend() {
106 return texture.blend;
107 }
108
109 inline void setAlpha(int alpha) {
110 this->alpha = alpha;
111 }
112
113 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
114 this->alpha = alpha;
115 this->mode = mode;
116 }
117
118 inline int getAlpha() {
119 return alpha;
120 }
121
122 inline SkXfermode::Mode getMode() {
123 return mode;
124 }
125
126 inline void setEmpty(bool empty) {
127 this->empty = empty;
128 }
129
130 inline bool isEmpty() {
131 return empty;
132 }
133
134 inline void setFbo(GLuint fbo) {
135 this->fbo = fbo;
136 }
137
138 inline GLuint getFbo() {
139 return fbo;
140 }
141
Romain Guy8ce00302013-01-15 18:51:42 -0800142 inline void setStencilRenderBuffer(GLuint renderBuffer) {
143 this->stencil = renderBuffer;
144 }
145
146 inline GLuint getStencilRenderBuffer() {
147 return stencil;
148 }
149
Romain Guy9ace8f52011-07-07 20:50:11 -0700150 inline GLuint getTexture() {
151 return texture.id;
152 }
153
154 inline GLenum getRenderTarget() {
155 return renderTarget;
156 }
157
158 inline void setRenderTarget(GLenum renderTarget) {
159 this->renderTarget = renderTarget;
160 }
161
Romain Guyd21b6e12011-11-30 20:21:23 -0800162 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
163 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700164 }
165
Romain Guyd21b6e12011-11-30 20:21:23 -0800166 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
167 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700168 }
169
170 inline bool isCacheable() {
171 return cacheable;
172 }
173
174 inline void setCacheable(bool cacheable) {
175 this->cacheable = cacheable;
176 }
177
Romain Guy7c25aab2012-10-18 15:05:02 -0700178 inline bool isDirty() {
179 return dirty;
180 }
181
182 inline void setDirty(bool dirty) {
183 this->dirty = dirty;
184 }
185
Romain Guy9ace8f52011-07-07 20:50:11 -0700186 inline bool isTextureLayer() {
187 return textureLayer;
188 }
189
190 inline void setTextureLayer(bool textureLayer) {
191 this->textureLayer = textureLayer;
192 }
193
194 inline SkiaColorFilter* getColorFilter() {
195 return colorFilter;
196 }
197
Chet Haased15ebf22012-09-05 11:40:29 -0700198 ANDROID_API void setColorFilter(SkiaColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700199
200 inline void bindTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700201 if (texture.id) {
202 glBindTexture(renderTarget, texture.id);
203 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700204 }
205
206 inline void generateTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700207 if (!texture.id) {
208 glGenTextures(1, &texture.id);
209 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700210 }
211
212 inline void deleteTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700213 if (texture.id) {
214 glDeleteTextures(1, &texture.id);
215 texture.id = 0;
216 }
217 }
218
219 /**
220 * When the caller frees the texture itself, the caller
221 * must call this method to tell this layer that it lost
222 * the texture.
223 */
224 void clearTexture() {
225 texture.id = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -0700226 }
227
228 inline void allocateTexture(GLenum format, GLenum storage) {
Romain Guyb2e2f242012-10-17 18:18:35 -0700229#if DEBUG_LAYERS
230 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
231#endif
Romain Guy9ace8f52011-07-07 20:50:11 -0700232 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL);
233 }
234
235 inline mat4& getTexTransform() {
236 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700237 }
238
Romain Guy302a9df2011-08-16 13:55:02 -0700239 inline mat4& getTransform() {
240 return transform;
241 }
242
Romain Guy9fc27812011-04-27 14:21:41 -0700243 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700244 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700245 */
246 Rect layer;
247 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700248 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700249 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700250 Rect texCoords;
251
Romain Guydda57022010-07-06 11:39:32 -0700252 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700253 * Dirty region indicating what parts of the layer
254 * have been drawn.
255 */
256 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700257 /**
258 * If the region is a rectangle, coordinates of the
259 * region are stored here.
260 */
261 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800262
263 /**
Romain Guyf219da52011-01-16 12:54:25 -0800264 * If the layer can be rendered as a mesh, this is non-null.
265 */
266 TextureVertex* mesh;
267 uint16_t* meshIndices;
268 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700269
Romain Guy2bf68f02012-03-02 13:37:47 -0800270 /**
271 * Used for deferred updates.
272 */
273 bool deferredUpdateScheduled;
274 OpenGLRenderer* renderer;
275 DisplayList* displayList;
276 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800277 bool debugDrawUpdate;
Romain Guy2bf68f02012-03-02 13:37:47 -0800278
Romain Guy9ace8f52011-07-07 20:50:11 -0700279private:
280 /**
281 * Name of the FBO used to render the layer. If the name is 0
282 * this layer is not backed by an FBO, but a simple texture.
283 */
284 GLuint fbo;
285
286 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800287 * Name of the render buffer used as the stencil buffer. If the
288 * name is 0, this layer does not have a stencil buffer.
289 */
290 GLuint stencil;
291
292 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700293 * Indicates whether this layer has been used already.
294 */
295 bool empty;
296
297 /**
298 * The texture backing this layer.
299 */
300 Texture texture;
301
Romain Guyaa6c24c2011-04-28 18:40:04 -0700302 /**
303 * If set to true (by default), the layer can be reused.
304 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700305 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700306
307 /**
308 * When set to true, this layer must be treated as a texture
309 * layer.
310 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700311 bool textureLayer;
312
313 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700314 * When set to true, this layer is dirty and should be cleared
315 * before any rendering occurs.
316 */
317 bool dirty;
318
319 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700320 * Indicates the render target.
321 */
322 GLenum renderTarget;
323
324 /**
325 * Color filter used to draw this layer. Optional.
326 */
327 SkiaColorFilter* colorFilter;
328
329 /**
330 * Opacity of the layer.
331 */
332 int alpha;
333 /**
334 * Blending mode of the layer.
335 */
336 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700337
338 /**
339 * Optional texture coordinates transform.
340 */
341 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700342
Romain Guy302a9df2011-08-16 13:55:02 -0700343 /**
344 * Optional transform.
345 */
346 mat4 transform;
347
Romain Guydda57022010-07-06 11:39:32 -0700348}; // struct Layer
349
350}; // namespace uirenderer
351}; // namespace android
352
Romain Guy5b3b3522010-10-27 18:57:51 -0700353#endif // ANDROID_HWUI_LAYER_H