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