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 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 17 | #include "RenderProperties.h" |
| 18 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 19 | #include <utils/Trace.h> |
| 20 | |
| 21 | #include <SkCanvas.h> |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 22 | #include <SkColorFilter.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 23 | #include <SkMatrix.h> |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 24 | #include <SkPath.h> |
| 25 | #include <SkPathOps.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 26 | |
| 27 | #include "Matrix.h" |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 28 | #include "OpenGLRenderer.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 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 34 | LayerProperties::LayerProperties() { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 35 | reset(); |
| 36 | } |
| 37 | |
| 38 | LayerProperties::~LayerProperties() { |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 39 | setType(LayerType::None); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void LayerProperties::reset() { |
| 43 | mOpaque = false; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 44 | setFromPaint(nullptr); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool LayerProperties::setColorFilter(SkColorFilter* filter) { |
| 48 | if (mColorFilter == filter) return false; |
| 49 | SkRefCnt_SafeAssign(mColorFilter, filter); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool LayerProperties::setFromPaint(const SkPaint* paint) { |
| 54 | bool changed = false; |
| 55 | SkXfermode::Mode mode; |
| 56 | int alpha; |
| 57 | OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode); |
| 58 | changed |= setAlpha(static_cast<uint8_t>(alpha)); |
| 59 | changed |= setXferMode(mode); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 60 | changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 61 | return changed; |
| 62 | } |
| 63 | |
| 64 | LayerProperties& LayerProperties::operator=(const LayerProperties& other) { |
| 65 | setType(other.type()); |
| 66 | setOpaque(other.opaque()); |
| 67 | setAlpha(other.alpha()); |
| 68 | setXferMode(other.xferMode()); |
| 69 | setColorFilter(other.colorFilter()); |
| 70 | return *this; |
| 71 | } |
| 72 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 73 | RenderProperties::ComputedFields::ComputedFields() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 74 | : mTransformMatrix(nullptr) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | RenderProperties::ComputedFields::~ComputedFields() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 78 | delete mTransformMatrix; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | RenderProperties::RenderProperties() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 82 | : mStaticMatrix(nullptr) |
| 83 | , mAnimationMatrix(nullptr) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | RenderProperties::~RenderProperties() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 87 | delete mStaticMatrix; |
| 88 | delete mAnimationMatrix; |
| 89 | } |
| 90 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 91 | RenderProperties& RenderProperties::operator=(const RenderProperties& other) { |
| 92 | if (this != &other) { |
| 93 | mPrimitiveFields = other.mPrimitiveFields; |
| 94 | setStaticMatrix(other.getStaticMatrix()); |
| 95 | setAnimationMatrix(other.getAnimationMatrix()); |
| 96 | setCameraDistance(other.getCameraDistance()); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 97 | mLayerProperties = other.layerProperties(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 98 | |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 99 | // 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] | 100 | mPrimitiveFields.mMatrixOrPivotDirty = true; |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 101 | updateMatrix(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 102 | } |
| 103 | return *this; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 104 | } |
| 105 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 106 | void RenderProperties::debugOutputProperties(const int level) const { |
| 107 | if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) { |
| 108 | ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop); |
| 109 | } |
| 110 | if (mStaticMatrix) { |
| 111 | ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING, |
| 112 | level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix)); |
| 113 | } |
| 114 | if (mAnimationMatrix) { |
| 115 | ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING, |
| 116 | level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix)); |
| 117 | } |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 118 | if (hasTransformMatrix()) { |
| 119 | if (isTransformTranslateOnly()) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 120 | ALOGD("%*sTranslate %.2f, %.2f, %.2f", |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 121 | level * 2, "", getTranslationX(), getTranslationY(), getZ()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 122 | } else { |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 123 | ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING, |
| 124 | level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix)); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 128 | const bool isLayer = effectiveLayerType() != LayerType::None; |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 129 | int clipFlags = getClippingFlags(); |
Chris Craik | 43a1d31 | 2015-05-27 11:28:14 -0700 | [diff] [blame] | 130 | if (mPrimitiveFields.mAlpha < 1 |
| 131 | && !MathUtils::isZero(mPrimitiveFields.mAlpha)) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 132 | if (isLayer) { |
| 133 | clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 134 | } |
Chris Craik | 8df5ffa | 2015-04-28 17:47:20 -0700 | [diff] [blame] | 135 | |
Chris Craik | 4e9d9b2 | 2015-06-12 11:07:23 -0700 | [diff] [blame] | 136 | if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) { |
| 137 | // simply scale rendering content's alpha |
| 138 | ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha); |
| 139 | } else { |
| 140 | // savelayeralpha to create an offscreen buffer to apply alpha |
| 141 | Rect layerBounds(0, 0, getWidth(), getHeight()); |
| 142 | if (clipFlags) { |
| 143 | getClippingRectForFlags(clipFlags, &layerBounds); |
| 144 | clipFlags = 0; // all clipping done by savelayer |
| 145 | } |
| 146 | ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "", |
| 147 | (int)layerBounds.left, (int)layerBounds.top, |
| 148 | (int)layerBounds.right, (int)layerBounds.bottom, |
| 149 | (int)(mPrimitiveFields.mAlpha * 255), |
| 150 | SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag); |
| 151 | } |
| 152 | |
| 153 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 154 | } |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 155 | if (clipFlags) { |
| 156 | Rect clipRect; |
| 157 | getClippingRectForFlags(clipFlags, &clipRect); |
John Reck | 78ce1c5 | 2014-03-24 15:43:49 -0700 | [diff] [blame] | 158 | ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "", |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 159 | (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 160 | } |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void RenderProperties::updateMatrix() { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 164 | if (mPrimitiveFields.mMatrixOrPivotDirty) { |
| 165 | if (!mComputedFields.mTransformMatrix) { |
| 166 | // only allocate a mPrimitiveFields.matrix if we have a complex transform |
| 167 | mComputedFields.mTransformMatrix = new SkMatrix(); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 168 | } |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 169 | if (!mPrimitiveFields.mPivotExplicitlySet) { |
| 170 | mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f; |
| 171 | mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f; |
| 172 | } |
| 173 | SkMatrix* transform = mComputedFields.mTransformMatrix; |
| 174 | transform->reset(); |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 175 | if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 176 | transform->setTranslate(getTranslationX(), getTranslationY()); |
| 177 | transform->preRotate(getRotation(), getPivotX(), getPivotY()); |
| 178 | transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY()); |
| 179 | } else { |
| 180 | SkMatrix transform3D; |
| 181 | mComputedFields.mTransformCamera.save(); |
| 182 | transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY()); |
| 183 | mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX); |
| 184 | mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY); |
| 185 | mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation); |
| 186 | mComputedFields.mTransformCamera.getMatrix(&transform3D); |
| 187 | transform3D.preTranslate(-getPivotX(), -getPivotY()); |
| 188 | transform3D.postTranslate(getPivotX() + getTranslationX(), |
| 189 | getPivotY() + getTranslationY()); |
| 190 | transform->postConcat(transform3D); |
| 191 | mComputedFields.mTransformCamera.restore(); |
| 192 | } |
| 193 | mPrimitiveFields.mMatrixOrPivotDirty = false; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 197 | } /* namespace uirenderer */ |
| 198 | } /* namespace android */ |