Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 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 | */ |
| 16 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 17 | #include "Layer.h" |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 18 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 19 | #include "renderstate/RenderState.h" |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 20 | #include "utils/Color.h" |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 21 | #include "utils/MathUtils.h" |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 22 | |
Kevin Lubick | 4e8ce46 | 2022-12-01 20:29:16 +0000 | [diff] [blame^] | 23 | #include <SkBlendMode.h> |
| 24 | |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 25 | #include <log/log.h> |
| 26 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 30 | Layer::Layer(RenderState& renderState, sk_sp<SkColorFilter> colorFilter, int alpha, |
| 31 | SkBlendMode mode) |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 32 | : mRenderState(renderState) |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 33 | , mColorFilter(colorFilter) |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 34 | , alpha(alpha) |
| 35 | , mode(mode) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 36 | // TODO: This is a violation of Android's typical ref counting, but it |
| 37 | // preserves the old inc/dec ref locations. This should be changed... |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 38 | incStrong(nullptr); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 39 | renderState.registerLayer(this); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 40 | transform.setIdentity(); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 43 | Layer::~Layer() { |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 44 | mRenderState.unregisterLayer(this); |
John Reck | 5799801 | 2015-01-29 10:17:57 -0800 | [diff] [blame] | 45 | } |
| 46 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 47 | void Layer::postDecStrong() { |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 48 | mRenderState.postDecStrong(this); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Stan Iliev | 8fc3d8e | 2018-09-28 18:44:26 -0400 | [diff] [blame] | 51 | SkBlendMode Layer::getMode() const { |
| 52 | if (mBlend || mode != SkBlendMode::kSrcOver) { |
| 53 | return mode; |
| 54 | } else { |
| 55 | return SkBlendMode::kSrc; |
| 56 | } |
| 57 | } |
| 58 | |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 59 | static inline SkScalar isIntegerAligned(SkScalar x) { |
John Reck | 9a7c192 | 2021-02-08 19:32:21 -0500 | [diff] [blame] | 60 | return MathUtils::isZero(roundf(x) - x); |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Disable filtering when there is no scaling in screen coordinates and the corners have the same |
| 64 | // fraction (for translate) or zero fraction (for any other rect-to-rect transform). |
| 65 | static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) { |
| 66 | if (!matrix.rectStaysRect()) return true; |
| 67 | SkRect dstDevRect = matrix.mapRect(dstRect); |
| 68 | float dstW, dstH; |
| 69 | if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) { |
| 70 | // Has a 90 or 270 degree rotation, although total matrix may also have scale factors |
| 71 | // in m10 and m01. Those scalings are automatically handled by mapRect so comparing |
| 72 | // dimensions is sufficient, but swap width and height comparison. |
| 73 | dstW = dstDevRect.height(); |
| 74 | dstH = dstDevRect.width(); |
| 75 | } else { |
| 76 | // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but |
| 77 | // dimensions are still safe to compare directly. |
| 78 | dstW = dstDevRect.width(); |
| 79 | dstH = dstDevRect.height(); |
| 80 | } |
| 81 | if (!(MathUtils::areEqual(dstW, srcRect.width()) && |
| 82 | MathUtils::areEqual(dstH, srcRect.height()))) { |
| 83 | return true; |
| 84 | } |
| 85 | // Device rect and source rect should be integer aligned to ensure there's no difference |
| 86 | // in how nearest-neighbor sampling is resolved. |
| 87 | return !(isIntegerAligned(srcRect.x()) && |
| 88 | isIntegerAligned(srcRect.y()) && |
| 89 | isIntegerAligned(dstDevRect.x()) && |
| 90 | isIntegerAligned(dstDevRect.y())); |
| 91 | } |
| 92 | |
| 93 | void Layer::draw(SkCanvas* canvas) { |
| 94 | GrRecordingContext* context = canvas->recordingContext(); |
| 95 | if (context == nullptr) { |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 96 | ALOGD("Attempting to draw LayerDrawable into an unsupported surface"); |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | SkMatrix layerTransform = getTransform(); |
| 100 | //sk_sp<SkImage> layerImage = getImage(); |
| 101 | const int layerWidth = getWidth(); |
| 102 | const int layerHeight = getHeight(); |
| 103 | if (layerImage) { |
| 104 | SkMatrix textureMatrixInv; |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 105 | // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed |
| 106 | // use bottom left origin and remove flipV and invert transformations. |
| 107 | SkMatrix flipV; |
| 108 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
| 109 | textureMatrixInv.preConcat(flipV); |
| 110 | textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight); |
| 111 | textureMatrixInv.postScale(layerImage->width(), layerImage->height()); |
| 112 | SkMatrix textureMatrix; |
| 113 | if (!textureMatrixInv.invert(&textureMatrix)) { |
| 114 | textureMatrix = textureMatrixInv; |
| 115 | } |
| 116 | |
| 117 | SkMatrix matrix; |
| 118 | matrix = SkMatrix::Concat(layerTransform, textureMatrix); |
| 119 | |
| 120 | SkPaint paint; |
| 121 | paint.setAlpha(getAlpha()); |
| 122 | paint.setBlendMode(getMode()); |
| 123 | paint.setColorFilter(getColorFilter()); |
| 124 | const bool nonIdentityMatrix = !matrix.isIdentity(); |
| 125 | if (nonIdentityMatrix) { |
| 126 | canvas->save(); |
| 127 | canvas->concat(matrix); |
| 128 | } |
| 129 | const SkMatrix& totalMatrix = canvas->getTotalMatrix(); |
| 130 | |
| 131 | SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height()); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 132 | SkSamplingOptions sampling; |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 133 | if (getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) { |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 134 | sampling = SkSamplingOptions(SkFilterMode::kLinear); |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 135 | } |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 136 | canvas->drawImage(layerImage.get(), 0, 0, sampling, &paint); |
Nader Jawad | 55f1976 | 2020-11-25 15:30:20 -0800 | [diff] [blame] | 137 | // restore the original matrix |
| 138 | if (nonIdentityMatrix) { |
| 139 | canvas->restore(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 144 | } // namespace uirenderer |
| 145 | } // namespace android |