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