blob: 4ab58302df8f5383145dc96c83ff311d0f6d45e0 [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
Romain Guyada4d532012-02-02 17:31:16 -080026#include <SkRegion.h>
Romain Guy079ba2c2010-07-16 14:12:24 -070027
Rob Tsuk487a92c2015-01-06 13:22:54 -080028#include "ClipArea.h"
Romain Guydda57022010-07-06 11:39:32 -070029#include "Layer.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070030#include "Matrix.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070031#include "Outline.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070032#include "Rect.h"
Chris Craikdeeda3d2014-05-05 19:09:33 -070033#include "utils/Macros.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070034
35namespace android {
36namespace uirenderer {
37
38/**
Chris Craikdeeda3d2014-05-05 19:09:33 -070039 * Temporary structure holding information for a single outline clip.
40 *
41 * These structures are treated as immutable once created, and only exist for a single frame, which
42 * is why they may only be allocated with a LinearAllocator.
43 */
44class RoundRectClipState {
45public:
Chris Craik7a896002016-02-19 15:51:02 -080046 static void* operator new(size_t size) = delete;
Chris Craikdeeda3d2014-05-05 19:09:33 -070047 static void* operator new(size_t size, LinearAllocator& allocator) {
John Reck7df9ff22016-02-10 16:08:08 -080048 return allocator.alloc<RoundRectClipState>(size);
Chris Craikdeeda3d2014-05-05 19:09:33 -070049 }
50
51 bool areaRequiresRoundRectClip(const Rect& rect) const {
52 return rect.intersects(dangerRects[0])
53 || rect.intersects(dangerRects[1])
54 || rect.intersects(dangerRects[2])
55 || rect.intersects(dangerRects[3]);
56 }
57
Chris Craike83cbd42014-09-03 17:52:24 -070058 bool highPriority;
Chris Craikdeeda3d2014-05-05 19:09:33 -070059 Matrix4 matrix;
60 Rect dangerRects[4];
Chris Craikaf4d04c2014-07-29 12:50:14 -070061 Rect innerRect;
62 float radius;
Chris Craikdeeda3d2014-05-05 19:09:33 -070063};
64
65/**
Romain Guy5cbbce52010-06-27 22:59:20 -070066 * A snapshot holds information about the current state of the rendering
67 * surface. A snapshot is usually created whenever the user calls save()
68 * and discarded when the user calls restore(). Once a snapshot is created,
69 * it can hold information for deferred rendering.
70 *
71 * Each snapshot has a link to a previous snapshot, indicating the previous
72 * state of the renderer.
73 */
John Reckd9ee5502015-10-06 10:06:37 -070074class Snapshot {
Romain Guy5cbbce52010-06-27 22:59:20 -070075public:
Romain Guyada4d532012-02-02 17:31:16 -080076
77 Snapshot();
John Reckd9ee5502015-10-06 10:06:37 -070078 Snapshot(Snapshot* s, int saveFlags);
Romain Guy5cbbce52010-06-27 22:59:20 -070079
80 /**
Romain Guyada4d532012-02-02 17:31:16 -080081 * Various flags set on ::flags.
Romain Guy5cbbce52010-06-27 22:59:20 -070082 */
83 enum Flags {
84 /**
85 * Indicates that the clip region was modified. When this
86 * snapshot is restored so must the clip.
87 */
88 kFlagClipSet = 0x1,
89 /**
Romain Guy5cbbce52010-06-27 22:59:20 -070090 * Indicates that this snapshot was created when saving
91 * a new layer.
92 */
Romain Guy079ba2c2010-07-16 14:12:24 -070093 kFlagIsLayer = 0x2,
Romain Guyf86ef572010-07-01 11:05:42 -070094 /**
Romain Guyeb993562010-10-05 18:14:38 -070095 * Indicates that this snapshot is a special type of layer
96 * backed by an FBO. This flag only makes sense when the
97 * flag kFlagIsLayer is also set.
Chris Craika64a2be2014-05-14 14:17:01 -070098 *
99 * Viewport has been modified to fit the new Fbo, and must be
100 * restored when this snapshot is restored.
Romain Guyeb993562010-10-05 18:14:38 -0700101 */
102 kFlagIsFboLayer = 0x4,
Romain Guy5cbbce52010-06-27 22:59:20 -0700103 };
104
105 /**
Romain Guyf607bdc2010-09-10 19:20:06 -0700106 * Modifies the current clip with the new clip rectangle and
107 * the specified operation. The specified rectangle is transformed
108 * by this snapshot's trasnformation.
Romain Guy3d58c032010-07-14 16:34:53 -0700109 */
Chris Craika2a70722015-12-17 12:58:24 -0800110 void clip(const Rect& localClip, SkRegion::Op op);
Romain Guyf607bdc2010-09-10 19:20:06 -0700111
112 /**
113 * Modifies the current clip with the new clip rectangle and
114 * the specified operation. The specified rectangle is considered
115 * already transformed.
116 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700117 void clipTransformed(const Rect& r, SkRegion::Op op = SkRegion::kIntersect_Op);
Romain Guy5cbbce52010-06-27 22:59:20 -0700118
119 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800120 * Modifies the current clip with the specified region and operation.
121 * The specified region is considered already transformed.
122 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700123 void clipRegionTransformed(const SkRegion& region, SkRegion::Op op);
Romain Guy8ce00302013-01-15 18:51:42 -0800124
125 /**
Rob Tsuk487a92c2015-01-06 13:22:54 -0800126 * Modifies the current clip with the specified path and operation.
127 */
Chris Craik4d3e7042015-08-20 12:54:25 -0700128 void clipPath(const SkPath& path, SkRegion::Op op);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800129
130 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700131 * Sets the current clip.
132 */
Romain Guyada4d532012-02-02 17:31:16 -0800133 void setClip(float left, float top, float right, float bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700134
Romain Guyada4d532012-02-02 17:31:16 -0800135 /**
136 * Returns the current clip in local coordinates. The clip rect is
137 * transformed by the inverse transform matrix.
138 */
Ben Cheng1a498682014-05-17 15:37:26 -0700139 ANDROID_API const Rect& getLocalClip();
Chris Craik3f0854292014-04-15 16:18:08 -0700140
141 /**
142 * Returns the current clip in render target coordinates.
143 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700144 const Rect& getRenderTargetClip() const { return mClipArea->getClipRect(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800145
146 /*
147 * Accessor functions so that the clip area can stay private
148 */
149 bool clipIsEmpty() const { return mClipArea->isEmpty(); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800150 const SkRegion& getClipRegion() const { return mClipArea->getClipRegion(); }
151 bool clipIsSimple() const { return mClipArea->isSimple(); }
152 const ClipArea& getClipArea() const { return *mClipArea; }
Chris Craike4db79d2015-12-22 16:32:23 -0800153 ClipArea& mutateClipArea() { return *mClipArea; }
Romain Guy959c91f2010-08-11 19:35:53 -0700154
Chris Craik04d46eb2016-04-07 13:51:07 -0700155 WARN_UNUSED_RESULT const ClipBase* serializeIntersectedClip(LinearAllocator& allocator,
156 const ClipBase* recordedClip, const Matrix4& recordedClipTransform);
157 void applyClip(const ClipBase* clip, const Matrix4& transform);
158
Romain Guyada4d532012-02-02 17:31:16 -0800159 /**
160 * Resets the clip to the specified rect.
161 */
162 void resetClip(float left, float top, float right, float bottom);
Romain Guy959c91f2010-08-11 19:35:53 -0700163
Chris Craika64a2be2014-05-14 14:17:01 -0700164 void initializeViewport(int width, int height) {
165 mViewportData.initialize(width, height);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800166 mClipAreaRoot.setViewportDimensions(width, height);
Chris Craika64a2be2014-05-14 14:17:01 -0700167 }
168
169 int getViewportWidth() const { return mViewportData.mWidth; }
170 int getViewportHeight() const { return mViewportData.mHeight; }
171 const Matrix4& getOrthoMatrix() const { return mViewportData.mOrthoMatrix; }
172
Chris Craik69e5adf2014-08-14 13:34:01 -0700173 const Vector3& getRelativeLightCenter() const { return mRelativeLightCenter; }
174 void setRelativeLightCenter(const Vector3& lightCenter) { mRelativeLightCenter = lightCenter; }
175
Romain Guyada4d532012-02-02 17:31:16 -0800176 /**
Chris Craikdeeda3d2014-05-05 19:09:33 -0700177 * Sets (and replaces) the current clipping outline
Chris Craike83cbd42014-09-03 17:52:24 -0700178 *
179 * If the current round rect clip is high priority, the incoming clip is ignored.
Chris Craikdeeda3d2014-05-05 19:09:33 -0700180 */
Chris Craike83cbd42014-09-03 17:52:24 -0700181 void setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
182 float radius, bool highPriority);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700183
184 /**
Chris Craikfca52b752015-04-28 11:45:59 -0700185 * Sets (and replaces) the current projection mask
186 */
Chris Craik5e00c7c2016-07-06 16:10:09 -0700187 void setProjectionPathMask(const SkPath* path);
Romain Guyaf636eb2010-12-09 17:47:21 -0800188
Romain Guy8b55f372010-08-18 17:10:07 -0700189 /**
Romain Guya3dc55f2012-09-28 13:55:44 -0700190 * Indicates whether the current transform has perspective components.
191 */
192 bool hasPerspectiveTransform() const;
193
194 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700195 * Dirty flags.
196 */
197 int flags;
198
199 /**
200 * Previous snapshot.
201 */
John Reckd9ee5502015-10-06 10:06:37 -0700202 Snapshot* previous;
Romain Guy5cbbce52010-06-27 22:59:20 -0700203
204 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800205 * A pointer to the currently active layer.
Romain Guyada4d532012-02-02 17:31:16 -0800206 *
207 * This snapshot does not own the layer, this pointer must not be freed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700208 */
Romain Guydda57022010-07-06 11:39:32 -0700209 Layer* layer;
Romain Guyf86ef572010-07-01 11:05:42 -0700210
Romain Guy8aef54f2010-09-01 15:13:49 -0700211 /**
Romain Guy421458a2011-11-21 15:14:37 -0800212 * Target FBO used for rendering. Set to 0 when rendering directly
213 * into the framebuffer.
Romain Guyeb993562010-10-05 18:14:38 -0700214 */
215 GLuint fbo;
216
217 /**
Romain Guy8aef54f2010-09-01 15:13:49 -0700218 * Local transformation. Holds the current translation, scale and
219 * rotation values.
Romain Guyada4d532012-02-02 17:31:16 -0800220 *
221 * This is a reference to a matrix owned by this snapshot or another
222 * snapshot. This pointer must not be freed. See ::mTransformRoot.
Romain Guy8aef54f2010-09-01 15:13:49 -0700223 */
224 mat4* transform;
225
226 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -0700227 * Current alpha value. This value is 1 by default, but may be set by a DisplayList which
228 * has translucent rendering in a non-overlapping View. This value will be used by
229 * the renderer to set the alpha in the current color being used for ensuing drawing
230 * operations. The value is inherited by child snapshots because the same value should
Rob Tsuk487a92c2015-01-06 13:22:54 -0800231 * be applied to descendants of the current DisplayList (for example, a TextView contains
Chet Haasedb8c9a62012-03-21 18:54:18 -0700232 * the base alpha value which should be applied to the child DisplayLists used for drawing
233 * the actual text).
234 */
235 float alpha;
236
Chris Craikdeeda3d2014-05-05 19:09:33 -0700237 /**
238 * Current clipping round rect.
239 *
240 * Points to data not owned by the snapshot, and may only be replaced by subsequent RR clips,
241 * never modified.
242 */
243 const RoundRectClipState* roundRectClipState;
244
Chris Craikfca52b752015-04-28 11:45:59 -0700245 /**
Chris Craik678ff812016-03-01 13:27:54 -0800246 * Current projection masking path - used exclusively to mask projected, tessellated circles.
Chris Craikfca52b752015-04-28 11:45:59 -0700247 */
Chris Craik678ff812016-03-01 13:27:54 -0800248 const SkPath* projectionPathMask;
Chris Craikfca52b752015-04-28 11:45:59 -0700249
Chris Craik5f803622013-03-21 14:39:04 -0700250 void dump() const;
251
Romain Guy5cbbce52010-06-27 22:59:20 -0700252private:
Chris Craika64a2be2014-05-14 14:17:01 -0700253 struct ViewportData {
Chris Craik92419752014-05-15 13:21:28 -0700254 ViewportData() : mWidth(0), mHeight(0) {}
Chris Craika64a2be2014-05-14 14:17:01 -0700255 void initialize(int width, int height) {
256 mWidth = width;
257 mHeight = height;
258 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
259 }
260
261 /*
262 * Width and height of current viewport.
263 *
264 * The viewport is always defined to be (0, 0, width, height).
265 */
266 int mWidth;
267 int mHeight;
268 /**
269 * Contains the current orthographic, projection matrix.
270 */
271 mat4 mOrthoMatrix;
272 };
273
Romain Guy8aef54f2010-09-01 15:13:49 -0700274 mat4 mTransformRoot;
Romain Guy079ba2c2010-07-16 14:12:24 -0700275
Rob Tsuk487a92c2015-01-06 13:22:54 -0800276 ClipArea mClipAreaRoot;
277 ClipArea* mClipArea;
278 Rect mLocalClip;
279
Chris Craika64a2be2014-05-14 14:17:01 -0700280 ViewportData mViewportData;
Chris Craik69e5adf2014-08-14 13:34:01 -0700281 Vector3 mRelativeLightCenter;
Romain Guy967e2bf2012-02-07 17:04:34 -0800282
Romain Guy5cbbce52010-06-27 22:59:20 -0700283}; // class Snapshot
284
285}; // namespace uirenderer
286}; // namespace android