blob: d8440ea0f0eef4a9bd941d02e36460806cf178a8 [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>
22
Romain Guydda57022010-07-06 11:39:32 -070023#include <GLES2/gl2.h>
24
Romain Guy5b3b3522010-10-27 18:57:51 -070025#include <ui/Region.h>
26
Derek Sollenbergerca79cf62012-08-14 16:44:52 -040027#include <SkPaint.h>
Romain Guydda57022010-07-06 11:39:32 -070028#include <SkXfermode.h>
29
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050030#include "Matrix.h"
Romain Guydda57022010-07-06 11:39:32 -070031#include "Rect.h"
Romain Guy3bbacf22013-02-06 16:51:04 -080032#include "RenderBuffer.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070033#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080034#include "Vertex.h"
Romain Guydda57022010-07-06 11:39:32 -070035
36namespace android {
37namespace uirenderer {
38
Romain Guy8550c4c2010-10-08 15:49:53 -070039///////////////////////////////////////////////////////////////////////////////
40// Layers
41///////////////////////////////////////////////////////////////////////////////
Romain Guydda57022010-07-06 11:39:32 -070042
Romain Guy2bf68f02012-03-02 13:37:47 -080043// Forward declarations
Romain Guy8aa195d2013-06-04 18:00:09 -070044class Caches;
Romain Guy2bf68f02012-03-02 13:37:47 -080045class OpenGLRenderer;
John Recke18264b2014-03-12 13:56:30 -070046class RenderNode;
Romain Guy96885eb2013-03-26 15:05:58 -070047class DeferredDisplayList;
48class DeferStateStruct;
Romain Guy2bf68f02012-03-02 13:37:47 -080049
Romain Guydda57022010-07-06 11:39:32 -070050/**
Romain Guyeb993562010-10-05 18:14:38 -070051 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070052 */
Chris Craik564acf72014-01-02 16:46:18 -080053class Layer {
54public:
Chet Haase603f6de2012-09-14 15:31:25 -070055 Layer(const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070056 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070057
Romain Guy2055aba2013-01-18 16:42:51 -080058 static uint32_t computeIdealWidth(uint32_t layerWidth);
59 static uint32_t computeIdealHeight(uint32_t layerHeight);
60
Romain Guy8ce00302013-01-15 18:51:42 -080061 /**
62 * Calling this method will remove (either by recycling or
63 * destroying) the associated FBO, if present, and any render
64 * buffer (stencil for instance.)
65 */
66 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070067
Romain Guydda57022010-07-06 11:39:32 -070068 /**
Romain Guy9fc27812011-04-27 14:21:41 -070069 * Sets this layer's region to a rectangle. Computes the appropriate
70 * texture coordinates.
71 */
72 void setRegionAsRect() {
73 const android::Rect& bounds = region.getBounds();
74 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
75 bounds.rightBottom().x, bounds.rightBottom().y);
76
Romain Guy9ace8f52011-07-07 20:50:11 -070077 const float texX = 1.0f / float(texture.width);
78 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070079 const float height = layer.getHeight();
80 texCoords.set(
81 regionRect.left * texX, (height - regionRect.top) * texY,
82 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070083
84 regionRect.translate(layer.left, layer.top);
85 }
86
John Recke18264b2014-03-12 13:56:30 -070087 void updateDeferred(OpenGLRenderer* renderer, RenderNode* displayList,
Romain Guy2bf68f02012-03-02 13:37:47 -080088 int left, int top, int right, int bottom) {
89 this->renderer = renderer;
90 this->displayList = displayList;
91 const Rect r(left, top, right, bottom);
92 dirtyRect.unionWith(r);
93 deferredUpdateScheduled = true;
94 }
95
Romain Guy3bbacf22013-02-06 16:51:04 -080096 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -070097 return texture.width;
98 }
99
Romain Guy3bbacf22013-02-06 16:51:04 -0800100 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700101 return texture.height;
102 }
103
Romain Guy2055aba2013-01-18 16:42:51 -0800104 /**
105 * Resize the layer and its texture if needed.
106 *
107 * @param width The new width of the layer
108 * @param height The new height of the layer
109 *
110 * @return True if the layer was resized or nothing happened, false if
111 * a failure occurred during the resizing operation
112 */
113 bool resize(const uint32_t width, const uint32_t height);
114
Romain Guy9ace8f52011-07-07 20:50:11 -0700115 void setSize(uint32_t width, uint32_t height) {
116 texture.width = width;
117 texture.height = height;
118 }
119
Derek Sollenberger674554f2014-02-19 16:47:32 +0000120 ANDROID_API void setPaint(const SkPaint* paint);
Chet Haased15ebf22012-09-05 11:40:29 -0700121
Romain Guy9ace8f52011-07-07 20:50:11 -0700122 inline void setBlend(bool blend) {
123 texture.blend = blend;
124 }
125
Romain Guy3bbacf22013-02-06 16:51:04 -0800126 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700127 return texture.blend;
128 }
129
Chris Craik9757ac02014-02-25 18:50:17 -0800130 inline void setForceFilter(bool forceFilter) {
131 this->forceFilter = forceFilter;
132 }
133
134 inline bool getForceFilter() const {
135 return forceFilter;
136 }
137
Romain Guy9ace8f52011-07-07 20:50:11 -0700138 inline void setAlpha(int alpha) {
139 this->alpha = alpha;
140 }
141
142 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
143 this->alpha = alpha;
144 this->mode = mode;
145 }
146
Romain Guy3bbacf22013-02-06 16:51:04 -0800147 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700148 return alpha;
149 }
150
Romain Guy3bbacf22013-02-06 16:51:04 -0800151 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700152 return mode;
153 }
154
155 inline void setEmpty(bool empty) {
156 this->empty = empty;
157 }
158
Romain Guy3bbacf22013-02-06 16:51:04 -0800159 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700160 return empty;
161 }
162
163 inline void setFbo(GLuint fbo) {
164 this->fbo = fbo;
165 }
166
Romain Guy3bbacf22013-02-06 16:51:04 -0800167 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700168 return fbo;
169 }
170
Romain Guy3bbacf22013-02-06 16:51:04 -0800171 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
172 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
173 this->stencil = renderBuffer;
174 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
175 GL_RENDERBUFFER, stencil->getName());
176 } else {
177 ALOGE("The specified render buffer is not a stencil buffer");
178 }
Romain Guy8ce00302013-01-15 18:51:42 -0800179 }
180
Romain Guy3bbacf22013-02-06 16:51:04 -0800181 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800182 return stencil;
183 }
184
Romain Guy3bbacf22013-02-06 16:51:04 -0800185 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700186 return texture.id;
187 }
188
Romain Guy3bbacf22013-02-06 16:51:04 -0800189 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700190 return renderTarget;
191 }
192
193 inline void setRenderTarget(GLenum renderTarget) {
194 this->renderTarget = renderTarget;
195 }
196
Romain Guyd21b6e12011-11-30 20:21:23 -0800197 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
198 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700199 }
200
Romain Guyd21b6e12011-11-30 20:21:23 -0800201 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
202 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700203 }
204
Romain Guy3bbacf22013-02-06 16:51:04 -0800205 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700206 return cacheable;
207 }
208
209 inline void setCacheable(bool cacheable) {
210 this->cacheable = cacheable;
211 }
212
Romain Guy3bbacf22013-02-06 16:51:04 -0800213 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700214 return dirty;
215 }
216
217 inline void setDirty(bool dirty) {
218 this->dirty = dirty;
219 }
220
Romain Guy3bbacf22013-02-06 16:51:04 -0800221 inline bool isTextureLayer() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700222 return textureLayer;
223 }
224
225 inline void setTextureLayer(bool textureLayer) {
226 this->textureLayer = textureLayer;
227 }
228
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500229 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700230 return colorFilter;
231 }
232
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500233 ANDROID_API void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700234
Romain Guy8aa195d2013-06-04 18:00:09 -0700235 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700236
Romain Guy8aa195d2013-06-04 18:00:09 -0700237 void bindTexture() const;
238 void generateTexture();
239 void allocateTexture();
240 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700241
242 /**
243 * When the caller frees the texture itself, the caller
244 * must call this method to tell this layer that it lost
245 * the texture.
246 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700247 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800248
Romain Guy9ace8f52011-07-07 20:50:11 -0700249 inline mat4& getTexTransform() {
250 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700251 }
252
Romain Guy302a9df2011-08-16 13:55:02 -0700253 inline mat4& getTransform() {
254 return transform;
255 }
256
Romain Guy96885eb2013-03-26 15:05:58 -0700257 void defer();
Romain Guye93482f2013-06-17 13:14:51 -0700258 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700259 void flush();
Romain Guy02b49b72013-03-29 12:37:16 -0700260 void render();
Romain Guy96885eb2013-03-26 15:05:58 -0700261
Romain Guy9fc27812011-04-27 14:21:41 -0700262 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700263 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700264 */
265 Rect layer;
266 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700267 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700268 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700269 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800270 /**
271 * Clipping rectangle.
272 */
273 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700274
Romain Guydda57022010-07-06 11:39:32 -0700275 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700276 * Dirty region indicating what parts of the layer
277 * have been drawn.
278 */
279 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700280 /**
281 * If the region is a rectangle, coordinates of the
282 * region are stored here.
283 */
284 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800285
286 /**
Romain Guyf219da52011-01-16 12:54:25 -0800287 * If the layer can be rendered as a mesh, this is non-null.
288 */
289 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800290 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700291
Romain Guy2bf68f02012-03-02 13:37:47 -0800292 /**
293 * Used for deferred updates.
294 */
295 bool deferredUpdateScheduled;
296 OpenGLRenderer* renderer;
John Recke18264b2014-03-12 13:56:30 -0700297 RenderNode* displayList;
Romain Guy2bf68f02012-03-02 13:37:47 -0800298 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800299 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700300 bool hasDrawnSinceUpdate;
Romain Guy2bf68f02012-03-02 13:37:47 -0800301
Romain Guy9ace8f52011-07-07 20:50:11 -0700302private:
Romain Guy8aa195d2013-06-04 18:00:09 -0700303 Caches& caches;
304
Romain Guy9ace8f52011-07-07 20:50:11 -0700305 /**
306 * Name of the FBO used to render the layer. If the name is 0
307 * this layer is not backed by an FBO, but a simple texture.
308 */
309 GLuint fbo;
310
311 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800312 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800313 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800314 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800315
316 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700317 * Indicates whether this layer has been used already.
318 */
319 bool empty;
320
321 /**
322 * The texture backing this layer.
323 */
324 Texture texture;
325
Romain Guyaa6c24c2011-04-28 18:40:04 -0700326 /**
327 * If set to true (by default), the layer can be reused.
328 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700329 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700330
331 /**
332 * When set to true, this layer must be treated as a texture
333 * layer.
334 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700335 bool textureLayer;
336
337 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700338 * When set to true, this layer is dirty and should be cleared
339 * before any rendering occurs.
340 */
341 bool dirty;
342
343 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700344 * Indicates the render target.
345 */
346 GLenum renderTarget;
347
348 /**
349 * Color filter used to draw this layer. Optional.
350 */
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500351 SkColorFilter* colorFilter;
Romain Guy9ace8f52011-07-07 20:50:11 -0700352
353 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800354 * Indicates raster data backing the layer is scaled, requiring filtration.
355 */
356 bool forceFilter;
357
358 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700359 * Opacity of the layer.
360 */
361 int alpha;
Chris Craik9757ac02014-02-25 18:50:17 -0800362
Romain Guy9ace8f52011-07-07 20:50:11 -0700363 /**
364 * Blending mode of the layer.
365 */
366 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700367
368 /**
369 * Optional texture coordinates transform.
370 */
371 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700372
Romain Guy302a9df2011-08-16 13:55:02 -0700373 /**
374 * Optional transform.
375 */
376 mat4 transform;
377
Romain Guy96885eb2013-03-26 15:05:58 -0700378 /**
379 * Used to defer display lists when the layer is updated with a
380 * display list.
381 */
382 DeferredDisplayList* deferredList;
383
Romain Guydda57022010-07-06 11:39:32 -0700384}; // struct Layer
385
386}; // namespace uirenderer
387}; // namespace android
388
Romain Guy5b3b3522010-10-27 18:57:51 -0700389#endif // ANDROID_HWUI_LAYER_H