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