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 | |
John Reck | 0aff62d | 2018-11-26 16:41:34 -0800 | [diff] [blame^] | 17 | #include <utils/MathUtils.h> |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 18 | #include "LayerDrawable.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) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 32 | DrawLayer(canvas->getGrContext(), 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 | |
John Reck | 0aff62d | 2018-11-26 16:41:34 -0800 | [diff] [blame^] | 36 | // This is a less-strict matrix.isTranslate() that will still report being translate-only |
| 37 | // on imperceptibly small scaleX & scaleY values. |
| 38 | static bool isBasicallyTranslate(const SkMatrix& matrix) { |
| 39 | if (!matrix.isScaleTranslate()) return false; |
| 40 | return MathUtils::isOne(matrix.getScaleX()) && MathUtils::isOne(matrix.getScaleY()); |
| 41 | } |
| 42 | |
| 43 | static bool shouldFilter(const SkMatrix& matrix) { |
| 44 | if (!matrix.isScaleTranslate()) return true; |
| 45 | |
| 46 | // We only care about meaningful scale here |
| 47 | bool noScale = MathUtils::isOne(matrix.getScaleX()) |
| 48 | && MathUtils::isOne(matrix.getScaleY()); |
| 49 | bool pixelAligned = SkScalarIsInt(matrix.getTranslateX()) |
| 50 | && SkScalarIsInt(matrix.getTranslateY()); |
| 51 | return !(noScale && pixelAligned); |
| 52 | } |
| 53 | |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 54 | bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer, |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 55 | const SkRect* srcRect, const SkRect* dstRect, |
| 56 | bool useLayerTransform) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 57 | if (context == nullptr) { |
| 58 | SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface")); |
| 59 | return false; |
| 60 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 61 | // transform the matrix based on the layer |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 62 | SkMatrix layerTransform = layer->getTransform(); |
| 63 | sk_sp<SkImage> layerImage = layer->getImage(); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 64 | const int layerWidth = layer->getWidth(); |
| 65 | const int layerHeight = layer->getHeight(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 66 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 67 | if (layerImage) { |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 68 | SkMatrix textureMatrixInv; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 69 | textureMatrixInv = layer->getTexTransform(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 70 | // 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] | 71 | // use bottom left origin and remove flipV and invert transformations. |
| 72 | SkMatrix flipV; |
| 73 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 74 | textureMatrixInv.preConcat(flipV); |
| 75 | textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 76 | textureMatrixInv.postScale(layerImage->width(), layerImage->height()); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 77 | SkMatrix textureMatrix; |
| 78 | if (!textureMatrixInv.invert(&textureMatrix)) { |
| 79 | textureMatrix = textureMatrixInv; |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Stan Iliev | aac878f | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 82 | SkMatrix matrix; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 83 | if (useLayerTransform) { |
Stan Iliev | aac878f | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 84 | matrix = SkMatrix::Concat(layerTransform, textureMatrix); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 85 | } else { |
| 86 | matrix = textureMatrix; |
Stan Iliev | aac878f | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 87 | } |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 88 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 89 | SkPaint paint; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 90 | paint.setAlpha(layer->getAlpha()); |
| 91 | paint.setBlendMode(layer->getMode()); |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 92 | paint.setColorFilter(layer->getColorSpaceWithFilter()); |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 93 | const bool nonIdentityMatrix = !matrix.isIdentity(); |
| 94 | if (nonIdentityMatrix) { |
| 95 | canvas->save(); |
| 96 | canvas->concat(matrix); |
| 97 | } |
Stan Iliev | aa0a331 | 2018-10-19 15:26:08 -0400 | [diff] [blame] | 98 | const SkMatrix& totalMatrix = canvas->getTotalMatrix(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 99 | if (dstRect || srcRect) { |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 100 | SkMatrix matrixInv; |
| 101 | if (!matrix.invert(&matrixInv)) { |
| 102 | matrixInv = matrix; |
| 103 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 104 | SkRect skiaSrcRect; |
| 105 | if (srcRect) { |
| 106 | skiaSrcRect = *srcRect; |
| 107 | } else { |
| 108 | skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight); |
| 109 | } |
| 110 | matrixInv.mapRect(&skiaSrcRect); |
| 111 | SkRect skiaDestRect; |
| 112 | if (dstRect) { |
| 113 | skiaDestRect = *dstRect; |
| 114 | } else { |
| 115 | skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight); |
| 116 | } |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 117 | matrixInv.mapRect(&skiaDestRect); |
Stan Iliev | aa0a331 | 2018-10-19 15:26:08 -0400 | [diff] [blame] | 118 | // If (matrix is identity or an integer translation) and (src/dst buffers size match), |
| 119 | // then use nearest neighbor, otherwise use bilerp sampling. |
| 120 | // Integer translation is defined as when src rect and dst rect align fractionally. |
| 121 | // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works |
| 122 | // only for SrcOver blending and without color filter (readback uses Src blending). |
John Reck | 0aff62d | 2018-11-26 16:41:34 -0800 | [diff] [blame^] | 123 | bool isIntegerTranslate = isBasicallyTranslate(totalMatrix) |
Stan Iliev | aa0a331 | 2018-10-19 15:26:08 -0400 | [diff] [blame] | 124 | && SkScalarFraction(skiaDestRect.fLeft + totalMatrix[SkMatrix::kMTransX]) |
| 125 | == SkScalarFraction(skiaSrcRect.fLeft) |
| 126 | && SkScalarFraction(skiaDestRect.fTop + totalMatrix[SkMatrix::kMTransY]) |
| 127 | == SkScalarFraction(skiaSrcRect.fTop); |
| 128 | if (layer->getForceFilter() || !isIntegerTranslate) { |
| 129 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 130 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 131 | canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint, |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 132 | SkCanvas::kFast_SrcRectConstraint); |
| 133 | } else { |
John Reck | 0aff62d | 2018-11-26 16:41:34 -0800 | [diff] [blame^] | 134 | if (layer->getForceFilter() || shouldFilter(totalMatrix)) { |
Stan Iliev | aa0a331 | 2018-10-19 15:26:08 -0400 | [diff] [blame] | 135 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 136 | } |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 137 | canvas->drawImage(layerImage.get(), 0, 0, &paint); |
| 138 | } |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 139 | // restore the original matrix |
| 140 | if (nonIdentityMatrix) { |
| 141 | canvas->restore(); |
| 142 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 143 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 144 | |
Ben Wagner | 6b62ac0 | 2018-05-29 14:16:02 -0400 | [diff] [blame] | 145 | return layerImage != nullptr; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 146 | } |
| 147 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 148 | }; // namespace skiapipeline |
| 149 | }; // namespace uirenderer |
| 150 | }; // namespace android |