blob: 00ba71302dc255f5818c28f64e265f63eef6f76a [file] [log] [blame]
Stan Iliev021693b2016-10-17 16:26:15 -04001/*
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 Reck1bcacfd2017-11-03 10:12:19 -070018#include "GlLayer.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050019#include "VkLayer.h"
20
Greg Danielac2d2322017-07-12 11:30:15 -040021#include "GrBackendSurface.h"
Derek Sollenbergerf87da672016-11-02 11:34:27 -040022#include "SkColorFilter.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050023#include "SkSurface.h"
Stan Iliev021693b2016-10-17 16:26:15 -040024#include "gl/GrGLTypes.h"
25
26namespace android {
27namespace uirenderer {
28namespace skiapipeline {
29
30void LayerDrawable::onDraw(SkCanvas* canvas) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040031 Layer* layer = mLayerUpdater->backingLayer();
32 if (layer) {
33 DrawLayer(canvas->getGrContext(), canvas, layer);
34 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050035}
36
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040037bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
38 const SkRect* dstRect) {
Stan Ilieve9d00122017-09-19 12:07:10 -040039 if (context == nullptr) {
40 SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
41 return false;
42 }
Stan Iliev021693b2016-10-17 16:26:15 -040043 // transform the matrix based on the layer
Stan Iliev944dbf22017-09-27 11:05:29 -040044 SkMatrix layerTransform;
45 layer->getTransform().copyTo(layerTransform);
Greg Daniel45ec62b2017-01-04 14:27:00 -050046 sk_sp<SkImage> layerImage;
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040047 const int layerWidth = layer->getWidth();
48 const int layerHeight = layer->getHeight();
Greg Daniel45ec62b2017-01-04 14:27:00 -050049 if (layer->getApi() == Layer::Api::OpenGL) {
50 GlLayer* glLayer = static_cast<GlLayer*>(layer);
51 GrGLTextureInfo externalTexture;
52 externalTexture.fTarget = glLayer->getRenderTarget();
53 externalTexture.fID = glLayer->getTextureId();
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040054 // The format may not be GL_RGBA8, but given the DeferredLayerUpdater and GLConsumer don't
55 // expose that info we use it as our default. Further, given that we only use this texture
56 // as a source this will not impact how Skia uses the texture. The only potential affect
57 // this is anticipated to have is that for some format types if we are not bound as an OES
58 // texture we may get invalid results for SKP capture if we read back the texture.
59 externalTexture.fFormat = GL_RGBA8;
Greg Daniel03d50fa2018-03-20 11:00:20 -040060 GrBackendTexture backendTexture(layerWidth, layerHeight, GrMipMapped::kNo, externalTexture);
Greg Danielac2d2322017-07-12 11:30:15 -040061 layerImage = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
Greg Daniel03d50fa2018-03-20 11:00:20 -040062 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
Greg Daniel45ec62b2017-01-04 14:27:00 -050063 } else {
64 SkASSERT(layer->getApi() == Layer::Api::Vulkan);
65 VkLayer* vkLayer = static_cast<VkLayer*>(layer);
66 canvas->clear(SK_ColorGREEN);
67 layerImage = vkLayer->getImage();
68 }
69
Stan Iliev021693b2016-10-17 16:26:15 -040070 if (layerImage) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040071 SkMatrix textureMatrixInv;
72 layer->getTexTransform().copyTo(textureMatrixInv);
John Reck1bcacfd2017-11-03 10:12:19 -070073 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
Stan Iliev944dbf22017-09-27 11:05:29 -040074 // use bottom left origin and remove flipV and invert transformations.
75 SkMatrix flipV;
76 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040077 textureMatrixInv.preConcat(flipV);
78 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
79 textureMatrixInv.postScale(layerWidth, layerHeight);
80 SkMatrix textureMatrix;
81 if (!textureMatrixInv.invert(&textureMatrix)) {
82 textureMatrix = textureMatrixInv;
Stan Iliev944dbf22017-09-27 11:05:29 -040083 }
84
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040085 SkMatrix matrix = SkMatrix::Concat(layerTransform, textureMatrix);
Stan Iliev944dbf22017-09-27 11:05:29 -040086
Stan Iliev021693b2016-10-17 16:26:15 -040087 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050088 paint.setAlpha(layer->getAlpha());
89 paint.setBlendMode(layer->getMode());
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040090 paint.setColorFilter(layer->getColorSpaceWithFilter());
Stan Ilieva73b0be2017-10-06 10:16:58 -040091
92 const bool nonIdentityMatrix = !matrix.isIdentity();
93 if (nonIdentityMatrix) {
94 canvas->save();
95 canvas->concat(matrix);
96 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040097 if (dstRect) {
98 SkMatrix matrixInv;
99 if (!matrix.invert(&matrixInv)) {
100 matrixInv = matrix;
101 }
102 SkRect srcRect = SkRect::MakeIWH(layerWidth, layerHeight);
103 matrixInv.mapRect(&srcRect);
104 SkRect skiaDestRect = *dstRect;
105 matrixInv.mapRect(&skiaDestRect);
106 canvas->drawImageRect(layerImage.get(), srcRect, skiaDestRect, &paint,
107 SkCanvas::kFast_SrcRectConstraint);
108 } else {
109 canvas->drawImage(layerImage.get(), 0, 0, &paint);
110 }
Stan Ilieva73b0be2017-10-06 10:16:58 -0400111 // restore the original matrix
112 if (nonIdentityMatrix) {
113 canvas->restore();
114 }
Stan Iliev021693b2016-10-17 16:26:15 -0400115 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500116
117 return layerImage;
Stan Iliev021693b2016-10-17 16:26:15 -0400118}
119
John Reck1bcacfd2017-11-03 10:12:19 -0700120}; // namespace skiapipeline
121}; // namespace uirenderer
122}; // namespace android