blob: 4d704abe205a15704a1448faa840038656fa834b [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:
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
66/**
Romain Guy5cbbce52010-06-27 22:59:20 -070067 * A snapshot holds information about the current state of the rendering
68 * surface. A snapshot is usually created whenever the user calls save()
69 * and discarded when the user calls restore(). Once a snapshot is created,
70 * it can hold information for deferred rendering.
71 *
72 * Each snapshot has a link to a previous snapshot, indicating the previous
73 * state of the renderer.
74 */
75class Snapshot: public LightRefBase<Snapshot> {
76public:
Romain Guyada4d532012-02-02 17:31:16 -080077
78 Snapshot();
79 Snapshot(const sp<Snapshot>& s, int saveFlags);
Romain Guy5cbbce52010-06-27 22:59:20 -070080
81 /**
Romain Guyada4d532012-02-02 17:31:16 -080082 * Various flags set on ::flags.
Romain Guy5cbbce52010-06-27 22:59:20 -070083 */
84 enum Flags {
85 /**
86 * Indicates that the clip region was modified. When this
87 * snapshot is restored so must the clip.
88 */
89 kFlagClipSet = 0x1,
90 /**
Romain Guy5cbbce52010-06-27 22:59:20 -070091 * Indicates that this snapshot was created when saving
92 * a new layer.
93 */
Romain Guy079ba2c2010-07-16 14:12:24 -070094 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070095 /**
Romain Guyeb993562010-10-05 18:14:38 -070096 * Indicates that this snapshot is a special type of layer
97 * backed by an FBO. This flag only makes sense when the
98 * flag kFlagIsLayer is also set.
Chris Craika64a2be2014-05-14 14:17:01 -070099 *
100 * Viewport has been modified to fit the new Fbo, and must be
101 * restored when this snapshot is restored.
Romain Guyeb993562010-10-05 18:14:38 -0700102 */
103 kFlagIsFboLayer = 0x4,
104 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700105 * Indicates that this snapshot or an ancestor snapshot is
106 * an FBO layer.
107 */
Chris Craika64a2be2014-05-14 14:17:01 -0700108 kFlagFboTarget = 0x8,
Romain Guy5cbbce52010-06-27 22:59:20 -0700109 };
110
111 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700112 * Modifies the current clip with the new clip rectangle and
113 * the specified operation. The specified rectangle is transformed
114 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700115 */
Romain Guyf607bdc2010-09-10 19:20:06 -0700116 bool clip(float left, float top, float right, float bottom,
Romain Guyada4d532012-02-02 17:31:16 -0800117 SkRegion::Op op = SkRegion::kIntersect_Op);
Romain Guyf607bdc2010-09-10 19:20:06 -0700118
119 /**
120 * Modifies the current clip with the new clip rectangle and
121 * the specified operation. The specified rectangle is considered
122 * already transformed.
123 */
Romain Guyada4d532012-02-02 17:31:16 -0800124 bool clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op);
Romain Guy5cbbce52010-06-27 22:59:20 -0700125
126 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800127 * Modifies the current clip with the specified region and operation.
128 * The specified region is considered already transformed.
129 */
130 bool clipRegionTransformed(const SkRegion& region, SkRegion::Op op);
131
132 /**
Rob Tsuk487a92c2015-01-06 13:22:54 -0800133 * Modifies the current clip with the specified path and operation.
134 */
135 bool clipPath(const SkPath& path, SkRegion::Op op);
136
137 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700138 * Sets the current clip.
139 */
Romain Guyada4d532012-02-02 17:31:16 -0800140 void setClip(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700141
Romain Guyada4d532012-02-02 17:31:16 -0800142 /**
143 * Returns the current clip in local coordinates. The clip rect is
144 * transformed by the inverse transform matrix.
145 */
Ben Cheng1a498682014-05-17 15:37:26 -0700146 ANDROID_API const Rect& getLocalClip();
Chris Craik3f0854292014-04-15 16:18:08 -0700147
148 /**
149 * Returns the current clip in render target coordinates.
150 */
Rob Tsuk487a92c2015-01-06 13:22:54 -0800151 const Rect& getRenderTargetClip() { return mClipArea->getClipRect(); }
152
153 /*
154 * Accessor functions so that the clip area can stay private
155 */
156 bool clipIsEmpty() const { return mClipArea->isEmpty(); }
157 const Rect& getClipRect() const { return mClipArea->getClipRect(); }
158 const SkRegion& getClipRegion() const { return mClipArea->getClipRegion(); }
159 bool clipIsSimple() const { return mClipArea->isSimple(); }
160 const ClipArea& getClipArea() const { return *mClipArea; }
Romain Guy959c91f2010-08-11 19:35:53 -0700161
Romain Guyada4d532012-02-02 17:31:16 -0800162 /**
163 * Resets the clip to the specified rect.
164 */
165 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700166
Romain Guyada4d532012-02-02 17:31:16 -0800167 /**
168 * Resets the current transform to a pure 3D translation.
169 */
170 void resetTransform(float x, float y, float z);
Romain Guy8aef54f2010-09-01 15:13:49 -0700171
Chris Craika64a2be2014-05-14 14:17:01 -0700172 void initializeViewport(int width, int height) {
173 mViewportData.initialize(width, height);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800174 mClipAreaRoot.setViewportDimensions(width, height);
Chris Craika64a2be2014-05-14 14:17:01 -0700175 }
176
177 int getViewportWidth() const { return mViewportData.mWidth; }
178 int getViewportHeight() const { return mViewportData.mHeight; }
179 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
180
Chris Craik69e5adf2014-08-14 13:34:01 -0700181 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
182 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
183
Romain Guyada4d532012-02-02 17:31:16 -0800184 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700185 * Sets (and replaces) the current clipping outline
Chris Craike83cbd42014-09-03 17:52:24 -0700186 *
187 * If the current round rect clip is high priority, the incoming clip is ignored.
Chris Craikdeeda3d2014-05-05 19:09:33 -0700188 */
Chris Craike83cbd42014-09-03 17:52:24 -0700189 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
190 float radius, bool highPriority);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700191
192 /**
Romain Guyada4d532012-02-02 17:31:16 -0800193 * Indicates whether this snapshot should be ignored. A snapshot
Rob Tsuk487a92c2015-01-06 13:22:54 -0800194 * is typically ignored if its layer is invisible or empty.
Romain Guyada4d532012-02-02 17:31:16 -0800195 */
196 bool isIgnored() const;
Romain Guyaf636eb2010-12-09 17:47:21 -0800197
Romain Guy8b55f372010-08-18 17:10:07 -0700198 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700199 * Indicates whether the current transform has perspective components.
200 */
201 bool hasPerspectiveTransform() const;
202
203 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700204 * Dirty flags.
205 */
206 int flags;
207
208 /**
209 * Previous snapshot.
210 */
211 sp<Snapshot> previous;
212
213 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800214 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800215 *
216 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700217 */
Romain Guydda57022010-07-06 11:39:32 -0700218 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700219
Romain Guy8aef54f2010-09-01 15:13:49 -0700220 /**
Romain Guy421458a2011-11-21 15:14:37 -0800221 * Target FBO used for rendering. Set to 0 when rendering directly
222 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700223 */
224 GLuint fbo;
225
226 /**
Romain Guydbc26d22010-10-11 17:58:29 -0700227 * Indicates that this snapshot is invisible and nothing should be drawn
Romain Guyaf636eb2010-12-09 17:47:21 -0800228 * inside it. This flag is set only when the layer clips drawing to its
229 * bounds and is passed to subsequent snapshots.
Romain Guydbc26d22010-10-11 17:58:29 -0700230 */
231 bool invisible;
232
233 /**
Romain Guyaf636eb2010-12-09 17:47:21 -0800234 * If set to true, the layer will not be composited. This is similar to
235 * invisible but this flag is not passed to subsequent snapshots.
236 */
237 bool empty;
238
239 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700240 * Local transformation. Holds the current translation, scale and
241 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800242 *
243 * This is a reference to a matrix owned by this snapshot or another
244 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700245 */
246 mat4* transform;
247
248 /**
Romain Guyf219da52011-01-16 12:54:25 -0800249 * The ancestor layer's dirty region.
Romain Guyada4d532012-02-02 17:31:16 -0800250 *
251 * This is a reference to a region owned by a layer. This pointer must
252 * not be freed.
Romain Guy5b3b3522010-10-27 18:57:51 -0700253 */
254 Region* region;
255
Chet Haasedb8c9a62012-03-21 18:54:18 -0700256 /**
257 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
258 * has translucent rendering in a non-overlapping View. This value will be used by
259 * the renderer to set the alpha in the current color being used for ensuing drawing
260 * operations. The value is inherited by child snapshots because the same value should
Rob Tsuk487a92c2015-01-06 13:22:54 -0800261 * be applied to descendants of the current DisplayList (for example, a TextView contains
Chet Haasedb8c9a62012-03-21 18:54:18 -0700262 * the base alpha value which should be applied to the child DisplayLists used for drawing
263 * the actual text).
264 */
265 float alpha;
266
Chris Craikdeeda3d2014-05-05 19:09:33 -0700267 /**
268 * Current clipping round rect.
269 *
270 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
271 * never modified.
272 */
273 const RoundRectClipState* roundRectClipState;
274
Chris Craik5f803622013-03-21 14:39:04 -0700275 void dump() const;
276
Romain Guy5cbbce52010-06-27 22:59:20 -0700277private:
Chris Craika64a2be2014-05-14 14:17:01 -0700278 struct ViewportData {
Chris Craik92419752014-05-15 13:21:28 -0700279 ViewportData() : mWidth(0), mHeight(0) {}
Chris Craika64a2be2014-05-14 14:17:01 -0700280 void initialize(int width, int height) {
281 mWidth = width;
282 mHeight = height;
283 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
284 }
285
286 /*
287 * Width and height of current viewport.
288 *
289 * The viewport is always defined to be (0, 0, width, height).
290 */
291 int mWidth;
292 int mHeight;
293 /**
294 * Contains the current orthographic, projection matrix.
295 */
296 mat4 mOrthoMatrix;
297 };
298
Romain Guy8aef54f2010-09-01 15:13:49 -0700299 mat4 mTransformRoot;
Romain Guy079ba2c2010-07-16 14:12:24 -0700300
Rob Tsuk487a92c2015-01-06 13:22:54 -0800301 ClipArea mClipAreaRoot;
302 ClipArea* mClipArea;
303 Rect mLocalClip;
304
Chris Craika64a2be2014-05-14 14:17:01 -0700305 ViewportData mViewportData;
Chris Craik69e5adf2014-08-14 13:34:01 -0700306 Vector3 mRelativeLightCenter;
Romain Guy967e2bf2012-02-07 17:04:34 -0800307
Romain Guy5cbbce52010-06-27 22:59:20 -0700308}; // class Snapshot
309
310}; // namespace uirenderer
311}; // namespace android
312
Romain Guy5b3b3522010-10-27 18:57:51 -0700313#endif // ANDROID_HWUI_SNAPSHOT_H