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