blob: 8b69b08a0ad14bf6fb09b3b6d18cb8ba22cc3b62 [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");
John Reckd0a0b2a2014-03-20 16:28:56 -07005 * you mPrimitiveFields.may not use this file except in compliance with the License.
6 * You mPrimitiveFields.may obtain a copy of the License at
John Reckacb6f072014-03-12 16:11:23 -07007 *
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 */
John Reckd0a0b2a2014-03-20 16:28:56 -070016
17#define LOG_TAG "OpenGLRenderer"
18
John Reckacb6f072014-03-12 16:11:23 -070019#include "RenderProperties.h"
20
John Reckd0a0b2a2014-03-20 16:28:56 -070021#include <utils/Trace.h>
22
23#include <SkCanvas.h>
John Reckacb6f072014-03-12 16:11:23 -070024#include <SkMatrix.h>
25
26#include "Matrix.h"
27
28namespace android {
29namespace uirenderer {
30
John Reckd0a0b2a2014-03-20 16:28:56 -070031RenderProperties::PrimitiveFields::PrimitiveFields()
John Reckacb6f072014-03-12 16:11:23 -070032 : mClipToBounds(true)
33 , mProjectBackwards(false)
34 , mProjectionReceiver(false)
John Reckacb6f072014-03-12 16:11:23 -070035 , mAlpha(1)
36 , mHasOverlappingRendering(true)
37 , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
38 , mRotation(0), mRotationX(0), mRotationY(0)
39 , mScaleX(1), mScaleY(1)
40 , mPivotX(0), mPivotY(0)
John Reckacb6f072014-03-12 16:11:23 -070041 , mLeft(0), mTop(0), mRight(0), mBottom(0)
42 , mWidth(0), mHeight(0)
43 , mPrevWidth(-1), mPrevHeight(-1)
44 , mPivotExplicitlySet(false)
45 , mMatrixDirty(false)
46 , mMatrixIsIdentity(true)
John Reckacb6f072014-03-12 16:11:23 -070047 , mMatrixFlags(0)
John Reckacb6f072014-03-12 16:11:23 -070048 , mCaching(false) {
John Reckacb6f072014-03-12 16:11:23 -070049}
50
John Reckd0a0b2a2014-03-20 16:28:56 -070051RenderProperties::ComputedFields::ComputedFields()
52 : mTransformMatrix(NULL)
53 , mTransformCamera(NULL)
54 , mTransformMatrix3D(NULL) {
55}
56
57RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070058 delete mTransformMatrix;
59 delete mTransformCamera;
60 delete mTransformMatrix3D;
John Reckd0a0b2a2014-03-20 16:28:56 -070061}
62
63RenderProperties::RenderProperties()
64 : mCameraDistance(0)
65 , mStaticMatrix(NULL)
66 , mAnimationMatrix(NULL) {
67}
68
69RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070070 delete mStaticMatrix;
71 delete mAnimationMatrix;
72}
73
John Reckd0a0b2a2014-03-20 16:28:56 -070074RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
75 if (this != &other) {
76 mPrimitiveFields = other.mPrimitiveFields;
77 setStaticMatrix(other.getStaticMatrix());
78 setAnimationMatrix(other.getAnimationMatrix());
79 setCameraDistance(other.getCameraDistance());
80
81 // Update the computed fields
82 updateMatrix();
83 }
84 return *this;
John Reckacb6f072014-03-12 16:11:23 -070085}
86
John Reckd0a0b2a2014-03-20 16:28:56 -070087void RenderProperties::debugOutputProperties(const int level) const {
88 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
89 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
90 }
91 if (mStaticMatrix) {
92 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
93 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
94 }
95 if (mAnimationMatrix) {
96 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
97 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
98 }
99 if (mPrimitiveFields.mMatrixFlags != 0) {
100 if (mPrimitiveFields.mMatrixFlags == TRANSLATION) {
101 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
102 level * 2, "", mPrimitiveFields.mTranslationX, mPrimitiveFields.mTranslationY, mPrimitiveFields.mTranslationZ);
103 } else {
104 ALOGD("%*sConcatMatrix %p: " MATRIX_4_STRING,
105 level * 2, "", mComputedFields.mTransformMatrix, MATRIX_4_ARGS(mComputedFields.mTransformMatrix));
106 }
107 }
108
109 bool clipToBoundsNeeded = mPrimitiveFields.mCaching ? false : mPrimitiveFields.mClipToBounds;
110 if (mPrimitiveFields.mAlpha < 1) {
111 if (mPrimitiveFields.mCaching) {
112 ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
113 } else if (!mPrimitiveFields.mHasOverlappingRendering) {
114 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
115 } else {
116 int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
117 if (clipToBoundsNeeded) {
118 flags |= SkCanvas::kClipToLayer_SaveFlag;
119 clipToBoundsNeeded = false; // clipping done by save layer
120 }
121 ALOGD("%*sSaveLayerAlpha %.2f, %.2f, %.2f, %.2f, %d, 0x%x", level * 2, "",
122 (float) 0, (float) 0, (float) mPrimitiveFields.mRight - mPrimitiveFields.mLeft, (float) mPrimitiveFields.mBottom - mPrimitiveFields.mTop,
123 (int)(mPrimitiveFields.mAlpha * 255), flags);
124 }
125 }
126 if (clipToBoundsNeeded) {
127 ALOGD("%*sClipRect %.2f, %.2f, %.2f, %.2f", level * 2, "", 0.0f, 0.0f,
128 (float) mPrimitiveFields.mRight - mPrimitiveFields.mLeft, (float) mPrimitiveFields.mBottom - mPrimitiveFields.mTop);
129 }
John Reckacb6f072014-03-12 16:11:23 -0700130}
131
132void RenderProperties::updateMatrix() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700133 if (mPrimitiveFields.mMatrixDirty) {
134 // NOTE: mComputedFields.mTransformMatrix won't be up to date if a DisplayList goes from a complex transform
135 // to a pure translate. This is safe because the mPrimitiveFields.matrix isn't read in pure translate cases.
136 if (mPrimitiveFields.mMatrixFlags && mPrimitiveFields.mMatrixFlags != TRANSLATION) {
137 if (!mComputedFields.mTransformMatrix) {
138 // only allocate a mPrimitiveFields.matrix if we have a complex transform
139 mComputedFields.mTransformMatrix = new Matrix4();
John Reckacb6f072014-03-12 16:11:23 -0700140 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700141 if (!mPrimitiveFields.mPivotExplicitlySet) {
142 if (mPrimitiveFields.mWidth != mPrimitiveFields.mPrevWidth || mPrimitiveFields.mHeight != mPrimitiveFields.mPrevHeight) {
143 mPrimitiveFields.mPrevWidth = mPrimitiveFields.mWidth;
144 mPrimitiveFields.mPrevHeight = mPrimitiveFields.mHeight;
145 mPrimitiveFields.mPivotX = mPrimitiveFields.mPrevWidth / 2.0f;
146 mPrimitiveFields.mPivotY = mPrimitiveFields.mPrevHeight / 2.0f;
John Reckacb6f072014-03-12 16:11:23 -0700147 }
148 }
149
John Reckd0a0b2a2014-03-20 16:28:56 -0700150 if ((mPrimitiveFields.mMatrixFlags & ROTATION_3D) == 0) {
151 mComputedFields.mTransformMatrix->loadTranslate(
152 mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX,
153 mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY,
John Reckacb6f072014-03-12 16:11:23 -0700154 0);
John Reckd0a0b2a2014-03-20 16:28:56 -0700155 mComputedFields.mTransformMatrix->rotate(mPrimitiveFields.mRotation, 0, 0, 1);
156 mComputedFields.mTransformMatrix->scale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, 1);
157 mComputedFields.mTransformMatrix->translate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY);
John Reckacb6f072014-03-12 16:11:23 -0700158 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700159 if (!mComputedFields.mTransformCamera) {
160 mComputedFields.mTransformCamera = new Sk3DView();
161 mComputedFields.mTransformMatrix3D = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700162 }
163 SkMatrix transformMatrix;
164 transformMatrix.reset();
John Reckd0a0b2a2014-03-20 16:28:56 -0700165 mComputedFields.mTransformCamera->save();
166 transformMatrix.preScale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, mPrimitiveFields.mPivotX, mPrimitiveFields.mPivotY);
167 mComputedFields.mTransformCamera->rotateX(mPrimitiveFields.mRotationX);
168 mComputedFields.mTransformCamera->rotateY(mPrimitiveFields.mRotationY);
169 mComputedFields.mTransformCamera->rotateZ(-mPrimitiveFields.mRotation);
170 mComputedFields.mTransformCamera->getMatrix(mComputedFields.mTransformMatrix3D);
171 mComputedFields.mTransformMatrix3D->preTranslate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY);
172 mComputedFields.mTransformMatrix3D->postTranslate(mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX,
173 mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY);
174 transformMatrix.postConcat(*mComputedFields.mTransformMatrix3D);
175 mComputedFields.mTransformCamera->restore();
John Reckacb6f072014-03-12 16:11:23 -0700176
John Reckd0a0b2a2014-03-20 16:28:56 -0700177 mComputedFields.mTransformMatrix->load(transformMatrix);
John Reckacb6f072014-03-12 16:11:23 -0700178 }
179 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700180 mPrimitiveFields.mMatrixDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700181 }
182}
183
184} /* namespace uirenderer */
185} /* namespace android */