blob: 49610d58b60dbcaff37e3898ec703c2ea29f86c7 [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
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050020#include <cutils/compiler.h>
Romain Guyf7f93552010-07-08 19:17:03 -070021#include <sys/types.h>
John Reck087bc0c2014-04-04 16:20:08 -070022#include <utils/StrongPointer.h>
Romain Guyf7f93552010-07-08 19:17:03 -070023
Romain Guydda57022010-07-06 11:39:32 -070024#include <GLES2/gl2.h>
25
Romain Guy5b3b3522010-10-27 18:57:51 -070026#include <ui/Region.h>
27
Derek Sollenbergerca79cf62012-08-14 16:44:52 -040028#include <SkPaint.h>
Romain Guydda57022010-07-06 11:39:32 -070029#include <SkXfermode.h>
30
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050031#include "Matrix.h"
Romain Guydda57022010-07-06 11:39:32 -070032#include "Rect.h"
Romain Guy3bbacf22013-02-06 16:51:04 -080033#include "RenderBuffer.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070034#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080035#include "Vertex.h"
Romain Guydda57022010-07-06 11:39:32 -070036
37namespace android {
38namespace uirenderer {
39
Romain Guy8550c4c2010-10-08 15:49:53 -070040///////////////////////////////////////////////////////////////////////////////
41// Layers
42///////////////////////////////////////////////////////////////////////////////
Romain Guydda57022010-07-06 11:39:32 -070043
Romain Guy2bf68f02012-03-02 13:37:47 -080044// Forward declarations
Romain Guy8aa195d2013-06-04 18:00:09 -070045class Caches;
Romain Guy2bf68f02012-03-02 13:37:47 -080046class OpenGLRenderer;
John Recke18264b2014-03-12 13:56:30 -070047class RenderNode;
Romain Guy96885eb2013-03-26 15:05:58 -070048class DeferredDisplayList;
49class DeferStateStruct;
Romain Guy2bf68f02012-03-02 13:37:47 -080050
Romain Guydda57022010-07-06 11:39:32 -070051/**
Romain Guyeb993562010-10-05 18:14:38 -070052 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070053 */
Chris Craik564acf72014-01-02 16:46:18 -080054class Layer {
55public:
Chet Haase603f6de2012-09-14 15:31:25 -070056 Layer(const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070057 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070058
Romain Guy2055aba2013-01-18 16:42:51 -080059 static uint32_t computeIdealWidth(uint32_t layerWidth);
60 static uint32_t computeIdealHeight(uint32_t layerHeight);
61
Romain Guy8ce00302013-01-15 18:51:42 -080062 /**
63 * Calling this method will remove (either by recycling or
64 * destroying) the associated FBO, if present, and any render
65 * buffer (stencil for instance.)
66 */
67 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070068
Romain Guydda57022010-07-06 11:39:32 -070069 /**
Romain Guy9fc27812011-04-27 14:21:41 -070070 * Sets this layer's region to a rectangle. Computes the appropriate
71 * texture coordinates.
72 */
73 void setRegionAsRect() {
74 const android::Rect& bounds = region.getBounds();
75 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
76 bounds.rightBottom().x, bounds.rightBottom().y);
77
Romain Guy9ace8f52011-07-07 20:50:11 -070078 const float texX = 1.0f / float(texture.width);
79 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070080 const float height = layer.getHeight();
81 texCoords.set(
82 regionRect.left * texX, (height - regionRect.top) * texY,
83 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070084
85 regionRect.translate(layer.left, layer.top);
86 }
87
John Reck668f0e32014-03-26 15:10:40 -070088 void updateDeferred(RenderNode* displayList,
John Reck087bc0c2014-04-04 16:20:08 -070089 int left, int top, int right, int bottom);
Romain Guy2bf68f02012-03-02 13:37:47 -080090
Romain Guy3bbacf22013-02-06 16:51:04 -080091 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070092 return texture.width;
93 }
94
Romain Guy3bbacf22013-02-06 16:51:04 -080095 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070096 return texture.height;
97 }
98
Romain Guy2055aba2013-01-18 16:42:51 -080099 /**
100 * Resize the layer and its texture if needed.
101 *
102 * @param width The new width of the layer
103 * @param height The new height of the layer
104 *
105 * @return True if the layer was resized or nothing happened, false if
106 * a failure occurred during the resizing operation
107 */
108 bool resize(const uint32_t width, const uint32_t height);
109
Romain Guy9ace8f52011-07-07 20:50:11 -0700110 void setSize(uint32_t width, uint32_t height) {
111 texture.width = width;
112 texture.height = height;
113 }
114
Derek Sollenberger674554f2014-02-19 16:47:32 +0000115 ANDROID_API void setPaint(const SkPaint* paint);
Chet Haased15ebf22012-09-05 11:40:29 -0700116
Romain Guy9ace8f52011-07-07 20:50:11 -0700117 inline void setBlend(bool blend) {
118 texture.blend = blend;
119 }
120
Romain Guy3bbacf22013-02-06 16:51:04 -0800121 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700122 return texture.blend;
123 }
124
Chris Craik9757ac02014-02-25 18:50:17 -0800125 inline void setForceFilter(bool forceFilter) {
126 this->forceFilter = forceFilter;
127 }
128
129 inline bool getForceFilter() const {
130 return forceFilter;
131 }
132
Romain Guy9ace8f52011-07-07 20:50:11 -0700133 inline void setAlpha(int alpha) {
134 this->alpha = alpha;
135 }
136
137 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
138 this->alpha = alpha;
139 this->mode = mode;
140 }
141
Romain Guy3bbacf22013-02-06 16:51:04 -0800142 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700143 return alpha;
144 }
145
Romain Guy3bbacf22013-02-06 16:51:04 -0800146 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700147 return mode;
148 }
149
150 inline void setEmpty(bool empty) {
151 this->empty = empty;
152 }
153
Romain Guy3bbacf22013-02-06 16:51:04 -0800154 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700155 return empty;
156 }
157
158 inline void setFbo(GLuint fbo) {
159 this->fbo = fbo;
160 }
161
Romain Guy3bbacf22013-02-06 16:51:04 -0800162 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700163 return fbo;
164 }
165
Romain Guy3bbacf22013-02-06 16:51:04 -0800166 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
167 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
168 this->stencil = renderBuffer;
169 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
170 GL_RENDERBUFFER, stencil->getName());
171 } else {
172 ALOGE("The specified render buffer is not a stencil buffer");
173 }
Romain Guy8ce00302013-01-15 18:51:42 -0800174 }
175
Romain Guy3bbacf22013-02-06 16:51:04 -0800176 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800177 return stencil;
178 }
179
Romain Guy3bbacf22013-02-06 16:51:04 -0800180 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700181 return texture.id;
182 }
183
Romain Guy3bbacf22013-02-06 16:51:04 -0800184 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700185 return renderTarget;
186 }
187
188 inline void setRenderTarget(GLenum renderTarget) {
189 this->renderTarget = renderTarget;
190 }
191
Romain Guyd21b6e12011-11-30 20:21:23 -0800192 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
193 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700194 }
195
Romain Guyd21b6e12011-11-30 20:21:23 -0800196 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
197 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700198 }
199
Romain Guy3bbacf22013-02-06 16:51:04 -0800200 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700201 return cacheable;
202 }
203
204 inline void setCacheable(bool cacheable) {
205 this->cacheable = cacheable;
206 }
207
Romain Guy3bbacf22013-02-06 16:51:04 -0800208 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700209 return dirty;
210 }
211
212 inline void setDirty(bool dirty) {
213 this->dirty = dirty;
214 }
215
Romain Guy3bbacf22013-02-06 16:51:04 -0800216 inline bool isTextureLayer() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700217 return textureLayer;
218 }
219
220 inline void setTextureLayer(bool textureLayer) {
221 this->textureLayer = textureLayer;
222 }
223
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500224 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700225 return colorFilter;
226 }
227
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500228 ANDROID_API void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700229
Chris Craik3f085422014-04-15 16:18:08 -0700230 inline void setConvexMask(const SkPath* convexMask) {
231 this->convexMask = convexMask;
232 }
233
234 inline const SkPath* getConvexMask() {
235 return convexMask;
236 }
237
Romain Guy8aa195d2013-06-04 18:00:09 -0700238 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700239
Romain Guy8aa195d2013-06-04 18:00:09 -0700240 void bindTexture() const;
241 void generateTexture();
242 void allocateTexture();
243 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700244
245 /**
246 * When the caller frees the texture itself, the caller
247 * must call this method to tell this layer that it lost
248 * the texture.
249 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700250 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800251
Romain Guy9ace8f52011-07-07 20:50:11 -0700252 inline mat4& getTexTransform() {
253 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700254 }
255
Romain Guy302a9df2011-08-16 13:55:02 -0700256 inline mat4& getTransform() {
257 return transform;
258 }
259
Romain Guy96885eb2013-03-26 15:05:58 -0700260 void defer();
Romain Guye93482f2013-06-17 13:14:51 -0700261 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700262 void flush();
Romain Guy02b49b72013-03-29 12:37:16 -0700263 void render();
Romain Guy96885eb2013-03-26 15:05:58 -0700264
Romain Guy9fc27812011-04-27 14:21:41 -0700265 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700266 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700267 */
268 Rect layer;
269 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700270 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700271 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700272 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800273 /**
274 * Clipping rectangle.
275 */
276 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700277
Romain Guydda57022010-07-06 11:39:32 -0700278 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700279 * Dirty region indicating what parts of the layer
280 * have been drawn.
281 */
282 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700283 /**
284 * If the region is a rectangle, coordinates of the
285 * region are stored here.
286 */
287 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800288
289 /**
Romain Guyf219da52011-01-16 12:54:25 -0800290 * If the layer can be rendered as a mesh, this is non-null.
291 */
292 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800293 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700294
Romain Guy2bf68f02012-03-02 13:37:47 -0800295 /**
296 * Used for deferred updates.
297 */
298 bool deferredUpdateScheduled;
299 OpenGLRenderer* renderer;
John Reck087bc0c2014-04-04 16:20:08 -0700300 sp<RenderNode> displayList;
Romain Guy2bf68f02012-03-02 13:37:47 -0800301 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800302 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700303 bool hasDrawnSinceUpdate;
Romain Guy2bf68f02012-03-02 13:37:47 -0800304
Romain Guy9ace8f52011-07-07 20:50:11 -0700305private:
John Reck668f0e32014-03-26 15:10:40 -0700306 void requireRenderer();
307
Romain Guy8aa195d2013-06-04 18:00:09 -0700308 Caches& caches;
309
Romain Guy9ace8f52011-07-07 20:50:11 -0700310 /**
311 * Name of the FBO used to render the layer. If the name is 0
312 * this layer is not backed by an FBO, but a simple texture.
313 */
314 GLuint fbo;
315
316 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800317 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800318 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800319 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800320
321 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700322 * Indicates whether this layer has been used already.
323 */
324 bool empty;
325
326 /**
327 * The texture backing this layer.
328 */
329 Texture texture;
330
Romain Guyaa6c24c2011-04-28 18:40:04 -0700331 /**
332 * If set to true (by default), the layer can be reused.
333 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700334 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700335
336 /**
337 * When set to true, this layer must be treated as a texture
338 * layer.
339 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700340 bool textureLayer;
341
342 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700343 * When set to true, this layer is dirty and should be cleared
344 * before any rendering occurs.
345 */
346 bool dirty;
347
348 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700349 * Indicates the render target.
350 */
351 GLenum renderTarget;
352
353 /**
354 * Color filter used to draw this layer. Optional.
355 */
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500356 SkColorFilter* colorFilter;
Romain Guy9ace8f52011-07-07 20:50:11 -0700357
358 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800359 * Indicates raster data backing the layer is scaled, requiring filtration.
360 */
361 bool forceFilter;
362
363 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700364 * Opacity of the layer.
365 */
366 int alpha;
Chris Craik9757ac02014-02-25 18:50:17 -0800367
Romain Guy9ace8f52011-07-07 20:50:11 -0700368 /**
369 * Blending mode of the layer.
370 */
371 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700372
373 /**
374 * Optional texture coordinates transform.
375 */
376 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700377
Romain Guy302a9df2011-08-16 13:55:02 -0700378 /**
379 * Optional transform.
380 */
381 mat4 transform;
382
Romain Guy96885eb2013-03-26 15:05:58 -0700383 /**
384 * Used to defer display lists when the layer is updated with a
385 * display list.
386 */
387 DeferredDisplayList* deferredList;
388
Chris Craik3f085422014-04-15 16:18:08 -0700389 /**
390 * This convex path should be used to mask the layer's draw to the screen.
391 *
392 * Data not owned/managed by layer object.
393 */
394 const SkPath* convexMask;
395
Romain Guydda57022010-07-06 11:39:32 -0700396}; // struct Layer
397
398}; // namespace uirenderer
399}; // namespace android
400
Romain Guy5b3b3522010-10-27 18:57:51 -0700401#endif // ANDROID_HWUI_LAYER_H