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 | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 46 | Snapshot(): invisible(false), flags(0), previous(NULL), layer(NULL), fbo(0) { |
| 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 | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 56 | height(s->height), |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 57 | invisible(s->invisible), |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 58 | flags(0), |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 59 | previous(s), |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 60 | layer(NULL), |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 61 | fbo(s->fbo), |
| 62 | viewport(s->viewport) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 63 | if (saveFlags & SkCanvas::kMatrix_SaveFlag) { |
| 64 | mTransformRoot.load(*s->transform); |
| 65 | transform = &mTransformRoot; |
| 66 | } else { |
| 67 | transform = s->transform; |
| 68 | } |
| 69 | |
| 70 | if (saveFlags & SkCanvas::kClip_SaveFlag) { |
| 71 | mClipRectRoot.set(*s->clipRect); |
| 72 | clipRect = &mClipRectRoot; |
| 73 | } else { |
| 74 | clipRect = s->clipRect; |
| 75 | } |
| 76 | |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 77 | if ((s->flags & Snapshot::kFlagClipSet) && |
| 78 | !(s->flags & Snapshot::kFlagDirtyLocalClip)) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 79 | mLocalClip.set(s->mLocalClip); |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 80 | } else { |
| 81 | flags |= Snapshot::kFlagDirtyLocalClip; |
| 82 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Various flags set on #flags. |
| 87 | */ |
| 88 | enum Flags { |
| 89 | /** |
| 90 | * Indicates that the clip region was modified. When this |
| 91 | * snapshot is restored so must the clip. |
| 92 | */ |
| 93 | kFlagClipSet = 0x1, |
| 94 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 95 | * Indicates that this snapshot was created when saving |
| 96 | * a new layer. |
| 97 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 98 | kFlagIsLayer = 0x2, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Indicates that this snapshot has changed the ortho matrix. |
| 101 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 102 | kFlagDirtyOrtho = 0x4, |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Indicates that the local clip should be recomputed. |
| 105 | */ |
| 106 | kFlagDirtyLocalClip = 0x8, |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /** |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 110 | * Intersects the current clip with the new clip rectangle. |
| 111 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 112 | bool clip(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 113 | bool clipped = false; |
| 114 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 115 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 116 | transform->mapRect(r); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 117 | |
| 118 | switch (op) { |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 119 | case SkRegion::kDifference_Op: |
| 120 | break; |
| 121 | case SkRegion::kIntersect_Op: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 122 | clipped = clipRect->intersect(r); |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 123 | break; |
| 124 | case SkRegion::kUnion_Op: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 125 | clipped = clipRect->unionWith(r); |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 126 | break; |
| 127 | case SkRegion::kXOR_Op: |
| 128 | break; |
| 129 | case SkRegion::kReverseDifference_Op: |
| 130 | break; |
| 131 | case SkRegion::kReplace_Op: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 132 | clipRect->set(r); |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 133 | clipped = true; |
| 134 | break; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 135 | } |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 136 | |
| 137 | if (clipped) { |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 138 | flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 141 | return clipped; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 145 | * Sets the current clip. |
| 146 | */ |
| 147 | void setClip(float left, float top, float right, float bottom) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 148 | clipRect->set(left, top, right, bottom); |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 149 | flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | const Rect& getLocalClip() { |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 153 | if (flags & Snapshot::kFlagDirtyLocalClip) { |
| 154 | mat4 inverse; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 155 | inverse.loadInverse(*transform); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 156 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 157 | mLocalClip.set(*clipRect); |
| 158 | inverse.mapRect(mLocalClip); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 159 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 160 | flags &= ~Snapshot::kFlagDirtyLocalClip; |
| 161 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 162 | return mLocalClip; |
| 163 | } |
| 164 | |
| 165 | // TODO: Temporary |
| 166 | void resetTransform(float x, float y, float z) { |
| 167 | transform = &mTransformRoot; |
| 168 | transform->loadTranslate(x, y, z); |
| 169 | } |
| 170 | |
| 171 | // TODO: Temporary |
| 172 | void resetClip(float left, float top, float right, float bottom) { |
| 173 | clipRect = &mClipRectRoot; |
| 174 | clipRect->set(left, top, right, bottom); |
| 175 | flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 179 | * Height of the framebuffer the snapshot is rendering into. |
| 180 | */ |
| 181 | int height; |
| 182 | |
| 183 | /** |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 184 | * If true, the layer won't be rendered. |
| 185 | */ |
| 186 | bool invisible; |
| 187 | |
| 188 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 189 | * Dirty flags. |
| 190 | */ |
| 191 | int flags; |
| 192 | |
| 193 | /** |
| 194 | * Previous snapshot. |
| 195 | */ |
| 196 | sp<Snapshot> previous; |
| 197 | |
| 198 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 199 | * Only set when the flag kFlagIsLayer is set. |
| 200 | */ |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 201 | Layer* layer; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 202 | GLuint fbo; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 203 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 204 | /** |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 205 | * Current viewport. |
| 206 | */ |
| 207 | Rect viewport; |
| 208 | |
| 209 | /** |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 210 | * Contains the previous ortho matrix. |
| 211 | */ |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 212 | mat4 orthoMatrix; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 213 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Local transformation. Holds the current translation, scale and |
| 216 | * rotation values. |
| 217 | */ |
| 218 | mat4* transform; |
| 219 | |
| 220 | /** |
| 221 | * Current clip region. The clip is stored in canvas-space coordinates, |
| 222 | * (screen-space coordinates in the regular case.) |
| 223 | */ |
| 224 | Rect* clipRect; |
| 225 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 226 | private: |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 227 | mat4 mTransformRoot; |
| 228 | Rect mClipRectRoot; |
| 229 | Rect mLocalClip; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 230 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 231 | }; // class Snapshot |
| 232 | |
| 233 | }; // namespace uirenderer |
| 234 | }; // namespace android |
| 235 | |
| 236 | #endif // ANDROID_UI_SNAPSHOT_H |