blob: 3ca0f8139c02ef64671002e42687b054448eabb6 [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"
Greg Daniel45ec62b2017-01-04 14:27:00 -050018
Greg Danielac2d2322017-07-12 11:30:15 -040019#include "GrBackendSurface.h"
Derek Sollenbergerf87da672016-11-02 11:34:27 -040020#include "SkColorFilter.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050021#include "SkSurface.h"
Stan Iliev021693b2016-10-17 16:26:15 -040022#include "gl/GrGLTypes.h"
23
24namespace android {
25namespace uirenderer {
26namespace skiapipeline {
27
28void LayerDrawable::onDraw(SkCanvas* canvas) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040029 Layer* layer = mLayerUpdater->backingLayer();
30 if (layer) {
Stan Iliev1a025a72018-09-05 16:35:11 -040031 DrawLayer(canvas->getGrContext(), canvas, layer, nullptr, nullptr, true);
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040032 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050033}
34
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040035bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
Stan Iliev1a025a72018-09-05 16:35:11 -040036 const SkRect* srcRect, const SkRect* dstRect,
37 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -040038 if (context == nullptr) {
39 SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
40 return false;
41 }
Stan Iliev021693b2016-10-17 16:26:15 -040042 // transform the matrix based on the layer
Stan Iliev564ca3e2018-09-04 22:00:00 +000043 SkMatrix layerTransform = layer->getTransform();
44 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040045 const int layerWidth = layer->getWidth();
46 const int layerHeight = layer->getHeight();
Greg Daniel45ec62b2017-01-04 14:27:00 -050047
Stan Iliev021693b2016-10-17 16:26:15 -040048 if (layerImage) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040049 SkMatrix textureMatrixInv;
Stan Iliev564ca3e2018-09-04 22:00:00 +000050 textureMatrixInv = layer->getTexTransform();
John Reck1bcacfd2017-11-03 10:12:19 -070051 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
Stan Iliev944dbf22017-09-27 11:05:29 -040052 // use bottom left origin and remove flipV and invert transformations.
53 SkMatrix flipV;
54 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040055 textureMatrixInv.preConcat(flipV);
56 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
Stan Iliev564ca3e2018-09-04 22:00:00 +000057 textureMatrixInv.postScale(layerImage->width(), layerImage->height());
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040058 SkMatrix textureMatrix;
59 if (!textureMatrixInv.invert(&textureMatrix)) {
60 textureMatrix = textureMatrixInv;
Stan Iliev944dbf22017-09-27 11:05:29 -040061 }
62
Stan Ilievaac878f2018-07-12 16:53:59 -040063 SkMatrix matrix;
Stan Iliev1a025a72018-09-05 16:35:11 -040064 if (useLayerTransform) {
Stan Ilievaac878f2018-07-12 16:53:59 -040065 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
Stan Iliev1a025a72018-09-05 16:35:11 -040066 } else {
67 matrix = textureMatrix;
Stan Ilievaac878f2018-07-12 16:53:59 -040068 }
Stan Iliev944dbf22017-09-27 11:05:29 -040069
Stan Iliev021693b2016-10-17 16:26:15 -040070 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050071 paint.setAlpha(layer->getAlpha());
72 paint.setBlendMode(layer->getMode());
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040073 paint.setColorFilter(layer->getColorSpaceWithFilter());
Stan Iliev564ca3e2018-09-04 22:00:00 +000074 if (layer->getForceFilter()) {
75 paint.setFilterQuality(kLow_SkFilterQuality);
76 }
Stan Ilieva73b0be2017-10-06 10:16:58 -040077
78 const bool nonIdentityMatrix = !matrix.isIdentity();
79 if (nonIdentityMatrix) {
80 canvas->save();
81 canvas->concat(matrix);
82 }
Stan Iliev1a025a72018-09-05 16:35:11 -040083 if (dstRect || srcRect) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040084 SkMatrix matrixInv;
85 if (!matrix.invert(&matrixInv)) {
86 matrixInv = matrix;
87 }
Stan Iliev1a025a72018-09-05 16:35:11 -040088 SkRect skiaSrcRect;
89 if (srcRect) {
90 skiaSrcRect = *srcRect;
91 } else {
92 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
93 }
94 matrixInv.mapRect(&skiaSrcRect);
95 SkRect skiaDestRect;
96 if (dstRect) {
97 skiaDestRect = *dstRect;
98 } else {
99 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
100 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400101 matrixInv.mapRect(&skiaDestRect);
Stan Iliev1a025a72018-09-05 16:35:11 -0400102 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint,
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400103 SkCanvas::kFast_SrcRectConstraint);
104 } else {
105 canvas->drawImage(layerImage.get(), 0, 0, &paint);
106 }
Stan Ilieva73b0be2017-10-06 10:16:58 -0400107 // restore the original matrix
108 if (nonIdentityMatrix) {
109 canvas->restore();
110 }
Stan Iliev021693b2016-10-17 16:26:15 -0400111 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500112
Ben Wagner6b62ac02018-05-29 14:16:02 -0400113 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400114}
115
John Reck1bcacfd2017-11-03 10:12:19 -0700116}; // namespace skiapipeline
117}; // namespace uirenderer
118}; // namespace android