• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
setFromPaint(const SkPaint * paint)58 bool LayerProperties::setFromPaint(const SkPaint* paint) {
59     bool changed = false;
60     changed |= setAlpha(static_cast<uint8_t>(PaintUtils::getAlphaDirect(paint)));
61     changed |= setXferMode(PaintUtils::getBlendModeDirect(paint));
62     changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
63     return changed;
64 }
65 
operator =(const LayerProperties & other)66 LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
67     setType(other.type());
68     setOpaque(other.opaque());
69     setAlpha(other.alpha());
70     setXferMode(other.xferMode());
71     setColorFilter(other.getColorFilter());
72     setImageFilter(other.getImageFilter());
73     mStretchEffect = other.mStretchEffect;
74     return *this;
75 }
76 
ComputedFields()77 RenderProperties::ComputedFields::ComputedFields() : mTransformMatrix(nullptr) {}
78 
~ComputedFields()79 RenderProperties::ComputedFields::~ComputedFields() {
80     delete mTransformMatrix;
81 }
82 
RenderProperties()83 RenderProperties::RenderProperties() : mStaticMatrix(nullptr), mAnimationMatrix(nullptr) {}
84 
~RenderProperties()85 RenderProperties::~RenderProperties() {
86     delete mStaticMatrix;
87     delete mAnimationMatrix;
88 }
89 
operator =(const RenderProperties & other)90 RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
91     if (this != &other) {
92         mPrimitiveFields = other.mPrimitiveFields;
93         setStaticMatrix(other.getStaticMatrix());
94         setAnimationMatrix(other.getAnimationMatrix());
95         setCameraDistance(other.getCameraDistance());
96         mLayerProperties = other.layerProperties();
97 
98         // Force recalculation of the matrix, since other's dirty bit may be clear
99         mPrimitiveFields.mMatrixOrPivotDirty = true;
100         updateMatrix();
101     }
102     return *this;
103 }
104 
dumpMatrix(std::ostream & output,std::string & indent,const char * label,SkMatrix * matrix)105 static void dumpMatrix(std::ostream& output, std::string& indent, const char* label,
106                        SkMatrix* matrix) {
107     if (matrix) {
108         output << indent << "(" << label << " " << matrix << ": ";
109         output << std::fixed << std::setprecision(2);
110         output << "[" << matrix->get(0) << " " << matrix->get(1) << " " << matrix->get(2) << "]";
111         output << " [" << matrix->get(3) << " " << matrix->get(4) << " " << matrix->get(5) << "]";
112         output << " [" << matrix->get(6) << " " << matrix->get(7) << " " << matrix->get(8) << "]";
113         output << ")" << std::endl;
114     }
115 }
116 
debugOutputProperties(std::ostream & output,const int level) const117 void RenderProperties::debugOutputProperties(std::ostream& output, const int level) const {
118     auto indent = std::string(level * 2, ' ');
119     if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
120         output << indent << "(Translate (left, top) " << mPrimitiveFields.mLeft << ", "
121                << mPrimitiveFields.mTop << ")" << std::endl;
122     }
123     dumpMatrix(output, indent, "ConcatMatrix (static)", mStaticMatrix);
124     dumpMatrix(output, indent, "ConcatMatrix (animation)", mAnimationMatrix);
125 
126     output << std::fixed << std::setprecision(2);
127     if (hasTransformMatrix()) {
128         if (isTransformTranslateOnly()) {
129             output << indent << "(Translate " << getTranslationX() << ", " << getTranslationY()
130                    << ", " << getZ() << ")" << std::endl;
131         } else {
132             dumpMatrix(output, indent, "ConcatMatrix ", mComputedFields.mTransformMatrix);
133         }
134     }
135 
136     const bool isLayer = effectiveLayerType() != LayerType::None;
137     int clipFlags = getClippingFlags();
138     if (mPrimitiveFields.mAlpha < 1 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
139         if (isLayer) {
140             clipFlags &= ~CLIP_TO_BOUNDS;  // bounds clipping done by layer
141         }
142 
143         if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
144             // simply scale rendering content's alpha
145             output << indent << "(ScaleAlpha " << mPrimitiveFields.mAlpha << ")" << std::endl;
146         } else {
147             // savelayeralpha to create an offscreen buffer to apply alpha
148             Rect layerBounds(0, 0, getWidth(), getHeight());
149             if (clipFlags) {
150                 getClippingRectForFlags(clipFlags, &layerBounds);
151                 clipFlags = 0;  // all clipping done by savelayer
152             }
153             output << indent << "(SaveLayerAlpha " << (int)layerBounds.left << ", "
154                    << (int)layerBounds.top << ", " << (int)layerBounds.right << ", "
155                    << (int)layerBounds.bottom << ", " << (int)(mPrimitiveFields.mAlpha * 255)
156                    << ", 0x" << std::hex << (SaveFlags::HasAlphaLayer | SaveFlags::ClipToLayer)
157                    << ")" << std::dec << std::endl;
158         }
159     }
160 
161     if (clipFlags) {
162         Rect clipRect;
163         getClippingRectForFlags(clipFlags, &clipRect);
164         output << indent << "(ClipRect " << (int)clipRect.left << ", " << (int)clipRect.top << ", "
165                << (int)clipRect.right << ", " << (int)clipRect.bottom << ")" << std::endl;
166     }
167 
168     if (getRevealClip().willClip()) {
169         Rect bounds;
170         getRevealClip().getBounds(&bounds);
171         output << indent << "(Clip to reveal clip with bounds " << bounds.left << ", " << bounds.top
172                << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl;
173     }
174 
175     auto& outline = mPrimitiveFields.mOutline;
176     if (outline.getShouldClip()) {
177         if (outline.isEmpty()) {
178             output << indent << "(Clip to empty outline)";
179         } else if (outline.willClip()) {
180             const Rect& bounds = outline.getBounds();
181             output << indent << "(Clip to outline with bounds " << bounds.left << ", " << bounds.top
182                    << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl;
183         }
184     }
185 }
186 
updateMatrix()187 void RenderProperties::updateMatrix() {
188     if (mPrimitiveFields.mMatrixOrPivotDirty) {
189         if (!mComputedFields.mTransformMatrix) {
190             // only allocate a mPrimitiveFields.matrix if we have a complex transform
191             mComputedFields.mTransformMatrix = new SkMatrix();
192         }
193         if (!mPrimitiveFields.mPivotExplicitlySet) {
194             mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
195             mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
196         }
197         SkMatrix* transform = mComputedFields.mTransformMatrix;
198         transform->reset();
199         if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
200             transform->setTranslate(getTranslationX(), getTranslationY());
201             transform->preRotate(getRotation(), getPivotX(), getPivotY());
202             transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
203         } else {
204             SkMatrix transform3D;
205             mComputedFields.mTransformCamera.save();
206             transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
207             mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
208             mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
209             mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
210             mComputedFields.mTransformCamera.getMatrix(&transform3D);
211             transform3D.preTranslate(-getPivotX(), -getPivotY());
212             transform3D.postTranslate(getPivotX() + getTranslationX(),
213                                       getPivotY() + getTranslationY());
214             transform->postConcat(transform3D);
215             mComputedFields.mTransformCamera.restore();
216         }
217         mPrimitiveFields.mMatrixOrPivotDirty = false;
218     }
219 }
220 
221 } /* namespace uirenderer */
222 } /* namespace android */
223