blob: 4789b338d1731ab344742ad9a1722b167bc00b61 [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
Rob Tsuk487a92c2015-01-06 13:22:54 -080029#include "ClipArea.h"
Romain Guydda570202010-07-06 11:39:32 -070030#include "Layer.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070031#include "Matrix.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070032#include "Outline.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070033#include "Rect.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070034#include "utils/Macros.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070035
36namespace android {
37namespace uirenderer {
38
39/**
Chris Craikdeeda3d2014-05-05 19:09:33 -070040 * Temporary structure holding information for a single outline clip.
41 *
42 * These structures are treated as immutable once created, and only exist for a single frame, which
43 * is why they may only be allocated with a LinearAllocator.
44 */
45class RoundRectClipState {
46public:
47 /** static void* operator new(size_t size); PURPOSELY OMITTED, allocator only **/
48 static void* operator new(size_t size, LinearAllocator& allocator) {
49 return allocator.alloc(size);
50 }
51
52 bool areaRequiresRoundRectClip(const Rect& rect) const {
53 return rect.intersects(dangerRects[0])
54 || rect.intersects(dangerRects[1])
55 || rect.intersects(dangerRects[2])
56 || rect.intersects(dangerRects[3]);
57 }
58
Chris Craike83cbd42014-09-03 17:52:24 -070059 bool highPriority;
Chris Craikdeeda3d2014-05-05 19:09:33 -070060 Matrix4 matrix;
61 Rect dangerRects[4];
Chris Craikaf4d04c2014-07-29 12:50:14 -070062 Rect innerRect;
63 float radius;
Chris Craikdeeda3d2014-05-05 19:09:33 -070064};
65
Chris Craikfca52b752015-04-28 11:45:59 -070066class ProjectionPathMask {
67public:
68 /** static void* operator new(size_t size); PURPOSELY OMITTED, allocator only **/
69 static void* operator new(size_t size, LinearAllocator& allocator) {
70 return allocator.alloc(size);
71 }
72
73 const SkPath* projectionMask;
74 Matrix4 projectionMaskTransform;
75};
76
Chris Craikdeeda3d2014-05-05 19:09:33 -070077/**
Romain Guy5cbbce52010-06-27 22:59:20 -070078 * A snapshot holds information about the current state of the rendering
79 * surface. A snapshot is usually created whenever the user calls save()
80 * and discarded when the user calls restore(). Once a snapshot is created,
81 * it can hold information for deferred rendering.
82 *
83 * Each snapshot has a link to a previous snapshot, indicating the previous
84 * state of the renderer.
85 */
John Reckd9ee5502015-10-06 10:06:37 -070086class Snapshot {
Romain Guy5cbbce52010-06-27 22:59:20 -070087public:
Romain Guyada4d532012-02-02 17:31:16 -080088
89 Snapshot();
John Reckd9ee5502015-10-06 10:06:37 -070090 Snapshot(Snapshot* s, int saveFlags);
Romain Guy5cbbce52010-06-27 22:59:20 -070091
92 /**
Romain Guyada4d532012-02-02 17:31:16 -080093 * Various flags set on ::flags.
Romain Guy5cbbce52010-06-27 22:59:20 -070094 */
95 enum Flags {
96 /**
97 * Indicates that the clip region was modified. When this
98 * snapshot is restored so must the clip.
99 */
100 kFlagClipSet = 0x1,
101 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700102 * Indicates that this snapshot was created when saving
103 * a new layer.
104 */
Romain Guy079ba2c2010-07-16 14:12:24 -0700105 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -0700106 /**
Romain Guyeb993562010-10-05 18:14:38 -0700107 * Indicates that this snapshot is a special type of layer
108 * backed by an FBO. This flag only makes sense when the
109 * flag kFlagIsLayer is also set.
Chris Craika64a2be2014-05-14 14:17:01 -0700110 *
111 * Viewport has been modified to fit the new Fbo, and must be
112 * restored when this snapshot is restored.
Romain Guyeb993562010-10-05 18:14:38 -0700113 */
114 kFlagIsFboLayer = 0x4,
115 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700116 * Indicates that this snapshot or an ancestor snapshot is
117 * an FBO layer.
118 */
Chris Craika64a2be2014-05-14 14:17:01 -0700119 kFlagFboTarget = 0x8,
Romain Guy5cbbce52010-06-27 22:59:20 -0700120 };
121
122 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700123 * Modifies the current clip with the new clip rectangle and
124 * the specified operation. The specified rectangle is transformed
125 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700126 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700127 void clip(float left, float top, float right, float bottom, SkRegion::Op op);
Romain Guyf607bdc2010-09-10 19:20:06 -0700128
129 /**
130 * Modifies the current clip with the new clip rectangle and
131 * the specified operation. The specified rectangle is considered
132 * already transformed.
133 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700134 void clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op);
Romain Guy5cbbce52010-06-27 22:59:20 -0700135
136 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800137 * Modifies the current clip with the specified region and operation.
138 * The specified region is considered already transformed.
139 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700140 void clipRegionTransformed(const SkRegion& region, SkRegion::Op op);
Romain Guy8ce00302013-01-15 18:51:42 -0800141
142 /**
Rob Tsuk487a92c2015-01-06 13:22:54 -0800143 * Modifies the current clip with the specified path and operation.
144 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700145 void clipPath(const SkPath& path, SkRegion::Op op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800146
147 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700148 * Sets the current clip.
149 */
Romain Guyada4d532012-02-02 17:31:16 -0800150 void setClip(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700151
Romain Guyada4d532012-02-02 17:31:16 -0800152 /**
153 * Returns the current clip in local coordinates. The clip rect is
154 * transformed by the inverse transform matrix.
155 */
Ben Cheng1a498682014-05-17 15:37:26 -0700156 ANDROID_API const Rect& getLocalClip();
Chris Craik3f0854292014-04-15 16:18:08 -0700157
158 /**
159 * Returns the current clip in render target coordinates.
160 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700161 const Rect& getRenderTargetClip() const { return mClipArea->getClipRect(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800162
163 /*
164 * Accessor functions so that the clip area can stay private
165 */
166 bool clipIsEmpty() const { return mClipArea->isEmpty(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800167 const SkRegion& getClipRegion() const { return mClipArea->getClipRegion(); }
168 bool clipIsSimple() const { return mClipArea->isSimple(); }
169 const ClipArea& getClipArea() const { return *mClipArea; }
Romain Guy959c91f2010-08-11 19:35:53 -0700170
Romain Guyada4d532012-02-02 17:31:16 -0800171 /**
172 * Resets the clip to the specified rect.
173 */
174 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700175
Romain Guyada4d532012-02-02 17:31:16 -0800176 /**
177 * Resets the current transform to a pure 3D translation.
178 */
179 void resetTransform(float x, float y, float z);
Romain Guy8aef54f2010-09-01 15:13:49 -0700180
Chris Craika64a2be2014-05-14 14:17:01 -0700181 void initializeViewport(int width, int height) {
182 mViewportData.initialize(width, height);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800183 mClipAreaRoot.setViewportDimensions(width, height);
Chris Craika64a2be2014-05-14 14:17:01 -0700184 }
185
186 int getViewportWidth() const { return mViewportData.mWidth; }
187 int getViewportHeight() const { return mViewportData.mHeight; }
188 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
189
Chris Craik69e5adf2014-08-14 13:34:01 -0700190 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
191 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
192
Romain Guyada4d532012-02-02 17:31:16 -0800193 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700194 * Sets (and replaces) the current clipping outline
Chris Craike83cbd42014-09-03 17:52:24 -0700195 *
196 * If the current round rect clip is high priority, the incoming clip is ignored.
Chris Craikdeeda3d2014-05-05 19:09:33 -0700197 */
Chris Craike83cbd42014-09-03 17:52:24 -0700198 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
199 float radius, bool highPriority);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700200
201 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700202 * Sets (and replaces) the current projection mask
203 */
204 void setProjectionPathMask(LinearAllocator& allocator, const SkPath* path);
205
206 /**
Romain Guyada4d532012-02-02 17:31:16 -0800207 * Indicates whether this snapshot should be ignored. A snapshot
Rob Tsuk487a92c2015-01-06 13:22:54 -0800208 * is typically ignored if its layer is invisible or empty.
Romain Guyada4d532012-02-02 17:31:16 -0800209 */
210 bool isIgnored() const;
Romain Guyaf636eb2010-12-09 17:47:21 -0800211
Romain Guy8b55f372010-08-18 17:10:07 -0700212 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700213 * Indicates whether the current transform has perspective components.
214 */
215 bool hasPerspectiveTransform() const;
216
217 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700218 * Fills outTransform with the current, total transform to screen space,
219 * across layer boundaries.
220 */
221 void buildScreenSpaceTransform(Matrix4* outTransform) const;
222
223 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700224 * Dirty flags.
225 */
226 int flags;
227
228 /**
229 * Previous snapshot.
230 */
John Reckd9ee5502015-10-06 10:06:37 -0700231 Snapshot* previous;
Romain Guy5cbbce52010-06-27 22:59:20 -0700232
233 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800234 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800235 *
236 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700237 */
Romain Guydda570202010-07-06 11:39:32 -0700238 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700239
Romain Guy8aef54f2010-09-01 15:13:49 -0700240 /**
Romain Guy421458a2011-11-21 15:14:37 -0800241 * Target FBO used for rendering. Set to 0 when rendering directly
242 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700243 */
244 GLuint fbo;
245
246 /**
Romain Guydbc26d22010-10-11 17:58:29 -0700247 * Indicates that this snapshot is invisible and nothing should be drawn
Romain Guyaf636eb2010-12-09 17:47:21 -0800248 * inside it. This flag is set only when the layer clips drawing to its
249 * bounds and is passed to subsequent snapshots.
Romain Guydbc26d22010-10-11 17:58:29 -0700250 */
251 bool invisible;
252
253 /**
Romain Guyaf636eb2010-12-09 17:47:21 -0800254 * If set to true, the layer will not be composited. This is similar to
255 * invisible but this flag is not passed to subsequent snapshots.
256 */
257 bool empty;
258
259 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700260 * Local transformation. Holds the current translation, scale and
261 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800262 *
263 * This is a reference to a matrix owned by this snapshot or another
264 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700265 */
266 mat4* transform;
267
268 /**
Romain Guyf219da52011-01-16 12:54:25 -0800269 * The ancestor layer's dirty region.
Romain Guyada4d532012-02-02 17:31:16 -0800270 *
271 * This is a reference to a region owned by a layer. This pointer must
272 * not be freed.
Romain Guy5b3b3522010-10-27 18:57:51 -0700273 */
274 Region* region;
275
Chet Haasedb8c9a62012-03-21 18:54:18 -0700276 /**
277 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
278 * has translucent rendering in a non-overlapping View. This value will be used by
279 * the renderer to set the alpha in the current color being used for ensuing drawing
280 * operations. The value is inherited by child snapshots because the same value should
Rob Tsuk487a92c2015-01-06 13:22:54 -0800281 * be applied to descendants of the current DisplayList (for example, a TextView contains
Chet Haasedb8c9a62012-03-21 18:54:18 -0700282 * the base alpha value which should be applied to the child DisplayLists used for drawing
283 * the actual text).
284 */
285 float alpha;
286
Chris Craikdeeda3d2014-05-05 19:09:33 -0700287 /**
288 * Current clipping round rect.
289 *
290 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
291 * never modified.
292 */
293 const RoundRectClipState* roundRectClipState;
294
Chris Craikfca52b752015-04-28 11:45:59 -0700295 /**
296 * Current projection masking path - used exclusively to mask tessellated circles.
297 */
298 const ProjectionPathMask* projectionPathMask;
299
Chris Craik5f803622013-03-21 14:39:04 -0700300 void dump() const;
301
Romain Guy5cbbce52010-06-27 22:59:20 -0700302private:
Chris Craika64a2be2014-05-14 14:17:01 -0700303 struct ViewportData {
Chris Craik92419752014-05-15 13:21:28 -0700304 ViewportData() : mWidth(0), mHeight(0) {}
Chris Craika64a2be2014-05-14 14:17:01 -0700305 void initialize(int width, int height) {
306 mWidth = width;
307 mHeight = height;
308 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
309 }
310
311 /*
312 * Width and height of current viewport.
313 *
314 * The viewport is always defined to be (0, 0, width, height).
315 */
316 int mWidth;
317 int mHeight;
318 /**
319 * Contains the current orthographic, projection matrix.
320 */
321 mat4 mOrthoMatrix;
322 };
323
Romain Guy8aef54f2010-09-01 15:13:49 -0700324 mat4 mTransformRoot;
Romain Guy079ba2c2010-07-16 14:12:24 -0700325
Rob Tsuk487a92c2015-01-06 13:22:54 -0800326 ClipArea mClipAreaRoot;
327 ClipArea* mClipArea;
328 Rect mLocalClip;
329
Chris Craika64a2be2014-05-14 14:17:01 -0700330 ViewportData mViewportData;
Chris Craik69e5adf2014-08-14 13:34:01 -0700331 Vector3 mRelativeLightCenter;
Romain Guy967e2bf2012-02-07 17:04:34 -0800332
Romain Guy5cbbce52010-06-27 22:59:20 -0700333}; // class Snapshot
334
335}; // namespace uirenderer
336}; // namespace android
337
Romain Guy5b3b3522010-10-27 18:57:51 -0700338#endif // ANDROID_HWUI_SNAPSHOT_H