RenderProperties.cpp revision 0334c314a5721f49b4d172a9cefe10f157cb28a4
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        , mClipToOutline(false)
30        , mAlpha(1)
31        , mHasOverlappingRendering(true)
32        , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
33        , mRotation(0), mRotationX(0), mRotationY(0)
34        , mScaleX(1), mScaleY(1)
35        , mPivotX(0), mPivotY(0)
36        , mCameraDistance(0)
37        , mLeft(0), mTop(0), mRight(0), mBottom(0)
38        , mWidth(0), mHeight(0)
39        , mPrevWidth(-1), mPrevHeight(-1)
40        , mPivotExplicitlySet(false)
41        , mMatrixDirty(false)
42        , mMatrixIsIdentity(true)
43        , mTransformMatrix(NULL)
44        , mMatrixFlags(0)
45        , mTransformCamera(NULL)
46        , mTransformMatrix3D(NULL)
47        , mStaticMatrix(NULL)
48        , mAnimationMatrix(NULL)
49        , mCaching(false) {
50    mOutline.rewind();
51}
52
53RenderProperties::~RenderProperties() {
54    delete mTransformMatrix;
55    delete mTransformCamera;
56    delete mTransformMatrix3D;
57    delete mStaticMatrix;
58    delete mAnimationMatrix;
59}
60
61float RenderProperties::getPivotX() {
62    updateMatrix();
63    return mPivotX;
64}
65
66float RenderProperties::getPivotY() {
67    updateMatrix();
68    return mPivotY;
69}
70
71void RenderProperties::updateMatrix() {
72    if (mMatrixDirty) {
73        // NOTE: mTransformMatrix won't be up to date if a DisplayList goes from a complex transform
74        // to a pure translate. This is safe because the matrix isn't read in pure translate cases.
75        if (mMatrixFlags && mMatrixFlags != TRANSLATION) {
76            if (!mTransformMatrix) {
77                // only allocate a matrix if we have a complex transform
78                mTransformMatrix = new Matrix4();
79            }
80            if (!mPivotExplicitlySet) {
81                if (mWidth != mPrevWidth || mHeight != mPrevHeight) {
82                    mPrevWidth = mWidth;
83                    mPrevHeight = mHeight;
84                    mPivotX = mPrevWidth / 2.0f;
85                    mPivotY = mPrevHeight / 2.0f;
86                }
87            }
88
89            if ((mMatrixFlags & ROTATION_3D) == 0) {
90                mTransformMatrix->loadTranslate(
91                        mPivotX + mTranslationX,
92                        mPivotY + mTranslationY,
93                        0);
94                mTransformMatrix->rotate(mRotation, 0, 0, 1);
95                mTransformMatrix->scale(mScaleX, mScaleY, 1);
96                mTransformMatrix->translate(-mPivotX, -mPivotY);
97            } else {
98                if (!mTransformCamera) {
99                    mTransformCamera = new Sk3DView();
100                    mTransformMatrix3D = new SkMatrix();
101                }
102                SkMatrix transformMatrix;
103                transformMatrix.reset();
104                mTransformCamera->save();
105                transformMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
106                mTransformCamera->rotateX(mRotationX);
107                mTransformCamera->rotateY(mRotationY);
108                mTransformCamera->rotateZ(-mRotation);
109                mTransformCamera->getMatrix(mTransformMatrix3D);
110                mTransformMatrix3D->preTranslate(-mPivotX, -mPivotY);
111                mTransformMatrix3D->postTranslate(mPivotX + mTranslationX,
112                        mPivotY + mTranslationY);
113                transformMatrix.postConcat(*mTransformMatrix3D);
114                mTransformCamera->restore();
115
116                mTransformMatrix->load(transformMatrix);
117            }
118        }
119        mMatrixDirty = false;
120    }
121}
122
123} /* namespace uirenderer */
124} /* namespace android */
125