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 | |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 17 | #include "GlLayer.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 | #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 | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 38 | // transform the matrix based on the layer |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 39 | SkMatrix layerTransform; |
| 40 | layer->getTransform().copyTo(layerTransform); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 41 | sk_sp<SkImage> layerImage; |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 42 | int layerWidth = layer->getWidth(); |
| 43 | int layerHeight = layer->getHeight(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 44 | if (layer->getApi() == Layer::Api::OpenGL) { |
| 45 | GlLayer* glLayer = static_cast<GlLayer*>(layer); |
| 46 | GrGLTextureInfo externalTexture; |
| 47 | externalTexture.fTarget = glLayer->getRenderTarget(); |
| 48 | externalTexture.fID = glLayer->getTextureId(); |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 49 | GrBackendTexture backendTexture(layerWidth, layerHeight, kRGBA_8888_GrPixelConfig, |
| 50 | externalTexture); |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 51 | layerImage = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin, |
| 52 | kPremul_SkAlphaType, nullptr); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 53 | } else { |
| 54 | SkASSERT(layer->getApi() == Layer::Api::Vulkan); |
| 55 | VkLayer* vkLayer = static_cast<VkLayer*>(layer); |
| 56 | canvas->clear(SK_ColorGREEN); |
| 57 | layerImage = vkLayer->getImage(); |
| 58 | } |
| 59 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 60 | if (layerImage) { |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 61 | SkMatrix textureMatrix; |
| 62 | layer->getTexTransform().copyTo(textureMatrix); |
| 63 | //TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed |
| 64 | // use bottom left origin and remove flipV and invert transformations. |
| 65 | SkMatrix flipV; |
| 66 | flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1); |
| 67 | textureMatrix.preConcat(flipV); |
| 68 | textureMatrix.preScale(1.0f/layerWidth, 1.0f/layerHeight); |
| 69 | textureMatrix.postScale(layerWidth, layerHeight); |
| 70 | SkMatrix textureMatrixInv; |
| 71 | if (!textureMatrix.invert(&textureMatrixInv)) { |
| 72 | textureMatrixInv = textureMatrix; |
| 73 | } |
| 74 | |
| 75 | SkMatrix matrix = SkMatrix::Concat(textureMatrixInv, layerTransform); |
| 76 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 77 | SkPaint paint; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 78 | paint.setAlpha(layer->getAlpha()); |
| 79 | paint.setBlendMode(layer->getMode()); |
| 80 | paint.setColorFilter(sk_ref_sp(layer->getColorFilter())); |
Stan Iliev | 944dbf2 | 2017-09-27 11:05:29 -0400 | [diff] [blame] | 81 | // draw image with a shader to avoid save/restore of the matrix |
| 82 | paint.setShader(layerImage->makeShader(&matrix)); |
| 83 | canvas->drawRect(SkRect::MakeWH(layerWidth, layerHeight), paint); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 84 | } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 85 | |
| 86 | return layerImage; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | }; // namespace skiapipeline |
| 90 | }; // namespace uirenderer |
| 91 | }; // namespace android |