Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_LAYER_H |
| 18 | #define ANDROID_HWUI_LAYER_H |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 19 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 22 | #include <GLES2/gl2.h> |
| 23 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 24 | #include <ui/Region.h> |
| 25 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 26 | #include <SkXfermode.h> |
| 27 | |
| 28 | #include "Rect.h" |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 29 | #include "SkiaColorFilter.h" |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 30 | #include "Texture.h" |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 31 | #include "Vertex.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | // Layers |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 39 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 40 | // Forward declarations |
| 41 | class OpenGLRenderer; |
| 42 | class DisplayList; |
| 43 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 44 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 45 | * A layer has dimensions and is backed by an OpenGL texture or FBO. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 46 | */ |
| 47 | struct Layer { |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 48 | Layer(const uint32_t layerWidth, const uint32_t layerHeight); |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 49 | ~Layer(); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 50 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 51 | static uint32_t computeIdealWidth(uint32_t layerWidth); |
| 52 | static uint32_t computeIdealHeight(uint32_t layerHeight); |
| 53 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Calling this method will remove (either by recycling or |
| 56 | * destroying) the associated FBO, if present, and any render |
| 57 | * buffer (stencil for instance.) |
| 58 | */ |
| 59 | void removeFbo(bool flush = true); |
Dave Burke | 56257af | 2012-09-25 20:30:09 -0700 | [diff] [blame] | 60 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 61 | /** |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 62 | * Sets this layer's region to a rectangle. Computes the appropriate |
| 63 | * texture coordinates. |
| 64 | */ |
| 65 | void setRegionAsRect() { |
| 66 | const android::Rect& bounds = region.getBounds(); |
| 67 | regionRect.set(bounds.leftTop().x, bounds.leftTop().y, |
| 68 | bounds.rightBottom().x, bounds.rightBottom().y); |
| 69 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 70 | const float texX = 1.0f / float(texture.width); |
| 71 | const float texY = 1.0f / float(texture.height); |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 72 | const float height = layer.getHeight(); |
| 73 | texCoords.set( |
| 74 | regionRect.left * texX, (height - regionRect.top) * texY, |
| 75 | regionRect.right * texX, (height - regionRect.bottom) * texY); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 76 | |
| 77 | regionRect.translate(layer.left, layer.top); |
| 78 | } |
| 79 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 80 | void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList, |
| 81 | int left, int top, int right, int bottom) { |
| 82 | this->renderer = renderer; |
| 83 | this->displayList = displayList; |
| 84 | const Rect r(left, top, right, bottom); |
| 85 | dirtyRect.unionWith(r); |
| 86 | deferredUpdateScheduled = true; |
| 87 | } |
| 88 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 89 | inline uint32_t getWidth() { |
| 90 | return texture.width; |
| 91 | } |
| 92 | |
| 93 | inline uint32_t getHeight() { |
| 94 | return texture.height; |
| 95 | } |
| 96 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Resize the layer and its texture if needed. |
| 99 | * |
| 100 | * @param width The new width of the layer |
| 101 | * @param height The new height of the layer |
| 102 | * |
| 103 | * @return True if the layer was resized or nothing happened, false if |
| 104 | * a failure occurred during the resizing operation |
| 105 | */ |
| 106 | bool resize(const uint32_t width, const uint32_t height); |
| 107 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 108 | void setSize(uint32_t width, uint32_t height) { |
| 109 | texture.width = width; |
| 110 | texture.height = height; |
| 111 | } |
| 112 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 113 | ANDROID_API void setPaint(SkPaint* paint); |
| 114 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 115 | inline void setBlend(bool blend) { |
| 116 | texture.blend = blend; |
| 117 | } |
| 118 | |
| 119 | inline bool isBlend() { |
| 120 | return texture.blend; |
| 121 | } |
| 122 | |
| 123 | inline void setAlpha(int alpha) { |
| 124 | this->alpha = alpha; |
| 125 | } |
| 126 | |
| 127 | inline void setAlpha(int alpha, SkXfermode::Mode mode) { |
| 128 | this->alpha = alpha; |
| 129 | this->mode = mode; |
| 130 | } |
| 131 | |
| 132 | inline int getAlpha() { |
| 133 | return alpha; |
| 134 | } |
| 135 | |
| 136 | inline SkXfermode::Mode getMode() { |
| 137 | return mode; |
| 138 | } |
| 139 | |
| 140 | inline void setEmpty(bool empty) { |
| 141 | this->empty = empty; |
| 142 | } |
| 143 | |
| 144 | inline bool isEmpty() { |
| 145 | return empty; |
| 146 | } |
| 147 | |
| 148 | inline void setFbo(GLuint fbo) { |
| 149 | this->fbo = fbo; |
| 150 | } |
| 151 | |
| 152 | inline GLuint getFbo() { |
| 153 | return fbo; |
| 154 | } |
| 155 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 156 | inline void setStencilRenderBuffer(GLuint renderBuffer) { |
| 157 | this->stencil = renderBuffer; |
| 158 | } |
| 159 | |
| 160 | inline GLuint getStencilRenderBuffer() { |
| 161 | return stencil; |
| 162 | } |
| 163 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 164 | inline GLuint getTexture() { |
| 165 | return texture.id; |
| 166 | } |
| 167 | |
| 168 | inline GLenum getRenderTarget() { |
| 169 | return renderTarget; |
| 170 | } |
| 171 | |
| 172 | inline void setRenderTarget(GLenum renderTarget) { |
| 173 | this->renderTarget = renderTarget; |
| 174 | } |
| 175 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 176 | void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { |
| 177 | texture.setWrap(wrap, bindTexture, force, renderTarget); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 180 | void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { |
| 181 | texture.setFilter(filter, bindTexture, force, renderTarget); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | inline bool isCacheable() { |
| 185 | return cacheable; |
| 186 | } |
| 187 | |
| 188 | inline void setCacheable(bool cacheable) { |
| 189 | this->cacheable = cacheable; |
| 190 | } |
| 191 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 192 | inline bool isDirty() { |
| 193 | return dirty; |
| 194 | } |
| 195 | |
| 196 | inline void setDirty(bool dirty) { |
| 197 | this->dirty = dirty; |
| 198 | } |
| 199 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 200 | inline bool isTextureLayer() { |
| 201 | return textureLayer; |
| 202 | } |
| 203 | |
| 204 | inline void setTextureLayer(bool textureLayer) { |
| 205 | this->textureLayer = textureLayer; |
| 206 | } |
| 207 | |
| 208 | inline SkiaColorFilter* getColorFilter() { |
| 209 | return colorFilter; |
| 210 | } |
| 211 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 212 | ANDROID_API void setColorFilter(SkiaColorFilter* filter); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 213 | |
| 214 | inline void bindTexture() { |
Romain Guy | ef09a21 | 2012-09-25 12:17:14 -0700 | [diff] [blame] | 215 | if (texture.id) { |
| 216 | glBindTexture(renderTarget, texture.id); |
| 217 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 220 | inline void bindStencilRenderBuffer() { |
| 221 | if (stencil) { |
| 222 | glBindRenderbuffer(GL_RENDERBUFFER, stencil); |
| 223 | } |
| 224 | } |
| 225 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 226 | inline void generateTexture() { |
Romain Guy | ef09a21 | 2012-09-25 12:17:14 -0700 | [diff] [blame] | 227 | if (!texture.id) { |
| 228 | glGenTextures(1, &texture.id); |
| 229 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | inline void deleteTexture() { |
Romain Guy | ef09a21 | 2012-09-25 12:17:14 -0700 | [diff] [blame] | 233 | if (texture.id) { |
| 234 | glDeleteTextures(1, &texture.id); |
| 235 | texture.id = 0; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * When the caller frees the texture itself, the caller |
| 241 | * must call this method to tell this layer that it lost |
| 242 | * the texture. |
| 243 | */ |
| 244 | void clearTexture() { |
| 245 | texture.id = 0; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | inline void allocateTexture(GLenum format, GLenum storage) { |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 249 | #if DEBUG_LAYERS |
| 250 | ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight()); |
| 251 | #endif |
Romain Guy | 2055aba | 2013-01-18 16:42:51 -0800 | [diff] [blame] | 252 | if (texture.id) { |
| 253 | glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, |
| 254 | format, storage, NULL); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | inline void allocateStencilRenderBuffer() { |
| 259 | if (stencil) { |
| 260 | glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, getWidth(), getHeight()); |
| 261 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | inline mat4& getTexTransform() { |
| 265 | return texTransform; |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 268 | inline mat4& getTransform() { |
| 269 | return transform; |
| 270 | } |
| 271 | |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 272 | /** |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 273 | * Bounds of the layer. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 274 | */ |
| 275 | Rect layer; |
| 276 | /** |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 277 | * Texture coordinates of the layer. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 278 | */ |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 279 | Rect texCoords; |
| 280 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 281 | /** |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 282 | * Dirty region indicating what parts of the layer |
| 283 | * have been drawn. |
| 284 | */ |
| 285 | Region region; |
Romain Guy | 4066767 | 2011-03-18 14:34:03 -0700 | [diff] [blame] | 286 | /** |
| 287 | * If the region is a rectangle, coordinates of the |
| 288 | * region are stored here. |
| 289 | */ |
| 290 | Rect regionRect; |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 291 | |
| 292 | /** |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 293 | * If the layer can be rendered as a mesh, this is non-null. |
| 294 | */ |
| 295 | TextureVertex* mesh; |
| 296 | uint16_t* meshIndices; |
| 297 | GLsizei meshElementCount; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 298 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 299 | /** |
| 300 | * Used for deferred updates. |
| 301 | */ |
| 302 | bool deferredUpdateScheduled; |
| 303 | OpenGLRenderer* renderer; |
| 304 | DisplayList* displayList; |
| 305 | Rect dirtyRect; |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 306 | bool debugDrawUpdate; |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 307 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 308 | private: |
| 309 | /** |
| 310 | * Name of the FBO used to render the layer. If the name is 0 |
| 311 | * this layer is not backed by an FBO, but a simple texture. |
| 312 | */ |
| 313 | GLuint fbo; |
| 314 | |
| 315 | /** |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 316 | * Name of the render buffer used as the stencil buffer. If the |
| 317 | * name is 0, this layer does not have a stencil buffer. |
| 318 | */ |
| 319 | GLuint stencil; |
| 320 | |
| 321 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 322 | * Indicates whether this layer has been used already. |
| 323 | */ |
| 324 | bool empty; |
| 325 | |
| 326 | /** |
| 327 | * The texture backing this layer. |
| 328 | */ |
| 329 | Texture texture; |
| 330 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 331 | /** |
| 332 | * If set to true (by default), the layer can be reused. |
| 333 | */ |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 334 | bool cacheable; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * When set to true, this layer must be treated as a texture |
| 338 | * layer. |
| 339 | */ |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 340 | bool textureLayer; |
| 341 | |
| 342 | /** |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 343 | * When set to true, this layer is dirty and should be cleared |
| 344 | * before any rendering occurs. |
| 345 | */ |
| 346 | bool dirty; |
| 347 | |
| 348 | /** |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 349 | * Indicates the render target. |
| 350 | */ |
| 351 | GLenum renderTarget; |
| 352 | |
| 353 | /** |
| 354 | * Color filter used to draw this layer. Optional. |
| 355 | */ |
| 356 | SkiaColorFilter* colorFilter; |
| 357 | |
| 358 | /** |
| 359 | * Opacity of the layer. |
| 360 | */ |
| 361 | int alpha; |
| 362 | /** |
| 363 | * Blending mode of the layer. |
| 364 | */ |
| 365 | SkXfermode::Mode mode; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 366 | |
| 367 | /** |
| 368 | * Optional texture coordinates transform. |
| 369 | */ |
| 370 | mat4 texTransform; |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 371 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 372 | /** |
| 373 | * Optional transform. |
| 374 | */ |
| 375 | mat4 transform; |
| 376 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 377 | }; // struct Layer |
| 378 | |
| 379 | }; // namespace uirenderer |
| 380 | }; // namespace android |
| 381 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 382 | #endif // ANDROID_HWUI_LAYER_H |