blob: 4dc57004e401caa94153d8d5ec498aff50956c54 [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
Jerome Gaillard8f6d6e02024-02-26 18:56:00 +000019#include <SkBlendMode.h>
20#include <SkCamera.h>
21#include <SkColor.h>
22#include <SkImageFilter.h>
23#include <SkMatrix.h>
24#include <SkRegion.h>
25#include <androidfw/ResourceTypes.h>
26#include <cutils/compiler.h>
27#include <stddef.h>
28#include <utils/Log.h>
Alec Mouri22d753f2019-09-05 17:11:45 -070029
Jerome Gaillard8f6d6e02024-02-26 18:56:00 +000030#include <algorithm>
31#include <ostream>
32#include <vector>
33
34#include "DeviceInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070035#include "Outline.h"
Chris Craik76caecf2015-11-02 19:17:45 -080036#include "Rect.h"
37#include "RevealClip.h"
John Reck5cb290b2021-02-01 13:47:31 -050038#include "effects/StretchEffect.h"
Chris Craik76caecf2015-11-02 19:17:45 -080039#include "utils/MathUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070040#include "utils/PaintUtils.h"
John Reckacb6f072014-03-12 16:11:23 -070041
John Reckacb6f072014-03-12 16:11:23 -070042class SkBitmap;
John Reck25fbb3f2014-06-12 13:46:45 -070043class SkColorFilter;
John Reckacb6f072014-03-12 16:11:23 -070044class SkPaint;
John Reckacb6f072014-03-12 16:11:23 -070045
46namespace android {
47namespace uirenderer {
48
49class Matrix4;
50class RenderNode;
John Reck25fbb3f2014-06-12 13:46:45 -070051class RenderProperties;
John Reckacb6f072014-03-12 16:11:23 -070052
John Reck79c7de72014-05-23 10:33:31 -070053// The __VA_ARGS__ will be executed if a & b are not equal
Chih-Hung Hsiehcef190d2016-05-19 15:25:50 -070054#define RP_SET(a, b, ...) ((a) != (b) ? ((a) = (b), ##__VA_ARGS__, true) : false)
John Reck79c7de72014-05-23 10:33:31 -070055#define RP_SET_AND_DIRTY(a, b) RP_SET(a, b, mPrimitiveFields.mMatrixOrPivotDirty = true)
56
John Reck25fbb3f2014-06-12 13:46:45 -070057// Keep in sync with View.java:LAYER_TYPE_*
Chris Craik182952f2015-03-09 14:17:29 -070058enum class LayerType {
59 None = 0,
Derek Sollenberger52230bc2018-02-16 12:24:13 -050060 // We cannot build the software layer directly (must be done at record time) and all management
61 // of software layers is handled in Java.
Chris Craik182952f2015-03-09 14:17:29 -070062 Software = 1,
63 RenderLayer = 2,
John Reck25fbb3f2014-06-12 13:46:45 -070064};
65
Chris Craika753f4c2014-07-24 12:39:17 -070066enum ClippingFlags {
John Reck1bcacfd2017-11-03 10:12:19 -070067 CLIP_TO_BOUNDS = 0x1 << 0,
Chris Craika753f4c2014-07-24 12:39:17 -070068 CLIP_TO_CLIP_BOUNDS = 0x1 << 1,
69};
70
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050071class LayerProperties {
John Reck25fbb3f2014-06-12 13:46:45 -070072public:
73 bool setType(LayerType type) {
74 if (RP_SET(mType, type)) {
75 reset();
76 return true;
77 }
78 return false;
79 }
80
John Reck1bcacfd2017-11-03 10:12:19 -070081 bool setOpaque(bool opaque) { return RP_SET(mOpaque, opaque); }
John Reck25fbb3f2014-06-12 13:46:45 -070082
John Reck1bcacfd2017-11-03 10:12:19 -070083 bool opaque() const { return mOpaque; }
John Reck25fbb3f2014-06-12 13:46:45 -070084
John Reck1bcacfd2017-11-03 10:12:19 -070085 bool setAlpha(uint8_t alpha) { return RP_SET(mAlpha, alpha); }
John Reck25fbb3f2014-06-12 13:46:45 -070086
John Reck1bcacfd2017-11-03 10:12:19 -070087 uint8_t alpha() const { return mAlpha; }
John Reck25fbb3f2014-06-12 13:46:45 -070088
John Reck1bcacfd2017-11-03 10:12:19 -070089 bool setXferMode(SkBlendMode mode) { return RP_SET(mMode, mode); }
John Reck25fbb3f2014-06-12 13:46:45 -070090
John Reck1bcacfd2017-11-03 10:12:19 -070091 SkBlendMode xferMode() const { return mMode; }
John Reck25fbb3f2014-06-12 13:46:45 -070092
Ben Wagnerc1a8a462018-07-12 12:41:28 -040093 SkColorFilter* getColorFilter() const { return mColorFilter.get(); }
John Reck25fbb3f2014-06-12 13:46:45 -070094
Nader Jawad390d6e82020-09-24 21:35:03 -070095 bool setImageFilter(SkImageFilter* imageFilter);
96
Dongya Jiange2b99382022-02-28 21:35:57 +080097 bool setBackdropImageFilter(SkImageFilter* imageFilter);
98
Nader Jawad390d6e82020-09-24 21:35:03 -070099 SkImageFilter* getImageFilter() const { return mImageFilter.get(); }
100
Dongya Jiange2b99382022-02-28 21:35:57 +0800101 SkImageFilter* getBackdropImageFilter() const { return mBackdropImageFilter.get(); }
102
John Reck5cb290b2021-02-01 13:47:31 -0500103 const StretchEffect& getStretchEffect() const { return mStretchEffect; }
104
105 StretchEffect& mutableStretchEffect() { return mStretchEffect; }
106
John Reck25fbb3f2014-06-12 13:46:45 -0700107 // Sets alpha, xfermode, and colorfilter from an SkPaint
108 // paint may be NULL, in which case defaults will be set
109 bool setFromPaint(const SkPaint* paint);
110
John Reck1bcacfd2017-11-03 10:12:19 -0700111 bool needsBlending() const { return !opaque() || alpha() < 255; }
John Reck25fbb3f2014-06-12 13:46:45 -0700112
113 LayerProperties& operator=(const LayerProperties& other);
114
John Reck470a9192018-12-17 10:55:54 -0800115 // Strongly recommend using effectiveLayerType instead
116 LayerType type() const { return mType; }
117
John Reck25fbb3f2014-06-12 13:46:45 -0700118private:
119 LayerProperties();
120 ~LayerProperties();
121 void reset();
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400122 bool setColorFilter(SkColorFilter* filter);
John Reck25fbb3f2014-06-12 13:46:45 -0700123
124 friend class RenderProperties;
125
Chris Craik182952f2015-03-09 14:17:29 -0700126 LayerType mType = LayerType::None;
John Reck25fbb3f2014-06-12 13:46:45 -0700127 // Whether or not that Layer's content is opaque, doesn't include alpha
128 bool mOpaque;
129 uint8_t mAlpha;
Mike Reed260ab722016-10-07 15:59:20 -0400130 SkBlendMode mMode;
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400131 sk_sp<SkColorFilter> mColorFilter;
Nader Jawad390d6e82020-09-24 21:35:03 -0700132 sk_sp<SkImageFilter> mImageFilter;
Dongya Jiange2b99382022-02-28 21:35:57 +0800133 sk_sp<SkImageFilter> mBackdropImageFilter;
John Reck5cb290b2021-02-01 13:47:31 -0500134 StretchEffect mStretchEffect;
John Reck25fbb3f2014-06-12 13:46:45 -0700135};
136
John Reckacb6f072014-03-12 16:11:23 -0700137/*
138 * Data structure that holds the properties for a RenderNode
139 */
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -0500140class RenderProperties {
John Reckacb6f072014-03-12 16:11:23 -0700141public:
142 RenderProperties();
143 virtual ~RenderProperties();
144
Chris Craika753f4c2014-07-24 12:39:17 -0700145 static bool setFlag(int flag, bool newValue, int* outFlags) {
146 if (newValue) {
147 if (!(flag & *outFlags)) {
148 *outFlags |= flag;
149 return true;
150 }
151 return false;
152 } else {
153 if (flag & *outFlags) {
154 *outFlags &= ~flag;
155 return true;
156 }
157 return false;
158 }
159 }
160
Chris Craika766cb22015-06-08 16:49:43 -0700161 /**
162 * Set internal layer state based on whether this layer
163 *
164 * Additionally, returns true if child RenderNodes with functors will need to use a layer
165 * to support clipping.
166 */
167 bool prepareForFunctorPresence(bool willHaveFunctor, bool ancestorDictatesFunctorsNeedLayer) {
168 // parent may have already dictated that a descendant layer is needed
John Reck1bcacfd2017-11-03 10:12:19 -0700169 bool functorsNeedLayer =
Chet Haase33c1ea72021-09-27 20:56:08 +0000170 ancestorDictatesFunctorsNeedLayer ||
171 CC_UNLIKELY(isClipMayBeComplex())
Chris Craika766cb22015-06-08 16:49:43 -0700172
173 // Round rect clipping forces layer for functors
Chet Haase33c1ea72021-09-27 20:56:08 +0000174 || CC_UNLIKELY(getOutline().willComplexClip()) ||
John Reck1bcacfd2017-11-03 10:12:19 -0700175 CC_UNLIKELY(getRevealClip().willClip())
Chris Craika766cb22015-06-08 16:49:43 -0700176
177 // Complex matrices forces layer, due to stencil clipping
John Reck1bcacfd2017-11-03 10:12:19 -0700178 || CC_UNLIKELY(getTransformMatrix() && !getTransformMatrix()->isScaleTranslate()) ||
179 CC_UNLIKELY(getAnimationMatrix() && !getAnimationMatrix()->isScaleTranslate()) ||
180 CC_UNLIKELY(getStaticMatrix() && !getStaticMatrix()->isScaleTranslate());
Chris Craika766cb22015-06-08 16:49:43 -0700181
182 mComputedFields.mNeedLayerForFunctors = (willHaveFunctor && functorsNeedLayer);
183
184 // If on a layer, will have consumed the need for isolating functors from stencil.
185 // Thus, it's safe to reset the flag until some descendent sets it.
186 return CC_LIKELY(effectiveLayerType() == LayerType::None) && functorsNeedLayer;
187 }
188
John Reckd0a0b2a2014-03-20 16:28:56 -0700189 RenderProperties& operator=(const RenderProperties& other);
190
John Reck79c7de72014-05-23 10:33:31 -0700191 bool setClipToBounds(bool clipToBounds) {
Chris Craika753f4c2014-07-24 12:39:17 -0700192 return setFlag(CLIP_TO_BOUNDS, clipToBounds, &mPrimitiveFields.mClippingFlags);
193 }
194
195 bool setClipBounds(const Rect& clipBounds) {
196 bool ret = setFlag(CLIP_TO_CLIP_BOUNDS, true, &mPrimitiveFields.mClippingFlags);
197 return RP_SET(mPrimitiveFields.mClipBounds, clipBounds) || ret;
198 }
199
200 bool setClipBoundsEmpty() {
201 return setFlag(CLIP_TO_CLIP_BOUNDS, false, &mPrimitiveFields.mClippingFlags);
John Reckacb6f072014-03-12 16:11:23 -0700202 }
203
John Reck79c7de72014-05-23 10:33:31 -0700204 bool setProjectBackwards(bool shouldProject) {
205 return RP_SET(mPrimitiveFields.mProjectBackwards, shouldProject);
John Reckacb6f072014-03-12 16:11:23 -0700206 }
207
Chris Craik6fe991e52015-10-20 09:39:42 -0700208 bool setProjectionReceiver(bool shouldReceive) {
209 return RP_SET(mPrimitiveFields.mProjectionReceiver, shouldReceive);
John Reckacb6f072014-03-12 16:11:23 -0700210 }
211
John Reck1bcacfd2017-11-03 10:12:19 -0700212 bool isProjectionReceiver() const { return mPrimitiveFields.mProjectionReceiver; }
John Reckacb6f072014-03-12 16:11:23 -0700213
Stan Ilievf09ee582018-11-06 17:35:50 -0500214 bool setClipMayBeComplex(bool isClipMayBeComplex) {
215 return RP_SET(mPrimitiveFields.mClipMayBeComplex, isClipMayBeComplex);
216 }
217
218 bool isClipMayBeComplex() const { return mPrimitiveFields.mClipMayBeComplex; }
219
John Reck79c7de72014-05-23 10:33:31 -0700220 bool setStaticMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700221 delete mStaticMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -0700222 if (matrix) {
223 mStaticMatrix = new SkMatrix(*matrix);
224 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800225 mStaticMatrix = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700226 }
John Reck79c7de72014-05-23 10:33:31 -0700227 return true;
John Reckacb6f072014-03-12 16:11:23 -0700228 }
229
230 // Can return NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700231 const SkMatrix* getStaticMatrix() const { return mStaticMatrix; }
John Reckacb6f072014-03-12 16:11:23 -0700232
John Reck79c7de72014-05-23 10:33:31 -0700233 bool setAnimationMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700234 delete mAnimationMatrix;
235 if (matrix) {
236 mAnimationMatrix = new SkMatrix(*matrix);
237 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800238 mAnimationMatrix = nullptr;
John Reckacb6f072014-03-12 16:11:23 -0700239 }
John Reck79c7de72014-05-23 10:33:31 -0700240 return true;
John Reckacb6f072014-03-12 16:11:23 -0700241 }
242
John Reck79c7de72014-05-23 10:33:31 -0700243 bool setAlpha(float alpha) {
John Reck3b52c032014-08-06 10:19:32 -0700244 alpha = MathUtils::clampAlpha(alpha);
John Reck79c7de72014-05-23 10:33:31 -0700245 return RP_SET(mPrimitiveFields.mAlpha, alpha);
John Reckacb6f072014-03-12 16:11:23 -0700246 }
247
John Reck1bcacfd2017-11-03 10:12:19 -0700248 float getAlpha() const { return mPrimitiveFields.mAlpha; }
John Reckacb6f072014-03-12 16:11:23 -0700249
John Reck79c7de72014-05-23 10:33:31 -0700250 bool setHasOverlappingRendering(bool hasOverlappingRendering) {
251 return RP_SET(mPrimitiveFields.mHasOverlappingRendering, hasOverlappingRendering);
John Reckacb6f072014-03-12 16:11:23 -0700252 }
253
John Reck1bcacfd2017-11-03 10:12:19 -0700254 bool hasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckacb6f072014-03-12 16:11:23 -0700255
John Reck79c7de72014-05-23 10:33:31 -0700256 bool setElevation(float elevation) {
257 return RP_SET(mPrimitiveFields.mElevation, elevation);
258 // Don't dirty matrix/pivot, since they don't respect Z
Chris Craikcc39e162014-04-25 18:34:11 -0700259 }
260
John Reck1bcacfd2017-11-03 10:12:19 -0700261 float getElevation() const { return mPrimitiveFields.mElevation; }
Chris Craikcc39e162014-04-25 18:34:11 -0700262
John Reck79c7de72014-05-23 10:33:31 -0700263 bool setTranslationX(float translationX) {
264 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationX, translationX);
John Reckacb6f072014-03-12 16:11:23 -0700265 }
266
John Reck1bcacfd2017-11-03 10:12:19 -0700267 float getTranslationX() const { return mPrimitiveFields.mTranslationX; }
John Reckacb6f072014-03-12 16:11:23 -0700268
John Reck79c7de72014-05-23 10:33:31 -0700269 bool setTranslationY(float translationY) {
270 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationY, translationY);
John Reckacb6f072014-03-12 16:11:23 -0700271 }
272
John Reck1bcacfd2017-11-03 10:12:19 -0700273 float getTranslationY() const { return mPrimitiveFields.mTranslationY; }
John Reckacb6f072014-03-12 16:11:23 -0700274
John Reck79c7de72014-05-23 10:33:31 -0700275 bool setTranslationZ(float translationZ) {
276 return RP_SET(mPrimitiveFields.mTranslationZ, translationZ);
277 // mMatrixOrPivotDirty not set, since matrix doesn't respect Z
John Reckacb6f072014-03-12 16:11:23 -0700278 }
279
John Reck1bcacfd2017-11-03 10:12:19 -0700280 float getTranslationZ() const { return mPrimitiveFields.mTranslationZ; }
John Reckacb6f072014-03-12 16:11:23 -0700281
John Recke45b1fd2014-04-15 09:50:16 -0700282 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700283 bool setX(float value) { return setTranslationX(value - getLeft()); }
John Recke45b1fd2014-04-15 09:50:16 -0700284
285 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700286 float getX() const { return getLeft() + getTranslationX(); }
John Recke45b1fd2014-04-15 09:50:16 -0700287
288 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700289 bool setY(float value) { return setTranslationY(value - getTop()); }
John Recke45b1fd2014-04-15 09:50:16 -0700290
291 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700292 float getY() const { return getTop() + getTranslationY(); }
John Recke45b1fd2014-04-15 09:50:16 -0700293
294 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700295 bool setZ(float value) { return setTranslationZ(value - getElevation()); }
John Recke45b1fd2014-04-15 09:50:16 -0700296
John Reck1bcacfd2017-11-03 10:12:19 -0700297 float getZ() const { return getElevation() + getTranslationZ(); }
Chris Craikcc39e162014-04-25 18:34:11 -0700298
John Reck79c7de72014-05-23 10:33:31 -0700299 bool setRotation(float rotation) {
300 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotation, rotation);
John Reckacb6f072014-03-12 16:11:23 -0700301 }
302
John Reck1bcacfd2017-11-03 10:12:19 -0700303 float getRotation() const { return mPrimitiveFields.mRotation; }
John Reckacb6f072014-03-12 16:11:23 -0700304
John Reck79c7de72014-05-23 10:33:31 -0700305 bool setRotationX(float rotationX) {
306 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationX, rotationX);
John Reckacb6f072014-03-12 16:11:23 -0700307 }
308
John Reck1bcacfd2017-11-03 10:12:19 -0700309 float getRotationX() const { return mPrimitiveFields.mRotationX; }
John Reckacb6f072014-03-12 16:11:23 -0700310
John Reck79c7de72014-05-23 10:33:31 -0700311 bool setRotationY(float rotationY) {
312 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationY, rotationY);
John Reckacb6f072014-03-12 16:11:23 -0700313 }
314
John Reck1bcacfd2017-11-03 10:12:19 -0700315 float getRotationY() const { return mPrimitiveFields.mRotationY; }
John Reckacb6f072014-03-12 16:11:23 -0700316
John Reck1bcacfd2017-11-03 10:12:19 -0700317 bool setScaleX(float scaleX) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleX, scaleX); }
John Reckacb6f072014-03-12 16:11:23 -0700318
John Reck1bcacfd2017-11-03 10:12:19 -0700319 float getScaleX() const { return mPrimitiveFields.mScaleX; }
John Reckacb6f072014-03-12 16:11:23 -0700320
John Reck1bcacfd2017-11-03 10:12:19 -0700321 bool setScaleY(float scaleY) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleY, scaleY); }
John Reckacb6f072014-03-12 16:11:23 -0700322
John Reck1bcacfd2017-11-03 10:12:19 -0700323 float getScaleY() const { return mPrimitiveFields.mScaleY; }
John Reckacb6f072014-03-12 16:11:23 -0700324
John Reck79c7de72014-05-23 10:33:31 -0700325 bool setPivotX(float pivotX) {
John Reck1bcacfd2017-11-03 10:12:19 -0700326 if (RP_SET(mPrimitiveFields.mPivotX, pivotX) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700327 mPrimitiveFields.mMatrixOrPivotDirty = true;
328 mPrimitiveFields.mPivotExplicitlySet = true;
329 return true;
330 }
331 return false;
John Reckacb6f072014-03-12 16:11:23 -0700332 }
333
John Reckd0a0b2a2014-03-20 16:28:56 -0700334 /* Note that getPivotX and getPivotY are adjusted by updateMatrix(),
John Reck79c7de72014-05-23 10:33:31 -0700335 * so the value returned may be stale if the RenderProperties has been
336 * modified since the last call to updateMatrix()
John Reckd0a0b2a2014-03-20 16:28:56 -0700337 */
John Reck1bcacfd2017-11-03 10:12:19 -0700338 float getPivotX() const { return mPrimitiveFields.mPivotX; }
John Reckacb6f072014-03-12 16:11:23 -0700339
John Reck79c7de72014-05-23 10:33:31 -0700340 bool setPivotY(float pivotY) {
John Reck1bcacfd2017-11-03 10:12:19 -0700341 if (RP_SET(mPrimitiveFields.mPivotY, pivotY) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700342 mPrimitiveFields.mMatrixOrPivotDirty = true;
343 mPrimitiveFields.mPivotExplicitlySet = true;
344 return true;
345 }
346 return false;
John Reckacb6f072014-03-12 16:11:23 -0700347 }
348
John Reck1bcacfd2017-11-03 10:12:19 -0700349 float getPivotY() const { return mPrimitiveFields.mPivotY; }
John Reckacb6f072014-03-12 16:11:23 -0700350
John Reck1bcacfd2017-11-03 10:12:19 -0700351 bool isPivotExplicitlySet() const { return mPrimitiveFields.mPivotExplicitlySet; }
Chris Craik49e6c7392014-03-31 12:34:11 -0700352
John Recke170fb62018-05-07 08:12:07 -0700353 bool resetPivot() { return RP_SET_AND_DIRTY(mPrimitiveFields.mPivotExplicitlySet, false); }
John Reck8686e1f2018-03-21 16:31:21 -0700354
John Reck79c7de72014-05-23 10:33:31 -0700355 bool setCameraDistance(float distance) {
Chris Craik49e6c7392014-03-31 12:34:11 -0700356 if (distance != getCameraDistance()) {
John Reckf7483e32014-04-11 08:54:47 -0700357 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -0700358 mComputedFields.mTransformCamera.setCameraLocation(0, 0, distance);
John Reck79c7de72014-05-23 10:33:31 -0700359 return true;
John Reckacb6f072014-03-12 16:11:23 -0700360 }
John Reck79c7de72014-05-23 10:33:31 -0700361 return false;
John Reckacb6f072014-03-12 16:11:23 -0700362 }
363
364 float getCameraDistance() const {
Chris Craik49e6c7392014-03-31 12:34:11 -0700365 // TODO: update getCameraLocationZ() to be const
366 return const_cast<Sk3DView*>(&mComputedFields.mTransformCamera)->getCameraLocationZ();
John Reckacb6f072014-03-12 16:11:23 -0700367 }
368
John Reck79c7de72014-05-23 10:33:31 -0700369 bool setLeft(int left) {
370 if (RP_SET(mPrimitiveFields.mLeft, left)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700371 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700372 if (!mPrimitiveFields.mPivotExplicitlySet) {
373 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700374 }
John Reck79c7de72014-05-23 10:33:31 -0700375 return true;
John Reckacb6f072014-03-12 16:11:23 -0700376 }
John Reck79c7de72014-05-23 10:33:31 -0700377 return false;
John Reckacb6f072014-03-12 16:11:23 -0700378 }
379
John Reck1bcacfd2017-11-03 10:12:19 -0700380 int getLeft() const { return mPrimitiveFields.mLeft; }
John Reckacb6f072014-03-12 16:11:23 -0700381
John Reck79c7de72014-05-23 10:33:31 -0700382 bool setTop(int top) {
383 if (RP_SET(mPrimitiveFields.mTop, top)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700384 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700385 if (!mPrimitiveFields.mPivotExplicitlySet) {
386 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700387 }
John Reck79c7de72014-05-23 10:33:31 -0700388 return true;
John Reckacb6f072014-03-12 16:11:23 -0700389 }
John Reck79c7de72014-05-23 10:33:31 -0700390 return false;
John Reckacb6f072014-03-12 16:11:23 -0700391 }
392
John Reck1bcacfd2017-11-03 10:12:19 -0700393 int getTop() const { return mPrimitiveFields.mTop; }
John Reckacb6f072014-03-12 16:11:23 -0700394
John Reck79c7de72014-05-23 10:33:31 -0700395 bool setRight(int right) {
396 if (RP_SET(mPrimitiveFields.mRight, right)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700397 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700398 if (!mPrimitiveFields.mPivotExplicitlySet) {
399 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700400 }
John Reck79c7de72014-05-23 10:33:31 -0700401 return true;
John Reckacb6f072014-03-12 16:11:23 -0700402 }
John Reck79c7de72014-05-23 10:33:31 -0700403 return false;
John Reckacb6f072014-03-12 16:11:23 -0700404 }
405
John Reck1bcacfd2017-11-03 10:12:19 -0700406 int getRight() const { return mPrimitiveFields.mRight; }
John Reckacb6f072014-03-12 16:11:23 -0700407
John Reck79c7de72014-05-23 10:33:31 -0700408 bool setBottom(int bottom) {
409 if (RP_SET(mPrimitiveFields.mBottom, bottom)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700410 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700411 if (!mPrimitiveFields.mPivotExplicitlySet) {
412 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700413 }
John Reck79c7de72014-05-23 10:33:31 -0700414 return true;
John Reckacb6f072014-03-12 16:11:23 -0700415 }
John Reck79c7de72014-05-23 10:33:31 -0700416 return false;
John Reckacb6f072014-03-12 16:11:23 -0700417 }
418
John Reck1bcacfd2017-11-03 10:12:19 -0700419 int getBottom() const { return mPrimitiveFields.mBottom; }
John Reckacb6f072014-03-12 16:11:23 -0700420
John Reck79c7de72014-05-23 10:33:31 -0700421 bool setLeftTop(int left, int top) {
422 bool leftResult = setLeft(left);
423 bool topResult = setTop(top);
424 return leftResult || topResult;
John Reckacb6f072014-03-12 16:11:23 -0700425 }
426
John Reck79c7de72014-05-23 10:33:31 -0700427 bool setLeftTopRightBottom(int left, int top, int right, int bottom) {
John Reck1bcacfd2017-11-03 10:12:19 -0700428 if (left != mPrimitiveFields.mLeft || top != mPrimitiveFields.mTop ||
429 right != mPrimitiveFields.mRight || bottom != mPrimitiveFields.mBottom) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700430 mPrimitiveFields.mLeft = left;
431 mPrimitiveFields.mTop = top;
432 mPrimitiveFields.mRight = right;
433 mPrimitiveFields.mBottom = bottom;
434 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
435 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700436 if (!mPrimitiveFields.mPivotExplicitlySet) {
437 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700438 }
John Reck79c7de72014-05-23 10:33:31 -0700439 return true;
John Reckacb6f072014-03-12 16:11:23 -0700440 }
John Reck79c7de72014-05-23 10:33:31 -0700441 return false;
John Reckacb6f072014-03-12 16:11:23 -0700442 }
443
Chris Craika753f4c2014-07-24 12:39:17 -0700444 bool offsetLeftRight(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700445 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700446 mPrimitiveFields.mLeft += offset;
447 mPrimitiveFields.mRight += offset;
John Reck79c7de72014-05-23 10:33:31 -0700448 return true;
John Reckacb6f072014-03-12 16:11:23 -0700449 }
John Reck79c7de72014-05-23 10:33:31 -0700450 return false;
John Reckacb6f072014-03-12 16:11:23 -0700451 }
452
Chris Craika753f4c2014-07-24 12:39:17 -0700453 bool offsetTopBottom(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700454 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700455 mPrimitiveFields.mTop += offset;
456 mPrimitiveFields.mBottom += offset;
John Reck79c7de72014-05-23 10:33:31 -0700457 return true;
John Reckacb6f072014-03-12 16:11:23 -0700458 }
John Reck79c7de72014-05-23 10:33:31 -0700459 return false;
John Reckacb6f072014-03-12 16:11:23 -0700460 }
461
John Reck1bcacfd2017-11-03 10:12:19 -0700462 int getWidth() const { return mPrimitiveFields.mWidth; }
John Reckacb6f072014-03-12 16:11:23 -0700463
John Reck1bcacfd2017-11-03 10:12:19 -0700464 int getHeight() const { return mPrimitiveFields.mHeight; }
John Reckacb6f072014-03-12 16:11:23 -0700465
John Reck1bcacfd2017-11-03 10:12:19 -0700466 const SkMatrix* getAnimationMatrix() const { return mAnimationMatrix; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700467
John Reckf7483e32014-04-11 08:54:47 -0700468 bool hasTransformMatrix() const {
469 return getTransformMatrix() && !getTransformMatrix()->isIdentity();
470 }
471
472 // May only call this if hasTransformMatrix() is true
473 bool isTransformTranslateOnly() const {
474 return getTransformMatrix()->getType() == SkMatrix::kTranslate_Mask;
John Reckd0a0b2a2014-03-20 16:28:56 -0700475 }
476
Chris Craik49e6c7392014-03-31 12:34:11 -0700477 const SkMatrix* getTransformMatrix() const {
John Reckf7483e32014-04-11 08:54:47 -0700478 LOG_ALWAYS_FATAL_IF(mPrimitiveFields.mMatrixOrPivotDirty, "Cannot get a dirty matrix!");
John Reckd0a0b2a2014-03-20 16:28:56 -0700479 return mComputedFields.mTransformMatrix;
480 }
481
John Reck1bcacfd2017-11-03 10:12:19 -0700482 int getClippingFlags() const { return mPrimitiveFields.mClippingFlags; }
Chris Craika753f4c2014-07-24 12:39:17 -0700483
John Reck1bcacfd2017-11-03 10:12:19 -0700484 bool getClipToBounds() const { return mPrimitiveFields.mClippingFlags & CLIP_TO_BOUNDS; }
Chris Craika753f4c2014-07-24 12:39:17 -0700485
John Reck1bcacfd2017-11-03 10:12:19 -0700486 const Rect& getClipBounds() const { return mPrimitiveFields.mClipBounds; }
John Recke248bd12015-08-05 13:53:53 -0700487
Chris Craika753f4c2014-07-24 12:39:17 -0700488 void getClippingRectForFlags(uint32_t flags, Rect* outRect) const {
489 if (flags & CLIP_TO_BOUNDS) {
490 outRect->set(0, 0, getWidth(), getHeight());
491 if (flags & CLIP_TO_CLIP_BOUNDS) {
Chris Craikac02eb92015-10-05 12:23:46 -0700492 outRect->doIntersect(mPrimitiveFields.mClipBounds);
Chris Craika753f4c2014-07-24 12:39:17 -0700493 }
494 } else {
495 outRect->set(mPrimitiveFields.mClipBounds);
496 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700497 }
498
John Reck1bcacfd2017-11-03 10:12:19 -0700499 bool getHasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700500
John Reck1bcacfd2017-11-03 10:12:19 -0700501 const Outline& getOutline() const { return mPrimitiveFields.mOutline; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700502
John Reck1bcacfd2017-11-03 10:12:19 -0700503 const RevealClip& getRevealClip() const { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700504
John Reck1bcacfd2017-11-03 10:12:19 -0700505 bool getProjectBackwards() const { return mPrimitiveFields.mProjectBackwards; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700506
sergeyvc3849aa2016-08-08 13:22:06 -0700507 void debugOutputProperties(std::ostream& output, const int level) const;
John Reckd0a0b2a2014-03-20 16:28:56 -0700508
John Reck25fbb3f2014-06-12 13:46:45 -0700509 void updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700510
John Reck1bcacfd2017-11-03 10:12:19 -0700511 Outline& mutableOutline() { return mPrimitiveFields.mOutline; }
Chris Craikb49f4462014-03-20 12:44:20 -0700512
John Reck1bcacfd2017-11-03 10:12:19 -0700513 RevealClip& mutableRevealClip() { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700514
John Reck1bcacfd2017-11-03 10:12:19 -0700515 const LayerProperties& layerProperties() const { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700516
John Reck1bcacfd2017-11-03 10:12:19 -0700517 LayerProperties& mutateLayerProperties() { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700518
John Reck293e8682014-06-17 10:34:02 -0700519 // Returns true if damage calculations should be clipped to bounds
520 // TODO: Figure out something better for getZ(), as children should still be
521 // clipped to this RP's bounds. But as we will damage -INT_MAX to INT_MAX
522 // for this RP's getZ() anyway, this can be optimized when we have a
523 // Z damage estimate instead of INT_MAX
524 bool getClipDamageToBounds() const {
525 return getClipToBounds() && (getZ() <= 0 || getOutline().isEmpty());
526 }
527
Chris Craik5c75c522014-09-05 14:08:08 -0700528 bool hasShadow() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700529 return getZ() > 0.0f && getOutline().getPath() != nullptr &&
530 getOutline().getAlpha() != 0.0f;
Chris Craik5c75c522014-09-05 14:08:08 -0700531 }
532
John Recke170fb62018-05-07 08:12:07 -0700533 SkColor getSpotShadowColor() const { return mPrimitiveFields.mSpotShadowColor; }
John Reck3c0369b2017-11-13 16:47:35 -0800534
John Reckd8be4a02017-11-17 15:06:24 -0800535 bool setSpotShadowColor(SkColor shadowColor) {
536 return RP_SET(mPrimitiveFields.mSpotShadowColor, shadowColor);
537 }
538
John Recke170fb62018-05-07 08:12:07 -0700539 SkColor getAmbientShadowColor() const { return mPrimitiveFields.mAmbientShadowColor; }
John Reckd8be4a02017-11-17 15:06:24 -0800540
541 bool setAmbientShadowColor(SkColor shadowColor) {
542 return RP_SET(mPrimitiveFields.mAmbientShadowColor, shadowColor);
John Reck3c0369b2017-11-13 16:47:35 -0800543 }
544
Chris Craik9fded232015-11-11 16:42:34 -0800545 bool fitsOnLayer() const {
Chris Craik76caecf2015-11-02 19:17:45 -0800546 const DeviceInfo* deviceInfo = DeviceInfo::get();
John Reck1bcacfd2017-11-03 10:12:19 -0700547 return mPrimitiveFields.mWidth <= deviceInfo->maxTextureSize() &&
John Reck1895e2e2024-11-13 11:48:29 -0500548 mPrimitiveFields.mHeight <= deviceInfo->maxTextureSize() &&
549 mPrimitiveFields.mWidth > 0 && mPrimitiveFields.mHeight > 0;
Chris Craik9fded232015-11-11 16:42:34 -0800550 }
551
552 bool promotedToLayer() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700553 return mLayerProperties.mType == LayerType::None && fitsOnLayer() &&
Nader Jawad6701a602021-02-23 18:14:22 -0800554 (mComputedFields.mNeedLayerForFunctors || mLayerProperties.mImageFilter != nullptr ||
John Reck09d9cca2021-05-10 19:34:58 -0400555 mLayerProperties.getStretchEffect().requiresLayer() ||
John Reck1bcacfd2017-11-03 10:12:19 -0700556 (!MathUtils::isZero(mPrimitiveFields.mAlpha) && mPrimitiveFields.mAlpha < 1 &&
557 mPrimitiveFields.mHasOverlappingRendering));
Chris Craik1a0808e2015-05-13 16:33:04 -0700558 }
559
560 LayerType effectiveLayerType() const {
Chris Craika766cb22015-06-08 16:49:43 -0700561 return CC_UNLIKELY(promotedToLayer()) ? LayerType::RenderLayer : mLayerProperties.mType;
Chris Craik856f0cc2015-04-21 15:13:29 -0700562 }
563
John Reck1423e132018-09-21 14:30:19 -0700564 bool setAllowForceDark(bool allow) {
565 return RP_SET(mPrimitiveFields.mAllowForceDark, allow);
566 }
567
568 bool getAllowForceDark() const {
569 return mPrimitiveFields.mAllowForceDark;
570 }
571
John Reckacb6f072014-03-12 16:11:23 -0700572private:
John Reckacb6f072014-03-12 16:11:23 -0700573 // Rendering properties
John Reckd0a0b2a2014-03-20 16:28:56 -0700574 struct PrimitiveFields {
John Recke248bd12015-08-05 13:53:53 -0700575 int mLeft = 0, mTop = 0, mRight = 0, mBottom = 0;
576 int mWidth = 0, mHeight = 0;
577 int mClippingFlags = CLIP_TO_BOUNDS;
John Reckd8be4a02017-11-17 15:06:24 -0800578 SkColor mSpotShadowColor = SK_ColorBLACK;
579 SkColor mAmbientShadowColor = SK_ColorBLACK;
John Recke248bd12015-08-05 13:53:53 -0700580 float mAlpha = 1;
581 float mTranslationX = 0, mTranslationY = 0, mTranslationZ = 0;
582 float mElevation = 0;
583 float mRotation = 0, mRotationX = 0, mRotationY = 0;
584 float mScaleX = 1, mScaleY = 1;
585 float mPivotX = 0, mPivotY = 0;
586 bool mHasOverlappingRendering = false;
587 bool mPivotExplicitlySet = false;
588 bool mMatrixOrPivotDirty = false;
589 bool mProjectBackwards = false;
590 bool mProjectionReceiver = false;
John Reck1423e132018-09-21 14:30:19 -0700591 bool mAllowForceDark = true;
Stan Ilievf09ee582018-11-06 17:35:50 -0500592 bool mClipMayBeComplex = false;
John Recke248bd12015-08-05 13:53:53 -0700593 Rect mClipBounds;
John Reckd0a0b2a2014-03-20 16:28:56 -0700594 Outline mOutline;
Chris Craik8c271ca2014-03-25 10:33:01 -0700595 RevealClip mRevealClip;
John Reckd0a0b2a2014-03-20 16:28:56 -0700596 } mPrimitiveFields;
597
John Reckacb6f072014-03-12 16:11:23 -0700598 SkMatrix* mStaticMatrix;
599 SkMatrix* mAnimationMatrix;
John Reck25fbb3f2014-06-12 13:46:45 -0700600 LayerProperties mLayerProperties;
John Reckacb6f072014-03-12 16:11:23 -0700601
John Reckd0a0b2a2014-03-20 16:28:56 -0700602 /**
603 * These fields are all generated from other properties and are not set directly.
604 */
605 struct ComputedFields {
606 ComputedFields();
607 ~ComputedFields();
608
609 /**
610 * Stores the total transformation of the DisplayList based upon its scalar
611 * translate/rotate/scale properties.
612 *
Chris Craik49e6c7392014-03-31 12:34:11 -0700613 * In the common translation-only case, the matrix isn't necessarily allocated,
614 * and the mTranslation properties are used directly.
John Reckd0a0b2a2014-03-20 16:28:56 -0700615 */
Chris Craik49e6c7392014-03-31 12:34:11 -0700616 SkMatrix* mTransformMatrix;
617
618 Sk3DView mTransformCamera;
Chris Craika766cb22015-06-08 16:49:43 -0700619
620 // Force layer on for functors to enable render features they don't yet support (clipping)
621 bool mNeedLayerForFunctors = false;
John Reckd0a0b2a2014-03-20 16:28:56 -0700622 } mComputedFields;
John Reckacb6f072014-03-12 16:11:23 -0700623};
624
625} /* namespace uirenderer */
626} /* namespace android */