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 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 21 | #include <SkColorFilter.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 22 | #include <SkMatrix.h> |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 23 | #include <SkPath.h> |
| 24 | #include <SkPathOps.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 25 | |
| 26 | #include "Matrix.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 27 | #include "hwui/Canvas.h" |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 28 | #include "utils/MathUtils.h" |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 29 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 33 | LayerProperties::LayerProperties() { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 34 | reset(); |
| 35 | } |
| 36 | |
| 37 | LayerProperties::~LayerProperties() { |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 38 | setType(LayerType::None); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void LayerProperties::reset() { |
| 42 | mOpaque = false; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 43 | setFromPaint(nullptr); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool LayerProperties::setColorFilter(SkColorFilter* filter) { |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 47 | if (mColorFilter.get() == filter) return false; |
| 48 | mColorFilter = sk_ref_sp(filter); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | return true; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Nader Jawad | 390d6e8 | 2020-09-24 21:35:03 -0700 | [diff] [blame] | 52 | bool LayerProperties::setImageFilter(SkImageFilter* imageFilter) { |
| 53 | if(mImageFilter.get() == imageFilter) return false; |
| 54 | mImageFilter = sk_ref_sp(imageFilter); |
| 55 | return true; |
| 56 | } |
| 57 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 58 | bool LayerProperties::setFromPaint(const SkPaint* paint) { |
| 59 | bool changed = false; |
Chris Craik | bf6f0f2 | 2015-10-01 12:36:07 -0700 | [diff] [blame] | 60 | changed |= setAlpha(static_cast<uint8_t>(PaintUtils::getAlphaDirect(paint))); |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 61 | changed |= setXferMode(PaintUtils::getBlendModeDirect(paint)); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 62 | changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 63 | return changed; |
| 64 | } |
| 65 | |
| 66 | LayerProperties& LayerProperties::operator=(const LayerProperties& other) { |
| 67 | setType(other.type()); |
| 68 | setOpaque(other.opaque()); |
| 69 | setAlpha(other.alpha()); |
| 70 | setXferMode(other.xferMode()); |
Ben Wagner | c1a8a46 | 2018-07-12 12:41:28 -0400 | [diff] [blame] | 71 | setColorFilter(other.getColorFilter()); |
Nader Jawad | 390d6e8 | 2020-09-24 21:35:03 -0700 | [diff] [blame] | 72 | setImageFilter(other.getImageFilter()); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 73 | return *this; |
| 74 | } |
| 75 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | RenderProperties::ComputedFields::ComputedFields() : mTransformMatrix(nullptr) {} |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 77 | |
| 78 | RenderProperties::ComputedFields::~ComputedFields() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 79 | delete mTransformMatrix; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 80 | } |
| 81 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 82 | RenderProperties::RenderProperties() : mStaticMatrix(nullptr), mAnimationMatrix(nullptr) {} |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 83 | |
| 84 | RenderProperties::~RenderProperties() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 85 | delete mStaticMatrix; |
| 86 | delete mAnimationMatrix; |
| 87 | } |
| 88 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 89 | RenderProperties& RenderProperties::operator=(const RenderProperties& other) { |
| 90 | if (this != &other) { |
| 91 | mPrimitiveFields = other.mPrimitiveFields; |
| 92 | setStaticMatrix(other.getStaticMatrix()); |
| 93 | setAnimationMatrix(other.getAnimationMatrix()); |
| 94 | setCameraDistance(other.getCameraDistance()); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 95 | mLayerProperties = other.layerProperties(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 96 | |
Chris Craik | 49e6c739 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 97 | // 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] | 98 | mPrimitiveFields.mMatrixOrPivotDirty = true; |
Chris Craik | 49e6c739 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 99 | updateMatrix(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 100 | } |
| 101 | return *this; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 104 | static void dumpMatrix(std::ostream& output, std::string& indent, const char* label, |
| 105 | SkMatrix* matrix) { |
| 106 | if (matrix) { |
| 107 | output << indent << "(" << label << " " << matrix << ": "; |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 108 | output << std::fixed << std::setprecision(2); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 109 | output << "[" << matrix->get(0) << " " << matrix->get(1) << " " << matrix->get(2) << "]"; |
| 110 | output << " [" << matrix->get(3) << " " << matrix->get(4) << " " << matrix->get(5) << "]"; |
| 111 | output << " [" << matrix->get(6) << " " << matrix->get(7) << " " << matrix->get(8) << "]"; |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 112 | output << ")" << std::endl; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void RenderProperties::debugOutputProperties(std::ostream& output, const int level) const { |
| 117 | auto indent = std::string(level * 2, ' '); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 118 | if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 119 | output << indent << "(Translate (left, top) " << mPrimitiveFields.mLeft << ", " |
| 120 | << mPrimitiveFields.mTop << ")" << std::endl; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 121 | } |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 122 | dumpMatrix(output, indent, "ConcatMatrix (static)", mStaticMatrix); |
| 123 | dumpMatrix(output, indent, "ConcatMatrix (animation)", mAnimationMatrix); |
| 124 | |
| 125 | output << std::fixed << std::setprecision(2); |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 126 | if (hasTransformMatrix()) { |
| 127 | if (isTransformTranslateOnly()) { |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 128 | output << indent << "(Translate " << getTranslationX() << ", " << getTranslationY() |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 129 | << ", " << getZ() << ")" << std::endl; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 130 | } else { |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 131 | dumpMatrix(output, indent, "ConcatMatrix ", mComputedFields.mTransformMatrix); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 135 | const bool isLayer = effectiveLayerType() != LayerType::None; |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 136 | int clipFlags = getClippingFlags(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 137 | if (mPrimitiveFields.mAlpha < 1 && !MathUtils::isZero(mPrimitiveFields.mAlpha)) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 138 | if (isLayer) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 139 | clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 140 | } |
Chris Craik | 8df5ffa | 2015-04-28 17:47:20 -0700 | [diff] [blame] | 141 | |
Chris Craik | 4e9d9b2 | 2015-06-12 11:07:23 -0700 | [diff] [blame] | 142 | if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) { |
| 143 | // simply scale rendering content's alpha |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 144 | output << indent << "(ScaleAlpha " << mPrimitiveFields.mAlpha << ")" << std::endl; |
Chris Craik | 4e9d9b2 | 2015-06-12 11:07:23 -0700 | [diff] [blame] | 145 | } else { |
| 146 | // savelayeralpha to create an offscreen buffer to apply alpha |
| 147 | Rect layerBounds(0, 0, getWidth(), getHeight()); |
| 148 | if (clipFlags) { |
| 149 | getClippingRectForFlags(clipFlags, &layerBounds); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 150 | clipFlags = 0; // all clipping done by savelayer |
Chris Craik | 4e9d9b2 | 2015-06-12 11:07:23 -0700 | [diff] [blame] | 151 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 152 | output << indent << "(SaveLayerAlpha " << (int)layerBounds.left << ", " |
| 153 | << (int)layerBounds.top << ", " << (int)layerBounds.right << ", " |
| 154 | << (int)layerBounds.bottom << ", " << (int)(mPrimitiveFields.mAlpha * 255) |
| 155 | << ", 0x" << std::hex << (SaveFlags::HasAlphaLayer | SaveFlags::ClipToLayer) |
| 156 | << ")" << std::dec << std::endl; |
Chris Craik | 4e9d9b2 | 2015-06-12 11:07:23 -0700 | [diff] [blame] | 157 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 158 | } |
Chris Craik | 91eff22 | 2016-02-22 13:39:33 -0800 | [diff] [blame] | 159 | |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 160 | if (clipFlags) { |
| 161 | Rect clipRect; |
| 162 | getClippingRectForFlags(clipFlags, &clipRect); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 163 | output << indent << "(ClipRect " << (int)clipRect.left << ", " << (int)clipRect.top << ", " |
| 164 | << (int)clipRect.right << ", " << (int)clipRect.bottom << ")" << std::endl; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 165 | } |
Chris Craik | 136d1af | 2016-04-04 13:40:39 -0700 | [diff] [blame] | 166 | |
| 167 | if (getRevealClip().willClip()) { |
| 168 | Rect bounds; |
| 169 | getRevealClip().getBounds(&bounds); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 170 | output << indent << "(Clip to reveal clip with bounds " << bounds.left << ", " << bounds.top |
| 171 | << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl; |
Chris Craik | 136d1af | 2016-04-04 13:40:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | auto& outline = mPrimitiveFields.mOutline; |
| 175 | if (outline.getShouldClip()) { |
| 176 | if (outline.isEmpty()) { |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 177 | output << indent << "(Clip to empty outline)"; |
Chris Craik | 136d1af | 2016-04-04 13:40:39 -0700 | [diff] [blame] | 178 | } else if (outline.willClip()) { |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 179 | const Rect& bounds = outline.getBounds(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 180 | output << indent << "(Clip to outline with bounds " << bounds.left << ", " << bounds.top |
| 181 | << ", " << bounds.right << ", " << bounds.bottom << ")" << std::endl; |
Chris Craik | 136d1af | 2016-04-04 13:40:39 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void RenderProperties::updateMatrix() { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 187 | if (mPrimitiveFields.mMatrixOrPivotDirty) { |
| 188 | if (!mComputedFields.mTransformMatrix) { |
| 189 | // only allocate a mPrimitiveFields.matrix if we have a complex transform |
| 190 | mComputedFields.mTransformMatrix = new SkMatrix(); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 191 | } |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 192 | if (!mPrimitiveFields.mPivotExplicitlySet) { |
| 193 | mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f; |
| 194 | mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f; |
| 195 | } |
| 196 | SkMatrix* transform = mComputedFields.mTransformMatrix; |
| 197 | transform->reset(); |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 198 | if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 199 | transform->setTranslate(getTranslationX(), getTranslationY()); |
| 200 | transform->preRotate(getRotation(), getPivotX(), getPivotY()); |
| 201 | transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY()); |
| 202 | } else { |
| 203 | SkMatrix transform3D; |
| 204 | mComputedFields.mTransformCamera.save(); |
| 205 | transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY()); |
| 206 | mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX); |
| 207 | mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY); |
| 208 | mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation); |
| 209 | mComputedFields.mTransformCamera.getMatrix(&transform3D); |
| 210 | transform3D.preTranslate(-getPivotX(), -getPivotY()); |
| 211 | transform3D.postTranslate(getPivotX() + getTranslationX(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 212 | getPivotY() + getTranslationY()); |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 213 | transform->postConcat(transform3D); |
| 214 | mComputedFields.mTransformCamera.restore(); |
| 215 | } |
| 216 | mPrimitiveFields.mMatrixOrPivotDirty = false; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 220 | } /* namespace uirenderer */ |
| 221 | } /* namespace android */ |