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