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