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