blob: d8f926ef392577ff16ad8a501fc0899b2a458b6c [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:
Chris Craik7a896002016-02-19 15:51:02 -080047 static void* operator new(size_t size) = delete;
Chris Craikdeeda3d2014-05-05 19:09:33 -070048 static void* operator new(size_t size, LinearAllocator& allocator) {
John Reck7df9ff22016-02-10 16:08:08 -080049 return allocator.alloc<RoundRectClipState>(size);
Chris Craikdeeda3d2014-05-05 19:09:33 -070050 }
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 Craik678ff812016-03-01 13:27:54 -080066// TODO: remove for HWUI_NEW_OPS
Chris Craikfca52b752015-04-28 11:45:59 -070067class ProjectionPathMask {
68public:
Chris Craik7a896002016-02-19 15:51:02 -080069 static void* operator new(size_t size) = delete;
Chris Craikfca52b752015-04-28 11:45:59 -070070 static void* operator new(size_t size, LinearAllocator& allocator) {
John Reck7df9ff22016-02-10 16:08:08 -080071 return allocator.alloc<ProjectionPathMask>(size);
Chris Craikfca52b752015-04-28 11:45:59 -070072 }
73
74 const SkPath* projectionMask;
75 Matrix4 projectionMaskTransform;
76};
77
Chris Craikdeeda3d2014-05-05 19:09:33 -070078/**
Romain Guy5cbbce52010-06-27 22:59:20 -070079 * A snapshot holds information about the current state of the rendering
80 * surface. A snapshot is usually created whenever the user calls save()
81 * and discarded when the user calls restore(). Once a snapshot is created,
82 * it can hold information for deferred rendering.
83 *
84 * Each snapshot has a link to a previous snapshot, indicating the previous
85 * state of the renderer.
86 */
John Reckd9ee5502015-10-06 10:06:37 -070087class Snapshot {
Romain Guy5cbbce52010-06-27 22:59:20 -070088public:
Romain Guyada4d532012-02-02 17:31:16 -080089
90 Snapshot();
John Reckd9ee5502015-10-06 10:06:37 -070091 Snapshot(Snapshot* s, int saveFlags);
Romain Guy5cbbce52010-06-27 22:59:20 -070092
93 /**
Romain Guyada4d532012-02-02 17:31:16 -080094 * Various flags set on ::flags.
Romain Guy5cbbce52010-06-27 22:59:20 -070095 */
96 enum Flags {
97 /**
98 * Indicates that the clip region was modified. When this
99 * snapshot is restored so must the clip.
100 */
101 kFlagClipSet = 0x1,
102 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700103 * Indicates that this snapshot was created when saving
104 * a new layer.
105 */
Romain Guy079ba2c2010-07-16 14:12:24 -0700106 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -0700107 /**
Romain Guyeb993562010-10-05 18:14:38 -0700108 * Indicates that this snapshot is a special type of layer
109 * backed by an FBO. This flag only makes sense when the
110 * flag kFlagIsLayer is also set.
Chris Craika64a2be2014-05-14 14:17:01 -0700111 *
112 * Viewport has been modified to fit the new Fbo, and must be
113 * restored when this snapshot is restored.
Romain Guyeb993562010-10-05 18:14:38 -0700114 */
115 kFlagIsFboLayer = 0x4,
116 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700117 * Indicates that this snapshot or an ancestor snapshot is
118 * an FBO layer.
119 */
Chris Craikb87eadd2016-01-06 09:16:05 -0800120 kFlagFboTarget = 0x8, // TODO: remove for HWUI_NEW_OPS
Romain Guy5cbbce52010-06-27 22:59:20 -0700121 };
122
123 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700124 * Modifies the current clip with the new clip rectangle and
125 * the specified operation. The specified rectangle is transformed
126 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700127 */
Chris Craika2a70722015-12-17 12:58:24 -0800128 void clip(const Rect& localClip, SkRegion::Op op);
Romain Guyf607bdc2010-09-10 19:20:06 -0700129
130 /**
131 * Modifies the current clip with the new clip rectangle and
132 * the specified operation. The specified rectangle is considered
133 * already transformed.
134 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700135 void clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op);
Romain Guy5cbbce52010-06-27 22:59:20 -0700136
137 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800138 * Modifies the current clip with the specified region and operation.
139 * The specified region is considered already transformed.
140 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700141 void clipRegionTransformed(const SkRegion& region, SkRegion::Op op);
Romain Guy8ce00302013-01-15 18:51:42 -0800142
143 /**
Rob Tsuk487a92c2015-01-06 13:22:54 -0800144 * Modifies the current clip with the specified path and operation.
145 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700146 void clipPath(const SkPath& path, SkRegion::Op op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800147
148 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700149 * Sets the current clip.
150 */
Romain Guyada4d532012-02-02 17:31:16 -0800151 void setClip(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700152
Romain Guyada4d532012-02-02 17:31:16 -0800153 /**
154 * Returns the current clip in local coordinates. The clip rect is
155 * transformed by the inverse transform matrix.
156 */
Ben Cheng1a498682014-05-17 15:37:26 -0700157 ANDROID_API const Rect& getLocalClip();
Chris Craik3f0854292014-04-15 16:18:08 -0700158
159 /**
160 * Returns the current clip in render target coordinates.
161 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700162 const Rect& getRenderTargetClip() const { return mClipArea->getClipRect(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800163
164 /*
165 * Accessor functions so that the clip area can stay private
166 */
167 bool clipIsEmpty() const { return mClipArea->isEmpty(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800168 const SkRegion& getClipRegion() const { return mClipArea->getClipRegion(); }
169 bool clipIsSimple() const { return mClipArea->isSimple(); }
170 const ClipArea& getClipArea() const { return *mClipArea; }
Chris Craike4db79d2015-12-22 16:32:23 -0800171 ClipArea& mutateClipArea() { return *mClipArea; }
Romain Guy959c91f2010-08-11 19:35:53 -0700172
Chris Craik04d46eb2016-04-07 13:51:07 -0700173 WARN_UNUSED_RESULT const ClipBase* serializeIntersectedClip(LinearAllocator& allocator,
174 const ClipBase* recordedClip, const Matrix4& recordedClipTransform);
175 void applyClip(const ClipBase* clip, const Matrix4& transform);
176
Romain Guyada4d532012-02-02 17:31:16 -0800177 /**
178 * Resets the clip to the specified rect.
179 */
180 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700181
Romain Guyada4d532012-02-02 17:31:16 -0800182 /**
183 * Resets the current transform to a pure 3D translation.
184 */
185 void resetTransform(float x, float y, float z);
Romain Guy8aef54f2010-09-01 15:13:49 -0700186
Chris Craika64a2be2014-05-14 14:17:01 -0700187 void initializeViewport(int width, int height) {
188 mViewportData.initialize(width, height);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800189 mClipAreaRoot.setViewportDimensions(width, height);
Chris Craika64a2be2014-05-14 14:17:01 -0700190 }
191
192 int getViewportWidth() const { return mViewportData.mWidth; }
193 int getViewportHeight() const { return mViewportData.mHeight; }
194 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
195
Chris Craik69e5adf2014-08-14 13:34:01 -0700196 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
197 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
198
Romain Guyada4d532012-02-02 17:31:16 -0800199 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700200 * Sets (and replaces) the current clipping outline
Chris Craike83cbd42014-09-03 17:52:24 -0700201 *
202 * If the current round rect clip is high priority, the incoming clip is ignored.
Chris Craikdeeda3d2014-05-05 19:09:33 -0700203 */
Chris Craike83cbd42014-09-03 17:52:24 -0700204 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
205 float radius, bool highPriority);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700206
207 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700208 * Sets (and replaces) the current projection mask
209 */
210 void setProjectionPathMask(LinearAllocator& allocator, const SkPath* path);
211
212 /**
Romain Guyada4d532012-02-02 17:31:16 -0800213 * Indicates whether this snapshot should be ignored. A snapshot
Rob Tsuk487a92c2015-01-06 13:22:54 -0800214 * is typically ignored if its layer is invisible or empty.
Romain Guyada4d532012-02-02 17:31:16 -0800215 */
216 bool isIgnored() const;
Romain Guyaf636eb2010-12-09 17:47:21 -0800217
Romain Guy8b55f372010-08-18 17:10:07 -0700218 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700219 * Indicates whether the current transform has perspective components.
220 */
221 bool hasPerspectiveTransform() const;
222
223 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700224 * Fills outTransform with the current, total transform to screen space,
225 * across layer boundaries.
226 */
Chris Craik678ff812016-03-01 13:27:54 -0800227 // TODO: remove for HWUI_NEW_OPS
Chris Craikfca52b752015-04-28 11:45:59 -0700228 void buildScreenSpaceTransform(Matrix4* outTransform) const;
229
230 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700231 * Dirty flags.
232 */
233 int flags;
234
235 /**
236 * Previous snapshot.
237 */
John Reckd9ee5502015-10-06 10:06:37 -0700238 Snapshot* previous;
Romain Guy5cbbce52010-06-27 22:59:20 -0700239
240 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800241 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800242 *
243 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700244 */
Romain Guydda570202010-07-06 11:39:32 -0700245 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700246
Romain Guy8aef54f2010-09-01 15:13:49 -0700247 /**
Romain Guy421458a2011-11-21 15:14:37 -0800248 * Target FBO used for rendering. Set to 0 when rendering directly
249 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700250 */
251 GLuint fbo;
252
253 /**
Romain Guydbc26d22010-10-11 17:58:29 -0700254 * Indicates that this snapshot is invisible and nothing should be drawn
Romain Guyaf636eb2010-12-09 17:47:21 -0800255 * inside it. This flag is set only when the layer clips drawing to its
256 * bounds and is passed to subsequent snapshots.
Romain Guydbc26d22010-10-11 17:58:29 -0700257 */
258 bool invisible;
259
260 /**
Romain Guyaf636eb2010-12-09 17:47:21 -0800261 * If set to true, the layer will not be composited. This is similar to
262 * invisible but this flag is not passed to subsequent snapshots.
263 */
264 bool empty;
265
266 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700267 * Local transformation. Holds the current translation, scale and
268 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800269 *
270 * This is a reference to a matrix owned by this snapshot or another
271 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700272 */
273 mat4* transform;
274
275 /**
Romain Guyf219da52011-01-16 12:54:25 -0800276 * The ancestor layer's dirty region.
Romain Guyada4d532012-02-02 17:31:16 -0800277 *
278 * This is a reference to a region owned by a layer. This pointer must
279 * not be freed.
Romain Guy5b3b3522010-10-27 18:57:51 -0700280 */
281 Region* region;
282
Chet Haasedb8c9a62012-03-21 18:54:18 -0700283 /**
284 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
285 * has translucent rendering in a non-overlapping View. This value will be used by
286 * the renderer to set the alpha in the current color being used for ensuing drawing
287 * operations. The value is inherited by child snapshots because the same value should
Rob Tsuk487a92c2015-01-06 13:22:54 -0800288 * be applied to descendants of the current DisplayList (for example, a TextView contains
Chet Haasedb8c9a62012-03-21 18:54:18 -0700289 * the base alpha value which should be applied to the child DisplayLists used for drawing
290 * the actual text).
291 */
292 float alpha;
293
Chris Craikdeeda3d2014-05-05 19:09:33 -0700294 /**
295 * Current clipping round rect.
296 *
297 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
298 * never modified.
299 */
300 const RoundRectClipState* roundRectClipState;
301
Chris Craikfca52b752015-04-28 11:45:59 -0700302 /**
Chris Craik678ff812016-03-01 13:27:54 -0800303 * Current projection masking path - used exclusively to mask projected, tessellated circles.
Chris Craikfca52b752015-04-28 11:45:59 -0700304 */
Chris Craik678ff812016-03-01 13:27:54 -0800305#if HWUI_NEW_OPS
306 const SkPath* projectionPathMask;
307#else
Chris Craikfca52b752015-04-28 11:45:59 -0700308 const ProjectionPathMask* projectionPathMask;
Chris Craik678ff812016-03-01 13:27:54 -0800309#endif
Chris Craikfca52b752015-04-28 11:45:59 -0700310
Chris Craik5f803622013-03-21 14:39:04 -0700311 void dump() const;
312
Romain Guy5cbbce52010-06-27 22:59:20 -0700313private:
Chris Craika64a2be2014-05-14 14:17:01 -0700314 struct ViewportData {
Chris Craik92419752014-05-15 13:21:28 -0700315 ViewportData() : mWidth(0), mHeight(0) {}
Chris Craika64a2be2014-05-14 14:17:01 -0700316 void initialize(int width, int height) {
317 mWidth = width;
318 mHeight = height;
319 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
320 }
321
322 /*
323 * Width and height of current viewport.
324 *
325 * The viewport is always defined to be (0, 0, width, height).
326 */
327 int mWidth;
328 int mHeight;
329 /**
330 * Contains the current orthographic, projection matrix.
331 */
332 mat4 mOrthoMatrix;
333 };
334
Romain Guy8aef54f2010-09-01 15:13:49 -0700335 mat4 mTransformRoot;
Romain Guy079ba2c2010-07-16 14:12:24 -0700336
Rob Tsuk487a92c2015-01-06 13:22:54 -0800337 ClipArea mClipAreaRoot;
338 ClipArea* mClipArea;
339 Rect mLocalClip;
340
Chris Craika64a2be2014-05-14 14:17:01 -0700341 ViewportData mViewportData;
Chris Craik69e5adf2014-08-14 13:34:01 -0700342 Vector3 mRelativeLightCenter;
Romain Guy967e2bf2012-02-07 17:04:34 -0800343
Romain Guy5cbbce52010-06-27 22:59:20 -0700344}; // class Snapshot
345
346}; // namespace uirenderer
347}; // namespace android
348
Romain Guy5b3b3522010-10-27 18:57:51 -0700349#endif // ANDROID_HWUI_SNAPSHOT_H