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