RenderProperties.cpp revision 5a4690bf26932c0d6940e4af8516d920e09ae81a
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you mPrimitiveFields.may not use this file except in compliance with the License.
6 * You mPrimitiveFields.may obtain a copy of the License at
7 *
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 */
16
17#include "RenderProperties.h"
18
19#include <utils/Trace.h>
20
21#include <SkCanvas.h>
22#include <SkColorFilter.h>
23#include <SkMatrix.h>
24#include <SkPath.h>
25#include <SkPathOps.h>
26
27#include "Matrix.h"
28#include "OpenGLRenderer.h"
29#include "utils/MathUtils.h"
30
31namespace android {
32namespace uirenderer {
33
34LayerProperties::LayerProperties() {
35    reset();
36}
37
38LayerProperties::~LayerProperties() {
39    setType(LayerType::None);
40}
41
42void LayerProperties::reset() {
43    mOpaque = false;
44    setFromPaint(nullptr);
45}
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);
60    changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
61    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
73RenderProperties::PrimitiveFields::PrimitiveFields()
74        : mClippingFlags(CLIP_TO_BOUNDS)
75        , mProjectBackwards(false)
76        , mProjectionReceiver(false)
77        , mAlpha(1)
78        , mHasOverlappingRendering(true)
79        , mElevation(0)
80        , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
81        , mRotation(0), mRotationX(0), mRotationY(0)
82        , mScaleX(1), mScaleY(1)
83        , mPivotX(0), mPivotY(0)
84        , mLeft(0), mTop(0), mRight(0), mBottom(0)
85        , mWidth(0), mHeight(0)
86        , mPivotExplicitlySet(false)
87        , mMatrixOrPivotDirty(false) {
88}
89
90RenderProperties::ComputedFields::ComputedFields()
91        : mTransformMatrix(nullptr) {
92}
93
94RenderProperties::ComputedFields::~ComputedFields() {
95    delete mTransformMatrix;
96}
97
98RenderProperties::RenderProperties()
99        : mStaticMatrix(nullptr)
100        , mAnimationMatrix(nullptr) {
101}
102
103RenderProperties::~RenderProperties() {
104    delete mStaticMatrix;
105    delete mAnimationMatrix;
106}
107
108RenderProperties& 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());
114        mLayerProperties = other.layerProperties();
115
116        // Force recalculation of the matrix, since other's dirty bit may be clear
117        mPrimitiveFields.mMatrixOrPivotDirty = true;
118        updateMatrix();
119    }
120    return *this;
121}
122
123void 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    }
135    if (hasTransformMatrix()) {
136        if (isTransformTranslateOnly()) {
137            ALOGD("%*sTranslate %.2f, %.2f, %.2f",
138                    level * 2, "", getTranslationX(), getTranslationY(), getZ());
139        } else {
140            ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
141                    level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
142        }
143    }
144
145    const bool isLayer = effectiveLayerType() != LayerType::None;
146    int clipFlags = getClippingFlags();
147    if (mPrimitiveFields.mAlpha < 1
148            && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
149        if (isLayer) {
150            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
151        }
152
153        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
171    }
172    if (clipFlags) {
173        Rect clipRect;
174        getClippingRectForFlags(clipFlags, &clipRect);
175        ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
176                (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
177    }
178}
179
180void RenderProperties::updateMatrix() {
181    if (mPrimitiveFields.mMatrixOrPivotDirty) {
182        if (!mComputedFields.mTransformMatrix) {
183            // only allocate a mPrimitiveFields.matrix if we have a complex transform
184            mComputedFields.mTransformMatrix = new SkMatrix();
185        }
186        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();
192        if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
193            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;
211    }
212}
213
214} /* namespace uirenderer */
215} /* namespace android */
216