blob: 395279806adb39b84d33c967e45e4e0a9988dee9 [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 Craikb49f4462014-03-20 12:44:20 -070016#ifndef RENDERNODEPROPERTIES_H
17#define RENDERNODEPROPERTIES_H
John Reckacb6f072014-03-12 16:11:23 -070018
Chris Craik76caecf2015-11-02 19:17:45 -080019#include "Caches.h"
20#include "DeviceInfo.h"
21#include "Rect.h"
22#include "RevealClip.h"
23#include "Outline.h"
24#include "utils/MathUtils.h"
John Reckacb6f072014-03-12 16:11:23 -070025
26#include <SkCamera.h>
27#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070028#include <SkRegion.h>
Tom Hudson2dc236b2014-10-15 15:46:42 -040029#include <SkXfermode.h>
Chris Craikb49f4462014-03-20 12:44:20 -070030
Chris Craik76caecf2015-11-02 19:17:45 -080031#include <algorithm>
32#include <stddef.h>
33#include <vector>
34#include <cutils/compiler.h>
35#include <androidfw/ResourceTypes.h>
36#include <utils/Log.h>
John Reckacb6f072014-03-12 16:11:23 -070037
John Reckacb6f072014-03-12 16:11:23 -070038class SkBitmap;
John Reck25fbb3f2014-06-12 13:46:45 -070039class SkColorFilter;
John Reckacb6f072014-03-12 16:11:23 -070040class SkPaint;
John Reckacb6f072014-03-12 16:11:23 -070041
42namespace android {
43namespace uirenderer {
44
45class Matrix4;
46class RenderNode;
John Reck25fbb3f2014-06-12 13:46:45 -070047class RenderProperties;
John Reckacb6f072014-03-12 16:11:23 -070048
John Reck79c7de72014-05-23 10:33:31 -070049// The __VA_ARGS__ will be executed if a & b are not equal
50#define RP_SET(a, b, ...) (a != b ? (a = b, ##__VA_ARGS__, true) : false)
51#define RP_SET_AND_DIRTY(a, b) RP_SET(a, b, mPrimitiveFields.mMatrixOrPivotDirty = true)
52
John Reck25fbb3f2014-06-12 13:46:45 -070053// Keep in sync with View.java:LAYER_TYPE_*
Chris Craik182952f2015-03-09 14:17:29 -070054enum class LayerType {
55 None = 0,
John Reck25fbb3f2014-06-12 13:46:45 -070056 // Although we cannot build the software layer directly (must be done at
57 // record time), this information is used when applying alpha.
Chris Craik182952f2015-03-09 14:17:29 -070058 Software = 1,
59 RenderLayer = 2,
John Reck25fbb3f2014-06-12 13:46:45 -070060 // TODO: LayerTypeSurfaceTexture? Maybe?
61};
62
Chris Craika753f4c2014-07-24 12:39:17 -070063enum ClippingFlags {
64 CLIP_TO_BOUNDS = 0x1 << 0,
65 CLIP_TO_CLIP_BOUNDS = 0x1 << 1,
66};
67
John Reck25fbb3f2014-06-12 13:46:45 -070068class ANDROID_API LayerProperties {
69public:
70 bool setType(LayerType type) {
71 if (RP_SET(mType, type)) {
72 reset();
73 return true;
74 }
75 return false;
76 }
77
John Reck25fbb3f2014-06-12 13:46:45 -070078 bool setOpaque(bool opaque) {
79 return RP_SET(mOpaque, opaque);
80 }
81
82 bool opaque() const {
83 return mOpaque;
84 }
85
86 bool setAlpha(uint8_t alpha) {
87 return RP_SET(mAlpha, alpha);
88 }
89
90 uint8_t alpha() const {
91 return mAlpha;
92 }
93
94 bool setXferMode(SkXfermode::Mode mode) {
95 return RP_SET(mMode, mode);
96 }
97
98 SkXfermode::Mode xferMode() const {
99 return mMode;
100 }
101
102 bool setColorFilter(SkColorFilter* filter);
103
104 SkColorFilter* colorFilter() const {
105 return mColorFilter;
106 }
107
108 // Sets alpha, xfermode, and colorfilter from an SkPaint
109 // paint may be NULL, in which case defaults will be set
110 bool setFromPaint(const SkPaint* paint);
111
112 bool needsBlending() const {
113 return !opaque() || alpha() < 255;
114 }
115
116 LayerProperties& operator=(const LayerProperties& other);
117
118private:
119 LayerProperties();
120 ~LayerProperties();
121 void reset();
122
Chris Craik856f0cc2015-04-21 15:13:29 -0700123 // Private since external users should go through properties().effectiveLayerType()
124 LayerType type() const {
125 return mType;
126 }
127
John Reck25fbb3f2014-06-12 13:46:45 -0700128 friend class RenderProperties;
129
Chris Craik182952f2015-03-09 14:17:29 -0700130 LayerType mType = LayerType::None;
John Reck25fbb3f2014-06-12 13:46:45 -0700131 // Whether or not that Layer's content is opaque, doesn't include alpha
132 bool mOpaque;
133 uint8_t mAlpha;
134 SkXfermode::Mode mMode;
Chris Craik182952f2015-03-09 14:17:29 -0700135 SkColorFilter* mColorFilter = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700136};
137
John Reckacb6f072014-03-12 16:11:23 -0700138/*
139 * Data structure that holds the properties for a RenderNode
140 */
John Reck25fbb3f2014-06-12 13:46:45 -0700141class ANDROID_API RenderProperties {
John Reckacb6f072014-03-12 16:11:23 -0700142public:
143 RenderProperties();
144 virtual ~RenderProperties();
145
Chris Craika753f4c2014-07-24 12:39:17 -0700146 static bool setFlag(int flag, bool newValue, int* outFlags) {
147 if (newValue) {
148 if (!(flag & *outFlags)) {
149 *outFlags |= flag;
150 return true;
151 }
152 return false;
153 } else {
154 if (flag & *outFlags) {
155 *outFlags &= ~flag;
156 return true;
157 }
158 return false;
159 }
160 }
161
Chris Craika766cb22015-06-08 16:49:43 -0700162 /**
163 * Set internal layer state based on whether this layer
164 *
165 * Additionally, returns true if child RenderNodes with functors will need to use a layer
166 * to support clipping.
167 */
168 bool prepareForFunctorPresence(bool willHaveFunctor, bool ancestorDictatesFunctorsNeedLayer) {
169 // parent may have already dictated that a descendant layer is needed
170 bool functorsNeedLayer = ancestorDictatesFunctorsNeedLayer
171
172 // Round rect clipping forces layer for functors
Chris Craikb60d3e72015-06-25 17:15:16 -0700173 || CC_UNLIKELY(getOutline().willRoundRectClip())
Chris Craika766cb22015-06-08 16:49:43 -0700174 || CC_UNLIKELY(getRevealClip().willClip())
175
176 // Complex matrices forces layer, due to stencil clipping
177 || CC_UNLIKELY(getTransformMatrix() && !getTransformMatrix()->isScaleTranslate())
178 || CC_UNLIKELY(getAnimationMatrix() && !getAnimationMatrix()->isScaleTranslate())
179 || CC_UNLIKELY(getStaticMatrix() && !getStaticMatrix()->isScaleTranslate());
180
181 mComputedFields.mNeedLayerForFunctors = (willHaveFunctor && functorsNeedLayer);
182
183 // If on a layer, will have consumed the need for isolating functors from stencil.
184 // Thus, it's safe to reset the flag until some descendent sets it.
185 return CC_LIKELY(effectiveLayerType() == LayerType::None) && functorsNeedLayer;
186 }
187
John Reckd0a0b2a2014-03-20 16:28:56 -0700188 RenderProperties& operator=(const RenderProperties& other);
189
John Reck79c7de72014-05-23 10:33:31 -0700190 bool setClipToBounds(bool clipToBounds) {
Chris Craika753f4c2014-07-24 12:39:17 -0700191 return setFlag(CLIP_TO_BOUNDS, clipToBounds, &mPrimitiveFields.mClippingFlags);
192 }
193
194 bool setClipBounds(const Rect& clipBounds) {
195 bool ret = setFlag(CLIP_TO_CLIP_BOUNDS, true, &mPrimitiveFields.mClippingFlags);
196 return RP_SET(mPrimitiveFields.mClipBounds, clipBounds) || ret;
197 }
198
199 bool setClipBoundsEmpty() {
200 return setFlag(CLIP_TO_CLIP_BOUNDS, false, &mPrimitiveFields.mClippingFlags);
John Reckacb6f072014-03-12 16:11:23 -0700201 }
202
John Reck79c7de72014-05-23 10:33:31 -0700203 bool setProjectBackwards(bool shouldProject) {
204 return RP_SET(mPrimitiveFields.mProjectBackwards, shouldProject);
John Reckacb6f072014-03-12 16:11:23 -0700205 }
206
Chris Craik6fe991e52015-10-20 09:39:42 -0700207 bool setProjectionReceiver(bool shouldReceive) {
208 return RP_SET(mPrimitiveFields.mProjectionReceiver, shouldReceive);
John Reckacb6f072014-03-12 16:11:23 -0700209 }
210
John Reckd0a0b2a2014-03-20 16:28:56 -0700211 bool isProjectionReceiver() const {
212 return mPrimitiveFields.mProjectionReceiver;
John Reckacb6f072014-03-12 16:11:23 -0700213 }
214
John Reck79c7de72014-05-23 10:33:31 -0700215 bool setStaticMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700216 delete mStaticMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -0700217 if (matrix) {
218 mStaticMatrix = new SkMatrix(*matrix);
219 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800220 mStaticMatrix = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700221 }
John Reck79c7de72014-05-23 10:33:31 -0700222 return true;
John Reckacb6f072014-03-12 16:11:23 -0700223 }
224
225 // Can return NULL
John Reckd0a0b2a2014-03-20 16:28:56 -0700226 const SkMatrix* getStaticMatrix() const {
John Reckacb6f072014-03-12 16:11:23 -0700227 return mStaticMatrix;
228 }
229
John Reck79c7de72014-05-23 10:33:31 -0700230 bool setAnimationMatrix(const SkMatrix* matrix) {
John Reckacb6f072014-03-12 16:11:23 -0700231 delete mAnimationMatrix;
232 if (matrix) {
233 mAnimationMatrix = new SkMatrix(*matrix);
234 } else {
Chris Craike84a2082014-12-22 14:28:49 -0800235 mAnimationMatrix = nullptr;
John Reckacb6f072014-03-12 16:11:23 -0700236 }
John Reck79c7de72014-05-23 10:33:31 -0700237 return true;
John Reckacb6f072014-03-12 16:11:23 -0700238 }
239
John Reck79c7de72014-05-23 10:33:31 -0700240 bool setAlpha(float alpha) {
John Reck3b52c032014-08-06 10:19:32 -0700241 alpha = MathUtils::clampAlpha(alpha);
John Reck79c7de72014-05-23 10:33:31 -0700242 return RP_SET(mPrimitiveFields.mAlpha, alpha);
John Reckacb6f072014-03-12 16:11:23 -0700243 }
244
245 float getAlpha() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700246 return mPrimitiveFields.mAlpha;
John Reckacb6f072014-03-12 16:11:23 -0700247 }
248
John Reck79c7de72014-05-23 10:33:31 -0700249 bool setHasOverlappingRendering(bool hasOverlappingRendering) {
250 return RP_SET(mPrimitiveFields.mHasOverlappingRendering, hasOverlappingRendering);
John Reckacb6f072014-03-12 16:11:23 -0700251 }
252
253 bool hasOverlappingRendering() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700254 return mPrimitiveFields.mHasOverlappingRendering;
John Reckacb6f072014-03-12 16:11:23 -0700255 }
256
John Reck79c7de72014-05-23 10:33:31 -0700257 bool setElevation(float elevation) {
258 return RP_SET(mPrimitiveFields.mElevation, elevation);
259 // Don't dirty matrix/pivot, since they don't respect Z
Chris Craikcc39e162014-04-25 18:34:11 -0700260 }
261
262 float getElevation() const {
263 return mPrimitiveFields.mElevation;
264 }
265
John Reck79c7de72014-05-23 10:33:31 -0700266 bool setTranslationX(float translationX) {
267 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationX, translationX);
John Reckacb6f072014-03-12 16:11:23 -0700268 }
269
270 float getTranslationX() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700271 return mPrimitiveFields.mTranslationX;
John Reckacb6f072014-03-12 16:11:23 -0700272 }
273
John Reck79c7de72014-05-23 10:33:31 -0700274 bool setTranslationY(float translationY) {
275 return RP_SET_AND_DIRTY(mPrimitiveFields.mTranslationY, translationY);
John Reckacb6f072014-03-12 16:11:23 -0700276 }
277
278 float getTranslationY() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700279 return mPrimitiveFields.mTranslationY;
John Reckacb6f072014-03-12 16:11:23 -0700280 }
281
John Reck79c7de72014-05-23 10:33:31 -0700282 bool setTranslationZ(float translationZ) {
283 return RP_SET(mPrimitiveFields.mTranslationZ, translationZ);
284 // mMatrixOrPivotDirty not set, since matrix doesn't respect Z
John Reckacb6f072014-03-12 16:11:23 -0700285 }
286
287 float getTranslationZ() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700288 return mPrimitiveFields.mTranslationZ;
John Reckacb6f072014-03-12 16:11:23 -0700289 }
290
John Recke45b1fd2014-04-15 09:50:16 -0700291 // Animation helper
John Reck79c7de72014-05-23 10:33:31 -0700292 bool setX(float value) {
293 return setTranslationX(value - getLeft());
John Recke45b1fd2014-04-15 09:50:16 -0700294 }
295
296 // Animation helper
297 float getX() const {
298 return getLeft() + getTranslationX();
299 }
300
301 // Animation helper
John Reck79c7de72014-05-23 10:33:31 -0700302 bool setY(float value) {
303 return setTranslationY(value - getTop());
John Recke45b1fd2014-04-15 09:50:16 -0700304 }
305
306 // Animation helper
307 float getY() const {
308 return getTop() + getTranslationY();
309 }
310
311 // Animation helper
John Reck79c7de72014-05-23 10:33:31 -0700312 bool setZ(float value) {
313 return setTranslationZ(value - getElevation());
John Recke45b1fd2014-04-15 09:50:16 -0700314 }
315
Chris Craikcc39e162014-04-25 18:34:11 -0700316 float getZ() const {
317 return getElevation() + getTranslationZ();
318 }
319
John Reck79c7de72014-05-23 10:33:31 -0700320 bool setRotation(float rotation) {
321 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotation, rotation);
John Reckacb6f072014-03-12 16:11:23 -0700322 }
323
324 float getRotation() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700325 return mPrimitiveFields.mRotation;
John Reckacb6f072014-03-12 16:11:23 -0700326 }
327
John Reck79c7de72014-05-23 10:33:31 -0700328 bool setRotationX(float rotationX) {
329 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationX, rotationX);
John Reckacb6f072014-03-12 16:11:23 -0700330 }
331
332 float getRotationX() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700333 return mPrimitiveFields.mRotationX;
John Reckacb6f072014-03-12 16:11:23 -0700334 }
335
John Reck79c7de72014-05-23 10:33:31 -0700336 bool setRotationY(float rotationY) {
337 return RP_SET_AND_DIRTY(mPrimitiveFields.mRotationY, rotationY);
John Reckacb6f072014-03-12 16:11:23 -0700338 }
339
340 float getRotationY() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700341 return mPrimitiveFields.mRotationY;
John Reckacb6f072014-03-12 16:11:23 -0700342 }
343
John Reck79c7de72014-05-23 10:33:31 -0700344 bool setScaleX(float scaleX) {
345 return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleX, scaleX);
John Reckacb6f072014-03-12 16:11:23 -0700346 }
347
348 float getScaleX() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700349 return mPrimitiveFields.mScaleX;
John Reckacb6f072014-03-12 16:11:23 -0700350 }
351
John Reck79c7de72014-05-23 10:33:31 -0700352 bool setScaleY(float scaleY) {
353 return RP_SET_AND_DIRTY(mPrimitiveFields.mScaleY, scaleY);
John Reckacb6f072014-03-12 16:11:23 -0700354 }
355
356 float getScaleY() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700357 return mPrimitiveFields.mScaleY;
John Reckacb6f072014-03-12 16:11:23 -0700358 }
359
John Reck79c7de72014-05-23 10:33:31 -0700360 bool setPivotX(float pivotX) {
361 if (RP_SET(mPrimitiveFields.mPivotX, pivotX)
362 || !mPrimitiveFields.mPivotExplicitlySet) {
363 mPrimitiveFields.mMatrixOrPivotDirty = true;
364 mPrimitiveFields.mPivotExplicitlySet = true;
365 return true;
366 }
367 return false;
John Reckacb6f072014-03-12 16:11:23 -0700368 }
369
John Reckd0a0b2a2014-03-20 16:28:56 -0700370 /* Note that getPivotX and getPivotY are adjusted by updateMatrix(),
John Reck79c7de72014-05-23 10:33:31 -0700371 * so the value returned may be stale if the RenderProperties has been
372 * modified since the last call to updateMatrix()
John Reckd0a0b2a2014-03-20 16:28:56 -0700373 */
374 float getPivotX() const {
375 return mPrimitiveFields.mPivotX;
376 }
John Reckacb6f072014-03-12 16:11:23 -0700377
John Reck79c7de72014-05-23 10:33:31 -0700378 bool setPivotY(float pivotY) {
379 if (RP_SET(mPrimitiveFields.mPivotY, pivotY)
380 || !mPrimitiveFields.mPivotExplicitlySet) {
381 mPrimitiveFields.mMatrixOrPivotDirty = true;
382 mPrimitiveFields.mPivotExplicitlySet = true;
383 return true;
384 }
385 return false;
John Reckacb6f072014-03-12 16:11:23 -0700386 }
387
John Reckd0a0b2a2014-03-20 16:28:56 -0700388 float getPivotY() const {
389 return mPrimitiveFields.mPivotY;
390 }
John Reckacb6f072014-03-12 16:11:23 -0700391
Chris Craik49e6c732014-03-31 12:34:11 -0700392 bool isPivotExplicitlySet() const {
393 return mPrimitiveFields.mPivotExplicitlySet;
394 }
395
John Reck79c7de72014-05-23 10:33:31 -0700396 bool setCameraDistance(float distance) {
Chris Craik49e6c732014-03-31 12:34:11 -0700397 if (distance != getCameraDistance()) {
John Reckf7483e32014-04-11 08:54:47 -0700398 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c732014-03-31 12:34:11 -0700399 mComputedFields.mTransformCamera.setCameraLocation(0, 0, distance);
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
405 float getCameraDistance() const {
Chris Craik49e6c732014-03-31 12:34:11 -0700406 // TODO: update getCameraLocationZ() to be const
407 return const_cast<Sk3DView*>(&mComputedFields.mTransformCamera)->getCameraLocationZ();
John Reckacb6f072014-03-12 16:11:23 -0700408 }
409
John Reck79c7de72014-05-23 10:33:31 -0700410 bool setLeft(int left) {
411 if (RP_SET(mPrimitiveFields.mLeft, left)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700412 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
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
John Recke248bd12015-08-05 13:53:53 -0700421 int getLeft() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700422 return mPrimitiveFields.mLeft;
John Reckacb6f072014-03-12 16:11:23 -0700423 }
424
John Reck79c7de72014-05-23 10:33:31 -0700425 bool setTop(int top) {
426 if (RP_SET(mPrimitiveFields.mTop, top)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700427 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700428 if (!mPrimitiveFields.mPivotExplicitlySet) {
429 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700430 }
John Reck79c7de72014-05-23 10:33:31 -0700431 return true;
John Reckacb6f072014-03-12 16:11:23 -0700432 }
John Reck79c7de72014-05-23 10:33:31 -0700433 return false;
John Reckacb6f072014-03-12 16:11:23 -0700434 }
435
John Recke248bd12015-08-05 13:53:53 -0700436 int getTop() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700437 return mPrimitiveFields.mTop;
John Reckacb6f072014-03-12 16:11:23 -0700438 }
439
John Reck79c7de72014-05-23 10:33:31 -0700440 bool setRight(int right) {
441 if (RP_SET(mPrimitiveFields.mRight, right)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700442 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
John Reckf7483e32014-04-11 08:54:47 -0700443 if (!mPrimitiveFields.mPivotExplicitlySet) {
444 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700445 }
John Reck79c7de72014-05-23 10:33:31 -0700446 return true;
John Reckacb6f072014-03-12 16:11:23 -0700447 }
John Reck79c7de72014-05-23 10:33:31 -0700448 return false;
John Reckacb6f072014-03-12 16:11:23 -0700449 }
450
John Recke248bd12015-08-05 13:53:53 -0700451 int getRight() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700452 return mPrimitiveFields.mRight;
John Reckacb6f072014-03-12 16:11:23 -0700453 }
454
John Reck79c7de72014-05-23 10:33:31 -0700455 bool setBottom(int bottom) {
456 if (RP_SET(mPrimitiveFields.mBottom, bottom)) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700457 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700458 if (!mPrimitiveFields.mPivotExplicitlySet) {
459 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700460 }
John Reck79c7de72014-05-23 10:33:31 -0700461 return true;
John Reckacb6f072014-03-12 16:11:23 -0700462 }
John Reck79c7de72014-05-23 10:33:31 -0700463 return false;
John Reckacb6f072014-03-12 16:11:23 -0700464 }
465
John Recke248bd12015-08-05 13:53:53 -0700466 int getBottom() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700467 return mPrimitiveFields.mBottom;
John Reckacb6f072014-03-12 16:11:23 -0700468 }
469
John Reck79c7de72014-05-23 10:33:31 -0700470 bool setLeftTop(int left, int top) {
471 bool leftResult = setLeft(left);
472 bool topResult = setTop(top);
473 return leftResult || topResult;
John Reckacb6f072014-03-12 16:11:23 -0700474 }
475
John Reck79c7de72014-05-23 10:33:31 -0700476 bool setLeftTopRightBottom(int left, int top, int right, int bottom) {
Chris Craikcc39e162014-04-25 18:34:11 -0700477 if (left != mPrimitiveFields.mLeft || top != mPrimitiveFields.mTop
478 || right != mPrimitiveFields.mRight || bottom != mPrimitiveFields.mBottom) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700479 mPrimitiveFields.mLeft = left;
480 mPrimitiveFields.mTop = top;
481 mPrimitiveFields.mRight = right;
482 mPrimitiveFields.mBottom = bottom;
483 mPrimitiveFields.mWidth = mPrimitiveFields.mRight - mPrimitiveFields.mLeft;
484 mPrimitiveFields.mHeight = mPrimitiveFields.mBottom - mPrimitiveFields.mTop;
John Reckf7483e32014-04-11 08:54:47 -0700485 if (!mPrimitiveFields.mPivotExplicitlySet) {
486 mPrimitiveFields.mMatrixOrPivotDirty = true;
John Reckacb6f072014-03-12 16:11:23 -0700487 }
John Reck79c7de72014-05-23 10:33:31 -0700488 return true;
John Reckacb6f072014-03-12 16:11:23 -0700489 }
John Reck79c7de72014-05-23 10:33:31 -0700490 return false;
John Reckacb6f072014-03-12 16:11:23 -0700491 }
492
Chris Craika753f4c2014-07-24 12:39:17 -0700493 bool offsetLeftRight(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700494 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700495 mPrimitiveFields.mLeft += offset;
496 mPrimitiveFields.mRight += offset;
John Reck79c7de72014-05-23 10:33:31 -0700497 return true;
John Reckacb6f072014-03-12 16:11:23 -0700498 }
John Reck79c7de72014-05-23 10:33:31 -0700499 return false;
John Reckacb6f072014-03-12 16:11:23 -0700500 }
501
Chris Craika753f4c2014-07-24 12:39:17 -0700502 bool offsetTopBottom(int offset) {
John Reckacb6f072014-03-12 16:11:23 -0700503 if (offset != 0) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700504 mPrimitiveFields.mTop += offset;
505 mPrimitiveFields.mBottom += offset;
John Reck79c7de72014-05-23 10:33:31 -0700506 return true;
John Reckacb6f072014-03-12 16:11:23 -0700507 }
John Reck79c7de72014-05-23 10:33:31 -0700508 return false;
John Reckacb6f072014-03-12 16:11:23 -0700509 }
510
Chris Craikb49f4462014-03-20 12:44:20 -0700511 int getWidth() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700512 return mPrimitiveFields.mWidth;
John Reckacb6f072014-03-12 16:11:23 -0700513 }
514
Chris Craikb49f4462014-03-20 12:44:20 -0700515 int getHeight() const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700516 return mPrimitiveFields.mHeight;
John Reckacb6f072014-03-12 16:11:23 -0700517 }
518
John Reckd0a0b2a2014-03-20 16:28:56 -0700519 const SkMatrix* getAnimationMatrix() const {
520 return mAnimationMatrix;
521 }
522
John Reckf7483e32014-04-11 08:54:47 -0700523 bool hasTransformMatrix() const {
524 return getTransformMatrix() && !getTransformMatrix()->isIdentity();
525 }
526
527 // May only call this if hasTransformMatrix() is true
528 bool isTransformTranslateOnly() const {
529 return getTransformMatrix()->getType() == SkMatrix::kTranslate_Mask;
John Reckd0a0b2a2014-03-20 16:28:56 -0700530 }
531
Chris Craik49e6c732014-03-31 12:34:11 -0700532 const SkMatrix* getTransformMatrix() const {
John Reckf7483e32014-04-11 08:54:47 -0700533 LOG_ALWAYS_FATAL_IF(mPrimitiveFields.mMatrixOrPivotDirty, "Cannot get a dirty matrix!");
John Reckd0a0b2a2014-03-20 16:28:56 -0700534 return mComputedFields.mTransformMatrix;
535 }
536
Chris Craika753f4c2014-07-24 12:39:17 -0700537 int getClippingFlags() const {
538 return mPrimitiveFields.mClippingFlags;
539 }
540
John Reckd0a0b2a2014-03-20 16:28:56 -0700541 bool getClipToBounds() const {
Chris Craika753f4c2014-07-24 12:39:17 -0700542 return mPrimitiveFields.mClippingFlags & CLIP_TO_BOUNDS;
543 }
544
John Recke248bd12015-08-05 13:53:53 -0700545 const Rect& getClipBounds() const {
546 return mPrimitiveFields.mClipBounds;
547 }
548
Chris Craika753f4c2014-07-24 12:39:17 -0700549 void getClippingRectForFlags(uint32_t flags, Rect* outRect) const {
550 if (flags & CLIP_TO_BOUNDS) {
551 outRect->set(0, 0, getWidth(), getHeight());
552 if (flags & CLIP_TO_CLIP_BOUNDS) {
Chris Craikac02eb92015-10-05 12:23:46 -0700553 outRect->doIntersect(mPrimitiveFields.mClipBounds);
Chris Craika753f4c2014-07-24 12:39:17 -0700554 }
555 } else {
556 outRect->set(mPrimitiveFields.mClipBounds);
557 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700558 }
559
560 bool getHasOverlappingRendering() const {
561 return mPrimitiveFields.mHasOverlappingRendering;
562 }
563
564 const Outline& getOutline() const {
565 return mPrimitiveFields.mOutline;
566 }
567
Chris Craik8c271ca2014-03-25 10:33:01 -0700568 const RevealClip& getRevealClip() const {
569 return mPrimitiveFields.mRevealClip;
570 }
571
John Reckd0a0b2a2014-03-20 16:28:56 -0700572 bool getProjectBackwards() const {
573 return mPrimitiveFields.mProjectBackwards;
574 }
575
576 void debugOutputProperties(const int level) const;
577
John Reck25fbb3f2014-06-12 13:46:45 -0700578 void updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700579
580 Outline& mutableOutline() {
581 return mPrimitiveFields.mOutline;
Chris Craikb49f4462014-03-20 12:44:20 -0700582 }
583
Chris Craik8c271ca2014-03-25 10:33:01 -0700584 RevealClip& mutableRevealClip() {
585 return mPrimitiveFields.mRevealClip;
586 }
587
John Reck25fbb3f2014-06-12 13:46:45 -0700588 const LayerProperties& layerProperties() const {
589 return mLayerProperties;
590 }
591
592 LayerProperties& mutateLayerProperties() {
593 return mLayerProperties;
594 }
595
John Reck293e8682014-06-17 10:34:02 -0700596 // Returns true if damage calculations should be clipped to bounds
597 // TODO: Figure out something better for getZ(), as children should still be
598 // clipped to this RP's bounds. But as we will damage -INT_MAX to INT_MAX
599 // for this RP's getZ() anyway, this can be optimized when we have a
600 // Z damage estimate instead of INT_MAX
601 bool getClipDamageToBounds() const {
602 return getClipToBounds() && (getZ() <= 0 || getOutline().isEmpty());
603 }
604
Chris Craik5c75c522014-09-05 14:08:08 -0700605 bool hasShadow() const {
Chris Craikb5a54352014-11-21 14:54:35 -0800606 return getZ() > 0.0f
Chris Craike84a2082014-12-22 14:28:49 -0800607 && getOutline().getPath() != nullptr
Chris Craik9fa364d2014-09-19 16:04:45 -0700608 && getOutline().getAlpha() != 0.0f;
Chris Craik5c75c522014-09-05 14:08:08 -0700609 }
610
Chris Craik9fded232015-11-11 16:42:34 -0800611 bool fitsOnLayer() const {
Chris Craik76caecf2015-11-02 19:17:45 -0800612 const DeviceInfo* deviceInfo = DeviceInfo::get();
Chris Craik9fded232015-11-11 16:42:34 -0800613 return mPrimitiveFields.mWidth <= deviceInfo->maxTextureSize()
Chris Craike3e481d2016-07-11 12:20:51 -0700614 && mPrimitiveFields.mHeight <= deviceInfo->maxTextureSize();
Chris Craik9fded232015-11-11 16:42:34 -0800615 }
616
617 bool promotedToLayer() const {
Chris Craik1a0808e2015-05-13 16:33:04 -0700618 return mLayerProperties.mType == LayerType::None
Chris Craik9fded232015-11-11 16:42:34 -0800619 && fitsOnLayer()
Chris Craika766cb22015-06-08 16:49:43 -0700620 && (mComputedFields.mNeedLayerForFunctors
621 || (!MathUtils::isZero(mPrimitiveFields.mAlpha)
622 && mPrimitiveFields.mAlpha < 1
623 && mPrimitiveFields.mHasOverlappingRendering));
Chris Craik1a0808e2015-05-13 16:33:04 -0700624 }
625
626 LayerType effectiveLayerType() const {
Chris Craika766cb22015-06-08 16:49:43 -0700627 return CC_UNLIKELY(promotedToLayer()) ? LayerType::RenderLayer : mLayerProperties.mType;
Chris Craik856f0cc2015-04-21 15:13:29 -0700628 }
629
John Reckacb6f072014-03-12 16:11:23 -0700630private:
John Reckacb6f072014-03-12 16:11:23 -0700631 // Rendering properties
John Reckd0a0b2a2014-03-20 16:28:56 -0700632 struct PrimitiveFields {
John Recke248bd12015-08-05 13:53:53 -0700633 int mLeft = 0, mTop = 0, mRight = 0, mBottom = 0;
634 int mWidth = 0, mHeight = 0;
635 int mClippingFlags = CLIP_TO_BOUNDS;
636 float mAlpha = 1;
637 float mTranslationX = 0, mTranslationY = 0, mTranslationZ = 0;
638 float mElevation = 0;
639 float mRotation = 0, mRotationX = 0, mRotationY = 0;
640 float mScaleX = 1, mScaleY = 1;
641 float mPivotX = 0, mPivotY = 0;
642 bool mHasOverlappingRendering = false;
643 bool mPivotExplicitlySet = false;
644 bool mMatrixOrPivotDirty = false;
645 bool mProjectBackwards = false;
646 bool mProjectionReceiver = false;
647 Rect mClipBounds;
John Reckd0a0b2a2014-03-20 16:28:56 -0700648 Outline mOutline;
Chris Craik8c271ca2014-03-25 10:33:01 -0700649 RevealClip mRevealClip;
John Reckd0a0b2a2014-03-20 16:28:56 -0700650 } mPrimitiveFields;
651
John Reckacb6f072014-03-12 16:11:23 -0700652 SkMatrix* mStaticMatrix;
653 SkMatrix* mAnimationMatrix;
John Reck25fbb3f2014-06-12 13:46:45 -0700654 LayerProperties mLayerProperties;
John Reckacb6f072014-03-12 16:11:23 -0700655
John Reckd0a0b2a2014-03-20 16:28:56 -0700656 /**
657 * These fields are all generated from other properties and are not set directly.
658 */
659 struct ComputedFields {
660 ComputedFields();
661 ~ComputedFields();
662
663 /**
664 * Stores the total transformation of the DisplayList based upon its scalar
665 * translate/rotate/scale properties.
666 *
Chris Craik49e6c732014-03-31 12:34:11 -0700667 * In the common translation-only case, the matrix isn't necessarily allocated,
668 * and the mTranslation properties are used directly.
John Reckd0a0b2a2014-03-20 16:28:56 -0700669 */
Chris Craik49e6c732014-03-31 12:34:11 -0700670 SkMatrix* mTransformMatrix;
671
672 Sk3DView mTransformCamera;
Chris Craika766cb22015-06-08 16:49:43 -0700673
674 // Force layer on for functors to enable render features they don't yet support (clipping)
675 bool mNeedLayerForFunctors = false;
John Reckd0a0b2a2014-03-20 16:28:56 -0700676 } mComputedFields;
John Reckacb6f072014-03-12 16:11:23 -0700677};
678
679} /* namespace uirenderer */
680} /* namespace android */
681
Chris Craikb49f4462014-03-20 12:44:20 -0700682#endif /* RENDERNODEPROPERTIES_H */