blob: ebd554357ab3ddfd170c8617967e0071f84534f7 [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 Guy3bbacf22013-02-06 16:51:04 -080030#include "RenderBuffer.h"
Romain Guy171c5922011-01-06 10:04:23 -080031#include "SkiaColorFilter.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070032#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080033#include "Vertex.h"
Romain Guydda57022010-07-06 11:39:32 -070034
35namespace android {
36namespace uirenderer {
37
Romain Guy8550c4c2010-10-08 15:49:53 -070038///////////////////////////////////////////////////////////////////////////////
39// Layers
40///////////////////////////////////////////////////////////////////////////////
Romain Guydda57022010-07-06 11:39:32 -070041
Romain Guy2bf68f02012-03-02 13:37:47 -080042// Forward declarations
Romain Guy8aa195d2013-06-04 18:00:09 -070043class Caches;
Romain Guy2bf68f02012-03-02 13:37:47 -080044class OpenGLRenderer;
45class DisplayList;
Romain Guy96885eb2013-03-26 15:05:58 -070046class DeferredDisplayList;
47class DeferStateStruct;
Romain Guy2bf68f02012-03-02 13:37:47 -080048
Romain Guydda57022010-07-06 11:39:32 -070049/**
Romain Guyeb993562010-10-05 18:14:38 -070050 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070051 */
52struct Layer {
Chet Haase603f6de2012-09-14 15:31:25 -070053 Layer(const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070054 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070055
Romain Guy2055aba2013-01-18 16:42:51 -080056 static uint32_t computeIdealWidth(uint32_t layerWidth);
57 static uint32_t computeIdealHeight(uint32_t layerHeight);
58
Romain Guy8ce00302013-01-15 18:51:42 -080059 /**
60 * Calling this method will remove (either by recycling or
61 * destroying) the associated FBO, if present, and any render
62 * buffer (stencil for instance.)
63 */
64 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070065
Romain Guydda57022010-07-06 11:39:32 -070066 /**
Romain Guy9fc27812011-04-27 14:21:41 -070067 * Sets this layer's region to a rectangle. Computes the appropriate
68 * texture coordinates.
69 */
70 void setRegionAsRect() {
71 const android::Rect& bounds = region.getBounds();
72 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
73 bounds.rightBottom().x, bounds.rightBottom().y);
74
Romain Guy9ace8f52011-07-07 20:50:11 -070075 const float texX = 1.0f / float(texture.width);
76 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070077 const float height = layer.getHeight();
78 texCoords.set(
79 regionRect.left * texX, (height - regionRect.top) * texY,
80 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070081
82 regionRect.translate(layer.left, layer.top);
83 }
84
Romain Guy2bf68f02012-03-02 13:37:47 -080085 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
86 int left, int top, int right, int bottom) {
87 this->renderer = renderer;
88 this->displayList = displayList;
89 const Rect r(left, top, right, bottom);
90 dirtyRect.unionWith(r);
91 deferredUpdateScheduled = true;
92 }
93
Romain Guy3bbacf22013-02-06 16:51:04 -080094 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070095 return texture.width;
96 }
97
Romain Guy3bbacf22013-02-06 16:51:04 -080098 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070099 return texture.height;
100 }
101
Romain Guy2055aba2013-01-18 16:42:51 -0800102 /**
103 * Resize the layer and its texture if needed.
104 *
105 * @param width The new width of the layer
106 * @param height The new height of the layer
107 *
108 * @return True if the layer was resized or nothing happened, false if
109 * a failure occurred during the resizing operation
110 */
111 bool resize(const uint32_t width, const uint32_t height);
112
Romain Guy9ace8f52011-07-07 20:50:11 -0700113 void setSize(uint32_t width, uint32_t height) {
114 texture.width = width;
115 texture.height = height;
116 }
117
Chet Haased15ebf22012-09-05 11:40:29 -0700118 ANDROID_API void setPaint(SkPaint* paint);
119
Romain Guy9ace8f52011-07-07 20:50:11 -0700120 inline void setBlend(bool blend) {
121 texture.blend = blend;
122 }
123
Romain Guy3bbacf22013-02-06 16:51:04 -0800124 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700125 return texture.blend;
126 }
127
128 inline void setAlpha(int alpha) {
129 this->alpha = alpha;
130 }
131
132 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
133 this->alpha = alpha;
134 this->mode = mode;
135 }
136
Romain Guy3bbacf22013-02-06 16:51:04 -0800137 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700138 return alpha;
139 }
140
Romain Guy3bbacf22013-02-06 16:51:04 -0800141 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700142 return mode;
143 }
144
145 inline void setEmpty(bool empty) {
146 this->empty = empty;
147 }
148
Romain Guy3bbacf22013-02-06 16:51:04 -0800149 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700150 return empty;
151 }
152
153 inline void setFbo(GLuint fbo) {
154 this->fbo = fbo;
155 }
156
Romain Guy3bbacf22013-02-06 16:51:04 -0800157 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700158 return fbo;
159 }
160
Romain Guy3bbacf22013-02-06 16:51:04 -0800161 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
162 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
163 this->stencil = renderBuffer;
164 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
165 GL_RENDERBUFFER, stencil->getName());
166 } else {
167 ALOGE("The specified render buffer is not a stencil buffer");
168 }
Romain Guy8ce00302013-01-15 18:51:42 -0800169 }
170
Romain Guy3bbacf22013-02-06 16:51:04 -0800171 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800172 return stencil;
173 }
174
Romain Guy3bbacf22013-02-06 16:51:04 -0800175 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700176 return texture.id;
177 }
178
Romain Guy3bbacf22013-02-06 16:51:04 -0800179 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700180 return renderTarget;
181 }
182
183 inline void setRenderTarget(GLenum renderTarget) {
184 this->renderTarget = renderTarget;
185 }
186
Romain Guyd21b6e12011-11-30 20:21:23 -0800187 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
188 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700189 }
190
Romain Guyd21b6e12011-11-30 20:21:23 -0800191 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
192 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700193 }
194
Romain Guy3bbacf22013-02-06 16:51:04 -0800195 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700196 return cacheable;
197 }
198
199 inline void setCacheable(bool cacheable) {
200 this->cacheable = cacheable;
201 }
202
Romain Guy3bbacf22013-02-06 16:51:04 -0800203 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700204 return dirty;
205 }
206
207 inline void setDirty(bool dirty) {
208 this->dirty = dirty;
209 }
210
Romain Guy3bbacf22013-02-06 16:51:04 -0800211 inline bool isTextureLayer() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700212 return textureLayer;
213 }
214
215 inline void setTextureLayer(bool textureLayer) {
216 this->textureLayer = textureLayer;
217 }
218
Romain Guy3bbacf22013-02-06 16:51:04 -0800219 inline SkiaColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700220 return colorFilter;
221 }
222
Chet Haased15ebf22012-09-05 11:40:29 -0700223 ANDROID_API void setColorFilter(SkiaColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700224
Romain Guy8aa195d2013-06-04 18:00:09 -0700225 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700226
Romain Guy8aa195d2013-06-04 18:00:09 -0700227 void bindTexture() const;
228 void generateTexture();
229 void allocateTexture();
230 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700231
232 /**
233 * When the caller frees the texture itself, the caller
234 * must call this method to tell this layer that it lost
235 * the texture.
236 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700237 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800238
Romain Guy9ace8f52011-07-07 20:50:11 -0700239 inline mat4& getTexTransform() {
240 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700241 }
242
Romain Guy302a9df2011-08-16 13:55:02 -0700243 inline mat4& getTransform() {
244 return transform;
245 }
246
Romain Guy96885eb2013-03-26 15:05:58 -0700247 void defer();
Romain Guye93482f2013-06-17 13:14:51 -0700248 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700249 void flush();
Romain Guy02b49b72013-03-29 12:37:16 -0700250 void render();
Romain Guy96885eb2013-03-26 15:05:58 -0700251
Romain Guy9fc27812011-04-27 14:21:41 -0700252 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700253 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700254 */
255 Rect layer;
256 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700257 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700258 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700259 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800260 /**
261 * Clipping rectangle.
262 */
263 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700264
Romain Guydda57022010-07-06 11:39:32 -0700265 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700266 * Dirty region indicating what parts of the layer
267 * have been drawn.
268 */
269 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700270 /**
271 * If the region is a rectangle, coordinates of the
272 * region are stored here.
273 */
274 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800275
276 /**
Romain Guyf219da52011-01-16 12:54:25 -0800277 * If the layer can be rendered as a mesh, this is non-null.
278 */
279 TextureVertex* mesh;
280 uint16_t* meshIndices;
281 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700282
Romain Guy2bf68f02012-03-02 13:37:47 -0800283 /**
284 * Used for deferred updates.
285 */
286 bool deferredUpdateScheduled;
287 OpenGLRenderer* renderer;
288 DisplayList* displayList;
289 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800290 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700291 bool hasDrawnSinceUpdate;
Romain Guy2bf68f02012-03-02 13:37:47 -0800292
Romain Guy9ace8f52011-07-07 20:50:11 -0700293private:
Romain Guy8aa195d2013-06-04 18:00:09 -0700294 Caches& caches;
295
Romain Guy9ace8f52011-07-07 20:50:11 -0700296 /**
297 * Name of the FBO used to render the layer. If the name is 0
298 * this layer is not backed by an FBO, but a simple texture.
299 */
300 GLuint fbo;
301
302 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800303 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800304 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800305 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800306
307 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700308 * Indicates whether this layer has been used already.
309 */
310 bool empty;
311
312 /**
313 * The texture backing this layer.
314 */
315 Texture texture;
316
Romain Guyaa6c24c2011-04-28 18:40:04 -0700317 /**
318 * If set to true (by default), the layer can be reused.
319 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700320 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700321
322 /**
323 * When set to true, this layer must be treated as a texture
324 * layer.
325 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700326 bool textureLayer;
327
328 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700329 * When set to true, this layer is dirty and should be cleared
330 * before any rendering occurs.
331 */
332 bool dirty;
333
334 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700335 * Indicates the render target.
336 */
337 GLenum renderTarget;
338
339 /**
340 * Color filter used to draw this layer. Optional.
341 */
342 SkiaColorFilter* colorFilter;
343
344 /**
345 * Opacity of the layer.
346 */
347 int alpha;
348 /**
349 * Blending mode of the layer.
350 */
351 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700352
353 /**
354 * Optional texture coordinates transform.
355 */
356 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700357
Romain Guy302a9df2011-08-16 13:55:02 -0700358 /**
359 * Optional transform.
360 */
361 mat4 transform;
362
Romain Guy96885eb2013-03-26 15:05:58 -0700363 /**
364 * Used to defer display lists when the layer is updated with a
365 * display list.
366 */
367 DeferredDisplayList* deferredList;
368
Romain Guydda57022010-07-06 11:39:32 -0700369}; // struct Layer
370
371}; // namespace uirenderer
372}; // namespace android
373
Romain Guy5b3b3522010-10-27 18:57:51 -0700374#endif // ANDROID_HWUI_LAYER_H