RenderProperties.cpp revision b49f446c98096c4790a11d9b5bc83a4e585278c9
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You 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#include "RenderProperties.h"
17
18#include <SkMatrix.h>
19
20#include "Matrix.h"
21
22namespace android {
23namespace uirenderer {
24
25RenderProperties::RenderProperties()
26        : mClipToBounds(true)
27        , mProjectBackwards(false)
28        , mProjectionReceiver(false)
29        , mAlpha(1)
30        , mHasOverlappingRendering(true)
31        , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
32        , mRotation(0), mRotationX(0), mRotationY(0)
33        , mScaleX(1), mScaleY(1)
34        , mPivotX(0), mPivotY(0)
35        , mCameraDistance(0)
36        , mLeft(0), mTop(0), mRight(0), mBottom(0)
37        , mWidth(0), mHeight(0)
38        , mPrevWidth(-1), mPrevHeight(-1)
39        , mPivotExplicitlySet(false)
40        , mMatrixDirty(false)
41        , mMatrixIsIdentity(true)
42        , mTransformMatrix(NULL)
43        , mMatrixFlags(0)
44        , mTransformCamera(NULL)
45        , mTransformMatrix3D(NULL)
46        , mStaticMatrix(NULL)
47        , mAnimationMatrix(NULL)
48        , mCaching(false) {
49}
50
51RenderProperties::~RenderProperties() {
52    delete mTransformMatrix;
53    delete mTransformCamera;
54    delete mTransformMatrix3D;
55    delete mStaticMatrix;
56    delete mAnimationMatrix;
57}
58
59float RenderProperties::getPivotX() {
60    updateMatrix();
61    return mPivotX;
62}
63
64float RenderProperties::getPivotY() {
65    updateMatrix();
66    return mPivotY;
67}
68
69void RenderProperties::updateMatrix() {
70    if (mMatrixDirty) {
71        // NOTE: mTransformMatrix won't be up to date if a DisplayList goes from a complex transform
72        // to a pure translate. This is safe because the matrix isn't read in pure translate cases.
73        if (mMatrixFlags && mMatrixFlags != TRANSLATION) {
74            if (!mTransformMatrix) {
75                // only allocate a matrix if we have a complex transform
76                mTransformMatrix = new Matrix4();
77            }
78            if (!mPivotExplicitlySet) {
79                if (mWidth != mPrevWidth || mHeight != mPrevHeight) {
80                    mPrevWidth = mWidth;
81                    mPrevHeight = mHeight;
82                    mPivotX = mPrevWidth / 2.0f;
83                    mPivotY = mPrevHeight / 2.0f;
84                }
85            }
86
87            if ((mMatrixFlags & ROTATION_3D) == 0) {
88                mTransformMatrix->loadTranslate(
89                        mPivotX + mTranslationX,
90                        mPivotY + mTranslationY,
91                        0);
92                mTransformMatrix->rotate(mRotation, 0, 0, 1);
93                mTransformMatrix->scale(mScaleX, mScaleY, 1);
94                mTransformMatrix->translate(-mPivotX, -mPivotY);
95            } else {
96                if (!mTransformCamera) {
97                    mTransformCamera = new Sk3DView();
98                    mTransformMatrix3D = new SkMatrix();
99                }
100                SkMatrix transformMatrix;
101                transformMatrix.reset();
102                mTransformCamera->save();
103                transformMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
104                mTransformCamera->rotateX(mRotationX);
105                mTransformCamera->rotateY(mRotationY);
106                mTransformCamera->rotateZ(-mRotation);
107                mTransformCamera->getMatrix(mTransformMatrix3D);
108                mTransformMatrix3D->preTranslate(-mPivotX, -mPivotY);
109                mTransformMatrix3D->postTranslate(mPivotX + mTranslationX,
110                        mPivotY + mTranslationY);
111                transformMatrix.postConcat(*mTransformMatrix3D);
112                mTransformCamera->restore();
113
114                mTransformMatrix->load(transformMatrix);
115            }
116        }
117        mMatrixDirty = false;
118    }
119}
120
121} /* namespace uirenderer */
122} /* namespace android */
123