blob: f577785110d3578b500b911005396320a220a46e [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
John Reck25fbb3f2014-06-12 13:46:45 -070021#include <SkColorFilter.h>
John Reckacb6f072014-03-12 16:11:23 -070022#include <SkMatrix.h>
Chris Craik8c271ca2014-03-25 10:33:01 -070023#include <SkPath.h>
24#include <SkPathOps.h>
John Reckacb6f072014-03-12 16:11:23 -070025
26#include "Matrix.h"
John Reck25fbb3f2014-06-12 13:46:45 -070027#include "OpenGLRenderer.h"
sergeyvdccca442016-03-21 15:38:21 -070028#include "hwui/Canvas.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;
Chris Craikbf6f0f22015-10-01 12:36:07 -070055 changed |= setAlpha(static_cast<uint8_t>(PaintUtils::getAlphaDirect(paint)));
56 changed |= setXferMode(PaintUtils::getXfermodeDirect(paint));
Chris Craikd41c4d82015-01-05 15:51:13 -080057 changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070058 return changed;
59}
60
61LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
62 setType(other.type());
63 setOpaque(other.opaque());
64 setAlpha(other.alpha());
65 setXferMode(other.xferMode());
66 setColorFilter(other.colorFilter());
67 return *this;
68}
69
John Reckd0a0b2a2014-03-20 16:28:56 -070070RenderProperties::ComputedFields::ComputedFields()
Chris Craikd41c4d82015-01-05 15:51:13 -080071 : mTransformMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070072}
73
74RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070075 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -070076}
77
78RenderProperties::RenderProperties()
Chris Craikd41c4d82015-01-05 15:51:13 -080079 : mStaticMatrix(nullptr)
80 , mAnimationMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070081}
82
83RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070084 delete mStaticMatrix;
85 delete mAnimationMatrix;
86}
87
John Reckd0a0b2a2014-03-20 16:28:56 -070088RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
89 if (this != &other) {
90 mPrimitiveFields = other.mPrimitiveFields;
91 setStaticMatrix(other.getStaticMatrix());
92 setAnimationMatrix(other.getAnimationMatrix());
93 setCameraDistance(other.getCameraDistance());
John Reck25fbb3f2014-06-12 13:46:45 -070094 mLayerProperties = other.layerProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -070095
Chris Craik49e6c732014-03-31 12:34:11 -070096 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -070097 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c732014-03-31 12:34:11 -070098 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -070099 }
100 return *this;
John Reckacb6f072014-03-12 16:11:23 -0700101}
102
John Reckd0a0b2a2014-03-20 16:28:56 -0700103void RenderProperties::debugOutputProperties(const int level) const {
104 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
Chris Craik91eff222016-02-22 13:39:33 -0800105 ALOGD("%*s(Translate (left, top) %d, %d)", level * 2, "",
106 mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
John Reckd0a0b2a2014-03-20 16:28:56 -0700107 }
108 if (mStaticMatrix) {
Chris Craik91eff222016-02-22 13:39:33 -0800109 ALOGD("%*s(ConcatMatrix (static) %p: " SK_MATRIX_STRING ")",
John Reckd0a0b2a2014-03-20 16:28:56 -0700110 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
111 }
112 if (mAnimationMatrix) {
Chris Craik91eff222016-02-22 13:39:33 -0800113 ALOGD("%*s(ConcatMatrix (animation) %p: " SK_MATRIX_STRING ")",
John Reckd0a0b2a2014-03-20 16:28:56 -0700114 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
115 }
John Reckf7483e32014-04-11 08:54:47 -0700116 if (hasTransformMatrix()) {
117 if (isTransformTranslateOnly()) {
Chris Craik91eff222016-02-22 13:39:33 -0800118 ALOGD("%*s(Translate %.2f, %.2f, %.2f)",
Chris Craikcc39e162014-04-25 18:34:11 -0700119 level * 2, "", getTranslationX(), getTranslationY(), getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700120 } else {
Chris Craik91eff222016-02-22 13:39:33 -0800121 ALOGD("%*s(ConcatMatrix %p: " SK_MATRIX_STRING ")",
Chris Craik49e6c732014-03-31 12:34:11 -0700122 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700123 }
124 }
125
Chris Craik856f0cc2015-04-21 15:13:29 -0700126 const bool isLayer = effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700127 int clipFlags = getClippingFlags();
Chris Craik43a1d312015-05-27 11:28:14 -0700128 if (mPrimitiveFields.mAlpha < 1
129 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
Chris Craika753f4c2014-07-24 12:39:17 -0700130 if (isLayer) {
131 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reckd0a0b2a2014-03-20 16:28:56 -0700132 }
Chris Craik8df5ffa2015-04-28 17:47:20 -0700133
Chris Craik4e9d9b22015-06-12 11:07:23 -0700134 if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
135 // simply scale rendering content's alpha
Chris Craik91eff222016-02-22 13:39:33 -0800136 ALOGD("%*s(ScaleAlpha %.2f)", level * 2, "", mPrimitiveFields.mAlpha);
Chris Craik4e9d9b22015-06-12 11:07:23 -0700137 } else {
138 // savelayeralpha to create an offscreen buffer to apply alpha
139 Rect layerBounds(0, 0, getWidth(), getHeight());
140 if (clipFlags) {
141 getClippingRectForFlags(clipFlags, &layerBounds);
142 clipFlags = 0; // all clipping done by savelayer
143 }
Chris Craik91eff222016-02-22 13:39:33 -0800144 ALOGD("%*s(SaveLayerAlpha %d, %d, %d, %d, %d, 0x%x)", level * 2, "",
Chris Craik4e9d9b22015-06-12 11:07:23 -0700145 (int)layerBounds.left, (int)layerBounds.top,
146 (int)layerBounds.right, (int)layerBounds.bottom,
147 (int)(mPrimitiveFields.mAlpha * 255),
Florin Malitaeecff562015-12-21 10:43:01 -0500148 SaveFlags::HasAlphaLayer | SaveFlags::ClipToLayer);
Chris Craik4e9d9b22015-06-12 11:07:23 -0700149 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700150 }
Chris Craik91eff222016-02-22 13:39:33 -0800151
Chris Craika753f4c2014-07-24 12:39:17 -0700152 if (clipFlags) {
153 Rect clipRect;
154 getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik91eff222016-02-22 13:39:33 -0800155 ALOGD("%*s(ClipRect %d, %d, %d, %d)", level * 2, "",
Chris Craika753f4c2014-07-24 12:39:17 -0700156 (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
John Reckd0a0b2a2014-03-20 16:28:56 -0700157 }
John Reckacb6f072014-03-12 16:11:23 -0700158}
159
160void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700161 if (mPrimitiveFields.mMatrixOrPivotDirty) {
162 if (!mComputedFields.mTransformMatrix) {
163 // only allocate a mPrimitiveFields.matrix if we have a complex transform
164 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700165 }
John Reckf7483e32014-04-11 08:54:47 -0700166 if (!mPrimitiveFields.mPivotExplicitlySet) {
167 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
168 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
169 }
170 SkMatrix* transform = mComputedFields.mTransformMatrix;
171 transform->reset();
Chris Craike0bb87d2014-04-22 17:55:41 -0700172 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700173 transform->setTranslate(getTranslationX(), getTranslationY());
174 transform->preRotate(getRotation(), getPivotX(), getPivotY());
175 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
176 } else {
177 SkMatrix transform3D;
178 mComputedFields.mTransformCamera.save();
179 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
180 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
181 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
182 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
183 mComputedFields.mTransformCamera.getMatrix(&transform3D);
184 transform3D.preTranslate(-getPivotX(), -getPivotY());
185 transform3D.postTranslate(getPivotX() + getTranslationX(),
186 getPivotY() + getTranslationY());
187 transform->postConcat(transform3D);
188 mComputedFields.mTransformCamera.restore();
189 }
190 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700191 }
192}
193
John Reckacb6f072014-03-12 16:11:23 -0700194} /* namespace uirenderer */
195} /* namespace android */