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