Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -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_SNAPSHOT_H |
| 18 | #define ANDROID_HWUI_SNAPSHOT_H |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | #include <GLES2/gl2ext.h> |
| 22 | |
| 23 | #include <utils/RefBase.h> |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 24 | #include <ui/Region.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 25 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 26 | #include <SkCanvas.h> |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 27 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 28 | #include "Layer.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 29 | #include "Matrix.h" |
| 30 | #include "Rect.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | |
| 35 | /** |
| 36 | * A snapshot holds information about the current state of the rendering |
| 37 | * surface. A snapshot is usually created whenever the user calls save() |
| 38 | * and discarded when the user calls restore(). Once a snapshot is created, |
| 39 | * it can hold information for deferred rendering. |
| 40 | * |
| 41 | * Each snapshot has a link to a previous snapshot, indicating the previous |
| 42 | * state of the renderer. |
| 43 | */ |
| 44 | class Snapshot: public LightRefBase<Snapshot> { |
| 45 | public: |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 46 | Snapshot(): flags(0), previous(NULL), layer(NULL), fbo(0), invisible(false), empty(false) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 47 | transform = &mTransformRoot; |
| 48 | clipRect = &mClipRectRoot; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 49 | region = NULL; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 50 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 53 | * Copies the specified snapshot/ The specified snapshot is stored as |
| 54 | * the previous snapshot. |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 55 | */ |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 56 | Snapshot(const sp<Snapshot>& s, int saveFlags): |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 57 | flags(0), previous(s), layer(NULL), fbo(s->fbo), |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 58 | invisible(s->invisible), empty(false), viewport(s->viewport), height(s->height) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 59 | if (saveFlags & SkCanvas::kMatrix_SaveFlag) { |
| 60 | mTransformRoot.load(*s->transform); |
| 61 | transform = &mTransformRoot; |
| 62 | } else { |
| 63 | transform = s->transform; |
| 64 | } |
| 65 | |
| 66 | if (saveFlags & SkCanvas::kClip_SaveFlag) { |
| 67 | mClipRectRoot.set(*s->clipRect); |
| 68 | clipRect = &mClipRectRoot; |
| 69 | } else { |
| 70 | clipRect = s->clipRect; |
| 71 | } |
| 72 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 73 | if (s->flags & Snapshot::kFlagFboTarget) { |
| 74 | flags |= Snapshot::kFlagFboTarget; |
| 75 | region = s->region; |
| 76 | } else { |
| 77 | region = NULL; |
| 78 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Various flags set on #flags. |
| 83 | */ |
| 84 | enum Flags { |
| 85 | /** |
| 86 | * Indicates that the clip region was modified. When this |
| 87 | * snapshot is restored so must the clip. |
| 88 | */ |
| 89 | kFlagClipSet = 0x1, |
| 90 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 91 | * Indicates that this snapshot was created when saving |
| 92 | * a new layer. |
| 93 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 94 | kFlagIsLayer = 0x2, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 95 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 96 | * Indicates that this snapshot is a special type of layer |
| 97 | * backed by an FBO. This flag only makes sense when the |
| 98 | * flag kFlagIsLayer is also set. |
| 99 | */ |
| 100 | kFlagIsFboLayer = 0x4, |
| 101 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 102 | * Indicates that this snapshot has changed the ortho matrix. |
| 103 | */ |
Romain Guy | ed7a8fc | 2011-10-04 19:21:27 -0700 | [diff] [blame] | 104 | kFlagDirtyOrtho = 0x8, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Indicates that this snapshot or an ancestor snapshot is |
| 107 | * an FBO layer. |
| 108 | */ |
Romain Guy | ed7a8fc | 2011-10-04 19:21:27 -0700 | [diff] [blame] | 109 | kFlagFboTarget = 0x10 |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /** |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 113 | * Modifies the current clip with the new clip rectangle and |
| 114 | * the specified operation. The specified rectangle is transformed |
| 115 | * by this snapshot's trasnformation. |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 116 | */ |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 117 | bool clip(float left, float top, float right, float bottom, |
| 118 | SkRegion::Op op = SkRegion::kIntersect_Op) { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 119 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 120 | transform->mapRect(r); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 121 | return clipTransformed(r, op); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Modifies the current clip with the new clip rectangle and |
| 126 | * the specified operation. The specified rectangle is considered |
| 127 | * already transformed. |
| 128 | */ |
| 129 | bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op) { |
| 130 | bool clipped = false; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 131 | |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 132 | // NOTE: The unimplemented operations require support for regions |
| 133 | // Supporting regions would require using a stencil buffer instead |
| 134 | // of the scissor. The stencil buffer itself is not too expensive |
| 135 | // (memory cost excluded) but on fillrate limited devices, managing |
| 136 | // the stencil might have a negative impact on the framerate. |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 137 | switch (op) { |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 138 | case SkRegion::kDifference_Op: |
| 139 | break; |
| 140 | case SkRegion::kIntersect_Op: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 141 | clipped = clipRect->intersect(r); |
Romain Guy | f28daff | 2011-02-04 00:59:34 -0800 | [diff] [blame] | 142 | if (!clipped) { |
| 143 | clipRect->setEmpty(); |
| 144 | clipped = true; |
| 145 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 146 | break; |
| 147 | case SkRegion::kUnion_Op: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 148 | clipped = clipRect->unionWith(r); |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 149 | break; |
| 150 | case SkRegion::kXOR_Op: |
| 151 | break; |
| 152 | case SkRegion::kReverseDifference_Op: |
| 153 | break; |
| 154 | case SkRegion::kReplace_Op: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 155 | clipRect->set(r); |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 156 | clipped = true; |
| 157 | break; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 158 | } |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 159 | |
| 160 | if (clipped) { |
Romain Guy | ed7a8fc | 2011-10-04 19:21:27 -0700 | [diff] [blame] | 161 | flags |= Snapshot::kFlagClipSet; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 164 | return clipped; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 168 | * Sets the current clip. |
| 169 | */ |
| 170 | void setClip(float left, float top, float right, float bottom) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 171 | clipRect->set(left, top, right, bottom); |
Romain Guy | ed7a8fc | 2011-10-04 19:21:27 -0700 | [diff] [blame] | 172 | flags |= Snapshot::kFlagClipSet; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | const Rect& getLocalClip() { |
Romain Guy | ed7a8fc | 2011-10-04 19:21:27 -0700 | [diff] [blame] | 176 | mat4 inverse; |
| 177 | inverse.loadInverse(*transform); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 178 | |
Romain Guy | ed7a8fc | 2011-10-04 19:21:27 -0700 | [diff] [blame] | 179 | mLocalClip.set(*clipRect); |
| 180 | inverse.mapRect(mLocalClip); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 181 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 182 | return mLocalClip; |
| 183 | } |
| 184 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 185 | void resetTransform(float x, float y, float z) { |
| 186 | transform = &mTransformRoot; |
| 187 | transform->loadTranslate(x, y, z); |
| 188 | } |
| 189 | |
| 190 | void resetClip(float left, float top, float right, float bottom) { |
| 191 | clipRect = &mClipRectRoot; |
| 192 | clipRect->set(left, top, right, bottom); |
Romain Guy | ed7a8fc | 2011-10-04 19:21:27 -0700 | [diff] [blame] | 193 | flags |= Snapshot::kFlagClipSet; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 196 | bool isIgnored() const { |
| 197 | return invisible || empty; |
| 198 | } |
| 199 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 200 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 201 | * Dirty flags. |
| 202 | */ |
| 203 | int flags; |
| 204 | |
| 205 | /** |
| 206 | * Previous snapshot. |
| 207 | */ |
| 208 | sp<Snapshot> previous; |
| 209 | |
| 210 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 211 | * Only set when the flag kFlagIsLayer is set. |
| 212 | */ |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 213 | Layer* layer; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 214 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 215 | /** |
Romain Guy | 421458a | 2011-11-21 15:14:37 -0800 | [diff] [blame^] | 216 | * Target FBO used for rendering. Set to 0 when rendering directly |
| 217 | * into the framebuffer. |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 218 | */ |
| 219 | GLuint fbo; |
| 220 | |
| 221 | /** |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 222 | * Indicates that this snapshot is invisible and nothing should be drawn |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 223 | * inside it. This flag is set only when the layer clips drawing to its |
| 224 | * bounds and is passed to subsequent snapshots. |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 225 | */ |
| 226 | bool invisible; |
| 227 | |
| 228 | /** |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 229 | * If set to true, the layer will not be composited. This is similar to |
| 230 | * invisible but this flag is not passed to subsequent snapshots. |
| 231 | */ |
| 232 | bool empty; |
| 233 | |
| 234 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 235 | * Current viewport. |
| 236 | */ |
| 237 | Rect viewport; |
| 238 | |
| 239 | /** |
| 240 | * Height of the framebuffer the snapshot is rendering into. |
| 241 | */ |
| 242 | int height; |
| 243 | |
| 244 | /** |
| 245 | * Contains the previous ortho matrix. |
| 246 | */ |
| 247 | mat4 orthoMatrix; |
| 248 | |
| 249 | /** |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 250 | * Local transformation. Holds the current translation, scale and |
| 251 | * rotation values. |
| 252 | */ |
| 253 | mat4* transform; |
| 254 | |
| 255 | /** |
| 256 | * Current clip region. The clip is stored in canvas-space coordinates, |
| 257 | * (screen-space coordinates in the regular case.) |
| 258 | */ |
| 259 | Rect* clipRect; |
| 260 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 261 | /** |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 262 | * The ancestor layer's dirty region. |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 263 | */ |
| 264 | Region* region; |
| 265 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 266 | private: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 267 | mat4 mTransformRoot; |
| 268 | Rect mClipRectRoot; |
| 269 | Rect mLocalClip; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 270 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 271 | }; // class Snapshot |
| 272 | |
| 273 | }; // namespace uirenderer |
| 274 | }; // namespace android |
| 275 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 276 | #endif // ANDROID_HWUI_SNAPSHOT_H |