blob: 3d2c2520624b3534dedcd0cc17e3f24ed61d8e20 [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 "Caches.h"
20#include "DeviceInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070021#include "Outline.h"
Chris Craik76caecf2015-11-02 19:17:45 -080022#include "Rect.h"
23#include "RevealClip.h"
Chris Craik76caecf2015-11-02 19:17:45 -080024#include "utils/MathUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070025#include "utils/PaintUtils.h"
John Reckacb6f072014-03-12 16:11:23 -070026
Mike Reed260ab722016-10-07 15:59:20 -040027#include <SkBlendMode.h>
John Reckacb6f072014-03-12 16:11:23 -070028#include <SkCamera.h>
John Reck3c0369b2017-11-13 16:47:35 -080029#include <SkColor.h>
John Reckacb6f072014-03-12 16:11:23 -070030#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070031#include <SkRegion.h>
Chris Craikb49f4462014-03-20 12:44:20 -070032
Chris Craik76caecf2015-11-02 19:17:45 -080033#include <androidfw/ResourceTypes.h>
John Reck1bcacfd2017-11-03 10:12:19 -070034#include <cutils/compiler.h>
35#include <stddef.h>
Chris Craik76caecf2015-11-02 19:17:45 -080036#include <utils/Log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070037#include <algorithm>
sergeyvc3849aa2016-08-08 13:22:06 -070038#include <ostream>
John Reck1bcacfd2017-11-03 10:12:19 -070039#include <vector>
John Reckacb6f072014-03-12 16:11:23 -070040
John Reckacb6f072014-03-12 16:11:23 -070041class SkBitmap;
John Reck25fbb3f2014-06-12 13:46:45 -070042class SkColorFilter;
John Reckacb6f072014-03-12 16:11:23 -070043class SkPaint;
John Reckacb6f072014-03-12 16:11:23 -070044
45namespace android {
46namespace uirenderer {
47
48class Matrix4;
49class RenderNode;
John Reck25fbb3f2014-06-12 13:46:45 -070050class RenderProperties;
John Reckacb6f072014-03-12 16:11:23 -070051
John Reck79c7de72014-05-23 10:33:31 -070052// The __VA_ARGS__ will be executed if a & b are not equal
Chih-Hung Hsiehcef190d2016-05-19 15:25:50 -070053#define RP_SET(a, b, ...) ((a) != (b) ? ((a) = (b), ##__VA_ARGS__, true) : false)
John Reck79c7de72014-05-23 10:33:31 -070054#define RP_SET_AND_DIRTY(a, b) RP_SET(a, b, mPrimitiveFields.mMatrixOrPivotDirty = true)
55
John Reck25fbb3f2014-06-12 13:46:45 -070056// Keep in sync with View.java:LAYER_TYPE_*
Chris Craik182952f2015-03-09 14:17:29 -070057enum class LayerType {
58 None = 0,
John Reck25fbb3f2014-06-12 13:46:45 -070059 // Although we cannot build the software layer directly (must be done at
60 // record time), this information is used when applying alpha.
Chris Craik182952f2015-03-09 14:17:29 -070061 Software = 1,
62 RenderLayer = 2,
John Reck25fbb3f2014-06-12 13:46:45 -070063 // TODO: LayerTypeSurfaceTexture? Maybe?
64};
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
John Reck25fbb3f2014-06-12 13:46:45 -070071class ANDROID_API LayerProperties {
72public:
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
93 bool setColorFilter(SkColorFilter* filter);
94
John Reck1bcacfd2017-11-03 10:12:19 -070095 SkColorFilter* colorFilter() const { return mColorFilter; }
John Reck25fbb3f2014-06-12 13:46:45 -070096
97 // Sets alpha, xfermode, and colorfilter from an SkPaint
98 // paint may be NULL, in which case defaults will be set
99 bool setFromPaint(const SkPaint* paint);
100
John Reck1bcacfd2017-11-03 10:12:19 -0700101 bool needsBlending() const { return !opaque() || alpha() < 255; }
John Reck25fbb3f2014-06-12 13:46:45 -0700102
103 LayerProperties& operator=(const LayerProperties& other);
104
105private:
106 LayerProperties();
107 ~LayerProperties();
108 void reset();
109
Chris Craik856f0cc2015-04-21 15:13:29 -0700110 // Private since external users should go through properties().effectiveLayerType()
John Reck1bcacfd2017-11-03 10:12:19 -0700111 LayerType type() const { return mType; }
Chris Craik856f0cc2015-04-21 15:13:29 -0700112
John Reck25fbb3f2014-06-12 13:46:45 -0700113 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;
Chris Craik182952f2015-03-09 14:17:29 -0700120 SkColorFilter* mColorFilter = nullptr;
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
Chris Craika766cb22015-06-08 16:49:43 -0700157
158 // Round rect clipping forces layer for functors
John Reck1bcacfd2017-11-03 10:12:19 -0700159 || CC_UNLIKELY(getOutline().willRoundRectClip()) ||
160 CC_UNLIKELY(getRevealClip().willClip())
Chris Craika766cb22015-06-08 16:49:43 -0700161
162 // Complex matrices forces layer, due to stencil clipping
John Reck1bcacfd2017-11-03 10:12:19 -0700163 || CC_UNLIKELY(getTransformMatrix() && !getTransformMatrix()->isScaleTranslate()) ||
164 CC_UNLIKELY(getAnimationMatrix() && !getAnimationMatrix()->isScaleTranslate()) ||
165 CC_UNLIKELY(getStaticMatrix() && !getStaticMatrix()->isScaleTranslate());
Chris Craika766cb22015-06-08 16:49:43 -0700166
167 mComputedFields.mNeedLayerForFunctors = (willHaveFunctor && functorsNeedLayer);
168
169 // If on a layer, will have consumed the need for isolating functors from stencil.
170 // Thus, it's safe to reset the flag until some descendent sets it.
171 return CC_LIKELY(effectiveLayerType() == LayerType::None) && functorsNeedLayer;
172 }
173
John Reckd0a0b2a2014-03-20 16:28:56 -0700174 RenderProperties& operator=(const RenderProperties& other);
175
John Reck79c7de72014-05-23 10:33:31 -0700176 bool setClipToBounds(bool clipToBounds) {
Chris Craika753f4c2014-07-24 12:39:17 -0700177 return setFlag(CLIP_TO_BOUNDS, clipToBounds, &mPrimitiveFields.mClippingFlags);
178 }
179
180 bool setClipBounds(const Rect& clipBounds) {
181 bool ret = setFlag(CLIP_TO_CLIP_BOUNDS, true, &mPrimitiveFields.mClippingFlags);
182 return RP_SET(mPrimitiveFields.mClipBounds, clipBounds) || ret;
183 }
184
185 bool setClipBoundsEmpty() {
186 return setFlag(CLIP_TO_CLIP_BOUNDS, false, &mPrimitiveFields.mClippingFlags);
John Reckacb6f072014-03-12 16:11:23 -0700187 }
188
John Reck79c7de72014-05-23 10:33:31 -0700189 bool setProjectBackwards(bool shouldProject) {
190 return RP_SET(mPrimitiveFields.mProjectBackwards, shouldProject);
John Reckacb6f072014-03-12 16:11:23 -0700191 }
192
Chris Craik6fe991e52015-10-20 09:39:42 -0700193 bool setProjectionReceiver(bool shouldReceive) {
194 return RP_SET(mPrimitiveFields.mProjectionReceiver, shouldReceive);
John Reckacb6f072014-03-12 16:11:23 -0700195 }
196
John Reck1bcacfd2017-11-03 10:12:19 -0700197 bool isProjectionReceiver() const { return mPrimitiveFields.mProjectionReceiver; }
John Reckacb6f072014-03-12 16:11:23 -0700198
John Reck79c7de72014-05-23 10:33:31 -0700199 bool setStaticMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700200 delete mStaticMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -0700201 if (matrix) {
202 mStaticMatrix = new SkMatrix(*matrix);
203 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800204 mStaticMatrix = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700205 }
John Reck79c7de72014-05-23 10:33:31 -0700206 return true;
John Reckacb6f072014-03-12 16:11:23 -0700207 }
208
209 // Can return NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700210 const SkMatrix* getStaticMatrix() const { return mStaticMatrix; }
John Reckacb6f072014-03-12 16:11:23 -0700211
John Reck79c7de72014-05-23 10:33:31 -0700212 bool setAnimationMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700213 delete mAnimationMatrix;
214 if (matrix) {
215 mAnimationMatrix = new SkMatrix(*matrix);
216 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800217 mAnimationMatrix = nullptr;
John Reckacb6f072014-03-12 16:11:23 -0700218 }
John Reck79c7de72014-05-23 10:33:31 -0700219 return true;
John Reckacb6f072014-03-12 16:11:23 -0700220 }
221
John Reck79c7de72014-05-23 10:33:31 -0700222 bool setAlpha(float alpha) {
John Reck3b52c032014-08-06 10:19:32 -0700223 alpha = MathUtils::clampAlpha(alpha);
John Reck79c7de72014-05-23 10:33:31 -0700224 return RP_SET(mPrimitiveFields.mAlpha, alpha);
John Reckacb6f072014-03-12 16:11:23 -0700225 }
226
John Reck1bcacfd2017-11-03 10:12:19 -0700227 float getAlpha() const { return mPrimitiveFields.mAlpha; }
John Reckacb6f072014-03-12 16:11:23 -0700228
John Reck79c7de72014-05-23 10:33:31 -0700229 bool setHasOverlappingRendering(bool hasOverlappingRendering) {
230 return RP_SET(mPrimitiveFields.mHasOverlappingRendering, hasOverlappingRendering);
John Reckacb6f072014-03-12 16:11:23 -0700231 }
232
John Reck1bcacfd2017-11-03 10:12:19 -0700233 bool hasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckacb6f072014-03-12 16:11:23 -0700234
John Reck79c7de72014-05-23 10:33:31 -0700235 bool setElevation(float elevation) {
236 return RP_SET(mPrimitiveFields.mElevation, elevation);
237 // Don't dirty matrix/pivot, since they don't respect Z
Chris Craikcc39e162014-04-25 18:34:11 -0700238 }
239
John Reck1bcacfd2017-11-03 10:12:19 -0700240 float getElevation() const { return mPrimitiveFields.mElevation; }
Chris Craikcc39e162014-04-25 18:34:11 -0700241
John Reck79c7de72014-05-23 10:33:31 -0700242 bool setTranslationX(float translationX) {
243 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationX, translationX);
John Reckacb6f072014-03-12 16:11:23 -0700244 }
245
John Reck1bcacfd2017-11-03 10:12:19 -0700246 float getTranslationX() const { return mPrimitiveFields.mTranslationX; }
John Reckacb6f072014-03-12 16:11:23 -0700247
John Reck79c7de72014-05-23 10:33:31 -0700248 bool setTranslationY(float translationY) {
249 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationY, translationY);
John Reckacb6f072014-03-12 16:11:23 -0700250 }
251
John Reck1bcacfd2017-11-03 10:12:19 -0700252 float getTranslationY() const { return mPrimitiveFields.mTranslationY; }
John Reckacb6f072014-03-12 16:11:23 -0700253
John Reck79c7de72014-05-23 10:33:31 -0700254 bool setTranslationZ(float translationZ) {
255 return RP_SET(mPrimitiveFields.mTranslationZ, translationZ);
256 // mMatrixOrPivotDirty not set, since matrix doesn't respect Z
John Reckacb6f072014-03-12 16:11:23 -0700257 }
258
John Reck1bcacfd2017-11-03 10:12:19 -0700259 float getTranslationZ() const { return mPrimitiveFields.mTranslationZ; }
John Reckacb6f072014-03-12 16:11:23 -0700260
John Recke45b1fd2014-04-15 09:50:16 -0700261 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700262 bool setX(float value) { return setTranslationX(value - getLeft()); }
John Recke45b1fd2014-04-15 09:50:16 -0700263
264 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700265 float getX() const { return getLeft() + getTranslationX(); }
John Recke45b1fd2014-04-15 09:50:16 -0700266
267 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700268 bool setY(float value) { return setTranslationY(value - getTop()); }
John Recke45b1fd2014-04-15 09:50:16 -0700269
270 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700271 float getY() const { return getTop() + getTranslationY(); }
John Recke45b1fd2014-04-15 09:50:16 -0700272
273 // Animation helper
John Reck1bcacfd2017-11-03 10:12:19 -0700274 bool setZ(float value) { return setTranslationZ(value - getElevation()); }
John Recke45b1fd2014-04-15 09:50:16 -0700275
John Reck1bcacfd2017-11-03 10:12:19 -0700276 float getZ() const { return getElevation() + getTranslationZ(); }
Chris Craikcc39e162014-04-25 18:34:11 -0700277
John Reck79c7de72014-05-23 10:33:31 -0700278 bool setRotation(float rotation) {
279 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotation, rotation);
John Reckacb6f072014-03-12 16:11:23 -0700280 }
281
John Reck1bcacfd2017-11-03 10:12:19 -0700282 float getRotation() const { return mPrimitiveFields.mRotation; }
John Reckacb6f072014-03-12 16:11:23 -0700283
John Reck79c7de72014-05-23 10:33:31 -0700284 bool setRotationX(float rotationX) {
285 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationX, rotationX);
John Reckacb6f072014-03-12 16:11:23 -0700286 }
287
John Reck1bcacfd2017-11-03 10:12:19 -0700288 float getRotationX() const { return mPrimitiveFields.mRotationX; }
John Reckacb6f072014-03-12 16:11:23 -0700289
John Reck79c7de72014-05-23 10:33:31 -0700290 bool setRotationY(float rotationY) {
291 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationY, rotationY);
John Reckacb6f072014-03-12 16:11:23 -0700292 }
293
John Reck1bcacfd2017-11-03 10:12:19 -0700294 float getRotationY() const { return mPrimitiveFields.mRotationY; }
John Reckacb6f072014-03-12 16:11:23 -0700295
John Reck1bcacfd2017-11-03 10:12:19 -0700296 bool setScaleX(float scaleX) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleX, scaleX); }
John Reckacb6f072014-03-12 16:11:23 -0700297
John Reck1bcacfd2017-11-03 10:12:19 -0700298 float getScaleX() const { return mPrimitiveFields.mScaleX; }
John Reckacb6f072014-03-12 16:11:23 -0700299
John Reck1bcacfd2017-11-03 10:12:19 -0700300 bool setScaleY(float scaleY) { return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleY, scaleY); }
John Reckacb6f072014-03-12 16:11:23 -0700301
John Reck1bcacfd2017-11-03 10:12:19 -0700302 float getScaleY() const { return mPrimitiveFields.mScaleY; }
John Reckacb6f072014-03-12 16:11:23 -0700303
John Reck79c7de72014-05-23 10:33:31 -0700304 bool setPivotX(float pivotX) {
John Reck1bcacfd2017-11-03 10:12:19 -0700305 if (RP_SET(mPrimitiveFields.mPivotX, pivotX) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700306 mPrimitiveFields.mMatrixOrPivotDirty = true;
307 mPrimitiveFields.mPivotExplicitlySet = true;
308 return true;
309 }
310 return false;
John Reckacb6f072014-03-12 16:11:23 -0700311 }
312
John Reckd0a0b2a2014-03-20 16:28:56 -0700313 /* Note that getPivotX and getPivotY are adjusted by updateMatrix(),
John Reck79c7de72014-05-23 10:33:31 -0700314 * so the value returned may be stale if the RenderProperties has been
315 * modified since the last call to updateMatrix()
John Reckd0a0b2a2014-03-20 16:28:56 -0700316 */
John Reck1bcacfd2017-11-03 10:12:19 -0700317 float getPivotX() const { return mPrimitiveFields.mPivotX; }
John Reckacb6f072014-03-12 16:11:23 -0700318
John Reck79c7de72014-05-23 10:33:31 -0700319 bool setPivotY(float pivotY) {
John Reck1bcacfd2017-11-03 10:12:19 -0700320 if (RP_SET(mPrimitiveFields.mPivotY, pivotY) || !mPrimitiveFields.mPivotExplicitlySet) {
John Reck79c7de72014-05-23 10:33:31 -0700321 mPrimitiveFields.mMatrixOrPivotDirty = true;
322 mPrimitiveFields.mPivotExplicitlySet = true;
323 return true;
324 }
325 return false;
John Reckacb6f072014-03-12 16:11:23 -0700326 }
327
John Reck1bcacfd2017-11-03 10:12:19 -0700328 float getPivotY() const { return mPrimitiveFields.mPivotY; }
John Reckacb6f072014-03-12 16:11:23 -0700329
John Reck1bcacfd2017-11-03 10:12:19 -0700330 bool isPivotExplicitlySet() const { return mPrimitiveFields.mPivotExplicitlySet; }
Chris Craik49e6c7392014-03-31 12:34:11 -0700331
John Reck79c7de72014-05-23 10:33:31 -0700332 bool setCameraDistance(float distance) {
Chris Craik49e6c7392014-03-31 12:34:11 -0700333 if (distance != getCameraDistance()) {
John Reckf7483e32014-04-11 08:54:47 -0700334 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -0700335 mComputedFields.mTransformCamera.setCameraLocation(0, 0, distance);
John Reck79c7de72014-05-23 10:33:31 -0700336 return true;
John Reckacb6f072014-03-12 16:11:23 -0700337 }
John Reck79c7de72014-05-23 10:33:31 -0700338 return false;
John Reckacb6f072014-03-12 16:11:23 -0700339 }
340
341 float getCameraDistance() const {
Chris Craik49e6c7392014-03-31 12:34:11 -0700342 // TODO: update getCameraLocationZ() to be const
343 return const_cast<Sk3DView*>(&mComputedFields.mTransformCamera)->getCameraLocationZ();
John Reckacb6f072014-03-12 16:11:23 -0700344 }
345
John Reck79c7de72014-05-23 10:33:31 -0700346 bool setLeft(int left) {
347 if (RP_SET(mPrimitiveFields.mLeft, left)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700348 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700349 if (!mPrimitiveFields.mPivotExplicitlySet) {
350 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700351 }
John Reck79c7de72014-05-23 10:33:31 -0700352 return true;
John Reckacb6f072014-03-12 16:11:23 -0700353 }
John Reck79c7de72014-05-23 10:33:31 -0700354 return false;
John Reckacb6f072014-03-12 16:11:23 -0700355 }
356
John Reck1bcacfd2017-11-03 10:12:19 -0700357 int getLeft() const { return mPrimitiveFields.mLeft; }
John Reckacb6f072014-03-12 16:11:23 -0700358
John Reck79c7de72014-05-23 10:33:31 -0700359 bool setTop(int top) {
360 if (RP_SET(mPrimitiveFields.mTop, top)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700361 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700362 if (!mPrimitiveFields.mPivotExplicitlySet) {
363 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700364 }
John Reck79c7de72014-05-23 10:33:31 -0700365 return true;
John Reckacb6f072014-03-12 16:11:23 -0700366 }
John Reck79c7de72014-05-23 10:33:31 -0700367 return false;
John Reckacb6f072014-03-12 16:11:23 -0700368 }
369
John Reck1bcacfd2017-11-03 10:12:19 -0700370 int getTop() const { return mPrimitiveFields.mTop; }
John Reckacb6f072014-03-12 16:11:23 -0700371
John Reck79c7de72014-05-23 10:33:31 -0700372 bool setRight(int right) {
373 if (RP_SET(mPrimitiveFields.mRight, right)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700374 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700375 if (!mPrimitiveFields.mPivotExplicitlySet) {
376 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700377 }
John Reck79c7de72014-05-23 10:33:31 -0700378 return true;
John Reckacb6f072014-03-12 16:11:23 -0700379 }
John Reck79c7de72014-05-23 10:33:31 -0700380 return false;
John Reckacb6f072014-03-12 16:11:23 -0700381 }
382
John Reck1bcacfd2017-11-03 10:12:19 -0700383 int getRight() const { return mPrimitiveFields.mRight; }
John Reckacb6f072014-03-12 16:11:23 -0700384
John Reck79c7de72014-05-23 10:33:31 -0700385 bool setBottom(int bottom) {
386 if (RP_SET(mPrimitiveFields.mBottom, bottom)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700387 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700388 if (!mPrimitiveFields.mPivotExplicitlySet) {
389 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700390 }
John Reck79c7de72014-05-23 10:33:31 -0700391 return true;
John Reckacb6f072014-03-12 16:11:23 -0700392 }
John Reck79c7de72014-05-23 10:33:31 -0700393 return false;
John Reckacb6f072014-03-12 16:11:23 -0700394 }
395
John Reck1bcacfd2017-11-03 10:12:19 -0700396 int getBottom() const { return mPrimitiveFields.mBottom; }
John Reckacb6f072014-03-12 16:11:23 -0700397
John Reck79c7de72014-05-23 10:33:31 -0700398 bool setLeftTop(int left, int top) {
399 bool leftResult = setLeft(left);
400 bool topResult = setTop(top);
401 return leftResult || topResult;
John Reckacb6f072014-03-12 16:11:23 -0700402 }
403
John Reck79c7de72014-05-23 10:33:31 -0700404 bool setLeftTopRightBottom(int left, int top, int right, int bottom) {
John Reck1bcacfd2017-11-03 10:12:19 -0700405 if (left != mPrimitiveFields.mLeft || top != mPrimitiveFields.mTop ||
406 right != mPrimitiveFields.mRight || bottom != mPrimitiveFields.mBottom) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700407 mPrimitiveFields.mLeft = left;
408 mPrimitiveFields.mTop = top;
409 mPrimitiveFields.mRight = right;
410 mPrimitiveFields.mBottom = bottom;
411 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
412 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700413 if (!mPrimitiveFields.mPivotExplicitlySet) {
414 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700415 }
John Reck79c7de72014-05-23 10:33:31 -0700416 return true;
John Reckacb6f072014-03-12 16:11:23 -0700417 }
John Reck79c7de72014-05-23 10:33:31 -0700418 return false;
John Reckacb6f072014-03-12 16:11:23 -0700419 }
420
Chris Craika753f4c2014-07-24 12:39:17 -0700421 bool offsetLeftRight(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700422 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700423 mPrimitiveFields.mLeft += offset;
424 mPrimitiveFields.mRight += offset;
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 offsetTopBottom(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700431 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700432 mPrimitiveFields.mTop += offset;
433 mPrimitiveFields.mBottom += 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
John Reck1bcacfd2017-11-03 10:12:19 -0700439 int getWidth() const { return mPrimitiveFields.mWidth; }
John Reckacb6f072014-03-12 16:11:23 -0700440
John Reck1bcacfd2017-11-03 10:12:19 -0700441 int getHeight() const { return mPrimitiveFields.mHeight; }
John Reckacb6f072014-03-12 16:11:23 -0700442
John Reck1bcacfd2017-11-03 10:12:19 -0700443 const SkMatrix* getAnimationMatrix() const { return mAnimationMatrix; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700444
John Reckf7483e32014-04-11 08:54:47 -0700445 bool hasTransformMatrix() const {
446 return getTransformMatrix() && !getTransformMatrix()->isIdentity();
447 }
448
449 // May only call this if hasTransformMatrix() is true
450 bool isTransformTranslateOnly() const {
451 return getTransformMatrix()->getType() == SkMatrix::kTranslate_Mask;
John Reckd0a0b2a2014-03-20 16:28:56 -0700452 }
453
Chris Craik49e6c7392014-03-31 12:34:11 -0700454 const SkMatrix* getTransformMatrix() const {
John Reckf7483e32014-04-11 08:54:47 -0700455 LOG_ALWAYS_FATAL_IF(mPrimitiveFields.mMatrixOrPivotDirty, "Cannot get a dirty matrix!");
John Reckd0a0b2a2014-03-20 16:28:56 -0700456 return mComputedFields.mTransformMatrix;
457 }
458
John Reck1bcacfd2017-11-03 10:12:19 -0700459 int getClippingFlags() const { return mPrimitiveFields.mClippingFlags; }
Chris Craika753f4c2014-07-24 12:39:17 -0700460
John Reck1bcacfd2017-11-03 10:12:19 -0700461 bool getClipToBounds() const { return mPrimitiveFields.mClippingFlags & CLIP_TO_BOUNDS; }
Chris Craika753f4c2014-07-24 12:39:17 -0700462
John Reck1bcacfd2017-11-03 10:12:19 -0700463 const Rect& getClipBounds() const { return mPrimitiveFields.mClipBounds; }
John Recke248bd12015-08-05 13:53:53 -0700464
Chris Craika753f4c2014-07-24 12:39:17 -0700465 void getClippingRectForFlags(uint32_t flags, Rect* outRect) const {
466 if (flags & CLIP_TO_BOUNDS) {
467 outRect->set(0, 0, getWidth(), getHeight());
468 if (flags & CLIP_TO_CLIP_BOUNDS) {
Chris Craikac02eb92015-10-05 12:23:46 -0700469 outRect->doIntersect(mPrimitiveFields.mClipBounds);
Chris Craika753f4c2014-07-24 12:39:17 -0700470 }
471 } else {
472 outRect->set(mPrimitiveFields.mClipBounds);
473 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700474 }
475
John Reck1bcacfd2017-11-03 10:12:19 -0700476 bool getHasOverlappingRendering() const { return mPrimitiveFields.mHasOverlappingRendering; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700477
John Reck1bcacfd2017-11-03 10:12:19 -0700478 const Outline& getOutline() const { return mPrimitiveFields.mOutline; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700479
John Reck1bcacfd2017-11-03 10:12:19 -0700480 const RevealClip& getRevealClip() const { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700481
John Reck1bcacfd2017-11-03 10:12:19 -0700482 bool getProjectBackwards() const { return mPrimitiveFields.mProjectBackwards; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700483
sergeyvc3849aa2016-08-08 13:22:06 -0700484 void debugOutputProperties(std::ostream& output, const int level) const;
John Reckd0a0b2a2014-03-20 16:28:56 -0700485
John Reck25fbb3f2014-06-12 13:46:45 -0700486 void updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700487
John Reck1bcacfd2017-11-03 10:12:19 -0700488 Outline& mutableOutline() { return mPrimitiveFields.mOutline; }
Chris Craikb49f4462014-03-20 12:44:20 -0700489
John Reck1bcacfd2017-11-03 10:12:19 -0700490 RevealClip& mutableRevealClip() { return mPrimitiveFields.mRevealClip; }
Chris Craik8c271ca2014-03-25 10:33:01 -0700491
John Reck1bcacfd2017-11-03 10:12:19 -0700492 const LayerProperties& layerProperties() const { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700493
John Reck1bcacfd2017-11-03 10:12:19 -0700494 LayerProperties& mutateLayerProperties() { return mLayerProperties; }
John Reck25fbb3f2014-06-12 13:46:45 -0700495
John Reck293e8682014-06-17 10:34:02 -0700496 // Returns true if damage calculations should be clipped to bounds
497 // TODO: Figure out something better for getZ(), as children should still be
498 // clipped to this RP's bounds. But as we will damage -INT_MAX to INT_MAX
499 // for this RP's getZ() anyway, this can be optimized when we have a
500 // Z damage estimate instead of INT_MAX
501 bool getClipDamageToBounds() const {
502 return getClipToBounds() && (getZ() <= 0 || getOutline().isEmpty());
503 }
504
Chris Craik5c75c522014-09-05 14:08:08 -0700505 bool hasShadow() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700506 return getZ() > 0.0f && getOutline().getPath() != nullptr &&
507 getOutline().getAlpha() != 0.0f;
Chris Craik5c75c522014-09-05 14:08:08 -0700508 }
509
John Reck3c0369b2017-11-13 16:47:35 -0800510 SkColor getShadowColor() const {
511 return mPrimitiveFields.mShadowColor;
512 }
513
514 bool setShadowColor(SkColor shadowColor) {
515 return RP_SET(mPrimitiveFields.mShadowColor, shadowColor);
516 }
517
Chris Craik9fded232015-11-11 16:42:34 -0800518 bool fitsOnLayer() const {
Chris Craik76caecf2015-11-02 19:17:45 -0800519 const DeviceInfo* deviceInfo = DeviceInfo::get();
John Reck1bcacfd2017-11-03 10:12:19 -0700520 return mPrimitiveFields.mWidth <= deviceInfo->maxTextureSize() &&
521 mPrimitiveFields.mHeight <= deviceInfo->maxTextureSize();
Chris Craik9fded232015-11-11 16:42:34 -0800522 }
523
524 bool promotedToLayer() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700525 return mLayerProperties.mType == LayerType::None && fitsOnLayer() &&
526 (mComputedFields.mNeedLayerForFunctors ||
527 (!MathUtils::isZero(mPrimitiveFields.mAlpha) && mPrimitiveFields.mAlpha < 1 &&
528 mPrimitiveFields.mHasOverlappingRendering));
Chris Craik1a0808e2015-05-13 16:33:04 -0700529 }
530
531 LayerType effectiveLayerType() const {
Chris Craika766cb22015-06-08 16:49:43 -0700532 return CC_UNLIKELY(promotedToLayer()) ? LayerType::RenderLayer : mLayerProperties.mType;
Chris Craik856f0cc2015-04-21 15:13:29 -0700533 }
534
John Reckacb6f072014-03-12 16:11:23 -0700535private:
John Reckacb6f072014-03-12 16:11:23 -0700536 // Rendering properties
John Reckd0a0b2a2014-03-20 16:28:56 -0700537 struct PrimitiveFields {
John Recke248bd12015-08-05 13:53:53 -0700538 int mLeft = 0, mTop = 0, mRight = 0, mBottom = 0;
539 int mWidth = 0, mHeight = 0;
540 int mClippingFlags = CLIP_TO_BOUNDS;
John Reck3c0369b2017-11-13 16:47:35 -0800541 SkColor mShadowColor = SK_ColorBLACK;
John Recke248bd12015-08-05 13:53:53 -0700542 float mAlpha = 1;
543 float mTranslationX = 0, mTranslationY = 0, mTranslationZ = 0;
544 float mElevation = 0;
545 float mRotation = 0, mRotationX = 0, mRotationY = 0;
546 float mScaleX = 1, mScaleY = 1;
547 float mPivotX = 0, mPivotY = 0;
548 bool mHasOverlappingRendering = false;
549 bool mPivotExplicitlySet = false;
550 bool mMatrixOrPivotDirty = false;
551 bool mProjectBackwards = false;
552 bool mProjectionReceiver = false;
553 Rect mClipBounds;
John Reckd0a0b2a2014-03-20 16:28:56 -0700554 Outline mOutline;
Chris Craik8c271ca2014-03-25 10:33:01 -0700555 RevealClip mRevealClip;
John Reckd0a0b2a2014-03-20 16:28:56 -0700556 } mPrimitiveFields;
557
John Reckacb6f072014-03-12 16:11:23 -0700558 SkMatrix* mStaticMatrix;
559 SkMatrix* mAnimationMatrix;
John Reck25fbb3f2014-06-12 13:46:45 -0700560 LayerProperties mLayerProperties;
John Reckacb6f072014-03-12 16:11:23 -0700561
John Reckd0a0b2a2014-03-20 16:28:56 -0700562 /**
563 * These fields are all generated from other properties and are not set directly.
564 */
565 struct ComputedFields {
566 ComputedFields();
567 ~ComputedFields();
568
569 /**
570 * Stores the total transformation of the DisplayList based upon its scalar
571 * translate/rotate/scale properties.
572 *
Chris Craik49e6c7392014-03-31 12:34:11 -0700573 * In the common translation-only case, the matrix isn't necessarily allocated,
574 * and the mTranslation properties are used directly.
John Reckd0a0b2a2014-03-20 16:28:56 -0700575 */
Chris Craik49e6c7392014-03-31 12:34:11 -0700576 SkMatrix* mTransformMatrix;
577
578 Sk3DView mTransformCamera;
Chris Craika766cb22015-06-08 16:49:43 -0700579
580 // Force layer on for functors to enable render features they don't yet support (clipping)
581 bool mNeedLayerForFunctors = false;
John Reckd0a0b2a2014-03-20 16:28:56 -0700582 } mComputedFields;
John Reckacb6f072014-03-12 16:11:23 -0700583};
584
585} /* namespace uirenderer */
586} /* namespace android */