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