blob: 420073ac3b092bd2adc51ba3cdc6d4b38a8eeb33 [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 Haased15ebf22012-09-05 11:40:29 -070048
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
Romain Guydda570202010-07-06 11:39:32 -070052 /**
Romain Guy9fc27812011-04-27 14:21:41 -070053 * Sets this layer's region to a rectangle. Computes the appropriate
54 * texture coordinates.
55 */
56 void setRegionAsRect() {
57 const android::Rect& bounds = region.getBounds();
58 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
59 bounds.rightBottom().x, bounds.rightBottom().y);
60
Romain Guy9ace8f52011-07-07 20:50:11 -070061 const float texX = 1.0f / float(texture.width);
62 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070063 const float height = layer.getHeight();
64 texCoords.set(
65 regionRect.left * texX, (height - regionRect.top) * texY,
66 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070067
68 regionRect.translate(layer.left, layer.top);
69 }
70
Romain Guy2bf68f02012-03-02 13:37:47 -080071 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
72 int left, int top, int right, int bottom) {
73 this->renderer = renderer;
74 this->displayList = displayList;
75 const Rect r(left, top, right, bottom);
76 dirtyRect.unionWith(r);
77 deferredUpdateScheduled = true;
78 }
79
Romain Guy9ace8f52011-07-07 20:50:11 -070080 inline uint32_t getWidth() {
81 return texture.width;
82 }
83
84 inline uint32_t getHeight() {
85 return texture.height;
86 }
87
88 void setSize(uint32_t width, uint32_t height) {
89 texture.width = width;
90 texture.height = height;
91 }
92
Chet Haased15ebf22012-09-05 11:40:29 -070093 ANDROID_API void setPaint(SkPaint* paint);
94
Romain Guy9ace8f52011-07-07 20:50:11 -070095 inline void setBlend(bool blend) {
96 texture.blend = blend;
97 }
98
99 inline bool isBlend() {
100 return texture.blend;
101 }
102
103 inline void setAlpha(int alpha) {
104 this->alpha = alpha;
105 }
106
107 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
108 this->alpha = alpha;
109 this->mode = mode;
110 }
111
112 inline int getAlpha() {
113 return alpha;
114 }
115
116 inline SkXfermode::Mode getMode() {
117 return mode;
118 }
119
120 inline void setEmpty(bool empty) {
121 this->empty = empty;
122 }
123
124 inline bool isEmpty() {
125 return empty;
126 }
127
128 inline void setFbo(GLuint fbo) {
129 this->fbo = fbo;
130 }
131
132 inline GLuint getFbo() {
133 return fbo;
134 }
135
136 inline GLuint* getTexturePointer() {
137 return &texture.id;
138 }
139
140 inline GLuint getTexture() {
141 return texture.id;
142 }
143
144 inline GLenum getRenderTarget() {
145 return renderTarget;
146 }
147
148 inline void setRenderTarget(GLenum renderTarget) {
149 this->renderTarget = renderTarget;
150 }
151
Romain Guyd21b6e12011-11-30 20:21:23 -0800152 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
153 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700154 }
155
Romain Guyd21b6e12011-11-30 20:21:23 -0800156 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
157 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700158 }
159
160 inline bool isCacheable() {
161 return cacheable;
162 }
163
164 inline void setCacheable(bool cacheable) {
165 this->cacheable = cacheable;
166 }
167
168 inline bool isTextureLayer() {
169 return textureLayer;
170 }
171
172 inline void setTextureLayer(bool textureLayer) {
173 this->textureLayer = textureLayer;
174 }
175
176 inline SkiaColorFilter* getColorFilter() {
177 return colorFilter;
178 }
179
Chet Haased15ebf22012-09-05 11:40:29 -0700180 ANDROID_API void setColorFilter(SkiaColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700181
182 inline void bindTexture() {
183 glBindTexture(renderTarget, texture.id);
184 }
185
186 inline void generateTexture() {
187 glGenTextures(1, &texture.id);
188 }
189
190 inline void deleteTexture() {
191 if (texture.id) glDeleteTextures(1, &texture.id);
192 }
193
Romain Guy912a7b32011-07-26 18:57:28 -0700194 inline void deleteFbo() {
195 if (fbo) glDeleteFramebuffers(1, &fbo);
196 }
197
Romain Guy9ace8f52011-07-07 20:50:11 -0700198 inline void allocateTexture(GLenum format, GLenum storage) {
199 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL);
200 }
201
202 inline mat4& getTexTransform() {
203 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700204 }
205
Romain Guy302a9df2011-08-16 13:55:02 -0700206 inline mat4& getTransform() {
207 return transform;
208 }
209
Romain Guy9fc27812011-04-27 14:21:41 -0700210 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700211 * Bounds of the layer.
Romain Guydda570202010-07-06 11:39:32 -0700212 */
213 Rect layer;
214 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700215 * Texture coordinates of the layer.
Romain Guydda570202010-07-06 11:39:32 -0700216 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700217 Rect texCoords;
218
Romain Guydda570202010-07-06 11:39:32 -0700219 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700220 * Dirty region indicating what parts of the layer
221 * have been drawn.
222 */
223 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700224 /**
225 * If the region is a rectangle, coordinates of the
226 * region are stored here.
227 */
228 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800229
230 /**
Romain Guyf219da52011-01-16 12:54:25 -0800231 * If the layer can be rendered as a mesh, this is non-null.
232 */
233 TextureVertex* mesh;
234 uint16_t* meshIndices;
235 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700236
Romain Guy2bf68f02012-03-02 13:37:47 -0800237 /**
238 * Used for deferred updates.
239 */
240 bool deferredUpdateScheduled;
241 OpenGLRenderer* renderer;
242 DisplayList* displayList;
243 Rect dirtyRect;
244
Romain Guy9ace8f52011-07-07 20:50:11 -0700245private:
246 /**
247 * Name of the FBO used to render the layer. If the name is 0
248 * this layer is not backed by an FBO, but a simple texture.
249 */
250 GLuint fbo;
251
252 /**
253 * Indicates whether this layer has been used already.
254 */
255 bool empty;
256
257 /**
258 * The texture backing this layer.
259 */
260 Texture texture;
261
Romain Guyaa6c24c2011-04-28 18:40:04 -0700262 /**
263 * If set to true (by default), the layer can be reused.
264 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700265 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700266
267 /**
268 * When set to true, this layer must be treated as a texture
269 * layer.
270 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700271 bool textureLayer;
272
273 /**
274 * Indicates the render target.
275 */
276 GLenum renderTarget;
277
278 /**
279 * Color filter used to draw this layer. Optional.
280 */
281 SkiaColorFilter* colorFilter;
282
283 /**
284 * Opacity of the layer.
285 */
286 int alpha;
287 /**
288 * Blending mode of the layer.
289 */
290 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700291
292 /**
293 * Optional texture coordinates transform.
294 */
295 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700296
Romain Guy302a9df2011-08-16 13:55:02 -0700297 /**
298 * Optional transform.
299 */
300 mat4 transform;
301
Romain Guydda570202010-07-06 11:39:32 -0700302}; // struct Layer
303
304}; // namespace uirenderer
305}; // namespace android
306
Romain Guy5b3b3522010-10-27 18:57:51 -0700307#endif // ANDROID_HWUI_LAYER_H