Layer.h revision d15ebf25c595b855f6978d0600218e3ea5f31e92
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 <SkXfermode.h> 27 28#include "Rect.h" 29#include "SkiaColorFilter.h" 30#include "Texture.h" 31#include "Vertex.h" 32 33namespace android { 34namespace uirenderer { 35 36/////////////////////////////////////////////////////////////////////////////// 37// Layers 38/////////////////////////////////////////////////////////////////////////////// 39 40// Forward declarations 41class OpenGLRenderer; 42class DisplayList; 43 44/** 45 * A layer has dimensions and is backed by an OpenGL texture or FBO. 46 */ 47struct Layer { 48 49 Layer(const uint32_t layerWidth, const uint32_t layerHeight) { 50 mesh = NULL; 51 meshIndices = NULL; 52 meshElementCount = 0; 53 cacheable = true; 54 textureLayer = false; 55 renderTarget = GL_TEXTURE_2D; 56 texture.width = layerWidth; 57 texture.height = layerHeight; 58 colorFilter = NULL; 59 deferredUpdateScheduled = false; 60 renderer = NULL; 61 displayList = NULL; 62 } 63 64 ~Layer(); 65 66 /** 67 * Sets this layer's region to a rectangle. Computes the appropriate 68 * texture coordinates. 69 */ 70 void setRegionAsRect() { 71 const android::Rect& bounds = region.getBounds(); 72 regionRect.set(bounds.leftTop().x, bounds.leftTop().y, 73 bounds.rightBottom().x, bounds.rightBottom().y); 74 75 const float texX = 1.0f / float(texture.width); 76 const float texY = 1.0f / float(texture.height); 77 const float height = layer.getHeight(); 78 texCoords.set( 79 regionRect.left * texX, (height - regionRect.top) * texY, 80 regionRect.right * texX, (height - regionRect.bottom) * texY); 81 82 regionRect.translate(layer.left, layer.top); 83 } 84 85 void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList, 86 int left, int top, int right, int bottom) { 87 this->renderer = renderer; 88 this->displayList = displayList; 89 const Rect r(left, top, right, bottom); 90 dirtyRect.unionWith(r); 91 deferredUpdateScheduled = true; 92 } 93 94 inline uint32_t getWidth() { 95 return texture.width; 96 } 97 98 inline uint32_t getHeight() { 99 return texture.height; 100 } 101 102 void setSize(uint32_t width, uint32_t height) { 103 texture.width = width; 104 texture.height = height; 105 } 106 107 ANDROID_API void setPaint(SkPaint* paint); 108 109 inline void setBlend(bool blend) { 110 texture.blend = blend; 111 } 112 113 inline bool isBlend() { 114 return texture.blend; 115 } 116 117 inline void setAlpha(int alpha) { 118 this->alpha = alpha; 119 } 120 121 inline void setAlpha(int alpha, SkXfermode::Mode mode) { 122 this->alpha = alpha; 123 this->mode = mode; 124 } 125 126 inline int getAlpha() { 127 return alpha; 128 } 129 130 inline SkXfermode::Mode getMode() { 131 return mode; 132 } 133 134 inline void setEmpty(bool empty) { 135 this->empty = empty; 136 } 137 138 inline bool isEmpty() { 139 return empty; 140 } 141 142 inline void setFbo(GLuint fbo) { 143 this->fbo = fbo; 144 } 145 146 inline GLuint getFbo() { 147 return fbo; 148 } 149 150 inline GLuint* getTexturePointer() { 151 return &texture.id; 152 } 153 154 inline GLuint getTexture() { 155 return texture.id; 156 } 157 158 inline GLenum getRenderTarget() { 159 return renderTarget; 160 } 161 162 inline void setRenderTarget(GLenum renderTarget) { 163 this->renderTarget = renderTarget; 164 } 165 166 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { 167 texture.setWrap(wrap, bindTexture, force, renderTarget); 168 } 169 170 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { 171 texture.setFilter(filter, bindTexture, force, renderTarget); 172 } 173 174 inline bool isCacheable() { 175 return cacheable; 176 } 177 178 inline void setCacheable(bool cacheable) { 179 this->cacheable = cacheable; 180 } 181 182 inline bool isTextureLayer() { 183 return textureLayer; 184 } 185 186 inline void setTextureLayer(bool textureLayer) { 187 this->textureLayer = textureLayer; 188 } 189 190 inline SkiaColorFilter* getColorFilter() { 191 return colorFilter; 192 } 193 194 ANDROID_API void setColorFilter(SkiaColorFilter* filter); 195 196 inline void bindTexture() { 197 glBindTexture(renderTarget, texture.id); 198 } 199 200 inline void generateTexture() { 201 glGenTextures(1, &texture.id); 202 } 203 204 inline void deleteTexture() { 205 if (texture.id) glDeleteTextures(1, &texture.id); 206 } 207 208 inline void deleteFbo() { 209 if (fbo) glDeleteFramebuffers(1, &fbo); 210 } 211 212 inline void allocateTexture(GLenum format, GLenum storage) { 213 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL); 214 } 215 216 inline mat4& getTexTransform() { 217 return texTransform; 218 } 219 220 inline mat4& getTransform() { 221 return transform; 222 } 223 224 /** 225 * Bounds of the layer. 226 */ 227 Rect layer; 228 /** 229 * Texture coordinates of the layer. 230 */ 231 Rect texCoords; 232 233 /** 234 * Dirty region indicating what parts of the layer 235 * have been drawn. 236 */ 237 Region region; 238 /** 239 * If the region is a rectangle, coordinates of the 240 * region are stored here. 241 */ 242 Rect regionRect; 243 244 /** 245 * If the layer can be rendered as a mesh, this is non-null. 246 */ 247 TextureVertex* mesh; 248 uint16_t* meshIndices; 249 GLsizei meshElementCount; 250 251 /** 252 * Used for deferred updates. 253 */ 254 bool deferredUpdateScheduled; 255 OpenGLRenderer* renderer; 256 DisplayList* displayList; 257 Rect dirtyRect; 258 259private: 260 /** 261 * Name of the FBO used to render the layer. If the name is 0 262 * this layer is not backed by an FBO, but a simple texture. 263 */ 264 GLuint fbo; 265 266 /** 267 * Indicates whether this layer has been used already. 268 */ 269 bool empty; 270 271 /** 272 * The texture backing this layer. 273 */ 274 Texture texture; 275 276 /** 277 * If set to true (by default), the layer can be reused. 278 */ 279 bool cacheable; 280 281 /** 282 * When set to true, this layer must be treated as a texture 283 * layer. 284 */ 285 bool textureLayer; 286 287 /** 288 * Indicates the render target. 289 */ 290 GLenum renderTarget; 291 292 /** 293 * Color filter used to draw this layer. Optional. 294 */ 295 SkiaColorFilter* colorFilter; 296 297 /** 298 * Opacity of the layer. 299 */ 300 int alpha; 301 /** 302 * Blending mode of the layer. 303 */ 304 SkXfermode::Mode mode; 305 306 /** 307 * Optional texture coordinates transform. 308 */ 309 mat4 texTransform; 310 311 /** 312 * Optional transform. 313 */ 314 mat4 transform; 315 316}; // struct Layer 317 318}; // namespace uirenderer 319}; // namespace android 320 321#endif // ANDROID_HWUI_LAYER_H 322