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