blob: 3975a766a8e587da8c89d88962077896980cd021 [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"
29
30namespace android {
31namespace uirenderer {
32
John Reckd0a0b2a2014-03-20 16:28:56 -070033RenderProperties::PrimitiveFields::PrimitiveFields()
John Reckacb6f072014-03-12 16:11:23 -070034 : mClipToBounds(true)
35 , mProjectBackwards(false)
36 , mProjectionReceiver(false)
John Reckacb6f072014-03-12 16:11:23 -070037 , mAlpha(1)
38 , mHasOverlappingRendering(true)
39 , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
40 , mRotation(0), mRotationX(0), mRotationY(0)
41 , mScaleX(1), mScaleY(1)
42 , mPivotX(0), mPivotY(0)
John Reckacb6f072014-03-12 16:11:23 -070043 , mLeft(0), mTop(0), mRight(0), mBottom(0)
44 , mWidth(0), mHeight(0)
45 , mPrevWidth(-1), mPrevHeight(-1)
46 , mPivotExplicitlySet(false)
47 , mMatrixDirty(false)
48 , mMatrixIsIdentity(true)
John Reckacb6f072014-03-12 16:11:23 -070049 , mMatrixFlags(0)
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()
54 : mTransformMatrix(NULL)
55 , mTransformCamera(NULL)
Chris Craik8c271ca2014-03-25 10:33:01 -070056 , mTransformMatrix3D(NULL)
57 , mClipPath(NULL) {
John Reckd0a0b2a2014-03-20 16:28:56 -070058}
59
60RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070061 delete mTransformMatrix;
62 delete mTransformCamera;
63 delete mTransformMatrix3D;
Chris Craik8c271ca2014-03-25 10:33:01 -070064 delete mClipPath;
John Reckd0a0b2a2014-03-20 16:28:56 -070065}
66
67RenderProperties::RenderProperties()
68 : mCameraDistance(0)
69 , mStaticMatrix(NULL)
70 , mAnimationMatrix(NULL) {
71}
72
73RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070074 delete mStaticMatrix;
75 delete mAnimationMatrix;
76}
77
John Reckd0a0b2a2014-03-20 16:28:56 -070078RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
79 if (this != &other) {
80 mPrimitiveFields = other.mPrimitiveFields;
81 setStaticMatrix(other.getStaticMatrix());
82 setAnimationMatrix(other.getAnimationMatrix());
83 setCameraDistance(other.getCameraDistance());
84
85 // Update the computed fields
86 updateMatrix();
Chris Craik8c271ca2014-03-25 10:33:01 -070087 updateClipPath();
John Reckd0a0b2a2014-03-20 16:28:56 -070088 }
89 return *this;
John Reckacb6f072014-03-12 16:11:23 -070090}
91
John Reckd0a0b2a2014-03-20 16:28:56 -070092void RenderProperties::debugOutputProperties(const int level) const {
93 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
94 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
95 }
96 if (mStaticMatrix) {
97 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
98 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
99 }
100 if (mAnimationMatrix) {
101 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
102 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
103 }
104 if (mPrimitiveFields.mMatrixFlags != 0) {
105 if (mPrimitiveFields.mMatrixFlags == TRANSLATION) {
106 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
107 level * 2, "", mPrimitiveFields.mTranslationX, mPrimitiveFields.mTranslationY, mPrimitiveFields.mTranslationZ);
108 } else {
109 ALOGD("%*sConcatMatrix %p: " MATRIX_4_STRING,
110 level * 2, "", mComputedFields.mTransformMatrix, MATRIX_4_ARGS(mComputedFields.mTransformMatrix));
111 }
112 }
113
114 bool clipToBoundsNeeded = mPrimitiveFields.mCaching ? false : mPrimitiveFields.mClipToBounds;
115 if (mPrimitiveFields.mAlpha < 1) {
116 if (mPrimitiveFields.mCaching) {
117 ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
118 } else if (!mPrimitiveFields.mHasOverlappingRendering) {
119 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
120 } else {
121 int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
122 if (clipToBoundsNeeded) {
123 flags |= SkCanvas::kClipToLayer_SaveFlag;
124 clipToBoundsNeeded = false; // clipping done by save layer
125 }
John Reck78ce1c52014-03-24 15:43:49 -0700126 ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
127 0, 0, getWidth(), getHeight(),
John Reckd0a0b2a2014-03-20 16:28:56 -0700128 (int)(mPrimitiveFields.mAlpha * 255), flags);
129 }
130 }
131 if (clipToBoundsNeeded) {
John Reck78ce1c52014-03-24 15:43:49 -0700132 ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
133 0, 0, getWidth(), getHeight());
John Reckd0a0b2a2014-03-20 16:28:56 -0700134 }
John Reckacb6f072014-03-12 16:11:23 -0700135}
136
137void RenderProperties::updateMatrix() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700138 if (mPrimitiveFields.mMatrixDirty) {
139 // NOTE: mComputedFields.mTransformMatrix won't be up to date if a DisplayList goes from a complex transform
140 // to a pure translate. This is safe because the mPrimitiveFields.matrix isn't read in pure translate cases.
141 if (mPrimitiveFields.mMatrixFlags && mPrimitiveFields.mMatrixFlags != TRANSLATION) {
142 if (!mComputedFields.mTransformMatrix) {
143 // only allocate a mPrimitiveFields.matrix if we have a complex transform
144 mComputedFields.mTransformMatrix = new Matrix4();
John Reckacb6f072014-03-12 16:11:23 -0700145 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700146 if (!mPrimitiveFields.mPivotExplicitlySet) {
147 if (mPrimitiveFields.mWidth != mPrimitiveFields.mPrevWidth || mPrimitiveFields.mHeight != mPrimitiveFields.mPrevHeight) {
148 mPrimitiveFields.mPrevWidth = mPrimitiveFields.mWidth;
149 mPrimitiveFields.mPrevHeight = mPrimitiveFields.mHeight;
150 mPrimitiveFields.mPivotX = mPrimitiveFields.mPrevWidth / 2.0f;
151 mPrimitiveFields.mPivotY = mPrimitiveFields.mPrevHeight / 2.0f;
John Reckacb6f072014-03-12 16:11:23 -0700152 }
153 }
154
John Reckd0a0b2a2014-03-20 16:28:56 -0700155 if ((mPrimitiveFields.mMatrixFlags & ROTATION_3D) == 0) {
156 mComputedFields.mTransformMatrix->loadTranslate(
157 mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX,
158 mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY,
John Reckacb6f072014-03-12 16:11:23 -0700159 0);
John Reckd0a0b2a2014-03-20 16:28:56 -0700160 mComputedFields.mTransformMatrix->rotate(mPrimitiveFields.mRotation, 0, 0, 1);
161 mComputedFields.mTransformMatrix->scale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, 1);
162 mComputedFields.mTransformMatrix->translate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY);
John Reckacb6f072014-03-12 16:11:23 -0700163 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700164 if (!mComputedFields.mTransformCamera) {
165 mComputedFields.mTransformCamera = new Sk3DView();
166 mComputedFields.mTransformMatrix3D = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700167 }
168 SkMatrix transformMatrix;
169 transformMatrix.reset();
John Reckd0a0b2a2014-03-20 16:28:56 -0700170 mComputedFields.mTransformCamera->save();
171 transformMatrix.preScale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, mPrimitiveFields.mPivotX, mPrimitiveFields.mPivotY);
172 mComputedFields.mTransformCamera->rotateX(mPrimitiveFields.mRotationX);
173 mComputedFields.mTransformCamera->rotateY(mPrimitiveFields.mRotationY);
174 mComputedFields.mTransformCamera->rotateZ(-mPrimitiveFields.mRotation);
175 mComputedFields.mTransformCamera->getMatrix(mComputedFields.mTransformMatrix3D);
176 mComputedFields.mTransformMatrix3D->preTranslate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY);
177 mComputedFields.mTransformMatrix3D->postTranslate(mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX,
178 mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY);
179 transformMatrix.postConcat(*mComputedFields.mTransformMatrix3D);
180 mComputedFields.mTransformCamera->restore();
John Reckacb6f072014-03-12 16:11:23 -0700181
John Reckd0a0b2a2014-03-20 16:28:56 -0700182 mComputedFields.mTransformMatrix->load(transformMatrix);
John Reckacb6f072014-03-12 16:11:23 -0700183 }
184 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700185 mPrimitiveFields.mMatrixDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700186 }
187}
188
Chris Craik8c271ca2014-03-25 10:33:01 -0700189void RenderProperties::updateClipPath() {
190 const SkPath* outlineClipPath = mPrimitiveFields.mOutline.willClip()
191 ? mPrimitiveFields.mOutline.getPath() : NULL;
192 const SkPath* revealClipPath = mPrimitiveFields.mRevealClip.getPath();
193
194 if (!outlineClipPath && !revealClipPath) {
195 // mComputedFields.mClipPath doesn't need to be updated, since it won't be used
196 return;
197 }
198
199 if (mComputedFields.mClipPath == NULL) {
200 mComputedFields.mClipPath = new SkPath();
201 }
202 SkPath* clipPath = mComputedFields.mClipPath;
203 mComputedFields.mClipPathOp = SkRegion::kIntersect_Op;
204
205 if (outlineClipPath && revealClipPath) {
206 SkPathOp op = kIntersect_PathOp;
207 if (mPrimitiveFields.mRevealClip.isInverseClip()) {
208 op = kDifference_PathOp; // apply difference step in the Op below, instead of draw time
209 }
210
211 Op(*outlineClipPath, *revealClipPath, op, clipPath);
212 } else if (outlineClipPath) {
213 *clipPath = *outlineClipPath;
214 } else {
215 *clipPath = *revealClipPath;
216 if (mPrimitiveFields.mRevealClip.isInverseClip()) {
217 // apply difference step at draw time
218 mComputedFields.mClipPathOp = SkRegion::kDifference_Op;
219 }
220 }
221}
222
John Reckacb6f072014-03-12 16:11:23 -0700223} /* namespace uirenderer */
224} /* namespace android */