blob: 8848b2f76da7fc510dd6de18053ea92078d70eef [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 Reck25fbb3f2014-06-12 13:46:45 -070024#include <SkColorFilter.h>
John Reckacb6f072014-03-12 16:11:23 -070025#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070026#include <SkPath.h>
27#include <SkPathOps.h>
John Reckacb6f072014-03-12 16:11:23 -070028
29#include "Matrix.h"
John Reck25fbb3f2014-06-12 13:46:45 -070030#include "OpenGLRenderer.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070031#include "utils/MathUtils.h"
John Reckf7483e32014-04-11 08:54:47 -070032
John Reckacb6f072014-03-12 16:11:23 -070033namespace android {
34namespace uirenderer {
35
John Reck25fbb3f2014-06-12 13:46:45 -070036LayerProperties::LayerProperties()
37 : mType(kLayerTypeNone)
38 , mColorFilter(NULL) {
39 reset();
40}
41
42LayerProperties::~LayerProperties() {
43 setType(kLayerTypeNone);
44}
45
46void LayerProperties::reset() {
47 mOpaque = false;
48 setFromPaint(NULL);
49}
50
51bool LayerProperties::setColorFilter(SkColorFilter* filter) {
52 if (mColorFilter == filter) return false;
53 SkRefCnt_SafeAssign(mColorFilter, filter);
54 return true;
55}
56
57bool LayerProperties::setFromPaint(const SkPaint* paint) {
58 bool changed = false;
59 SkXfermode::Mode mode;
60 int alpha;
61 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
62 changed |= setAlpha(static_cast<uint8_t>(alpha));
63 changed |= setXferMode(mode);
64 changed |= setColorFilter(paint ? paint->getColorFilter() : NULL);
65 return changed;
66}
67
68LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
69 setType(other.type());
70 setOpaque(other.opaque());
71 setAlpha(other.alpha());
72 setXferMode(other.xferMode());
73 setColorFilter(other.colorFilter());
74 return *this;
75}
76
John Reckd0a0b2a2014-03-20 16:28:56 -070077RenderProperties::PrimitiveFields::PrimitiveFields()
John Reckacb6f072014-03-12 16:11:23 -070078 : mClipToBounds(true)
79 , mProjectBackwards(false)
80 , mProjectionReceiver(false)
John Reckacb6f072014-03-12 16:11:23 -070081 , mAlpha(1)
82 , mHasOverlappingRendering(true)
Chris Craikcc39e162014-04-25 18:34:11 -070083 , mElevation(0)
John Reckacb6f072014-03-12 16:11:23 -070084 , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
85 , mRotation(0), mRotationX(0), mRotationY(0)
86 , mScaleX(1), mScaleY(1)
87 , mPivotX(0), mPivotY(0)
John Reckacb6f072014-03-12 16:11:23 -070088 , mLeft(0), mTop(0), mRight(0), mBottom(0)
89 , mWidth(0), mHeight(0)
John Reckacb6f072014-03-12 16:11:23 -070090 , mPivotExplicitlySet(false)
John Reck25fbb3f2014-06-12 13:46:45 -070091 , mMatrixOrPivotDirty(false) {
John Reckacb6f072014-03-12 16:11:23 -070092}
93
John Reckd0a0b2a2014-03-20 16:28:56 -070094RenderProperties::ComputedFields::ComputedFields()
Chris Craik2bcad172014-05-14 18:11:23 -070095 : mTransformMatrix(NULL) {
John Reckd0a0b2a2014-03-20 16:28:56 -070096}
97
98RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070099 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -0700100}
101
102RenderProperties::RenderProperties()
Chris Craik49e6c732014-03-31 12:34:11 -0700103 : mStaticMatrix(NULL)
John Reckd0a0b2a2014-03-20 16:28:56 -0700104 , mAnimationMatrix(NULL) {
105}
106
107RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -0700108 delete mStaticMatrix;
109 delete mAnimationMatrix;
110}
111
John Reckd0a0b2a2014-03-20 16:28:56 -0700112RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
113 if (this != &other) {
114 mPrimitiveFields = other.mPrimitiveFields;
115 setStaticMatrix(other.getStaticMatrix());
116 setAnimationMatrix(other.getAnimationMatrix());
117 setCameraDistance(other.getCameraDistance());
John Reck25fbb3f2014-06-12 13:46:45 -0700118 mLayerProperties = other.layerProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -0700119
Chris Craik49e6c732014-03-31 12:34:11 -0700120 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -0700121 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c732014-03-31 12:34:11 -0700122 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700123 }
124 return *this;
John Reckacb6f072014-03-12 16:11:23 -0700125}
126
John Reckd0a0b2a2014-03-20 16:28:56 -0700127void RenderProperties::debugOutputProperties(const int level) const {
128 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
129 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
130 }
131 if (mStaticMatrix) {
132 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
133 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
134 }
135 if (mAnimationMatrix) {
136 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
137 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
138 }
John Reckf7483e32014-04-11 08:54:47 -0700139 if (hasTransformMatrix()) {
140 if (isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700141 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
Chris Craikcc39e162014-04-25 18:34:11 -0700142 level * 2, "", getTranslationX(), getTranslationY(), getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700143 } else {
Chris Craik49e6c732014-03-31 12:34:11 -0700144 ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
145 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700146 }
147 }
148
John Reck25fbb3f2014-06-12 13:46:45 -0700149 bool clipToBoundsNeeded = layerProperties().type() != kLayerTypeNone ? false : mPrimitiveFields.mClipToBounds;
John Reckd0a0b2a2014-03-20 16:28:56 -0700150 if (mPrimitiveFields.mAlpha < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700151 if (layerProperties().type() != kLayerTypeNone) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700152 ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
153 } else if (!mPrimitiveFields.mHasOverlappingRendering) {
154 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
155 } else {
156 int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
157 if (clipToBoundsNeeded) {
158 flags |= SkCanvas::kClipToLayer_SaveFlag;
159 clipToBoundsNeeded = false; // clipping done by save layer
160 }
John Reck78ce1c52014-03-24 15:43:49 -0700161 ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
162 0, 0, getWidth(), getHeight(),
John Reckd0a0b2a2014-03-20 16:28:56 -0700163 (int)(mPrimitiveFields.mAlpha * 255), flags);
164 }
165 }
166 if (clipToBoundsNeeded) {
John Reck78ce1c52014-03-24 15:43:49 -0700167 ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
168 0, 0, getWidth(), getHeight());
John Reckd0a0b2a2014-03-20 16:28:56 -0700169 }
John Reckacb6f072014-03-12 16:11:23 -0700170}
171
172void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700173 if (mPrimitiveFields.mMatrixOrPivotDirty) {
174 if (!mComputedFields.mTransformMatrix) {
175 // only allocate a mPrimitiveFields.matrix if we have a complex transform
176 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700177 }
John Reckf7483e32014-04-11 08:54:47 -0700178 if (!mPrimitiveFields.mPivotExplicitlySet) {
179 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
180 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
181 }
182 SkMatrix* transform = mComputedFields.mTransformMatrix;
183 transform->reset();
Chris Craike0bb87d2014-04-22 17:55:41 -0700184 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700185 transform->setTranslate(getTranslationX(), getTranslationY());
186 transform->preRotate(getRotation(), getPivotX(), getPivotY());
187 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
188 } else {
189 SkMatrix transform3D;
190 mComputedFields.mTransformCamera.save();
191 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
192 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
193 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
194 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
195 mComputedFields.mTransformCamera.getMatrix(&transform3D);
196 transform3D.preTranslate(-getPivotX(), -getPivotY());
197 transform3D.postTranslate(getPivotX() + getTranslationX(),
198 getPivotY() + getTranslationY());
199 transform->postConcat(transform3D);
200 mComputedFields.mTransformCamera.restore();
201 }
202 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700203 }
204}
205
John Reckacb6f072014-03-12 16:11:23 -0700206} /* namespace uirenderer */
207} /* namespace android */