blob: 04379ae68a0dc42beb792a8126a2379738a56f48 [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
Chris Craik76caecf2015-11-02 19:17:45 -080019#include "DeviceInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070020#include "Outline.h"
Chris Craik76caecf2015-11-02 19:17:45 -080021#include "Rect.h"
22#include "RevealClip.h"
Chris Craik76caecf2015-11-02 19:17:45 -080023#include "utils/MathUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070024#include "utils/PaintUtils.h"
John Reckacb6f072014-03-12 16:11:23 -070025
Mike Reed260ab722016-10-07 15:59:20 -040026#include <SkBlendMode.h>
John Reckacb6f072014-03-12 16:11:23 -070027#include <SkCamera.h>
John Reck3c0369b2017-11-13 16:47:35 -080028#include <SkColor.h>
John Reckacb6f072014-03-12 16:11:23 -070029#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070030#include <SkRegion.h>
Chris Craikb49f4462014-03-20 12:44:20 -070031
Chris Craik76caecf2015-11-02 19:17:45 -080032#include <androidfw/ResourceTypes.h>
John Reck1bcacfd2017-11-03 10:12:19 -070033#include <cutils/compiler.h>
34#include <stddef.h>
Chris Craik76caecf2015-11-02 19:17:45 -080035#include <utils/Log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070036#include <algorithm>
sergeyvc3849aa2016-08-08 13:22:06 -070037#include <ostream>
John Reck1bcacfd2017-11-03 10:12:19 -070038#include <vector>
John Reckacb6f072014-03-12 16:11:23 -070039
John Reckacb6f072014-03-12 16:11:23 -070040class SkBitmap;
John Reck25fbb3f2014-06-12 13:46:45 -070041class SkColorFilter;
John Reckacb6f072014-03-12 16:11:23 -070042class SkPaint;
John Reckacb6f072014-03-12 16:11:23 -070043
44namespace android {
45namespace uirenderer {
46
47class Matrix4;
48class RenderNode;
John Reck25fbb3f2014-06-12 13:46:45 -070049class RenderProperties;
John Reckacb6f072014-03-12 16:11:23 -070050
John Reck79c7de72014-05-23 10:33:31 -070051// The __VA_ARGS__ will be executed if a & b are not equal
Chih-Hung Hsiehcef190d2016-05-19 15:25:50 -070052#define RP_SET(a, b, ...) ((a) != (b) ? ((a) = (b), ##__VA_ARGS__, true) : false)
John Reck79c7de72014-05-23 10:33:31 -070053#define RP_SET_AND_DIRTY(a, b) RP_SET(a, b, mPrimitiveFields.mMatrixOrPivotDirty = true)
54
John Reck25fbb3f2014-06-12 13:46:45 -070055// Keep in sync with View.java:LAYER_TYPE_*
Chris Craik182952f2015-03-09 14:17:29 -070056enum class LayerType {
57 None = 0,
Derek Sollenberger52230bc2018-02-16 12:24:13 -050058 // We cannot build the software layer directly (must be done at record time) and all management
59 // of software layers is handled in Java.
Chris Craik182952f2015-03-09 14:17:29 -070060 Software = 1,
61 RenderLayer = 2,
John Reck25fbb3f2014-06-12 13:46:45 -070062};
63
Chris Craika753f4c2014-07-24 12:39:17 -070064enum ClippingFlags {
John Reck1bcacfd2017-11-03 10:12:19 -070065 CLIP_TO_BOUNDS = 0x1 << 0,
Chris Craika753f4c2014-07-24 12:39:17 -070066 CLIP_TO_CLIP_BOUNDS = 0x1 << 1,
67};
68
John Reck25fbb3f2014-06-12 13:46:45 -070069class ANDROID_API LayerProperties {
70public:
71 bool setType(LayerType type) {
72 if (RP_SET(mType, type)) {
73 reset();
74 return true;
75 }
76 return false;
77 }
78
John Reck1bcacfd2017-11-03 10:12:19 -070079 bool setOpaque(bool opaque) { return RP_SET(mOpaque, opaque); }
John Reck25fbb3f2014-06-12 13:46:45 -070080
John Reck1bcacfd2017-11-03 10:12:19 -070081 bool opaque() const { return mOpaque; }
John Reck25fbb3f2014-06-12 13:46:45 -070082
John Reck1bcacfd2017-11-03 10:12:19 -070083 bool setAlpha(uint8_t alpha) { return RP_SET(mAlpha, alpha); }
John Reck25fbb3f2014-06-12 13:46:45 -070084
John Reck1bcacfd2017-11-03 10:12:19 -070085 uint8_t alpha() const { return mAlpha; }
John Reck25fbb3f2014-06-12 13:46:45 -070086
John Reck1bcacfd2017-11-03 10:12:19 -070087 bool setXferMode(SkBlendMode mode) { return RP_SET(mMode, mode); }
John Reck25fbb3f2014-06-12 13:46:45 -070088
John Reck1bcacfd2017-11-03 10:12:19 -070089 SkBlendMode xferMode() const { return mMode; }
John Reck25fbb3f2014-06-12 13:46:45 -070090
Ben Wagnerc1a8a462018-07-12 12:41:28 -040091 SkColorFilter* getColorFilter() const { return mColorFilter.get(); }
John Reck25fbb3f2014-06-12 13:46:45 -070092
93 // Sets alpha, xfermode, and colorfilter from an SkPaint
94 // paint may be NULL, in which case defaults will be set
95 bool setFromPaint(const SkPaint* paint);
96
John Reck1bcacfd2017-11-03 10:12:19 -070097 bool needsBlending() const { return !opaque() || alpha() < 255; }
John Reck25fbb3f2014-06-12 13:46:45 -070098
99 LayerProperties& operator=(const LayerProperties& other);
100
101private:
102 LayerProperties();
103 ~LayerProperties();
104 void reset();
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400105 bool setColorFilter(SkColorFilter* filter);
John Reck25fbb3f2014-06-12 13:46:45 -0700106
Chris Craik856f0cc2015-04-21 15:13:29 -0700107 // Private since external users should go through properties().effectiveLayerType()
John Reck1bcacfd2017-11-03 10:12:19 -0700108 LayerType type() const { return mType; }
Chris Craik856f0cc2015-04-21 15:13:29 -0700109
John Reck25fbb3f2014-06-12 13:46:45 -0700110 friend class RenderProperties;
111
Chris Craik182952f2015-03-09 14:17:29 -0700112 LayerType mType = LayerType::None;
John Reck25fbb3f2014-06-12 13:46:45 -0700113 // Whether or not that Layer's content is opaque, doesn't include alpha
114 bool mOpaque;
115 uint8_t mAlpha;
Mike Reed260ab722016-10-07 15:59:20 -0400116 SkBlendMode mMode;
Ben Wagnerc1a8a462018-07-12 12:41:28 -0400117 sk_sp<SkColorFilter> mColorFilter;
John Reck25fbb3f2014-06-12 13:46:45 -0700118};
119
John Reckacb6f072014-03-12 16:11:23 -0700120/*
121 * Data structure that holds the properties for a RenderNode
122 */
John Reck25fbb3f2014-06-12 13:46:45 -0700123class ANDROID_API RenderProperties {
John Reckacb6f072014-03-12 16:11:23 -0700124public:
125 RenderProperties();
126 virtual ~RenderProperties();
127
Chris Craika753f4c2014-07-24 12:39:17 -0700128 static bool setFlag(int flag, bool newValue, int* outFlags) {
129 if (newValue) {
130 if (!(flag & *outFlags)) {
131 *outFlags |= flag;
132 return true;
133 }
134 return false;
135 } else {
136 if (flag & *outFlags) {
137 *outFlags &= ~flag;
138 return true;
139 }
140 return false;
141 }
142 }
143
Chris Craika766cb22015-06-08 16:49:43 -0700144 /**
145 * Set internal layer state based on whether this layer
146 *
147 * Additionally, returns true if child RenderNodes with functors will need to use a layer
148 * to support clipping.
149 */
150 bool prepareForFunctorPresence(bool willHaveFunctor, bool ancestorDictatesFunctorsNeedLayer) {
151 // parent may have already dictated that a descendant layer is needed
John Reck1bcacfd2017-11-03 10:12:19 -0700152 bool functorsNeedLayer =
153 ancestorDictatesFunctorsNeedLayer
Chris Craika766cb22015-06-08 16:49:43 -0700154
155 // Round rect clipping forces layer for functors
John Reck1bcacfd2017-11-03 10:12:19 -0700156 || CC_UNLIKELY(getOutline().willRoundRectClip()) ||
157 CC_UNLIKELY(getRevealClip().willClip())
Chris Craika766cb22015-06-08 16:49:43 -0700158
159 // Complex matrices forces layer, due to stencil clipping
John Reck1bcacfd2017-11-03 10:12:19 -0700160 || CC_UNLIKELY(getTransformMatrix() && !getTransformMatrix()->isScaleTranslate()) ||
161 CC_UNLIKELY(getAnimationMatrix() && !getAnimationMatrix()->isScaleTranslate()) ||
162 CC_UNLIKELY(getStaticMatrix() && !getStaticMatrix()->isScaleTranslate());
Chris Craika766cb22015-06-08 16:49:43 -0700163
164 mComputedFields.mNeedLayerForFunctors = (willHaveFunctor && functorsNeedLayer);
165
166 // If on a layer, will have consumed the need for isolating functors from stencil.
167 // Thus, it's safe to reset the flag until some descendent sets it.
168 return CC_LIKELY(effectiveLayerType() == LayerType::None) && functorsNeedLayer;
169 }
170
John Reckd0a0b2a2014-03-20 16:28:56 -0700171 RenderProperties& operator=(const RenderProperties& other);
172
John Reck79c7de72014-05-23 10:33:31 -0700173 bool setClipToBounds(bool clipToBounds) {
Chris Craika753f4c2014-07-24 12:39:17 -0700174 return setFlag(CLIP_TO_BOUNDS, clipToBounds, &mPrimitiveFields.mClippingFlags);
175 }
176
177 bool setClipBounds(const Rect& clipBounds) {
178 bool ret = setFlag(CLIP_TO_CLIP_BOUNDS, true, &mPrimitiveFields.mClippingFlags);
179 return RP_SET(mPrimitiveFields.mClipBounds, clipBounds) || ret;
180 }
181
182 bool setClipBoundsEmpty() {
183 return setFlag(CLIP_TO_CLIP_BOUNDS, false, &mPrimitiveFields.mClippingFlags);
John Reckacb6f072014-03-12 16:11:23 -0700184 }
185
John Reck79c7de72014-05-23 10:33:31 -0700186 bool setProjectBackwards(bool shouldProject) {
187 return RP_SET(mPrimitiveFields.mProjectBackwards, shouldProject);
John Reckacb6f072014-03-12 16:11:23 -0700188 }
189
Chris Craik6fe991e52015-10-20 09:39:42 -0700190 bool setProjectionReceiver(bool shouldReceive) {
191 return RP_SET(mPrimitiveFields.mProjectionReceiver, shouldReceive);
John Reckacb6f072014-03-12 16:11:23 -0700192 }
193
John Reck1bcacfd2017-11-03 10:12:19 -0700194 bool isProjectionReceiver() const { return mPrimitiveFields.mProjectionReceiver; }
John Reckacb6f072014-03-12 16:11:23 -0700195
John Reck79c7de72014-05-23 10:33:31 -0700196 bool setStaticMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700197 delete mStaticMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -0700198 if (matrix) {
199 mStaticMatrix = new SkMatrix(*matrix);
200 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800201 mStaticMatrix = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700202 }
John Reck79c7de72014-05-23 10:33:31 -0700203 return true;
John Reckacb6f072014-03-12 16:11:23 -0700204 }
205
206 // Can return NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700207 const SkMatrix* getStaticMatrix() const { return mStaticMatrix; }
John Reckacb6f072014-03-12 16:11:23 -0700208
John Reck79c7de72014-05-23 10:33:31 -0700209 bool setAnimationMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700210 delete mAnimationMatrix;
211 if (matrix) {
212 mAnimationMatrix = new SkMatrix(*matrix);
213 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800214 mAnimationMatrix = nullptr;
John Reckacb6f072014-03-12 16:11:23 -0700215 }
John Reck79c7de72014-05-23 10:33:31 -0700216 return true;
John Reckacb6f072014-03-12 16:11:23 -0700217 }
218
John Reck79c7de72014-05-23 10:33:31 -0700219 bool setAlpha(float alpha) {
John Reck3b52c032014-08-06 10:19:32 -0700220 alpha = MathUtils::clampAlpha(alpha);
John Reck79c7de72014-05-23 10:33:31 -0700221 return RP_SET(mPrimitiveFields.mAlpha, alpha);
John Reckacb6f072014-03-12 16:11:23 -0700222 }
223
John Reck1bcacfd2017-11-03 10:12:19 -0700224 float getAlpha() const { return mPrimitiveFields.mAlpha; }
John Reckacb6f072014-03-12 16:11:23 -0700225
John Reck79c7de72014-05-23 10:33:31 -0700226 bool setHasOverlappingRendering(bool hasOverlappingRendering) {
227 return RP_SET(mPrimitiveFields.mHasOverlappingRendering, hasOverlappingRendering);
John Reckacb6f072014-03-12 16:11:23 -0700228 }
229
John Reck1bcacfd2017-11-03 10:12:19 -0700230 bool hasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckacb6f072014-03-12 16:11:23 -0700231
John Reck79c7de72014-05-23 10:33:31 -0700232 bool setElevation(float elevation) {
233 return RP_SET(mPrimitiveFields.mElevation, elevation);
234 // Don't dirty matrix/pivot, since they don't respect Z
Chris Craikcc39e162014-04-25 18:34:11 -0700235 }
236
John Reck1bcacfd2017-11-03 10:12:19 -0700237 float getElevation() const { return mPrimitiveFields.mElevation; }
Chris Craikcc39e162014-04-25 18:34:11 -0700238
John Reck79c7de72014-05-23 10:33:31 -0700239 bool setTranslationX(float translationX) {
240 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationX, translationX);
John Reckacb6f072014-03-12 16:11:23 -0700241 }
242
John Reck1bcacfd2017-11-03 10:12:19 -0700243 float getTranslationX() const { return mPrimitiveFields.mTranslationX; }
John Reckacb6f072014-03-12 16:11:23 -0700244
John Reck79c7de72014-05-23 10:33:31 -0700245 bool setTranslationY(float translationY) {
246 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationY, translationY);
John Reckacb6f072014-03-12 16:11:23 -0700247 }
248
John Reck1bcacfd2017-11-03 10:12:19 -0700249 float getTranslationY() const { return mPrimitiveFields.mTranslationY; }
John Reckacb6f072014-03-12 16:11:23 -0700250
John Reck79c7de72014-05-23 10:33:31 -0700251 bool setTranslationZ(float translationZ) {
252 return RP_SET(mPrimitiveFields.mTranslationZ, translationZ);
253 // mMatrixOrPivotDirty not set, since matrix doesn't respect Z
John Reckacb6f072014-03-12 16:11:23 -0700254 }
255
John Reck1bcacfd2017-11-03 10:12:19 -0700256 float getTranslationZ() const { return mPrimitiveFields.mTranslationZ; }
John Reckacb6f072014-03-12 16:11:23 -0700257
John Recke45b1fd2014-04-15 09:50:16 -0700258 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700259 bool setX(float value) { return setTranslationX(value - getLeft()); }
John Recke45b1fd2014-04-15 09:50:16 -0700260
261 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700262 float getX() const { return getLeft() + getTranslationX(); }
John Recke45b1fd2014-04-15 09:50:16 -0700263
264 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700265 bool setY(float value) { return setTranslationY(value - getTop()); }
John Recke45b1fd2014-04-15 09:50:16 -0700266
267 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700268 float getY() const { return getTop() + getTranslationY(); }
John Recke45b1fd2014-04-15 09:50:16 -0700269
270 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700271 bool setZ(float value) { return setTranslationZ(value - getElevation()); }
John Recke45b1fd2014-04-15 09:50:16 -0700272
John Reck1bcacfd2017-11-03 10:12:19 -0700273 float getZ() const { return getElevation() + getTranslationZ(); }
Chris Craikcc39e162014-04-25 18:34:11 -0700274
John Reck79c7de72014-05-23 10:33:31 -0700275 bool setRotation(float rotation) {
276 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotation, rotation);
John Reckacb6f072014-03-12 16:11:23 -0700277 }
278
John Reck1bcacfd2017-11-03 10:12:19 -0700279 float getRotation() const { return mPrimitiveFields.mRotation; }
John Reckacb6f072014-03-12 16:11:23 -0700280
John Reck79c7de72014-05-23 10:33:31 -0700281 bool setRotationX(float rotationX) {
282 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationX, rotationX);
John Reckacb6f072014-03-12 16:11:23 -0700283 }
284
John Reck1bcacfd2017-11-03 10:12:19 -0700285 float getRotationX() const { return mPrimitiveFields.mRotationX; }
John Reckacb6f072014-03-12 16:11:23 -0700286
John Reck79c7de72014-05-23 10:33:31 -0700287 bool setRotationY(float rotationY) {
288 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationY, rotationY);
John Reckacb6f072014-03-12 16:11:23 -0700289 }
290
John Reck1bcacfd2017-11-03 10:12:19 -0700291 float getRotationY() const { return mPrimitiveFields.mRotationY; }
John Reckacb6f072014-03-12 16:11:23 -0700292
John Reck1bcacfd2017-11-03 10:12:19 -0700293 bool setScaleX(float scaleX) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleX, scaleX); }
John Reckacb6f072014-03-12 16:11:23 -0700294
John Reck1bcacfd2017-11-03 10:12:19 -0700295 float getScaleX() const { return mPrimitiveFields.mScaleX; }
John Reckacb6f072014-03-12 16:11:23 -0700296
John Reck1bcacfd2017-11-03 10:12:19 -0700297 bool setScaleY(float scaleY) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleY, scaleY); }
John Reckacb6f072014-03-12 16:11:23 -0700298
John Reck1bcacfd2017-11-03 10:12:19 -0700299 float getScaleY() const { return mPrimitiveFields.mScaleY; }
John Reckacb6f072014-03-12 16:11:23 -0700300
John Reck79c7de72014-05-23 10:33:31 -0700301 bool setPivotX(float pivotX) {
John Reck1bcacfd2017-11-03 10:12:19 -0700302 if (RP_SET(mPrimitiveFields.mPivotX, pivotX) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700303 mPrimitiveFields.mMatrixOrPivotDirty = true;
304 mPrimitiveFields.mPivotExplicitlySet = true;
305 return true;
306 }
307 return false;
John Reckacb6f072014-03-12 16:11:23 -0700308 }
309
John Reckd0a0b2a2014-03-20 16:28:56 -0700310 /* Note that getPivotX and getPivotY are adjusted by updateMatrix(),
John Reck79c7de72014-05-23 10:33:31 -0700311 * so the value returned may be stale if the RenderProperties has been
312 * modified since the last call to updateMatrix()
John Reckd0a0b2a2014-03-20 16:28:56 -0700313 */
John Reck1bcacfd2017-11-03 10:12:19 -0700314 float getPivotX() const { return mPrimitiveFields.mPivotX; }
John Reckacb6f072014-03-12 16:11:23 -0700315
John Reck79c7de72014-05-23 10:33:31 -0700316 bool setPivotY(float pivotY) {
John Reck1bcacfd2017-11-03 10:12:19 -0700317 if (RP_SET(mPrimitiveFields.mPivotY, pivotY) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700318 mPrimitiveFields.mMatrixOrPivotDirty = true;
319 mPrimitiveFields.mPivotExplicitlySet = true;
320 return true;
321 }
322 return false;
John Reckacb6f072014-03-12 16:11:23 -0700323 }
324
John Reck1bcacfd2017-11-03 10:12:19 -0700325 float getPivotY() const { return mPrimitiveFields.mPivotY; }
John Reckacb6f072014-03-12 16:11:23 -0700326
John Reck1bcacfd2017-11-03 10:12:19 -0700327 bool isPivotExplicitlySet() const { return mPrimitiveFields.mPivotExplicitlySet; }
Chris Craik49e6c732014-03-31 12:34:11 -0700328
John Recke170fb62018-05-07 08:12:07 -0700329 bool resetPivot() { return RP_SET_AND_DIRTY(mPrimitiveFields.mPivotExplicitlySet, false); }
John Reck8686e1f2018-03-21 16:31:21 -0700330
John Reck79c7de72014-05-23 10:33:31 -0700331 bool setCameraDistance(float distance) {
Chris Craik49e6c732014-03-31 12:34:11 -0700332 if (distance != getCameraDistance()) {
John Reckf7483e32014-04-11 08:54:47 -0700333 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c732014-03-31 12:34:11 -0700334 mComputedFields.mTransformCamera.setCameraLocation(0, 0, distance);
John Reck79c7de72014-05-23 10:33:31 -0700335 return true;
John Reckacb6f072014-03-12 16:11:23 -0700336 }
John Reck79c7de72014-05-23 10:33:31 -0700337 return false;
John Reckacb6f072014-03-12 16:11:23 -0700338 }
339
340 float getCameraDistance() const {
Chris Craik49e6c732014-03-31 12:34:11 -0700341 // TODO: update getCameraLocationZ() to be const
342 return const_cast<Sk3DView*>(&mComputedFields.mTransformCamera)->getCameraLocationZ();
John Reckacb6f072014-03-12 16:11:23 -0700343 }
344
John Reck79c7de72014-05-23 10:33:31 -0700345 bool setLeft(int left) {
346 if (RP_SET(mPrimitiveFields.mLeft, left)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700347 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700348 if (!mPrimitiveFields.mPivotExplicitlySet) {
349 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700350 }
John Reck79c7de72014-05-23 10:33:31 -0700351 return true;
John Reckacb6f072014-03-12 16:11:23 -0700352 }
John Reck79c7de72014-05-23 10:33:31 -0700353 return false;
John Reckacb6f072014-03-12 16:11:23 -0700354 }
355
John Reck1bcacfd2017-11-03 10:12:19 -0700356 int getLeft() const { return mPrimitiveFields.mLeft; }
John Reckacb6f072014-03-12 16:11:23 -0700357
John Reck79c7de72014-05-23 10:33:31 -0700358 bool setTop(int top) {
359 if (RP_SET(mPrimitiveFields.mTop, top)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700360 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700361 if (!mPrimitiveFields.mPivotExplicitlySet) {
362 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700363 }
John Reck79c7de72014-05-23 10:33:31 -0700364 return true;
John Reckacb6f072014-03-12 16:11:23 -0700365 }
John Reck79c7de72014-05-23 10:33:31 -0700366 return false;
John Reckacb6f072014-03-12 16:11:23 -0700367 }
368
John Reck1bcacfd2017-11-03 10:12:19 -0700369 int getTop() const { return mPrimitiveFields.mTop; }
John Reckacb6f072014-03-12 16:11:23 -0700370
John Reck79c7de72014-05-23 10:33:31 -0700371 bool setRight(int right) {
372 if (RP_SET(mPrimitiveFields.mRight, right)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700373 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700374 if (!mPrimitiveFields.mPivotExplicitlySet) {
375 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700376 }
John Reck79c7de72014-05-23 10:33:31 -0700377 return true;
John Reckacb6f072014-03-12 16:11:23 -0700378 }
John Reck79c7de72014-05-23 10:33:31 -0700379 return false;
John Reckacb6f072014-03-12 16:11:23 -0700380 }
381
John Reck1bcacfd2017-11-03 10:12:19 -0700382 int getRight() const { return mPrimitiveFields.mRight; }
John Reckacb6f072014-03-12 16:11:23 -0700383
John Reck79c7de72014-05-23 10:33:31 -0700384 bool setBottom(int bottom) {
385 if (RP_SET(mPrimitiveFields.mBottom, bottom)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700386 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700387 if (!mPrimitiveFields.mPivotExplicitlySet) {
388 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700389 }
John Reck79c7de72014-05-23 10:33:31 -0700390 return true;
John Reckacb6f072014-03-12 16:11:23 -0700391 }
John Reck79c7de72014-05-23 10:33:31 -0700392 return false;
John Reckacb6f072014-03-12 16:11:23 -0700393 }
394
John Reck1bcacfd2017-11-03 10:12:19 -0700395 int getBottom() const { return mPrimitiveFields.mBottom; }
John Reckacb6f072014-03-12 16:11:23 -0700396
John Reck79c7de72014-05-23 10:33:31 -0700397 bool setLeftTop(int left, int top) {
398 bool leftResult = setLeft(left);
399 bool topResult = setTop(top);
400 return leftResult || topResult;
John Reckacb6f072014-03-12 16:11:23 -0700401 }
402
John Reck79c7de72014-05-23 10:33:31 -0700403 bool setLeftTopRightBottom(int left, int top, int right, int bottom) {
John Reck1bcacfd2017-11-03 10:12:19 -0700404 if (left != mPrimitiveFields.mLeft || top != mPrimitiveFields.mTop ||
405 right != mPrimitiveFields.mRight || bottom != mPrimitiveFields.mBottom) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700406 mPrimitiveFields.mLeft = left;
407 mPrimitiveFields.mTop = top;
408 mPrimitiveFields.mRight = right;
409 mPrimitiveFields.mBottom = bottom;
410 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
411 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700412 if (!mPrimitiveFields.mPivotExplicitlySet) {
413 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700414 }
John Reck79c7de72014-05-23 10:33:31 -0700415 return true;
John Reckacb6f072014-03-12 16:11:23 -0700416 }
John Reck79c7de72014-05-23 10:33:31 -0700417 return false;
John Reckacb6f072014-03-12 16:11:23 -0700418 }
419
Chris Craika753f4c2014-07-24 12:39:17 -0700420 bool offsetLeftRight(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700421 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700422 mPrimitiveFields.mLeft += offset;
423 mPrimitiveFields.mRight += offset;
John Reck79c7de72014-05-23 10:33:31 -0700424 return true;
John Reckacb6f072014-03-12 16:11:23 -0700425 }
John Reck79c7de72014-05-23 10:33:31 -0700426 return false;
John Reckacb6f072014-03-12 16:11:23 -0700427 }
428
Chris Craika753f4c2014-07-24 12:39:17 -0700429 bool offsetTopBottom(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700430 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700431 mPrimitiveFields.mTop += offset;
432 mPrimitiveFields.mBottom += offset;
John Reck79c7de72014-05-23 10:33:31 -0700433 return true;
John Reckacb6f072014-03-12 16:11:23 -0700434 }
John Reck79c7de72014-05-23 10:33:31 -0700435 return false;
John Reckacb6f072014-03-12 16:11:23 -0700436 }
437
John Reck1bcacfd2017-11-03 10:12:19 -0700438 int getWidth() const { return mPrimitiveFields.mWidth; }
John Reckacb6f072014-03-12 16:11:23 -0700439
John Reck1bcacfd2017-11-03 10:12:19 -0700440 int getHeight() const { return mPrimitiveFields.mHeight; }
John Reckacb6f072014-03-12 16:11:23 -0700441
John Reck1bcacfd2017-11-03 10:12:19 -0700442 const SkMatrix* getAnimationMatrix() const { return mAnimationMatrix; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700443
John Reckf7483e32014-04-11 08:54:47 -0700444 bool hasTransformMatrix() const {
445 return getTransformMatrix() && !getTransformMatrix()->isIdentity();
446 }
447
448 // May only call this if hasTransformMatrix() is true
449 bool isTransformTranslateOnly() const {
450 return getTransformMatrix()->getType() == SkMatrix::kTranslate_Mask;
John Reckd0a0b2a2014-03-20 16:28:56 -0700451 }
452
Chris Craik49e6c732014-03-31 12:34:11 -0700453 const SkMatrix* getTransformMatrix() const {
John Reckf7483e32014-04-11 08:54:47 -0700454 LOG_ALWAYS_FATAL_IF(mPrimitiveFields.mMatrixOrPivotDirty, "Cannot get a dirty matrix!");
John Reckd0a0b2a2014-03-20 16:28:56 -0700455 return mComputedFields.mTransformMatrix;
456 }
457
John Reck1bcacfd2017-11-03 10:12:19 -0700458 int getClippingFlags() const { return mPrimitiveFields.mClippingFlags; }
Chris Craika753f4c2014-07-24 12:39:17 -0700459
John Reck1bcacfd2017-11-03 10:12:19 -0700460 bool getClipToBounds() const { return mPrimitiveFields.mClippingFlags & CLIP_TO_BOUNDS; }
Chris Craika753f4c2014-07-24 12:39:17 -0700461
John Reck1bcacfd2017-11-03 10:12:19 -0700462 const Rect& getClipBounds() const { return mPrimitiveFields.mClipBounds; }
John Recke248bd12015-08-05 13:53:53 -0700463
Chris Craika753f4c2014-07-24 12:39:17 -0700464 void getClippingRectForFlags(uint32_t flags, Rect* outRect) const {
465 if (flags & CLIP_TO_BOUNDS) {
466 outRect->set(0, 0, getWidth(), getHeight());
467 if (flags & CLIP_TO_CLIP_BOUNDS) {
Chris Craikac02eb92015-10-05 12:23:46 -0700468 outRect->doIntersect(mPrimitiveFields.mClipBounds);
Chris Craika753f4c2014-07-24 12:39:17 -0700469 }
470 } else {
471 outRect->set(mPrimitiveFields.mClipBounds);
472 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700473 }
474
John Reck1bcacfd2017-11-03 10:12:19 -0700475 bool getHasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700476
John Reck1bcacfd2017-11-03 10:12:19 -0700477 const Outline& getOutline() const { return mPrimitiveFields.mOutline; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700478
John Reck1bcacfd2017-11-03 10:12:19 -0700479 const RevealClip& getRevealClip() const { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700480
John Reck1bcacfd2017-11-03 10:12:19 -0700481 bool getProjectBackwards() const { return mPrimitiveFields.mProjectBackwards; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700482
sergeyvc3849aa2016-08-08 13:22:06 -0700483 void debugOutputProperties(std::ostream& output, const int level) const;
John Reckd0a0b2a2014-03-20 16:28:56 -0700484
John Reck25fbb3f2014-06-12 13:46:45 -0700485 void updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700486
John Reck1bcacfd2017-11-03 10:12:19 -0700487 Outline& mutableOutline() { return mPrimitiveFields.mOutline; }
Chris Craikb49f4462014-03-20 12:44:20 -0700488
John Reck1bcacfd2017-11-03 10:12:19 -0700489 RevealClip& mutableRevealClip() { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700490
John Reck1bcacfd2017-11-03 10:12:19 -0700491 const LayerProperties& layerProperties() const { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700492
John Reck1bcacfd2017-11-03 10:12:19 -0700493 LayerProperties& mutateLayerProperties() { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700494
John Reck293e8682014-06-17 10:34:02 -0700495 // Returns true if damage calculations should be clipped to bounds
496 // TODO: Figure out something better for getZ(), as children should still be
497 // clipped to this RP's bounds. But as we will damage -INT_MAX to INT_MAX
498 // for this RP's getZ() anyway, this can be optimized when we have a
499 // Z damage estimate instead of INT_MAX
500 bool getClipDamageToBounds() const {
501 return getClipToBounds() && (getZ() <= 0 || getOutline().isEmpty());
502 }
503
Chris Craik5c75c522014-09-05 14:08:08 -0700504 bool hasShadow() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700505 return getZ() > 0.0f && getOutline().getPath() != nullptr &&
506 getOutline().getAlpha() != 0.0f;
Chris Craik5c75c522014-09-05 14:08:08 -0700507 }
508
John Recke170fb62018-05-07 08:12:07 -0700509 SkColor getSpotShadowColor() const { return mPrimitiveFields.mSpotShadowColor; }
John Reck3c0369b2017-11-13 16:47:35 -0800510
John Reckd8be4a02017-11-17 15:06:24 -0800511 bool setSpotShadowColor(SkColor shadowColor) {
512 return RP_SET(mPrimitiveFields.mSpotShadowColor, shadowColor);
513 }
514
John Recke170fb62018-05-07 08:12:07 -0700515 SkColor getAmbientShadowColor() const { return mPrimitiveFields.mAmbientShadowColor; }
John Reckd8be4a02017-11-17 15:06:24 -0800516
517 bool setAmbientShadowColor(SkColor shadowColor) {
518 return RP_SET(mPrimitiveFields.mAmbientShadowColor, shadowColor);
John Reck3c0369b2017-11-13 16:47:35 -0800519 }
520
Chris Craik9fded232015-11-11 16:42:34 -0800521 bool fitsOnLayer() const {
Chris Craik76caecf2015-11-02 19:17:45 -0800522 const DeviceInfo* deviceInfo = DeviceInfo::get();
John Reck1bcacfd2017-11-03 10:12:19 -0700523 return mPrimitiveFields.mWidth <= deviceInfo->maxTextureSize() &&
524 mPrimitiveFields.mHeight <= deviceInfo->maxTextureSize();
Chris Craik9fded232015-11-11 16:42:34 -0800525 }
526
527 bool promotedToLayer() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700528 return mLayerProperties.mType == LayerType::None && fitsOnLayer() &&
529 (mComputedFields.mNeedLayerForFunctors ||
530 (!MathUtils::isZero(mPrimitiveFields.mAlpha) && mPrimitiveFields.mAlpha < 1 &&
531 mPrimitiveFields.mHasOverlappingRendering));
Chris Craik1a0808e2015-05-13 16:33:04 -0700532 }
533
534 LayerType effectiveLayerType() const {
Chris Craika766cb22015-06-08 16:49:43 -0700535 return CC_UNLIKELY(promotedToLayer()) ? LayerType::RenderLayer : mLayerProperties.mType;
Chris Craik856f0cc2015-04-21 15:13:29 -0700536 }
537
John Reck1423e132018-09-21 14:30:19 -0700538 bool setAllowForceDark(bool allow) {
539 return RP_SET(mPrimitiveFields.mAllowForceDark, allow);
540 }
541
542 bool getAllowForceDark() const {
543 return mPrimitiveFields.mAllowForceDark;
544 }
545
John Reckacb6f072014-03-12 16:11:23 -0700546private:
John Reckacb6f072014-03-12 16:11:23 -0700547 // Rendering properties
John Reckd0a0b2a2014-03-20 16:28:56 -0700548 struct PrimitiveFields {
John Recke248bd12015-08-05 13:53:53 -0700549 int mLeft = 0, mTop = 0, mRight = 0, mBottom = 0;
550 int mWidth = 0, mHeight = 0;
551 int mClippingFlags = CLIP_TO_BOUNDS;
John Reckd8be4a02017-11-17 15:06:24 -0800552 SkColor mSpotShadowColor = SK_ColorBLACK;
553 SkColor mAmbientShadowColor = SK_ColorBLACK;
John Recke248bd12015-08-05 13:53:53 -0700554 float mAlpha = 1;
555 float mTranslationX = 0, mTranslationY = 0, mTranslationZ = 0;
556 float mElevation = 0;
557 float mRotation = 0, mRotationX = 0, mRotationY = 0;
558 float mScaleX = 1, mScaleY = 1;
559 float mPivotX = 0, mPivotY = 0;
560 bool mHasOverlappingRendering = false;
561 bool mPivotExplicitlySet = false;
562 bool mMatrixOrPivotDirty = false;
563 bool mProjectBackwards = false;
564 bool mProjectionReceiver = false;
John Reck1423e132018-09-21 14:30:19 -0700565 bool mAllowForceDark = true;
John Recke248bd12015-08-05 13:53:53 -0700566 Rect mClipBounds;
John Reckd0a0b2a2014-03-20 16:28:56 -0700567 Outline mOutline;
Chris Craik8c271ca2014-03-25 10:33:01 -0700568 RevealClip mRevealClip;
John Reckd0a0b2a2014-03-20 16:28:56 -0700569 } mPrimitiveFields;
570
John Reckacb6f072014-03-12 16:11:23 -0700571 SkMatrix* mStaticMatrix;
572 SkMatrix* mAnimationMatrix;
John Reck25fbb3f2014-06-12 13:46:45 -0700573 LayerProperties mLayerProperties;
John Reckacb6f072014-03-12 16:11:23 -0700574
John Reckd0a0b2a2014-03-20 16:28:56 -0700575 /**
576 * These fields are all generated from other properties and are not set directly.
577 */
578 struct ComputedFields {
579 ComputedFields();
580 ~ComputedFields();
581
582 /**
583 * Stores the total transformation of the DisplayList based upon its scalar
584 * translate/rotate/scale properties.
585 *
Chris Craik49e6c732014-03-31 12:34:11 -0700586 * In the common translation-only case, the matrix isn't necessarily allocated,
587 * and the mTranslation properties are used directly.
John Reckd0a0b2a2014-03-20 16:28:56 -0700588 */
Chris Craik49e6c732014-03-31 12:34:11 -0700589 SkMatrix* mTransformMatrix;
590
591 Sk3DView mTransformCamera;
Chris Craika766cb22015-06-08 16:49:43 -0700592
593 // Force layer on for functors to enable render features they don't yet support (clipping)
594 bool mNeedLayerForFunctors = false;
John Reckd0a0b2a2014-03-20 16:28:56 -0700595 } mComputedFields;
John Reckacb6f072014-03-12 16:11:23 -0700596};
597
598} /* namespace uirenderer */
599} /* namespace android */