Layer.h revision 69e5adffb19135d51bde8e458f4907d7265f3e23
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 { 56public: 57 Layer(RenderState& renderState, const uint32_t layerWidth, const uint32_t layerHeight); 58 ~Layer(); 59 60 static uint32_t computeIdealWidth(uint32_t layerWidth); 61 static uint32_t computeIdealHeight(uint32_t layerHeight); 62 63 /** 64 * Calling this method will remove (either by recycling or 65 * destroying) the associated FBO, if present, and any render 66 * buffer (stencil for instance.) 67 */ 68 void removeFbo(bool flush = true); 69 70 /** 71 * Sets this layer's region to a rectangle. Computes the appropriate 72 * texture coordinates. 73 */ 74 void setRegionAsRect() { 75 const android::Rect& bounds = region.getBounds(); 76 regionRect.set(bounds.leftTop().x, bounds.leftTop().y, 77 bounds.rightBottom().x, bounds.rightBottom().y); 78 79 const float texX = 1.0f / float(texture.width); 80 const float texY = 1.0f / float(texture.height); 81 const float height = layer.getHeight(); 82 texCoords.set( 83 regionRect.left * texX, (height - regionRect.top) * texY, 84 regionRect.right * texX, (height - regionRect.bottom) * texY); 85 86 regionRect.translate(layer.left, layer.top); 87 } 88 89 void setWindowTransform(Matrix4& windowTransform) { 90 cachedInvTransformInWindow.loadInverse(windowTransform); 91 rendererLightPosDirty = true; 92 } 93 94 void updateDeferred(RenderNode* renderNode, int left, int top, int right, int bottom); 95 96 inline uint32_t getWidth() const { 97 return texture.width; 98 } 99 100 inline uint32_t getHeight() const { 101 return texture.height; 102 } 103 104 /** 105 * Resize the layer and its texture if needed. 106 * 107 * @param width The new width of the layer 108 * @param height The new height of the layer 109 * 110 * @return True if the layer was resized or nothing happened, false if 111 * a failure occurred during the resizing operation 112 */ 113 bool resize(const uint32_t width, const uint32_t height); 114 115 void setSize(uint32_t width, uint32_t height) { 116 texture.width = width; 117 texture.height = height; 118 } 119 120 ANDROID_API void setPaint(const SkPaint* paint); 121 122 inline void setBlend(bool blend) { 123 texture.blend = blend; 124 } 125 126 inline bool isBlend() const { 127 return texture.blend; 128 } 129 130 inline void setForceFilter(bool forceFilter) { 131 this->forceFilter = forceFilter; 132 } 133 134 inline bool getForceFilter() const { 135 return forceFilter; 136 } 137 138 inline void setAlpha(int alpha) { 139 this->alpha = alpha; 140 } 141 142 inline void setAlpha(int alpha, SkXfermode::Mode mode) { 143 this->alpha = alpha; 144 this->mode = mode; 145 } 146 147 inline int getAlpha() const { 148 return alpha; 149 } 150 151 inline SkXfermode::Mode getMode() const { 152 return mode; 153 } 154 155 inline void setEmpty(bool empty) { 156 this->empty = empty; 157 } 158 159 inline bool isEmpty() const { 160 return empty; 161 } 162 163 inline void setFbo(GLuint fbo) { 164 this->fbo = fbo; 165 } 166 167 inline GLuint getFbo() const { 168 return fbo; 169 } 170 171 inline void setStencilRenderBuffer(RenderBuffer* renderBuffer) { 172 if (RenderBuffer::isStencilBuffer(renderBuffer->getFormat())) { 173 this->stencil = renderBuffer; 174 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, 175 GL_RENDERBUFFER, stencil->getName()); 176 } else { 177 ALOGE("The specified render buffer is not a stencil buffer"); 178 } 179 } 180 181 inline RenderBuffer* getStencilRenderBuffer() const { 182 return stencil; 183 } 184 185 inline GLuint getTexture() const { 186 return texture.id; 187 } 188 189 inline GLenum getRenderTarget() const { 190 return renderTarget; 191 } 192 193 inline void setRenderTarget(GLenum renderTarget) { 194 this->renderTarget = renderTarget; 195 } 196 197 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { 198 texture.setWrap(wrap, bindTexture, force, renderTarget); 199 } 200 201 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { 202 texture.setFilter(filter, bindTexture, force, renderTarget); 203 } 204 205 inline bool isCacheable() const { 206 return cacheable; 207 } 208 209 inline void setCacheable(bool cacheable) { 210 this->cacheable = cacheable; 211 } 212 213 inline bool isDirty() const { 214 return dirty; 215 } 216 217 inline void setDirty(bool dirty) { 218 this->dirty = dirty; 219 } 220 221 inline bool isTextureLayer() const { 222 return textureLayer; 223 } 224 225 inline void setTextureLayer(bool textureLayer) { 226 this->textureLayer = textureLayer; 227 } 228 229 inline SkColorFilter* getColorFilter() const { 230 return colorFilter; 231 } 232 233 ANDROID_API void setColorFilter(SkColorFilter* filter); 234 235 inline void setConvexMask(const SkPath* convexMask) { 236 this->convexMask = convexMask; 237 } 238 239 inline const SkPath* getConvexMask() { 240 return convexMask; 241 } 242 243 void bindStencilRenderBuffer() const; 244 245 void bindTexture() const; 246 void generateTexture(); 247 void allocateTexture(); 248 void deleteTexture(); 249 250 /** 251 * When the caller frees the texture itself, the caller 252 * must call this method to tell this layer that it lost 253 * the texture. 254 */ 255 ANDROID_API void clearTexture(); 256 257 inline mat4& getTexTransform() { 258 return texTransform; 259 } 260 261 inline mat4& getTransform() { 262 return transform; 263 } 264 265 void defer(const OpenGLRenderer& rootRenderer); 266 void cancelDefer(); 267 void flush(); 268 void render(const OpenGLRenderer& rootRenderer); 269 270 /** 271 * Bounds of the layer. 272 */ 273 Rect layer; 274 /** 275 * Texture coordinates of the layer. 276 */ 277 Rect texCoords; 278 /** 279 * Clipping rectangle. 280 */ 281 Rect clipRect; 282 283 /** 284 * Dirty region indicating what parts of the layer 285 * have been drawn. 286 */ 287 Region region; 288 /** 289 * If the region is a rectangle, coordinates of the 290 * region are stored here. 291 */ 292 Rect regionRect; 293 294 /** 295 * If the layer can be rendered as a mesh, this is non-null. 296 */ 297 TextureVertex* mesh; 298 GLsizei meshElementCount; 299 300 /** 301 * Used for deferred updates. 302 */ 303 bool deferredUpdateScheduled; 304 OpenGLRenderer* renderer; 305 sp<RenderNode> renderNode; 306 Rect dirtyRect; 307 bool debugDrawUpdate; 308 bool hasDrawnSinceUpdate; 309 310private: 311 void requireRenderer(); 312 void updateLightPosFromRenderer(const OpenGLRenderer& rootRenderer); 313 314 Caches& caches; 315 316 RenderState& renderState; 317 318 /** 319 * Name of the FBO used to render the layer. If the name is 0 320 * this layer is not backed by an FBO, but a simple texture. 321 */ 322 GLuint fbo; 323 324 /** 325 * The render buffer used as the stencil buffer. 326 */ 327 RenderBuffer* stencil; 328 329 /** 330 * Indicates whether this layer has been used already. 331 */ 332 bool empty; 333 334 /** 335 * The texture backing this layer. 336 */ 337 Texture texture; 338 339 /** 340 * If set to true (by default), the layer can be reused. 341 */ 342 bool cacheable; 343 344 /** 345 * When set to true, this layer must be treated as a texture 346 * layer. 347 */ 348 bool textureLayer; 349 350 /** 351 * When set to true, this layer is dirty and should be cleared 352 * before any rendering occurs. 353 */ 354 bool dirty; 355 356 /** 357 * Indicates the render target. 358 */ 359 GLenum renderTarget; 360 361 /** 362 * Color filter used to draw this layer. Optional. 363 */ 364 SkColorFilter* colorFilter; 365 366 /** 367 * Indicates raster data backing the layer is scaled, requiring filtration. 368 */ 369 bool forceFilter; 370 371 /** 372 * Opacity of the layer. 373 */ 374 int alpha; 375 376 /** 377 * Blending mode of the layer. 378 */ 379 SkXfermode::Mode mode; 380 381 /** 382 * Optional texture coordinates transform. 383 */ 384 mat4 texTransform; 385 386 /** 387 * Optional transform. 388 */ 389 mat4 transform; 390 391 /** 392 * Cached transform of layer in window, updated only on creation / resize 393 */ 394 mat4 cachedInvTransformInWindow; 395 bool rendererLightPosDirty; 396 397 /** 398 * Used to defer display lists when the layer is updated with a 399 * display list. 400 */ 401 DeferredDisplayList* deferredList; 402 403 /** 404 * This convex path should be used to mask the layer's draw to the screen. 405 * 406 * Data not owned/managed by layer object. 407 */ 408 const SkPath* convexMask; 409 410}; // struct Layer 411 412}; // namespace uirenderer 413}; // namespace android 414 415#endif // ANDROID_HWUI_LAYER_H 416