blob: 7e3cad4ca9d0f21c3f00be0969091aa346fd94e9 [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"
sergeyvdccca442016-03-21 15:38:21 -070027#include "hwui/Canvas.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070028#include "utils/MathUtils.h"
John Reckf7483e32014-04-11 08:54:47 -070029
John Reckacb6f072014-03-12 16:11:23 -070030namespace android {
31namespace uirenderer {
32
Chris Craik182952f2015-03-09 14:17:29 -070033LayerProperties::LayerProperties() {
John Reck25fbb3f2014-06-12 13:46:45 -070034 reset();
35}
36
37LayerProperties::~LayerProperties() {
Chris Craik182952f2015-03-09 14:17:29 -070038 setType(LayerType::None);
John Reck25fbb3f2014-06-12 13:46:45 -070039}
40
41void LayerProperties::reset() {
42 mOpaque = false;
Chris Craikd41c4d82015-01-05 15:51:13 -080043 setFromPaint(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070044}
45
46bool LayerProperties::setColorFilter(SkColorFilter* filter) {
47 if (mColorFilter == filter) return false;
48 SkRefCnt_SafeAssign(mColorFilter, filter);
49 return true;
50}
51
52bool LayerProperties::setFromPaint(const SkPaint* paint) {
53 bool changed = false;
Chris Craikbf6f0f22015-10-01 12:36:07 -070054 changed |= setAlpha(static_cast<uint8_t>(PaintUtils::getAlphaDirect(paint)));
55 changed |= setXferMode(PaintUtils::getXfermodeDirect(paint));
Chris Craikd41c4d82015-01-05 15:51:13 -080056 changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -070057 return changed;
58}
59
60LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
61 setType(other.type());
62 setOpaque(other.opaque());
63 setAlpha(other.alpha());
64 setXferMode(other.xferMode());
65 setColorFilter(other.colorFilter());
66 return *this;
67}
68
John Reckd0a0b2a2014-03-20 16:28:56 -070069RenderProperties::ComputedFields::ComputedFields()
Chris Craikd41c4d82015-01-05 15:51:13 -080070 : mTransformMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070071}
72
73RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070074 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -070075}
76
77RenderProperties::RenderProperties()
Chris Craikd41c4d82015-01-05 15:51:13 -080078 : mStaticMatrix(nullptr)
79 , mAnimationMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070080}
81
82RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070083 delete mStaticMatrix;
84 delete mAnimationMatrix;
85}
86
John Reckd0a0b2a2014-03-20 16:28:56 -070087RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
88 if (this != &other) {
89 mPrimitiveFields = other.mPrimitiveFields;
90 setStaticMatrix(other.getStaticMatrix());
91 setAnimationMatrix(other.getAnimationMatrix());
92 setCameraDistance(other.getCameraDistance());
John Reck25fbb3f2014-06-12 13:46:45 -070093 mLayerProperties = other.layerProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -070094
Chris Craik49e6c7392014-03-31 12:34:11 -070095 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -070096 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c7392014-03-31 12:34:11 -070097 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -070098 }
99 return *this;
John Reckacb6f072014-03-12 16:11:23 -0700100}
101
John Reckd0a0b2a2014-03-20 16:28:56 -0700102void RenderProperties::debugOutputProperties(const int level) const {
103 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
Chris Craik91eff222016-02-22 13:39:33 -0800104 ALOGD("%*s(Translate (left, top) %d, %d)", level * 2, "",
105 mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
John Reckd0a0b2a2014-03-20 16:28:56 -0700106 }
107 if (mStaticMatrix) {
Chris Craik91eff222016-02-22 13:39:33 -0800108 ALOGD("%*s(ConcatMatrix (static) %p: " SK_MATRIX_STRING ")",
John Reckd0a0b2a2014-03-20 16:28:56 -0700109 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
110 }
111 if (mAnimationMatrix) {
Chris Craik91eff222016-02-22 13:39:33 -0800112 ALOGD("%*s(ConcatMatrix (animation) %p: " SK_MATRIX_STRING ")",
John Reckd0a0b2a2014-03-20 16:28:56 -0700113 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
114 }
John Reckf7483e32014-04-11 08:54:47 -0700115 if (hasTransformMatrix()) {
116 if (isTransformTranslateOnly()) {
Chris Craik91eff222016-02-22 13:39:33 -0800117 ALOGD("%*s(Translate %.2f, %.2f, %.2f)",
Chris Craikcc39e162014-04-25 18:34:11 -0700118 level * 2, "", getTranslationX(), getTranslationY(), getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700119 } else {
Chris Craik91eff222016-02-22 13:39:33 -0800120 ALOGD("%*s(ConcatMatrix %p: " SK_MATRIX_STRING ")",
Chris Craik49e6c7392014-03-31 12:34:11 -0700121 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700122 }
123 }
124
Chris Craik856f0cc2015-04-21 15:13:29 -0700125 const bool isLayer = effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700126 int clipFlags = getClippingFlags();
Chris Craik43a1d312015-05-27 11:28:14 -0700127 if (mPrimitiveFields.mAlpha < 1
128 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
Chris Craika753f4c2014-07-24 12:39:17 -0700129 if (isLayer) {
130 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reckd0a0b2a2014-03-20 16:28:56 -0700131 }
Chris Craik8df5ffa2015-04-28 17:47:20 -0700132
Chris Craik4e9d9b22015-06-12 11:07:23 -0700133 if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
134 // simply scale rendering content's alpha
Chris Craik91eff222016-02-22 13:39:33 -0800135 ALOGD("%*s(ScaleAlpha %.2f)", level * 2, "", mPrimitiveFields.mAlpha);
Chris Craik4e9d9b22015-06-12 11:07:23 -0700136 } else {
137 // savelayeralpha to create an offscreen buffer to apply alpha
138 Rect layerBounds(0, 0, getWidth(), getHeight());
139 if (clipFlags) {
140 getClippingRectForFlags(clipFlags, &layerBounds);
141 clipFlags = 0; // all clipping done by savelayer
142 }
Chris Craik91eff222016-02-22 13:39:33 -0800143 ALOGD("%*s(SaveLayerAlpha %d, %d, %d, %d, %d, 0x%x)", level * 2, "",
Chris Craik4e9d9b22015-06-12 11:07:23 -0700144 (int)layerBounds.left, (int)layerBounds.top,
145 (int)layerBounds.right, (int)layerBounds.bottom,
146 (int)(mPrimitiveFields.mAlpha * 255),
Florin Malitaeecff562015-12-21 10:43:01 -0500147 SaveFlags::HasAlphaLayer | SaveFlags::ClipToLayer);
Chris Craik4e9d9b22015-06-12 11:07:23 -0700148 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700149 }
Chris Craik91eff222016-02-22 13:39:33 -0800150
Chris Craika753f4c2014-07-24 12:39:17 -0700151 if (clipFlags) {
152 Rect clipRect;
153 getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik91eff222016-02-22 13:39:33 -0800154 ALOGD("%*s(ClipRect %d, %d, %d, %d)", level * 2, "",
Chris Craika753f4c2014-07-24 12:39:17 -0700155 (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
John Reckd0a0b2a2014-03-20 16:28:56 -0700156 }
Chris Craik136d1af2016-04-04 13:40:39 -0700157
158 if (getRevealClip().willClip()) {
159 Rect bounds;
160 getRevealClip().getBounds(&bounds);
161 ALOGD("%*s(Clip to reveal clip with bounds %.2f %.2f %.2f %.2f)", level * 2, "",
162 RECT_ARGS(bounds));
163 }
164
165 auto& outline = mPrimitiveFields.mOutline;
166 if (outline.getShouldClip()) {
167 if (outline.isEmpty()) {
168 ALOGD("%*s(Clip to empty outline)", level * 2, "");
169 } else if (outline.willClip()) {
170 ALOGD("%*s(Clip to outline with bounds %.2f %.2f %.2f %.2f)", level * 2, "",
171 RECT_ARGS(outline.getBounds()));
172 }
173 }
John Reckacb6f072014-03-12 16:11:23 -0700174}
175
176void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700177 if (mPrimitiveFields.mMatrixOrPivotDirty) {
178 if (!mComputedFields.mTransformMatrix) {
179 // only allocate a mPrimitiveFields.matrix if we have a complex transform
180 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700181 }
John Reckf7483e32014-04-11 08:54:47 -0700182 if (!mPrimitiveFields.mPivotExplicitlySet) {
183 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
184 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
185 }
186 SkMatrix* transform = mComputedFields.mTransformMatrix;
187 transform->reset();
Chris Craike0bb87d2014-04-22 17:55:41 -0700188 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700189 transform->setTranslate(getTranslationX(), getTranslationY());
190 transform->preRotate(getRotation(), getPivotX(), getPivotY());
191 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
192 } else {
193 SkMatrix transform3D;
194 mComputedFields.mTransformCamera.save();
195 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
196 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
197 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
198 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
199 mComputedFields.mTransformCamera.getMatrix(&transform3D);
200 transform3D.preTranslate(-getPivotX(), -getPivotY());
201 transform3D.postTranslate(getPivotX() + getTranslationX(),
202 getPivotY() + getTranslationY());
203 transform->postConcat(transform3D);
204 mComputedFields.mTransformCamera.restore();
205 }
206 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700207 }
208}
209
John Reckacb6f072014-03-12 16:11:23 -0700210} /* namespace uirenderer */
211} /* namespace android */