1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you mPrimitiveFields.may not use this file except in compliance with the License.
6 * You mPrimitiveFields.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 */
16
17 #include "RenderProperties.h"
18
19 #include <utils/Trace.h>
20
21 #include <SkColorFilter.h>
22 #include <SkMatrix.h>
23 #include <SkPath.h>
24 #include <SkPathOps.h>
25
26 #include "Matrix.h"
27 #include "hwui/Canvas.h"
28 #include "utils/MathUtils.h"
29
30 namespace android {
31 namespace uirenderer {
32
LayerProperties()33 LayerProperties::LayerProperties() {
34 reset();
35 }
36
~LayerProperties()37 LayerProperties::~LayerProperties() {
38 setType(LayerType::None);
39 }
40
reset()41 void LayerProperties::reset() {
42 mOpaque = false;
43 setFromPaint(nullptr);
44 }
45
setColorFilter(SkColorFilter * filter)46 bool LayerProperties::setColorFilter(SkColorFilter* filter) {
47 if (mColorFilter.get() == filter) return false;
48 mColorFilter = sk_ref_sp(filter);
49 return true;
50 }
51
setImageFilter(SkImageFilter * imageFilter)52 bool LayerProperties::setImageFilter(SkImageFilter* imageFilter) {
53 if(mImageFilter.get() == imageFilter) return false;
54 mImageFilter = sk_ref_sp(imageFilter);
55 return true;
56 }
57
setBackdropImageFilter(SkImageFilter * imageFilter)58 bool LayerProperties::setBackdropImageFilter(SkImageFilter* imageFilter) {
59 if (mBackdropImageFilter.get() == imageFilter) return false;
60 mBackdropImageFilter = sk_ref_sp(imageFilter);
61 return true;
62 }
63
setFromPaint(const SkPaint * paint)64 bool LayerProperties::setFromPaint(const SkPaint* paint) {
65 bool changed = false;
66 changed |= setAlpha(static_cast<uint8_t>(PaintUtils::getAlphaDirect(paint)));
67 changed |= setXferMode(PaintUtils::getBlendModeDirect(paint));
68 changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
69 return changed;
70 }
71
operator =(const LayerProperties & other)72 LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
73 setType(other.type());
74 setOpaque(other.opaque());
75 setAlpha(other.alpha());
76 setXferMode(other.xferMode());
77 setColorFilter(other.getColorFilter());
78 setImageFilter(other.getImageFilter());
79 setBackdropImageFilter(other.getBackdropImageFilter());
80 mStretchEffect = other.mStretchEffect;
81 return *this;
82 }
83
ComputedFields()84 RenderProperties::ComputedFields::ComputedFields() : mTransformMatrix(nullptr) {}
85
~ComputedFields()86 RenderProperties::ComputedFields::~ComputedFields() {
87 delete mTransformMatrix;
88 }
89
RenderProperties()90 RenderProperties::RenderProperties() : mStaticMatrix(nullptr), mAnimationMatrix(nullptr) {}
91
~RenderProperties()92 RenderProperties::~RenderProperties() {
93 delete mStaticMatrix;
94 delete mAnimationMatrix;
95 }
96
operator =(const RenderProperties & other)97 RenderProperties& 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());
103 mLayerProperties = other.layerProperties();
104
105 // Force recalculation of the matrix, since other's dirty bit may be clear
106 mPrimitiveFields.mMatrixOrPivotDirty = true;
107 updateMatrix();
108 }
109 return *this;
110 }
111
dumpMatrix(std::ostream & output,std::string & indent,const char * label,SkMatrix * matrix)112 static void dumpMatrix(std::ostream& output, std::string& indent, const char* label,
113 SkMatrix* matrix) {
114 if (matrix) {
115 output << indent << "(" << label << " " << matrix << ": ";
116 output << std::fixed << std::setprecision(2);
117 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) << "]";
120 output << ")" << std::endl;
121 }
122 }
123
debugOutputProperties(std::ostream & output,const int level) const124 void RenderProperties::debugOutputProperties(std::ostream& output, const int level) const {
125 auto indent = std::string(level * 2, ' ');
126 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
127 output << indent << "(Translate (left, top) " << mPrimitiveFields.mLeft << ", "
128 << mPrimitiveFields.mTop << ")" << std::endl;
129 }
130 dumpMatrix(output, indent, "ConcatMatrix (static)", mStaticMatrix);
131 dumpMatrix(output, indent, "ConcatMatrix (animation)", mAnimationMatrix);
132
133 output << std::fixed << std::setprecision(2);
134 if (hasTransformMatrix()) {
135 if (isTransformTranslateOnly()) {
136 output << indent << "(Translate " << getTranslationX() << ", " << getTranslationY()
137 << ", " << getZ() << ")" << std::endl;
138 } else {
139 dumpMatrix(output, indent, "ConcatMatrix ", mComputedFields.mTransformMatrix);
140 }
141 }
142
143 const bool isLayer = effectiveLayerType() != LayerType::None;
144 int clipFlags = getClippingFlags();
145 if (mPrimitiveFields.mAlpha < 1 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
146 if (isLayer) {
147 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
148 }
149
150 if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
151 // simply scale rendering content's alpha
152 output << indent << "(ScaleAlpha " << mPrimitiveFields.mAlpha << ")" << std::endl;
153 } 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);
158 clipFlags = 0; // all clipping done by savelayer
159 }
160 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;
165 }
166 }
167
168 if (clipFlags) {
169 Rect clipRect;
170 getClippingRectForFlags(clipFlags, &clipRect);
171 output << indent << "(ClipRect " << (int)clipRect.left << ", " << (int)clipRect.top << ", "
172 << (int)clipRect.right << ", " << (int)clipRect.bottom << ")" << std::endl;
173 }
174
175 if (getRevealClip().willClip()) {
176 Rect bounds;
177 getRevealClip().getBounds(&bounds);
178 output << indent << "(Clip to reveal clip with bounds " << bounds.left << ", " << bounds.top
179 << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl;
180 }
181
182 auto& outline = mPrimitiveFields.mOutline;
183 if (outline.getShouldClip()) {
184 if (outline.isEmpty()) {
185 output << indent << "(Clip to empty outline)";
186 } else if (outline.willClip()) {
187 const Rect& bounds = outline.getBounds();
188 output << indent << "(Clip to outline with bounds " << bounds.left << ", " << bounds.top
189 << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl;
190 }
191 }
192 }
193
updateMatrix()194 void RenderProperties::updateMatrix() {
195 if (mPrimitiveFields.mMatrixOrPivotDirty) {
196 if (!mComputedFields.mTransformMatrix) {
197 // only allocate a mPrimitiveFields.matrix if we have a complex transform
198 mComputedFields.mTransformMatrix = new SkMatrix();
199 }
200 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();
206 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
207 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(),
220 getPivotY() + getTranslationY());
221 transform->postConcat(transform3D);
222 mComputedFields.mTransformCamera.restore();
223 }
224 mPrimitiveFields.mMatrixOrPivotDirty = false;
225 }
226 }
227
228 } /* namespace uirenderer */
229 } /* namespace android */
230