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