blob: 287e58af0f69128640975d38e37f269d4c287a11 [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
Romain Guy5cbbce52010-06-27 22:59:20 -070018
19#include <GLES2/gl2.h>
20#include <GLES2/gl2ext.h>
21
Chris Craikdeeda3d2014-05-05 19:09:33 -070022#include <utils/LinearAllocator.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <utils/RefBase.h>
Romain Guy5b3b3522010-10-27 18:57:51 -070024#include <ui/Region.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Mike Reed6e49c9f2016-12-02 15:36:59 -050026#include <SkClipOp.h>
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
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 */
John Reckd9ee5502015-10-06 10:06:37 -070075class Snapshot {
Romain Guy5cbbce52010-06-27 22:59:20 -070076public:
Romain Guyada4d532012-02-02 17:31:16 -080077
78 Snapshot();
John Reckd9ee5502015-10-06 10:06:37 -070079 Snapshot(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,
Romain Guy5cbbce52010-06-27 22:59:20 -0700104 };
105
106 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700107 * Modifies the current clip with the new clip rectangle and
108 * the specified operation. The specified rectangle is transformed
109 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700110 */
Mike Reed6e49c9f2016-12-02 15:36:59 -0500111 void clip(const Rect& localClip, SkClipOp op);
Romain Guyf607bdc2010-09-10 19:20:06 -0700112
113 /**
114 * Modifies the current clip with the new clip rectangle and
115 * the specified operation. The specified rectangle is considered
116 * already transformed.
117 */
Mike Reed6e49c9f2016-12-02 15:36:59 -0500118 void clipTransformed(const Rect& r, SkClipOp op = kIntersect_SkClipOp);
Romain Guy5cbbce52010-06-27 22:59:20 -0700119
120 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800121 * Modifies the current clip with the specified region and operation.
122 * The specified region is considered already transformed.
123 */
Mike Reed6e49c9f2016-12-02 15:36:59 -0500124 void clipRegionTransformed(const SkRegion& region, SkClipOp op);
Romain Guy8ce00302013-01-15 18:51:42 -0800125
126 /**
Rob Tsuk487a92c2015-01-06 13:22:54 -0800127 * Modifies the current clip with the specified path and operation.
128 */
Mike Reed6e49c9f2016-12-02 15:36:59 -0500129 void clipPath(const SkPath& path, SkClipOp op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800130
131 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700132 * Sets the current clip.
133 */
Romain Guyada4d532012-02-02 17:31:16 -0800134 void setClip(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700135
Romain Guyada4d532012-02-02 17:31:16 -0800136 /**
137 * Returns the current clip in local coordinates. The clip rect is
138 * transformed by the inverse transform matrix.
139 */
Ben Cheng1a498682014-05-17 15:37:26 -0700140 ANDROID_API const Rect& getLocalClip();
Chris Craik3f0854292014-04-15 16:18:08 -0700141
142 /**
143 * Returns the current clip in render target coordinates.
144 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700145 const Rect& getRenderTargetClip() const { return mClipArea->getClipRect(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800146
147 /*
148 * Accessor functions so that the clip area can stay private
149 */
150 bool clipIsEmpty() const { return mClipArea->isEmpty(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800151 const SkRegion& getClipRegion() const { return mClipArea->getClipRegion(); }
152 bool clipIsSimple() const { return mClipArea->isSimple(); }
153 const ClipArea& getClipArea() const { return *mClipArea; }
Chris Craike4db79d2015-12-22 16:32:23 -0800154 ClipArea& mutateClipArea() { return *mClipArea; }
Romain Guy959c91f2010-08-11 19:35:53 -0700155
Chris Craik04d46eb2016-04-07 13:51:07 -0700156 WARN_UNUSED_RESULT const ClipBase* serializeIntersectedClip(LinearAllocator& allocator,
157 const ClipBase* recordedClip, const Matrix4& recordedClipTransform);
158 void applyClip(const ClipBase* clip, const Matrix4& transform);
159
Romain Guyada4d532012-02-02 17:31:16 -0800160 /**
161 * Resets the clip to the specified rect.
162 */
163 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700164
Chris Craika64a2be2014-05-14 14:17:01 -0700165 void initializeViewport(int width, int height) {
166 mViewportData.initialize(width, height);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800167 mClipAreaRoot.setViewportDimensions(width, height);
Chris Craika64a2be2014-05-14 14:17:01 -0700168 }
169
170 int getViewportWidth() const { return mViewportData.mWidth; }
171 int getViewportHeight() const { return mViewportData.mHeight; }
172 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
173
Chris Craik69e5adf2014-08-14 13:34:01 -0700174 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
175 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
176
Romain Guyada4d532012-02-02 17:31:16 -0800177 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700178 * Sets (and replaces) the current clipping outline
Chris Craike83cbd42014-09-03 17:52:24 -0700179 *
180 * If the current round rect clip is high priority, the incoming clip is ignored.
Chris Craikdeeda3d2014-05-05 19:09:33 -0700181 */
Chris Craike83cbd42014-09-03 17:52:24 -0700182 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
183 float radius, bool highPriority);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700184
185 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700186 * Sets (and replaces) the current projection mask
187 */
Chris Craik5e00c7c2016-07-06 16:10:09 -0700188 void setProjectionPathMask(const SkPath* path);
Romain Guyaf636eb2010-12-09 17:47:21 -0800189
Romain Guy8b55f372010-08-18 17:10:07 -0700190 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700191 * Indicates whether the current transform has perspective components.
192 */
193 bool hasPerspectiveTransform() const;
194
195 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700196 * Dirty flags.
197 */
198 int flags;
199
200 /**
201 * Previous snapshot.
202 */
John Reckd9ee5502015-10-06 10:06:37 -0700203 Snapshot* previous;
Romain Guy5cbbce52010-06-27 22:59:20 -0700204
205 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800206 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800207 *
208 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700209 */
Romain Guydda57022010-07-06 11:39:32 -0700210 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700211
Romain Guy8aef54f2010-09-01 15:13:49 -0700212 /**
Romain Guy421458a2011-11-21 15:14:37 -0800213 * Target FBO used for rendering. Set to 0 when rendering directly
214 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700215 */
216 GLuint fbo;
217
218 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700219 * Local transformation. Holds the current translation, scale and
220 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800221 *
222 * This is a reference to a matrix owned by this snapshot or another
223 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700224 */
225 mat4* transform;
226
227 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -0700228 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
229 * has translucent rendering in a non-overlapping View. This value will be used by
230 * the renderer to set the alpha in the current color being used for ensuing drawing
231 * operations. The value is inherited by child snapshots because the same value should
Rob Tsuk487a92c2015-01-06 13:22:54 -0800232 * be applied to descendants of the current DisplayList (for example, a TextView contains
Chet Haasedb8c9a62012-03-21 18:54:18 -0700233 * the base alpha value which should be applied to the child DisplayLists used for drawing
234 * the actual text).
235 */
236 float alpha;
237
Chris Craikdeeda3d2014-05-05 19:09:33 -0700238 /**
239 * Current clipping round rect.
240 *
241 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
242 * never modified.
243 */
244 const RoundRectClipState* roundRectClipState;
245
Chris Craikfca52b752015-04-28 11:45:59 -0700246 /**
Chris Craik678ff812016-03-01 13:27:54 -0800247 * Current projection masking path - used exclusively to mask projected, tessellated circles.
Chris Craikfca52b752015-04-28 11:45:59 -0700248 */
Chris Craik678ff812016-03-01 13:27:54 -0800249 const SkPath* projectionPathMask;
Chris Craikfca52b752015-04-28 11:45:59 -0700250
Chris Craik5f803622013-03-21 14:39:04 -0700251 void dump() const;
252
Romain Guy5cbbce52010-06-27 22:59:20 -0700253private:
Chris Craika64a2be2014-05-14 14:17:01 -0700254 struct ViewportData {
Chris Craik92419752014-05-15 13:21:28 -0700255 ViewportData() : mWidth(0), mHeight(0) {}
Chris Craika64a2be2014-05-14 14:17:01 -0700256 void initialize(int width, int height) {
257 mWidth = width;
258 mHeight = height;
259 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
260 }
261
262 /*
263 * Width and height of current viewport.
264 *
265 * The viewport is always defined to be (0, 0, width, height).
266 */
267 int mWidth;
268 int mHeight;
269 /**
270 * Contains the current orthographic, projection matrix.
271 */
272 mat4 mOrthoMatrix;
273 };
274
Romain Guy8aef54f2010-09-01 15:13:49 -0700275 mat4 mTransformRoot;
Romain Guy079ba2c2010-07-16 14:12:24 -0700276
Rob Tsuk487a92c2015-01-06 13:22:54 -0800277 ClipArea mClipAreaRoot;
278 ClipArea* mClipArea;
279 Rect mLocalClip;
280
Chris Craika64a2be2014-05-14 14:17:01 -0700281 ViewportData mViewportData;
Chris Craik69e5adf2014-08-14 13:34:01 -0700282 Vector3 mRelativeLightCenter;
Romain Guy967e2bf2012-02-07 17:04:34 -0800283
Romain Guy5cbbce52010-06-27 22:59:20 -0700284}; // class Snapshot
285
286}; // namespace uirenderer
287}; // namespace android