blob: 6163df5aef3f63734aa184c0a76cb8afd83ebbd6 [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>
Chris Craik8c271ca2014-03-25 10:33:01 -070025#include <SkPath.h>
26#include <SkPathOps.h>
John Reckacb6f072014-03-12 16:11:23 -070027
28#include "Matrix.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070029#include "utils/MathUtils.h"
John Reckf7483e32014-04-11 08:54:47 -070030
John Reckacb6f072014-03-12 16:11:23 -070031namespace android {
32namespace uirenderer {
33
John Reckd0a0b2a2014-03-20 16:28:56 -070034RenderProperties::PrimitiveFields::PrimitiveFields()
John Reckacb6f072014-03-12 16:11:23 -070035 : mClipToBounds(true)
36 , mProjectBackwards(false)
37 , mProjectionReceiver(false)
John Reckacb6f072014-03-12 16:11:23 -070038 , mAlpha(1)
39 , mHasOverlappingRendering(true)
Chris Craikcc39e162014-04-25 18:34:11 -070040 , mElevation(0)
John Reckacb6f072014-03-12 16:11:23 -070041 , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
42 , mRotation(0), mRotationX(0), mRotationY(0)
43 , mScaleX(1), mScaleY(1)
44 , mPivotX(0), mPivotY(0)
John Reckacb6f072014-03-12 16:11:23 -070045 , mLeft(0), mTop(0), mRight(0), mBottom(0)
46 , mWidth(0), mHeight(0)
John Recke4267ea2014-06-03 15:53:15 -070047 , mScrollX(0), mScrollY(0)
John Reckacb6f072014-03-12 16:11:23 -070048 , mPivotExplicitlySet(false)
John Reckf7483e32014-04-11 08:54:47 -070049 , mMatrixOrPivotDirty(false)
John Reckacb6f072014-03-12 16:11:23 -070050 , mCaching(false) {
John Reckacb6f072014-03-12 16:11:23 -070051}
52
John Reckd0a0b2a2014-03-20 16:28:56 -070053RenderProperties::ComputedFields::ComputedFields()
Chris Craik2bcad172014-05-14 18:11:23 -070054 : mTransformMatrix(NULL) {
John Reckd0a0b2a2014-03-20 16:28:56 -070055}
56
57RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070058 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -070059}
60
61RenderProperties::RenderProperties()
Chris Craik49e6c7392014-03-31 12:34:11 -070062 : mStaticMatrix(NULL)
John Reckd0a0b2a2014-03-20 16:28:56 -070063 , mAnimationMatrix(NULL) {
64}
65
66RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070067 delete mStaticMatrix;
68 delete mAnimationMatrix;
69}
70
John Reckd0a0b2a2014-03-20 16:28:56 -070071RenderProperties& 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
Chris Craik49e6c7392014-03-31 12:34:11 -070078 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -070079 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -070080 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -070081 }
82 return *this;
John Reckacb6f072014-03-12 16:11:23 -070083}
84
John Reckd0a0b2a2014-03-20 16:28:56 -070085void 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 }
John Reckf7483e32014-04-11 08:54:47 -070097 if (hasTransformMatrix()) {
98 if (isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -070099 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
Chris Craikcc39e162014-04-25 18:34:11 -0700100 level * 2, "", getTranslationX(), getTranslationY(), getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700101 } else {
Chris Craik49e6c7392014-03-31 12:34:11 -0700102 ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
103 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700104 }
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 }
John Reck78ce1c52014-03-24 15:43:49 -0700119 ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
120 0, 0, getWidth(), getHeight(),
John Reckd0a0b2a2014-03-20 16:28:56 -0700121 (int)(mPrimitiveFields.mAlpha * 255), flags);
122 }
123 }
124 if (clipToBoundsNeeded) {
John Reck78ce1c52014-03-24 15:43:49 -0700125 ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
126 0, 0, getWidth(), getHeight());
John Reckd0a0b2a2014-03-20 16:28:56 -0700127 }
John Reckacb6f072014-03-12 16:11:23 -0700128}
129
130void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700131 if (mPrimitiveFields.mMatrixOrPivotDirty) {
132 if (!mComputedFields.mTransformMatrix) {
133 // only allocate a mPrimitiveFields.matrix if we have a complex transform
134 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700135 }
John Reckf7483e32014-04-11 08:54:47 -0700136 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();
Chris Craike0bb87d2014-04-22 17:55:41 -0700142 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700143 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;
John Reckacb6f072014-03-12 16:11:23 -0700161 }
162}
163
John Reckacb6f072014-03-12 16:11:23 -0700164} /* namespace uirenderer */
165} /* namespace android */