blob: 3a01d049109cb1d78282a1f99c5ca6024e00fbaa [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 Guydda57022010-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
Romain Guyada4d532012-02-02 17:31:16 -0800173 /**
174 * Resets the clip to the specified rect.
175 */
176 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700177
Romain Guyada4d532012-02-02 17:31:16 -0800178 /**
179 * Resets the current transform to a pure 3D translation.
180 */
181 void resetTransform(float x, float y, float z);
Romain Guy8aef54f2010-09-01 15:13:49 -0700182
Chris Craika64a2be2014-05-14 14:17:01 -0700183 void initializeViewport(int width, int height) {
184 mViewportData.initialize(width, height);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800185 mClipAreaRoot.setViewportDimensions(width, height);
Chris Craika64a2be2014-05-14 14:17:01 -0700186 }
187
188 int getViewportWidth() const { return mViewportData.mWidth; }
189 int getViewportHeight() const { return mViewportData.mHeight; }
190 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
191
Chris Craik69e5adf2014-08-14 13:34:01 -0700192 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
193 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
194
Romain Guyada4d532012-02-02 17:31:16 -0800195 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700196 * Sets (and replaces) the current clipping outline
Chris Craike83cbd42014-09-03 17:52:24 -0700197 *
198 * If the current round rect clip is high priority, the incoming clip is ignored.
Chris Craikdeeda3d2014-05-05 19:09:33 -0700199 */
Chris Craike83cbd42014-09-03 17:52:24 -0700200 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
201 float radius, bool highPriority);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700202
203 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700204 * Sets (and replaces) the current projection mask
205 */
206 void setProjectionPathMask(LinearAllocator& allocator, const SkPath* path);
207
208 /**
Romain Guyada4d532012-02-02 17:31:16 -0800209 * Indicates whether this snapshot should be ignored. A snapshot
Rob Tsuk487a92c2015-01-06 13:22:54 -0800210 * is typically ignored if its layer is invisible or empty.
Romain Guyada4d532012-02-02 17:31:16 -0800211 */
212 bool isIgnored() const;
Romain Guyaf636eb2010-12-09 17:47:21 -0800213
Romain Guy8b55f372010-08-18 17:10:07 -0700214 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700215 * Indicates whether the current transform has perspective components.
216 */
217 bool hasPerspectiveTransform() const;
218
219 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700220 * Fills outTransform with the current, total transform to screen space,
221 * across layer boundaries.
222 */
Chris Craik678ff812016-03-01 13:27:54 -0800223 // TODO: remove for HWUI_NEW_OPS
Chris Craikfca52b752015-04-28 11:45:59 -0700224 void buildScreenSpaceTransform(Matrix4* outTransform) const;
225
226 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700227 * Dirty flags.
228 */
229 int flags;
230
231 /**
232 * Previous snapshot.
233 */
John Reckd9ee5502015-10-06 10:06:37 -0700234 Snapshot* previous;
Romain Guy5cbbce52010-06-27 22:59:20 -0700235
236 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800237 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800238 *
239 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700240 */
Romain Guydda57022010-07-06 11:39:32 -0700241 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700242
Romain Guy8aef54f2010-09-01 15:13:49 -0700243 /**
Romain Guy421458a2011-11-21 15:14:37 -0800244 * Target FBO used for rendering. Set to 0 when rendering directly
245 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700246 */
247 GLuint fbo;
248
249 /**
Romain Guydbc26d22010-10-11 17:58:29 -0700250 * Indicates that this snapshot is invisible and nothing should be drawn
Romain Guyaf636eb2010-12-09 17:47:21 -0800251 * inside it. This flag is set only when the layer clips drawing to its
252 * bounds and is passed to subsequent snapshots.
Romain Guydbc26d22010-10-11 17:58:29 -0700253 */
254 bool invisible;
255
256 /**
Romain Guyaf636eb2010-12-09 17:47:21 -0800257 * If set to true, the layer will not be composited. This is similar to
258 * invisible but this flag is not passed to subsequent snapshots.
259 */
260 bool empty;
261
262 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700263 * Local transformation. Holds the current translation, scale and
264 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800265 *
266 * This is a reference to a matrix owned by this snapshot or another
267 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700268 */
269 mat4* transform;
270
271 /**
Romain Guyf219da52011-01-16 12:54:25 -0800272 * The ancestor layer's dirty region.
Romain Guyada4d532012-02-02 17:31:16 -0800273 *
274 * This is a reference to a region owned by a layer. This pointer must
275 * not be freed.
Romain Guy5b3b3522010-10-27 18:57:51 -0700276 */
277 Region* region;
278
Chet Haasedb8c9a62012-03-21 18:54:18 -0700279 /**
280 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
281 * has translucent rendering in a non-overlapping View. This value will be used by
282 * the renderer to set the alpha in the current color being used for ensuing drawing
283 * operations. The value is inherited by child snapshots because the same value should
Rob Tsuk487a92c2015-01-06 13:22:54 -0800284 * be applied to descendants of the current DisplayList (for example, a TextView contains
Chet Haasedb8c9a62012-03-21 18:54:18 -0700285 * the base alpha value which should be applied to the child DisplayLists used for drawing
286 * the actual text).
287 */
288 float alpha;
289
Chris Craikdeeda3d2014-05-05 19:09:33 -0700290 /**
291 * Current clipping round rect.
292 *
293 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
294 * never modified.
295 */
296 const RoundRectClipState* roundRectClipState;
297
Chris Craikfca52b752015-04-28 11:45:59 -0700298 /**
Chris Craik678ff812016-03-01 13:27:54 -0800299 * Current projection masking path - used exclusively to mask projected, tessellated circles.
Chris Craikfca52b752015-04-28 11:45:59 -0700300 */
Chris Craik678ff812016-03-01 13:27:54 -0800301#if HWUI_NEW_OPS
302 const SkPath* projectionPathMask;
303#else
Chris Craikfca52b752015-04-28 11:45:59 -0700304 const ProjectionPathMask* projectionPathMask;
Chris Craik678ff812016-03-01 13:27:54 -0800305#endif
Chris Craikfca52b752015-04-28 11:45:59 -0700306
Chris Craik5f803622013-03-21 14:39:04 -0700307 void dump() const;
308
Romain Guy5cbbce52010-06-27 22:59:20 -0700309private:
Chris Craika64a2be2014-05-14 14:17:01 -0700310 struct ViewportData {
Chris Craik92419752014-05-15 13:21:28 -0700311 ViewportData() : mWidth(0), mHeight(0) {}
Chris Craika64a2be2014-05-14 14:17:01 -0700312 void initialize(int width, int height) {
313 mWidth = width;
314 mHeight = height;
315 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
316 }
317
318 /*
319 * Width and height of current viewport.
320 *
321 * The viewport is always defined to be (0, 0, width, height).
322 */
323 int mWidth;
324 int mHeight;
325 /**
326 * Contains the current orthographic, projection matrix.
327 */
328 mat4 mOrthoMatrix;
329 };
330
Romain Guy8aef54f2010-09-01 15:13:49 -0700331 mat4 mTransformRoot;
Romain Guy079ba2c2010-07-16 14:12:24 -0700332
Rob Tsuk487a92c2015-01-06 13:22:54 -0800333 ClipArea mClipAreaRoot;
334 ClipArea* mClipArea;
335 Rect mLocalClip;
336
Chris Craika64a2be2014-05-14 14:17:01 -0700337 ViewportData mViewportData;
Chris Craik69e5adf2014-08-14 13:34:01 -0700338 Vector3 mRelativeLightCenter;
Romain Guy967e2bf2012-02-07 17:04:34 -0800339
Romain Guy5cbbce52010-06-27 22:59:20 -0700340}; // class Snapshot
341
342}; // namespace uirenderer
343}; // namespace android
344
Romain Guy5b3b3522010-10-27 18:57:51 -0700345#endif // ANDROID_HWUI_SNAPSHOT_H