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" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 18 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 19 | #include "GrBackendSurface.h" |
Derek Sollenberger | f87da67 | 2016-11-02 11:34:27 -0400 | [diff] [blame] | 20 | #include "SkColorFilter.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 21 | #include "SkSurface.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 22 | #include "gl/GrGLTypes.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | namespace skiapipeline { |
| 27 | |
| 28 | void LayerDrawable::onDraw(SkCanvas* canvas) { |
Derek Sollenberger | f5a370e | 2017-06-15 13:50:08 -0400 | [diff] [blame] | 29 | Layer* layer = mLayerUpdater->backingLayer(); |
| 30 | if (layer) { |
| 31 | DrawLayer(canvas->getGrContext(), canvas, layer); |
| 32 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 35 | bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer, |
| 36 | const SkRect* dstRect) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 37 | if (context == nullptr) { |
| 38 | SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface")); |
| 39 | return false; |
| 40 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 41 | // transform the matrix based on the layer |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 42 | SkMatrix layerTransform = layer->getTransform(); |
| 43 | sk_sp<SkImage> layerImage = layer->getImage(); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 44 | const int layerWidth = layer->getWidth(); |
| 45 | const int layerHeight = layer->getHeight(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 46 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 47 | if (layerImage) { |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 48 | SkMatrix textureMatrixInv; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 49 | textureMatrixInv = layer->getTexTransform(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 50 | // 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] | 51 | // use bottom left origin and remove flipV and invert transformations. |
| 52 | SkMatrix flipV; |
| 53 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 54 | textureMatrixInv.preConcat(flipV); |
| 55 | textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 56 | textureMatrixInv.postScale(layerImage->width(), layerImage->height()); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 57 | SkMatrix textureMatrix; |
| 58 | if (!textureMatrixInv.invert(&textureMatrix)) { |
| 59 | textureMatrix = textureMatrixInv; |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Stan Iliev | aac878f | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 62 | SkMatrix matrix; |
| 63 | if (dstRect) { |
| 64 | // Destination rectangle is set only when we are trying to read back the content |
| 65 | // of the layer. In this case we don't want to apply layer transform. |
| 66 | matrix = textureMatrix; |
| 67 | } else { |
| 68 | matrix = SkMatrix::Concat(layerTransform, textureMatrix); |
| 69 | } |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 70 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 71 | SkPaint paint; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 72 | paint.setAlpha(layer->getAlpha()); |
| 73 | paint.setBlendMode(layer->getMode()); |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 74 | paint.setColorFilter(layer->getColorSpaceWithFilter()); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 75 | if (layer->getForceFilter()) { |
| 76 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 77 | } |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 78 | |
| 79 | const bool nonIdentityMatrix = !matrix.isIdentity(); |
| 80 | if (nonIdentityMatrix) { |
| 81 | canvas->save(); |
| 82 | canvas->concat(matrix); |
| 83 | } |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 84 | if (dstRect) { |
| 85 | SkMatrix matrixInv; |
| 86 | if (!matrix.invert(&matrixInv)) { |
| 87 | matrixInv = matrix; |
| 88 | } |
| 89 | SkRect srcRect = SkRect::MakeIWH(layerWidth, layerHeight); |
| 90 | matrixInv.mapRect(&srcRect); |
| 91 | SkRect skiaDestRect = *dstRect; |
| 92 | matrixInv.mapRect(&skiaDestRect); |
| 93 | canvas->drawImageRect(layerImage.get(), srcRect, skiaDestRect, &paint, |
| 94 | SkCanvas::kFast_SrcRectConstraint); |
| 95 | } else { |
| 96 | canvas->drawImage(layerImage.get(), 0, 0, &paint); |
| 97 | } |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 98 | // restore the original matrix |
| 99 | if (nonIdentityMatrix) { |
| 100 | canvas->restore(); |
| 101 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 102 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 103 | |
Ben Wagner | 6b62ac0 | 2018-05-29 14:16:02 -0400 | [diff] [blame] | 104 | return layerImage != nullptr; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 105 | } |
| 106 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 107 | }; // namespace skiapipeline |
| 108 | }; // namespace uirenderer |
| 109 | }; // namespace android |