blob: 9f1ceed5090f38fd3797d29d18cba282bd42050f [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
Chris Craik182952f2015-03-09 14:17:29 -070036LayerProperties::LayerProperties() {
John Reck25fbb3f2014-06-12 13:46:45 -070037 reset();
38}
39
40LayerProperties::~LayerProperties() {
Chris Craik182952f2015-03-09 14:17:29 -070041 setType(LayerType::None);
John Reck25fbb3f2014-06-12 13:46:45 -070042}
43
44void LayerProperties::reset() {
45 mOpaque = false;
Chris Craikd41c4d82015-01-05 15:51:13 -080046 setFromPaint(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070047}
48
49bool LayerProperties::setColorFilter(SkColorFilter* filter) {
50 if (mColorFilter == filter) return false;
51 SkRefCnt_SafeAssign(mColorFilter, filter);
52 return true;
53}
54
55bool LayerProperties::setFromPaint(const SkPaint* paint) {
56 bool changed = false;
57 SkXfermode::Mode mode;
58 int alpha;
59 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
60 changed |= setAlpha(static_cast<uint8_t>(alpha));
61 changed |= setXferMode(mode);
Chris Craikd41c4d82015-01-05 15:51:13 -080062 changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070063 return changed;
64}
65
66LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
67 setType(other.type());
68 setOpaque(other.opaque());
69 setAlpha(other.alpha());
70 setXferMode(other.xferMode());
71 setColorFilter(other.colorFilter());
72 return *this;
73}
74
John Reckd0a0b2a2014-03-20 16:28:56 -070075RenderProperties::PrimitiveFields::PrimitiveFields()
Chris Craika753f4c2014-07-24 12:39:17 -070076 : mClippingFlags(CLIP_TO_BOUNDS)
John Reckacb6f072014-03-12 16:11:23 -070077 , mProjectBackwards(false)
78 , mProjectionReceiver(false)
John Reckacb6f072014-03-12 16:11:23 -070079 , mAlpha(1)
80 , mHasOverlappingRendering(true)
Chris Craikcc39e162014-04-25 18:34:11 -070081 , mElevation(0)
John Reckacb6f072014-03-12 16:11:23 -070082 , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
83 , mRotation(0), mRotationX(0), mRotationY(0)
84 , mScaleX(1), mScaleY(1)
85 , mPivotX(0), mPivotY(0)
John Reckacb6f072014-03-12 16:11:23 -070086 , mLeft(0), mTop(0), mRight(0), mBottom(0)
87 , mWidth(0), mHeight(0)
John Reckacb6f072014-03-12 16:11:23 -070088 , mPivotExplicitlySet(false)
John Reck25fbb3f2014-06-12 13:46:45 -070089 , mMatrixOrPivotDirty(false) {
John Reckacb6f072014-03-12 16:11:23 -070090}
91
John Reckd0a0b2a2014-03-20 16:28:56 -070092RenderProperties::ComputedFields::ComputedFields()
Chris Craikd41c4d82015-01-05 15:51:13 -080093 : mTransformMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070094}
95
96RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070097 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -070098}
99
100RenderProperties::RenderProperties()
Chris Craikd41c4d82015-01-05 15:51:13 -0800101 : mStaticMatrix(nullptr)
102 , mAnimationMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700103}
104
105RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -0700106 delete mStaticMatrix;
107 delete mAnimationMatrix;
108}
109
John Reckd0a0b2a2014-03-20 16:28:56 -0700110RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
111 if (this != &other) {
112 mPrimitiveFields = other.mPrimitiveFields;
113 setStaticMatrix(other.getStaticMatrix());
114 setAnimationMatrix(other.getAnimationMatrix());
115 setCameraDistance(other.getCameraDistance());
John Reck25fbb3f2014-06-12 13:46:45 -0700116 mLayerProperties = other.layerProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -0700117
Chris Craik49e6c732014-03-31 12:34:11 -0700118 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -0700119 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c732014-03-31 12:34:11 -0700120 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700121 }
122 return *this;
John Reckacb6f072014-03-12 16:11:23 -0700123}
124
John Reckd0a0b2a2014-03-20 16:28:56 -0700125void RenderProperties::debugOutputProperties(const int level) const {
126 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
127 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
128 }
129 if (mStaticMatrix) {
130 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
131 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
132 }
133 if (mAnimationMatrix) {
134 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
135 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
136 }
John Reckf7483e32014-04-11 08:54:47 -0700137 if (hasTransformMatrix()) {
138 if (isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700139 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
Chris Craikcc39e162014-04-25 18:34:11 -0700140 level * 2, "", getTranslationX(), getTranslationY(), getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700141 } else {
Chris Craik49e6c732014-03-31 12:34:11 -0700142 ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
143 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700144 }
145 }
146
Chris Craik182952f2015-03-09 14:17:29 -0700147 const bool isLayer = layerProperties().type() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700148 int clipFlags = getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700149 if (mPrimitiveFields.mAlpha < 1) {
Chris Craika753f4c2014-07-24 12:39:17 -0700150 if (isLayer) {
151 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
152
John Reckd0a0b2a2014-03-20 16:28:56 -0700153 ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
154 } else if (!mPrimitiveFields.mHasOverlappingRendering) {
155 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
156 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700157 Rect layerBounds(0, 0, getWidth(), getHeight());
158 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
159 if (clipFlags) {
160 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
161 getClippingRectForFlags(clipFlags, &layerBounds);
162 clipFlags = 0; // all clipping done by saveLayer
John Reckd0a0b2a2014-03-20 16:28:56 -0700163 }
Chris Craika753f4c2014-07-24 12:39:17 -0700164
John Reck78ce1c52014-03-24 15:43:49 -0700165 ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
Chris Craika753f4c2014-07-24 12:39:17 -0700166 (int)layerBounds.left, (int)layerBounds.top, (int)layerBounds.right, (int)layerBounds.bottom,
167 (int)(mPrimitiveFields.mAlpha * 255), saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700168 }
169 }
Chris Craika753f4c2014-07-24 12:39:17 -0700170 if (clipFlags) {
171 Rect clipRect;
172 getClippingRectForFlags(clipFlags, &clipRect);
John Reck78ce1c52014-03-24 15:43:49 -0700173 ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
Chris Craika753f4c2014-07-24 12:39:17 -0700174 (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
John Reckd0a0b2a2014-03-20 16:28:56 -0700175 }
John Reckacb6f072014-03-12 16:11:23 -0700176}
177
178void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700179 if (mPrimitiveFields.mMatrixOrPivotDirty) {
180 if (!mComputedFields.mTransformMatrix) {
181 // only allocate a mPrimitiveFields.matrix if we have a complex transform
182 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700183 }
John Reckf7483e32014-04-11 08:54:47 -0700184 if (!mPrimitiveFields.mPivotExplicitlySet) {
185 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
186 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
187 }
188 SkMatrix* transform = mComputedFields.mTransformMatrix;
189 transform->reset();
Chris Craike0bb87d2014-04-22 17:55:41 -0700190 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700191 transform->setTranslate(getTranslationX(), getTranslationY());
192 transform->preRotate(getRotation(), getPivotX(), getPivotY());
193 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
194 } else {
195 SkMatrix transform3D;
196 mComputedFields.mTransformCamera.save();
197 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
198 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
199 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
200 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
201 mComputedFields.mTransformCamera.getMatrix(&transform3D);
202 transform3D.preTranslate(-getPivotX(), -getPivotY());
203 transform3D.postTranslate(getPivotX() + getTranslationX(),
204 getPivotY() + getTranslationY());
205 transform->postConcat(transform3D);
206 mComputedFields.mTransformCamera.restore();
207 }
208 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700209 }
210}
211
John Reckacb6f072014-03-12 16:11:23 -0700212} /* namespace uirenderer */
213} /* namespace android */