RenderProperties.cpp revision e248bd1b2c3fcf8088429507e73b31f45ee2544b
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 <SkCanvas.h>
22#include <SkColorFilter.h>
23#include <SkMatrix.h>
24#include <SkPath.h>
25#include <SkPathOps.h>
26
27#include "Matrix.h"
28#include "OpenGLRenderer.h"
29#include "utils/MathUtils.h"
30
31namespace android {
32namespace uirenderer {
33
34LayerProperties::LayerProperties() {
35    reset();
36}
37
38LayerProperties::~LayerProperties() {
39    setType(LayerType::None);
40}
41
42void LayerProperties::reset() {
43    mOpaque = false;
44    setFromPaint(nullptr);
45}
46
47bool LayerProperties::setColorFilter(SkColorFilter* filter) {
48   if (mColorFilter == filter) return false;
49   SkRefCnt_SafeAssign(mColorFilter, filter);
50   return true;
51}
52
53bool LayerProperties::setFromPaint(const SkPaint* paint) {
54    bool changed = false;
55    SkXfermode::Mode mode;
56    int alpha;
57    OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
58    changed |= setAlpha(static_cast<uint8_t>(alpha));
59    changed |= setXferMode(mode);
60    changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
61    return changed;
62}
63
64LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
65    setType(other.type());
66    setOpaque(other.opaque());
67    setAlpha(other.alpha());
68    setXferMode(other.xferMode());
69    setColorFilter(other.colorFilter());
70    return *this;
71}
72
73RenderProperties::ComputedFields::ComputedFields()
74        : mTransformMatrix(nullptr) {
75}
76
77RenderProperties::ComputedFields::~ComputedFields() {
78    delete mTransformMatrix;
79}
80
81RenderProperties::RenderProperties()
82        : mStaticMatrix(nullptr)
83        , mAnimationMatrix(nullptr) {
84}
85
86RenderProperties::~RenderProperties() {
87    delete mStaticMatrix;
88    delete mAnimationMatrix;
89}
90
91RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
92    if (this != &other) {
93        mPrimitiveFields = other.mPrimitiveFields;
94        setStaticMatrix(other.getStaticMatrix());
95        setAnimationMatrix(other.getAnimationMatrix());
96        setCameraDistance(other.getCameraDistance());
97        mLayerProperties = other.layerProperties();
98
99        // Force recalculation of the matrix, since other's dirty bit may be clear
100        mPrimitiveFields.mMatrixOrPivotDirty = true;
101        updateMatrix();
102    }
103    return *this;
104}
105
106void RenderProperties::debugOutputProperties(const int level) const {
107    if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
108        ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
109    }
110    if (mStaticMatrix) {
111        ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
112                level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
113    }
114    if (mAnimationMatrix) {
115        ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
116                level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
117    }
118    if (hasTransformMatrix()) {
119        if (isTransformTranslateOnly()) {
120            ALOGD("%*sTranslate %.2f, %.2f, %.2f",
121                    level * 2, "", getTranslationX(), getTranslationY(), getZ());
122        } else {
123            ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
124                    level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
125        }
126    }
127
128    const bool isLayer = effectiveLayerType() != LayerType::None;
129    int clipFlags = getClippingFlags();
130    if (mPrimitiveFields.mAlpha < 1
131            && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
132        if (isLayer) {
133            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
134        }
135
136        if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
137            // simply scale rendering content's alpha
138            ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
139        } else {
140            // savelayeralpha to create an offscreen buffer to apply alpha
141            Rect layerBounds(0, 0, getWidth(), getHeight());
142            if (clipFlags) {
143                getClippingRectForFlags(clipFlags, &layerBounds);
144                clipFlags = 0; // all clipping done by savelayer
145            }
146            ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
147                    (int)layerBounds.left, (int)layerBounds.top,
148                    (int)layerBounds.right, (int)layerBounds.bottom,
149                    (int)(mPrimitiveFields.mAlpha * 255),
150                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
151        }
152
153
154    }
155    if (clipFlags) {
156        Rect clipRect;
157        getClippingRectForFlags(clipFlags, &clipRect);
158        ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
159                (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
160    }
161}
162
163void RenderProperties::updateMatrix() {
164    if (mPrimitiveFields.mMatrixOrPivotDirty) {
165        if (!mComputedFields.mTransformMatrix) {
166            // only allocate a mPrimitiveFields.matrix if we have a complex transform
167            mComputedFields.mTransformMatrix = new SkMatrix();
168        }
169        if (!mPrimitiveFields.mPivotExplicitlySet) {
170            mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
171            mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
172        }
173        SkMatrix* transform = mComputedFields.mTransformMatrix;
174        transform->reset();
175        if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
176            transform->setTranslate(getTranslationX(), getTranslationY());
177            transform->preRotate(getRotation(), getPivotX(), getPivotY());
178            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
179        } else {
180            SkMatrix transform3D;
181            mComputedFields.mTransformCamera.save();
182            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
183            mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
184            mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
185            mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
186            mComputedFields.mTransformCamera.getMatrix(&transform3D);
187            transform3D.preTranslate(-getPivotX(), -getPivotY());
188            transform3D.postTranslate(getPivotX() + getTranslationX(),
189                    getPivotY() + getTranslationY());
190            transform->postConcat(transform3D);
191            mComputedFields.mTransformCamera.restore();
192        }
193        mPrimitiveFields.mMatrixOrPivotDirty = false;
194    }
195}
196
197} /* namespace uirenderer */
198} /* namespace android */
199