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 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 23 | #include <utils/LinearAllocator.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 25 | #include <ui/Region.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 26 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 27 | #include <SkRegion.h> |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 28 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 29 | #include "Layer.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 30 | #include "Matrix.h" |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 31 | #include "Outline.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 32 | #include "Rect.h" |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 33 | #include "utils/Macros.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | |
| 38 | /** |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 39 | * Temporary structure holding information for a single outline clip. |
| 40 | * |
| 41 | * These structures are treated as immutable once created, and only exist for a single frame, which |
| 42 | * is why they may only be allocated with a LinearAllocator. |
| 43 | */ |
| 44 | class RoundRectClipState { |
| 45 | public: |
| 46 | /** static void* operator new(size_t size); PURPOSELY OMITTED, allocator only **/ |
| 47 | static void* operator new(size_t size, LinearAllocator& allocator) { |
| 48 | return allocator.alloc(size); |
| 49 | } |
| 50 | |
| 51 | bool areaRequiresRoundRectClip(const Rect& rect) const { |
| 52 | return rect.intersects(dangerRects[0]) |
| 53 | || rect.intersects(dangerRects[1]) |
| 54 | || rect.intersects(dangerRects[2]) |
| 55 | || rect.intersects(dangerRects[3]); |
| 56 | } |
| 57 | |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 58 | bool highPriority; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 59 | Matrix4 matrix; |
| 60 | Rect dangerRects[4]; |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 61 | Rect innerRect; |
| 62 | float radius; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 66 | * A snapshot holds information about the current state of the rendering |
| 67 | * surface. A snapshot is usually created whenever the user calls save() |
| 68 | * and discarded when the user calls restore(). Once a snapshot is created, |
| 69 | * it can hold information for deferred rendering. |
| 70 | * |
| 71 | * Each snapshot has a link to a previous snapshot, indicating the previous |
| 72 | * state of the renderer. |
| 73 | */ |
| 74 | class Snapshot: public LightRefBase<Snapshot> { |
| 75 | public: |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 76 | |
| 77 | Snapshot(); |
| 78 | Snapshot(const sp<Snapshot>& s, int saveFlags); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 81 | * Various flags set on ::flags. |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 82 | */ |
| 83 | enum Flags { |
| 84 | /** |
| 85 | * Indicates that the clip region was modified. When this |
| 86 | * snapshot is restored so must the clip. |
| 87 | */ |
| 88 | kFlagClipSet = 0x1, |
| 89 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 90 | * Indicates that this snapshot was created when saving |
| 91 | * a new layer. |
| 92 | */ |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 93 | kFlagIsLayer = 0x2, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 94 | /** |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 95 | * Indicates that this snapshot is a special type of layer |
| 96 | * backed by an FBO. This flag only makes sense when the |
| 97 | * flag kFlagIsLayer is also set. |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 98 | * |
| 99 | * Viewport has been modified to fit the new Fbo, and must be |
| 100 | * restored when this snapshot is restored. |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 101 | */ |
| 102 | kFlagIsFboLayer = 0x4, |
| 103 | /** |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 104 | * Indicates that this snapshot or an ancestor snapshot is |
| 105 | * an FBO layer. |
| 106 | */ |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 107 | kFlagFboTarget = 0x8, |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /** |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 111 | * Modifies the current clip with the new clip rectangle and |
| 112 | * the specified operation. The specified rectangle is transformed |
| 113 | * by this snapshot's trasnformation. |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 114 | */ |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 115 | bool clip(float left, float top, float right, float bottom, |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 116 | SkRegion::Op op = SkRegion::kIntersect_Op); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Modifies the current clip with the new clip rectangle and |
| 120 | * the specified operation. The specified rectangle is considered |
| 121 | * already transformed. |
| 122 | */ |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 123 | bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 126 | * Modifies the current clip with the specified region and operation. |
| 127 | * The specified region is considered already transformed. |
| 128 | */ |
| 129 | bool clipRegionTransformed(const SkRegion& region, SkRegion::Op op); |
| 130 | |
| 131 | /** |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 132 | * Sets the current clip. |
| 133 | */ |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 134 | void setClip(float left, float top, float right, float bottom); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 135 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 136 | /** |
| 137 | * Returns the current clip in local coordinates. The clip rect is |
| 138 | * transformed by the inverse transform matrix. |
| 139 | */ |
Ben Cheng | 1a49868 | 2014-05-17 15:37:26 -0700 | [diff] [blame] | 140 | ANDROID_API const Rect& getLocalClip(); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Returns the current clip in render target coordinates. |
| 144 | */ |
| 145 | const Rect& getRenderTargetClip() { return *clipRect; } |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 146 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 147 | /** |
| 148 | * Resets the clip to the specified rect. |
| 149 | */ |
| 150 | void resetClip(float left, float top, float right, float bottom); |
Romain Guy | 959c91f | 2010-08-11 19:35:53 -0700 | [diff] [blame] | 151 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 152 | /** |
| 153 | * Resets the current transform to a pure 3D translation. |
| 154 | */ |
| 155 | void resetTransform(float x, float y, float z); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 156 | |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 157 | void initializeViewport(int width, int height) { |
| 158 | mViewportData.initialize(width, height); |
| 159 | } |
| 160 | |
| 161 | int getViewportWidth() const { return mViewportData.mWidth; } |
| 162 | int getViewportHeight() const { return mViewportData.mHeight; } |
| 163 | const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; } |
| 164 | |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 165 | const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; } |
| 166 | void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; } |
| 167 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 168 | /** |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 169 | * Sets (and replaces) the current clipping outline |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 170 | * |
| 171 | * If the current round rect clip is high priority, the incoming clip is ignored. |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 172 | */ |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 173 | void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds, |
| 174 | float radius, bool highPriority); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 175 | |
| 176 | /** |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 177 | * Indicates whether this snapshot should be ignored. A snapshot |
| 178 | * is typicalled ignored if its layer is invisible or empty. |
| 179 | */ |
| 180 | bool isIgnored() const; |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 181 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 182 | /** |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 183 | * Indicates whether the current transform has perspective components. |
| 184 | */ |
| 185 | bool hasPerspectiveTransform() const; |
| 186 | |
| 187 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 188 | * Dirty flags. |
| 189 | */ |
| 190 | int flags; |
| 191 | |
| 192 | /** |
| 193 | * Previous snapshot. |
| 194 | */ |
| 195 | sp<Snapshot> previous; |
| 196 | |
| 197 | /** |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 198 | * A pointer to the currently active layer. |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 199 | * |
| 200 | * 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] | 201 | */ |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 202 | Layer* layer; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 203 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 204 | /** |
Romain Guy | 421458a | 2011-11-21 15:14:37 -0800 | [diff] [blame] | 205 | * Target FBO used for rendering. Set to 0 when rendering directly |
| 206 | * into the framebuffer. |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 207 | */ |
| 208 | GLuint fbo; |
| 209 | |
| 210 | /** |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 211 | * Indicates that this snapshot is invisible and nothing should be drawn |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 212 | * inside it. This flag is set only when the layer clips drawing to its |
| 213 | * bounds and is passed to subsequent snapshots. |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 214 | */ |
| 215 | bool invisible; |
| 216 | |
| 217 | /** |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 218 | * If set to true, the layer will not be composited. This is similar to |
| 219 | * invisible but this flag is not passed to subsequent snapshots. |
| 220 | */ |
| 221 | bool empty; |
| 222 | |
| 223 | /** |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 224 | * Local transformation. Holds the current translation, scale and |
| 225 | * rotation values. |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 226 | * |
| 227 | * This is a reference to a matrix owned by this snapshot or another |
| 228 | * snapshot. This pointer must not be freed. See ::mTransformRoot. |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 229 | */ |
| 230 | mat4* transform; |
| 231 | |
| 232 | /** |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 233 | * Current clip rect. The clip is stored in canvas-space coordinates, |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 234 | * (screen-space coordinates in the regular case.) |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 235 | * |
| 236 | * This is a reference to a rect owned by this snapshot or another |
| 237 | * snapshot. This pointer must not be freed. See ::mClipRectRoot. |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 238 | */ |
| 239 | Rect* clipRect; |
| 240 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 241 | /** |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 242 | * Current clip region. The clip is stored in canvas-space coordinates, |
| 243 | * (screen-space coordinates in the regular case.) |
| 244 | * |
| 245 | * This is a reference to a region owned by this snapshot or another |
| 246 | * snapshot. This pointer must not be freed. See ::mClipRegionRoot. |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 247 | */ |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 248 | SkRegion* clipRegion; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 249 | |
| 250 | /** |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 251 | * The ancestor layer's dirty region. |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 252 | * |
| 253 | * This is a reference to a region owned by a layer. This pointer must |
| 254 | * not be freed. |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 255 | */ |
| 256 | Region* region; |
| 257 | |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Current alpha value. This value is 1 by default, but may be set by a DisplayList which |
| 260 | * has translucent rendering in a non-overlapping View. This value will be used by |
| 261 | * the renderer to set the alpha in the current color being used for ensuing drawing |
| 262 | * operations. The value is inherited by child snapshots because the same value should |
| 263 | * be applied to descendents of the current DisplayList (for example, a TextView contains |
| 264 | * the base alpha value which should be applied to the child DisplayLists used for drawing |
| 265 | * the actual text). |
| 266 | */ |
| 267 | float alpha; |
| 268 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 269 | /** |
| 270 | * Current clipping round rect. |
| 271 | * |
| 272 | * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips, |
| 273 | * never modified. |
| 274 | */ |
| 275 | const RoundRectClipState* roundRectClipState; |
| 276 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 277 | void dump() const; |
| 278 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 279 | private: |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 280 | struct ViewportData { |
Chris Craik | 9241975 | 2014-05-15 13:21:28 -0700 | [diff] [blame] | 281 | ViewportData() : mWidth(0), mHeight(0) {} |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 282 | void initialize(int width, int height) { |
| 283 | mWidth = width; |
| 284 | mHeight = height; |
| 285 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Width and height of current viewport. |
| 290 | * |
| 291 | * The viewport is always defined to be (0, 0, width, height). |
| 292 | */ |
| 293 | int mWidth; |
| 294 | int mHeight; |
| 295 | /** |
| 296 | * Contains the current orthographic, projection matrix. |
| 297 | */ |
| 298 | mat4 mOrthoMatrix; |
| 299 | }; |
| 300 | |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 301 | void ensureClipRegion(); |
| 302 | void copyClipRectFromRegion(); |
| 303 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 304 | 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] | 305 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 306 | mat4 mTransformRoot; |
| 307 | Rect mClipRectRoot; |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 308 | Rect mLocalClip; // don't use directly, call getLocalClip() which initializes this |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 309 | |
Romain Guy | 0baaac5 | 2012-08-31 20:31:01 -0700 | [diff] [blame] | 310 | SkRegion mClipRegionRoot; |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 311 | ViewportData mViewportData; |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 312 | Vector3 mRelativeLightCenter; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 313 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 314 | }; // class Snapshot |
| 315 | |
| 316 | }; // namespace uirenderer |
| 317 | }; // namespace android |
| 318 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 319 | #endif // ANDROID_HWUI_SNAPSHOT_H |