RenderProperties.cpp revision d0a0b2a3140bfb1819a116413ce9d81886697a07
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 26#include "Matrix.h" 27 28namespace android { 29namespace uirenderer { 30 31RenderProperties::PrimitiveFields::PrimitiveFields() 32 : mClipToBounds(true) 33 , mProjectBackwards(false) 34 , mProjectionReceiver(false) 35 , mAlpha(1) 36 , mHasOverlappingRendering(true) 37 , mTranslationX(0), mTranslationY(0), mTranslationZ(0) 38 , mRotation(0), mRotationX(0), mRotationY(0) 39 , mScaleX(1), mScaleY(1) 40 , mPivotX(0), mPivotY(0) 41 , mLeft(0), mTop(0), mRight(0), mBottom(0) 42 , mWidth(0), mHeight(0) 43 , mPrevWidth(-1), mPrevHeight(-1) 44 , mPivotExplicitlySet(false) 45 , mMatrixDirty(false) 46 , mMatrixIsIdentity(true) 47 , mMatrixFlags(0) 48 , mCaching(false) { 49} 50 51RenderProperties::ComputedFields::ComputedFields() 52 : mTransformMatrix(NULL) 53 , mTransformCamera(NULL) 54 , mTransformMatrix3D(NULL) { 55} 56 57RenderProperties::ComputedFields::~ComputedFields() { 58 delete mTransformMatrix; 59 delete mTransformCamera; 60 delete mTransformMatrix3D; 61} 62 63RenderProperties::RenderProperties() 64 : mCameraDistance(0) 65 , mStaticMatrix(NULL) 66 , mAnimationMatrix(NULL) { 67} 68 69RenderProperties::~RenderProperties() { 70 delete mStaticMatrix; 71 delete mAnimationMatrix; 72} 73 74RenderProperties& RenderProperties::operator=(const RenderProperties& other) { 75 if (this != &other) { 76 mPrimitiveFields = other.mPrimitiveFields; 77 setStaticMatrix(other.getStaticMatrix()); 78 setAnimationMatrix(other.getAnimationMatrix()); 79 setCameraDistance(other.getCameraDistance()); 80 81 // Update the computed fields 82 updateMatrix(); 83 } 84 return *this; 85} 86 87void RenderProperties::debugOutputProperties(const int level) const { 88 if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) { 89 ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop); 90 } 91 if (mStaticMatrix) { 92 ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING, 93 level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix)); 94 } 95 if (mAnimationMatrix) { 96 ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING, 97 level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix)); 98 } 99 if (mPrimitiveFields.mMatrixFlags != 0) { 100 if (mPrimitiveFields.mMatrixFlags == TRANSLATION) { 101 ALOGD("%*sTranslate %.2f, %.2f, %.2f", 102 level * 2, "", mPrimitiveFields.mTranslationX, mPrimitiveFields.mTranslationY, mPrimitiveFields.mTranslationZ); 103 } else { 104 ALOGD("%*sConcatMatrix %p: " MATRIX_4_STRING, 105 level * 2, "", mComputedFields.mTransformMatrix, MATRIX_4_ARGS(mComputedFields.mTransformMatrix)); 106 } 107 } 108 109 bool clipToBoundsNeeded = mPrimitiveFields.mCaching ? false : mPrimitiveFields.mClipToBounds; 110 if (mPrimitiveFields.mAlpha < 1) { 111 if (mPrimitiveFields.mCaching) { 112 ALOGD("%*sSetOverrideLayerAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha); 113 } else if (!mPrimitiveFields.mHasOverlappingRendering) { 114 ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha); 115 } else { 116 int flags = SkCanvas::kHasAlphaLayer_SaveFlag; 117 if (clipToBoundsNeeded) { 118 flags |= SkCanvas::kClipToLayer_SaveFlag; 119 clipToBoundsNeeded = false; // clipping done by save layer 120 } 121 ALOGD("%*sSaveLayerAlpha %.2f, %.2f, %.2f, %.2f, %d, 0x%x", level * 2, "", 122 (float) 0, (float) 0, (float) mPrimitiveFields.mRight - mPrimitiveFields.mLeft, (float) mPrimitiveFields.mBottom - mPrimitiveFields.mTop, 123 (int)(mPrimitiveFields.mAlpha * 255), flags); 124 } 125 } 126 if (clipToBoundsNeeded) { 127 ALOGD("%*sClipRect %.2f, %.2f, %.2f, %.2f", level * 2, "", 0.0f, 0.0f, 128 (float) mPrimitiveFields.mRight - mPrimitiveFields.mLeft, (float) mPrimitiveFields.mBottom - mPrimitiveFields.mTop); 129 } 130} 131 132void RenderProperties::updateMatrix() { 133 if (mPrimitiveFields.mMatrixDirty) { 134 // NOTE: mComputedFields.mTransformMatrix won't be up to date if a DisplayList goes from a complex transform 135 // to a pure translate. This is safe because the mPrimitiveFields.matrix isn't read in pure translate cases. 136 if (mPrimitiveFields.mMatrixFlags && mPrimitiveFields.mMatrixFlags != TRANSLATION) { 137 if (!mComputedFields.mTransformMatrix) { 138 // only allocate a mPrimitiveFields.matrix if we have a complex transform 139 mComputedFields.mTransformMatrix = new Matrix4(); 140 } 141 if (!mPrimitiveFields.mPivotExplicitlySet) { 142 if (mPrimitiveFields.mWidth != mPrimitiveFields.mPrevWidth || mPrimitiveFields.mHeight != mPrimitiveFields.mPrevHeight) { 143 mPrimitiveFields.mPrevWidth = mPrimitiveFields.mWidth; 144 mPrimitiveFields.mPrevHeight = mPrimitiveFields.mHeight; 145 mPrimitiveFields.mPivotX = mPrimitiveFields.mPrevWidth / 2.0f; 146 mPrimitiveFields.mPivotY = mPrimitiveFields.mPrevHeight / 2.0f; 147 } 148 } 149 150 if ((mPrimitiveFields.mMatrixFlags & ROTATION_3D) == 0) { 151 mComputedFields.mTransformMatrix->loadTranslate( 152 mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX, 153 mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY, 154 0); 155 mComputedFields.mTransformMatrix->rotate(mPrimitiveFields.mRotation, 0, 0, 1); 156 mComputedFields.mTransformMatrix->scale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, 1); 157 mComputedFields.mTransformMatrix->translate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY); 158 } else { 159 if (!mComputedFields.mTransformCamera) { 160 mComputedFields.mTransformCamera = new Sk3DView(); 161 mComputedFields.mTransformMatrix3D = new SkMatrix(); 162 } 163 SkMatrix transformMatrix; 164 transformMatrix.reset(); 165 mComputedFields.mTransformCamera->save(); 166 transformMatrix.preScale(mPrimitiveFields.mScaleX, mPrimitiveFields.mScaleY, mPrimitiveFields.mPivotX, mPrimitiveFields.mPivotY); 167 mComputedFields.mTransformCamera->rotateX(mPrimitiveFields.mRotationX); 168 mComputedFields.mTransformCamera->rotateY(mPrimitiveFields.mRotationY); 169 mComputedFields.mTransformCamera->rotateZ(-mPrimitiveFields.mRotation); 170 mComputedFields.mTransformCamera->getMatrix(mComputedFields.mTransformMatrix3D); 171 mComputedFields.mTransformMatrix3D->preTranslate(-mPrimitiveFields.mPivotX, -mPrimitiveFields.mPivotY); 172 mComputedFields.mTransformMatrix3D->postTranslate(mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX, 173 mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY); 174 transformMatrix.postConcat(*mComputedFields.mTransformMatrix3D); 175 mComputedFields.mTransformCamera->restore(); 176 177 mComputedFields.mTransformMatrix->load(transformMatrix); 178 } 179 } 180 mPrimitiveFields.mMatrixDirty = false; 181 } 182} 183 184} /* namespace uirenderer */ 185} /* namespace android */ 186