RenderProperties.cpp revision bf6f0f260886a04a1680c7f9917124a751322ca4
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    changed |= setAlpha(static_cast<uint8_t>(PaintUtils::getAlphaDirect(paint)));
56    changed |= setXferMode(PaintUtils::getXfermodeDirect(paint));
57    changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
58    return changed;
59}
60
61LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
62    setType(other.type());
63    setOpaque(other.opaque());
64    setAlpha(other.alpha());
65    setXferMode(other.xferMode());
66    setColorFilter(other.colorFilter());
67    return *this;
68}
69
70RenderProperties::ComputedFields::ComputedFields()
71        : mTransformMatrix(nullptr) {
72}
73
74RenderProperties::ComputedFields::~ComputedFields() {
75    delete mTransformMatrix;
76}
77
78RenderProperties::RenderProperties()
79        : mStaticMatrix(nullptr)
80        , mAnimationMatrix(nullptr) {
81}
82
83RenderProperties::~RenderProperties() {
84    delete mStaticMatrix;
85    delete mAnimationMatrix;
86}
87
88RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
89    if (this != &other) {
90        mPrimitiveFields = other.mPrimitiveFields;
91        setStaticMatrix(other.getStaticMatrix());
92        setAnimationMatrix(other.getAnimationMatrix());
93        setCameraDistance(other.getCameraDistance());
94        mLayerProperties = other.layerProperties();
95
96        // Force recalculation of the matrix, since other's dirty bit may be clear
97        mPrimitiveFields.mMatrixOrPivotDirty = true;
98        updateMatrix();
99    }
100    return *this;
101}
102
103void RenderProperties::debugOutputProperties(const int level) const {
104    if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
105        ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
106    }
107    if (mStaticMatrix) {
108        ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
109                level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
110    }
111    if (mAnimationMatrix) {
112        ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
113                level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
114    }
115    if (hasTransformMatrix()) {
116        if (isTransformTranslateOnly()) {
117            ALOGD("%*sTranslate %.2f, %.2f, %.2f",
118                    level * 2, "", getTranslationX(), getTranslationY(), getZ());
119        } else {
120            ALOGD("%*sConcatMatrix %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("%*sScaleAlpha %.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("%*sSaveLayerAlpha %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                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
148        }
149
150
151    }
152    if (clipFlags) {
153        Rect clipRect;
154        getClippingRectForFlags(clipFlags, &clipRect);
155        ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
156                (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
157    }
158}
159
160void RenderProperties::updateMatrix() {
161    if (mPrimitiveFields.mMatrixOrPivotDirty) {
162        if (!mComputedFields.mTransformMatrix) {
163            // only allocate a mPrimitiveFields.matrix if we have a complex transform
164            mComputedFields.mTransformMatrix = new SkMatrix();
165        }
166        if (!mPrimitiveFields.mPivotExplicitlySet) {
167            mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
168            mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
169        }
170        SkMatrix* transform = mComputedFields.mTransformMatrix;
171        transform->reset();
172        if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
173            transform->setTranslate(getTranslationX(), getTranslationY());
174            transform->preRotate(getRotation(), getPivotX(), getPivotY());
175            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
176        } else {
177            SkMatrix transform3D;
178            mComputedFields.mTransformCamera.save();
179            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
180            mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
181            mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
182            mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
183            mComputedFields.mTransformCamera.getMatrix(&transform3D);
184            transform3D.preTranslate(-getPivotX(), -getPivotY());
185            transform3D.postTranslate(getPivotX() + getTranslationX(),
186                    getPivotY() + getTranslationY());
187            transform->postConcat(transform3D);
188            mComputedFields.mTransformCamera.restore();
189        }
190        mPrimitiveFields.mMatrixOrPivotDirty = false;
191    }
192}
193
194} /* namespace uirenderer */
195} /* namespace android */
196