blob: 59c66d7ce4d3276db078986ba3ee990a6076b8ce [file] [log] [blame]
Romain Guydda570202010-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 Guydda570202010-07-06 11:39:32 -070019
Romain Guyf7f93552010-07-08 19:17:03 -070020#include <sys/types.h>
21
Romain Guydda570202010-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 Guydda570202010-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 Guydda570202010-07-06 11:39:32 -070032
33namespace android {
34namespace uirenderer {
35
Romain Guy8550c4c2010-10-08 15:49:53 -070036///////////////////////////////////////////////////////////////////////////////
37// Layers
38///////////////////////////////////////////////////////////////////////////////
Romain Guydda570202010-07-06 11:39:32 -070039
Romain Guy2bf68f02012-03-02 13:37:47 -080040// Forward declarations
41class OpenGLRenderer;
42class DisplayList;
43
Romain Guydda570202010-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 Guydda570202010-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 Guydda570202010-07-06 11:39:32 -070051 /**
Romain Guy9fc27812011-04-27 14:21:41 -070052 * Sets this layer's region to a rectangle. Computes the appropriate
53 * texture coordinates.
54 */
55 void setRegionAsRect() {
56 const android::Rect& bounds = region.getBounds();
57 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
58 bounds.rightBottom().x, bounds.rightBottom().y);
59
Romain Guy9ace8f52011-07-07 20:50:11 -070060 const float texX = 1.0f / float(texture.width);
61 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070062 const float height = layer.getHeight();
63 texCoords.set(
64 regionRect.left * texX, (height - regionRect.top) * texY,
65 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070066
67 regionRect.translate(layer.left, layer.top);
68 }
69
Romain Guy2bf68f02012-03-02 13:37:47 -080070 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
71 int left, int top, int right, int bottom) {
72 this->renderer = renderer;
73 this->displayList = displayList;
74 const Rect r(left, top, right, bottom);
75 dirtyRect.unionWith(r);
76 deferredUpdateScheduled = true;
77 }
78
Romain Guy9ace8f52011-07-07 20:50:11 -070079 inline uint32_t getWidth() {
80 return texture.width;
81 }
82
83 inline uint32_t getHeight() {
84 return texture.height;
85 }
86
87 void setSize(uint32_t width, uint32_t height) {
88 texture.width = width;
89 texture.height = height;
90 }
91
Chet Haased15ebf22012-09-05 11:40:29 -070092 ANDROID_API void setPaint(SkPaint* paint);
93
Romain Guy9ace8f52011-07-07 20:50:11 -070094 inline void setBlend(bool blend) {
95 texture.blend = blend;
96 }
97
98 inline bool isBlend() {
99 return texture.blend;
100 }
101
102 inline void setAlpha(int alpha) {
103 this->alpha = alpha;
104 }
105
106 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
107 this->alpha = alpha;
108 this->mode = mode;
109 }
110
111 inline int getAlpha() {
112 return alpha;
113 }
114
115 inline SkXfermode::Mode getMode() {
116 return mode;
117 }
118
119 inline void setEmpty(bool empty) {
120 this->empty = empty;
121 }
122
123 inline bool isEmpty() {
124 return empty;
125 }
126
127 inline void setFbo(GLuint fbo) {
128 this->fbo = fbo;
129 }
130
131 inline GLuint getFbo() {
132 return fbo;
133 }
134
Romain Guy9ace8f52011-07-07 20:50:11 -0700135 inline GLuint getTexture() {
136 return texture.id;
137 }
138
139 inline GLenum getRenderTarget() {
140 return renderTarget;
141 }
142
143 inline void setRenderTarget(GLenum renderTarget) {
144 this->renderTarget = renderTarget;
145 }
146
Romain Guyd21b6e12011-11-30 20:21:23 -0800147 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
148 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700149 }
150
Romain Guyd21b6e12011-11-30 20:21:23 -0800151 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
152 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700153 }
154
155 inline bool isCacheable() {
156 return cacheable;
157 }
158
159 inline void setCacheable(bool cacheable) {
160 this->cacheable = cacheable;
161 }
162
163 inline bool isTextureLayer() {
164 return textureLayer;
165 }
166
167 inline void setTextureLayer(bool textureLayer) {
168 this->textureLayer = textureLayer;
169 }
170
171 inline SkiaColorFilter* getColorFilter() {
172 return colorFilter;
173 }
174
Chet Haased15ebf22012-09-05 11:40:29 -0700175 ANDROID_API void setColorFilter(SkiaColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700176
177 inline void bindTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700178 if (texture.id) {
179 glBindTexture(renderTarget, texture.id);
180 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700181 }
182
183 inline void generateTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700184 if (!texture.id) {
185 glGenTextures(1, &texture.id);
186 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700187 }
188
189 inline void deleteTexture() {
Romain Guyef09a212012-09-25 12:17:14 -0700190 if (texture.id) {
191 glDeleteTextures(1, &texture.id);
192 texture.id = 0;
193 }
194 }
195
196 /**
197 * When the caller frees the texture itself, the caller
198 * must call this method to tell this layer that it lost
199 * the texture.
200 */
201 void clearTexture() {
202 texture.id = 0;
Romain Guy9ace8f52011-07-07 20:50:11 -0700203 }
204
Romain Guy912a7b32011-07-26 18:57:28 -0700205 inline void deleteFbo() {
206 if (fbo) glDeleteFramebuffers(1, &fbo);
207 }
208
Romain Guy9ace8f52011-07-07 20:50:11 -0700209 inline void allocateTexture(GLenum format, GLenum storage) {
210 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL);
211 }
212
213 inline mat4& getTexTransform() {
214 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700215 }
216
Romain Guy302a9df2011-08-16 13:55:02 -0700217 inline mat4& getTransform() {
218 return transform;
219 }
220
Romain Guy9fc27812011-04-27 14:21:41 -0700221 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700222 * Bounds of the layer.
Romain Guydda570202010-07-06 11:39:32 -0700223 */
224 Rect layer;
225 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700226 * Texture coordinates of the layer.
Romain Guydda570202010-07-06 11:39:32 -0700227 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700228 Rect texCoords;
229
Romain Guydda570202010-07-06 11:39:32 -0700230 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700231 * Dirty region indicating what parts of the layer
232 * have been drawn.
233 */
234 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700235 /**
236 * If the region is a rectangle, coordinates of the
237 * region are stored here.
238 */
239 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800240
241 /**
Romain Guyf219da52011-01-16 12:54:25 -0800242 * If the layer can be rendered as a mesh, this is non-null.
243 */
244 TextureVertex* mesh;
245 uint16_t* meshIndices;
246 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700247
Romain Guy2bf68f02012-03-02 13:37:47 -0800248 /**
249 * Used for deferred updates.
250 */
251 bool deferredUpdateScheduled;
252 OpenGLRenderer* renderer;
253 DisplayList* displayList;
254 Rect dirtyRect;
255
Romain Guy9ace8f52011-07-07 20:50:11 -0700256private:
257 /**
258 * Name of the FBO used to render the layer. If the name is 0
259 * this layer is not backed by an FBO, but a simple texture.
260 */
261 GLuint fbo;
262
263 /**
264 * Indicates whether this layer has been used already.
265 */
266 bool empty;
267
268 /**
269 * The texture backing this layer.
270 */
271 Texture texture;
272
Romain Guyaa6c24c2011-04-28 18:40:04 -0700273 /**
274 * If set to true (by default), the layer can be reused.
275 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700276 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700277
278 /**
279 * When set to true, this layer must be treated as a texture
280 * layer.
281 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700282 bool textureLayer;
283
284 /**
285 * Indicates the render target.
286 */
287 GLenum renderTarget;
288
289 /**
290 * Color filter used to draw this layer. Optional.
291 */
292 SkiaColorFilter* colorFilter;
293
294 /**
295 * Opacity of the layer.
296 */
297 int alpha;
298 /**
299 * Blending mode of the layer.
300 */
301 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700302
303 /**
304 * Optional texture coordinates transform.
305 */
306 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700307
Romain Guy302a9df2011-08-16 13:55:02 -0700308 /**
309 * Optional transform.
310 */
311 mat4 transform;
312
Romain Guydda570202010-07-06 11:39:32 -0700313}; // struct Layer
314
315}; // namespace uirenderer
316}; // namespace android
317
Romain Guy5b3b3522010-10-27 18:57:51 -0700318#endif // ANDROID_HWUI_LAYER_H