RenderProperties.cpp revision 5e00c7ce063116c11315639f0035aca8ad73e8cc
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
102void RenderProperties::debugOutputProperties(const int level) const {
103    if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
104        ALOGD("%*s(Translate (left, top) %d, %d)", level * 2, "",
105                mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
106    }
107    if (mStaticMatrix) {
108        ALOGD("%*s(ConcatMatrix (static) %p: " SK_MATRIX_STRING ")",
109                level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
110    }
111    if (mAnimationMatrix) {
112        ALOGD("%*s(ConcatMatrix (animation) %p: " SK_MATRIX_STRING ")",
113                level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
114    }
115    if (hasTransformMatrix()) {
116        if (isTransformTranslateOnly()) {
117            ALOGD("%*s(Translate %.2f, %.2f, %.2f)",
118                    level * 2, "", getTranslationX(), getTranslationY(), getZ());
119        } else {
120            ALOGD("%*s(ConcatMatrix %p: " SK_MATRIX_STRING ")",
121                    level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
122        }
123    }
124
125    const bool isLayer = effectiveLayerType() != LayerType::None;
126    int clipFlags = getClippingFlags();
127    if (mPrimitiveFields.mAlpha < 1
128            && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
129        if (isLayer) {
130            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
131        }
132
133        if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
134            // simply scale rendering content's alpha
135            ALOGD("%*s(ScaleAlpha %.2f)", level * 2, "", mPrimitiveFields.mAlpha);
136        } else {
137            // savelayeralpha to create an offscreen buffer to apply alpha
138            Rect layerBounds(0, 0, getWidth(), getHeight());
139            if (clipFlags) {
140                getClippingRectForFlags(clipFlags, &layerBounds);
141                clipFlags = 0; // all clipping done by savelayer
142            }
143            ALOGD("%*s(SaveLayerAlpha %d, %d, %d, %d, %d, 0x%x)", level * 2, "",
144                    (int)layerBounds.left, (int)layerBounds.top,
145                    (int)layerBounds.right, (int)layerBounds.bottom,
146                    (int)(mPrimitiveFields.mAlpha * 255),
147                    SaveFlags::HasAlphaLayer | SaveFlags::ClipToLayer);
148        }
149    }
150
151    if (clipFlags) {
152        Rect clipRect;
153        getClippingRectForFlags(clipFlags, &clipRect);
154        ALOGD("%*s(ClipRect %d, %d, %d, %d)", level * 2, "",
155                (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
156    }
157
158    if (getRevealClip().willClip()) {
159        Rect bounds;
160        getRevealClip().getBounds(&bounds);
161        ALOGD("%*s(Clip to reveal clip with bounds %.2f %.2f %.2f %.2f)", level * 2, "",
162                RECT_ARGS(bounds));
163    }
164
165    auto& outline = mPrimitiveFields.mOutline;
166    if (outline.getShouldClip()) {
167        if (outline.isEmpty()) {
168            ALOGD("%*s(Clip to empty outline)", level * 2, "");
169        } else if (outline.willClip()) {
170            ALOGD("%*s(Clip to outline with bounds %.2f %.2f %.2f %.2f)", level * 2, "",
171                    RECT_ARGS(outline.getBounds()));
172        }
173    }
174}
175
176void RenderProperties::updateMatrix() {
177    if (mPrimitiveFields.mMatrixOrPivotDirty) {
178        if (!mComputedFields.mTransformMatrix) {
179            // only allocate a mPrimitiveFields.matrix if we have a complex transform
180            mComputedFields.mTransformMatrix = new SkMatrix();
181        }
182        if (!mPrimitiveFields.mPivotExplicitlySet) {
183            mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
184            mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
185        }
186        SkMatrix* transform = mComputedFields.mTransformMatrix;
187        transform->reset();
188        if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
189            transform->setTranslate(getTranslationX(), getTranslationY());
190            transform->preRotate(getRotation(), getPivotX(), getPivotY());
191            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
192        } else {
193            SkMatrix transform3D;
194            mComputedFields.mTransformCamera.save();
195            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
196            mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
197            mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
198            mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
199            mComputedFields.mTransformCamera.getMatrix(&transform3D);
200            transform3D.preTranslate(-getPivotX(), -getPivotY());
201            transform3D.postTranslate(getPivotX() + getTranslationX(),
202                    getPivotY() + getTranslationY());
203            transform->postConcat(transform3D);
204            mComputedFields.mTransformCamera.restore();
205        }
206        mPrimitiveFields.mMatrixOrPivotDirty = false;
207    }
208}
209
210} /* namespace uirenderer */
211} /* namespace android */
212