Layer.h revision ca79cf69d09efa0c327e9b1237d86a119aea5da7
1dda570201ac851dd85af3861f7e575721d3345daRomain Guy/* 2dda570201ac851dd85af3861f7e575721d3345daRomain Guy * Copyright (C) 2010 The Android Open Source Project 3dda570201ac851dd85af3861f7e575721d3345daRomain Guy * 4dda570201ac851dd85af3861f7e575721d3345daRomain Guy * Licensed under the Apache License, Version 2.0 (the "License"); 5dda570201ac851dd85af3861f7e575721d3345daRomain Guy * you may not use this file except in compliance with the License. 6dda570201ac851dd85af3861f7e575721d3345daRomain Guy * You may obtain a copy of the License at 7dda570201ac851dd85af3861f7e575721d3345daRomain Guy * 8dda570201ac851dd85af3861f7e575721d3345daRomain Guy * http://www.apache.org/licenses/LICENSE-2.0 9dda570201ac851dd85af3861f7e575721d3345daRomain Guy * 10dda570201ac851dd85af3861f7e575721d3345daRomain Guy * Unless required by applicable law or agreed to in writing, software 11dda570201ac851dd85af3861f7e575721d3345daRomain Guy * distributed under the License is distributed on an "AS IS" BASIS, 12dda570201ac851dd85af3861f7e575721d3345daRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13dda570201ac851dd85af3861f7e575721d3345daRomain Guy * See the License for the specific language governing permissions and 14dda570201ac851dd85af3861f7e575721d3345daRomain Guy * limitations under the License. 15dda570201ac851dd85af3861f7e575721d3345daRomain Guy */ 16dda570201ac851dd85af3861f7e575721d3345daRomain Guy 17dda570201ac851dd85af3861f7e575721d3345daRomain Guy#ifndef ANDROID_HWUI_LAYER_H 18dda570201ac851dd85af3861f7e575721d3345daRomain Guy#define ANDROID_HWUI_LAYER_H 19dda570201ac851dd85af3861f7e575721d3345daRomain Guy 20f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy#include <sys/types.h> 21f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy 22dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include <GLES2/gl2.h> 23dda570201ac851dd85af3861f7e575721d3345daRomain Guy 24dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include <ui/Region.h> 25dda570201ac851dd85af3861f7e575721d3345daRomain Guy 26dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include <SkPaint.h> 27dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include <SkXfermode.h> 28dda570201ac851dd85af3861f7e575721d3345daRomain Guy 29dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include "Rect.h" 30dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include "SkiaColorFilter.h" 31dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include "Texture.h" 32dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include "Vertex.h" 33dda570201ac851dd85af3861f7e575721d3345daRomain Guy 34dda570201ac851dd85af3861f7e575721d3345daRomain Guynamespace android { 356c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guynamespace uirenderer { 366c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy 376c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy/////////////////////////////////////////////////////////////////////////////// 38dda570201ac851dd85af3861f7e575721d3345daRomain Guy// Layers 39dda570201ac851dd85af3861f7e575721d3345daRomain Guy/////////////////////////////////////////////////////////////////////////////// 40dda570201ac851dd85af3861f7e575721d3345daRomain Guy 41dda570201ac851dd85af3861f7e575721d3345daRomain Guy// Forward declarations 426c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guyclass OpenGLRenderer; 436c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guyclass DisplayList; 446c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy 456c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy/** 46dda570201ac851dd85af3861f7e575721d3345daRomain Guy * A layer has dimensions and is backed by an OpenGL texture or FBO. 476c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy */ 486c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guystruct Layer { 496c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy Layer(const uint32_t layerWidth, const uint32_t layerHeight); 50dda570201ac851dd85af3861f7e575721d3345daRomain Guy ~Layer(); 51dda570201ac851dd85af3861f7e575721d3345daRomain Guy 52dda570201ac851dd85af3861f7e575721d3345daRomain Guy void removeFbo(); 53dda570201ac851dd85af3861f7e575721d3345daRomain Guy 54dda570201ac851dd85af3861f7e575721d3345daRomain Guy /** 55dda570201ac851dd85af3861f7e575721d3345daRomain Guy * Sets this layer's region to a rectangle. Computes the appropriate 56dda570201ac851dd85af3861f7e575721d3345daRomain Guy * texture coordinates. 57dda570201ac851dd85af3861f7e575721d3345daRomain Guy */ 58dda570201ac851dd85af3861f7e575721d3345daRomain Guy void setRegionAsRect() { 59f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy const android::Rect& bounds = region.getBounds(); 60dda570201ac851dd85af3861f7e575721d3345daRomain Guy regionRect.set(bounds.leftTop().x, bounds.leftTop().y, 61dda570201ac851dd85af3861f7e575721d3345daRomain Guy bounds.rightBottom().x, bounds.rightBottom().y); 62dda570201ac851dd85af3861f7e575721d3345daRomain Guy 63dda570201ac851dd85af3861f7e575721d3345daRomain Guy const float texX = 1.0f / float(texture.width); 64dda570201ac851dd85af3861f7e575721d3345daRomain Guy const float texY = 1.0f / float(texture.height); 65dda570201ac851dd85af3861f7e575721d3345daRomain Guy const float height = layer.getHeight(); 66f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy texCoords.set( 67dda570201ac851dd85af3861f7e575721d3345daRomain Guy regionRect.left * texX, (height - regionRect.top) * texY, 68dda570201ac851dd85af3861f7e575721d3345daRomain Guy regionRect.right * texX, (height - regionRect.bottom) * texY); 69dda570201ac851dd85af3861f7e575721d3345daRomain Guy 70dda570201ac851dd85af3861f7e575721d3345daRomain Guy regionRect.translate(layer.left, layer.top); 71dda570201ac851dd85af3861f7e575721d3345daRomain Guy } 72dda570201ac851dd85af3861f7e575721d3345daRomain Guy 73dda570201ac851dd85af3861f7e575721d3345daRomain Guy void updateDeferred(OpenGLRenderer* renderer, DisplayList* displayList, 74dda570201ac851dd85af3861f7e575721d3345daRomain Guy int left, int top, int right, int bottom) { 75dda570201ac851dd85af3861f7e575721d3345daRomain Guy this->renderer = renderer; 76f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy this->displayList = displayList; 77dda570201ac851dd85af3861f7e575721d3345daRomain Guy const Rect r(left, top, right, bottom); 78dda570201ac851dd85af3861f7e575721d3345daRomain Guy dirtyRect.unionWith(r); 79dda570201ac851dd85af3861f7e575721d3345daRomain Guy deferredUpdateScheduled = true; 80dda570201ac851dd85af3861f7e575721d3345daRomain Guy } 81dda570201ac851dd85af3861f7e575721d3345daRomain Guy 82dda570201ac851dd85af3861f7e575721d3345daRomain Guy inline uint32_t getWidth() { 83dda570201ac851dd85af3861f7e575721d3345daRomain Guy return texture.width; 84dda570201ac851dd85af3861f7e575721d3345daRomain Guy } 8538c85b907a478af96d71b9a5df5a2066b8475311Romain Guy 8638c85b907a478af96d71b9a5df5a2066b8475311Romain Guy inline uint32_t getHeight() { 8738c85b907a478af96d71b9a5df5a2066b8475311Romain Guy return texture.height; 8838c85b907a478af96d71b9a5df5a2066b8475311Romain Guy } 89dda570201ac851dd85af3861f7e575721d3345daRomain Guy 90dda570201ac851dd85af3861f7e575721d3345daRomain Guy void setSize(uint32_t width, uint32_t height) { 91dda570201ac851dd85af3861f7e575721d3345daRomain Guy texture.width = width; 92dda570201ac851dd85af3861f7e575721d3345daRomain Guy texture.height = height; 93dda570201ac851dd85af3861f7e575721d3345daRomain Guy } 94dda570201ac851dd85af3861f7e575721d3345daRomain Guy 95 ANDROID_API void setPaint(SkPaint* paint); 96 97 inline void setBlend(bool blend) { 98 texture.blend = blend; 99 } 100 101 inline bool isBlend() { 102 return texture.blend; 103 } 104 105 inline void setAlpha(int alpha) { 106 this->alpha = alpha; 107 } 108 109 inline void setAlpha(int alpha, SkXfermode::Mode mode) { 110 this->alpha = alpha; 111 this->mode = mode; 112 } 113 114 inline int getAlpha() { 115 return alpha; 116 } 117 118 inline SkXfermode::Mode getMode() { 119 return mode; 120 } 121 122 inline void setEmpty(bool empty) { 123 this->empty = empty; 124 } 125 126 inline bool isEmpty() { 127 return empty; 128 } 129 130 inline void setFbo(GLuint fbo) { 131 this->fbo = fbo; 132 } 133 134 inline GLuint getFbo() { 135 return fbo; 136 } 137 138 inline GLuint getTexture() { 139 return texture.id; 140 } 141 142 inline GLenum getRenderTarget() { 143 return renderTarget; 144 } 145 146 inline void setRenderTarget(GLenum renderTarget) { 147 this->renderTarget = renderTarget; 148 } 149 150 void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { 151 texture.setWrap(wrap, bindTexture, force, renderTarget); 152 } 153 154 void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { 155 texture.setFilter(filter, bindTexture, force, renderTarget); 156 } 157 158 inline bool isCacheable() { 159 return cacheable; 160 } 161 162 inline void setCacheable(bool cacheable) { 163 this->cacheable = cacheable; 164 } 165 166 inline bool isDirty() { 167 return dirty; 168 } 169 170 inline void setDirty(bool dirty) { 171 this->dirty = dirty; 172 } 173 174 inline bool isTextureLayer() { 175 return textureLayer; 176 } 177 178 inline void setTextureLayer(bool textureLayer) { 179 this->textureLayer = textureLayer; 180 } 181 182 inline SkiaColorFilter* getColorFilter() { 183 return colorFilter; 184 } 185 186 ANDROID_API void setColorFilter(SkiaColorFilter* filter); 187 188 inline void bindTexture() { 189 if (texture.id) { 190 glBindTexture(renderTarget, texture.id); 191 } 192 } 193 194 inline void generateTexture() { 195 if (!texture.id) { 196 glGenTextures(1, &texture.id); 197 } 198 } 199 200 inline void deleteTexture() { 201 if (texture.id) { 202 glDeleteTextures(1, &texture.id); 203 texture.id = 0; 204 } 205 } 206 207 /** 208 * When the caller frees the texture itself, the caller 209 * must call this method to tell this layer that it lost 210 * the texture. 211 */ 212 void clearTexture() { 213 texture.id = 0; 214 } 215 216 inline void deleteFbo() { 217 if (fbo) glDeleteFramebuffers(1, &fbo); 218 } 219 220 inline void allocateTexture(GLenum format, GLenum storage) { 221#if DEBUG_LAYERS 222 ALOGD(" Allocate layer: %dx%d", getWidth(), getHeight()); 223#endif 224 glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL); 225 } 226 227 inline mat4& getTexTransform() { 228 return texTransform; 229 } 230 231 inline mat4& getTransform() { 232 return transform; 233 } 234 235 /** 236 * Bounds of the layer. 237 */ 238 Rect layer; 239 /** 240 * Texture coordinates of the layer. 241 */ 242 Rect texCoords; 243 244 /** 245 * Dirty region indicating what parts of the layer 246 * have been drawn. 247 */ 248 Region region; 249 /** 250 * If the region is a rectangle, coordinates of the 251 * region are stored here. 252 */ 253 Rect regionRect; 254 255 /** 256 * If the layer can be rendered as a mesh, this is non-null. 257 */ 258 TextureVertex* mesh; 259 uint16_t* meshIndices; 260 GLsizei meshElementCount; 261 262 /** 263 * Used for deferred updates. 264 */ 265 bool deferredUpdateScheduled; 266 OpenGLRenderer* renderer; 267 DisplayList* displayList; 268 Rect dirtyRect; 269 bool debugDrawUpdate; 270 271private: 272 /** 273 * Name of the FBO used to render the layer. If the name is 0 274 * this layer is not backed by an FBO, but a simple texture. 275 */ 276 GLuint fbo; 277 278 /** 279 * Indicates whether this layer has been used already. 280 */ 281 bool empty; 282 283 /** 284 * The texture backing this layer. 285 */ 286 Texture texture; 287 288 /** 289 * If set to true (by default), the layer can be reused. 290 */ 291 bool cacheable; 292 293 /** 294 * When set to true, this layer must be treated as a texture 295 * layer. 296 */ 297 bool textureLayer; 298 299 /** 300 * When set to true, this layer is dirty and should be cleared 301 * before any rendering occurs. 302 */ 303 bool dirty; 304 305 /** 306 * Indicates the render target. 307 */ 308 GLenum renderTarget; 309 310 /** 311 * Color filter used to draw this layer. Optional. 312 */ 313 SkiaColorFilter* colorFilter; 314 315 /** 316 * Opacity of the layer. 317 */ 318 int alpha; 319 /** 320 * Blending mode of the layer. 321 */ 322 SkXfermode::Mode mode; 323 324 /** 325 * Optional texture coordinates transform. 326 */ 327 mat4 texTransform; 328 329 /** 330 * Optional transform. 331 */ 332 mat4 transform; 333 334}; // struct Layer 335 336}; // namespace uirenderer 337}; // namespace android 338 339#endif // ANDROID_HWUI_LAYER_H 340