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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_SNAPSHOT_H |
| 18 | #define ANDROID_HWUI_SNAPSHOT_H |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | #include <GLES2/gl2ext.h> |
| 22 | |
| 23 | #include <utils/RefBase.h> |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 24 | #include <ui/Region.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 25 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 26 | #include <SkRegion.h> |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 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 | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 46 | |
| 47 | Snapshot(); |
| 48 | Snapshot(const sp<Snapshot>& s, int saveFlags); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 51 | * Various flags set on ::flags. |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 52 | */ |
| 53 | enum Flags { |
| 54 | /** |
| 55 | * Indicates that the clip region was modified. When this |
| 56 | * snapshot is restored so must the clip. |
| 57 | */ |
| 58 | kFlagClipSet = 0x1, |
| 59 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 60 | * Indicates that this snapshot was created when saving |
| 61 | * a new layer. |
| 62 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 63 | kFlagIsLayer = 0x2, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 64 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 65 | * Indicates that this snapshot is a special type of layer |
| 66 | * backed by an FBO. This flag only makes sense when the |
| 67 | * flag kFlagIsLayer is also set. |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 68 | * |
| 69 | * Viewport has been modified to fit the new Fbo, and must be |
| 70 | * restored when this snapshot is restored. |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 71 | */ |
| 72 | kFlagIsFboLayer = 0x4, |
| 73 | /** |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 74 | * Indicates that this snapshot or an ancestor snapshot is |
| 75 | * an FBO layer. |
| 76 | */ |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 77 | kFlagFboTarget = 0x8, |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /** |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 81 | * Modifies the current clip with the new clip rectangle and |
| 82 | * the specified operation. The specified rectangle is transformed |
| 83 | * by this snapshot's trasnformation. |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 84 | */ |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 85 | bool clip(float left, float top, float right, float bottom, |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 86 | SkRegion::Op op = SkRegion::kIntersect_Op); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Modifies the current clip with the new clip rectangle and |
| 90 | * the specified operation. The specified rectangle is considered |
| 91 | * already transformed. |
| 92 | */ |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 93 | bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 96 | * Modifies the current clip with the specified region and operation. |
| 97 | * The specified region is considered already transformed. |
| 98 | */ |
| 99 | bool clipRegionTransformed(const SkRegion& region, SkRegion::Op op); |
| 100 | |
| 101 | /** |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 102 | * Sets the current clip. |
| 103 | */ |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 104 | void setClip(float left, float top, float right, float bottom); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 105 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Returns the current clip in local coordinates. The clip rect is |
| 108 | * transformed by the inverse transform matrix. |
| 109 | */ |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 110 | const Rect& getLocalClip(); |
| 111 | |
| 112 | /** |
| 113 | * Returns the current clip in render target coordinates. |
| 114 | */ |
| 115 | const Rect& getRenderTargetClip() { return *clipRect; } |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 116 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Resets the clip to the specified rect. |
| 119 | */ |
| 120 | void resetClip(float left, float top, float right, float bottom); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 121 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Resets the current transform to a pure 3D translation. |
| 124 | */ |
| 125 | void resetTransform(float x, float y, float z); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 126 | |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 127 | void initializeViewport(int width, int height) { |
| 128 | mViewportData.initialize(width, height); |
| 129 | } |
| 130 | |
| 131 | int getViewportWidth() const { return mViewportData.mWidth; } |
| 132 | int getViewportHeight() const { return mViewportData.mHeight; } |
| 133 | const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; } |
| 134 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Indicates whether this snapshot should be ignored. A snapshot |
| 137 | * is typicalled ignored if its layer is invisible or empty. |
| 138 | */ |
| 139 | bool isIgnored() const; |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 140 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 141 | /** |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 142 | * Indicates whether the current transform has perspective components. |
| 143 | */ |
| 144 | bool hasPerspectiveTransform() const; |
| 145 | |
| 146 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 147 | * Dirty flags. |
| 148 | */ |
| 149 | int flags; |
| 150 | |
| 151 | /** |
| 152 | * Previous snapshot. |
| 153 | */ |
| 154 | sp<Snapshot> previous; |
| 155 | |
| 156 | /** |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 157 | * A pointer to the currently active layer. |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 158 | * |
| 159 | * This snapshot does not own the layer, this pointer must not be freed. |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 160 | */ |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 161 | Layer* layer; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 162 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 163 | /** |
Romain Guy | 421458a | 2011-11-21 15:14:37 -0800 | [diff] [blame] | 164 | * Target FBO used for rendering. Set to 0 when rendering directly |
| 165 | * into the framebuffer. |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 166 | */ |
| 167 | GLuint fbo; |
| 168 | |
| 169 | /** |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 170 | * Indicates that this snapshot is invisible and nothing should be drawn |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 171 | * inside it. This flag is set only when the layer clips drawing to its |
| 172 | * bounds and is passed to subsequent snapshots. |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 173 | */ |
| 174 | bool invisible; |
| 175 | |
| 176 | /** |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 177 | * If set to true, the layer will not be composited. This is similar to |
| 178 | * invisible but this flag is not passed to subsequent snapshots. |
| 179 | */ |
| 180 | bool empty; |
| 181 | |
| 182 | /** |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 183 | * Local transformation. Holds the current translation, scale and |
| 184 | * rotation values. |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 185 | * |
| 186 | * This is a reference to a matrix owned by this snapshot or another |
| 187 | * snapshot. This pointer must not be freed. See ::mTransformRoot. |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 188 | */ |
| 189 | mat4* transform; |
| 190 | |
| 191 | /** |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 192 | * Current clip rect. The clip is stored in canvas-space coordinates, |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 193 | * (screen-space coordinates in the regular case.) |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 194 | * |
| 195 | * This is a reference to a rect owned by this snapshot or another |
| 196 | * snapshot. This pointer must not be freed. See ::mClipRectRoot. |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 197 | */ |
| 198 | Rect* clipRect; |
| 199 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 200 | /** |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 201 | * Current clip region. The clip is stored in canvas-space coordinates, |
| 202 | * (screen-space coordinates in the regular case.) |
| 203 | * |
| 204 | * This is a reference to a region owned by this snapshot or another |
| 205 | * snapshot. This pointer must not be freed. See ::mClipRegionRoot. |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 206 | */ |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 207 | SkRegion* clipRegion; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 208 | |
| 209 | /** |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 210 | * The ancestor layer's dirty region. |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 211 | * |
| 212 | * This is a reference to a region owned by a layer. This pointer must |
| 213 | * not be freed. |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 214 | */ |
| 215 | Region* region; |
| 216 | |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Current alpha value. This value is 1 by default, but may be set by a DisplayList which |
| 219 | * has translucent rendering in a non-overlapping View. This value will be used by |
| 220 | * the renderer to set the alpha in the current color being used for ensuing drawing |
| 221 | * operations. The value is inherited by child snapshots because the same value should |
| 222 | * be applied to descendents of the current DisplayList (for example, a TextView contains |
| 223 | * the base alpha value which should be applied to the child DisplayLists used for drawing |
| 224 | * the actual text). |
| 225 | */ |
| 226 | float alpha; |
| 227 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 228 | void dump() const; |
| 229 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 230 | private: |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 231 | struct ViewportData { |
Chris Craik | 9241975 | 2014-05-15 13:21:28 -0700 | [diff] [blame^] | 232 | ViewportData() : mWidth(0), mHeight(0) {} |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 233 | void initialize(int width, int height) { |
| 234 | mWidth = width; |
| 235 | mHeight = height; |
| 236 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Width and height of current viewport. |
| 241 | * |
| 242 | * The viewport is always defined to be (0, 0, width, height). |
| 243 | */ |
| 244 | int mWidth; |
| 245 | int mHeight; |
| 246 | /** |
| 247 | * Contains the current orthographic, projection matrix. |
| 248 | */ |
| 249 | mat4 mOrthoMatrix; |
| 250 | }; |
| 251 | |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 252 | void ensureClipRegion(); |
| 253 | void copyClipRectFromRegion(); |
| 254 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 255 | bool clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 256 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 257 | mat4 mTransformRoot; |
| 258 | Rect mClipRectRoot; |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 259 | Rect mLocalClip; // don't use directly, call getLocalClip() which initializes this |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 260 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 261 | SkRegion mClipRegionRoot; |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 262 | ViewportData mViewportData; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 263 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 264 | }; // class Snapshot |
| 265 | |
| 266 | }; // namespace uirenderer |
| 267 | }; // namespace android |
| 268 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 269 | #endif // ANDROID_HWUI_SNAPSHOT_H |