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