blob: 485dbf619280868ce6ec4052e6ac6fd8a7a7f823 [file] [log] [blame]
Romain Guy5cbbce52010-06-27 22:59:20 -07001/*
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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_SNAPSHOT_H
18#define ANDROID_HWUI_SNAPSHOT_H
Romain Guy5cbbce52010-06-27 22:59:20 -070019
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
Chris Craikdeeda3d2014-05-05 19:09:33 -070023#include <utils/LinearAllocator.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070024#include <utils/RefBase.h>
Romain Guy5b3b3522010-10-27 18:57:51 -070025#include <ui/Region.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guyada4d532012-02-02 17:31:16 -080027#include <SkRegion.h>
Romain Guy079ba2c2010-07-16 14:12:24 -070028
Romain Guydda57022010-07-06 11:39:32 -070029#include "Layer.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070030#include "Matrix.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070031#include "Outline.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070032#include "Rect.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070033#include "utils/Macros.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070034
35namespace android {
36namespace uirenderer {
37
38/**
Chris Craikdeeda3d2014-05-05 19:09:33 -070039 * 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 */
44class RoundRectClipState {
45public:
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
58 Matrix4 matrix;
59 Rect dangerRects[4];
60 Rect outlineInnerRect;
61 float outlineRadius;
62};
63
64/**
Romain Guy5cbbce52010-06-27 22:59:20 -070065 * A snapshot holds information about the current state of the rendering
66 * surface. A snapshot is usually created whenever the user calls save()
67 * and discarded when the user calls restore(). Once a snapshot is created,
68 * it can hold information for deferred rendering.
69 *
70 * Each snapshot has a link to a previous snapshot, indicating the previous
71 * state of the renderer.
72 */
73class Snapshot: public LightRefBase<Snapshot> {
74public:
Romain Guyada4d532012-02-02 17:31:16 -080075
76 Snapshot();
77 Snapshot(const sp<Snapshot>& s, int saveFlags);
Romain Guy5cbbce52010-06-27 22:59:20 -070078
79 /**
Romain Guyada4d532012-02-02 17:31:16 -080080 * Various flags set on ::flags.
Romain Guy5cbbce52010-06-27 22:59:20 -070081 */
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 Guy5cbbce52010-06-27 22:59:20 -070089 * Indicates that this snapshot was created when saving
90 * a new layer.
91 */
Romain Guy079ba2c2010-07-16 14:12:24 -070092 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070093 /**
Romain Guyeb993562010-10-05 18:14:38 -070094 * Indicates that this snapshot is a special type of layer
95 * backed by an FBO. This flag only makes sense when the
96 * flag kFlagIsLayer is also set.
Chris Craika64a2be2014-05-14 14:17:01 -070097 *
98 * Viewport has been modified to fit the new Fbo, and must be
99 * restored when this snapshot is restored.
Romain Guyeb993562010-10-05 18:14:38 -0700100 */
101 kFlagIsFboLayer = 0x4,
102 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700103 * Indicates that this snapshot or an ancestor snapshot is
104 * an FBO layer.
105 */
Chris Craika64a2be2014-05-14 14:17:01 -0700106 kFlagFboTarget = 0x8,
Romain Guy5cbbce52010-06-27 22:59:20 -0700107 };
108
109 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700110 * Modifies the current clip with the new clip rectangle and
111 * the specified operation. The specified rectangle is transformed
112 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700113 */
Romain Guyf607bdc2010-09-10 19:20:06 -0700114 bool clip(float left, float top, float right, float bottom,
Romain Guyada4d532012-02-02 17:31:16 -0800115 SkRegion::Op op = SkRegion::kIntersect_Op);
Romain Guyf607bdc2010-09-10 19:20:06 -0700116
117 /**
118 * Modifies the current clip with the new clip rectangle and
119 * the specified operation. The specified rectangle is considered
120 * already transformed.
121 */
Romain Guyada4d532012-02-02 17:31:16 -0800122 bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op);
Romain Guy5cbbce52010-06-27 22:59:20 -0700123
124 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800125 * Modifies the current clip with the specified region and operation.
126 * The specified region is considered already transformed.
127 */
128 bool clipRegionTransformed(const SkRegion& region, SkRegion::Op op);
129
130 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700131 * Sets the current clip.
132 */
Romain Guyada4d532012-02-02 17:31:16 -0800133 void setClip(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700134
Romain Guyada4d532012-02-02 17:31:16 -0800135 /**
136 * Returns the current clip in local coordinates. The clip rect is
137 * transformed by the inverse transform matrix.
138 */
Ben Cheng1a498682014-05-17 15:37:26 -0700139 ANDROID_API const Rect& getLocalClip();
Chris Craik3f0854292014-04-15 16:18:08 -0700140
141 /**
142 * Returns the current clip in render target coordinates.
143 */
144 const Rect& getRenderTargetClip() { return *clipRect; }
Romain Guy959c91f2010-08-11 19:35:53 -0700145
Romain Guyada4d532012-02-02 17:31:16 -0800146 /**
147 * Resets the clip to the specified rect.
148 */
149 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700150
Romain Guyada4d532012-02-02 17:31:16 -0800151 /**
152 * Resets the current transform to a pure 3D translation.
153 */
154 void resetTransform(float x, float y, float z);
Romain Guy8aef54f2010-09-01 15:13:49 -0700155
Chris Craika64a2be2014-05-14 14:17:01 -0700156 void initializeViewport(int width, int height) {
157 mViewportData.initialize(width, height);
158 }
159
160 int getViewportWidth() const { return mViewportData.mWidth; }
161 int getViewportHeight() const { return mViewportData.mHeight; }
162 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
163
Romain Guyada4d532012-02-02 17:31:16 -0800164 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700165 * Sets (and replaces) the current clipping outline
166 */
167 void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
168
169 /**
Romain Guyada4d532012-02-02 17:31:16 -0800170 * Indicates whether this snapshot should be ignored. A snapshot
171 * is typicalled ignored if its layer is invisible or empty.
172 */
173 bool isIgnored() const;
Romain Guyaf636eb2010-12-09 17:47:21 -0800174
Romain Guy8b55f372010-08-18 17:10:07 -0700175 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700176 * Indicates whether the current transform has perspective components.
177 */
178 bool hasPerspectiveTransform() const;
179
180 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700181 * Dirty flags.
182 */
183 int flags;
184
185 /**
186 * Previous snapshot.
187 */
188 sp<Snapshot> previous;
189
190 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800191 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800192 *
193 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700194 */
Romain Guydda57022010-07-06 11:39:32 -0700195 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700196
Romain Guy8aef54f2010-09-01 15:13:49 -0700197 /**
Romain Guy421458a2011-11-21 15:14:37 -0800198 * Target FBO used for rendering. Set to 0 when rendering directly
199 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700200 */
201 GLuint fbo;
202
203 /**
Romain Guydbc26d22010-10-11 17:58:29 -0700204 * Indicates that this snapshot is invisible and nothing should be drawn
Romain Guyaf636eb2010-12-09 17:47:21 -0800205 * inside it. This flag is set only when the layer clips drawing to its
206 * bounds and is passed to subsequent snapshots.
Romain Guydbc26d22010-10-11 17:58:29 -0700207 */
208 bool invisible;
209
210 /**
Romain Guyaf636eb2010-12-09 17:47:21 -0800211 * If set to true, the layer will not be composited. This is similar to
212 * invisible but this flag is not passed to subsequent snapshots.
213 */
214 bool empty;
215
216 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700217 * Local transformation. Holds the current translation, scale and
218 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800219 *
220 * This is a reference to a matrix owned by this snapshot or another
221 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700222 */
223 mat4* transform;
224
225 /**
Romain Guy967e2bf2012-02-07 17:04:34 -0800226 * Current clip rect. The clip is stored in canvas-space coordinates,
Romain Guy8aef54f2010-09-01 15:13:49 -0700227 * (screen-space coordinates in the regular case.)
Romain Guyada4d532012-02-02 17:31:16 -0800228 *
229 * This is a reference to a rect owned by this snapshot or another
230 * snapshot. This pointer must not be freed. See ::mClipRectRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700231 */
232 Rect* clipRect;
233
Romain Guy5b3b3522010-10-27 18:57:51 -0700234 /**
Romain Guy967e2bf2012-02-07 17:04:34 -0800235 * Current clip region. The clip is stored in canvas-space coordinates,
236 * (screen-space coordinates in the regular case.)
237 *
238 * This is a reference to a region owned by this snapshot or another
239 * snapshot. This pointer must not be freed. See ::mClipRegionRoot.
Romain Guy967e2bf2012-02-07 17:04:34 -0800240 */
Romain Guy0baaac52012-08-31 20:31:01 -0700241 SkRegion* clipRegion;
Romain Guy967e2bf2012-02-07 17:04:34 -0800242
243 /**
Romain Guyf219da52011-01-16 12:54:25 -0800244 * The ancestor layer's dirty region.
Romain Guyada4d532012-02-02 17:31:16 -0800245 *
246 * This is a reference to a region owned by a layer. This pointer must
247 * not be freed.
Romain Guy5b3b3522010-10-27 18:57:51 -0700248 */
249 Region* region;
250
Chet Haasedb8c9a62012-03-21 18:54:18 -0700251 /**
252 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
253 * has translucent rendering in a non-overlapping View. This value will be used by
254 * the renderer to set the alpha in the current color being used for ensuing drawing
255 * operations. The value is inherited by child snapshots because the same value should
256 * be applied to descendents of the current DisplayList (for example, a TextView contains
257 * the base alpha value which should be applied to the child DisplayLists used for drawing
258 * the actual text).
259 */
260 float alpha;
261
Chris Craikdeeda3d2014-05-05 19:09:33 -0700262 /**
263 * Current clipping round rect.
264 *
265 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
266 * never modified.
267 */
268 const RoundRectClipState* roundRectClipState;
269
Chris Craik5f803622013-03-21 14:39:04 -0700270 void dump() const;
271
Romain Guy5cbbce52010-06-27 22:59:20 -0700272private:
Chris Craika64a2be2014-05-14 14:17:01 -0700273 struct ViewportData {
274 ViewportData() : mWidth(0), mHeight() {}
275 void initialize(int width, int height) {
276 mWidth = width;
277 mHeight = height;
278 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
279 }
280
281 /*
282 * Width and height of current viewport.
283 *
284 * The viewport is always defined to be (0, 0, width, height).
285 */
286 int mWidth;
287 int mHeight;
288 /**
289 * Contains the current orthographic, projection matrix.
290 */
291 mat4 mOrthoMatrix;
292 };
293
Romain Guy967e2bf2012-02-07 17:04:34 -0800294 void ensureClipRegion();
295 void copyClipRectFromRegion();
296
Romain Guy0baaac52012-08-31 20:31:01 -0700297 bool clipRegionOp(float left, float top, float right, float bottom, SkRegion::Op op);
Romain Guy967e2bf2012-02-07 17:04:34 -0800298
Romain Guy8aef54f2010-09-01 15:13:49 -0700299 mat4 mTransformRoot;
300 Rect mClipRectRoot;
Chris Craik3f0854292014-04-15 16:18:08 -0700301 Rect mLocalClip; // don't use directly, call getLocalClip() which initializes this
Romain Guy079ba2c2010-07-16 14:12:24 -0700302
Romain Guy0baaac52012-08-31 20:31:01 -0700303 SkRegion mClipRegionRoot;
Chris Craika64a2be2014-05-14 14:17:01 -0700304 ViewportData mViewportData;
Romain Guy967e2bf2012-02-07 17:04:34 -0800305
Romain Guy5cbbce52010-06-27 22:59:20 -0700306}; // class Snapshot
307
308}; // namespace uirenderer
309}; // namespace android
310
Romain Guy5b3b3522010-10-27 18:57:51 -0700311#endif // ANDROID_HWUI_SNAPSHOT_H