blob: 3b4f293dddd3480459772fde866d64b32b557faa [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
Romain Guy3bbacf22013-02-06 16:51:04 -0800203 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700204 return texture.id;
205 }
206
Romain Guy3bbacf22013-02-06 16:51:04 -0800207 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700208 return renderTarget;
209 }
210
211 inline void setRenderTarget(GLenum renderTarget) {
212 this->renderTarget = renderTarget;
213 }
214
Romain Guyd21b6e12011-11-30 20:21:23 -0800215 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
216 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700217 }
218
Romain Guyd21b6e12011-11-30 20:21:23 -0800219 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
220 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700221 }
222
Romain Guy3bbacf22013-02-06 16:51:04 -0800223 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700224 return cacheable;
225 }
226
227 inline void setCacheable(bool cacheable) {
228 this->cacheable = cacheable;
229 }
230
Romain Guy3bbacf22013-02-06 16:51:04 -0800231 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700232 return dirty;
233 }
234
235 inline void setDirty(bool dirty) {
236 this->dirty = dirty;
237 }
238
Romain Guy3bbacf22013-02-06 16:51:04 -0800239 inline bool isTextureLayer() const {
Chris Craik8a226d22014-09-08 16:40:21 -0700240 return type == kType_Texture;
Romain Guy9ace8f52011-07-07 20:50:11 -0700241 }
242
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500243 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700244 return colorFilter;
245 }
246
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500247 ANDROID_API void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700248
Chris Craik3f0854292014-04-15 16:18:08 -0700249 inline void setConvexMask(const SkPath* convexMask) {
250 this->convexMask = convexMask;
251 }
252
253 inline const SkPath* getConvexMask() {
254 return convexMask;
255 }
256
Romain Guy8aa195d2013-06-04 18:00:09 -0700257 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700258
Romain Guy8aa195d2013-06-04 18:00:09 -0700259 void bindTexture() const;
260 void generateTexture();
261 void allocateTexture();
262 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700263
264 /**
265 * When the caller frees the texture itself, the caller
266 * must call this method to tell this layer that it lost
267 * the texture.
268 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700269 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800270
Romain Guy9ace8f52011-07-07 20:50:11 -0700271 inline mat4& getTexTransform() {
272 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700273 }
274
Romain Guy302a9df2011-08-16 13:55:02 -0700275 inline mat4& getTransform() {
276 return transform;
277 }
278
Chris Craik69e5adf2014-08-14 13:34:01 -0700279 void defer(const OpenGLRenderer& rootRenderer);
Romain Guye93482f2013-06-17 13:14:51 -0700280 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700281 void flush();
Chris Craik69e5adf2014-08-14 13:34:01 -0700282 void render(const OpenGLRenderer& rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700283
Romain Guy9fc27812011-04-27 14:21:41 -0700284 /**
John Reck0e89e2b2014-10-31 14:49:06 -0700285 * Posts a decStrong call to the appropriate thread.
286 * Thread-safe.
287 */
288 void postDecStrong();
289
290 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700291 * Bounds of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700292 */
293 Rect layer;
294 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700295 * Texture coordinates of the layer.
Romain Guydda57022010-07-06 11:39:32 -0700296 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700297 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800298 /**
299 * Clipping rectangle.
300 */
301 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700302
Romain Guydda57022010-07-06 11:39:32 -0700303 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700304 * Dirty region indicating what parts of the layer
305 * have been drawn.
306 */
307 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700308 /**
309 * If the region is a rectangle, coordinates of the
310 * region are stored here.
311 */
312 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800313
314 /**
Romain Guyf219da52011-01-16 12:54:25 -0800315 * If the layer can be rendered as a mesh, this is non-null.
316 */
317 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800318 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700319
Romain Guy2bf68f02012-03-02 13:37:47 -0800320 /**
321 * Used for deferred updates.
322 */
323 bool deferredUpdateScheduled;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800324 std::unique_ptr<OpenGLRenderer> renderer;
Chris Craika7090e02014-06-20 16:01:00 -0700325 sp<RenderNode> renderNode;
Romain Guy2bf68f02012-03-02 13:37:47 -0800326 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800327 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700328 bool hasDrawnSinceUpdate;
John Reck443a7142014-09-04 17:40:05 -0700329 bool wasBuildLayered;
Romain Guy2bf68f02012-03-02 13:37:47 -0800330
Romain Guy9ace8f52011-07-07 20:50:11 -0700331private:
John Reck668f0e32014-03-26 15:10:40 -0700332 void requireRenderer();
Chris Craik69e5adf2014-08-14 13:34:01 -0700333 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer);
John Reck668f0e32014-03-26 15:10:40 -0700334
Romain Guy8aa195d2013-06-04 18:00:09 -0700335 Caches& caches;
336
John Reck3b202512014-06-23 13:13:08 -0700337 RenderState& renderState;
338
Romain Guy9ace8f52011-07-07 20:50:11 -0700339 /**
340 * Name of the FBO used to render the layer. If the name is 0
341 * this layer is not backed by an FBO, but a simple texture.
342 */
343 GLuint fbo;
344
345 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800346 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800347 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800348 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800349
350 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700351 * Indicates whether this layer has been used already.
352 */
353 bool empty;
354
355 /**
356 * The texture backing this layer.
357 */
358 Texture texture;
359
Romain Guyaa6c24c2011-04-28 18:40:04 -0700360 /**
361 * If set to true (by default), the layer can be reused.
362 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700363 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700364
365 /**
Chris Craik8a226d22014-09-08 16:40:21 -0700366 * Denotes whether the layer is a DisplayList, or Texture layer.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700367 */
Chris Craik8a226d22014-09-08 16:40:21 -0700368 const Type type;
Romain Guy9ace8f52011-07-07 20:50:11 -0700369
370 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700371 * When set to true, this layer is dirty and should be cleared
372 * before any rendering occurs.
373 */
374 bool dirty;
375
376 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700377 * Indicates the render target.
378 */
379 GLenum renderTarget;
380
381 /**
382 * Color filter used to draw this layer. Optional.
383 */
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500384 SkColorFilter* colorFilter;
Romain Guy9ace8f52011-07-07 20:50:11 -0700385
386 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800387 * Indicates raster data backing the layer is scaled, requiring filtration.
388 */
389 bool forceFilter;
390
391 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700392 * Opacity of the layer.
393 */
394 int alpha;
Chris Craik9757ac02014-02-25 18:50:17 -0800395
Romain Guy9ace8f52011-07-07 20:50:11 -0700396 /**
397 * Blending mode of the layer.
398 */
399 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700400
401 /**
402 * Optional texture coordinates transform.
403 */
404 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700405
Romain Guy302a9df2011-08-16 13:55:02 -0700406 /**
407 * Optional transform.
408 */
409 mat4 transform;
410
Romain Guy96885eb2013-03-26 15:05:58 -0700411 /**
Chris Craik69e5adf2014-08-14 13:34:01 -0700412 * Cached transform of layer in window, updated only on creation / resize
413 */
414 mat4 cachedInvTransformInWindow;
415 bool rendererLightPosDirty;
416
417 /**
Romain Guy96885eb2013-03-26 15:05:58 -0700418 * Used to defer display lists when the layer is updated with a
419 * display list.
420 */
Chris Craik51d6a3d2014-12-22 17:16:56 -0800421 std::unique_ptr<DeferredDisplayList> deferredList;
Romain Guy96885eb2013-03-26 15:05:58 -0700422
Chris Craik3f0854292014-04-15 16:18:08 -0700423 /**
424 * This convex path should be used to mask the layer's draw to the screen.
425 *
426 * Data not owned/managed by layer object.
427 */
428 const SkPath* convexMask;
429
Romain Guydda57022010-07-06 11:39:32 -0700430}; // struct Layer
431
432}; // namespace uirenderer
433}; // namespace android
434
Romain Guy5b3b3522010-10-27 18:57:51 -0700435#endif // ANDROID_HWUI_LAYER_H