blob: 58ec32d8a294cfa197688d21eca2bf4767a31bf2 [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
John Reckf7483e32014-04-11 08:54:47 -070030/**
31 * Convenience value to check for float values that are close enough to zero to be considered
32 * zero.
33 */
34#define NONZERO_EPSILON .001f
35
36static inline bool is_zero(float value) {
Chris Craik222f3312014-04-22 10:08:27 -070037 return (value >= -NONZERO_EPSILON) && (value <= NONZERO_EPSILON);
John Reckf7483e32014-04-11 08:54:47 -070038}
39
John Reckacb6f072014-03-12 16:11:23 -070040namespace android {
41namespace uirenderer {
42
John Reckd0a0b2a2014-03-20 16:28:56 -070043RenderProperties::PrimitiveFields::PrimitiveFields()
John Reckacb6f072014-03-12 16:11:23 -070044 : mClipToBounds(true)
45 , mProjectBackwards(false)
46 , mProjectionReceiver(false)
John Reckacb6f072014-03-12 16:11:23 -070047 , mAlpha(1)
48 , mHasOverlappingRendering(true)
49 , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
50 , mRotation(0), mRotationX(0), mRotationY(0)
51 , mScaleX(1), mScaleY(1)
52 , mPivotX(0), mPivotY(0)
John Reckacb6f072014-03-12 16:11:23 -070053 , mLeft(0), mTop(0), mRight(0), mBottom(0)
54 , mWidth(0), mHeight(0)
John Reckacb6f072014-03-12 16:11:23 -070055 , mPivotExplicitlySet(false)
John Reckf7483e32014-04-11 08:54:47 -070056 , mMatrixOrPivotDirty(false)
John Reckacb6f072014-03-12 16:11:23 -070057 , mCaching(false) {
John Reckacb6f072014-03-12 16:11:23 -070058}
59
John Reckd0a0b2a2014-03-20 16:28:56 -070060RenderProperties::ComputedFields::ComputedFields()
61 : mTransformMatrix(NULL)
Chris Craik8c271ca2014-03-25 10:33:01 -070062 , mClipPath(NULL) {
John Reckd0a0b2a2014-03-20 16:28:56 -070063}
64
65RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070066 delete mTransformMatrix;
Chris Craik8c271ca2014-03-25 10:33:01 -070067 delete mClipPath;
John Reckd0a0b2a2014-03-20 16:28:56 -070068}
69
70RenderProperties::RenderProperties()
Chris Craik49e6c7392014-03-31 12:34:11 -070071 : mStaticMatrix(NULL)
John Reckd0a0b2a2014-03-20 16:28:56 -070072 , mAnimationMatrix(NULL) {
73}
74
75RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070076 delete mStaticMatrix;
77 delete mAnimationMatrix;
78}
79
John Reckd0a0b2a2014-03-20 16:28:56 -070080RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
81 if (this != &other) {
82 mPrimitiveFields = other.mPrimitiveFields;
83 setStaticMatrix(other.getStaticMatrix());
84 setAnimationMatrix(other.getAnimationMatrix());
85 setCameraDistance(other.getCameraDistance());
86
Chris Craik49e6c7392014-03-31 12:34:11 -070087 // Update the computed clip path
Chris Craik8c271ca2014-03-25 10:33:01 -070088 updateClipPath();
Chris Craik49e6c7392014-03-31 12:34:11 -070089
90 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -070091 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -070092 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -070093 }
94 return *this;
John Reckacb6f072014-03-12 16:11:23 -070095}
96
John Reckd0a0b2a2014-03-20 16:28:56 -070097void RenderProperties::debugOutputProperties(const int level) const {
98 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
99 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
100 }
101 if (mStaticMatrix) {
102 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
103 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
104 }
105 if (mAnimationMatrix) {
106 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
107 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
108 }
John Reckf7483e32014-04-11 08:54:47 -0700109 if (hasTransformMatrix()) {
110 if (isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700111 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
112 level * 2, "", mPrimitiveFields.mTranslationX, mPrimitiveFields.mTranslationY, mPrimitiveFields.mTranslationZ);
113 } else {
Chris Craik49e6c7392014-03-31 12:34:11 -0700114 ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
115 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700116 }
117 }
118
119 bool clipToBoundsNeeded = mPrimitiveFields.mCaching ? false : mPrimitiveFields.mClipToBounds;
120 if (mPrimitiveFields.mAlpha < 1) {
121 if (mPrimitiveFields.mCaching) {
122 ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
123 } else if (!mPrimitiveFields.mHasOverlappingRendering) {
124 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
125 } else {
126 int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
127 if (clipToBoundsNeeded) {
128 flags |= SkCanvas::kClipToLayer_SaveFlag;
129 clipToBoundsNeeded = false; // clipping done by save layer
130 }
John Reck78ce1c52014-03-24 15:43:49 -0700131 ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
132 0, 0, getWidth(), getHeight(),
John Reckd0a0b2a2014-03-20 16:28:56 -0700133 (int)(mPrimitiveFields.mAlpha * 255), flags);
134 }
135 }
136 if (clipToBoundsNeeded) {
John Reck78ce1c52014-03-24 15:43:49 -0700137 ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
138 0, 0, getWidth(), getHeight());
John Reckd0a0b2a2014-03-20 16:28:56 -0700139 }
John Reckacb6f072014-03-12 16:11:23 -0700140}
141
142void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700143 if (mPrimitiveFields.mMatrixOrPivotDirty) {
144 if (!mComputedFields.mTransformMatrix) {
145 // only allocate a mPrimitiveFields.matrix if we have a complex transform
146 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700147 }
John Reckf7483e32014-04-11 08:54:47 -0700148 if (!mPrimitiveFields.mPivotExplicitlySet) {
149 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
150 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
151 }
152 SkMatrix* transform = mComputedFields.mTransformMatrix;
153 transform->reset();
154 if (is_zero(getRotationX()) && is_zero(getRotationY())) {
155 transform->setTranslate(getTranslationX(), getTranslationY());
156 transform->preRotate(getRotation(), getPivotX(), getPivotY());
157 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
158 } else {
159 SkMatrix transform3D;
160 mComputedFields.mTransformCamera.save();
161 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
162 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
163 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
164 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
165 mComputedFields.mTransformCamera.getMatrix(&transform3D);
166 transform3D.preTranslate(-getPivotX(), -getPivotY());
167 transform3D.postTranslate(getPivotX() + getTranslationX(),
168 getPivotY() + getTranslationY());
169 transform->postConcat(transform3D);
170 mComputedFields.mTransformCamera.restore();
171 }
172 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700173 }
174}
175
Chris Craik8c271ca2014-03-25 10:33:01 -0700176void RenderProperties::updateClipPath() {
177 const SkPath* outlineClipPath = mPrimitiveFields.mOutline.willClip()
178 ? mPrimitiveFields.mOutline.getPath() : NULL;
179 const SkPath* revealClipPath = mPrimitiveFields.mRevealClip.getPath();
180
181 if (!outlineClipPath && !revealClipPath) {
182 // mComputedFields.mClipPath doesn't need to be updated, since it won't be used
183 return;
184 }
185
186 if (mComputedFields.mClipPath == NULL) {
187 mComputedFields.mClipPath = new SkPath();
188 }
189 SkPath* clipPath = mComputedFields.mClipPath;
190 mComputedFields.mClipPathOp = SkRegion::kIntersect_Op;
191
192 if (outlineClipPath && revealClipPath) {
193 SkPathOp op = kIntersect_PathOp;
194 if (mPrimitiveFields.mRevealClip.isInverseClip()) {
195 op = kDifference_PathOp; // apply difference step in the Op below, instead of draw time
196 }
197
198 Op(*outlineClipPath, *revealClipPath, op, clipPath);
199 } else if (outlineClipPath) {
200 *clipPath = *outlineClipPath;
201 } else {
202 *clipPath = *revealClipPath;
203 if (mPrimitiveFields.mRevealClip.isInverseClip()) {
204 // apply difference step at draw time
205 mComputedFields.mClipPathOp = SkRegion::kDifference_Op;
206 }
207 }
208}
209
John Reckacb6f072014-03-12 16:11:23 -0700210} /* namespace uirenderer */
211} /* namespace android */