blob: c5371236b9cf59be88cbde81e667d45d08f88770 [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");
John Reckd0a0b2a2014-03-20 16:28:56 -07005 * you mPrimitiveFields.may not use this file except in compliance with the License.
6 * You mPrimitiveFields.may obtain a copy of the License at
John Reckacb6f072014-03-12 16:11:23 -07007 *
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 */
John Reckd0a0b2a2014-03-20 16:28:56 -070016
John Reckacb6f072014-03-12 16:11:23 -070017#include "RenderProperties.h"
18
John Reckd0a0b2a2014-03-20 16:28:56 -070019#include <utils/Trace.h>
20
John Reck25fbb3f2014-06-12 13:46:45 -070021#include <SkColorFilter.h>
John Reckacb6f072014-03-12 16:11:23 -070022#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070023#include <SkPath.h>
24#include <SkPathOps.h>
John Reckacb6f072014-03-12 16:11:23 -070025
26#include "Matrix.h"
sergeyvdccca442016-03-21 15:38:21 -070027#include "hwui/Canvas.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070028#include "utils/MathUtils.h"
John Reckf7483e32014-04-11 08:54:47 -070029
John Reckacb6f072014-03-12 16:11:23 -070030namespace android {
31namespace uirenderer {
32
Chris Craik182952f2015-03-09 14:17:29 -070033LayerProperties::LayerProperties() {
John Reck25fbb3f2014-06-12 13:46:45 -070034 reset();
35}
36
37LayerProperties::~LayerProperties() {
Chris Craik182952f2015-03-09 14:17:29 -070038 setType(LayerType::None);
John Reck25fbb3f2014-06-12 13:46:45 -070039}
40
41void LayerProperties::reset() {
42 mOpaque = false;
Chris Craikd41c4d82015-01-05 15:51:13 -080043 setFromPaint(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070044}
45
46bool LayerProperties::setColorFilter(SkColorFilter* filter) {
Ben Wagnerc1a8a462018-07-12 12:41:28 -040047 if (mColorFilter.get() == filter) return false;
48 mColorFilter = sk_ref_sp(filter);
John Reck1bcacfd2017-11-03 10:12:19 -070049 return true;
John Reck25fbb3f2014-06-12 13:46:45 -070050}
51
Nader Jawad390d6e82020-09-24 21:35:03 -070052bool LayerProperties::setImageFilter(SkImageFilter* imageFilter) {
53 if(mImageFilter.get() == imageFilter) return false;
54 mImageFilter = sk_ref_sp(imageFilter);
55 return true;
56}
57
Dongya Jiangfdcf72c2022-02-28 21:35:57 +080058bool LayerProperties::setBackdropImageFilter(SkImageFilter* imageFilter) {
59 if (mBackdropImageFilter.get() == imageFilter) return false;
60 mBackdropImageFilter = sk_ref_sp(imageFilter);
61 return true;
62}
63
John Reck25fbb3f2014-06-12 13:46:45 -070064bool LayerProperties::setFromPaint(const SkPaint* paint) {
65 bool changed = false;
Chris Craikbf6f0f22015-10-01 12:36:07 -070066 changed |= setAlpha(static_cast<uint8_t>(PaintUtils::getAlphaDirect(paint)));
Mike Reed260ab722016-10-07 15:59:20 -040067 changed |= setXferMode(PaintUtils::getBlendModeDirect(paint));
Chris Craikd41c4d82015-01-05 15:51:13 -080068 changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070069 return changed;
70}
71
72LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
73 setType(other.type());
74 setOpaque(other.opaque());
75 setAlpha(other.alpha());
76 setXferMode(other.xferMode());
Ben Wagnerc1a8a462018-07-12 12:41:28 -040077 setColorFilter(other.getColorFilter());
Nader Jawad390d6e82020-09-24 21:35:03 -070078 setImageFilter(other.getImageFilter());
Dongya Jiangfdcf72c2022-02-28 21:35:57 +080079 setBackdropImageFilter(other.getBackdropImageFilter());
John Reck5d53a752021-02-08 19:22:59 -050080 mStretchEffect = other.mStretchEffect;
John Reck25fbb3f2014-06-12 13:46:45 -070081 return *this;
82}
83
John Reck1bcacfd2017-11-03 10:12:19 -070084RenderProperties::ComputedFields::ComputedFields() : mTransformMatrix(nullptr) {}
John Reckd0a0b2a2014-03-20 16:28:56 -070085
86RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070087 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -070088}
89
John Reck1bcacfd2017-11-03 10:12:19 -070090RenderProperties::RenderProperties() : mStaticMatrix(nullptr), mAnimationMatrix(nullptr) {}
John Reckd0a0b2a2014-03-20 16:28:56 -070091
92RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070093 delete mStaticMatrix;
94 delete mAnimationMatrix;
95}
96
John Reckd0a0b2a2014-03-20 16:28:56 -070097RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
98 if (this != &other) {
99 mPrimitiveFields = other.mPrimitiveFields;
100 setStaticMatrix(other.getStaticMatrix());
101 setAnimationMatrix(other.getAnimationMatrix());
102 setCameraDistance(other.getCameraDistance());
John Reck25fbb3f2014-06-12 13:46:45 -0700103 mLayerProperties = other.layerProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -0700104
Chris Craik49e6c7392014-03-31 12:34:11 -0700105 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -0700106 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -0700107 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700108 }
109 return *this;
John Reckacb6f072014-03-12 16:11:23 -0700110}
111
John Reck1bcacfd2017-11-03 10:12:19 -0700112static void dumpMatrix(std::ostream& output, std::string& indent, const char* label,
113 SkMatrix* matrix) {
114 if (matrix) {
115 output << indent << "(" << label << " " << matrix << ": ";
sergeyvc3849aa2016-08-08 13:22:06 -0700116 output << std::fixed << std::setprecision(2);
John Reck1bcacfd2017-11-03 10:12:19 -0700117 output << "[" << matrix->get(0) << " " << matrix->get(1) << " " << matrix->get(2) << "]";
118 output << " [" << matrix->get(3) << " " << matrix->get(4) << " " << matrix->get(5) << "]";
119 output << " [" << matrix->get(6) << " " << matrix->get(7) << " " << matrix->get(8) << "]";
sergeyvc3849aa2016-08-08 13:22:06 -0700120 output << ")" << std::endl;
121 }
122}
123
124void RenderProperties::debugOutputProperties(std::ostream& output, const int level) const {
125 auto indent = std::string(level * 2, ' ');
John Reckd0a0b2a2014-03-20 16:28:56 -0700126 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
John Reck1bcacfd2017-11-03 10:12:19 -0700127 output << indent << "(Translate (left, top) " << mPrimitiveFields.mLeft << ", "
128 << mPrimitiveFields.mTop << ")" << std::endl;
John Reckd0a0b2a2014-03-20 16:28:56 -0700129 }
sergeyvc3849aa2016-08-08 13:22:06 -0700130 dumpMatrix(output, indent, "ConcatMatrix (static)", mStaticMatrix);
131 dumpMatrix(output, indent, "ConcatMatrix (animation)", mAnimationMatrix);
132
133 output << std::fixed << std::setprecision(2);
John Reckf7483e32014-04-11 08:54:47 -0700134 if (hasTransformMatrix()) {
135 if (isTransformTranslateOnly()) {
sergeyvc3849aa2016-08-08 13:22:06 -0700136 output << indent << "(Translate " << getTranslationX() << ", " << getTranslationY()
John Reck1bcacfd2017-11-03 10:12:19 -0700137 << ", " << getZ() << ")" << std::endl;
John Reckd0a0b2a2014-03-20 16:28:56 -0700138 } else {
sergeyvc3849aa2016-08-08 13:22:06 -0700139 dumpMatrix(output, indent, "ConcatMatrix ", mComputedFields.mTransformMatrix);
John Reckd0a0b2a2014-03-20 16:28:56 -0700140 }
141 }
142
Chris Craik856f0cc2015-04-21 15:13:29 -0700143 const bool isLayer = effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700144 int clipFlags = getClippingFlags();
John Reck1bcacfd2017-11-03 10:12:19 -0700145 if (mPrimitiveFields.mAlpha < 1 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
Chris Craika753f4c2014-07-24 12:39:17 -0700146 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700147 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reckd0a0b2a2014-03-20 16:28:56 -0700148 }
Chris Craik8df5ffa2015-04-28 17:47:20 -0700149
Chris Craik4e9d9b22015-06-12 11:07:23 -0700150 if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
151 // simply scale rendering content's alpha
sergeyvc3849aa2016-08-08 13:22:06 -0700152 output << indent << "(ScaleAlpha " << mPrimitiveFields.mAlpha << ")" << std::endl;
Chris Craik4e9d9b22015-06-12 11:07:23 -0700153 } else {
154 // savelayeralpha to create an offscreen buffer to apply alpha
155 Rect layerBounds(0, 0, getWidth(), getHeight());
156 if (clipFlags) {
157 getClippingRectForFlags(clipFlags, &layerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700158 clipFlags = 0; // all clipping done by savelayer
Chris Craik4e9d9b22015-06-12 11:07:23 -0700159 }
John Reck1bcacfd2017-11-03 10:12:19 -0700160 output << indent << "(SaveLayerAlpha " << (int)layerBounds.left << ", "
161 << (int)layerBounds.top << ", " << (int)layerBounds.right << ", "
162 << (int)layerBounds.bottom << ", " << (int)(mPrimitiveFields.mAlpha * 255)
163 << ", 0x" << std::hex << (SaveFlags::HasAlphaLayer | SaveFlags::ClipToLayer)
164 << ")" << std::dec << std::endl;
Chris Craik4e9d9b22015-06-12 11:07:23 -0700165 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700166 }
Chris Craik91eff222016-02-22 13:39:33 -0800167
Chris Craika753f4c2014-07-24 12:39:17 -0700168 if (clipFlags) {
169 Rect clipRect;
170 getClippingRectForFlags(clipFlags, &clipRect);
John Reck1bcacfd2017-11-03 10:12:19 -0700171 output << indent << "(ClipRect " << (int)clipRect.left << ", " << (int)clipRect.top << ", "
172 << (int)clipRect.right << ", " << (int)clipRect.bottom << ")" << std::endl;
John Reckd0a0b2a2014-03-20 16:28:56 -0700173 }
Chris Craik136d1af2016-04-04 13:40:39 -0700174
175 if (getRevealClip().willClip()) {
176 Rect bounds;
177 getRevealClip().getBounds(&bounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700178 output << indent << "(Clip to reveal clip with bounds " << bounds.left << ", " << bounds.top
179 << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl;
Chris Craik136d1af2016-04-04 13:40:39 -0700180 }
181
182 auto& outline = mPrimitiveFields.mOutline;
183 if (outline.getShouldClip()) {
184 if (outline.isEmpty()) {
sergeyvc3849aa2016-08-08 13:22:06 -0700185 output << indent << "(Clip to empty outline)";
Chris Craik136d1af2016-04-04 13:40:39 -0700186 } else if (outline.willClip()) {
sergeyvc3849aa2016-08-08 13:22:06 -0700187 const Rect& bounds = outline.getBounds();
John Reck1bcacfd2017-11-03 10:12:19 -0700188 output << indent << "(Clip to outline with bounds " << bounds.left << ", " << bounds.top
189 << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl;
Chris Craik136d1af2016-04-04 13:40:39 -0700190 }
191 }
John Reckacb6f072014-03-12 16:11:23 -0700192}
193
194void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700195 if (mPrimitiveFields.mMatrixOrPivotDirty) {
196 if (!mComputedFields.mTransformMatrix) {
197 // only allocate a mPrimitiveFields.matrix if we have a complex transform
198 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700199 }
John Reckf7483e32014-04-11 08:54:47 -0700200 if (!mPrimitiveFields.mPivotExplicitlySet) {
201 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
202 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
203 }
204 SkMatrix* transform = mComputedFields.mTransformMatrix;
205 transform->reset();
Chris Craike0bb87d2014-04-22 17:55:41 -0700206 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700207 transform->setTranslate(getTranslationX(), getTranslationY());
208 transform->preRotate(getRotation(), getPivotX(), getPivotY());
209 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
210 } else {
211 SkMatrix transform3D;
212 mComputedFields.mTransformCamera.save();
213 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
214 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
215 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
216 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
217 mComputedFields.mTransformCamera.getMatrix(&transform3D);
218 transform3D.preTranslate(-getPivotX(), -getPivotY());
219 transform3D.postTranslate(getPivotX() + getTranslationX(),
John Reck1bcacfd2017-11-03 10:12:19 -0700220 getPivotY() + getTranslationY());
John Reckf7483e32014-04-11 08:54:47 -0700221 transform->postConcat(transform3D);
222 mComputedFields.mTransformCamera.restore();
223 }
224 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700225 }
226}
227
John Reckacb6f072014-03-12 16:11:23 -0700228} /* namespace uirenderer */
229} /* namespace android */