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 | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 18 | #include "GlLayer.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 19 | #include "VkLayer.h" |
| 20 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 21 | #include "GrBackendSurface.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" |
| 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) { |
| 33 | DrawLayer(canvas->getGrContext(), canvas, layer); |
| 34 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 37 | bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer, |
| 38 | const SkRect* dstRect) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 39 | if (context == nullptr) { |
| 40 | SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface")); |
| 41 | return false; |
| 42 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 43 | // transform the matrix based on the layer |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 44 | SkMatrix layerTransform; |
| 45 | layer->getTransform().copyTo(layerTransform); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 46 | sk_sp<SkImage> layerImage; |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 47 | const int layerWidth = layer->getWidth(); |
| 48 | const int layerHeight = layer->getHeight(); |
Stan Iliev | 93ac401 | 2018-09-10 14:48:38 -0400 | [diff] [blame] | 49 | const int bufferWidth = layer->getBufferWidth(); |
| 50 | const int bufferHeight = layer->getBufferHeight(); |
| 51 | if (bufferWidth <= 0 || bufferHeight <=0) { |
| 52 | return false; |
| 53 | } |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 54 | if (layer->getApi() == Layer::Api::OpenGL) { |
| 55 | GlLayer* glLayer = static_cast<GlLayer*>(layer); |
| 56 | GrGLTextureInfo externalTexture; |
| 57 | externalTexture.fTarget = glLayer->getRenderTarget(); |
| 58 | externalTexture.fID = glLayer->getTextureId(); |
Derek Sollenberger | 551d08e | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 59 | // The format may not be GL_RGBA8, but given the DeferredLayerUpdater and GLConsumer don't |
| 60 | // expose that info we use it as our default. Further, given that we only use this texture |
| 61 | // as a source this will not impact how Skia uses the texture. The only potential affect |
| 62 | // this is anticipated to have is that for some format types if we are not bound as an OES |
| 63 | // texture we may get invalid results for SKP capture if we read back the texture. |
| 64 | externalTexture.fFormat = GL_RGBA8; |
Stan Iliev | 93ac401 | 2018-09-10 14:48:38 -0400 | [diff] [blame] | 65 | GrBackendTexture backendTexture(bufferWidth, bufferHeight, GrMipMapped::kNo, externalTexture); |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 66 | layerImage = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 67 | kPremul_SkAlphaType, nullptr); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 68 | } else { |
| 69 | SkASSERT(layer->getApi() == Layer::Api::Vulkan); |
| 70 | VkLayer* vkLayer = static_cast<VkLayer*>(layer); |
| 71 | canvas->clear(SK_ColorGREEN); |
| 72 | layerImage = vkLayer->getImage(); |
| 73 | } |
| 74 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 75 | if (layerImage) { |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 76 | SkMatrix textureMatrixInv; |
| 77 | layer->getTexTransform().copyTo(textureMatrixInv); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | // 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] | 79 | // use bottom left origin and remove flipV and invert transformations. |
| 80 | SkMatrix flipV; |
| 81 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 82 | textureMatrixInv.preConcat(flipV); |
| 83 | textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight); |
Stan Iliev | 93ac401 | 2018-09-10 14:48:38 -0400 | [diff] [blame] | 84 | textureMatrixInv.postScale(bufferWidth, bufferHeight); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 85 | SkMatrix textureMatrix; |
| 86 | if (!textureMatrixInv.invert(&textureMatrix)) { |
| 87 | textureMatrix = textureMatrixInv; |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Stan Iliev | ff129ae | 2018-07-12 16:53:59 -0400 | [diff] [blame] | 90 | SkMatrix matrix; |
| 91 | if (dstRect) { |
| 92 | // Destination rectangle is set only when we are trying to read back the content |
| 93 | // of the layer. In this case we don't want to apply layer transform. |
| 94 | matrix = textureMatrix; |
| 95 | } else { |
| 96 | matrix = SkMatrix::Concat(layerTransform, textureMatrix); |
| 97 | } |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [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 | 551d08e | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 102 | paint.setColorFilter(layer->getColorSpaceWithFilter()); |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 103 | |
| 104 | const bool nonIdentityMatrix = !matrix.isIdentity(); |
| 105 | if (nonIdentityMatrix) { |
| 106 | canvas->save(); |
| 107 | canvas->concat(matrix); |
| 108 | } |
Stan Iliev | 402fff1 | 2018-10-25 16:28:27 -0400 | [diff] [blame] | 109 | const SkMatrix& totalMatrix = canvas->getTotalMatrix(); |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 110 | if (dstRect) { |
| 111 | SkMatrix matrixInv; |
| 112 | if (!matrix.invert(&matrixInv)) { |
| 113 | matrixInv = matrix; |
| 114 | } |
| 115 | SkRect srcRect = SkRect::MakeIWH(layerWidth, layerHeight); |
| 116 | matrixInv.mapRect(&srcRect); |
| 117 | SkRect skiaDestRect = *dstRect; |
| 118 | matrixInv.mapRect(&skiaDestRect); |
Stan Iliev | 402fff1 | 2018-10-25 16:28:27 -0400 | [diff] [blame] | 119 | // If (matrix is identity or an integer translation) and (src/dst buffers size match), |
| 120 | // then use nearest neighbor, otherwise use bilerp sampling. |
| 121 | // Integer translation is defined as when src rect and dst rect align fractionally. |
| 122 | // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works |
| 123 | // only for SrcOver blending and without color filter (readback uses Src blending). |
| 124 | bool isIntegerTranslate = totalMatrix.isTranslate() |
| 125 | && SkScalarFraction(skiaDestRect.fLeft + totalMatrix[SkMatrix::kMTransX]) |
| 126 | == SkScalarFraction(srcRect.fLeft) |
| 127 | && SkScalarFraction(skiaDestRect.fTop + totalMatrix[SkMatrix::kMTransY]) |
| 128 | == SkScalarFraction(srcRect.fTop); |
| 129 | if (layer->getForceFilter() || !isIntegerTranslate) { |
| 130 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 131 | } |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 132 | canvas->drawImageRect(layerImage.get(), srcRect, skiaDestRect, &paint, |
| 133 | SkCanvas::kFast_SrcRectConstraint); |
| 134 | } else { |
Stan Iliev | 402fff1 | 2018-10-25 16:28:27 -0400 | [diff] [blame] | 135 | bool isIntegerTranslate = totalMatrix.isTranslate() |
| 136 | && SkScalarIsInt(totalMatrix[SkMatrix::kMTransX]) |
| 137 | && SkScalarIsInt(totalMatrix[SkMatrix::kMTransY]); |
| 138 | if (layer->getForceFilter() || !isIntegerTranslate) { |
| 139 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 140 | } |
Leon Scroggins III | 1a12ab2 | 2018-03-26 15:00:49 -0400 | [diff] [blame] | 141 | canvas->drawImage(layerImage.get(), 0, 0, &paint); |
| 142 | } |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 143 | // restore the original matrix |
| 144 | if (nonIdentityMatrix) { |
| 145 | canvas->restore(); |
| 146 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 147 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 148 | |
| 149 | return layerImage; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 150 | } |
| 151 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 152 | }; // namespace skiapipeline |
| 153 | }; // namespace uirenderer |
| 154 | }; // namespace android |