blob: a9ae9b5322be5e498109b84b615af54ae3a632b3 [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
John Reckacb6f072014-03-12 16:11:23 -070017#include "RenderProperties.h"
18
John Reckd0a0b2a2014-03-20 16:28:56 -070019#include <utils/Trace.h>
20
21#include <SkCanvas.h>
John Reck25fbb3f2014-06-12 13:46:45 -070022#include <SkColorFilter.h>
John Reckacb6f072014-03-12 16:11:23 -070023#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070024#include <SkPath.h>
25#include <SkPathOps.h>
John Reckacb6f072014-03-12 16:11:23 -070026
27#include "Matrix.h"
John Reck25fbb3f2014-06-12 13:46:45 -070028#include "OpenGLRenderer.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
Chris Craik182952f2015-03-09 14:17:29 -070034LayerProperties::LayerProperties() {
John Reck25fbb3f2014-06-12 13:46:45 -070035 reset();
36}
37
38LayerProperties::~LayerProperties() {
Chris Craik182952f2015-03-09 14:17:29 -070039 setType(LayerType::None);
John Reck25fbb3f2014-06-12 13:46:45 -070040}
41
42void LayerProperties::reset() {
43 mOpaque = false;
Chris Craikd41c4d82015-01-05 15:51:13 -080044 setFromPaint(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070045}
46
47bool LayerProperties::setColorFilter(SkColorFilter* filter) {
48 if (mColorFilter == filter) return false;
49 SkRefCnt_SafeAssign(mColorFilter, filter);
50 return true;
51}
52
53bool LayerProperties::setFromPaint(const SkPaint* paint) {
54 bool changed = false;
55 SkXfermode::Mode mode;
56 int alpha;
57 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
58 changed |= setAlpha(static_cast<uint8_t>(alpha));
59 changed |= setXferMode(mode);
Chris Craikd41c4d82015-01-05 15:51:13 -080060 changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070061 return changed;
62}
63
64LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
65 setType(other.type());
66 setOpaque(other.opaque());
67 setAlpha(other.alpha());
68 setXferMode(other.xferMode());
69 setColorFilter(other.colorFilter());
70 return *this;
71}
72
John Reckd0a0b2a2014-03-20 16:28:56 -070073RenderProperties::PrimitiveFields::PrimitiveFields()
Chris Craika753f4c2014-07-24 12:39:17 -070074 : mClippingFlags(CLIP_TO_BOUNDS)
John Reckacb6f072014-03-12 16:11:23 -070075 , mProjectBackwards(false)
76 , mProjectionReceiver(false)
John Reckacb6f072014-03-12 16:11:23 -070077 , mAlpha(1)
78 , mHasOverlappingRendering(true)
Chris Craikcc39e162014-04-25 18:34:11 -070079 , mElevation(0)
John Reckacb6f072014-03-12 16:11:23 -070080 , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
81 , mRotation(0), mRotationX(0), mRotationY(0)
82 , mScaleX(1), mScaleY(1)
83 , mPivotX(0), mPivotY(0)
John Reckacb6f072014-03-12 16:11:23 -070084 , mLeft(0), mTop(0), mRight(0), mBottom(0)
85 , mWidth(0), mHeight(0)
John Reckacb6f072014-03-12 16:11:23 -070086 , mPivotExplicitlySet(false)
John Reck25fbb3f2014-06-12 13:46:45 -070087 , mMatrixOrPivotDirty(false) {
John Reckacb6f072014-03-12 16:11:23 -070088}
89
John Reckd0a0b2a2014-03-20 16:28:56 -070090RenderProperties::ComputedFields::ComputedFields()
Chris Craikd41c4d82015-01-05 15:51:13 -080091 : mTransformMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070092}
93
94RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070095 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -070096}
97
98RenderProperties::RenderProperties()
Chris Craikd41c4d82015-01-05 15:51:13 -080099 : mStaticMatrix(nullptr)
100 , mAnimationMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700101}
102
103RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -0700104 delete mStaticMatrix;
105 delete mAnimationMatrix;
106}
107
John Reckd0a0b2a2014-03-20 16:28:56 -0700108RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
109 if (this != &other) {
110 mPrimitiveFields = other.mPrimitiveFields;
111 setStaticMatrix(other.getStaticMatrix());
112 setAnimationMatrix(other.getAnimationMatrix());
113 setCameraDistance(other.getCameraDistance());
John Reck25fbb3f2014-06-12 13:46:45 -0700114 mLayerProperties = other.layerProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -0700115
Chris Craik49e6c7392014-03-31 12:34:11 -0700116 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -0700117 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -0700118 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700119 }
120 return *this;
John Reckacb6f072014-03-12 16:11:23 -0700121}
122
John Reckd0a0b2a2014-03-20 16:28:56 -0700123void RenderProperties::debugOutputProperties(const int level) const {
124 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
125 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
126 }
127 if (mStaticMatrix) {
128 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
129 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
130 }
131 if (mAnimationMatrix) {
132 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
133 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
134 }
John Reckf7483e32014-04-11 08:54:47 -0700135 if (hasTransformMatrix()) {
136 if (isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700137 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
Chris Craikcc39e162014-04-25 18:34:11 -0700138 level * 2, "", getTranslationX(), getTranslationY(), getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700139 } else {
Chris Craik49e6c7392014-03-31 12:34:11 -0700140 ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
141 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700142 }
143 }
144
Chris Craik856f0cc2015-04-21 15:13:29 -0700145 const bool isLayer = effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700146 int clipFlags = getClippingFlags();
Chris Craik43a1d312015-05-27 11:28:14 -0700147 if (mPrimitiveFields.mAlpha < 1
148 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
Chris Craika753f4c2014-07-24 12:39:17 -0700149 if (isLayer) {
150 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reckd0a0b2a2014-03-20 16:28:56 -0700151 }
Chris Craik8df5ffa2015-04-28 17:47:20 -0700152
Chris Craik4e9d9b22015-06-12 11:07:23 -0700153 if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
154 // simply scale rendering content's alpha
155 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
156 } else {
157 // savelayeralpha to create an offscreen buffer to apply alpha
158 Rect layerBounds(0, 0, getWidth(), getHeight());
159 if (clipFlags) {
160 getClippingRectForFlags(clipFlags, &layerBounds);
161 clipFlags = 0; // all clipping done by savelayer
162 }
163 ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
164 (int)layerBounds.left, (int)layerBounds.top,
165 (int)layerBounds.right, (int)layerBounds.bottom,
166 (int)(mPrimitiveFields.mAlpha * 255),
167 SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
168 }
169
170
John Reckd0a0b2a2014-03-20 16:28:56 -0700171 }
Chris Craika753f4c2014-07-24 12:39:17 -0700172 if (clipFlags) {
173 Rect clipRect;
174 getClippingRectForFlags(clipFlags, &clipRect);
John Reck78ce1c52014-03-24 15:43:49 -0700175 ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
Chris Craika753f4c2014-07-24 12:39:17 -0700176 (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
John Reckd0a0b2a2014-03-20 16:28:56 -0700177 }
John Reckacb6f072014-03-12 16:11:23 -0700178}
179
180void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700181 if (mPrimitiveFields.mMatrixOrPivotDirty) {
182 if (!mComputedFields.mTransformMatrix) {
183 // only allocate a mPrimitiveFields.matrix if we have a complex transform
184 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700185 }
John Reckf7483e32014-04-11 08:54:47 -0700186 if (!mPrimitiveFields.mPivotExplicitlySet) {
187 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
188 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
189 }
190 SkMatrix* transform = mComputedFields.mTransformMatrix;
191 transform->reset();
Chris Craike0bb87d2014-04-22 17:55:41 -0700192 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700193 transform->setTranslate(getTranslationX(), getTranslationY());
194 transform->preRotate(getRotation(), getPivotX(), getPivotY());
195 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
196 } else {
197 SkMatrix transform3D;
198 mComputedFields.mTransformCamera.save();
199 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
200 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
201 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
202 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
203 mComputedFields.mTransformCamera.getMatrix(&transform3D);
204 transform3D.preTranslate(-getPivotX(), -getPivotY());
205 transform3D.postTranslate(getPivotX() + getTranslationX(),
206 getPivotY() + getTranslationY());
207 transform->postConcat(transform3D);
208 mComputedFields.mTransformCamera.restore();
209 }
210 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700211 }
212}
213
John Reckacb6f072014-03-12 16:11:23 -0700214} /* namespace uirenderer */
215} /* namespace android */