blob: e358b57f6fe1e561fda220516337f5016d7c0afc [file] [log] [blame]
John Reckacb6f072014-03-12 16:11:23 -07001/*
2 * Copyright (C) 2014 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 */
Chris Craik5e00c7c2016-07-06 16:10:09 -070016
17#pragma once
John Reckacb6f072014-03-12 16:11:23 -070018
Alec Mouri22d753f2019-09-05 17:11:45 -070019#ifdef __ANDROID__ // Layoutlib does not support device info
Chris Craik76caecf2015-11-02 19:17:45 -080020#include "DeviceInfo.h"
Alec Mouri22d753f2019-09-05 17:11:45 -070021#endif // __ANDROID__
22
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "Outline.h"
Chris Craik76caecf2015-11-02 19:17:45 -080024#include "Rect.h"
25#include "RevealClip.h"
John Reck5cb290b2021-02-01 13:47:31 -050026#include "effects/StretchEffect.h"
Chris Craik76caecf2015-11-02 19:17:45 -080027#include "utils/MathUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070028#include "utils/PaintUtils.h"
John Reckacb6f072014-03-12 16:11:23 -070029
Mike Reed260ab722016-10-07 15:59:20 -040030#include <SkBlendMode.h>
Nader Jawad390d6e82020-09-24 21:35:03 -070031#include <SkImageFilter.h>
John Reckacb6f072014-03-12 16:11:23 -070032#include <SkCamera.h>
John Reck3c0369b2017-11-13 16:47:35 -080033#include <SkColor.h>
John Reckacb6f072014-03-12 16:11:23 -070034#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070035#include <SkRegion.h>
Chris Craikb49f4462014-03-20 12:44:20 -070036
Chris Craik76caecf2015-11-02 19:17:45 -080037#include <androidfw/ResourceTypes.h>
John Reck1bcacfd2017-11-03 10:12:19 -070038#include <cutils/compiler.h>
39#include <stddef.h>
Chris Craik76caecf2015-11-02 19:17:45 -080040#include <utils/Log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070041#include <algorithm>
sergeyvc3849aa2016-08-08 13:22:06 -070042#include <ostream>
John Reck1bcacfd2017-11-03 10:12:19 -070043#include <vector>
John Reckacb6f072014-03-12 16:11:23 -070044
John Reckacb6f072014-03-12 16:11:23 -070045class SkBitmap;
John Reck25fbb3f2014-06-12 13:46:45 -070046class SkColorFilter;
John Reckacb6f072014-03-12 16:11:23 -070047class SkPaint;
John Reckacb6f072014-03-12 16:11:23 -070048
49namespace android {
50namespace uirenderer {
51
52class Matrix4;
53class RenderNode;
John Reck25fbb3f2014-06-12 13:46:45 -070054class RenderProperties;
John Reckacb6f072014-03-12 16:11:23 -070055
John Reck79c7de72014-05-23 10:33:31 -070056// The __VA_ARGS__ will be executed if a & b are not equal
Chih-Hung Hsiehcef190d2016-05-19 15:25:50 -070057#define RP_SET(a, b, ...) ((a) != (b) ? ((a) = (b), ##__VA_ARGS__, true) : false)
John Reck79c7de72014-05-23 10:33:31 -070058#define RP_SET_AND_DIRTY(a, b) RP_SET(a, b, mPrimitiveFields.mMatrixOrPivotDirty = true)
59
John Reck25fbb3f2014-06-12 13:46:45 -070060// Keep in sync with View.java:LAYER_TYPE_*
Chris Craik182952f2015-03-09 14:17:29 -070061enum class LayerType {
62 None = 0,
Derek Sollenberger52230bc2018-02-16 12:24:13 -050063 // We cannot build the software layer directly (must be done at record time) and all management
64 // of software layers is handled in Java.
Chris Craik182952f2015-03-09 14:17:29 -070065 Software = 1,
66 RenderLayer = 2,
John Reck25fbb3f2014-06-12 13:46:45 -070067};
68
Chris Craika753f4c2014-07-24 12:39:17 -070069enum ClippingFlags {
John Reck1bcacfd2017-11-03 10:12:19 -070070 CLIP_TO_BOUNDS = 0x1 << 0,
Chris Craika753f4c2014-07-24 12:39:17 -070071 CLIP_TO_CLIP_BOUNDS = 0x1 << 1,
72};
73
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050074class LayerProperties {
John Reck25fbb3f2014-06-12 13:46:45 -070075public:
76 bool setType(LayerType type) {
77 if (RP_SET(mType, type)) {
78 reset();
79 return true;
80 }
81 return false;
82 }
83
John Reck1bcacfd2017-11-03 10:12:19 -070084 bool setOpaque(bool opaque) { return RP_SET(mOpaque, opaque); }
John Reck25fbb3f2014-06-12 13:46:45 -070085
John Reck1bcacfd2017-11-03 10:12:19 -070086 bool opaque() const { return mOpaque; }
John Reck25fbb3f2014-06-12 13:46:45 -070087
John Reck1bcacfd2017-11-03 10:12:19 -070088 bool setAlpha(uint8_t alpha) { return RP_SET(mAlpha, alpha); }
John Reck25fbb3f2014-06-12 13:46:45 -070089
John Reck1bcacfd2017-11-03 10:12:19 -070090 uint8_t alpha() const { return mAlpha; }
John Reck25fbb3f2014-06-12 13:46:45 -070091
John Reck1bcacfd2017-11-03 10:12:19 -070092 bool setXferMode(SkBlendMode mode) { return RP_SET(mMode, mode); }
John Reck25fbb3f2014-06-12 13:46:45 -070093
John Reck1bcacfd2017-11-03 10:12:19 -070094 SkBlendMode xferMode() const { return mMode; }
John Reck25fbb3f2014-06-12 13:46:45 -070095
Ben Wagnerc1a8a462018-07-12 12:41:28 -040096 SkColorFilter* getColorFilter() const { return mColorFilter.get(); }
John Reck25fbb3f2014-06-12 13:46:45 -070097
Nader Jawad390d6e82020-09-24 21:35:03 -070098 bool setImageFilter(SkImageFilter* imageFilter);
99
Dongya Jiange2b99382022-02-28 21:35:57 +0800100 bool setBackdropImageFilter(SkImageFilter* imageFilter);
101
Nader Jawad390d6e82020-09-24 21:35:03 -0700102 SkImageFilter* getImageFilter() const { return mImageFilter.get(); }
103
Dongya Jiange2b99382022-02-28 21:35:57 +0800104 SkImageFilter* getBackdropImageFilter() const { return mBackdropImageFilter.get(); }
105
John Reck5cb290b2021-02-01 13:47:31 -0500106 const StretchEffect& getStretchEffect() const { return mStretchEffect; }
107
108 StretchEffect& mutableStretchEffect() { return mStretchEffect; }
109
John Reck25fbb3f2014-06-12 13:46:45 -0700110 // Sets alpha, xfermode, and colorfilter from an SkPaint
111 // paint may be NULL, in which case defaults will be set
112 bool setFromPaint(const SkPaint* paint);
113
John Reck1bcacfd2017-11-03 10:12:19 -0700114 bool needsBlending() const { return !opaque() || alpha() < 255; }
John Reck25fbb3f2014-06-12 13:46:45 -0700115
116 LayerProperties& operator=(const LayerProperties& other);
117
John Reck470a9192018-12-17 10:55:54 -0800118 // Strongly recommend using effectiveLayerType instead
119 LayerType type() const { return mType; }
120
John Reck25fbb3f2014-06-12 13:46:45 -0700121private:
122 LayerProperties();
123 ~LayerProperties();
124 void reset();
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400125 bool setColorFilter(SkColorFilter* filter);
John Reck25fbb3f2014-06-12 13:46:45 -0700126
127 friend class RenderProperties;
128
Chris Craik182952f2015-03-09 14:17:29 -0700129 LayerType mType = LayerType::None;
John Reck25fbb3f2014-06-12 13:46:45 -0700130 // Whether or not that Layer's content is opaque, doesn't include alpha
131 bool mOpaque;
132 uint8_t mAlpha;
Mike Reed260ab722016-10-07 15:59:20 -0400133 SkBlendMode mMode;
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400134 sk_sp<SkColorFilter> mColorFilter;
Nader Jawad390d6e82020-09-24 21:35:03 -0700135 sk_sp<SkImageFilter> mImageFilter;
Dongya Jiange2b99382022-02-28 21:35:57 +0800136 sk_sp<SkImageFilter> mBackdropImageFilter;
John Reck5cb290b2021-02-01 13:47:31 -0500137 StretchEffect mStretchEffect;
John Reck25fbb3f2014-06-12 13:46:45 -0700138};
139
John Reckacb6f072014-03-12 16:11:23 -0700140/*
141 * Data structure that holds the properties for a RenderNode
142 */
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -0500143class RenderProperties {
John Reckacb6f072014-03-12 16:11:23 -0700144public:
145 RenderProperties();
146 virtual ~RenderProperties();
147
Chris Craika753f4c2014-07-24 12:39:17 -0700148 static bool setFlag(int flag, bool newValue, int* outFlags) {
149 if (newValue) {
150 if (!(flag & *outFlags)) {
151 *outFlags |= flag;
152 return true;
153 }
154 return false;
155 } else {
156 if (flag & *outFlags) {
157 *outFlags &= ~flag;
158 return true;
159 }
160 return false;
161 }
162 }
163
Chris Craika766cb22015-06-08 16:49:43 -0700164 /**
165 * Set internal layer state based on whether this layer
166 *
167 * Additionally, returns true if child RenderNodes with functors will need to use a layer
168 * to support clipping.
169 */
170 bool prepareForFunctorPresence(bool willHaveFunctor, bool ancestorDictatesFunctorsNeedLayer) {
171 // parent may have already dictated that a descendant layer is needed
John Reck1bcacfd2017-11-03 10:12:19 -0700172 bool functorsNeedLayer =
Chet Haase33c1ea72021-09-27 20:56:08 +0000173 ancestorDictatesFunctorsNeedLayer ||
174 CC_UNLIKELY(isClipMayBeComplex())
Chris Craika766cb22015-06-08 16:49:43 -0700175
176 // Round rect clipping forces layer for functors
Chet Haase33c1ea72021-09-27 20:56:08 +0000177 || CC_UNLIKELY(getOutline().willComplexClip()) ||
John Reck1bcacfd2017-11-03 10:12:19 -0700178 CC_UNLIKELY(getRevealClip().willClip())
Chris Craika766cb22015-06-08 16:49:43 -0700179
180 // Complex matrices forces layer, due to stencil clipping
John Reck1bcacfd2017-11-03 10:12:19 -0700181 || CC_UNLIKELY(getTransformMatrix() && !getTransformMatrix()->isScaleTranslate()) ||
182 CC_UNLIKELY(getAnimationMatrix() && !getAnimationMatrix()->isScaleTranslate()) ||
183 CC_UNLIKELY(getStaticMatrix() && !getStaticMatrix()->isScaleTranslate());
Chris Craika766cb22015-06-08 16:49:43 -0700184
185 mComputedFields.mNeedLayerForFunctors = (willHaveFunctor && functorsNeedLayer);
186
187 // If on a layer, will have consumed the need for isolating functors from stencil.
188 // Thus, it's safe to reset the flag until some descendent sets it.
189 return CC_LIKELY(effectiveLayerType() == LayerType::None) && functorsNeedLayer;
190 }
191
John Reckd0a0b2a2014-03-20 16:28:56 -0700192 RenderProperties& operator=(const RenderProperties& other);
193
John Reck79c7de72014-05-23 10:33:31 -0700194 bool setClipToBounds(bool clipToBounds) {
Chris Craika753f4c2014-07-24 12:39:17 -0700195 return setFlag(CLIP_TO_BOUNDS, clipToBounds, &mPrimitiveFields.mClippingFlags);
196 }
197
198 bool setClipBounds(const Rect& clipBounds) {
199 bool ret = setFlag(CLIP_TO_CLIP_BOUNDS, true, &mPrimitiveFields.mClippingFlags);
200 return RP_SET(mPrimitiveFields.mClipBounds, clipBounds) || ret;
201 }
202
203 bool setClipBoundsEmpty() {
204 return setFlag(CLIP_TO_CLIP_BOUNDS, false, &mPrimitiveFields.mClippingFlags);
John Reckacb6f072014-03-12 16:11:23 -0700205 }
206
John Reck79c7de72014-05-23 10:33:31 -0700207 bool setProjectBackwards(bool shouldProject) {
208 return RP_SET(mPrimitiveFields.mProjectBackwards, shouldProject);
John Reckacb6f072014-03-12 16:11:23 -0700209 }
210
Chris Craik6fe991e52015-10-20 09:39:42 -0700211 bool setProjectionReceiver(bool shouldReceive) {
212 return RP_SET(mPrimitiveFields.mProjectionReceiver, shouldReceive);
John Reckacb6f072014-03-12 16:11:23 -0700213 }
214
John Reck1bcacfd2017-11-03 10:12:19 -0700215 bool isProjectionReceiver() const { return mPrimitiveFields.mProjectionReceiver; }
John Reckacb6f072014-03-12 16:11:23 -0700216
Stan Ilievf09ee582018-11-06 17:35:50 -0500217 bool setClipMayBeComplex(bool isClipMayBeComplex) {
218 return RP_SET(mPrimitiveFields.mClipMayBeComplex, isClipMayBeComplex);
219 }
220
221 bool isClipMayBeComplex() const { return mPrimitiveFields.mClipMayBeComplex; }
222
John Reck79c7de72014-05-23 10:33:31 -0700223 bool setStaticMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700224 delete mStaticMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -0700225 if (matrix) {
226 mStaticMatrix = new SkMatrix(*matrix);
227 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800228 mStaticMatrix = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700229 }
John Reck79c7de72014-05-23 10:33:31 -0700230 return true;
John Reckacb6f072014-03-12 16:11:23 -0700231 }
232
233 // Can return NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700234 const SkMatrix* getStaticMatrix() const { return mStaticMatrix; }
John Reckacb6f072014-03-12 16:11:23 -0700235
John Reck79c7de72014-05-23 10:33:31 -0700236 bool setAnimationMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700237 delete mAnimationMatrix;
238 if (matrix) {
239 mAnimationMatrix = new SkMatrix(*matrix);
240 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800241 mAnimationMatrix = nullptr;
John Reckacb6f072014-03-12 16:11:23 -0700242 }
John Reck79c7de72014-05-23 10:33:31 -0700243 return true;
John Reckacb6f072014-03-12 16:11:23 -0700244 }
245
John Reck79c7de72014-05-23 10:33:31 -0700246 bool setAlpha(float alpha) {
John Reck3b52c032014-08-06 10:19:32 -0700247 alpha = MathUtils::clampAlpha(alpha);
John Reck79c7de72014-05-23 10:33:31 -0700248 return RP_SET(mPrimitiveFields.mAlpha, alpha);
John Reckacb6f072014-03-12 16:11:23 -0700249 }
250
John Reck1bcacfd2017-11-03 10:12:19 -0700251 float getAlpha() const { return mPrimitiveFields.mAlpha; }
John Reckacb6f072014-03-12 16:11:23 -0700252
John Reck79c7de72014-05-23 10:33:31 -0700253 bool setHasOverlappingRendering(bool hasOverlappingRendering) {
254 return RP_SET(mPrimitiveFields.mHasOverlappingRendering, hasOverlappingRendering);
John Reckacb6f072014-03-12 16:11:23 -0700255 }
256
John Reck1bcacfd2017-11-03 10:12:19 -0700257 bool hasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckacb6f072014-03-12 16:11:23 -0700258
John Reck79c7de72014-05-23 10:33:31 -0700259 bool setElevation(float elevation) {
260 return RP_SET(mPrimitiveFields.mElevation, elevation);
261 // Don't dirty matrix/pivot, since they don't respect Z
Chris Craikcc39e162014-04-25 18:34:11 -0700262 }
263
John Reck1bcacfd2017-11-03 10:12:19 -0700264 float getElevation() const { return mPrimitiveFields.mElevation; }
Chris Craikcc39e162014-04-25 18:34:11 -0700265
John Reck79c7de72014-05-23 10:33:31 -0700266 bool setTranslationX(float translationX) {
267 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationX, translationX);
John Reckacb6f072014-03-12 16:11:23 -0700268 }
269
John Reck1bcacfd2017-11-03 10:12:19 -0700270 float getTranslationX() const { return mPrimitiveFields.mTranslationX; }
John Reckacb6f072014-03-12 16:11:23 -0700271
John Reck79c7de72014-05-23 10:33:31 -0700272 bool setTranslationY(float translationY) {
273 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationY, translationY);
John Reckacb6f072014-03-12 16:11:23 -0700274 }
275
John Reck1bcacfd2017-11-03 10:12:19 -0700276 float getTranslationY() const { return mPrimitiveFields.mTranslationY; }
John Reckacb6f072014-03-12 16:11:23 -0700277
John Reck79c7de72014-05-23 10:33:31 -0700278 bool setTranslationZ(float translationZ) {
279 return RP_SET(mPrimitiveFields.mTranslationZ, translationZ);
280 // mMatrixOrPivotDirty not set, since matrix doesn't respect Z
John Reckacb6f072014-03-12 16:11:23 -0700281 }
282
John Reck1bcacfd2017-11-03 10:12:19 -0700283 float getTranslationZ() const { return mPrimitiveFields.mTranslationZ; }
John Reckacb6f072014-03-12 16:11:23 -0700284
John Recke45b1fd2014-04-15 09:50:16 -0700285 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700286 bool setX(float value) { return setTranslationX(value - getLeft()); }
John Recke45b1fd2014-04-15 09:50:16 -0700287
288 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700289 float getX() const { return getLeft() + getTranslationX(); }
John Recke45b1fd2014-04-15 09:50:16 -0700290
291 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700292 bool setY(float value) { return setTranslationY(value - getTop()); }
John Recke45b1fd2014-04-15 09:50:16 -0700293
294 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700295 float getY() const { return getTop() + getTranslationY(); }
John Recke45b1fd2014-04-15 09:50:16 -0700296
297 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700298 bool setZ(float value) { return setTranslationZ(value - getElevation()); }
John Recke45b1fd2014-04-15 09:50:16 -0700299
John Reck1bcacfd2017-11-03 10:12:19 -0700300 float getZ() const { return getElevation() + getTranslationZ(); }
Chris Craikcc39e162014-04-25 18:34:11 -0700301
John Reck79c7de72014-05-23 10:33:31 -0700302 bool setRotation(float rotation) {
303 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotation, rotation);
John Reckacb6f072014-03-12 16:11:23 -0700304 }
305
John Reck1bcacfd2017-11-03 10:12:19 -0700306 float getRotation() const { return mPrimitiveFields.mRotation; }
John Reckacb6f072014-03-12 16:11:23 -0700307
John Reck79c7de72014-05-23 10:33:31 -0700308 bool setRotationX(float rotationX) {
309 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationX, rotationX);
John Reckacb6f072014-03-12 16:11:23 -0700310 }
311
John Reck1bcacfd2017-11-03 10:12:19 -0700312 float getRotationX() const { return mPrimitiveFields.mRotationX; }
John Reckacb6f072014-03-12 16:11:23 -0700313
John Reck79c7de72014-05-23 10:33:31 -0700314 bool setRotationY(float rotationY) {
315 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationY, rotationY);
John Reckacb6f072014-03-12 16:11:23 -0700316 }
317
John Reck1bcacfd2017-11-03 10:12:19 -0700318 float getRotationY() const { return mPrimitiveFields.mRotationY; }
John Reckacb6f072014-03-12 16:11:23 -0700319
John Reck1bcacfd2017-11-03 10:12:19 -0700320 bool setScaleX(float scaleX) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleX, scaleX); }
John Reckacb6f072014-03-12 16:11:23 -0700321
John Reck1bcacfd2017-11-03 10:12:19 -0700322 float getScaleX() const { return mPrimitiveFields.mScaleX; }
John Reckacb6f072014-03-12 16:11:23 -0700323
John Reck1bcacfd2017-11-03 10:12:19 -0700324 bool setScaleY(float scaleY) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleY, scaleY); }
John Reckacb6f072014-03-12 16:11:23 -0700325
John Reck1bcacfd2017-11-03 10:12:19 -0700326 float getScaleY() const { return mPrimitiveFields.mScaleY; }
John Reckacb6f072014-03-12 16:11:23 -0700327
John Reck79c7de72014-05-23 10:33:31 -0700328 bool setPivotX(float pivotX) {
John Reck1bcacfd2017-11-03 10:12:19 -0700329 if (RP_SET(mPrimitiveFields.mPivotX, pivotX) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700330 mPrimitiveFields.mMatrixOrPivotDirty = true;
331 mPrimitiveFields.mPivotExplicitlySet = true;
332 return true;
333 }
334 return false;
John Reckacb6f072014-03-12 16:11:23 -0700335 }
336
John Reckd0a0b2a2014-03-20 16:28:56 -0700337 /* Note that getPivotX and getPivotY are adjusted by updateMatrix(),
John Reck79c7de72014-05-23 10:33:31 -0700338 * so the value returned may be stale if the RenderProperties has been
339 * modified since the last call to updateMatrix()
John Reckd0a0b2a2014-03-20 16:28:56 -0700340 */
John Reck1bcacfd2017-11-03 10:12:19 -0700341 float getPivotX() const { return mPrimitiveFields.mPivotX; }
John Reckacb6f072014-03-12 16:11:23 -0700342
John Reck79c7de72014-05-23 10:33:31 -0700343 bool setPivotY(float pivotY) {
John Reck1bcacfd2017-11-03 10:12:19 -0700344 if (RP_SET(mPrimitiveFields.mPivotY, pivotY) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700345 mPrimitiveFields.mMatrixOrPivotDirty = true;
346 mPrimitiveFields.mPivotExplicitlySet = true;
347 return true;
348 }
349 return false;
John Reckacb6f072014-03-12 16:11:23 -0700350 }
351
John Reck1bcacfd2017-11-03 10:12:19 -0700352 float getPivotY() const { return mPrimitiveFields.mPivotY; }
John Reckacb6f072014-03-12 16:11:23 -0700353
John Reck1bcacfd2017-11-03 10:12:19 -0700354 bool isPivotExplicitlySet() const { return mPrimitiveFields.mPivotExplicitlySet; }
Chris Craik49e6c7392014-03-31 12:34:11 -0700355
John Recke170fb62018-05-07 08:12:07 -0700356 bool resetPivot() { return RP_SET_AND_DIRTY(mPrimitiveFields.mPivotExplicitlySet, false); }
John Reck8686e1f2018-03-21 16:31:21 -0700357
John Reck79c7de72014-05-23 10:33:31 -0700358 bool setCameraDistance(float distance) {
Chris Craik49e6c7392014-03-31 12:34:11 -0700359 if (distance != getCameraDistance()) {
John Reckf7483e32014-04-11 08:54:47 -0700360 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -0700361 mComputedFields.mTransformCamera.setCameraLocation(0, 0, distance);
John Reck79c7de72014-05-23 10:33:31 -0700362 return true;
John Reckacb6f072014-03-12 16:11:23 -0700363 }
John Reck79c7de72014-05-23 10:33:31 -0700364 return false;
John Reckacb6f072014-03-12 16:11:23 -0700365 }
366
367 float getCameraDistance() const {
Chris Craik49e6c7392014-03-31 12:34:11 -0700368 // TODO: update getCameraLocationZ() to be const
369 return const_cast<Sk3DView*>(&mComputedFields.mTransformCamera)->getCameraLocationZ();
John Reckacb6f072014-03-12 16:11:23 -0700370 }
371
John Reck79c7de72014-05-23 10:33:31 -0700372 bool setLeft(int left) {
373 if (RP_SET(mPrimitiveFields.mLeft, left)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700374 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700375 if (!mPrimitiveFields.mPivotExplicitlySet) {
376 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700377 }
John Reck79c7de72014-05-23 10:33:31 -0700378 return true;
John Reckacb6f072014-03-12 16:11:23 -0700379 }
John Reck79c7de72014-05-23 10:33:31 -0700380 return false;
John Reckacb6f072014-03-12 16:11:23 -0700381 }
382
John Reck1bcacfd2017-11-03 10:12:19 -0700383 int getLeft() const { return mPrimitiveFields.mLeft; }
John Reckacb6f072014-03-12 16:11:23 -0700384
John Reck79c7de72014-05-23 10:33:31 -0700385 bool setTop(int top) {
386 if (RP_SET(mPrimitiveFields.mTop, top)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700387 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700388 if (!mPrimitiveFields.mPivotExplicitlySet) {
389 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700390 }
John Reck79c7de72014-05-23 10:33:31 -0700391 return true;
John Reckacb6f072014-03-12 16:11:23 -0700392 }
John Reck79c7de72014-05-23 10:33:31 -0700393 return false;
John Reckacb6f072014-03-12 16:11:23 -0700394 }
395
John Reck1bcacfd2017-11-03 10:12:19 -0700396 int getTop() const { return mPrimitiveFields.mTop; }
John Reckacb6f072014-03-12 16:11:23 -0700397
John Reck79c7de72014-05-23 10:33:31 -0700398 bool setRight(int right) {
399 if (RP_SET(mPrimitiveFields.mRight, right)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700400 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700401 if (!mPrimitiveFields.mPivotExplicitlySet) {
402 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700403 }
John Reck79c7de72014-05-23 10:33:31 -0700404 return true;
John Reckacb6f072014-03-12 16:11:23 -0700405 }
John Reck79c7de72014-05-23 10:33:31 -0700406 return false;
John Reckacb6f072014-03-12 16:11:23 -0700407 }
408
John Reck1bcacfd2017-11-03 10:12:19 -0700409 int getRight() const { return mPrimitiveFields.mRight; }
John Reckacb6f072014-03-12 16:11:23 -0700410
John Reck79c7de72014-05-23 10:33:31 -0700411 bool setBottom(int bottom) {
412 if (RP_SET(mPrimitiveFields.mBottom, bottom)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700413 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700414 if (!mPrimitiveFields.mPivotExplicitlySet) {
415 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700416 }
John Reck79c7de72014-05-23 10:33:31 -0700417 return true;
John Reckacb6f072014-03-12 16:11:23 -0700418 }
John Reck79c7de72014-05-23 10:33:31 -0700419 return false;
John Reckacb6f072014-03-12 16:11:23 -0700420 }
421
John Reck1bcacfd2017-11-03 10:12:19 -0700422 int getBottom() const { return mPrimitiveFields.mBottom; }
John Reckacb6f072014-03-12 16:11:23 -0700423
John Reck79c7de72014-05-23 10:33:31 -0700424 bool setLeftTop(int left, int top) {
425 bool leftResult = setLeft(left);
426 bool topResult = setTop(top);
427 return leftResult || topResult;
John Reckacb6f072014-03-12 16:11:23 -0700428 }
429
John Reck79c7de72014-05-23 10:33:31 -0700430 bool setLeftTopRightBottom(int left, int top, int right, int bottom) {
John Reck1bcacfd2017-11-03 10:12:19 -0700431 if (left != mPrimitiveFields.mLeft || top != mPrimitiveFields.mTop ||
432 right != mPrimitiveFields.mRight || bottom != mPrimitiveFields.mBottom) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700433 mPrimitiveFields.mLeft = left;
434 mPrimitiveFields.mTop = top;
435 mPrimitiveFields.mRight = right;
436 mPrimitiveFields.mBottom = bottom;
437 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
438 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700439 if (!mPrimitiveFields.mPivotExplicitlySet) {
440 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700441 }
John Reck79c7de72014-05-23 10:33:31 -0700442 return true;
John Reckacb6f072014-03-12 16:11:23 -0700443 }
John Reck79c7de72014-05-23 10:33:31 -0700444 return false;
John Reckacb6f072014-03-12 16:11:23 -0700445 }
446
Chris Craika753f4c2014-07-24 12:39:17 -0700447 bool offsetLeftRight(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700448 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700449 mPrimitiveFields.mLeft += offset;
450 mPrimitiveFields.mRight += offset;
John Reck79c7de72014-05-23 10:33:31 -0700451 return true;
John Reckacb6f072014-03-12 16:11:23 -0700452 }
John Reck79c7de72014-05-23 10:33:31 -0700453 return false;
John Reckacb6f072014-03-12 16:11:23 -0700454 }
455
Chris Craika753f4c2014-07-24 12:39:17 -0700456 bool offsetTopBottom(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700457 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700458 mPrimitiveFields.mTop += offset;
459 mPrimitiveFields.mBottom += offset;
John Reck79c7de72014-05-23 10:33:31 -0700460 return true;
John Reckacb6f072014-03-12 16:11:23 -0700461 }
John Reck79c7de72014-05-23 10:33:31 -0700462 return false;
John Reckacb6f072014-03-12 16:11:23 -0700463 }
464
John Reck1bcacfd2017-11-03 10:12:19 -0700465 int getWidth() const { return mPrimitiveFields.mWidth; }
John Reckacb6f072014-03-12 16:11:23 -0700466
John Reck1bcacfd2017-11-03 10:12:19 -0700467 int getHeight() const { return mPrimitiveFields.mHeight; }
John Reckacb6f072014-03-12 16:11:23 -0700468
John Reck1bcacfd2017-11-03 10:12:19 -0700469 const SkMatrix* getAnimationMatrix() const { return mAnimationMatrix; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700470
John Reckf7483e32014-04-11 08:54:47 -0700471 bool hasTransformMatrix() const {
472 return getTransformMatrix() && !getTransformMatrix()->isIdentity();
473 }
474
475 // May only call this if hasTransformMatrix() is true
476 bool isTransformTranslateOnly() const {
477 return getTransformMatrix()->getType() == SkMatrix::kTranslate_Mask;
John Reckd0a0b2a2014-03-20 16:28:56 -0700478 }
479
Chris Craik49e6c7392014-03-31 12:34:11 -0700480 const SkMatrix* getTransformMatrix() const {
John Reckf7483e32014-04-11 08:54:47 -0700481 LOG_ALWAYS_FATAL_IF(mPrimitiveFields.mMatrixOrPivotDirty, "Cannot get a dirty matrix!");
John Reckd0a0b2a2014-03-20 16:28:56 -0700482 return mComputedFields.mTransformMatrix;
483 }
484
John Reck1bcacfd2017-11-03 10:12:19 -0700485 int getClippingFlags() const { return mPrimitiveFields.mClippingFlags; }
Chris Craika753f4c2014-07-24 12:39:17 -0700486
John Reck1bcacfd2017-11-03 10:12:19 -0700487 bool getClipToBounds() const { return mPrimitiveFields.mClippingFlags & CLIP_TO_BOUNDS; }
Chris Craika753f4c2014-07-24 12:39:17 -0700488
John Reck1bcacfd2017-11-03 10:12:19 -0700489 const Rect& getClipBounds() const { return mPrimitiveFields.mClipBounds; }
John Recke248bd12015-08-05 13:53:53 -0700490
Chris Craika753f4c2014-07-24 12:39:17 -0700491 void getClippingRectForFlags(uint32_t flags, Rect* outRect) const {
492 if (flags & CLIP_TO_BOUNDS) {
493 outRect->set(0, 0, getWidth(), getHeight());
494 if (flags & CLIP_TO_CLIP_BOUNDS) {
Chris Craikac02eb92015-10-05 12:23:46 -0700495 outRect->doIntersect(mPrimitiveFields.mClipBounds);
Chris Craika753f4c2014-07-24 12:39:17 -0700496 }
497 } else {
498 outRect->set(mPrimitiveFields.mClipBounds);
499 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700500 }
501
John Reck1bcacfd2017-11-03 10:12:19 -0700502 bool getHasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700503
John Reck1bcacfd2017-11-03 10:12:19 -0700504 const Outline& getOutline() const { return mPrimitiveFields.mOutline; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700505
John Reck1bcacfd2017-11-03 10:12:19 -0700506 const RevealClip& getRevealClip() const { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700507
John Reck1bcacfd2017-11-03 10:12:19 -0700508 bool getProjectBackwards() const { return mPrimitiveFields.mProjectBackwards; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700509
sergeyvc3849aa2016-08-08 13:22:06 -0700510 void debugOutputProperties(std::ostream& output, const int level) const;
John Reckd0a0b2a2014-03-20 16:28:56 -0700511
John Reck25fbb3f2014-06-12 13:46:45 -0700512 void updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700513
John Reck1bcacfd2017-11-03 10:12:19 -0700514 Outline& mutableOutline() { return mPrimitiveFields.mOutline; }
Chris Craikb49f4462014-03-20 12:44:20 -0700515
John Reck1bcacfd2017-11-03 10:12:19 -0700516 RevealClip& mutableRevealClip() { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700517
John Reck1bcacfd2017-11-03 10:12:19 -0700518 const LayerProperties& layerProperties() const { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700519
John Reck1bcacfd2017-11-03 10:12:19 -0700520 LayerProperties& mutateLayerProperties() { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700521
John Reck293e8682014-06-17 10:34:02 -0700522 // Returns true if damage calculations should be clipped to bounds
523 // TODO: Figure out something better for getZ(), as children should still be
524 // clipped to this RP's bounds. But as we will damage -INT_MAX to INT_MAX
525 // for this RP's getZ() anyway, this can be optimized when we have a
526 // Z damage estimate instead of INT_MAX
527 bool getClipDamageToBounds() const {
528 return getClipToBounds() && (getZ() <= 0 || getOutline().isEmpty());
529 }
530
Chris Craik5c75c522014-09-05 14:08:08 -0700531 bool hasShadow() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700532 return getZ() > 0.0f && getOutline().getPath() != nullptr &&
533 getOutline().getAlpha() != 0.0f;
Chris Craik5c75c522014-09-05 14:08:08 -0700534 }
535
John Recke170fb62018-05-07 08:12:07 -0700536 SkColor getSpotShadowColor() const { return mPrimitiveFields.mSpotShadowColor; }
John Reck3c0369b2017-11-13 16:47:35 -0800537
John Reckd8be4a02017-11-17 15:06:24 -0800538 bool setSpotShadowColor(SkColor shadowColor) {
539 return RP_SET(mPrimitiveFields.mSpotShadowColor, shadowColor);
540 }
541
John Recke170fb62018-05-07 08:12:07 -0700542 SkColor getAmbientShadowColor() const { return mPrimitiveFields.mAmbientShadowColor; }
John Reckd8be4a02017-11-17 15:06:24 -0800543
544 bool setAmbientShadowColor(SkColor shadowColor) {
545 return RP_SET(mPrimitiveFields.mAmbientShadowColor, shadowColor);
John Reck3c0369b2017-11-13 16:47:35 -0800546 }
547
Chris Craik9fded232015-11-11 16:42:34 -0800548 bool fitsOnLayer() const {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100549#ifdef __ANDROID__ // Layoutlib does not support device info
Chris Craik76caecf2015-11-02 19:17:45 -0800550 const DeviceInfo* deviceInfo = DeviceInfo::get();
John Reck1bcacfd2017-11-03 10:12:19 -0700551 return mPrimitiveFields.mWidth <= deviceInfo->maxTextureSize() &&
552 mPrimitiveFields.mHeight <= deviceInfo->maxTextureSize();
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100553#else
554 return mPrimitiveFields.mWidth <= 4096 && mPrimitiveFields.mHeight <= 4096;
555#endif
Chris Craik9fded232015-11-11 16:42:34 -0800556 }
557
558 bool promotedToLayer() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700559 return mLayerProperties.mType == LayerType::None && fitsOnLayer() &&
Nader Jawad6701a602021-02-23 18:14:22 -0800560 (mComputedFields.mNeedLayerForFunctors || mLayerProperties.mImageFilter != nullptr ||
John Reck09d9cca2021-05-10 19:34:58 -0400561 mLayerProperties.getStretchEffect().requiresLayer() ||
John Reck1bcacfd2017-11-03 10:12:19 -0700562 (!MathUtils::isZero(mPrimitiveFields.mAlpha) && mPrimitiveFields.mAlpha < 1 &&
563 mPrimitiveFields.mHasOverlappingRendering));
Chris Craik1a0808e2015-05-13 16:33:04 -0700564 }
565
566 LayerType effectiveLayerType() const {
Chris Craika766cb22015-06-08 16:49:43 -0700567 return CC_UNLIKELY(promotedToLayer()) ? LayerType::RenderLayer : mLayerProperties.mType;
Chris Craik856f0cc2015-04-21 15:13:29 -0700568 }
569
John Reck1423e132018-09-21 14:30:19 -0700570 bool setAllowForceDark(bool allow) {
571 return RP_SET(mPrimitiveFields.mAllowForceDark, allow);
572 }
573
574 bool getAllowForceDark() const {
575 return mPrimitiveFields.mAllowForceDark;
576 }
577
John Reckacb6f072014-03-12 16:11:23 -0700578private:
John Reckacb6f072014-03-12 16:11:23 -0700579 // Rendering properties
John Reckd0a0b2a2014-03-20 16:28:56 -0700580 struct PrimitiveFields {
John Recke248bd12015-08-05 13:53:53 -0700581 int mLeft = 0, mTop = 0, mRight = 0, mBottom = 0;
582 int mWidth = 0, mHeight = 0;
583 int mClippingFlags = CLIP_TO_BOUNDS;
John Reckd8be4a02017-11-17 15:06:24 -0800584 SkColor mSpotShadowColor = SK_ColorBLACK;
585 SkColor mAmbientShadowColor = SK_ColorBLACK;
John Recke248bd12015-08-05 13:53:53 -0700586 float mAlpha = 1;
587 float mTranslationX = 0, mTranslationY = 0, mTranslationZ = 0;
588 float mElevation = 0;
589 float mRotation = 0, mRotationX = 0, mRotationY = 0;
590 float mScaleX = 1, mScaleY = 1;
591 float mPivotX = 0, mPivotY = 0;
592 bool mHasOverlappingRendering = false;
593 bool mPivotExplicitlySet = false;
594 bool mMatrixOrPivotDirty = false;
595 bool mProjectBackwards = false;
596 bool mProjectionReceiver = false;
John Reck1423e132018-09-21 14:30:19 -0700597 bool mAllowForceDark = true;
Stan Ilievf09ee582018-11-06 17:35:50 -0500598 bool mClipMayBeComplex = false;
John Recke248bd12015-08-05 13:53:53 -0700599 Rect mClipBounds;
John Reckd0a0b2a2014-03-20 16:28:56 -0700600 Outline mOutline;
Chris Craik8c271ca2014-03-25 10:33:01 -0700601 RevealClip mRevealClip;
John Reckd0a0b2a2014-03-20 16:28:56 -0700602 } mPrimitiveFields;
603
John Reckacb6f072014-03-12 16:11:23 -0700604 SkMatrix* mStaticMatrix;
605 SkMatrix* mAnimationMatrix;
John Reck25fbb3f2014-06-12 13:46:45 -0700606 LayerProperties mLayerProperties;
John Reckacb6f072014-03-12 16:11:23 -0700607
John Reckd0a0b2a2014-03-20 16:28:56 -0700608 /**
609 * These fields are all generated from other properties and are not set directly.
610 */
611 struct ComputedFields {
612 ComputedFields();
613 ~ComputedFields();
614
615 /**
616 * Stores the total transformation of the DisplayList based upon its scalar
617 * translate/rotate/scale properties.
618 *
Chris Craik49e6c7392014-03-31 12:34:11 -0700619 * In the common translation-only case, the matrix isn't necessarily allocated,
620 * and the mTranslation properties are used directly.
John Reckd0a0b2a2014-03-20 16:28:56 -0700621 */
Chris Craik49e6c7392014-03-31 12:34:11 -0700622 SkMatrix* mTransformMatrix;
623
624 Sk3DView mTransformCamera;
Chris Craika766cb22015-06-08 16:49:43 -0700625
626 // Force layer on for functors to enable render features they don't yet support (clipping)
627 bool mNeedLayerForFunctors = false;
John Reckd0a0b2a2014-03-20 16:28:56 -0700628 } mComputedFields;
John Reckacb6f072014-03-12 16:11:23 -0700629};
630
631} /* namespace uirenderer */
632} /* namespace android */