blob: 181eb6c6bf4f6209326ab27a46028e268db0e2a2 [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
Chet Haase98d3a642012-09-26 10:27:40 -070051 void removeFbo();
Dave Burke56257af2012-09-25 20:30:09 -070052
Romain Guydda57022010-07-06 11:39:32 -070053 /**
Romain Guy9fc27812011-04-27 14:21:41 -070054 * Sets this layer's region to a rectangle. Computes the appropriate
55 * texture coordinates.
56 */
57 void setRegionAsRect() {
58 const android::Rect& bounds = region.getBounds();
59 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
60 bounds.rightBottom().x, bounds.rightBottom().y);
61
Romain Guy9ace8f52011-07-07 20:50:11 -070062 const float texX = 1.0f / float(texture.width);
63 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070064 const float height = layer.getHeight();
65 texCoords.set(
66 regionRect.left * texX, (height - regionRect.top) * texY,
67 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070068
69 regionRect.translate(layer.left, layer.top);
70 }
71
Romain Guy2bf68f02012-03-02 13:37:47 -080072 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
73 int left, int top, int right, int bottom) {
74 this->renderer = renderer;
75 this->displayList = displayList;
76 const Rect r(left, top, right, bottom);
77 dirtyRect.unionWith(r);
78 deferredUpdateScheduled = true;
79 }
80
Romain Guy9ace8f52011-07-07 20:50:11 -070081 inline uint32_t getWidth() {
82 return texture.width;
83 }
84
85 inline uint32_t getHeight() {
86 return texture.height;
87 }
88
89 void setSize(uint32_t width, uint32_t height) {
90 texture.width = width;
91 texture.height = height;
92 }
93
Chet Haased15ebf22012-09-05 11:40:29 -070094 ANDROID_API void setPaint(SkPaint* paint);
95
Romain Guy9ace8f52011-07-07 20:50:11 -070096 inline void setBlend(bool blend) {
97 texture.blend = blend;
98 }
99
100 inline bool isBlend() {
101 return texture.blend;
102 }
103
104 inline void setAlpha(int alpha) {
105 this->alpha = alpha;
106 }
107
108 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
109 this->alpha = alpha;
110 this->mode = mode;
111 }
112
113 inline int getAlpha() {
114 return alpha;
115 }
116
117 inline SkXfermode::Mode getMode() {
118 return mode;
119 }
120
121 inline void setEmpty(bool empty) {
122 this->empty = empty;
123 }
124
125 inline bool isEmpty() {
126 return empty;
127 }
128
129 inline void setFbo(GLuint fbo) {
130 this->fbo = fbo;
131 }
132
133 inline GLuint getFbo() {
134 return fbo;
135 }
136
Romain Guy9ace8f52011-07-07 20:50:11 -0700137 inline GLuint getTexture() {
138 return texture.id;
139 }
140
141 inline GLenum getRenderTarget() {
142 return renderTarget;
143 }
144
145 inline void setRenderTarget(GLenum renderTarget) {
146 this->renderTarget = renderTarget;
147 }
148
Romain Guyd21b6e12011-11-30 20:21:23 -0800149 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
150 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700151 }
152
Romain Guyd21b6e12011-11-30 20:21:23 -0800153 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
154 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700155 }
156
157 inline bool isCacheable() {
158 return cacheable;
159 }
160
161 inline void setCacheable(bool cacheable) {
162 this->cacheable = cacheable;
163 }
164
Romain Guy7c25aab2012-10-18 15:05:02 -0700165 inline bool isDirty() {
166 return dirty;
167 }
168
169 inline void setDirty(bool dirty) {
170 this->dirty = dirty;
171 }
172
Romain Guy9ace8f52011-07-07 20:50:11 -0700173 inline bool isTextureLayer() {
174 return textureLayer;
175 }
176
177 inline void setTextureLayer(bool textureLayer) {
178 this->textureLayer = textureLayer;
179 }
180
181 inline SkiaColorFilter* getColorFilter() {
182 return colorFilter;
183 }
184
Chet Haased15ebf22012-09-05 11:40:29 -0700185 ANDROID_API void setColorFilter(SkiaColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700186
187 inline void bindTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700188 if (texture.id) {
189 glBindTexture(renderTarget, texture.id);
190 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700191 }
192
193 inline void generateTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700194 if (!texture.id) {
195 glGenTextures(1, &texture.id);
196 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700197 }
198
199 inline void deleteTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700200 if (texture.id) {
201 glDeleteTextures(1, &texture.id);
202 texture.id = 0;
203 }
204 }
205
206 /**
207 * When the caller frees the texture itself, the caller
208 * must call this method to tell this layer that it lost
209 * the texture.
210 */
211 void clearTexture() {
212 texture.id = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -0700213 }
214
Romain Guy912a7b32011-07-26 18:57:28 -0700215 inline void deleteFbo() {
216 if (fbo) glDeleteFramebuffers(1, &fbo);
217 }
218
Romain Guy9ace8f52011-07-07 20:50:11 -0700219 inline void allocateTexture(GLenum format, GLenum storage) {
Romain Guyb2e2f242012-10-17 18:18:35 -0700220#if DEBUG_LAYERS
221 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
222#endif
Romain Guy9ace8f52011-07-07 20:50:11 -0700223 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL);
224 }
225
226 inline mat4& getTexTransform() {
227 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700228 }
229
Romain Guy302a9df2011-08-16 13:55:02 -0700230 inline mat4& getTransform() {
231 return transform;
232 }
233
Romain Guy9fc27812011-04-27 14:21:41 -0700234 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700235 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700236 */
237 Rect layer;
238 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700239 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700240 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700241 Rect texCoords;
242
Romain Guydda57022010-07-06 11:39:32 -0700243 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700244 * Dirty region indicating what parts of the layer
245 * have been drawn.
246 */
247 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700248 /**
249 * If the region is a rectangle, coordinates of the
250 * region are stored here.
251 */
252 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800253
254 /**
Romain Guyf219da52011-01-16 12:54:25 -0800255 * If the layer can be rendered as a mesh, this is non-null.
256 */
257 TextureVertex* mesh;
258 uint16_t* meshIndices;
259 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700260
Romain Guy2bf68f02012-03-02 13:37:47 -0800261 /**
262 * Used for deferred updates.
263 */
264 bool deferredUpdateScheduled;
265 OpenGLRenderer* renderer;
266 DisplayList* displayList;
267 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800268 bool debugDrawUpdate;
Romain Guy2bf68f02012-03-02 13:37:47 -0800269
Romain Guy9ace8f52011-07-07 20:50:11 -0700270private:
271 /**
272 * Name of the FBO used to render the layer. If the name is 0
273 * this layer is not backed by an FBO, but a simple texture.
274 */
275 GLuint fbo;
276
277 /**
278 * Indicates whether this layer has been used already.
279 */
280 bool empty;
281
282 /**
283 * The texture backing this layer.
284 */
285 Texture texture;
286
Romain Guyaa6c24c2011-04-28 18:40:04 -0700287 /**
288 * If set to true (by default), the layer can be reused.
289 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700290 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700291
292 /**
293 * When set to true, this layer must be treated as a texture
294 * layer.
295 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700296 bool textureLayer;
297
298 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700299 * When set to true, this layer is dirty and should be cleared
300 * before any rendering occurs.
301 */
302 bool dirty;
303
304 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700305 * Indicates the render target.
306 */
307 GLenum renderTarget;
308
309 /**
310 * Color filter used to draw this layer. Optional.
311 */
312 SkiaColorFilter* colorFilter;
313
314 /**
315 * Opacity of the layer.
316 */
317 int alpha;
318 /**
319 * Blending mode of the layer.
320 */
321 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700322
323 /**
324 * Optional texture coordinates transform.
325 */
326 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700327
Romain Guy302a9df2011-08-16 13:55:02 -0700328 /**
329 * Optional transform.
330 */
331 mat4 transform;
332
Romain Guydda57022010-07-06 11:39:32 -0700333}; // struct Layer
334
335}; // namespace uirenderer
336}; // namespace android
337
Romain Guy5b3b3522010-10-27 18:57:51 -0700338#endif // ANDROID_HWUI_LAYER_H