blob: 36a4ed11d2e8dd2cbf3a523bfe0da5bb78b48188 [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;
John Reck3b202512014-06-23 13:13:08 -070046class RenderState;
Romain Guy2bf68f02012-03-02 13:37:47 -080047class OpenGLRenderer;
John Recke18264b2014-03-12 13:56:30 -070048class RenderNode;
Romain Guy96885eb2013-03-26 15:05:58 -070049class DeferredDisplayList;
50class DeferStateStruct;
Romain Guy2bf68f02012-03-02 13:37:47 -080051
Romain Guydda57022010-07-06 11:39:32 -070052/**
Romain Guyeb993562010-10-05 18:14:38 -070053 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070054 */
Chris Craik564acf72014-01-02 16:46:18 -080055class Layer {
56public:
Chris Craik8a226d22014-09-08 16:40:21 -070057 enum Type {
58 kType_Texture,
59 kType_DisplayList,
60 };
61
62 Layer(Type type, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070063 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070064
Romain Guy2055aba2013-01-18 16:42:51 -080065 static uint32_t computeIdealWidth(uint32_t layerWidth);
66 static uint32_t computeIdealHeight(uint32_t layerHeight);
67
Romain Guy8ce00302013-01-15 18:51:42 -080068 /**
69 * Calling this method will remove (either by recycling or
70 * destroying) the associated FBO, if present, and any render
71 * buffer (stencil for instance.)
72 */
73 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070074
Romain Guydda57022010-07-06 11:39:32 -070075 /**
Romain Guy9fc27812011-04-27 14:21:41 -070076 * Sets this layer's region to a rectangle. Computes the appropriate
77 * texture coordinates.
78 */
79 void setRegionAsRect() {
80 const android::Rect& bounds = region.getBounds();
81 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
82 bounds.rightBottom().x, bounds.rightBottom().y);
83
Romain Guy9ace8f52011-07-07 20:50:11 -070084 const float texX = 1.0f / float(texture.width);
85 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070086 const float height = layer.getHeight();
87 texCoords.set(
88 regionRect.left * texX, (height - regionRect.top) * texY,
89 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -070090
91 regionRect.translate(layer.left, layer.top);
92 }
93
Chris Craik69e5adf2014-08-14 13:34:01 -070094 void setWindowTransform(Matrix4& windowTransform) {
95 cachedInvTransformInWindow.loadInverse(windowTransform);
96 rendererLightPosDirty = true;
97 }
98
Chris Craika7090e02014-06-20 16:01:00 -070099 void updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom);
Romain Guy2bf68f02012-03-02 13:37:47 -0800100
Romain Guy3bbacf22013-02-06 16:51:04 -0800101 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700102 return texture.width;
103 }
104
Romain Guy3bbacf22013-02-06 16:51:04 -0800105 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700106 return texture.height;
107 }
108
Romain Guy2055aba2013-01-18 16:42:51 -0800109 /**
110 * Resize the layer and its texture if needed.
111 *
112 * @param width The new width of the layer
113 * @param height The new height of the layer
114 *
115 * @return True if the layer was resized or nothing happened, false if
116 * a failure occurred during the resizing operation
117 */
118 bool resize(const uint32_t width, const uint32_t height);
119
Romain Guy9ace8f52011-07-07 20:50:11 -0700120 void setSize(uint32_t width, uint32_t height) {
121 texture.width = width;
122 texture.height = height;
123 }
124
Derek Sollenberger674554f2014-02-19 16:47:32 +0000125 ANDROID_API void setPaint(const SkPaint* paint);
Chet Haased15ebf22012-09-05 11:40:29 -0700126
Romain Guy9ace8f52011-07-07 20:50:11 -0700127 inline void setBlend(bool blend) {
128 texture.blend = blend;
129 }
130
Romain Guy3bbacf22013-02-06 16:51:04 -0800131 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700132 return texture.blend;
133 }
134
Chris Craik9757ac02014-02-25 18:50:17 -0800135 inline void setForceFilter(bool forceFilter) {
136 this->forceFilter = forceFilter;
137 }
138
139 inline bool getForceFilter() const {
140 return forceFilter;
141 }
142
Romain Guy9ace8f52011-07-07 20:50:11 -0700143 inline void setAlpha(int alpha) {
144 this->alpha = alpha;
145 }
146
147 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
148 this->alpha = alpha;
149 this->mode = mode;
150 }
151
Romain Guy3bbacf22013-02-06 16:51:04 -0800152 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700153 return alpha;
154 }
155
Romain Guy3bbacf22013-02-06 16:51:04 -0800156 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700157 return mode;
158 }
159
160 inline void setEmpty(bool empty) {
161 this->empty = empty;
162 }
163
Romain Guy3bbacf22013-02-06 16:51:04 -0800164 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700165 return empty;
166 }
167
168 inline void setFbo(GLuint fbo) {
169 this->fbo = fbo;
170 }
171
Romain Guy3bbacf22013-02-06 16:51:04 -0800172 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700173 return fbo;
174 }
175
Romain Guy3bbacf22013-02-06 16:51:04 -0800176 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
177 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
178 this->stencil = renderBuffer;
179 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
180 GL_RENDERBUFFER, stencil->getName());
181 } else {
182 ALOGE("The specified render buffer is not a stencil buffer");
183 }
Romain Guy8ce00302013-01-15 18:51:42 -0800184 }
185
Romain Guy3bbacf22013-02-06 16:51:04 -0800186 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800187 return stencil;
188 }
189
Romain Guy3bbacf22013-02-06 16:51:04 -0800190 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700191 return texture.id;
192 }
193
Romain Guy3bbacf22013-02-06 16:51:04 -0800194 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700195 return renderTarget;
196 }
197
198 inline void setRenderTarget(GLenum renderTarget) {
199 this->renderTarget = renderTarget;
200 }
201
Romain Guyd21b6e12011-11-30 20:21:23 -0800202 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
203 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700204 }
205
Romain Guyd21b6e12011-11-30 20:21:23 -0800206 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
207 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700208 }
209
Romain Guy3bbacf22013-02-06 16:51:04 -0800210 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700211 return cacheable;
212 }
213
214 inline void setCacheable(bool cacheable) {
215 this->cacheable = cacheable;
216 }
217
Romain Guy3bbacf22013-02-06 16:51:04 -0800218 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700219 return dirty;
220 }
221
222 inline void setDirty(bool dirty) {
223 this->dirty = dirty;
224 }
225
Romain Guy3bbacf22013-02-06 16:51:04 -0800226 inline bool isTextureLayer() const {
Chris Craik8a226d22014-09-08 16:40:21 -0700227 return type == kType_Texture;
Romain Guy9ace8f52011-07-07 20:50:11 -0700228 }
229
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500230 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700231 return colorFilter;
232 }
233
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500234 ANDROID_API void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700235
Chris Craik3f085422014-04-15 16:18:08 -0700236 inline void setConvexMask(const SkPath* convexMask) {
237 this->convexMask = convexMask;
238 }
239
240 inline const SkPath* getConvexMask() {
241 return convexMask;
242 }
243
Romain Guy8aa195d2013-06-04 18:00:09 -0700244 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700245
Romain Guy8aa195d2013-06-04 18:00:09 -0700246 void bindTexture() const;
247 void generateTexture();
248 void allocateTexture();
249 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700250
251 /**
252 * When the caller frees the texture itself, the caller
253 * must call this method to tell this layer that it lost
254 * the texture.
255 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700256 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800257
Romain Guy9ace8f52011-07-07 20:50:11 -0700258 inline mat4& getTexTransform() {
259 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700260 }
261
Romain Guy302a9df2011-08-16 13:55:02 -0700262 inline mat4& getTransform() {
263 return transform;
264 }
265
Chris Craik69e5adf2014-08-14 13:34:01 -0700266 void defer(const OpenGLRenderer& rootRenderer);
Romain Guye93482f2013-06-17 13:14:51 -0700267 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700268 void flush();
Chris Craik69e5adf2014-08-14 13:34:01 -0700269 void render(const OpenGLRenderer& rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700270
Romain Guy9fc27812011-04-27 14:21:41 -0700271 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700272 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700273 */
274 Rect layer;
275 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700276 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700277 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700278 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800279 /**
280 * Clipping rectangle.
281 */
282 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700283
Romain Guydda57022010-07-06 11:39:32 -0700284 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700285 * Dirty region indicating what parts of the layer
286 * have been drawn.
287 */
288 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700289 /**
290 * If the region is a rectangle, coordinates of the
291 * region are stored here.
292 */
293 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800294
295 /**
Romain Guyf219da52011-01-16 12:54:25 -0800296 * If the layer can be rendered as a mesh, this is non-null.
297 */
298 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800299 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700300
Romain Guy2bf68f02012-03-02 13:37:47 -0800301 /**
302 * Used for deferred updates.
303 */
304 bool deferredUpdateScheduled;
305 OpenGLRenderer* renderer;
Chris Craika7090e02014-06-20 16:01:00 -0700306 sp<RenderNode> renderNode;
Romain Guy2bf68f02012-03-02 13:37:47 -0800307 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800308 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700309 bool hasDrawnSinceUpdate;
John Reck443a7142014-09-04 17:40:05 -0700310 bool wasBuildLayered;
Romain Guy2bf68f02012-03-02 13:37:47 -0800311
Romain Guy9ace8f52011-07-07 20:50:11 -0700312private:
John Reck668f0e32014-03-26 15:10:40 -0700313 void requireRenderer();
Chris Craik69e5adf2014-08-14 13:34:01 -0700314 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer);
John Reck668f0e32014-03-26 15:10:40 -0700315
Romain Guy8aa195d2013-06-04 18:00:09 -0700316 Caches& caches;
317
John Reck3b202512014-06-23 13:13:08 -0700318 RenderState& renderState;
319
Romain Guy9ace8f52011-07-07 20:50:11 -0700320 /**
321 * Name of the FBO used to render the layer. If the name is 0
322 * this layer is not backed by an FBO, but a simple texture.
323 */
324 GLuint fbo;
325
326 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800327 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800328 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800329 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800330
331 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700332 * Indicates whether this layer has been used already.
333 */
334 bool empty;
335
336 /**
337 * The texture backing this layer.
338 */
339 Texture texture;
340
Romain Guyaa6c24c2011-04-28 18:40:04 -0700341 /**
342 * If set to true (by default), the layer can be reused.
343 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700344 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700345
346 /**
Chris Craik8a226d22014-09-08 16:40:21 -0700347 * Denotes whether the layer is a DisplayList, or Texture layer.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700348 */
Chris Craik8a226d22014-09-08 16:40:21 -0700349 const Type type;
Romain Guy9ace8f52011-07-07 20:50:11 -0700350
351 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700352 * When set to true, this layer is dirty and should be cleared
353 * before any rendering occurs.
354 */
355 bool dirty;
356
357 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700358 * Indicates the render target.
359 */
360 GLenum renderTarget;
361
362 /**
363 * Color filter used to draw this layer. Optional.
364 */
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500365 SkColorFilter* colorFilter;
Romain Guy9ace8f52011-07-07 20:50:11 -0700366
367 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800368 * Indicates raster data backing the layer is scaled, requiring filtration.
369 */
370 bool forceFilter;
371
372 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700373 * Opacity of the layer.
374 */
375 int alpha;
Chris Craik9757ac02014-02-25 18:50:17 -0800376
Romain Guy9ace8f52011-07-07 20:50:11 -0700377 /**
378 * Blending mode of the layer.
379 */
380 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700381
382 /**
383 * Optional texture coordinates transform.
384 */
385 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700386
Romain Guy302a9df2011-08-16 13:55:02 -0700387 /**
388 * Optional transform.
389 */
390 mat4 transform;
391
Romain Guy96885eb2013-03-26 15:05:58 -0700392 /**
Chris Craik69e5adf2014-08-14 13:34:01 -0700393 * Cached transform of layer in window, updated only on creation / resize
394 */
395 mat4 cachedInvTransformInWindow;
396 bool rendererLightPosDirty;
397
398 /**
Romain Guy96885eb2013-03-26 15:05:58 -0700399 * Used to defer display lists when the layer is updated with a
400 * display list.
401 */
402 DeferredDisplayList* deferredList;
403
Chris Craik3f085422014-04-15 16:18:08 -0700404 /**
405 * This convex path should be used to mask the layer's draw to the screen.
406 *
407 * Data not owned/managed by layer object.
408 */
409 const SkPath* convexMask;
410
Romain Guydda57022010-07-06 11:39:32 -0700411}; // struct Layer
412
413}; // namespace uirenderer
414}; // namespace android
415
Romain Guy5b3b3522010-10-27 18:57:51 -0700416#endif // ANDROID_HWUI_LAYER_H