blob: 471a4a6084c138e2874d21d5bbd460f8f7b7f0e9 [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 */
Chris Craik564acf72014-01-02 16:46:18 -080052class Layer {
53public:
Chet Haase603f6de2012-09-14 15:31:25 -070054 Layer(const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070055 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070056
Romain Guy2055aba2013-01-18 16:42:51 -080057 static uint32_t computeIdealWidth(uint32_t layerWidth);
58 static uint32_t computeIdealHeight(uint32_t layerHeight);
59
Romain Guy8ce00302013-01-15 18:51:42 -080060 /**
61 * Calling this method will remove (either by recycling or
62 * destroying) the associated FBO, if present, and any render
63 * buffer (stencil for instance.)
64 */
65 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070066
Romain Guydda57022010-07-06 11:39:32 -070067 /**
Romain Guy9fc27812011-04-27 14:21:41 -070068 * Sets this layer's region to a rectangle. Computes the appropriate
69 * texture coordinates.
70 */
71 void setRegionAsRect() {
72 const android::Rect& bounds = region.getBounds();
73 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
74 bounds.rightBottom().x, bounds.rightBottom().y);
75
Romain Guy9ace8f52011-07-07 20:50:11 -070076 const float texX = 1.0f / float(texture.width);
77 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070078 const float height = layer.getHeight();
79 texCoords.set(
80 regionRect.left * texX, (height - regionRect.top) * texY,
81 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070082
83 regionRect.translate(layer.left, layer.top);
84 }
85
Romain Guy2bf68f02012-03-02 13:37:47 -080086 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList,
87 int left, int top, int right, int bottom) {
88 this->renderer = renderer;
89 this->displayList = displayList;
90 const Rect r(left, top, right, bottom);
91 dirtyRect.unionWith(r);
92 deferredUpdateScheduled = true;
93 }
94
Romain Guy3bbacf22013-02-06 16:51:04 -080095 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070096 return texture.width;
97 }
98
Romain Guy3bbacf22013-02-06 16:51:04 -080099 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700100 return texture.height;
101 }
102
Romain Guy2055aba2013-01-18 16:42:51 -0800103 /**
104 * Resize the layer and its texture if needed.
105 *
106 * @param width The new width of the layer
107 * @param height The new height of the layer
108 *
109 * @return True if the layer was resized or nothing happened, false if
110 * a failure occurred during the resizing operation
111 */
112 bool resize(const uint32_t width, const uint32_t height);
113
Romain Guy9ace8f52011-07-07 20:50:11 -0700114 void setSize(uint32_t width, uint32_t height) {
115 texture.width = width;
116 texture.height = height;
117 }
118
Chet Haased15ebf22012-09-05 11:40:29 -0700119 ANDROID_API void setPaint(SkPaint* paint);
120
Romain Guy9ace8f52011-07-07 20:50:11 -0700121 inline void setBlend(bool blend) {
122 texture.blend = blend;
123 }
124
Romain Guy3bbacf22013-02-06 16:51:04 -0800125 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700126 return texture.blend;
127 }
128
129 inline void setAlpha(int alpha) {
130 this->alpha = alpha;
131 }
132
133 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
134 this->alpha = alpha;
135 this->mode = mode;
136 }
137
Romain Guy3bbacf22013-02-06 16:51:04 -0800138 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700139 return alpha;
140 }
141
Romain Guy3bbacf22013-02-06 16:51:04 -0800142 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700143 return mode;
144 }
145
146 inline void setEmpty(bool empty) {
147 this->empty = empty;
148 }
149
Romain Guy3bbacf22013-02-06 16:51:04 -0800150 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700151 return empty;
152 }
153
154 inline void setFbo(GLuint fbo) {
155 this->fbo = fbo;
156 }
157
Romain Guy3bbacf22013-02-06 16:51:04 -0800158 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700159 return fbo;
160 }
161
Romain Guy3bbacf22013-02-06 16:51:04 -0800162 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
163 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
164 this->stencil = renderBuffer;
165 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
166 GL_RENDERBUFFER, stencil->getName());
167 } else {
168 ALOGE("The specified render buffer is not a stencil buffer");
169 }
Romain Guy8ce00302013-01-15 18:51:42 -0800170 }
171
Romain Guy3bbacf22013-02-06 16:51:04 -0800172 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800173 return stencil;
174 }
175
Romain Guy3bbacf22013-02-06 16:51:04 -0800176 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700177 return texture.id;
178 }
179
Romain Guy3bbacf22013-02-06 16:51:04 -0800180 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700181 return renderTarget;
182 }
183
184 inline void setRenderTarget(GLenum renderTarget) {
185 this->renderTarget = renderTarget;
186 }
187
Romain Guyd21b6e12011-11-30 20:21:23 -0800188 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
189 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700190 }
191
Romain Guyd21b6e12011-11-30 20:21:23 -0800192 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
193 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700194 }
195
Romain Guy3bbacf22013-02-06 16:51:04 -0800196 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700197 return cacheable;
198 }
199
200 inline void setCacheable(bool cacheable) {
201 this->cacheable = cacheable;
202 }
203
Romain Guy3bbacf22013-02-06 16:51:04 -0800204 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700205 return dirty;
206 }
207
208 inline void setDirty(bool dirty) {
209 this->dirty = dirty;
210 }
211
Romain Guy3bbacf22013-02-06 16:51:04 -0800212 inline bool isTextureLayer() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700213 return textureLayer;
214 }
215
216 inline void setTextureLayer(bool textureLayer) {
217 this->textureLayer = textureLayer;
218 }
219
Romain Guy3bbacf22013-02-06 16:51:04 -0800220 inline SkiaColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700221 return colorFilter;
222 }
223
Chet Haased15ebf22012-09-05 11:40:29 -0700224 ANDROID_API void setColorFilter(SkiaColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700225
Romain Guy8aa195d2013-06-04 18:00:09 -0700226 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700227
Romain Guy8aa195d2013-06-04 18:00:09 -0700228 void bindTexture() const;
229 void generateTexture();
230 void allocateTexture();
231 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700232
233 /**
234 * When the caller frees the texture itself, the caller
235 * must call this method to tell this layer that it lost
236 * the texture.
237 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700238 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800239
Romain Guy9ace8f52011-07-07 20:50:11 -0700240 inline mat4& getTexTransform() {
241 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700242 }
243
Romain Guy302a9df2011-08-16 13:55:02 -0700244 inline mat4& getTransform() {
245 return transform;
246 }
247
Romain Guy96885eb2013-03-26 15:05:58 -0700248 void defer();
Romain Guye93482f2013-06-17 13:14:51 -0700249 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700250 void flush();
Romain Guy02b49b72013-03-29 12:37:16 -0700251 void render();
Romain Guy96885eb2013-03-26 15:05:58 -0700252
Romain Guy9fc27812011-04-27 14:21:41 -0700253 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700254 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700255 */
256 Rect layer;
257 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700258 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700259 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700260 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800261 /**
262 * Clipping rectangle.
263 */
264 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700265
Romain Guydda57022010-07-06 11:39:32 -0700266 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700267 * Dirty region indicating what parts of the layer
268 * have been drawn.
269 */
270 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700271 /**
272 * If the region is a rectangle, coordinates of the
273 * region are stored here.
274 */
275 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800276
277 /**
Romain Guyf219da52011-01-16 12:54:25 -0800278 * If the layer can be rendered as a mesh, this is non-null.
279 */
280 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800281 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