blob: efb9f6b819577b5052e0bc8e02150ae43704b699 [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
Derek Sollenbergerca79cf62012-08-14 16:44:52 -040026#include <SkPaint.h>
Romain Guydda57022010-07-06 11:39:32 -070027#include <SkXfermode.h>
28
29#include "Rect.h"
Romain Guy171c5922011-01-06 10:04:23 -080030#include "SkiaColorFilter.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070031#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080032#include "Vertex.h"
Romain Guydda57022010-07-06 11:39:32 -070033
34namespace android {
35namespace uirenderer {
36
Romain Guy8550c4c2010-10-08 15:49:53 -070037///////////////////////////////////////////////////////////////////////////////
38// Layers
39///////////////////////////////////////////////////////////////////////////////
Romain Guydda57022010-07-06 11:39:32 -070040
Romain Guy2bf68f02012-03-02 13:37:47 -080041// Forward declarations
42class OpenGLRenderer;
43class DisplayList;
44
Romain Guydda57022010-07-06 11:39:32 -070045/**
Romain Guyeb993562010-10-05 18:14:38 -070046 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070047 */
48struct Layer {
Chet Haase603f6de2012-09-14 15:31:25 -070049 Layer(const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070050 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070051
Chet Haase98d3a642012-09-26 10:27:40 -070052 void removeFbo();
Dave Burke56257af2012-09-25 20:30:09 -070053
Romain Guydda57022010-07-06 11:39:32 -070054 /**
Romain Guy9fc27812011-04-27 14:21:41 -070055 * Sets this layer's region to a rectangle. Computes the appropriate
56 * texture coordinates.
57 */
58 void setRegionAsRect() {
59 const android::Rect& bounds = region.getBounds();
60 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
61 bounds.rightBottom().x, bounds.rightBottom().y);
62
Romain Guy9ace8f52011-07-07 20:50:11 -070063 const float texX = 1.0f / float(texture.width);
64 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070065 const float height = layer.getHeight();
66 texCoords.set(
67 regionRect.left * texX, (height - regionRect.top) * texY,
68 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070069
70 regionRect.translate(layer.left, layer.top);
71 }
72
Romain Guy2bf68f02012-03-02 13:37:47 -080073 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
74 int left, int top, int right, int bottom) {
75 this->renderer = renderer;
76 this->displayList = displayList;
77 const Rect r(left, top, right, bottom);
78 dirtyRect.unionWith(r);
79 deferredUpdateScheduled = true;
80 }
81
Romain Guy9ace8f52011-07-07 20:50:11 -070082 inline uint32_t getWidth() {
83 return texture.width;
84 }
85
86 inline uint32_t getHeight() {
87 return texture.height;
88 }
89
90 void setSize(uint32_t width, uint32_t height) {
91 texture.width = width;
92 texture.height = height;
93 }
94
Chet Haased15ebf22012-09-05 11:40:29 -070095 ANDROID_API void setPaint(SkPaint* paint);
96
Romain Guy9ace8f52011-07-07 20:50:11 -070097 inline void setBlend(bool blend) {
98 texture.blend = blend;
99 }
100
101 inline bool isBlend() {
102 return texture.blend;
103 }
104
105 inline void setAlpha(int alpha) {
106 this->alpha = alpha;
107 }
108
109 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
110 this->alpha = alpha;
111 this->mode = mode;
112 }
113
114 inline int getAlpha() {
115 return alpha;
116 }
117
118 inline SkXfermode::Mode getMode() {
119 return mode;
120 }
121
122 inline void setEmpty(bool empty) {
123 this->empty = empty;
124 }
125
126 inline bool isEmpty() {
127 return empty;
128 }
129
130 inline void setFbo(GLuint fbo) {
131 this->fbo = fbo;
132 }
133
134 inline GLuint getFbo() {
135 return fbo;
136 }
137
Romain Guy9ace8f52011-07-07 20:50:11 -0700138 inline GLuint getTexture() {
139 return texture.id;
140 }
141
142 inline GLenum getRenderTarget() {
143 return renderTarget;
144 }
145
146 inline void setRenderTarget(GLenum renderTarget) {
147 this->renderTarget = renderTarget;
148 }
149
Romain Guyd21b6e12011-11-30 20:21:23 -0800150 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
151 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700152 }
153
Romain Guyd21b6e12011-11-30 20:21:23 -0800154 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
155 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700156 }
157
158 inline bool isCacheable() {
159 return cacheable;
160 }
161
162 inline void setCacheable(bool cacheable) {
163 this->cacheable = cacheable;
164 }
165
Romain Guy7c25aab2012-10-18 15:05:02 -0700166 inline bool isDirty() {
167 return dirty;
168 }
169
170 inline void setDirty(bool dirty) {
171 this->dirty = dirty;
172 }
173
Romain Guy9ace8f52011-07-07 20:50:11 -0700174 inline bool isTextureLayer() {
175 return textureLayer;
176 }
177
178 inline void setTextureLayer(bool textureLayer) {
179 this->textureLayer = textureLayer;
180 }
181
182 inline SkiaColorFilter* getColorFilter() {
183 return colorFilter;
184 }
185
Chet Haased15ebf22012-09-05 11:40:29 -0700186 ANDROID_API void setColorFilter(SkiaColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700187
188 inline void bindTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700189 if (texture.id) {
190 glBindTexture(renderTarget, texture.id);
191 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700192 }
193
194 inline void generateTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700195 if (!texture.id) {
196 glGenTextures(1, &texture.id);
197 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700198 }
199
200 inline void deleteTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700201 if (texture.id) {
202 glDeleteTextures(1, &texture.id);
203 texture.id = 0;
204 }
205 }
206
207 /**
208 * When the caller frees the texture itself, the caller
209 * must call this method to tell this layer that it lost
210 * the texture.
211 */
212 void clearTexture() {
213 texture.id = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -0700214 }
215
Romain Guy912a7b32011-07-26 18:57:28 -0700216 inline void deleteFbo() {
217 if (fbo) glDeleteFramebuffers(1, &fbo);
218 }
219
Romain Guy9ace8f52011-07-07 20:50:11 -0700220 inline void allocateTexture(GLenum format, GLenum storage) {
Romain Guyb2e2f242012-10-17 18:18:35 -0700221#if DEBUG_LAYERS
222 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight());
223#endif
Romain Guy9ace8f52011-07-07 20:50:11 -0700224 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL);
225 }
226
227 inline mat4& getTexTransform() {
228 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700229 }
230
Romain Guy302a9df2011-08-16 13:55:02 -0700231 inline mat4& getTransform() {
232 return transform;
233 }
234
Romain Guy9fc27812011-04-27 14:21:41 -0700235 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700236 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700237 */
238 Rect layer;
239 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700240 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700241 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700242 Rect texCoords;
243
Romain Guydda57022010-07-06 11:39:32 -0700244 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700245 * Dirty region indicating what parts of the layer
246 * have been drawn.
247 */
248 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700249 /**
250 * If the region is a rectangle, coordinates of the
251 * region are stored here.
252 */
253 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800254
255 /**
Romain Guyf219da52011-01-16 12:54:25 -0800256 * If the layer can be rendered as a mesh, this is non-null.
257 */
258 TextureVertex* mesh;
259 uint16_t* meshIndices;
260 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700261
Romain Guy2bf68f02012-03-02 13:37:47 -0800262 /**
263 * Used for deferred updates.
264 */
265 bool deferredUpdateScheduled;
266 OpenGLRenderer* renderer;
267 DisplayList* displayList;
268 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800269 bool debugDrawUpdate;
Romain Guy2bf68f02012-03-02 13:37:47 -0800270
Romain Guy9ace8f52011-07-07 20:50:11 -0700271private:
272 /**
273 * Name of the FBO used to render the layer. If the name is 0
274 * this layer is not backed by an FBO, but a simple texture.
275 */
276 GLuint fbo;
277
278 /**
279 * Indicates whether this layer has been used already.
280 */
281 bool empty;
282
283 /**
284 * The texture backing this layer.
285 */
286 Texture texture;
287
Romain Guyaa6c24c2011-04-28 18:40:04 -0700288 /**
289 * If set to true (by default), the layer can be reused.
290 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700291 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700292
293 /**
294 * When set to true, this layer must be treated as a texture
295 * layer.
296 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700297 bool textureLayer;
298
299 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700300 * When set to true, this layer is dirty and should be cleared
301 * before any rendering occurs.
302 */
303 bool dirty;
304
305 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700306 * Indicates the render target.
307 */
308 GLenum renderTarget;
309
310 /**
311 * Color filter used to draw this layer. Optional.
312 */
313 SkiaColorFilter* colorFilter;
314
315 /**
316 * Opacity of the layer.
317 */
318 int alpha;
319 /**
320 * Blending mode of the layer.
321 */
322 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700323
324 /**
325 * Optional texture coordinates transform.
326 */
327 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700328
Romain Guy302a9df2011-08-16 13:55:02 -0700329 /**
330 * Optional transform.
331 */
332 mat4 transform;
333
Romain Guydda57022010-07-06 11:39:32 -0700334}; // struct Layer
335
336}; // namespace uirenderer
337}; // namespace android
338
Romain Guy5b3b3522010-10-27 18:57:51 -0700339#endif // ANDROID_HWUI_LAYER_H