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