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