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 | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 25 | #include <SkRegion.h> |
| 26 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 27 | #include "Layer.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 28 | #include "Matrix.h" |
| 29 | #include "Rect.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
| 34 | /** |
| 35 | * A snapshot holds information about the current state of the rendering |
| 36 | * surface. A snapshot is usually created whenever the user calls save() |
| 37 | * and discarded when the user calls restore(). Once a snapshot is created, |
| 38 | * it can hold information for deferred rendering. |
| 39 | * |
| 40 | * Each snapshot has a link to a previous snapshot, indicating the previous |
| 41 | * state of the renderer. |
| 42 | */ |
| 43 | class Snapshot: public LightRefBase<Snapshot> { |
| 44 | public: |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame^] | 45 | Snapshot(): invisible(false), flags(0), previous(NULL), layer(NULL), fbo(0) { } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Copies the specified snapshot. Only the transform and clip rectangle |
| 49 | * are copied. The layer information is set to 0 and the transform is |
| 50 | * assumed to be dirty. The specified snapshot is stored as the previous |
| 51 | * snapshot. |
| 52 | */ |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 53 | Snapshot(const sp<Snapshot>& s): |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 54 | height(s->height), |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 55 | transform(s->transform), |
| 56 | clipRect(s->clipRect), |
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 | dda5702 | 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 | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 63 | if ((s->flags & Snapshot::kFlagClipSet) && |
| 64 | !(s->flags & Snapshot::kFlagDirtyLocalClip)) { |
| 65 | localClip.set(s->localClip); |
| 66 | } else { |
| 67 | flags |= Snapshot::kFlagDirtyLocalClip; |
| 68 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Various flags set on #flags. |
| 73 | */ |
| 74 | enum Flags { |
| 75 | /** |
| 76 | * Indicates that the clip region was modified. When this |
| 77 | * snapshot is restored so must the clip. |
| 78 | */ |
| 79 | kFlagClipSet = 0x1, |
| 80 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 81 | * Indicates that this snapshot was created when saving |
| 82 | * a new layer. |
| 83 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 84 | kFlagIsLayer = 0x2, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Indicates that this snapshot has changed the ortho matrix. |
| 87 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 88 | kFlagDirtyOrtho = 0x4, |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Indicates that the local clip should be recomputed. |
| 91 | */ |
| 92 | kFlagDirtyLocalClip = 0x8, |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /** |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 96 | * Intersects the current clip with the new clip rectangle. |
| 97 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 98 | bool clip(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 99 | bool clipped = false; |
| 100 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 101 | Rect r(left, top, right, bottom); |
| 102 | transform.mapRect(r); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 103 | |
| 104 | switch (op) { |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 105 | case SkRegion::kDifference_Op: |
| 106 | break; |
| 107 | case SkRegion::kIntersect_Op: |
| 108 | clipped = clipRect.intersect(r); |
| 109 | break; |
| 110 | case SkRegion::kUnion_Op: |
| 111 | clipped = clipRect.unionWith(r); |
| 112 | break; |
| 113 | case SkRegion::kXOR_Op: |
| 114 | break; |
| 115 | case SkRegion::kReverseDifference_Op: |
| 116 | break; |
| 117 | case SkRegion::kReplace_Op: |
| 118 | clipRect.set(r); |
| 119 | clipped = true; |
| 120 | break; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 121 | } |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 122 | |
| 123 | if (clipped) { |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 124 | flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 127 | return clipped; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 131 | * Sets the current clip. |
| 132 | */ |
| 133 | void setClip(float left, float top, float right, float bottom) { |
| 134 | clipRect.set(left, top, right, bottom); |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 135 | flags |= Snapshot::kFlagClipSet | Snapshot::kFlagDirtyLocalClip; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | const Rect& getLocalClip() { |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 139 | if (flags & Snapshot::kFlagDirtyLocalClip) { |
| 140 | mat4 inverse; |
| 141 | inverse.loadInverse(transform); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 142 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 143 | localClip.set(clipRect); |
| 144 | inverse.mapRect(localClip); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 145 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 146 | flags &= ~Snapshot::kFlagDirtyLocalClip; |
| 147 | } |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 148 | return localClip; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 152 | * Height of the framebuffer the snapshot is rendering into. |
| 153 | */ |
| 154 | int height; |
| 155 | |
| 156 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 157 | * Local transformation. Holds the current translation, scale and |
| 158 | * rotation values. |
| 159 | */ |
| 160 | mat4 transform; |
| 161 | |
| 162 | /** |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 163 | * Current clip region. The clip is stored in canvas-space coordinates, |
| 164 | * (screen-space coordinates in the regular case.) |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 165 | */ |
| 166 | Rect clipRect; |
| 167 | |
| 168 | /** |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame^] | 169 | * If true, the layer won't be rendered. |
| 170 | */ |
| 171 | bool invisible; |
| 172 | |
| 173 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 174 | * Dirty flags. |
| 175 | */ |
| 176 | int flags; |
| 177 | |
| 178 | /** |
| 179 | * Previous snapshot. |
| 180 | */ |
| 181 | sp<Snapshot> previous; |
| 182 | |
| 183 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 184 | * Only set when the flag kFlagIsLayer is set. |
| 185 | */ |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 186 | Layer* layer; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 187 | GLuint fbo; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 188 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 189 | /** |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 190 | * Current viewport. |
| 191 | */ |
| 192 | Rect viewport; |
| 193 | |
| 194 | /** |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 195 | * Contains the previous ortho matrix. |
| 196 | */ |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 197 | mat4 orthoMatrix; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 198 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 199 | private: |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 200 | Rect localClip; |
| 201 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 202 | }; // class Snapshot |
| 203 | |
| 204 | }; // namespace uirenderer |
| 205 | }; // namespace android |
| 206 | |
| 207 | #endif // ANDROID_UI_SNAPSHOT_H |