Layer.h revision 0e89e2b7bcb2c035e8cee77f93120e7c5617f8d2
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 17#ifndef ANDROID_HWUI_LAYER_H 18#define ANDROID_HWUI_LAYER_H 19 20#include <cutils/compiler.h> 21#include <sys/types.h> 22#include <utils/StrongPointer.h> 23 24#include <GLES2/gl2.h> 25 26#include <ui/Region.h> 27 28#include <SkPaint.h> 29#include <SkXfermode.h> 30 31#include "Matrix.h" 32#include "Rect.h" 33#include "RenderBuffer.h" 34#include "Texture.h" 35#include "Vertex.h" 36 37namespace android { 38namespace uirenderer { 39 40/////////////////////////////////////////////////////////////////////////////// 41// Layers 42/////////////////////////////////////////////////////////////////////////////// 43 44// Forward declarations 45class Caches; 46class RenderState; 47class OpenGLRenderer; 48class RenderNode; 49class DeferredDisplayList; 50class DeferStateStruct; 51 52/** 53 * A layer has dimensions and is backed by an OpenGL texture or FBO. 54 */ 55class Layer : public VirtualLightRefBase { 56public: 57 enum Type { 58 kType_Texture, 59 kType_DisplayList, 60 }; 61 62 // 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 73 Layer(Type type, RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight); 74 ~Layer(); 75 76 static uint32_t computeIdealWidth(uint32_t layerWidth); 77 static uint32_t computeIdealHeight(uint32_t layerHeight); 78 79 /** 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); 85 86 /** 87 * 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 95 const float texX = 1.0f / float(texture.width); 96 const float texY = 1.0f / float(texture.height); 97 const float height = layer.getHeight(); 98 texCoords.set( 99 regionRect.left * texX, (height - regionRect.top) * texY, 100 regionRect.right * texX, (height - regionRect.bottom) * texY); 101 102 regionRect.translate(layer.left, layer.top); 103 } 104 105 void setWindowTransform(Matrix4& windowTransform) { 106 cachedInvTransformInWindow.loadInverse(windowTransform); 107 rendererLightPosDirty = true; 108 } 109 110 void updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom); 111 112 inline uint32_t getWidth() const { 113 return texture.width; 114 } 115 116 inline uint32_t getHeight() const { 117 return texture.height; 118 } 119 120 /** 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 131 void setSize(uint32_t width, uint32_t height) { 132 texture.width = width; 133 texture.height = height; 134 } 135 136 ANDROID_API void setPaint(const SkPaint* paint); 137 138 inline void setBlend(bool blend) { 139 texture.blend = blend; 140 } 141 142 inline bool isBlend() const { 143 return texture.blend; 144 } 145 146 inline void setForceFilter(bool forceFilter) { 147 this->forceFilter = forceFilter; 148 } 149 150 inline bool getForceFilter() const { 151 return forceFilter; 152 } 153 154 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 163 inline int getAlpha() const { 164 return alpha; 165 } 166 167 inline SkXfermode::Mode getMode() const { 168 return mode; 169 } 170 171 inline void setEmpty(bool empty) { 172 this->empty = empty; 173 } 174 175 inline bool isEmpty() const { 176 return empty; 177 } 178 179 inline void setFbo(GLuint fbo) { 180 this->fbo = fbo; 181 } 182 183 inline GLuint getFbo() const { 184 return fbo; 185 } 186 187 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 } 195 } 196 197 inline RenderBuffer* getStencilRenderBuffer() const { 198 return stencil; 199 } 200 201 inline GLuint getTexture() const { 202 return texture.id; 203 } 204 205 inline GLenum getRenderTarget() const { 206 return renderTarget; 207 } 208 209 inline void setRenderTarget(GLenum renderTarget) { 210 this->renderTarget = renderTarget; 211 } 212 213 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { 214 texture.setWrap(wrap, bindTexture, force, renderTarget); 215 } 216 217 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { 218 texture.setFilter(filter, bindTexture, force, renderTarget); 219 } 220 221 inline bool isCacheable() const { 222 return cacheable; 223 } 224 225 inline void setCacheable(bool cacheable) { 226 this->cacheable = cacheable; 227 } 228 229 inline bool isDirty() const { 230 return dirty; 231 } 232 233 inline void setDirty(bool dirty) { 234 this->dirty = dirty; 235 } 236 237 inline bool isTextureLayer() const { 238 return type == kType_Texture; 239 } 240 241 inline SkColorFilter* getColorFilter() const { 242 return colorFilter; 243 } 244 245 ANDROID_API void setColorFilter(SkColorFilter* filter); 246 247 inline void setConvexMask(const SkPath* convexMask) { 248 this->convexMask = convexMask; 249 } 250 251 inline const SkPath* getConvexMask() { 252 return convexMask; 253 } 254 255 void bindStencilRenderBuffer() const; 256 257 void bindTexture() const; 258 void generateTexture(); 259 void allocateTexture(); 260 void deleteTexture(); 261 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 */ 267 ANDROID_API void clearTexture(); 268 269 inline mat4& getTexTransform() { 270 return texTransform; 271 } 272 273 inline mat4& getTransform() { 274 return transform; 275 } 276 277 void defer(const OpenGLRenderer& rootRenderer); 278 void cancelDefer(); 279 void flush(); 280 void render(const OpenGLRenderer& rootRenderer); 281 282 /** 283 * Posts a decStrong call to the appropriate thread. 284 * Thread-safe. 285 */ 286 void postDecStrong(); 287 288 /** 289 * Bounds of the layer. 290 */ 291 Rect layer; 292 /** 293 * Texture coordinates of the layer. 294 */ 295 Rect texCoords; 296 /** 297 * Clipping rectangle. 298 */ 299 Rect clipRect; 300 301 /** 302 * Dirty region indicating what parts of the layer 303 * have been drawn. 304 */ 305 Region region; 306 /** 307 * If the region is a rectangle, coordinates of the 308 * region are stored here. 309 */ 310 Rect regionRect; 311 312 /** 313 * If the layer can be rendered as a mesh, this is non-null. 314 */ 315 TextureVertex* mesh; 316 GLsizei meshElementCount; 317 318 /** 319 * Used for deferred updates. 320 */ 321 bool deferredUpdateScheduled; 322 OpenGLRenderer* renderer; 323 sp<RenderNode> renderNode; 324 Rect dirtyRect; 325 bool debugDrawUpdate; 326 bool hasDrawnSinceUpdate; 327 bool wasBuildLayered; 328 329private: 330 void requireRenderer(); 331 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer); 332 333 Caches& caches; 334 335 RenderState& renderState; 336 337 /** 338 * Name of the FBO used to render the layer. If the name is 0 339 * this layer is not backed by an FBO, but a simple texture. 340 */ 341 GLuint fbo; 342 343 /** 344 * The render buffer used as the stencil buffer. 345 */ 346 RenderBuffer* stencil; 347 348 /** 349 * Indicates whether this layer has been used already. 350 */ 351 bool empty; 352 353 /** 354 * The texture backing this layer. 355 */ 356 Texture texture; 357 358 /** 359 * If set to true (by default), the layer can be reused. 360 */ 361 bool cacheable; 362 363 /** 364 * Denotes whether the layer is a DisplayList, or Texture layer. 365 */ 366 const Type type; 367 368 /** 369 * When set to true, this layer is dirty and should be cleared 370 * before any rendering occurs. 371 */ 372 bool dirty; 373 374 /** 375 * Indicates the render target. 376 */ 377 GLenum renderTarget; 378 379 /** 380 * Color filter used to draw this layer. Optional. 381 */ 382 SkColorFilter* colorFilter; 383 384 /** 385 * Indicates raster data backing the layer is scaled, requiring filtration. 386 */ 387 bool forceFilter; 388 389 /** 390 * Opacity of the layer. 391 */ 392 int alpha; 393 394 /** 395 * Blending mode of the layer. 396 */ 397 SkXfermode::Mode mode; 398 399 /** 400 * Optional texture coordinates transform. 401 */ 402 mat4 texTransform; 403 404 /** 405 * Optional transform. 406 */ 407 mat4 transform; 408 409 /** 410 * Cached transform of layer in window, updated only on creation / resize 411 */ 412 mat4 cachedInvTransformInWindow; 413 bool rendererLightPosDirty; 414 415 /** 416 * Used to defer display lists when the layer is updated with a 417 * display list. 418 */ 419 DeferredDisplayList* deferredList; 420 421 /** 422 * This convex path should be used to mask the layer's draw to the screen. 423 * 424 * Data not owned/managed by layer object. 425 */ 426 const SkPath* convexMask; 427 428}; // struct Layer 429 430}; // namespace uirenderer 431}; // namespace android 432 433#endif // ANDROID_HWUI_LAYER_H 434