RenderProperties.cpp revision 222f33178b1547b70350ead660070eb01e56eac2
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 <SkMatrix.h>
25#include <SkPath.h>
26#include <SkPathOps.h>
27
28#include "Matrix.h"
29
30/**
31 * Convenience value to check for float values that are close enough to zero to be considered
32 * zero.
33 */
34#define NONZERO_EPSILON .001f
35
36static inline bool is_zero(float value) {
37    return (value >= -NONZERO_EPSILON) && (value <= NONZERO_EPSILON);
38}
39
40namespace android {
41namespace uirenderer {
42
43RenderProperties::PrimitiveFields::PrimitiveFields()
44        : mClipToBounds(true)
45        , mProjectBackwards(false)
46        , mProjectionReceiver(false)
47        , mAlpha(1)
48        , mHasOverlappingRendering(true)
49        , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
50        , mRotation(0), mRotationX(0), mRotationY(0)
51        , mScaleX(1), mScaleY(1)
52        , mPivotX(0), mPivotY(0)
53        , mLeft(0), mTop(0), mRight(0), mBottom(0)
54        , mWidth(0), mHeight(0)
55        , mPivotExplicitlySet(false)
56        , mMatrixOrPivotDirty(false)
57        , mCaching(false) {
58}
59
60RenderProperties::ComputedFields::ComputedFields()
61        : mTransformMatrix(NULL)
62        , mClipPath(NULL) {
63}
64
65RenderProperties::ComputedFields::~ComputedFields() {
66    delete mTransformMatrix;
67    delete mClipPath;
68}
69
70RenderProperties::RenderProperties()
71        : mStaticMatrix(NULL)
72        , mAnimationMatrix(NULL) {
73}
74
75RenderProperties::~RenderProperties() {
76    delete mStaticMatrix;
77    delete mAnimationMatrix;
78}
79
80RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
81    if (this != &other) {
82        mPrimitiveFields = other.mPrimitiveFields;
83        setStaticMatrix(other.getStaticMatrix());
84        setAnimationMatrix(other.getAnimationMatrix());
85        setCameraDistance(other.getCameraDistance());
86
87        // Update the computed clip path
88        updateClipPath();
89
90        // Force recalculation of the matrix, since other's dirty bit may be clear
91        mPrimitiveFields.mMatrixOrPivotDirty = true;
92        updateMatrix();
93    }
94    return *this;
95}
96
97void RenderProperties::debugOutputProperties(const int level) const {
98    if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
99        ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
100    }
101    if (mStaticMatrix) {
102        ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
103                level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
104    }
105    if (mAnimationMatrix) {
106        ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
107                level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
108    }
109    if (hasTransformMatrix()) {
110        if (isTransformTranslateOnly()) {
111            ALOGD("%*sTranslate %.2f, %.2f, %.2f",
112                    level * 2, "", mPrimitiveFields.mTranslationX, mPrimitiveFields.mTranslationY, mPrimitiveFields.mTranslationZ);
113        } else {
114            ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
115                    level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
116        }
117    }
118
119    bool clipToBoundsNeeded = mPrimitiveFields.mCaching ? false : mPrimitiveFields.mClipToBounds;
120    if (mPrimitiveFields.mAlpha < 1) {
121        if (mPrimitiveFields.mCaching) {
122            ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
123        } else if (!mPrimitiveFields.mHasOverlappingRendering) {
124            ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
125        } else {
126            int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
127            if (clipToBoundsNeeded) {
128                flags |= SkCanvas::kClipToLayer_SaveFlag;
129                clipToBoundsNeeded = false; // clipping done by save layer
130            }
131            ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
132                    0, 0, getWidth(), getHeight(),
133                    (int)(mPrimitiveFields.mAlpha * 255), flags);
134        }
135    }
136    if (clipToBoundsNeeded) {
137        ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
138                0, 0, getWidth(), getHeight());
139    }
140}
141
142void RenderProperties::updateMatrix() {
143    if (mPrimitiveFields.mMatrixOrPivotDirty) {
144        if (!mComputedFields.mTransformMatrix) {
145            // only allocate a mPrimitiveFields.matrix if we have a complex transform
146            mComputedFields.mTransformMatrix = new SkMatrix();
147        }
148        if (!mPrimitiveFields.mPivotExplicitlySet) {
149            mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f;
150            mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f;
151        }
152        SkMatrix* transform = mComputedFields.mTransformMatrix;
153        transform->reset();
154        if (is_zero(getRotationX()) && is_zero(getRotationY())) {
155            transform->setTranslate(getTranslationX(), getTranslationY());
156            transform->preRotate(getRotation(), getPivotX(), getPivotY());
157            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
158        } else {
159            SkMatrix transform3D;
160            mComputedFields.mTransformCamera.save();
161            transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY());
162            mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX);
163            mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY);
164            mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation);
165            mComputedFields.mTransformCamera.getMatrix(&transform3D);
166            transform3D.preTranslate(-getPivotX(), -getPivotY());
167            transform3D.postTranslate(getPivotX() + getTranslationX(),
168                    getPivotY() + getTranslationY());
169            transform->postConcat(transform3D);
170            mComputedFields.mTransformCamera.restore();
171        }
172        mPrimitiveFields.mMatrixOrPivotDirty = false;
173    }
174}
175
176void RenderProperties::updateClipPath() {
177    const SkPath* outlineClipPath = mPrimitiveFields.mOutline.willClip()
178            ? mPrimitiveFields.mOutline.getPath() : NULL;
179    const SkPath* revealClipPath = mPrimitiveFields.mRevealClip.getPath();
180
181    if (!outlineClipPath && !revealClipPath) {
182        // mComputedFields.mClipPath doesn't need to be updated, since it won't be used
183        return;
184    }
185
186    if (mComputedFields.mClipPath == NULL) {
187        mComputedFields.mClipPath = new SkPath();
188    }
189    SkPath* clipPath = mComputedFields.mClipPath;
190    mComputedFields.mClipPathOp = SkRegion::kIntersect_Op;
191
192    if (outlineClipPath && revealClipPath) {
193        SkPathOp op = kIntersect_PathOp;
194        if (mPrimitiveFields.mRevealClip.isInverseClip()) {
195            op = kDifference_PathOp; // apply difference step in the Op below, instead of draw time
196        }
197
198        Op(*outlineClipPath, *revealClipPath, op, clipPath);
199    } else if (outlineClipPath) {
200        *clipPath = *outlineClipPath;
201    } else {
202        *clipPath = *revealClipPath;
203        if (mPrimitiveFields.mRevealClip.isInverseClip()) {
204            // apply difference step at draw time
205            mComputedFields.mClipPathOp = SkRegion::kDifference_Op;
206        }
207    }
208}
209
210} /* namespace uirenderer */
211} /* namespace android */
212