RenderProperties.cpp revision 8c271ca63b62061fd22cfee78fd6a574b44476fd
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
30namespace android {
31namespace uirenderer {
32
33RenderProperties::PrimitiveFields::PrimitiveFields()
34        : mClipToBounds(true)
35        , mProjectBackwards(false)
36        , mProjectionReceiver(false)
37        , mAlpha(1)
38        , mHasOverlappingRendering(true)
39        , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
40        , mRotation(0), mRotationX(0), mRotationY(0)
41        , mScaleX(1), mScaleY(1)
42        , mPivotX(0), mPivotY(0)
43        , mLeft(0), mTop(0), mRight(0), mBottom(0)
44        , mWidth(0), mHeight(0)
45        , mPrevWidth(-1), mPrevHeight(-1)
46        , mPivotExplicitlySet(false)
47        , mMatrixDirty(false)
48        , mMatrixIsIdentity(true)
49        , mMatrixFlags(0)
50        , mCaching(false) {
51}
52
53RenderProperties::ComputedFields::ComputedFields()
54        : mTransformMatrix(NULL)
55        , mTransformCamera(NULL)
56        , mTransformMatrix3D(NULL)
57        , mClipPath(NULL) {
58}
59
60RenderProperties::ComputedFields::~ComputedFields() {
61    delete mTransformMatrix;
62    delete mTransformCamera;
63    delete mTransformMatrix3D;
64    delete mClipPath;
65}
66
67RenderProperties::RenderProperties()
68        : mCameraDistance(0)
69        , mStaticMatrix(NULL)
70        , mAnimationMatrix(NULL) {
71}
72
73RenderProperties::~RenderProperties() {
74    delete mStaticMatrix;
75    delete mAnimationMatrix;
76}
77
78RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
79    if (this != &other) {
80        mPrimitiveFields = other.mPrimitiveFields;
81        setStaticMatrix(other.getStaticMatrix());
82        setAnimationMatrix(other.getAnimationMatrix());
83        setCameraDistance(other.getCameraDistance());
84
85        // Update the computed fields
86        updateMatrix();
87        updateClipPath();
88    }
89    return *this;
90}
91
92void RenderProperties::debugOutputProperties(const int level) const {
93    if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
94        ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
95    }
96    if (mStaticMatrix) {
97        ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
98                level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
99    }
100    if (mAnimationMatrix) {
101        ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
102                level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
103    }
104    if (mPrimitiveFields.mMatrixFlags != 0) {
105        if (mPrimitiveFields.mMatrixFlags == TRANSLATION) {
106            ALOGD("%*sTranslate %.2f, %.2f, %.2f",
107                    level * 2, "", mPrimitiveFields.mTranslationX, mPrimitiveFields.mTranslationY, mPrimitiveFields.mTranslationZ);
108        } else {
109            ALOGD("%*sConcatMatrix %p: " MATRIX_4_STRING,
110                    level * 2, "", mComputedFields.mTransformMatrix, MATRIX_4_ARGS(mComputedFields.mTransformMatrix));
111        }
112    }
113
114    bool clipToBoundsNeeded = mPrimitiveFields.mCaching ? false : mPrimitiveFields.mClipToBounds;
115    if (mPrimitiveFields.mAlpha < 1) {
116        if (mPrimitiveFields.mCaching) {
117            ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
118        } else if (!mPrimitiveFields.mHasOverlappingRendering) {
119            ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
120        } else {
121            int flags = SkCanvas::kHasAlphaLayer_SaveFlag;
122            if (clipToBoundsNeeded) {
123                flags |= SkCanvas::kClipToLayer_SaveFlag;
124                clipToBoundsNeeded = false; // clipping done by save layer
125            }
126            ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
127                    0, 0, getWidth(), getHeight(),
128                    (int)(mPrimitiveFields.mAlpha * 255), flags);
129        }
130    }
131    if (clipToBoundsNeeded) {
132        ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "",
133                0, 0, getWidth(), getHeight());
134    }
135}
136
137void RenderProperties::updateMatrix() {
138    if (mPrimitiveFields.mMatrixDirty) {
139        // NOTE: mComputedFields.mTransformMatrix won't be up to date if a DisplayList goes from a complex transform
140        // to a pure translate. This is safe because the mPrimitiveFields.matrix isn't read in pure translate cases.
141        if (mPrimitiveFields.mMatrixFlags && mPrimitiveFields.mMatrixFlags != TRANSLATION) {
142            if (!mComputedFields.mTransformMatrix) {
143                // only allocate a mPrimitiveFields.matrix if we have a complex transform
144                mComputedFields.mTransformMatrix = new Matrix4();
145            }
146            if (!mPrimitiveFields.mPivotExplicitlySet) {
147                if (mPrimitiveFields.mWidth != mPrimitiveFields.mPrevWidth || mPrimitiveFields.mHeight != mPrimitiveFields.mPrevHeight) {
148                    mPrimitiveFields.mPrevWidth = mPrimitiveFields.mWidth;
149                    mPrimitiveFields.mPrevHeight = mPrimitiveFields.mHeight;
150                    mPrimitiveFields.mPivotX = mPrimitiveFields.mPrevWidth / 2.0f;
151                    mPrimitiveFields.mPivotY = mPrimitiveFields.mPrevHeight / 2.0f;
152                }
153            }
154
155            if ((mPrimitiveFields.mMatrixFlags & ROTATION_3D) == 0) {
156                mComputedFields.mTransformMatrix->loadTranslate(
157                        mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX,
158                        mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY,
159                        0);
160                mComputedFields.mTransformMatrix->rotate(mPrimitiveFields.mRotation, 0, 0, 1);
161                mComputedFields.mTransformMatrix->scale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, 1);
162                mComputedFields.mTransformMatrix->translate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY);
163            } else {
164                if (!mComputedFields.mTransformCamera) {
165                    mComputedFields.mTransformCamera = new Sk3DView();
166                    mComputedFields.mTransformMatrix3D = new SkMatrix();
167                }
168                SkMatrix transformMatrix;
169                transformMatrix.reset();
170                mComputedFields.mTransformCamera->save();
171                transformMatrix.preScale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, mPrimitiveFields.mPivotX, mPrimitiveFields.mPivotY);
172                mComputedFields.mTransformCamera->rotateX(mPrimitiveFields.mRotationX);
173                mComputedFields.mTransformCamera->rotateY(mPrimitiveFields.mRotationY);
174                mComputedFields.mTransformCamera->rotateZ(-mPrimitiveFields.mRotation);
175                mComputedFields.mTransformCamera->getMatrix(mComputedFields.mTransformMatrix3D);
176                mComputedFields.mTransformMatrix3D->preTranslate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY);
177                mComputedFields.mTransformMatrix3D->postTranslate(mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX,
178                        mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY);
179                transformMatrix.postConcat(*mComputedFields.mTransformMatrix3D);
180                mComputedFields.mTransformCamera->restore();
181
182                mComputedFields.mTransformMatrix->load(transformMatrix);
183            }
184        }
185        mPrimitiveFields.mMatrixDirty = false;
186    }
187}
188
189void RenderProperties::updateClipPath() {
190    const SkPath* outlineClipPath = mPrimitiveFields.mOutline.willClip()
191            ? mPrimitiveFields.mOutline.getPath() : NULL;
192    const SkPath* revealClipPath = mPrimitiveFields.mRevealClip.getPath();
193
194    if (!outlineClipPath && !revealClipPath) {
195        // mComputedFields.mClipPath doesn't need to be updated, since it won't be used
196        return;
197    }
198
199    if (mComputedFields.mClipPath == NULL) {
200        mComputedFields.mClipPath = new SkPath();
201    }
202    SkPath* clipPath = mComputedFields.mClipPath;
203    mComputedFields.mClipPathOp = SkRegion::kIntersect_Op;
204
205    if (outlineClipPath && revealClipPath) {
206        SkPathOp op = kIntersect_PathOp;
207        if (mPrimitiveFields.mRevealClip.isInverseClip()) {
208            op = kDifference_PathOp; // apply difference step in the Op below, instead of draw time
209        }
210
211        Op(*outlineClipPath, *revealClipPath, op, clipPath);
212    } else if (outlineClipPath) {
213        *clipPath = *outlineClipPath;
214    } else {
215        *clipPath = *revealClipPath;
216        if (mPrimitiveFields.mRevealClip.isInverseClip()) {
217            // apply difference step at draw time
218            mComputedFields.mClipPathOp = SkRegion::kDifference_Op;
219        }
220    }
221}
222
223} /* namespace uirenderer */
224} /* namespace android */
225