blob: ad74bff8dc25746c625d5d69f91c03b101c87820 [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::ComputedFields::ComputedFields()
Chris Craikd41c4d82015-01-05 15:51:13 -080074 : mTransformMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070075}
76
77RenderProperties::ComputedFields::~ComputedFields() {
John Reckacb6f072014-03-12 16:11:23 -070078 delete mTransformMatrix;
John Reckd0a0b2a2014-03-20 16:28:56 -070079}
80
81RenderProperties::RenderProperties()
Chris Craikd41c4d82015-01-05 15:51:13 -080082 : mStaticMatrix(nullptr)
83 , mAnimationMatrix(nullptr) {
John Reckd0a0b2a2014-03-20 16:28:56 -070084}
85
86RenderProperties::~RenderProperties() {
John Reckacb6f072014-03-12 16:11:23 -070087 delete mStaticMatrix;
88 delete mAnimationMatrix;
89}
90
John Reckd0a0b2a2014-03-20 16:28:56 -070091RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
92 if (this != &other) {
93 mPrimitiveFields = other.mPrimitiveFields;
94 setStaticMatrix(other.getStaticMatrix());
95 setAnimationMatrix(other.getAnimationMatrix());
96 setCameraDistance(other.getCameraDistance());
John Reck25fbb3f2014-06-12 13:46:45 -070097 mLayerProperties = other.layerProperties();
John Reckd0a0b2a2014-03-20 16:28:56 -070098
Chris Craik49e6c732014-03-31 12:34:11 -070099 // Force recalculation of the matrix, since other's dirty bit may be clear
John Reckf7483e32014-04-11 08:54:47 -0700100 mPrimitiveFields.mMatrixOrPivotDirty = true;
Chris Craik49e6c732014-03-31 12:34:11 -0700101 updateMatrix();
John Reckd0a0b2a2014-03-20 16:28:56 -0700102 }
103 return *this;
John Reckacb6f072014-03-12 16:11:23 -0700104}
105
John Reckd0a0b2a2014-03-20 16:28:56 -0700106void RenderProperties::debugOutputProperties(const int level) const {
107 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
108 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
109 }
110 if (mStaticMatrix) {
111 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
112 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
113 }
114 if (mAnimationMatrix) {
115 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
116 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
117 }
John Reckf7483e32014-04-11 08:54:47 -0700118 if (hasTransformMatrix()) {
119 if (isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700120 ALOGD("%*sTranslate %.2f, %.2f, %.2f",
Chris Craikcc39e162014-04-25 18:34:11 -0700121 level * 2, "", getTranslationX(), getTranslationY(), getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700122 } else {
Chris Craik49e6c732014-03-31 12:34:11 -0700123 ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
124 level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
John Reckd0a0b2a2014-03-20 16:28:56 -0700125 }
126 }
127
Chris Craik856f0cc2015-04-21 15:13:29 -0700128 const bool isLayer = effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700129 int clipFlags = getClippingFlags();
Chris Craik43a1d312015-05-27 11:28:14 -0700130 if (mPrimitiveFields.mAlpha < 1
131 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
Chris Craika753f4c2014-07-24 12:39:17 -0700132 if (isLayer) {
133 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reckd0a0b2a2014-03-20 16:28:56 -0700134 }
Chris Craik8df5ffa2015-04-28 17:47:20 -0700135
Chris Craik4e9d9b22015-06-12 11:07:23 -0700136 if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
137 // simply scale rendering content's alpha
138 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
139 } else {
140 // savelayeralpha to create an offscreen buffer to apply alpha
141 Rect layerBounds(0, 0, getWidth(), getHeight());
142 if (clipFlags) {
143 getClippingRectForFlags(clipFlags, &layerBounds);
144 clipFlags = 0; // all clipping done by savelayer
145 }
146 ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
147 (int)layerBounds.left, (int)layerBounds.top,
148 (int)layerBounds.right, (int)layerBounds.bottom,
149 (int)(mPrimitiveFields.mAlpha * 255),
150 SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
151 }
152
153
John Reckd0a0b2a2014-03-20 16:28:56 -0700154 }
Chris Craika753f4c2014-07-24 12:39:17 -0700155 if (clipFlags) {
156 Rect clipRect;
157 getClippingRectForFlags(clipFlags, &clipRect);
John Reck78ce1c52014-03-24 15:43:49 -0700158 ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
Chris Craika753f4c2014-07-24 12:39:17 -0700159 (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
John Reckd0a0b2a2014-03-20 16:28:56 -0700160 }
John Reckacb6f072014-03-12 16:11:23 -0700161}
162
163void RenderProperties::updateMatrix() {
John Reckf7483e32014-04-11 08:54:47 -0700164 if (mPrimitiveFields.mMatrixOrPivotDirty) {
165 if (!mComputedFields.mTransformMatrix) {
166 // only allocate a mPrimitiveFields.matrix if we have a complex transform
167 mComputedFields.mTransformMatrix = new SkMatrix();
John Reckacb6f072014-03-12 16:11:23 -0700168 }
John Reckf7483e32014-04-11 08:54:47 -0700169 if (!mPrimitiveFields.mPivotExplicitlySet) {
170 mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
171 mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
172 }
173 SkMatrix* transform = mComputedFields.mTransformMatrix;
174 transform->reset();
Chris Craike0bb87d2014-04-22 17:55:41 -0700175 if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
John Reckf7483e32014-04-11 08:54:47 -0700176 transform->setTranslate(getTranslationX(), getTranslationY());
177 transform->preRotate(getRotation(), getPivotX(), getPivotY());
178 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
179 } else {
180 SkMatrix transform3D;
181 mComputedFields.mTransformCamera.save();
182 transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
183 mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
184 mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
185 mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
186 mComputedFields.mTransformCamera.getMatrix(&transform3D);
187 transform3D.preTranslate(-getPivotX(), -getPivotY());
188 transform3D.postTranslate(getPivotX() + getTranslationX(),
189 getPivotY() + getTranslationY());
190 transform->postConcat(transform3D);
191 mComputedFields.mTransformCamera.restore();
192 }
193 mPrimitiveFields.mMatrixOrPivotDirty = false;
John Reckacb6f072014-03-12 16:11:23 -0700194 }
195}
196
John Reckacb6f072014-03-12 16:11:23 -0700197} /* namespace uirenderer */
198} /* namespace android */