blob: e196cb14e59ee4b45d1483522615c390a0eecf00 [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 */
John Reck0e89e2b2014-10-31 14:49:06 -070055class Layer : public VirtualLightRefBase {
Chris Craik564acf72014-01-02 16:46:18 -080056public:
Chris Craik8a226d22014-09-08 16:40:21 -070057 enum Type {
58 kType_Texture,
59 kType_DisplayList,
60 };
61
Chris Craikbfd1cd62014-09-10 13:04:31 -070062 // layer lifecycle, controlled from outside
63 enum State {
64 kState_Uncached = 0,
65 kState_InCache = 1,
66 kState_FailedToCache = 2,
67 kState_RemovedFromCache = 3,
68 kState_DeletedFromCache = 4,
69 kState_InGarbageList = 5,
70 };
71 State state; // public for logging/debugging purposes
72
Chris Craik8a226d22014-09-08 16:40:21 -070073 Layer(Type type, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070074 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070075
Romain Guy2055aba2013-01-18 16:42:51 -080076 static uint32_t computeIdealWidth(uint32_t layerWidth);
77 static uint32_t computeIdealHeight(uint32_t layerHeight);
78
Romain Guy8ce00302013-01-15 18:51:42 -080079 /**
80 * Calling this method will remove (either by recycling or
81 * destroying) the associated FBO, if present, and any render
82 * buffer (stencil for instance.)
83 */
84 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070085
Romain Guydda57022010-07-06 11:39:32 -070086 /**
Romain Guy9fc27812011-04-27 14:21:41 -070087 * Sets this layer's region to a rectangle. Computes the appropriate
88 * texture coordinates.
89 */
90 void setRegionAsRect() {
91 const android::Rect& bounds = region.getBounds();
92 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
93 bounds.rightBottom().x, bounds.rightBottom().y);
94
Romain Guy9ace8f52011-07-07 20:50:11 -070095 const float texX = 1.0f / float(texture.width);
96 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070097 const float height = layer.getHeight();
98 texCoords.set(
99 regionRect.left * texX, (height - regionRect.top) * texY,
100 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -0700101
102 regionRect.translate(layer.left, layer.top);
103 }
104
Chris Craik69e5adf2014-08-14 13:34:01 -0700105 void setWindowTransform(Matrix4& windowTransform) {
106 cachedInvTransformInWindow.loadInverse(windowTransform);
107 rendererLightPosDirty = true;
108 }
109
Chris Craika7090e02014-06-20 16:01:00 -0700110 void updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom);
Romain Guy2bf68f02012-03-02 13:37:47 -0800111
Romain Guy3bbacf22013-02-06 16:51:04 -0800112 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700113 return texture.width;
114 }
115
Romain Guy3bbacf22013-02-06 16:51:04 -0800116 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700117 return texture.height;
118 }
119
Romain Guy2055aba2013-01-18 16:42:51 -0800120 /**
121 * Resize the layer and its texture if needed.
122 *
123 * @param width The new width of the layer
124 * @param height The new height of the layer
125 *
126 * @return True if the layer was resized or nothing happened, false if
127 * a failure occurred during the resizing operation
128 */
129 bool resize(const uint32_t width, const uint32_t height);
130
Romain Guy9ace8f52011-07-07 20:50:11 -0700131 void setSize(uint32_t width, uint32_t height) {
132 texture.width = width;
133 texture.height = height;
134 }
135
Derek Sollenberger674554f2014-02-19 16:47:32 +0000136 ANDROID_API void setPaint(const SkPaint* paint);
Chet Haased15ebf22012-09-05 11:40:29 -0700137
Romain Guy9ace8f52011-07-07 20:50:11 -0700138 inline void setBlend(bool blend) {
139 texture.blend = blend;
140 }
141
Romain Guy3bbacf22013-02-06 16:51:04 -0800142 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700143 return texture.blend;
144 }
145
Chris Craik9757ac02014-02-25 18:50:17 -0800146 inline void setForceFilter(bool forceFilter) {
147 this->forceFilter = forceFilter;
148 }
149
150 inline bool getForceFilter() const {
151 return forceFilter;
152 }
153
Romain Guy9ace8f52011-07-07 20:50:11 -0700154 inline void setAlpha(int alpha) {
155 this->alpha = alpha;
156 }
157
158 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
159 this->alpha = alpha;
160 this->mode = mode;
161 }
162
Romain Guy3bbacf22013-02-06 16:51:04 -0800163 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700164 return alpha;
165 }
166
Romain Guy3bbacf22013-02-06 16:51:04 -0800167 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700168 return mode;
169 }
170
171 inline void setEmpty(bool empty) {
172 this->empty = empty;
173 }
174
Romain Guy3bbacf22013-02-06 16:51:04 -0800175 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700176 return empty;
177 }
178
179 inline void setFbo(GLuint fbo) {
180 this->fbo = fbo;
181 }
182
Romain Guy3bbacf22013-02-06 16:51:04 -0800183 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700184 return fbo;
185 }
186
Romain Guy3bbacf22013-02-06 16:51:04 -0800187 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
188 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
189 this->stencil = renderBuffer;
190 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
191 GL_RENDERBUFFER, stencil->getName());
192 } else {
193 ALOGE("The specified render buffer is not a stencil buffer");
194 }
Romain Guy8ce00302013-01-15 18:51:42 -0800195 }
196
Romain Guy3bbacf22013-02-06 16:51:04 -0800197 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800198 return stencil;
199 }
200
Romain Guy3bbacf22013-02-06 16:51:04 -0800201 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700202 return texture.id;
203 }
204
Romain Guy3bbacf22013-02-06 16:51:04 -0800205 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700206 return renderTarget;
207 }
208
209 inline void setRenderTarget(GLenum renderTarget) {
210 this->renderTarget = renderTarget;
211 }
212
Romain Guyd21b6e12011-11-30 20:21:23 -0800213 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
214 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700215 }
216
Romain Guyd21b6e12011-11-30 20:21:23 -0800217 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
218 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700219 }
220
Romain Guy3bbacf22013-02-06 16:51:04 -0800221 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700222 return cacheable;
223 }
224
225 inline void setCacheable(bool cacheable) {
226 this->cacheable = cacheable;
227 }
228
Romain Guy3bbacf22013-02-06 16:51:04 -0800229 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700230 return dirty;
231 }
232
233 inline void setDirty(bool dirty) {
234 this->dirty = dirty;
235 }
236
Romain Guy3bbacf22013-02-06 16:51:04 -0800237 inline bool isTextureLayer() const {
Chris Craik8a226d22014-09-08 16:40:21 -0700238 return type == kType_Texture;
Romain Guy9ace8f52011-07-07 20:50:11 -0700239 }
240
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500241 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700242 return colorFilter;
243 }
244
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500245 ANDROID_API void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700246
Chris Craik3f085422014-04-15 16:18:08 -0700247 inline void setConvexMask(const SkPath* convexMask) {
248 this->convexMask = convexMask;
249 }
250
251 inline const SkPath* getConvexMask() {
252 return convexMask;
253 }
254
Romain Guy8aa195d2013-06-04 18:00:09 -0700255 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700256
Romain Guy8aa195d2013-06-04 18:00:09 -0700257 void bindTexture() const;
258 void generateTexture();
259 void allocateTexture();
260 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700261
262 /**
263 * When the caller frees the texture itself, the caller
264 * must call this method to tell this layer that it lost
265 * the texture.
266 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700267 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800268
Romain Guy9ace8f52011-07-07 20:50:11 -0700269 inline mat4& getTexTransform() {
270 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700271 }
272
Romain Guy302a9df2011-08-16 13:55:02 -0700273 inline mat4& getTransform() {
274 return transform;
275 }
276
Chris Craik69e5adf2014-08-14 13:34:01 -0700277 void defer(const OpenGLRenderer& rootRenderer);
Romain Guye93482f2013-06-17 13:14:51 -0700278 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700279 void flush();
Chris Craik69e5adf2014-08-14 13:34:01 -0700280 void render(const OpenGLRenderer& rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700281
Romain Guy9fc27812011-04-27 14:21:41 -0700282 /**
John Reck0e89e2b2014-10-31 14:49:06 -0700283 * Posts a decStrong call to the appropriate thread.
284 * Thread-safe.
285 */
286 void postDecStrong();
287
288 /**
John Reck57998012015-01-29 10:17:57 -0800289 * Lost the GL context but the layer is still around, mark it invalid internally
290 * so the dtor knows not to do any GL work
291 */
292 void onGlContextLost();
293
294 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700295 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700296 */
297 Rect layer;
298 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700299 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700300 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700301 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800302 /**
303 * Clipping rectangle.
304 */
305 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700306
Romain Guydda57022010-07-06 11:39:32 -0700307 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700308 * Dirty region indicating what parts of the layer
309 * have been drawn.
310 */
311 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700312 /**
313 * If the region is a rectangle, coordinates of the
314 * region are stored here.
315 */
316 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800317
318 /**
Romain Guyf219da52011-01-16 12:54:25 -0800319 * If the layer can be rendered as a mesh, this is non-null.
320 */
321 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800322 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700323
Romain Guy2bf68f02012-03-02 13:37:47 -0800324 /**
325 * Used for deferred updates.
326 */
327 bool deferredUpdateScheduled;
328 OpenGLRenderer* renderer;
Chris Craika7090e02014-06-20 16:01:00 -0700329 sp<RenderNode> renderNode;
Romain Guy2bf68f02012-03-02 13:37:47 -0800330 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800331 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700332 bool hasDrawnSinceUpdate;
John Reck443a7142014-09-04 17:40:05 -0700333 bool wasBuildLayered;
Romain Guy2bf68f02012-03-02 13:37:47 -0800334
Romain Guy9ace8f52011-07-07 20:50:11 -0700335private:
John Reck668f0e32014-03-26 15:10:40 -0700336 void requireRenderer();
Chris Craik69e5adf2014-08-14 13:34:01 -0700337 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer);
John Reck668f0e32014-03-26 15:10:40 -0700338
Romain Guy8aa195d2013-06-04 18:00:09 -0700339 Caches& caches;
340
John Reck3b202512014-06-23 13:13:08 -0700341 RenderState& renderState;
342
Romain Guy9ace8f52011-07-07 20:50:11 -0700343 /**
344 * Name of the FBO used to render the layer. If the name is 0
345 * this layer is not backed by an FBO, but a simple texture.
346 */
347 GLuint fbo;
348
349 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800350 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800351 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800352 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800353
354 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700355 * Indicates whether this layer has been used already.
356 */
357 bool empty;
358
359 /**
360 * The texture backing this layer.
361 */
362 Texture texture;
363
Romain Guyaa6c24c2011-04-28 18:40:04 -0700364 /**
365 * If set to true (by default), the layer can be reused.
366 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700367 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700368
369 /**
Chris Craik8a226d22014-09-08 16:40:21 -0700370 * Denotes whether the layer is a DisplayList, or Texture layer.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700371 */
Chris Craik8a226d22014-09-08 16:40:21 -0700372 const Type type;
Romain Guy9ace8f52011-07-07 20:50:11 -0700373
374 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700375 * When set to true, this layer is dirty and should be cleared
376 * before any rendering occurs.
377 */
378 bool dirty;
379
380 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700381 * Indicates the render target.
382 */
383 GLenum renderTarget;
384
385 /**
386 * Color filter used to draw this layer. Optional.
387 */
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500388 SkColorFilter* colorFilter;
Romain Guy9ace8f52011-07-07 20:50:11 -0700389
390 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800391 * Indicates raster data backing the layer is scaled, requiring filtration.
392 */
393 bool forceFilter;
394
395 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700396 * Opacity of the layer.
397 */
398 int alpha;
Chris Craik9757ac02014-02-25 18:50:17 -0800399
Romain Guy9ace8f52011-07-07 20:50:11 -0700400 /**
401 * Blending mode of the layer.
402 */
403 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700404
405 /**
406 * Optional texture coordinates transform.
407 */
408 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700409
Romain Guy302a9df2011-08-16 13:55:02 -0700410 /**
411 * Optional transform.
412 */
413 mat4 transform;
414
Romain Guy96885eb2013-03-26 15:05:58 -0700415 /**
Chris Craik69e5adf2014-08-14 13:34:01 -0700416 * Cached transform of layer in window, updated only on creation / resize
417 */
418 mat4 cachedInvTransformInWindow;
419 bool rendererLightPosDirty;
420
421 /**
Romain Guy96885eb2013-03-26 15:05:58 -0700422 * Used to defer display lists when the layer is updated with a
423 * display list.
424 */
425 DeferredDisplayList* deferredList;
426
Chris Craik3f085422014-04-15 16:18:08 -0700427 /**
428 * This convex path should be used to mask the layer's draw to the screen.
429 *
430 * Data not owned/managed by layer object.
431 */
432 const SkPath* convexMask;
433
Romain Guydda57022010-07-06 11:39:32 -0700434}; // struct Layer
435
436}; // namespace uirenderer
437}; // namespace android
438
Romain Guy5b3b3522010-10-27 18:57:51 -0700439#endif // ANDROID_HWUI_LAYER_H