Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #include "LayerDrawable.h" |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 18 | #include <utils/MathUtils.h> |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 19 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 20 | #include "GrBackendSurface.h" |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 21 | #include "Matrix.h" |
Derek Sollenberger | f87da67 | 2016-11-02 11:34:27 -0400 | [diff] [blame] | 22 | #include "SkColorFilter.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 23 | #include "SkSurface.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 24 | #include "gl/GrGLTypes.h" |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 25 | #include "system/window.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | namespace skiapipeline { |
| 30 | |
| 31 | void LayerDrawable::onDraw(SkCanvas* canvas) { |
Derek Sollenberger | f5a370e | 2017-06-15 13:50:08 -0400 | [diff] [blame] | 32 | Layer* layer = mLayerUpdater->backingLayer(); |
| 33 | if (layer) { |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 34 | SkRect srcRect = layer->getCropRect(); |
| 35 | DrawLayer(canvas->recordingContext(), canvas, layer, &srcRect, nullptr, true); |
Derek Sollenberger | f5a370e | 2017-06-15 13:50:08 -0400 | [diff] [blame] | 36 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Stan Iliev | 694f3e4 | 2019-07-29 17:00:49 -0400 | [diff] [blame] | 39 | static inline SkScalar isIntegerAligned(SkScalar x) { |
John Reck | 9a7c192 | 2021-02-08 19:32:21 -0500 | [diff] [blame] | 40 | return MathUtils::isZero(roundf(x) - x); |
Stan Iliev | 694f3e4 | 2019-07-29 17:00:49 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 43 | // Disable filtering when there is no scaling in screen coordinates and the corners have the same |
| 44 | // fraction (for translate) or zero fraction (for any other rect-to-rect transform). |
| 45 | static bool shouldFilterRect(const SkMatrix& matrix, const SkRect& srcRect, const SkRect& dstRect) { |
| 46 | if (!matrix.rectStaysRect()) return true; |
| 47 | SkRect dstDevRect = matrix.mapRect(dstRect); |
| 48 | float dstW, dstH; |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 49 | if (MathUtils::isZero(matrix.getScaleX()) && MathUtils::isZero(matrix.getScaleY())) { |
| 50 | // Has a 90 or 270 degree rotation, although total matrix may also have scale factors |
| 51 | // in m10 and m01. Those scalings are automatically handled by mapRect so comparing |
| 52 | // dimensions is sufficient, but swap width and height comparison. |
| 53 | dstW = dstDevRect.height(); |
| 54 | dstH = dstDevRect.width(); |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 55 | } else { |
| 56 | // Handle H/V flips or 180 rotation matrices. Axes may have been mirrored, but |
| 57 | // dimensions are still safe to compare directly. |
| 58 | dstW = dstDevRect.width(); |
| 59 | dstH = dstDevRect.height(); |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 60 | } |
| 61 | if (!(MathUtils::areEqual(dstW, srcRect.width()) && |
| 62 | MathUtils::areEqual(dstH, srcRect.height()))) { |
| 63 | return true; |
| 64 | } |
Stan Iliev | 019adb0 | 2019-09-26 14:29:48 -0400 | [diff] [blame] | 65 | // Device rect and source rect should be integer aligned to ensure there's no difference |
| 66 | // in how nearest-neighbor sampling is resolved. |
| 67 | return !(isIntegerAligned(srcRect.x()) && |
| 68 | isIntegerAligned(srcRect.y()) && |
| 69 | isIntegerAligned(dstDevRect.x()) && |
| 70 | isIntegerAligned(dstDevRect.y())); |
John Reck | 0aff62d | 2018-11-26 16:41:34 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 73 | // TODO: Context arg probably doesn't belong here – do debug check at callsite instead. |
| 74 | bool LayerDrawable::DrawLayer(GrRecordingContext* context, |
| 75 | SkCanvas* canvas, |
| 76 | Layer* layer, |
| 77 | const SkRect* srcRect, |
| 78 | const SkRect* dstRect, |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 79 | bool useLayerTransform) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 80 | if (context == nullptr) { |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 81 | ALOGD("Attempting to draw LayerDrawable into an unsupported surface"); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 82 | return false; |
| 83 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 84 | // transform the matrix based on the layer |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 85 | const uint32_t transform = layer->getTextureTransform(); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 86 | sk_sp<SkImage> layerImage = layer->getImage(); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 87 | const int layerWidth = layer->getWidth(); |
| 88 | const int layerHeight = layer->getHeight(); |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 89 | SkMatrix layerTransform = layer->getTransform(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 90 | if (layerImage) { |
Stan Iliev | aac878f | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 91 | SkMatrix matrix; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 92 | if (useLayerTransform) { |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 93 | matrix = layerTransform; |
Stan Iliev | aac878f | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 94 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 95 | SkPaint paint; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 96 | paint.setAlpha(layer->getAlpha()); |
| 97 | paint.setBlendMode(layer->getMode()); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 98 | paint.setColorFilter(layer->getColorFilter()); |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 99 | const bool nonIdentityMatrix = !matrix.isIdentity(); |
| 100 | if (nonIdentityMatrix) { |
| 101 | canvas->save(); |
| 102 | canvas->concat(matrix); |
| 103 | } |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 104 | const SkMatrix totalMatrix = canvas->getTotalMatrix(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 105 | if (dstRect || srcRect) { |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 106 | SkMatrix matrixInv; |
| 107 | if (!matrix.invert(&matrixInv)) { |
| 108 | matrixInv = matrix; |
| 109 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 110 | SkRect skiaSrcRect; |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 111 | if (srcRect && !srcRect->isEmpty()) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 112 | skiaSrcRect = *srcRect; |
| 113 | } else { |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 114 | skiaSrcRect = (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) |
| 115 | ? SkRect::MakeIWH(layerHeight, layerWidth) |
| 116 | : SkRect::MakeIWH(layerWidth, layerHeight); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 117 | } |
| 118 | matrixInv.mapRect(&skiaSrcRect); |
| 119 | SkRect skiaDestRect; |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 120 | if (dstRect && !dstRect->isEmpty()) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 121 | skiaDestRect = *dstRect; |
| 122 | } else { |
| 123 | skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight); |
| 124 | } |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 125 | matrixInv.mapRect(&skiaDestRect); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 126 | SkSamplingOptions sampling(SkFilterMode::kNearest); |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 127 | if (layer->getForceFilter() || |
| 128 | shouldFilterRect(totalMatrix, skiaSrcRect, skiaDestRect)) { |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 129 | sampling = SkSamplingOptions(SkFilterMode::kLinear); |
Stan Iliev | aa0a331 | 2018-10-19 15:26:08 -0400 | [diff] [blame] | 130 | } |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 131 | |
| 132 | const float px = skiaDestRect.centerX(); |
| 133 | const float py = skiaDestRect.centerY(); |
| 134 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 135 | matrix.postScale(-1.f, 1.f, px, py); |
| 136 | } |
| 137 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 138 | matrix.postScale(1.f, -1.f, px, py); |
| 139 | } |
| 140 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 141 | matrix.postRotate(90, 0, 0); |
| 142 | matrix.postTranslate(skiaDestRect.height(), 0); |
| 143 | } |
| 144 | auto constraint = SkCanvas::kFast_SrcRectConstraint; |
| 145 | if (srcRect && srcRect->isEmpty()) { |
| 146 | constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 147 | } |
| 148 | matrix.postConcat(SkMatrix::MakeRectToRect(skiaSrcRect, skiaDestRect, |
| 149 | SkMatrix::kFill_ScaleToFit)); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 150 | canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, sampling, &paint, |
ramindani | ef54c03 | 2021-07-16 22:33:25 +0000 | [diff] [blame^] | 151 | constraint); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 152 | } else { |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 153 | SkRect imageRect = SkRect::MakeIWH(layerImage->width(), layerImage->height()); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 154 | SkSamplingOptions sampling(SkFilterMode::kNearest); |
Stan Iliev | 134372d | 2019-07-10 16:46:09 -0400 | [diff] [blame] | 155 | if (layer->getForceFilter() || shouldFilterRect(totalMatrix, imageRect, imageRect)) { |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 156 | sampling = SkSamplingOptions(SkFilterMode::kLinear); |
Stan Iliev | aa0a331 | 2018-10-19 15:26:08 -0400 | [diff] [blame] | 157 | } |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 158 | canvas->drawImage(layerImage.get(), 0, 0, sampling, &paint); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 159 | } |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 160 | // restore the original matrix |
| 161 | if (nonIdentityMatrix) { |
| 162 | canvas->restore(); |
| 163 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 164 | } |
Ben Wagner | 6b62ac0 | 2018-05-29 14:16:02 -0400 | [diff] [blame] | 165 | return layerImage != nullptr; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 168 | } // namespace skiapipeline |
| 169 | } // namespace uirenderer |
| 170 | } // namespace android |