Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_LAYER_H |
| 18 | #define ANDROID_HWUI_LAYER_H |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 19 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 22 | #include <GLES2/gl2.h> |
| 23 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 24 | #include <ui/Region.h> |
| 25 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 26 | #include <SkXfermode.h> |
| 27 | |
| 28 | #include "Rect.h" |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 29 | #include "SkiaColorFilter.h" |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 30 | #include "Texture.h" |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 31 | #include "Vertex.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | // Layers |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 39 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 40 | // Forward declarations |
| 41 | class OpenGLRenderer; |
| 42 | class DisplayList; |
| 43 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 44 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 45 | * A layer has dimensions and is backed by an OpenGL texture or FBO. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 46 | */ |
| 47 | struct Layer { |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 48 | Layer(const uint32_t layerWidth, const uint32_t layerHeight); |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 49 | ~Layer(); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 50 | |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 51 | void freeResourcesLocked(); |
| 52 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 53 | /** |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 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 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 62 | const float texX = 1.0f / float(texture.width); |
| 63 | const float texY = 1.0f / float(texture.height); |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 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); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 68 | |
| 69 | regionRect.translate(layer.left, layer.top); |
| 70 | } |
| 71 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 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 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 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 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 94 | ANDROID_API void setPaint(SkPaint* paint); |
| 95 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 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* getTexturePointer() { |
| 138 | return &texture.id; |
| 139 | } |
| 140 | |
| 141 | inline GLuint getTexture() { |
| 142 | return texture.id; |
| 143 | } |
| 144 | |
| 145 | inline GLenum getRenderTarget() { |
| 146 | return renderTarget; |
| 147 | } |
| 148 | |
| 149 | inline void setRenderTarget(GLenum renderTarget) { |
| 150 | this->renderTarget = renderTarget; |
| 151 | } |
| 152 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 153 | void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) { |
| 154 | texture.setWrap(wrap, bindTexture, force, renderTarget); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 157 | void setFilter(GLenum filter, bool bindTexture = false, bool force = false) { |
| 158 | texture.setFilter(filter, bindTexture, force, renderTarget); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | inline bool isCacheable() { |
| 162 | return cacheable; |
| 163 | } |
| 164 | |
| 165 | inline void setCacheable(bool cacheable) { |
| 166 | this->cacheable = cacheable; |
| 167 | } |
| 168 | |
| 169 | inline bool isTextureLayer() { |
| 170 | return textureLayer; |
| 171 | } |
| 172 | |
| 173 | inline void setTextureLayer(bool textureLayer) { |
| 174 | this->textureLayer = textureLayer; |
| 175 | } |
| 176 | |
| 177 | inline SkiaColorFilter* getColorFilter() { |
| 178 | return colorFilter; |
| 179 | } |
| 180 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 181 | ANDROID_API void setColorFilter(SkiaColorFilter* filter); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 182 | |
| 183 | inline void bindTexture() { |
| 184 | glBindTexture(renderTarget, texture.id); |
| 185 | } |
| 186 | |
| 187 | inline void generateTexture() { |
| 188 | glGenTextures(1, &texture.id); |
| 189 | } |
| 190 | |
| 191 | inline void deleteTexture() { |
| 192 | if (texture.id) glDeleteTextures(1, &texture.id); |
| 193 | } |
| 194 | |
Romain Guy | 912a7b3 | 2011-07-26 18:57:28 -0700 | [diff] [blame] | 195 | inline void deleteFbo() { |
| 196 | if (fbo) glDeleteFramebuffers(1, &fbo); |
| 197 | } |
| 198 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 199 | inline void allocateTexture(GLenum format, GLenum storage) { |
| 200 | glTexImage2D(renderTarget, 0, format, getWidth(), getHeight(), 0, format, storage, NULL); |
| 201 | } |
| 202 | |
| 203 | inline mat4& getTexTransform() { |
| 204 | return texTransform; |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 207 | inline mat4& getTransform() { |
| 208 | return transform; |
| 209 | } |
| 210 | |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 211 | /** |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 212 | * Bounds of the layer. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 213 | */ |
| 214 | Rect layer; |
| 215 | /** |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 216 | * Texture coordinates of the layer. |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 217 | */ |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 218 | Rect texCoords; |
| 219 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 220 | /** |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 221 | * Dirty region indicating what parts of the layer |
| 222 | * have been drawn. |
| 223 | */ |
| 224 | Region region; |
Romain Guy | 4066767 | 2011-03-18 14:34:03 -0700 | [diff] [blame] | 225 | /** |
| 226 | * If the region is a rectangle, coordinates of the |
| 227 | * region are stored here. |
| 228 | */ |
| 229 | Rect regionRect; |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 230 | |
| 231 | /** |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 232 | * If the layer can be rendered as a mesh, this is non-null. |
| 233 | */ |
| 234 | TextureVertex* mesh; |
| 235 | uint16_t* meshIndices; |
| 236 | GLsizei meshElementCount; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 237 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 238 | /** |
| 239 | * Used for deferred updates. |
| 240 | */ |
| 241 | bool deferredUpdateScheduled; |
| 242 | OpenGLRenderer* renderer; |
| 243 | DisplayList* displayList; |
| 244 | Rect dirtyRect; |
| 245 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 246 | private: |
| 247 | /** |
| 248 | * Name of the FBO used to render the layer. If the name is 0 |
| 249 | * this layer is not backed by an FBO, but a simple texture. |
| 250 | */ |
| 251 | GLuint fbo; |
| 252 | |
| 253 | /** |
| 254 | * Indicates whether this layer has been used already. |
| 255 | */ |
| 256 | bool empty; |
| 257 | |
| 258 | /** |
| 259 | * The texture backing this layer. |
| 260 | */ |
| 261 | Texture texture; |
| 262 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 263 | /** |
| 264 | * If set to true (by default), the layer can be reused. |
| 265 | */ |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 266 | bool cacheable; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 267 | |
| 268 | /** |
| 269 | * When set to true, this layer must be treated as a texture |
| 270 | * layer. |
| 271 | */ |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 272 | bool textureLayer; |
| 273 | |
| 274 | /** |
| 275 | * Indicates the render target. |
| 276 | */ |
| 277 | GLenum renderTarget; |
| 278 | |
| 279 | /** |
| 280 | * Color filter used to draw this layer. Optional. |
| 281 | */ |
| 282 | SkiaColorFilter* colorFilter; |
| 283 | |
| 284 | /** |
| 285 | * Opacity of the layer. |
| 286 | */ |
| 287 | int alpha; |
| 288 | /** |
| 289 | * Blending mode of the layer. |
| 290 | */ |
| 291 | SkXfermode::Mode mode; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * Optional texture coordinates transform. |
| 295 | */ |
| 296 | mat4 texTransform; |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 297 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Optional transform. |
| 300 | */ |
| 301 | mat4 transform; |
| 302 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 303 | }; // struct Layer |
| 304 | |
| 305 | }; // namespace uirenderer |
| 306 | }; // namespace android |
| 307 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 308 | #endif // ANDROID_HWUI_LAYER_H |