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 | |
| 37 | bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 38 | if (context == nullptr) { |
| 39 | SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface")); |
| 40 | return false; |
| 41 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 42 | // transform the matrix based on the layer |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 43 | SkMatrix layerTransform; |
| 44 | layer->getTransform().copyTo(layerTransform); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 45 | sk_sp<SkImage> layerImage; |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 46 | int layerWidth = layer->getWidth(); |
| 47 | int layerHeight = layer->getHeight(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 48 | if (layer->getApi() == Layer::Api::OpenGL) { |
| 49 | GlLayer* glLayer = static_cast<GlLayer*>(layer); |
| 50 | GrGLTextureInfo externalTexture; |
| 51 | externalTexture.fTarget = glLayer->getRenderTarget(); |
Greg Daniel | 03d50fa | 2018-03-20 11:00:20 -0400 | [diff] [blame^] | 52 | SkASSERT(GL_RGBA == glLayer->getTexture().internalFormat()); |
| 53 | externalTexture.fFormat = GL_RGBA8; |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 54 | externalTexture.fID = glLayer->getTextureId(); |
Greg Daniel | 03d50fa | 2018-03-20 11:00:20 -0400 | [diff] [blame^] | 55 | GrBackendTexture backendTexture(layerWidth, layerHeight, GrMipMapped::kNo, externalTexture); |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 56 | layerImage = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin, |
Greg Daniel | 03d50fa | 2018-03-20 11:00:20 -0400 | [diff] [blame^] | 57 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 58 | } else { |
| 59 | SkASSERT(layer->getApi() == Layer::Api::Vulkan); |
| 60 | VkLayer* vkLayer = static_cast<VkLayer*>(layer); |
| 61 | canvas->clear(SK_ColorGREEN); |
| 62 | layerImage = vkLayer->getImage(); |
| 63 | } |
| 64 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 65 | if (layerImage) { |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 66 | SkMatrix textureMatrix; |
| 67 | layer->getTexTransform().copyTo(textureMatrix); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 68 | // 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] | 69 | // use bottom left origin and remove flipV and invert transformations. |
| 70 | SkMatrix flipV; |
| 71 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
| 72 | textureMatrix.preConcat(flipV); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | textureMatrix.preScale(1.0f / layerWidth, 1.0f / layerHeight); |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 74 | textureMatrix.postScale(layerWidth, layerHeight); |
| 75 | SkMatrix textureMatrixInv; |
| 76 | if (!textureMatrix.invert(&textureMatrixInv)) { |
| 77 | textureMatrixInv = textureMatrix; |
| 78 | } |
| 79 | |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 80 | SkMatrix matrix = SkMatrix::Concat(layerTransform, textureMatrixInv); |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 81 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 82 | SkPaint paint; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 83 | paint.setAlpha(layer->getAlpha()); |
| 84 | paint.setBlendMode(layer->getMode()); |
| 85 | paint.setColorFilter(sk_ref_sp(layer->getColorFilter())); |
Stan Iliev | a73b0be | 2017-10-06 10:16:58 -0400 | [diff] [blame] | 86 | |
| 87 | const bool nonIdentityMatrix = !matrix.isIdentity(); |
| 88 | if (nonIdentityMatrix) { |
| 89 | canvas->save(); |
| 90 | canvas->concat(matrix); |
| 91 | } |
| 92 | canvas->drawImage(layerImage.get(), 0, 0, &paint); |
| 93 | // restore the original matrix |
| 94 | if (nonIdentityMatrix) { |
| 95 | canvas->restore(); |
| 96 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 97 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 98 | |
| 99 | return layerImage; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 100 | } |
| 101 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 102 | }; // namespace skiapipeline |
| 103 | }; // namespace uirenderer |
| 104 | }; // namespace android |