RenderProperties.cpp revision 4e9d9b2a590105035c29f12ecf0689333e6b55e7
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#define LOG_TAG "OpenGLRenderer"
18
19#include "RenderProperties.h"
20
21#include <utils/Trace.h>
22
23#include <SkCanvas.h>
24#include <SkColorFilter.h>
25#include <SkMatrix.h>
26#include <SkPath.h>
27#include <SkPathOps.h>
28
29#include "Matrix.h"
30#include "OpenGLRenderer.h"
31#include "utils/MathUtils.h"
32
33namespace android {
34namespace uirenderer {
35
36LayerProperties::LayerProperties() {
37    reset();
38}
39
40LayerProperties::~LayerProperties() {
41    setType(LayerType::None);
42}
43
44void LayerProperties::reset() {
45    mOpaque = false;
46    setFromPaint(nullptr);
47}
48
49bool LayerProperties::setColorFilter(SkColorFilter* filter) {
50   if (mColorFilter == filter) return false;
51   SkRefCnt_SafeAssign(mColorFilter, filter);
52   return true;
53}
54
55bool LayerProperties::setFromPaint(const SkPaint* paint) {
56    bool changed = false;
57    SkXfermode::Mode mode;
58    int alpha;
59    OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
60    changed |= setAlpha(static_cast<uint8_t>(alpha));
61    changed |= setXferMode(mode);
62    changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr);
63    return changed;
64}
65
66LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
67    setType(other.type());
68    setOpaque(other.opaque());
69    setAlpha(other.alpha());
70    setXferMode(other.xferMode());
71    setColorFilter(other.colorFilter());
72    return *this;
73}
74
75RenderProperties::PrimitiveFields::PrimitiveFields()
76        : mClippingFlags(CLIP_TO_BOUNDS)
77        , mProjectBackwards(false)
78        , mProjectionReceiver(false)
79        , mAlpha(1)
80        , mHasOverlappingRendering(true)
81        , mElevation(0)
82        , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
83        , mRotation(0), mRotationX(0), mRotationY(0)
84        , mScaleX(1), mScaleY(1)
85        , mPivotX(0), mPivotY(0)
86        , mLeft(0), mTop(0), mRight(0), mBottom(0)
87        , mWidth(0), mHeight(0)
88        , mPivotExplicitlySet(false)
89        , mMatrixOrPivotDirty(false) {
90}
91
92RenderProperties::ComputedFields::ComputedFields()
93        : mTransformMatrix(nullptr) {
94}
95
96RenderProperties::ComputedFields::~ComputedFields() {
97    delete mTransformMatrix;
98}
99
100RenderProperties::RenderProperties()
101        : mStaticMatrix(nullptr)
102        , mAnimationMatrix(nullptr) {
103}
104
105RenderProperties::~RenderProperties() {
106    delete mStaticMatrix;
107    delete mAnimationMatrix;
108}
109
110RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
111    if (this != &other) {
112        mPrimitiveFields = other.mPrimitiveFields;
113        setStaticMatrix(other.getStaticMatrix());
114        setAnimationMatrix(other.getAnimationMatrix());
115        setCameraDistance(other.getCameraDistance());
116        mLayerProperties = other.layerProperties();
117
118        // Force recalculation of the matrix, since other's dirty bit may be clear
119        mPrimitiveFields.mMatrixOrPivotDirty = true;
120        updateMatrix();
121    }
122    return *this;
123}
124
125void RenderProperties::debugOutputProperties(const int level) const {
126    if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
127        ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
128    }
129    if (mStaticMatrix) {
130        ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
131                level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
132    }
133    if (mAnimationMatrix) {
134        ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
135                level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
136    }
137    if (hasTransformMatrix()) {
138        if (isTransformTranslateOnly()) {
139            ALOGD("%*sTranslate %.2f, %.2f, %.2f",
140                    level * 2, "", getTranslationX(), getTranslationY(), getZ());
141        } else {
142            ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
143                    level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
144        }
145    }
146
147    const bool isLayer = effectiveLayerType() != LayerType::None;
148    int clipFlags = getClippingFlags();
149    if (mPrimitiveFields.mAlpha < 1
150            && !MathUtils::isZero(mPrimitiveFields.mAlpha)) {
151        if (isLayer) {
152            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
153        }
154
155        if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
156            // simply scale rendering content's alpha
157            ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
158        } else {
159            // savelayeralpha to create an offscreen buffer to apply alpha
160            Rect layerBounds(0, 0, getWidth(), getHeight());
161            if (clipFlags) {
162                getClippingRectForFlags(clipFlags, &layerBounds);
163                clipFlags = 0; // all clipping done by savelayer
164            }
165            ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
166                    (int)layerBounds.left, (int)layerBounds.top,
167                    (int)layerBounds.right, (int)layerBounds.bottom,
168                    (int)(mPrimitiveFields.mAlpha * 255),
169                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
170        }
171
172
173    }
174    if (clipFlags) {
175        Rect clipRect;
176        getClippingRectForFlags(clipFlags, &clipRect);
177        ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
178                (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom);
179    }
180}
181
182void RenderProperties::updateMatrix() {
183    if (mPrimitiveFields.mMatrixOrPivotDirty) {
184        if (!mComputedFields.mTransformMatrix) {
185            // only allocate a mPrimitiveFields.matrix if we have a complex transform
186            mComputedFields.mTransformMatrix = new SkMatrix();
187        }
188        if (!mPrimitiveFields.mPivotExplicitlySet) {
189            mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
190            mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
191        }
192        SkMatrix* transform = mComputedFields.mTransformMatrix;
193        transform->reset();
194        if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) {
195            transform->setTranslate(getTranslationX(), getTranslationY());
196            transform->preRotate(getRotation(), getPivotX(), getPivotY());
197            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
198        } else {
199            SkMatrix transform3D;
200            mComputedFields.mTransformCamera.save();
201            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
202            mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
203            mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
204            mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
205            mComputedFields.mTransformCamera.getMatrix(&transform3D);
206            transform3D.preTranslate(-getPivotX(), -getPivotY());
207            transform3D.postTranslate(getPivotX() + getTranslationX(),
208                    getPivotY() + getTranslationY());
209            transform->postConcat(transform3D);
210            mComputedFields.mTransformCamera.restore();
211        }
212        mPrimitiveFields.mMatrixOrPivotDirty = false;
213    }
214}
215
216} /* namespace uirenderer */
217} /* namespace android */
218