blob: 655f819ca41b1bcf53959e9741714a5e0a74eab2 [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
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <ui/Region.h>
Chris Craikdeeda3d2014-05-05 19:09:33 -070023#include <utils/LinearAllocator.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070024#include <utils/RefBase.h>
25
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 {
John Reck1bcacfd2017-11-03 10:12:19 -070053 return rect.intersects(dangerRects[0]) || rect.intersects(dangerRects[1]) ||
54 rect.intersects(dangerRects[2]) || rect.intersects(dangerRects[3]);
Chris Craikdeeda3d2014-05-05 19:09:33 -070055 }
56
Chris Craike83cbd42014-09-03 17:52:24 -070057 bool highPriority;
Chris Craikdeeda3d2014-05-05 19:09:33 -070058 Matrix4 matrix;
59 Rect dangerRects[4];
Chris Craikaf4d04c2014-07-29 12:50:14 -070060 Rect innerRect;
61 float radius;
Chris Craikdeeda3d2014-05-05 19:09:33 -070062};
63
64/**
Romain Guy5cbbce52010-06-27 22:59:20 -070065 * A snapshot holds information about the current state of the rendering
66 * surface. A snapshot is usually created whenever the user calls save()
67 * and discarded when the user calls restore(). Once a snapshot is created,
68 * it can hold information for deferred rendering.
69 *
70 * Each snapshot has a link to a previous snapshot, indicating the previous
71 * state of the renderer.
72 */
John Reckd9ee5502015-10-06 10:06:37 -070073class Snapshot {
Romain Guy5cbbce52010-06-27 22:59:20 -070074public:
Romain Guyada4d532012-02-02 17:31:16 -080075 Snapshot();
John Reckd9ee5502015-10-06 10:06:37 -070076 Snapshot(Snapshot* s, int saveFlags);
Romain Guy5cbbce52010-06-27 22:59:20 -070077
78 /**
Romain Guyada4d532012-02-02 17:31:16 -080079 * Various flags set on ::flags.
Romain Guy5cbbce52010-06-27 22:59:20 -070080 */
81 enum Flags {
82 /**
83 * Indicates that the clip region was modified. When this
84 * snapshot is restored so must the clip.
85 */
86 kFlagClipSet = 0x1,
87 /**
Romain Guy5cbbce52010-06-27 22:59:20 -070088 * Indicates that this snapshot was created when saving
89 * a new layer.
90 */
Romain Guy079ba2c2010-07-16 14:12:24 -070091 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070092 /**
Romain Guyeb993562010-10-05 18:14:38 -070093 * Indicates that this snapshot is a special type of layer
94 * backed by an FBO. This flag only makes sense when the
95 * flag kFlagIsLayer is also set.
Chris Craika64a2be2014-05-14 14:17:01 -070096 *
97 * Viewport has been modified to fit the new Fbo, and must be
98 * restored when this snapshot is restored.
Romain Guyeb993562010-10-05 18:14:38 -070099 */
100 kFlagIsFboLayer = 0x4,
Romain Guy5cbbce52010-06-27 22:59:20 -0700101 };
102
103 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700104 * Modifies the current clip with the new clip rectangle and
105 * the specified operation. The specified rectangle is transformed
106 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700107 */
Mike Reed6e49c9f2016-12-02 15:36:59 -0500108 void clip(const Rect& localClip, SkClipOp op);
Romain Guyf607bdc2010-09-10 19:20:06 -0700109
110 /**
111 * Modifies the current clip with the new clip rectangle and
112 * the specified operation. The specified rectangle is considered
113 * already transformed.
114 */
Mike Reed6c67f1d2016-12-14 10:29:54 -0500115 void clipTransformed(const Rect& r, SkClipOp op = SkClipOp::kIntersect);
Romain Guy5cbbce52010-06-27 22:59:20 -0700116
117 /**
Rob Tsuk487a92c2015-01-06 13:22:54 -0800118 * Modifies the current clip with the specified path and operation.
119 */
Mike Reed6e49c9f2016-12-02 15:36:59 -0500120 void clipPath(const SkPath& path, SkClipOp op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800121
122 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700123 * Sets the current clip.
124 */
Romain Guyada4d532012-02-02 17:31:16 -0800125 void setClip(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700126
Romain Guyada4d532012-02-02 17:31:16 -0800127 /**
128 * Returns the current clip in local coordinates. The clip rect is
129 * transformed by the inverse transform matrix.
130 */
Ben Cheng1a498682014-05-17 15:37:26 -0700131 ANDROID_API const Rect& getLocalClip();
Chris Craik3f0854292014-04-15 16:18:08 -0700132
133 /**
134 * Returns the current clip in render target coordinates.
135 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700136 const Rect& getRenderTargetClip() const { return mClipArea->getClipRect(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800137
138 /*
139 * Accessor functions so that the clip area can stay private
140 */
141 bool clipIsEmpty() const { return mClipArea->isEmpty(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800142 const SkRegion& getClipRegion() const { return mClipArea->getClipRegion(); }
143 bool clipIsSimple() const { return mClipArea->isSimple(); }
144 const ClipArea& getClipArea() const { return *mClipArea; }
Chris Craike4db79d2015-12-22 16:32:23 -0800145 ClipArea& mutateClipArea() { return *mClipArea; }
Romain Guy959c91f2010-08-11 19:35:53 -0700146
John Reck1bcacfd2017-11-03 10:12:19 -0700147 WARN_UNUSED_RESULT const ClipBase* serializeIntersectedClip(
148 LinearAllocator& allocator, const ClipBase* recordedClip,
149 const Matrix4& recordedClipTransform);
Chris Craik04d46eb2016-04-07 13:51:07 -0700150 void applyClip(const ClipBase* clip, const Matrix4& transform);
151
Romain Guyada4d532012-02-02 17:31:16 -0800152 /**
153 * Resets the clip to the specified rect.
154 */
155 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700156
Chris Craika64a2be2014-05-14 14:17:01 -0700157 void initializeViewport(int width, int height) {
158 mViewportData.initialize(width, height);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800159 mClipAreaRoot.setViewportDimensions(width, height);
Chris Craika64a2be2014-05-14 14:17:01 -0700160 }
161
162 int getViewportWidth() const { return mViewportData.mWidth; }
163 int getViewportHeight() const { return mViewportData.mHeight; }
164 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
165
Chris Craik69e5adf2014-08-14 13:34:01 -0700166 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
167 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
168
Romain Guyada4d532012-02-02 17:31:16 -0800169 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700170 * Sets (and replaces) the current clipping outline
Chris Craike83cbd42014-09-03 17:52:24 -0700171 *
172 * If the current round rect clip is high priority, the incoming clip is ignored.
Chris Craikdeeda3d2014-05-05 19:09:33 -0700173 */
John Reck1bcacfd2017-11-03 10:12:19 -0700174 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds, float radius,
175 bool highPriority);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700176
177 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700178 * Sets (and replaces) the current projection mask
179 */
Chris Craik5e00c7c2016-07-06 16:10:09 -0700180 void setProjectionPathMask(const SkPath* path);
Romain Guyaf636eb2010-12-09 17:47:21 -0800181
Romain Guy8b55f372010-08-18 17:10:07 -0700182 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700183 * Indicates whether the current transform has perspective components.
184 */
185 bool hasPerspectiveTransform() const;
186
187 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700188 * Dirty flags.
189 */
190 int flags;
191
192 /**
193 * Previous snapshot.
194 */
John Reckd9ee5502015-10-06 10:06:37 -0700195 Snapshot* previous;
Romain Guy5cbbce52010-06-27 22:59:20 -0700196
197 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800198 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800199 *
200 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700201 */
Romain Guydda57022010-07-06 11:39:32 -0700202 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700203
Romain Guy8aef54f2010-09-01 15:13:49 -0700204 /**
Romain Guy421458a2011-11-21 15:14:37 -0800205 * Target FBO used for rendering. Set to 0 when rendering directly
206 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700207 */
208 GLuint fbo;
209
210 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700211 * Local transformation. Holds the current translation, scale and
212 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800213 *
214 * This is a reference to a matrix owned by this snapshot or another
215 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700216 */
217 mat4* transform;
218
219 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -0700220 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
221 * has translucent rendering in a non-overlapping View. This value will be used by
222 * the renderer to set the alpha in the current color being used for ensuing drawing
223 * operations. The value is inherited by child snapshots because the same value should
Rob Tsuk487a92c2015-01-06 13:22:54 -0800224 * be applied to descendants of the current DisplayList (for example, a TextView contains
Chet Haasedb8c9a62012-03-21 18:54:18 -0700225 * the base alpha value which should be applied to the child DisplayLists used for drawing
226 * the actual text).
227 */
228 float alpha;
229
Chris Craikdeeda3d2014-05-05 19:09:33 -0700230 /**
231 * Current clipping round rect.
232 *
233 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
234 * never modified.
235 */
236 const RoundRectClipState* roundRectClipState;
237
Chris Craikfca52b752015-04-28 11:45:59 -0700238 /**
Chris Craik678ff812016-03-01 13:27:54 -0800239 * Current projection masking path - used exclusively to mask projected, tessellated circles.
Chris Craikfca52b752015-04-28 11:45:59 -0700240 */
Chris Craik678ff812016-03-01 13:27:54 -0800241 const SkPath* projectionPathMask;
Chris Craikfca52b752015-04-28 11:45:59 -0700242
Chris Craik5f803622013-03-21 14:39:04 -0700243 void dump() const;
244
Romain Guy5cbbce52010-06-27 22:59:20 -0700245private:
Chris Craika64a2be2014-05-14 14:17:01 -0700246 struct ViewportData {
Chris Craik92419752014-05-15 13:21:28 -0700247 ViewportData() : mWidth(0), mHeight(0) {}
Chris Craika64a2be2014-05-14 14:17:01 -0700248 void initialize(int width, int height) {
249 mWidth = width;
250 mHeight = height;
251 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
252 }
253
254 /*
255 * Width and height of current viewport.
256 *
257 * The viewport is always defined to be (0, 0, width, height).
258 */
259 int mWidth;
260 int mHeight;
261 /**
262 * Contains the current orthographic, projection matrix.
263 */
264 mat4 mOrthoMatrix;
265 };
266
Romain Guy8aef54f2010-09-01 15:13:49 -0700267 mat4 mTransformRoot;
Romain Guy079ba2c2010-07-16 14:12:24 -0700268
Rob Tsuk487a92c2015-01-06 13:22:54 -0800269 ClipArea mClipAreaRoot;
270 ClipArea* mClipArea;
271 Rect mLocalClip;
272
Chris Craika64a2be2014-05-14 14:17:01 -0700273 ViewportData mViewportData;
Chris Craik69e5adf2014-08-14 13:34:01 -0700274 Vector3 mRelativeLightCenter;
Romain Guy967e2bf2012-02-07 17:04:34 -0800275
John Reck1bcacfd2017-11-03 10:12:19 -0700276}; // class Snapshot
Romain Guy5cbbce52010-06-27 22:59:20 -0700277
John Reck1bcacfd2017-11-03 10:12:19 -0700278}; // namespace uirenderer
279}; // namespace android