blob: 3c1f1d1846dd8eded0cc349d7fe143294fb68dfc [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>
Nick Kralevichbfed8272014-11-01 18:37:39 -070023#include <utils/RefBase.h>
Chris Craik51d6a3d2014-12-22 17:16:56 -080024#include <memory>
Romain Guyf7f93552010-07-08 19:17:03 -070025
Romain Guydda57022010-07-06 11:39:32 -070026#include <GLES2/gl2.h>
27
Romain Guy5b3b3522010-10-27 18:57:51 -070028#include <ui/Region.h>
29
Derek Sollenbergerca79cf62012-08-14 16:44:52 -040030#include <SkPaint.h>
Romain Guydda57022010-07-06 11:39:32 -070031#include <SkXfermode.h>
32
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050033#include "Matrix.h"
Romain Guydda57022010-07-06 11:39:32 -070034#include "Rect.h"
Romain Guy3bbacf22013-02-06 16:51:04 -080035#include "RenderBuffer.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070036#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080037#include "Vertex.h"
Romain Guydda57022010-07-06 11:39:32 -070038
39namespace android {
40namespace uirenderer {
41
Romain Guy8550c4c2010-10-08 15:49:53 -070042///////////////////////////////////////////////////////////////////////////////
43// Layers
44///////////////////////////////////////////////////////////////////////////////
Romain Guydda57022010-07-06 11:39:32 -070045
Romain Guy2bf68f02012-03-02 13:37:47 -080046// Forward declarations
Romain Guy8aa195d2013-06-04 18:00:09 -070047class Caches;
Chris Craik51d6a3d2014-12-22 17:16:56 -080048class RenderNode;
John Reck3b202512014-06-23 13:13:08 -070049class RenderState;
Romain Guy2bf68f02012-03-02 13:37:47 -080050class OpenGLRenderer;
Romain Guy96885eb2013-03-26 15:05:58 -070051class DeferredDisplayList;
Chih-Hung Hsiehd3448e42014-09-15 14:28:52 -070052struct DeferStateStruct;
Romain Guy2bf68f02012-03-02 13:37:47 -080053
Romain Guydda57022010-07-06 11:39:32 -070054/**
Romain Guyeb993562010-10-05 18:14:38 -070055 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda57022010-07-06 11:39:32 -070056 */
John Reck0e89e2b2014-10-31 14:49:06 -070057class Layer : public VirtualLightRefBase {
Chris Craik564acf72014-01-02 16:46:18 -080058public:
Chris Craik8a226d22014-09-08 16:40:21 -070059 enum Type {
60 kType_Texture,
61 kType_DisplayList,
62 };
63
Chris Craikbfd1cd62014-09-10 13:04:31 -070064 // layer lifecycle, controlled from outside
65 enum State {
66 kState_Uncached = 0,
67 kState_InCache = 1,
68 kState_FailedToCache = 2,
69 kState_RemovedFromCache = 3,
70 kState_DeletedFromCache = 4,
71 kState_InGarbageList = 5,
72 };
73 State state; // public for logging/debugging purposes
74
Chris Craik8a226d22014-09-08 16:40:21 -070075 Layer(Type type, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070076 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070077
Romain Guy2055aba2013-01-18 16:42:51 -080078 static uint32_t computeIdealWidth(uint32_t layerWidth);
79 static uint32_t computeIdealHeight(uint32_t layerHeight);
80
Romain Guy8ce00302013-01-15 18:51:42 -080081 /**
82 * Calling this method will remove (either by recycling or
83 * destroying) the associated FBO, if present, and any render
84 * buffer (stencil for instance.)
85 */
86 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070087
Romain Guydda57022010-07-06 11:39:32 -070088 /**
Romain Guy9fc27812011-04-27 14:21:41 -070089 * Sets this layer's region to a rectangle. Computes the appropriate
90 * texture coordinates.
91 */
92 void setRegionAsRect() {
93 const android::Rect& bounds = region.getBounds();
94 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
95 bounds.rightBottom().x, bounds.rightBottom().y);
96
Romain Guy9ace8f52011-07-07 20:50:11 -070097 const float texX = 1.0f / float(texture.width);
98 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070099 const float height = layer.getHeight();
100 texCoords.set(
101 regionRect.left * texX, (height - regionRect.top) * texY,
102 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -0700103
104 regionRect.translate(layer.left, layer.top);
105 }
106
Chris Craik69e5adf2014-08-14 13:34:01 -0700107 void setWindowTransform(Matrix4& windowTransform) {
108 cachedInvTransformInWindow.loadInverse(windowTransform);
109 rendererLightPosDirty = true;
110 }
111
Chris Craika7090e02014-06-20 16:01:00 -0700112 void updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom);
Romain Guy2bf68f02012-03-02 13:37:47 -0800113
Romain Guy3bbacf22013-02-06 16:51:04 -0800114 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700115 return texture.width;
116 }
117
Romain Guy3bbacf22013-02-06 16:51:04 -0800118 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700119 return texture.height;
120 }
121
Romain Guy2055aba2013-01-18 16:42:51 -0800122 /**
123 * Resize the layer and its texture if needed.
124 *
125 * @param width The new width of the layer
126 * @param height The new height of the layer
127 *
128 * @return True if the layer was resized or nothing happened, false if
129 * a failure occurred during the resizing operation
130 */
131 bool resize(const uint32_t width, const uint32_t height);
132
Romain Guy9ace8f52011-07-07 20:50:11 -0700133 void setSize(uint32_t width, uint32_t height) {
134 texture.width = width;
135 texture.height = height;
136 }
137
Derek Sollenberger674554f2014-02-19 16:47:32 +0000138 ANDROID_API void setPaint(const SkPaint* paint);
Chet Haased15ebf22012-09-05 11:40:29 -0700139
Romain Guy9ace8f52011-07-07 20:50:11 -0700140 inline void setBlend(bool blend) {
141 texture.blend = blend;
142 }
143
Romain Guy3bbacf22013-02-06 16:51:04 -0800144 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700145 return texture.blend;
146 }
147
Chris Craik9757ac02014-02-25 18:50:17 -0800148 inline void setForceFilter(bool forceFilter) {
149 this->forceFilter = forceFilter;
150 }
151
152 inline bool getForceFilter() const {
153 return forceFilter;
154 }
155
Romain Guy9ace8f52011-07-07 20:50:11 -0700156 inline void setAlpha(int alpha) {
157 this->alpha = alpha;
158 }
159
160 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
161 this->alpha = alpha;
162 this->mode = mode;
163 }
164
Romain Guy3bbacf22013-02-06 16:51:04 -0800165 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700166 return alpha;
167 }
168
Romain Guy3bbacf22013-02-06 16:51:04 -0800169 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700170 return mode;
171 }
172
173 inline void setEmpty(bool empty) {
174 this->empty = empty;
175 }
176
Romain Guy3bbacf22013-02-06 16:51:04 -0800177 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700178 return empty;
179 }
180
181 inline void setFbo(GLuint fbo) {
182 this->fbo = fbo;
183 }
184
Romain Guy3bbacf22013-02-06 16:51:04 -0800185 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700186 return fbo;
187 }
188
Romain Guy3bbacf22013-02-06 16:51:04 -0800189 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
190 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
191 this->stencil = renderBuffer;
192 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
193 GL_RENDERBUFFER, stencil->getName());
194 } else {
195 ALOGE("The specified render buffer is not a stencil buffer");
196 }
Romain Guy8ce00302013-01-15 18:51:42 -0800197 }
198
Romain Guy3bbacf22013-02-06 16:51:04 -0800199 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800200 return stencil;
201 }
202
Chris Craikf27133d2015-02-19 09:51:53 -0800203 inline GLuint getTextureId() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700204 return texture.id;
205 }
206
Chris Craikf27133d2015-02-19 09:51:53 -0800207 inline Texture& getTexture() {
208 return texture;
209 }
210
Romain Guy3bbacf22013-02-06 16:51:04 -0800211 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700212 return renderTarget;
213 }
214
215 inline void setRenderTarget(GLenum renderTarget) {
216 this->renderTarget = renderTarget;
217 }
218
Romain Guyd21b6e12011-11-30 20:21:23 -0800219 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
220 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700221 }
222
Romain Guyd21b6e12011-11-30 20:21:23 -0800223 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
224 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700225 }
226
Romain Guy3bbacf22013-02-06 16:51:04 -0800227 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700228 return cacheable;
229 }
230
231 inline void setCacheable(bool cacheable) {
232 this->cacheable = cacheable;
233 }
234
Romain Guy3bbacf22013-02-06 16:51:04 -0800235 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700236 return dirty;
237 }
238
239 inline void setDirty(bool dirty) {
240 this->dirty = dirty;
241 }
242
Romain Guy3bbacf22013-02-06 16:51:04 -0800243 inline bool isTextureLayer() const {
Chris Craik8a226d22014-09-08 16:40:21 -0700244 return type == kType_Texture;
Romain Guy9ace8f52011-07-07 20:50:11 -0700245 }
246
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500247 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700248 return colorFilter;
249 }
250
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500251 ANDROID_API void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700252
Chris Craik3f085422014-04-15 16:18:08 -0700253 inline void setConvexMask(const SkPath* convexMask) {
254 this->convexMask = convexMask;
255 }
256
257 inline const SkPath* getConvexMask() {
258 return convexMask;
259 }
260
Romain Guy8aa195d2013-06-04 18:00:09 -0700261 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700262
Romain Guy8aa195d2013-06-04 18:00:09 -0700263 void bindTexture() const;
264 void generateTexture();
265 void allocateTexture();
266 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700267
268 /**
269 * When the caller frees the texture itself, the caller
270 * must call this method to tell this layer that it lost
271 * the texture.
272 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700273 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800274
Romain Guy9ace8f52011-07-07 20:50:11 -0700275 inline mat4& getTexTransform() {
276 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700277 }
278
Romain Guy302a9df2011-08-16 13:55:02 -0700279 inline mat4& getTransform() {
280 return transform;
281 }
282
Chris Craik69e5adf2014-08-14 13:34:01 -0700283 void defer(const OpenGLRenderer& rootRenderer);
Romain Guye93482f2013-06-17 13:14:51 -0700284 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700285 void flush();
Chris Craik69e5adf2014-08-14 13:34:01 -0700286 void render(const OpenGLRenderer& rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700287
Romain Guy9fc27812011-04-27 14:21:41 -0700288 /**
John Reck0e89e2b2014-10-31 14:49:06 -0700289 * Posts a decStrong call to the appropriate thread.
290 * Thread-safe.
291 */
292 void postDecStrong();
293
294 /**
John Reck57998012015-01-29 10:17:57 -0800295 * Lost the GL context but the layer is still around, mark it invalid internally
296 * so the dtor knows not to do any GL work
297 */
298 void onGlContextLost();
299
300 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700301 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700302 */
303 Rect layer;
304 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700305 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700306 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700307 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800308 /**
309 * Clipping rectangle.
310 */
311 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700312
Romain Guydda57022010-07-06 11:39:32 -0700313 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700314 * Dirty region indicating what parts of the layer
315 * have been drawn.
316 */
317 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700318 /**
319 * If the region is a rectangle, coordinates of the
320 * region are stored here.
321 */
322 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800323
324 /**
Romain Guyf219da52011-01-16 12:54:25 -0800325 * If the layer can be rendered as a mesh, this is non-null.
326 */
327 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800328 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700329
Romain Guy2bf68f02012-03-02 13:37:47 -0800330 /**
331 * Used for deferred updates.
332 */
333 bool deferredUpdateScheduled;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800334 std::unique_ptr<OpenGLRenderer> renderer;
Chris Craika7090e02014-06-20 16:01:00 -0700335 sp<RenderNode> renderNode;
Romain Guy2bf68f02012-03-02 13:37:47 -0800336 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800337 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700338 bool hasDrawnSinceUpdate;
John Reck443a7142014-09-04 17:40:05 -0700339 bool wasBuildLayered;
Romain Guy2bf68f02012-03-02 13:37:47 -0800340
Romain Guy9ace8f52011-07-07 20:50:11 -0700341private:
John Reck668f0e32014-03-26 15:10:40 -0700342 void requireRenderer();
Chris Craik69e5adf2014-08-14 13:34:01 -0700343 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer);
John Reck668f0e32014-03-26 15:10:40 -0700344
Romain Guy8aa195d2013-06-04 18:00:09 -0700345 Caches& caches;
346
John Reck3b202512014-06-23 13:13:08 -0700347 RenderState& renderState;
348
Romain Guy9ace8f52011-07-07 20:50:11 -0700349 /**
350 * Name of the FBO used to render the layer. If the name is 0
351 * this layer is not backed by an FBO, but a simple texture.
352 */
353 GLuint fbo;
354
355 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800356 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800357 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800358 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800359
360 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700361 * Indicates whether this layer has been used already.
362 */
363 bool empty;
364
365 /**
366 * The texture backing this layer.
367 */
368 Texture texture;
369
Romain Guyaa6c24c2011-04-28 18:40:04 -0700370 /**
371 * If set to true (by default), the layer can be reused.
372 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700373 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700374
375 /**
Chris Craik8a226d22014-09-08 16:40:21 -0700376 * Denotes whether the layer is a DisplayList, or Texture layer.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700377 */
Chris Craik8a226d22014-09-08 16:40:21 -0700378 const Type type;
Romain Guy9ace8f52011-07-07 20:50:11 -0700379
380 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700381 * When set to true, this layer is dirty and should be cleared
382 * before any rendering occurs.
383 */
384 bool dirty;
385
386 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700387 * Indicates the render target.
388 */
389 GLenum renderTarget;
390
391 /**
392 * Color filter used to draw this layer. Optional.
393 */
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500394 SkColorFilter* colorFilter;
Romain Guy9ace8f52011-07-07 20:50:11 -0700395
396 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800397 * Indicates raster data backing the layer is scaled, requiring filtration.
398 */
399 bool forceFilter;
400
401 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700402 * Opacity of the layer.
403 */
404 int alpha;
Chris Craik9757ac02014-02-25 18:50:17 -0800405
Romain Guy9ace8f52011-07-07 20:50:11 -0700406 /**
407 * Blending mode of the layer.
408 */
409 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700410
411 /**
412 * Optional texture coordinates transform.
413 */
414 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700415
Romain Guy302a9df2011-08-16 13:55:02 -0700416 /**
417 * Optional transform.
418 */
419 mat4 transform;
420
Romain Guy96885eb2013-03-26 15:05:58 -0700421 /**
Chris Craik69e5adf2014-08-14 13:34:01 -0700422 * Cached transform of layer in window, updated only on creation / resize
423 */
424 mat4 cachedInvTransformInWindow;
425 bool rendererLightPosDirty;
426
427 /**
Romain Guy96885eb2013-03-26 15:05:58 -0700428 * Used to defer display lists when the layer is updated with a
429 * display list.
430 */
Chris Craik51d6a3d2014-12-22 17:16:56 -0800431 std::unique_ptr<DeferredDisplayList> deferredList;
Romain Guy96885eb2013-03-26 15:05:58 -0700432
Chris Craik3f085422014-04-15 16:18:08 -0700433 /**
434 * This convex path should be used to mask the layer's draw to the screen.
435 *
436 * Data not owned/managed by layer object.
437 */
438 const SkPath* convexMask;
439
Romain Guydda57022010-07-06 11:39:32 -0700440}; // struct Layer
441
442}; // namespace uirenderer
443}; // namespace android
444
Romain Guy5b3b3522010-10-27 18:57:51 -0700445#endif // ANDROID_HWUI_LAYER_H