RenderProperties.cpp revision 2bcad176757386d906157bb898167fbcebe9f55e
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#define LOG_TAG "OpenGLRenderer"
18
19#include "RenderProperties.h"
20
21#include <utils/Trace.h>
22
23#include <SkCanvas.h>
24#include <SkMatrix.h>
25#include <SkPath.h>
26#include <SkPathOps.h>
27
28#include "Matrix.h"
29#include "utils/MathUtils.h"
30
31namespace android {
32namespace uirenderer {
33
34RenderProperties::PrimitiveFields::PrimitiveFields()
35        : mClipToBounds(true)
36        , mProjectBackwards(false)
37        , mProjectionReceiver(false)
38        , mAlpha(1)
39        , mHasOverlappingRendering(true)
40        , mElevation(0)
41        , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
42        , mRotation(0), mRotationX(0), mRotationY(0)
43        , mScaleX(1), mScaleY(1)
44        , mPivotX(0), mPivotY(0)
45        , mLeft(0), mTop(0), mRight(0), mBottom(0)
46        , mWidth(0), mHeight(0)
47        , mPivotExplicitlySet(false)
48        , mMatrixOrPivotDirty(false)
49        , mCaching(false) {
50}
51
52RenderProperties::ComputedFields::ComputedFields()
53        : mTransformMatrix(NULL) {
54}
55
56RenderProperties::ComputedFields::~ComputedFields() {
57    delete mTransformMatrix;
58}
59
60RenderProperties::RenderProperties()
61        : mStaticMatrix(NULL)
62        , mAnimationMatrix(NULL) {
63}
64
65RenderProperties::~RenderProperties() {
66    delete mStaticMatrix;
67    delete mAnimationMatrix;
68}
69
70RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
71    if (this != &other) {
72        mPrimitiveFields = other.mPrimitiveFields;
73        setStaticMatrix(other.getStaticMatrix());
74        setAnimationMatrix(other.getAnimationMatrix());
75        setCameraDistance(other.getCameraDistance());
76
77        // Force recalculation of the matrix, since other's dirty bit may be clear
78        mPrimitiveFields.mMatrixOrPivotDirty = true;
79        updateMatrix();
80    }
81    return *this;
82}
83
84void RenderProperties::debugOutputProperties(const int level) const {
85    if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
86        ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
87    }
88    if (mStaticMatrix) {
89        ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
90                level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
91    }
92    if (mAnimationMatrix) {
93        ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
94                level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
95    }
96    if (hasTransformMatrix()) {
97        if (isTransformTranslateOnly()) {
98            ALOGD("%*sTranslate %.2f, %.2f, %.2f",
99                    level * 2, "", getTranslationX(), getTranslationY(), getZ());
100        } else {
101            ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
102                    level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
103        }
104    }
105
106    bool clipToBoundsNeeded = mPrimitiveFields.mCaching ? false : mPrimitiveFields.mClipToBounds;
107    if (mPrimitiveFields.mAlpha < 1) {
108        if (mPrimitiveFields.mCaching) {
109            ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
110        } else if (!mPrimitiveFields.mHasOverlappingRendering) {
111            ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
112        } else {
113            int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
114            if (clipToBoundsNeeded) {
115                flags |= SkCanvas::kClipToLayer_SaveFlag;
116                clipToBoundsNeeded = false; // clipping done by save layer
117            }
118            ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
119                    0, 0, getWidth(), getHeight(),
120                    (int)(mPrimitiveFields.mAlpha * 255), flags);
121        }
122    }
123    if (clipToBoundsNeeded) {
124        ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
125                0, 0, getWidth(), getHeight());
126    }
127}
128
129void RenderProperties::updateMatrix() {
130    if (mPrimitiveFields.mMatrixOrPivotDirty) {
131        if (!mComputedFields.mTransformMatrix) {
132            // only allocate a mPrimitiveFields.matrix if we have a complex transform
133            mComputedFields.mTransformMatrix = new SkMatrix();
134        }
135        if (!mPrimitiveFields.mPivotExplicitlySet) {
136            mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
137            mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
138        }
139        SkMatrix* transform = mComputedFields.mTransformMatrix;
140        transform->reset();
141        if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
142            transform->setTranslate(getTranslationX(), getTranslationY());
143            transform->preRotate(getRotation(), getPivotX(), getPivotY());
144            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
145        } else {
146            SkMatrix transform3D;
147            mComputedFields.mTransformCamera.save();
148            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
149            mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
150            mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
151            mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
152            mComputedFields.mTransformCamera.getMatrix(&transform3D);
153            transform3D.preTranslate(-getPivotX(), -getPivotY());
154            transform3D.postTranslate(getPivotX() + getTranslationX(),
155                    getPivotY() + getTranslationY());
156            transform->postConcat(transform3D);
157            mComputedFields.mTransformCamera.restore();
158        }
159        mPrimitiveFields.mMatrixOrPivotDirty = false;
160    }
161}
162
163} /* namespace uirenderer */
164} /* namespace android */
165