blob: 24f6035b67082573bd0b17757b20f55b2b7ad622 [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"
Chris Craik76caecf2015-11-02 19:17:45 -080026#include "utils/MathUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070027#include "utils/PaintUtils.h"
John Reckacb6f072014-03-12 16:11:23 -070028
Mike Reed260ab722016-10-07 15:59:20 -040029#include <SkBlendMode.h>
John Reckacb6f072014-03-12 16:11:23 -070030#include <SkCamera.h>
John Reck3c0369b2017-11-13 16:47:35 -080031#include <SkColor.h>
John Reckacb6f072014-03-12 16:11:23 -070032#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070033#include <SkRegion.h>
Chris Craikb49f4462014-03-20 12:44:20 -070034
Chris Craik76caecf2015-11-02 19:17:45 -080035#include <androidfw/ResourceTypes.h>
John Reck1bcacfd2017-11-03 10:12:19 -070036#include <cutils/compiler.h>
37#include <stddef.h>
Chris Craik76caecf2015-11-02 19:17:45 -080038#include <utils/Log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070039#include <algorithm>
sergeyvc3849aa2016-08-08 13:22:06 -070040#include <ostream>
John Reck1bcacfd2017-11-03 10:12:19 -070041#include <vector>
John Reckacb6f072014-03-12 16:11:23 -070042
John Reckacb6f072014-03-12 16:11:23 -070043class SkBitmap;
John Reck25fbb3f2014-06-12 13:46:45 -070044class SkColorFilter;
John Reckacb6f072014-03-12 16:11:23 -070045class SkPaint;
John Reckacb6f072014-03-12 16:11:23 -070046
47namespace android {
48namespace uirenderer {
49
50class Matrix4;
51class RenderNode;
John Reck25fbb3f2014-06-12 13:46:45 -070052class RenderProperties;
John Reckacb6f072014-03-12 16:11:23 -070053
John Reck79c7de72014-05-23 10:33:31 -070054// The __VA_ARGS__ will be executed if a & b are not equal
Chih-Hung Hsiehcef190d2016-05-19 15:25:50 -070055#define RP_SET(a, b, ...) ((a) != (b) ? ((a) = (b), ##__VA_ARGS__, true) : false)
John Reck79c7de72014-05-23 10:33:31 -070056#define RP_SET_AND_DIRTY(a, b) RP_SET(a, b, mPrimitiveFields.mMatrixOrPivotDirty = true)
57
John Reck25fbb3f2014-06-12 13:46:45 -070058// Keep in sync with View.java:LAYER_TYPE_*
Chris Craik182952f2015-03-09 14:17:29 -070059enum class LayerType {
60 None = 0,
Derek Sollenberger52230bc2018-02-16 12:24:13 -050061 // We cannot build the software layer directly (must be done at record time) and all management
62 // of software layers is handled in Java.
Chris Craik182952f2015-03-09 14:17:29 -070063 Software = 1,
64 RenderLayer = 2,
John Reck25fbb3f2014-06-12 13:46:45 -070065};
66
Chris Craika753f4c2014-07-24 12:39:17 -070067enum ClippingFlags {
John Reck1bcacfd2017-11-03 10:12:19 -070068 CLIP_TO_BOUNDS = 0x1 << 0,
Chris Craika753f4c2014-07-24 12:39:17 -070069 CLIP_TO_CLIP_BOUNDS = 0x1 << 1,
70};
71
John Reck25fbb3f2014-06-12 13:46:45 -070072class ANDROID_API LayerProperties {
73public:
74 bool setType(LayerType type) {
75 if (RP_SET(mType, type)) {
76 reset();
77 return true;
78 }
79 return false;
80 }
81
John Reck1bcacfd2017-11-03 10:12:19 -070082 bool setOpaque(bool opaque) { return RP_SET(mOpaque, opaque); }
John Reck25fbb3f2014-06-12 13:46:45 -070083
John Reck1bcacfd2017-11-03 10:12:19 -070084 bool opaque() const { return mOpaque; }
John Reck25fbb3f2014-06-12 13:46:45 -070085
John Reck1bcacfd2017-11-03 10:12:19 -070086 bool setAlpha(uint8_t alpha) { return RP_SET(mAlpha, alpha); }
John Reck25fbb3f2014-06-12 13:46:45 -070087
John Reck1bcacfd2017-11-03 10:12:19 -070088 uint8_t alpha() const { return mAlpha; }
John Reck25fbb3f2014-06-12 13:46:45 -070089
John Reck1bcacfd2017-11-03 10:12:19 -070090 bool setXferMode(SkBlendMode mode) { return RP_SET(mMode, mode); }
John Reck25fbb3f2014-06-12 13:46:45 -070091
John Reck1bcacfd2017-11-03 10:12:19 -070092 SkBlendMode xferMode() const { return mMode; }
John Reck25fbb3f2014-06-12 13:46:45 -070093
Ben Wagnerc1a8a462018-07-12 12:41:28 -040094 SkColorFilter* getColorFilter() const { return mColorFilter.get(); }
John Reck25fbb3f2014-06-12 13:46:45 -070095
96 // Sets alpha, xfermode, and colorfilter from an SkPaint
97 // paint may be NULL, in which case defaults will be set
98 bool setFromPaint(const SkPaint* paint);
99
John Reck1bcacfd2017-11-03 10:12:19 -0700100 bool needsBlending() const { return !opaque() || alpha() < 255; }
John Reck25fbb3f2014-06-12 13:46:45 -0700101
102 LayerProperties& operator=(const LayerProperties& other);
103
John Reck470a9192018-12-17 10:55:54 -0800104 // Strongly recommend using effectiveLayerType instead
105 LayerType type() const { return mType; }
106
John Reck25fbb3f2014-06-12 13:46:45 -0700107private:
108 LayerProperties();
109 ~LayerProperties();
110 void reset();
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400111 bool setColorFilter(SkColorFilter* filter);
John Reck25fbb3f2014-06-12 13:46:45 -0700112
113 friend class RenderProperties;
114
Chris Craik182952f2015-03-09 14:17:29 -0700115 LayerType mType = LayerType::None;
John Reck25fbb3f2014-06-12 13:46:45 -0700116 // Whether or not that Layer's content is opaque, doesn't include alpha
117 bool mOpaque;
118 uint8_t mAlpha;
Mike Reed260ab722016-10-07 15:59:20 -0400119 SkBlendMode mMode;
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400120 sk_sp<SkColorFilter> mColorFilter;
John Reck25fbb3f2014-06-12 13:46:45 -0700121};
122
John Reckacb6f072014-03-12 16:11:23 -0700123/*
124 * Data structure that holds the properties for a RenderNode
125 */
John Reck25fbb3f2014-06-12 13:46:45 -0700126class ANDROID_API RenderProperties {
John Reckacb6f072014-03-12 16:11:23 -0700127public:
128 RenderProperties();
129 virtual ~RenderProperties();
130
Chris Craika753f4c2014-07-24 12:39:17 -0700131 static bool setFlag(int flag, bool newValue, int* outFlags) {
132 if (newValue) {
133 if (!(flag & *outFlags)) {
134 *outFlags |= flag;
135 return true;
136 }
137 return false;
138 } else {
139 if (flag & *outFlags) {
140 *outFlags &= ~flag;
141 return true;
142 }
143 return false;
144 }
145 }
146
Chris Craika766cb22015-06-08 16:49:43 -0700147 /**
148 * Set internal layer state based on whether this layer
149 *
150 * Additionally, returns true if child RenderNodes with functors will need to use a layer
151 * to support clipping.
152 */
153 bool prepareForFunctorPresence(bool willHaveFunctor, bool ancestorDictatesFunctorsNeedLayer) {
154 // parent may have already dictated that a descendant layer is needed
John Reck1bcacfd2017-11-03 10:12:19 -0700155 bool functorsNeedLayer =
156 ancestorDictatesFunctorsNeedLayer
Stan Ilievf09ee582018-11-06 17:35:50 -0500157 || CC_UNLIKELY(isClipMayBeComplex())
Chris Craika766cb22015-06-08 16:49:43 -0700158
159 // Round rect clipping forces layer for functors
John Reck1bcacfd2017-11-03 10:12:19 -0700160 || CC_UNLIKELY(getOutline().willRoundRectClip()) ||
161 CC_UNLIKELY(getRevealClip().willClip())
Chris Craika766cb22015-06-08 16:49:43 -0700162
163 // Complex matrices forces layer, due to stencil clipping
John Reck1bcacfd2017-11-03 10:12:19 -0700164 || CC_UNLIKELY(getTransformMatrix() && !getTransformMatrix()->isScaleTranslate()) ||
165 CC_UNLIKELY(getAnimationMatrix() && !getAnimationMatrix()->isScaleTranslate()) ||
166 CC_UNLIKELY(getStaticMatrix() && !getStaticMatrix()->isScaleTranslate());
Chris Craika766cb22015-06-08 16:49:43 -0700167
168 mComputedFields.mNeedLayerForFunctors = (willHaveFunctor && functorsNeedLayer);
169
170 // If on a layer, will have consumed the need for isolating functors from stencil.
171 // Thus, it's safe to reset the flag until some descendent sets it.
172 return CC_LIKELY(effectiveLayerType() == LayerType::None) && functorsNeedLayer;
173 }
174
John Reckd0a0b2a2014-03-20 16:28:56 -0700175 RenderProperties& operator=(const RenderProperties& other);
176
John Reck79c7de72014-05-23 10:33:31 -0700177 bool setClipToBounds(bool clipToBounds) {
Chris Craika753f4c2014-07-24 12:39:17 -0700178 return setFlag(CLIP_TO_BOUNDS, clipToBounds, &mPrimitiveFields.mClippingFlags);
179 }
180
181 bool setClipBounds(const Rect& clipBounds) {
182 bool ret = setFlag(CLIP_TO_CLIP_BOUNDS, true, &mPrimitiveFields.mClippingFlags);
183 return RP_SET(mPrimitiveFields.mClipBounds, clipBounds) || ret;
184 }
185
186 bool setClipBoundsEmpty() {
187 return setFlag(CLIP_TO_CLIP_BOUNDS, false, &mPrimitiveFields.mClippingFlags);
John Reckacb6f072014-03-12 16:11:23 -0700188 }
189
John Reck79c7de72014-05-23 10:33:31 -0700190 bool setProjectBackwards(bool shouldProject) {
191 return RP_SET(mPrimitiveFields.mProjectBackwards, shouldProject);
John Reckacb6f072014-03-12 16:11:23 -0700192 }
193
Chris Craik6fe991e52015-10-20 09:39:42 -0700194 bool setProjectionReceiver(bool shouldReceive) {
195 return RP_SET(mPrimitiveFields.mProjectionReceiver, shouldReceive);
John Reckacb6f072014-03-12 16:11:23 -0700196 }
197
John Reck1bcacfd2017-11-03 10:12:19 -0700198 bool isProjectionReceiver() const { return mPrimitiveFields.mProjectionReceiver; }
John Reckacb6f072014-03-12 16:11:23 -0700199
Stan Ilievf09ee582018-11-06 17:35:50 -0500200 bool setClipMayBeComplex(bool isClipMayBeComplex) {
201 return RP_SET(mPrimitiveFields.mClipMayBeComplex, isClipMayBeComplex);
202 }
203
204 bool isClipMayBeComplex() const { return mPrimitiveFields.mClipMayBeComplex; }
205
John Reck79c7de72014-05-23 10:33:31 -0700206 bool setStaticMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700207 delete mStaticMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -0700208 if (matrix) {
209 mStaticMatrix = new SkMatrix(*matrix);
210 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800211 mStaticMatrix = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700212 }
John Reck79c7de72014-05-23 10:33:31 -0700213 return true;
John Reckacb6f072014-03-12 16:11:23 -0700214 }
215
216 // Can return NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700217 const SkMatrix* getStaticMatrix() const { return mStaticMatrix; }
John Reckacb6f072014-03-12 16:11:23 -0700218
John Reck79c7de72014-05-23 10:33:31 -0700219 bool setAnimationMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700220 delete mAnimationMatrix;
221 if (matrix) {
222 mAnimationMatrix = new SkMatrix(*matrix);
223 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800224 mAnimationMatrix = nullptr;
John Reckacb6f072014-03-12 16:11:23 -0700225 }
John Reck79c7de72014-05-23 10:33:31 -0700226 return true;
John Reckacb6f072014-03-12 16:11:23 -0700227 }
228
John Reck79c7de72014-05-23 10:33:31 -0700229 bool setAlpha(float alpha) {
John Reck3b52c032014-08-06 10:19:32 -0700230 alpha = MathUtils::clampAlpha(alpha);
John Reck79c7de72014-05-23 10:33:31 -0700231 return RP_SET(mPrimitiveFields.mAlpha, alpha);
John Reckacb6f072014-03-12 16:11:23 -0700232 }
233
John Reck1bcacfd2017-11-03 10:12:19 -0700234 float getAlpha() const { return mPrimitiveFields.mAlpha; }
John Reckacb6f072014-03-12 16:11:23 -0700235
John Reck79c7de72014-05-23 10:33:31 -0700236 bool setHasOverlappingRendering(bool hasOverlappingRendering) {
237 return RP_SET(mPrimitiveFields.mHasOverlappingRendering, hasOverlappingRendering);
John Reckacb6f072014-03-12 16:11:23 -0700238 }
239
John Reck1bcacfd2017-11-03 10:12:19 -0700240 bool hasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckacb6f072014-03-12 16:11:23 -0700241
John Reck79c7de72014-05-23 10:33:31 -0700242 bool setElevation(float elevation) {
243 return RP_SET(mPrimitiveFields.mElevation, elevation);
244 // Don't dirty matrix/pivot, since they don't respect Z
Chris Craikcc39e162014-04-25 18:34:11 -0700245 }
246
John Reck1bcacfd2017-11-03 10:12:19 -0700247 float getElevation() const { return mPrimitiveFields.mElevation; }
Chris Craikcc39e162014-04-25 18:34:11 -0700248
John Reck79c7de72014-05-23 10:33:31 -0700249 bool setTranslationX(float translationX) {
250 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationX, translationX);
John Reckacb6f072014-03-12 16:11:23 -0700251 }
252
John Reck1bcacfd2017-11-03 10:12:19 -0700253 float getTranslationX() const { return mPrimitiveFields.mTranslationX; }
John Reckacb6f072014-03-12 16:11:23 -0700254
John Reck79c7de72014-05-23 10:33:31 -0700255 bool setTranslationY(float translationY) {
256 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationY, translationY);
John Reckacb6f072014-03-12 16:11:23 -0700257 }
258
John Reck1bcacfd2017-11-03 10:12:19 -0700259 float getTranslationY() const { return mPrimitiveFields.mTranslationY; }
John Reckacb6f072014-03-12 16:11:23 -0700260
John Reck79c7de72014-05-23 10:33:31 -0700261 bool setTranslationZ(float translationZ) {
262 return RP_SET(mPrimitiveFields.mTranslationZ, translationZ);
263 // mMatrixOrPivotDirty not set, since matrix doesn't respect Z
John Reckacb6f072014-03-12 16:11:23 -0700264 }
265
John Reck1bcacfd2017-11-03 10:12:19 -0700266 float getTranslationZ() const { return mPrimitiveFields.mTranslationZ; }
John Reckacb6f072014-03-12 16:11:23 -0700267
John Recke45b1fd2014-04-15 09:50:16 -0700268 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700269 bool setX(float value) { return setTranslationX(value - getLeft()); }
John Recke45b1fd2014-04-15 09:50:16 -0700270
271 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700272 float getX() const { return getLeft() + getTranslationX(); }
John Recke45b1fd2014-04-15 09:50:16 -0700273
274 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700275 bool setY(float value) { return setTranslationY(value - getTop()); }
John Recke45b1fd2014-04-15 09:50:16 -0700276
277 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700278 float getY() const { return getTop() + getTranslationY(); }
John Recke45b1fd2014-04-15 09:50:16 -0700279
280 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700281 bool setZ(float value) { return setTranslationZ(value - getElevation()); }
John Recke45b1fd2014-04-15 09:50:16 -0700282
John Reck1bcacfd2017-11-03 10:12:19 -0700283 float getZ() const { return getElevation() + getTranslationZ(); }
Chris Craikcc39e162014-04-25 18:34:11 -0700284
John Reck79c7de72014-05-23 10:33:31 -0700285 bool setRotation(float rotation) {
286 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotation, rotation);
John Reckacb6f072014-03-12 16:11:23 -0700287 }
288
John Reck1bcacfd2017-11-03 10:12:19 -0700289 float getRotation() const { return mPrimitiveFields.mRotation; }
John Reckacb6f072014-03-12 16:11:23 -0700290
John Reck79c7de72014-05-23 10:33:31 -0700291 bool setRotationX(float rotationX) {
292 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationX, rotationX);
John Reckacb6f072014-03-12 16:11:23 -0700293 }
294
John Reck1bcacfd2017-11-03 10:12:19 -0700295 float getRotationX() const { return mPrimitiveFields.mRotationX; }
John Reckacb6f072014-03-12 16:11:23 -0700296
John Reck79c7de72014-05-23 10:33:31 -0700297 bool setRotationY(float rotationY) {
298 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationY, rotationY);
John Reckacb6f072014-03-12 16:11:23 -0700299 }
300
John Reck1bcacfd2017-11-03 10:12:19 -0700301 float getRotationY() const { return mPrimitiveFields.mRotationY; }
John Reckacb6f072014-03-12 16:11:23 -0700302
John Reck1bcacfd2017-11-03 10:12:19 -0700303 bool setScaleX(float scaleX) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleX, scaleX); }
John Reckacb6f072014-03-12 16:11:23 -0700304
John Reck1bcacfd2017-11-03 10:12:19 -0700305 float getScaleX() const { return mPrimitiveFields.mScaleX; }
John Reckacb6f072014-03-12 16:11:23 -0700306
John Reck1bcacfd2017-11-03 10:12:19 -0700307 bool setScaleY(float scaleY) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleY, scaleY); }
John Reckacb6f072014-03-12 16:11:23 -0700308
John Reck1bcacfd2017-11-03 10:12:19 -0700309 float getScaleY() const { return mPrimitiveFields.mScaleY; }
John Reckacb6f072014-03-12 16:11:23 -0700310
John Reck79c7de72014-05-23 10:33:31 -0700311 bool setPivotX(float pivotX) {
John Reck1bcacfd2017-11-03 10:12:19 -0700312 if (RP_SET(mPrimitiveFields.mPivotX, pivotX) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700313 mPrimitiveFields.mMatrixOrPivotDirty = true;
314 mPrimitiveFields.mPivotExplicitlySet = true;
315 return true;
316 }
317 return false;
John Reckacb6f072014-03-12 16:11:23 -0700318 }
319
John Reckd0a0b2a2014-03-20 16:28:56 -0700320 /* Note that getPivotX and getPivotY are adjusted by updateMatrix(),
John Reck79c7de72014-05-23 10:33:31 -0700321 * so the value returned may be stale if the RenderProperties has been
322 * modified since the last call to updateMatrix()
John Reckd0a0b2a2014-03-20 16:28:56 -0700323 */
John Reck1bcacfd2017-11-03 10:12:19 -0700324 float getPivotX() const { return mPrimitiveFields.mPivotX; }
John Reckacb6f072014-03-12 16:11:23 -0700325
John Reck79c7de72014-05-23 10:33:31 -0700326 bool setPivotY(float pivotY) {
John Reck1bcacfd2017-11-03 10:12:19 -0700327 if (RP_SET(mPrimitiveFields.mPivotY, pivotY) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700328 mPrimitiveFields.mMatrixOrPivotDirty = true;
329 mPrimitiveFields.mPivotExplicitlySet = true;
330 return true;
331 }
332 return false;
John Reckacb6f072014-03-12 16:11:23 -0700333 }
334
John Reck1bcacfd2017-11-03 10:12:19 -0700335 float getPivotY() const { return mPrimitiveFields.mPivotY; }
John Reckacb6f072014-03-12 16:11:23 -0700336
John Reck1bcacfd2017-11-03 10:12:19 -0700337 bool isPivotExplicitlySet() const { return mPrimitiveFields.mPivotExplicitlySet; }
Chris Craik49e6c732014-03-31 12:34:11 -0700338
John Recke170fb62018-05-07 08:12:07 -0700339 bool resetPivot() { return RP_SET_AND_DIRTY(mPrimitiveFields.mPivotExplicitlySet, false); }
John Reck8686e1f2018-03-21 16:31:21 -0700340
John Reck79c7de72014-05-23 10:33:31 -0700341 bool setCameraDistance(float distance) {
Chris Craik49e6c732014-03-31 12:34:11 -0700342 if (distance != getCameraDistance()) {
John Reckf7483e32014-04-11 08:54:47 -0700343 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c732014-03-31 12:34:11 -0700344 mComputedFields.mTransformCamera.setCameraLocation(0, 0, distance);
John Reck79c7de72014-05-23 10:33:31 -0700345 return true;
John Reckacb6f072014-03-12 16:11:23 -0700346 }
John Reck79c7de72014-05-23 10:33:31 -0700347 return false;
John Reckacb6f072014-03-12 16:11:23 -0700348 }
349
350 float getCameraDistance() const {
Chris Craik49e6c732014-03-31 12:34:11 -0700351 // TODO: update getCameraLocationZ() to be const
352 return const_cast<Sk3DView*>(&mComputedFields.mTransformCamera)->getCameraLocationZ();
John Reckacb6f072014-03-12 16:11:23 -0700353 }
354
John Reck79c7de72014-05-23 10:33:31 -0700355 bool setLeft(int left) {
356 if (RP_SET(mPrimitiveFields.mLeft, left)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700357 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700358 if (!mPrimitiveFields.mPivotExplicitlySet) {
359 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700360 }
John Reck79c7de72014-05-23 10:33:31 -0700361 return true;
John Reckacb6f072014-03-12 16:11:23 -0700362 }
John Reck79c7de72014-05-23 10:33:31 -0700363 return false;
John Reckacb6f072014-03-12 16:11:23 -0700364 }
365
John Reck1bcacfd2017-11-03 10:12:19 -0700366 int getLeft() const { return mPrimitiveFields.mLeft; }
John Reckacb6f072014-03-12 16:11:23 -0700367
John Reck79c7de72014-05-23 10:33:31 -0700368 bool setTop(int top) {
369 if (RP_SET(mPrimitiveFields.mTop, top)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700370 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700371 if (!mPrimitiveFields.mPivotExplicitlySet) {
372 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700373 }
John Reck79c7de72014-05-23 10:33:31 -0700374 return true;
John Reckacb6f072014-03-12 16:11:23 -0700375 }
John Reck79c7de72014-05-23 10:33:31 -0700376 return false;
John Reckacb6f072014-03-12 16:11:23 -0700377 }
378
John Reck1bcacfd2017-11-03 10:12:19 -0700379 int getTop() const { return mPrimitiveFields.mTop; }
John Reckacb6f072014-03-12 16:11:23 -0700380
John Reck79c7de72014-05-23 10:33:31 -0700381 bool setRight(int right) {
382 if (RP_SET(mPrimitiveFields.mRight, right)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700383 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700384 if (!mPrimitiveFields.mPivotExplicitlySet) {
385 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700386 }
John Reck79c7de72014-05-23 10:33:31 -0700387 return true;
John Reckacb6f072014-03-12 16:11:23 -0700388 }
John Reck79c7de72014-05-23 10:33:31 -0700389 return false;
John Reckacb6f072014-03-12 16:11:23 -0700390 }
391
John Reck1bcacfd2017-11-03 10:12:19 -0700392 int getRight() const { return mPrimitiveFields.mRight; }
John Reckacb6f072014-03-12 16:11:23 -0700393
John Reck79c7de72014-05-23 10:33:31 -0700394 bool setBottom(int bottom) {
395 if (RP_SET(mPrimitiveFields.mBottom, bottom)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700396 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700397 if (!mPrimitiveFields.mPivotExplicitlySet) {
398 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700399 }
John Reck79c7de72014-05-23 10:33:31 -0700400 return true;
John Reckacb6f072014-03-12 16:11:23 -0700401 }
John Reck79c7de72014-05-23 10:33:31 -0700402 return false;
John Reckacb6f072014-03-12 16:11:23 -0700403 }
404
John Reck1bcacfd2017-11-03 10:12:19 -0700405 int getBottom() const { return mPrimitiveFields.mBottom; }
John Reckacb6f072014-03-12 16:11:23 -0700406
John Reck79c7de72014-05-23 10:33:31 -0700407 bool setLeftTop(int left, int top) {
408 bool leftResult = setLeft(left);
409 bool topResult = setTop(top);
410 return leftResult || topResult;
John Reckacb6f072014-03-12 16:11:23 -0700411 }
412
John Reck79c7de72014-05-23 10:33:31 -0700413 bool setLeftTopRightBottom(int left, int top, int right, int bottom) {
John Reck1bcacfd2017-11-03 10:12:19 -0700414 if (left != mPrimitiveFields.mLeft || top != mPrimitiveFields.mTop ||
415 right != mPrimitiveFields.mRight || bottom != mPrimitiveFields.mBottom) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700416 mPrimitiveFields.mLeft = left;
417 mPrimitiveFields.mTop = top;
418 mPrimitiveFields.mRight = right;
419 mPrimitiveFields.mBottom = bottom;
420 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
421 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700422 if (!mPrimitiveFields.mPivotExplicitlySet) {
423 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700424 }
John Reck79c7de72014-05-23 10:33:31 -0700425 return true;
John Reckacb6f072014-03-12 16:11:23 -0700426 }
John Reck79c7de72014-05-23 10:33:31 -0700427 return false;
John Reckacb6f072014-03-12 16:11:23 -0700428 }
429
Chris Craika753f4c2014-07-24 12:39:17 -0700430 bool offsetLeftRight(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700431 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700432 mPrimitiveFields.mLeft += offset;
433 mPrimitiveFields.mRight += offset;
John Reck79c7de72014-05-23 10:33:31 -0700434 return true;
John Reckacb6f072014-03-12 16:11:23 -0700435 }
John Reck79c7de72014-05-23 10:33:31 -0700436 return false;
John Reckacb6f072014-03-12 16:11:23 -0700437 }
438
Chris Craika753f4c2014-07-24 12:39:17 -0700439 bool offsetTopBottom(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700440 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700441 mPrimitiveFields.mTop += offset;
442 mPrimitiveFields.mBottom += offset;
John Reck79c7de72014-05-23 10:33:31 -0700443 return true;
John Reckacb6f072014-03-12 16:11:23 -0700444 }
John Reck79c7de72014-05-23 10:33:31 -0700445 return false;
John Reckacb6f072014-03-12 16:11:23 -0700446 }
447
John Reck1bcacfd2017-11-03 10:12:19 -0700448 int getWidth() const { return mPrimitiveFields.mWidth; }
John Reckacb6f072014-03-12 16:11:23 -0700449
John Reck1bcacfd2017-11-03 10:12:19 -0700450 int getHeight() const { return mPrimitiveFields.mHeight; }
John Reckacb6f072014-03-12 16:11:23 -0700451
John Reck1bcacfd2017-11-03 10:12:19 -0700452 const SkMatrix* getAnimationMatrix() const { return mAnimationMatrix; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700453
John Reckf7483e32014-04-11 08:54:47 -0700454 bool hasTransformMatrix() const {
455 return getTransformMatrix() && !getTransformMatrix()->isIdentity();
456 }
457
458 // May only call this if hasTransformMatrix() is true
459 bool isTransformTranslateOnly() const {
460 return getTransformMatrix()->getType() == SkMatrix::kTranslate_Mask;
John Reckd0a0b2a2014-03-20 16:28:56 -0700461 }
462
Chris Craik49e6c732014-03-31 12:34:11 -0700463 const SkMatrix* getTransformMatrix() const {
John Reckf7483e32014-04-11 08:54:47 -0700464 LOG_ALWAYS_FATAL_IF(mPrimitiveFields.mMatrixOrPivotDirty, "Cannot get a dirty matrix!");
John Reckd0a0b2a2014-03-20 16:28:56 -0700465 return mComputedFields.mTransformMatrix;
466 }
467
John Reck1bcacfd2017-11-03 10:12:19 -0700468 int getClippingFlags() const { return mPrimitiveFields.mClippingFlags; }
Chris Craika753f4c2014-07-24 12:39:17 -0700469
John Reck1bcacfd2017-11-03 10:12:19 -0700470 bool getClipToBounds() const { return mPrimitiveFields.mClippingFlags & CLIP_TO_BOUNDS; }
Chris Craika753f4c2014-07-24 12:39:17 -0700471
John Reck1bcacfd2017-11-03 10:12:19 -0700472 const Rect& getClipBounds() const { return mPrimitiveFields.mClipBounds; }
John Recke248bd12015-08-05 13:53:53 -0700473
Chris Craika753f4c2014-07-24 12:39:17 -0700474 void getClippingRectForFlags(uint32_t flags, Rect* outRect) const {
475 if (flags & CLIP_TO_BOUNDS) {
476 outRect->set(0, 0, getWidth(), getHeight());
477 if (flags & CLIP_TO_CLIP_BOUNDS) {
Chris Craikac02eb92015-10-05 12:23:46 -0700478 outRect->doIntersect(mPrimitiveFields.mClipBounds);
Chris Craika753f4c2014-07-24 12:39:17 -0700479 }
480 } else {
481 outRect->set(mPrimitiveFields.mClipBounds);
482 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700483 }
484
John Reck1bcacfd2017-11-03 10:12:19 -0700485 bool getHasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700486
John Reck1bcacfd2017-11-03 10:12:19 -0700487 const Outline& getOutline() const { return mPrimitiveFields.mOutline; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700488
John Reck1bcacfd2017-11-03 10:12:19 -0700489 const RevealClip& getRevealClip() const { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700490
John Reck1bcacfd2017-11-03 10:12:19 -0700491 bool getProjectBackwards() const { return mPrimitiveFields.mProjectBackwards; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700492
sergeyvc3849aa2016-08-08 13:22:06 -0700493 void debugOutputProperties(std::ostream& output, const int level) const;
John Reckd0a0b2a2014-03-20 16:28:56 -0700494
John Reck25fbb3f2014-06-12 13:46:45 -0700495 void updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700496
John Reck1bcacfd2017-11-03 10:12:19 -0700497 Outline& mutableOutline() { return mPrimitiveFields.mOutline; }
Chris Craikb49f4462014-03-20 12:44:20 -0700498
John Reck1bcacfd2017-11-03 10:12:19 -0700499 RevealClip& mutableRevealClip() { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700500
John Reck1bcacfd2017-11-03 10:12:19 -0700501 const LayerProperties& layerProperties() const { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700502
John Reck1bcacfd2017-11-03 10:12:19 -0700503 LayerProperties& mutateLayerProperties() { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700504
John Reck293e8682014-06-17 10:34:02 -0700505 // Returns true if damage calculations should be clipped to bounds
506 // TODO: Figure out something better for getZ(), as children should still be
507 // clipped to this RP's bounds. But as we will damage -INT_MAX to INT_MAX
508 // for this RP's getZ() anyway, this can be optimized when we have a
509 // Z damage estimate instead of INT_MAX
510 bool getClipDamageToBounds() const {
511 return getClipToBounds() && (getZ() <= 0 || getOutline().isEmpty());
512 }
513
Chris Craik5c75c522014-09-05 14:08:08 -0700514 bool hasShadow() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700515 return getZ() > 0.0f && getOutline().getPath() != nullptr &&
516 getOutline().getAlpha() != 0.0f;
Chris Craik5c75c522014-09-05 14:08:08 -0700517 }
518
John Recke170fb62018-05-07 08:12:07 -0700519 SkColor getSpotShadowColor() const { return mPrimitiveFields.mSpotShadowColor; }
John Reck3c0369b2017-11-13 16:47:35 -0800520
John Reckd8be4a02017-11-17 15:06:24 -0800521 bool setSpotShadowColor(SkColor shadowColor) {
522 return RP_SET(mPrimitiveFields.mSpotShadowColor, shadowColor);
523 }
524
John Recke170fb62018-05-07 08:12:07 -0700525 SkColor getAmbientShadowColor() const { return mPrimitiveFields.mAmbientShadowColor; }
John Reckd8be4a02017-11-17 15:06:24 -0800526
527 bool setAmbientShadowColor(SkColor shadowColor) {
528 return RP_SET(mPrimitiveFields.mAmbientShadowColor, shadowColor);
John Reck3c0369b2017-11-13 16:47:35 -0800529 }
530
Chris Craik9fded232015-11-11 16:42:34 -0800531 bool fitsOnLayer() const {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100532#ifdef __ANDROID__ // Layoutlib does not support device info
Chris Craik76caecf2015-11-02 19:17:45 -0800533 const DeviceInfo* deviceInfo = DeviceInfo::get();
John Reck1bcacfd2017-11-03 10:12:19 -0700534 return mPrimitiveFields.mWidth <= deviceInfo->maxTextureSize() &&
535 mPrimitiveFields.mHeight <= deviceInfo->maxTextureSize();
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100536#else
537 return mPrimitiveFields.mWidth <= 4096 && mPrimitiveFields.mHeight <= 4096;
538#endif
Chris Craik9fded232015-11-11 16:42:34 -0800539 }
540
541 bool promotedToLayer() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700542 return mLayerProperties.mType == LayerType::None && fitsOnLayer() &&
543 (mComputedFields.mNeedLayerForFunctors ||
544 (!MathUtils::isZero(mPrimitiveFields.mAlpha) && mPrimitiveFields.mAlpha < 1 &&
545 mPrimitiveFields.mHasOverlappingRendering));
Chris Craik1a0808e2015-05-13 16:33:04 -0700546 }
547
548 LayerType effectiveLayerType() const {
Chris Craika766cb22015-06-08 16:49:43 -0700549 return CC_UNLIKELY(promotedToLayer()) ? LayerType::RenderLayer : mLayerProperties.mType;
Chris Craik856f0cc2015-04-21 15:13:29 -0700550 }
551
John Reck1423e132018-09-21 14:30:19 -0700552 bool setAllowForceDark(bool allow) {
553 return RP_SET(mPrimitiveFields.mAllowForceDark, allow);
554 }
555
556 bool getAllowForceDark() const {
557 return mPrimitiveFields.mAllowForceDark;
558 }
559
John Reckacb6f072014-03-12 16:11:23 -0700560private:
John Reckacb6f072014-03-12 16:11:23 -0700561 // Rendering properties
John Reckd0a0b2a2014-03-20 16:28:56 -0700562 struct PrimitiveFields {
John Recke248bd12015-08-05 13:53:53 -0700563 int mLeft = 0, mTop = 0, mRight = 0, mBottom = 0;
564 int mWidth = 0, mHeight = 0;
565 int mClippingFlags = CLIP_TO_BOUNDS;
John Reckd8be4a02017-11-17 15:06:24 -0800566 SkColor mSpotShadowColor = SK_ColorBLACK;
567 SkColor mAmbientShadowColor = SK_ColorBLACK;
John Recke248bd12015-08-05 13:53:53 -0700568 float mAlpha = 1;
569 float mTranslationX = 0, mTranslationY = 0, mTranslationZ = 0;
570 float mElevation = 0;
571 float mRotation = 0, mRotationX = 0, mRotationY = 0;
572 float mScaleX = 1, mScaleY = 1;
573 float mPivotX = 0, mPivotY = 0;
574 bool mHasOverlappingRendering = false;
575 bool mPivotExplicitlySet = false;
576 bool mMatrixOrPivotDirty = false;
577 bool mProjectBackwards = false;
578 bool mProjectionReceiver = false;
John Reck1423e132018-09-21 14:30:19 -0700579 bool mAllowForceDark = true;
Stan Ilievf09ee582018-11-06 17:35:50 -0500580 bool mClipMayBeComplex = false;
John Recke248bd12015-08-05 13:53:53 -0700581 Rect mClipBounds;
John Reckd0a0b2a2014-03-20 16:28:56 -0700582 Outline mOutline;
Chris Craik8c271ca2014-03-25 10:33:01 -0700583 RevealClip mRevealClip;
John Reckd0a0b2a2014-03-20 16:28:56 -0700584 } mPrimitiveFields;
585
John Reckacb6f072014-03-12 16:11:23 -0700586 SkMatrix* mStaticMatrix;
587 SkMatrix* mAnimationMatrix;
John Reck25fbb3f2014-06-12 13:46:45 -0700588 LayerProperties mLayerProperties;
John Reckacb6f072014-03-12 16:11:23 -0700589
John Reckd0a0b2a2014-03-20 16:28:56 -0700590 /**
591 * These fields are all generated from other properties and are not set directly.
592 */
593 struct ComputedFields {
594 ComputedFields();
595 ~ComputedFields();
596
597 /**
598 * Stores the total transformation of the DisplayList based upon its scalar
599 * translate/rotate/scale properties.
600 *
Chris Craik49e6c732014-03-31 12:34:11 -0700601 * In the common translation-only case, the matrix isn't necessarily allocated,
602 * and the mTranslation properties are used directly.
John Reckd0a0b2a2014-03-20 16:28:56 -0700603 */
Chris Craik49e6c732014-03-31 12:34:11 -0700604 SkMatrix* mTransformMatrix;
605
606 Sk3DView mTransformCamera;
Chris Craika766cb22015-06-08 16:49:43 -0700607
608 // Force layer on for functors to enable render features they don't yet support (clipping)
609 bool mNeedLayerForFunctors = false;
John Reckd0a0b2a2014-03-20 16:28:56 -0700610 } mComputedFields;
John Reckacb6f072014-03-12 16:11:23 -0700611};
612
613} /* namespace uirenderer */
614} /* namespace android */