John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 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 |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 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 | */ |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 19 | #include "RenderProperties.h" |
| 20 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 21 | #include <utils/Trace.h> |
| 22 | |
| 23 | #include <SkCanvas.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 24 | #include <SkMatrix.h> |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 25 | #include <SkPath.h> |
| 26 | #include <SkPathOps.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 27 | |
| 28 | #include "Matrix.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 33 | RenderProperties::PrimitiveFields::PrimitiveFields() |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 34 | : mClipToBounds(true) |
| 35 | , mProjectBackwards(false) |
| 36 | , mProjectionReceiver(false) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 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) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 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) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 49 | , mMatrixFlags(0) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 50 | , mCaching(false) { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 51 | } |
| 52 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 53 | RenderProperties::ComputedFields::ComputedFields() |
| 54 | : mTransformMatrix(NULL) |
| 55 | , mTransformCamera(NULL) |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 56 | , mTransformMatrix3D(NULL) |
| 57 | , mClipPath(NULL) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | RenderProperties::ComputedFields::~ComputedFields() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 61 | delete mTransformMatrix; |
| 62 | delete mTransformCamera; |
| 63 | delete mTransformMatrix3D; |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 64 | delete mClipPath; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | RenderProperties::RenderProperties() |
| 68 | : mCameraDistance(0) |
| 69 | , mStaticMatrix(NULL) |
| 70 | , mAnimationMatrix(NULL) { |
| 71 | } |
| 72 | |
| 73 | RenderProperties::~RenderProperties() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 74 | delete mStaticMatrix; |
| 75 | delete mAnimationMatrix; |
| 76 | } |
| 77 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 78 | RenderProperties& 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(); |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 87 | updateClipPath(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 88 | } |
| 89 | return *this; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 92 | void 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 | } |
John Reck | 78ce1c5 | 2014-03-24 15:43:49 -0700 | [diff] [blame] | 126 | ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "", |
| 127 | 0, 0, getWidth(), getHeight(), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 128 | (int)(mPrimitiveFields.mAlpha * 255), flags); |
| 129 | } |
| 130 | } |
| 131 | if (clipToBoundsNeeded) { |
John Reck | 78ce1c5 | 2014-03-24 15:43:49 -0700 | [diff] [blame] | 132 | ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "", |
| 133 | 0, 0, getWidth(), getHeight()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 134 | } |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void RenderProperties::updateMatrix() { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 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(); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 145 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 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; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 155 | if ((mPrimitiveFields.mMatrixFlags & ROTATION_3D) == 0) { |
| 156 | mComputedFields.mTransformMatrix->loadTranslate( |
| 157 | mPrimitiveFields.mPivotX + mPrimitiveFields.mTranslationX, |
| 158 | mPrimitiveFields.mPivotY + mPrimitiveFields.mTranslationY, |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 159 | 0); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 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); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 163 | } else { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 164 | if (!mComputedFields.mTransformCamera) { |
| 165 | mComputedFields.mTransformCamera = new Sk3DView(); |
| 166 | mComputedFields.mTransformMatrix3D = new SkMatrix(); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 167 | } |
| 168 | SkMatrix transformMatrix; |
| 169 | transformMatrix.reset(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 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(); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 181 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 182 | mComputedFields.mTransformMatrix->load(transformMatrix); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 185 | mPrimitiveFields.mMatrixDirty = false; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 189 | void 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 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 223 | } /* namespace uirenderer */ |
| 224 | } /* namespace android */ |