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