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