blob: 3b909d5858d6bb6603c084fab8d1a06b7b1ecf27 [file] [log] [blame]
Romain Guydda570202010-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 Guydda570202010-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>
Romain Guyf7f93552010-07-08 19:17:03 -070024
Romain Guydda570202010-07-06 11:39:32 -070025#include <GLES2/gl2.h>
26
Romain Guy5b3b3522010-10-27 18:57:51 -070027#include <ui/Region.h>
28
Derek Sollenbergerca79cf62012-08-14 16:44:52 -040029#include <SkPaint.h>
Romain Guydda570202010-07-06 11:39:32 -070030#include <SkXfermode.h>
31
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -050032#include "Matrix.h"
Romain Guydda570202010-07-06 11:39:32 -070033#include "Rect.h"
Romain Guy3bbacf22013-02-06 16:51:04 -080034#include "RenderBuffer.h"
Romain Guy9ace8f52011-07-07 20:50:11 -070035#include "Texture.h"
Romain Guyf219da52011-01-16 12:54:25 -080036#include "Vertex.h"
Romain Guydda570202010-07-06 11:39:32 -070037
38namespace android {
39namespace uirenderer {
40
Romain Guy8550c4c2010-10-08 15:49:53 -070041///////////////////////////////////////////////////////////////////////////////
42// Layers
43///////////////////////////////////////////////////////////////////////////////
Romain Guydda570202010-07-06 11:39:32 -070044
Romain Guy2bf68f02012-03-02 13:37:47 -080045// Forward declarations
Romain Guy8aa195d2013-06-04 18:00:09 -070046class Caches;
John Reck3b202512014-06-23 13:13:08 -070047class RenderState;
Romain Guy2bf68f02012-03-02 13:37:47 -080048class OpenGLRenderer;
John Recke18264b2014-03-12 13:56:30 -070049class RenderNode;
Romain Guy96885eb2013-03-26 15:05:58 -070050class DeferredDisplayList;
Chih-Hung Hsiehd3448e42014-09-15 14:28:52 -070051struct DeferStateStruct;
Romain Guy2bf68f02012-03-02 13:37:47 -080052
Romain Guydda570202010-07-06 11:39:32 -070053/**
Romain Guyeb993562010-10-05 18:14:38 -070054 * A layer has dimensions and is backed by an OpenGL texture or FBO.
Romain Guydda570202010-07-06 11:39:32 -070055 */
John Reck0e89e2b2014-10-31 14:49:06 -070056class Layer : public VirtualLightRefBase {
Chris Craik564acf72014-01-02 16:46:18 -080057public:
Chris Craik8a226d22014-09-08 16:40:21 -070058 enum Type {
59 kType_Texture,
60 kType_DisplayList,
61 };
62
Chris Craikbfd1cd62014-09-10 13:04:31 -070063 // layer lifecycle, controlled from outside
64 enum State {
65 kState_Uncached = 0,
66 kState_InCache = 1,
67 kState_FailedToCache = 2,
68 kState_RemovedFromCache = 3,
69 kState_DeletedFromCache = 4,
70 kState_InGarbageList = 5,
71 };
72 State state; // public for logging/debugging purposes
73
Chris Craik8a226d22014-09-08 16:40:21 -070074 Layer(Type type, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight);
Chet Haased15ebf22012-09-05 11:40:29 -070075 ~Layer();
Romain Guy8550c4c2010-10-08 15:49:53 -070076
Romain Guy2055aba2013-01-18 16:42:51 -080077 static uint32_t computeIdealWidth(uint32_t layerWidth);
78 static uint32_t computeIdealHeight(uint32_t layerHeight);
79
Romain Guy8ce00302013-01-15 18:51:42 -080080 /**
81 * Calling this method will remove (either by recycling or
82 * destroying) the associated FBO, if present, and any render
83 * buffer (stencil for instance.)
84 */
85 void removeFbo(bool flush = true);
Dave Burke56257af2012-09-25 20:30:09 -070086
Romain Guydda570202010-07-06 11:39:32 -070087 /**
Romain Guy9fc27812011-04-27 14:21:41 -070088 * Sets this layer's region to a rectangle. Computes the appropriate
89 * texture coordinates.
90 */
91 void setRegionAsRect() {
92 const android::Rect& bounds = region.getBounds();
93 regionRect.set(bounds.leftTop().x, bounds.leftTop().y,
94 bounds.rightBottom().x, bounds.rightBottom().y);
95
Romain Guy9ace8f52011-07-07 20:50:11 -070096 const float texX = 1.0f / float(texture.width);
97 const float texY = 1.0f / float(texture.height);
Romain Guy9fc27812011-04-27 14:21:41 -070098 const float height = layer.getHeight();
99 texCoords.set(
100 regionRect.left * texX, (height - regionRect.top) * texY,
101 regionRect.right * texX, (height - regionRect.bottom) * texY);
Romain Guy9ace8f52011-07-07 20:50:11 -0700102
103 regionRect.translate(layer.left, layer.top);
104 }
105
Chris Craik69e5adf2014-08-14 13:34:01 -0700106 void setWindowTransform(Matrix4& windowTransform) {
107 cachedInvTransformInWindow.loadInverse(windowTransform);
108 rendererLightPosDirty = true;
109 }
110
Chris Craika7090e02014-06-20 16:01:00 -0700111 void updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom);
Romain Guy2bf68f02012-03-02 13:37:47 -0800112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113 inline uint32_t getWidth() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700114 return texture.width;
115 }
116
Romain Guy3bbacf22013-02-06 16:51:04 -0800117 inline uint32_t getHeight() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700118 return texture.height;
119 }
120
Romain Guy2055aba2013-01-18 16:42:51 -0800121 /**
122 * Resize the layer and its texture if needed.
123 *
124 * @param width The new width of the layer
125 * @param height The new height of the layer
126 *
127 * @return True if the layer was resized or nothing happened, false if
128 * a failure occurred during the resizing operation
129 */
130 bool resize(const uint32_t width, const uint32_t height);
131
Romain Guy9ace8f52011-07-07 20:50:11 -0700132 void setSize(uint32_t width, uint32_t height) {
133 texture.width = width;
134 texture.height = height;
135 }
136
Derek Sollenberger674554f2014-02-19 16:47:32 +0000137 ANDROID_API void setPaint(const SkPaint* paint);
Chet Haased15ebf22012-09-05 11:40:29 -0700138
Romain Guy9ace8f52011-07-07 20:50:11 -0700139 inline void setBlend(bool blend) {
140 texture.blend = blend;
141 }
142
Romain Guy3bbacf22013-02-06 16:51:04 -0800143 inline bool isBlend() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700144 return texture.blend;
145 }
146
Chris Craik9757ac02014-02-25 18:50:17 -0800147 inline void setForceFilter(bool forceFilter) {
148 this->forceFilter = forceFilter;
149 }
150
151 inline bool getForceFilter() const {
152 return forceFilter;
153 }
154
Romain Guy9ace8f52011-07-07 20:50:11 -0700155 inline void setAlpha(int alpha) {
156 this->alpha = alpha;
157 }
158
159 inline void setAlpha(int alpha, SkXfermode::Mode mode) {
160 this->alpha = alpha;
161 this->mode = mode;
162 }
163
Romain Guy3bbacf22013-02-06 16:51:04 -0800164 inline int getAlpha() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700165 return alpha;
166 }
167
Romain Guy3bbacf22013-02-06 16:51:04 -0800168 inline SkXfermode::Mode getMode() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700169 return mode;
170 }
171
172 inline void setEmpty(bool empty) {
173 this->empty = empty;
174 }
175
Romain Guy3bbacf22013-02-06 16:51:04 -0800176 inline bool isEmpty() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700177 return empty;
178 }
179
180 inline void setFbo(GLuint fbo) {
181 this->fbo = fbo;
182 }
183
Romain Guy3bbacf22013-02-06 16:51:04 -0800184 inline GLuint getFbo() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700185 return fbo;
186 }
187
Romain Guy3bbacf22013-02-06 16:51:04 -0800188 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) {
189 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) {
190 this->stencil = renderBuffer;
191 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
192 GL_RENDERBUFFER, stencil->getName());
193 } else {
194 ALOGE("The specified render buffer is not a stencil buffer");
195 }
Romain Guy8ce00302013-01-15 18:51:42 -0800196 }
197
Romain Guy3bbacf22013-02-06 16:51:04 -0800198 inline RenderBuffer* getStencilRenderBuffer() const {
Romain Guy8ce00302013-01-15 18:51:42 -0800199 return stencil;
200 }
201
Romain Guy3bbacf22013-02-06 16:51:04 -0800202 inline GLuint getTexture() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700203 return texture.id;
204 }
205
Romain Guy3bbacf22013-02-06 16:51:04 -0800206 inline GLenum getRenderTarget() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700207 return renderTarget;
208 }
209
210 inline void setRenderTarget(GLenum renderTarget) {
211 this->renderTarget = renderTarget;
212 }
213
Romain Guyd21b6e12011-11-30 20:21:23 -0800214 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
215 texture.setWrap(wrap, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700216 }
217
Romain Guyd21b6e12011-11-30 20:21:23 -0800218 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
219 texture.setFilter(filter, bindTexture, force, renderTarget);
Romain Guy9ace8f52011-07-07 20:50:11 -0700220 }
221
Romain Guy3bbacf22013-02-06 16:51:04 -0800222 inline bool isCacheable() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700223 return cacheable;
224 }
225
226 inline void setCacheable(bool cacheable) {
227 this->cacheable = cacheable;
228 }
229
Romain Guy3bbacf22013-02-06 16:51:04 -0800230 inline bool isDirty() const {
Romain Guy7c25aab2012-10-18 15:05:02 -0700231 return dirty;
232 }
233
234 inline void setDirty(bool dirty) {
235 this->dirty = dirty;
236 }
237
Romain Guy3bbacf22013-02-06 16:51:04 -0800238 inline bool isTextureLayer() const {
Chris Craik8a226d22014-09-08 16:40:21 -0700239 return type == kType_Texture;
Romain Guy9ace8f52011-07-07 20:50:11 -0700240 }
241
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500242 inline SkColorFilter* getColorFilter() const {
Romain Guy9ace8f52011-07-07 20:50:11 -0700243 return colorFilter;
244 }
245
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500246 ANDROID_API void setColorFilter(SkColorFilter* filter);
Romain Guy9ace8f52011-07-07 20:50:11 -0700247
Chris Craik3f0854292014-04-15 16:18:08 -0700248 inline void setConvexMask(const SkPath* convexMask) {
249 this->convexMask = convexMask;
250 }
251
252 inline const SkPath* getConvexMask() {
253 return convexMask;
254 }
255
Romain Guy8aa195d2013-06-04 18:00:09 -0700256 void bindStencilRenderBuffer() const;
Romain Guy9ace8f52011-07-07 20:50:11 -0700257
Romain Guy8aa195d2013-06-04 18:00:09 -0700258 void bindTexture() const;
259 void generateTexture();
260 void allocateTexture();
261 void deleteTexture();
Romain Guyef09a212012-09-25 12:17:14 -0700262
263 /**
264 * When the caller frees the texture itself, the caller
265 * must call this method to tell this layer that it lost
266 * the texture.
267 */
Romain Guy8aa195d2013-06-04 18:00:09 -0700268 ANDROID_API void clearTexture();
Romain Guy2055aba2013-01-18 16:42:51 -0800269
Romain Guy9ace8f52011-07-07 20:50:11 -0700270 inline mat4& getTexTransform() {
271 return texTransform;
Romain Guy9fc27812011-04-27 14:21:41 -0700272 }
273
Romain Guy302a9df2011-08-16 13:55:02 -0700274 inline mat4& getTransform() {
275 return transform;
276 }
277
Chris Craik69e5adf2014-08-14 13:34:01 -0700278 void defer(const OpenGLRenderer& rootRenderer);
Romain Guye93482f2013-06-17 13:14:51 -0700279 void cancelDefer();
Romain Guy96885eb2013-03-26 15:05:58 -0700280 void flush();
Chris Craik69e5adf2014-08-14 13:34:01 -0700281 void render(const OpenGLRenderer& rootRenderer);
Romain Guy96885eb2013-03-26 15:05:58 -0700282
Romain Guy9fc27812011-04-27 14:21:41 -0700283 /**
John Reck0e89e2b2014-10-31 14:49:06 -0700284 * Posts a decStrong call to the appropriate thread.
285 * Thread-safe.
286 */
287 void postDecStrong();
288
289 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700290 * Bounds of the layer.
Romain Guydda570202010-07-06 11:39:32 -0700291 */
292 Rect layer;
293 /**
Romain Guy8550c4c2010-10-08 15:49:53 -0700294 * Texture coordinates of the layer.
Romain Guydda570202010-07-06 11:39:32 -0700295 */
Romain Guy8550c4c2010-10-08 15:49:53 -0700296 Rect texCoords;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800297 /**
298 * Clipping rectangle.
299 */
300 Rect clipRect;
Romain Guy8550c4c2010-10-08 15:49:53 -0700301
Romain Guydda570202010-07-06 11:39:32 -0700302 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700303 * Dirty region indicating what parts of the layer
304 * have been drawn.
305 */
306 Region region;
Romain Guy40667672011-03-18 14:34:03 -0700307 /**
308 * If the region is a rectangle, coordinates of the
309 * region are stored here.
310 */
311 Rect regionRect;
Romain Guy171c5922011-01-06 10:04:23 -0800312
313 /**
Romain Guyf219da52011-01-16 12:54:25 -0800314 * If the layer can be rendered as a mesh, this is non-null.
315 */
316 TextureVertex* mesh;
Romain Guyf219da52011-01-16 12:54:25 -0800317 GLsizei meshElementCount;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700318
Romain Guy2bf68f02012-03-02 13:37:47 -0800319 /**
320 * Used for deferred updates.
321 */
322 bool deferredUpdateScheduled;
323 OpenGLRenderer* renderer;
Chris Craika7090e02014-06-20 16:01:00 -0700324 sp<RenderNode> renderNode;
Romain Guy2bf68f02012-03-02 13:37:47 -0800325 Rect dirtyRect;
Romain Guy5bb3c732012-11-29 17:52:58 -0800326 bool debugDrawUpdate;
Chris Craik34416ea2013-04-15 16:08:28 -0700327 bool hasDrawnSinceUpdate;
John Reck443a7142014-09-04 17:40:05 -0700328 bool wasBuildLayered;
Romain Guy2bf68f02012-03-02 13:37:47 -0800329
Romain Guy9ace8f52011-07-07 20:50:11 -0700330private:
John Reck668f0e32014-03-26 15:10:40 -0700331 void requireRenderer();
Chris Craik69e5adf2014-08-14 13:34:01 -0700332 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer);
John Reck668f0e32014-03-26 15:10:40 -0700333
Romain Guy8aa195d2013-06-04 18:00:09 -0700334 Caches& caches;
335
John Reck3b202512014-06-23 13:13:08 -0700336 RenderState& renderState;
337
Romain Guy9ace8f52011-07-07 20:50:11 -0700338 /**
339 * Name of the FBO used to render the layer. If the name is 0
340 * this layer is not backed by an FBO, but a simple texture.
341 */
342 GLuint fbo;
343
344 /**
Romain Guy3bbacf22013-02-06 16:51:04 -0800345 * The render buffer used as the stencil buffer.
Romain Guy8ce00302013-01-15 18:51:42 -0800346 */
Romain Guy3bbacf22013-02-06 16:51:04 -0800347 RenderBuffer* stencil;
Romain Guy8ce00302013-01-15 18:51:42 -0800348
349 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700350 * Indicates whether this layer has been used already.
351 */
352 bool empty;
353
354 /**
355 * The texture backing this layer.
356 */
357 Texture texture;
358
Romain Guyaa6c24c2011-04-28 18:40:04 -0700359 /**
360 * If set to true (by default), the layer can be reused.
361 */
Romain Guy9ace8f52011-07-07 20:50:11 -0700362 bool cacheable;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700363
364 /**
Chris Craik8a226d22014-09-08 16:40:21 -0700365 * Denotes whether the layer is a DisplayList, or Texture layer.
Romain Guyaa6c24c2011-04-28 18:40:04 -0700366 */
Chris Craik8a226d22014-09-08 16:40:21 -0700367 const Type type;
Romain Guy9ace8f52011-07-07 20:50:11 -0700368
369 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700370 * When set to true, this layer is dirty and should be cleared
371 * before any rendering occurs.
372 */
373 bool dirty;
374
375 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700376 * Indicates the render target.
377 */
378 GLenum renderTarget;
379
380 /**
381 * Color filter used to draw this layer. Optional.
382 */
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500383 SkColorFilter* colorFilter;
Romain Guy9ace8f52011-07-07 20:50:11 -0700384
385 /**
Chris Craik9757ac02014-02-25 18:50:17 -0800386 * Indicates raster data backing the layer is scaled, requiring filtration.
387 */
388 bool forceFilter;
389
390 /**
Romain Guy9ace8f52011-07-07 20:50:11 -0700391 * Opacity of the layer.
392 */
393 int alpha;
Chris Craik9757ac02014-02-25 18:50:17 -0800394
Romain Guy9ace8f52011-07-07 20:50:11 -0700395 /**
396 * Blending mode of the layer.
397 */
398 SkXfermode::Mode mode;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700399
400 /**
401 * Optional texture coordinates transform.
402 */
403 mat4 texTransform;
Romain Guy8f0095c2011-05-02 17:24:22 -0700404
Romain Guy302a9df2011-08-16 13:55:02 -0700405 /**
406 * Optional transform.
407 */
408 mat4 transform;
409
Romain Guy96885eb2013-03-26 15:05:58 -0700410 /**
Chris Craik69e5adf2014-08-14 13:34:01 -0700411 * Cached transform of layer in window, updated only on creation / resize
412 */
413 mat4 cachedInvTransformInWindow;
414 bool rendererLightPosDirty;
415
416 /**
Romain Guy96885eb2013-03-26 15:05:58 -0700417 * Used to defer display lists when the layer is updated with a
418 * display list.
419 */
420 DeferredDisplayList* deferredList;
421
Chris Craik3f0854292014-04-15 16:18:08 -0700422 /**
423 * This convex path should be used to mask the layer's draw to the screen.
424 *
425 * Data not owned/managed by layer object.
426 */
427 const SkPath* convexMask;
428
Romain Guydda570202010-07-06 11:39:32 -0700429}; // struct Layer
430
431}; // namespace uirenderer
432}; // namespace android
433
Romain Guy5b3b3522010-10-27 18:57:51 -0700434#endif // ANDROID_HWUI_LAYER_H