• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 namespace android {
34 namespace uirenderer {
35 
LayerProperties()36 LayerProperties::LayerProperties()
37         : mType(kLayerTypeNone)
38         , mColorFilter(NULL) {
39     reset();
40 }
41 
~LayerProperties()42 LayerProperties::~LayerProperties() {
43     setType(kLayerTypeNone);
44 }
45 
reset()46 void LayerProperties::reset() {
47     mOpaque = false;
48     setFromPaint(NULL);
49 }
50 
setColorFilter(SkColorFilter * filter)51 bool LayerProperties::setColorFilter(SkColorFilter* filter) {
52    if (mColorFilter == filter) return false;
53    SkRefCnt_SafeAssign(mColorFilter, filter);
54    return true;
55 }
56 
setFromPaint(const SkPaint * paint)57 bool LayerProperties::setFromPaint(const SkPaint* paint) {
58     bool changed = false;
59     SkXfermode::Mode mode;
60     int alpha;
61     OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode);
62     changed |= setAlpha(static_cast<uint8_t>(alpha));
63     changed |= setXferMode(mode);
64     changed |= setColorFilter(paint ? paint->getColorFilter() : NULL);
65     return changed;
66 }
67 
operator =(const LayerProperties & other)68 LayerProperties& LayerProperties::operator=(const LayerProperties& other) {
69     setType(other.type());
70     setOpaque(other.opaque());
71     setAlpha(other.alpha());
72     setXferMode(other.xferMode());
73     setColorFilter(other.colorFilter());
74     return *this;
75 }
76 
PrimitiveFields()77 RenderProperties::PrimitiveFields::PrimitiveFields()
78         : mClippingFlags(CLIP_TO_BOUNDS)
79         , mProjectBackwards(false)
80         , mProjectionReceiver(false)
81         , mAlpha(1)
82         , mHasOverlappingRendering(true)
83         , mElevation(0)
84         , mTranslationX(0), mTranslationY(0), mTranslationZ(0)
85         , mRotation(0), mRotationX(0), mRotationY(0)
86         , mScaleX(1), mScaleY(1)
87         , mPivotX(0), mPivotY(0)
88         , mLeft(0), mTop(0), mRight(0), mBottom(0)
89         , mWidth(0), mHeight(0)
90         , mPivotExplicitlySet(false)
91         , mMatrixOrPivotDirty(false) {
92 }
93 
ComputedFields()94 RenderProperties::ComputedFields::ComputedFields()
95         : mTransformMatrix(NULL) {
96 }
97 
~ComputedFields()98 RenderProperties::ComputedFields::~ComputedFields() {
99     delete mTransformMatrix;
100 }
101 
RenderProperties()102 RenderProperties::RenderProperties()
103         : mStaticMatrix(NULL)
104         , mAnimationMatrix(NULL) {
105 }
106 
~RenderProperties()107 RenderProperties::~RenderProperties() {
108     delete mStaticMatrix;
109     delete mAnimationMatrix;
110 }
111 
operator =(const RenderProperties & other)112 RenderProperties& RenderProperties::operator=(const RenderProperties& other) {
113     if (this != &other) {
114         mPrimitiveFields = other.mPrimitiveFields;
115         setStaticMatrix(other.getStaticMatrix());
116         setAnimationMatrix(other.getAnimationMatrix());
117         setCameraDistance(other.getCameraDistance());
118         mLayerProperties = other.layerProperties();
119 
120         // Force recalculation of the matrix, since other's dirty bit may be clear
121         mPrimitiveFields.mMatrixOrPivotDirty = true;
122         updateMatrix();
123     }
124     return *this;
125 }
126 
debugOutputProperties(const int level) const127 void RenderProperties::debugOutputProperties(const int level) const {
128     if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) {
129         ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop);
130     }
131     if (mStaticMatrix) {
132         ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING,
133                 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix));
134     }
135     if (mAnimationMatrix) {
136         ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING,
137                 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix));
138     }
139     if (hasTransformMatrix()) {
140         if (isTransformTranslateOnly()) {
141             ALOGD("%*sTranslate %.2f, %.2f, %.2f",
142                     level * 2, "", getTranslationX(), getTranslationY(), getZ());
143         } else {
144             ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING,
145                     level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix));
146         }
147     }
148 
149     const bool isLayer = layerProperties().type() != kLayerTypeNone;
150     int clipFlags = getClippingFlags();
151     if (mPrimitiveFields.mAlpha < 1) {
152         if (isLayer) {
153             clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
154 
155             ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
156         } else if (!mPrimitiveFields.mHasOverlappingRendering) {
157             ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
158         } else {
159             Rect layerBounds(0, 0, getWidth(), getHeight());
160             int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
161             if (clipFlags) {
162                 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
163                 getClippingRectForFlags(clipFlags, &layerBounds);
164                 clipFlags = 0; // all clipping done by saveLayer
165             }
166 
167             ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
168                     (int)layerBounds.left, (int)layerBounds.top, (int)layerBounds.right, (int)layerBounds.bottom,
169                     (int)(mPrimitiveFields.mAlpha * 255), saveFlags);
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 
updateMatrix()180 void 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