blob: 0cd64069bc5cf8fa4bef49b3795c03f997699ac6 [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
John Reck0aff62d2018-11-26 16:41:34 -080017#include <utils/MathUtils.h>
Stan Iliev021693b2016-10-17 16:26:15 -040018#include "LayerDrawable.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050019
Greg Danielac2d2322017-07-12 11:30:15 -040020#include "GrBackendSurface.h"
Derek Sollenbergerf87da672016-11-02 11:34:27 -040021#include "SkColorFilter.h"
Greg Daniel45ec62b2017-01-04 14:27:00 -050022#include "SkSurface.h"
Stan Iliev021693b2016-10-17 16:26:15 -040023#include "gl/GrGLTypes.h"
24
25namespace android {
26namespace uirenderer {
27namespace skiapipeline {
28
29void LayerDrawable::onDraw(SkCanvas* canvas) {
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040030 Layer* layer = mLayerUpdater->backingLayer();
31 if (layer) {
Stan Iliev1a025a72018-09-05 16:35:11 -040032 DrawLayer(canvas->getGrContext(), canvas, layer, nullptr, nullptr, true);
Derek Sollenbergerf5a370e2017-06-15 13:50:08 -040033 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050034}
35
John Reck0aff62d2018-11-26 16:41:34 -080036// This is a less-strict matrix.isTranslate() that will still report being translate-only
37// on imperceptibly small scaleX & scaleY values.
38static bool isBasicallyTranslate(const SkMatrix& matrix) {
39 if (!matrix.isScaleTranslate()) return false;
40 return MathUtils::isOne(matrix.getScaleX()) && MathUtils::isOne(matrix.getScaleY());
41}
42
43static bool shouldFilter(const SkMatrix& matrix) {
44 if (!matrix.isScaleTranslate()) return true;
45
46 // We only care about meaningful scale here
47 bool noScale = MathUtils::isOne(matrix.getScaleX())
48 && MathUtils::isOne(matrix.getScaleY());
49 bool pixelAligned = SkScalarIsInt(matrix.getTranslateX())
50 && SkScalarIsInt(matrix.getTranslateY());
51 return !(noScale && pixelAligned);
52}
53
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040054bool LayerDrawable::DrawLayer(GrContext* context, SkCanvas* canvas, Layer* layer,
Stan Iliev1a025a72018-09-05 16:35:11 -040055 const SkRect* srcRect, const SkRect* dstRect,
56 bool useLayerTransform) {
Stan Ilieve9d00122017-09-19 12:07:10 -040057 if (context == nullptr) {
58 SkDEBUGF(("Attempting to draw LayerDrawable into an unsupported surface"));
59 return false;
60 }
Stan Iliev021693b2016-10-17 16:26:15 -040061 // transform the matrix based on the layer
Stan Iliev564ca3e2018-09-04 22:00:00 +000062 SkMatrix layerTransform = layer->getTransform();
63 sk_sp<SkImage> layerImage = layer->getImage();
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040064 const int layerWidth = layer->getWidth();
65 const int layerHeight = layer->getHeight();
Greg Daniel45ec62b2017-01-04 14:27:00 -050066
Stan Iliev021693b2016-10-17 16:26:15 -040067 if (layerImage) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040068 SkMatrix textureMatrixInv;
Stan Iliev564ca3e2018-09-04 22:00:00 +000069 textureMatrixInv = layer->getTexTransform();
John Reck1bcacfd2017-11-03 10:12:19 -070070 // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
Stan Iliev944dbf22017-09-27 11:05:29 -040071 // use bottom left origin and remove flipV and invert transformations.
72 SkMatrix flipV;
73 flipV.setAll(1, 0, 0, 0, -1, 1, 0, 0, 1);
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040074 textureMatrixInv.preConcat(flipV);
75 textureMatrixInv.preScale(1.0f / layerWidth, 1.0f / layerHeight);
Stan Iliev564ca3e2018-09-04 22:00:00 +000076 textureMatrixInv.postScale(layerImage->width(), layerImage->height());
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040077 SkMatrix textureMatrix;
78 if (!textureMatrixInv.invert(&textureMatrix)) {
79 textureMatrix = textureMatrixInv;
Stan Iliev944dbf22017-09-27 11:05:29 -040080 }
81
Stan Ilievaac878f2018-07-12 16:53:59 -040082 SkMatrix matrix;
Stan Iliev1a025a72018-09-05 16:35:11 -040083 if (useLayerTransform) {
Stan Ilievaac878f2018-07-12 16:53:59 -040084 matrix = SkMatrix::Concat(layerTransform, textureMatrix);
Stan Iliev1a025a72018-09-05 16:35:11 -040085 } else {
86 matrix = textureMatrix;
Stan Ilievaac878f2018-07-12 16:53:59 -040087 }
Stan Iliev944dbf22017-09-27 11:05:29 -040088
Stan Iliev021693b2016-10-17 16:26:15 -040089 SkPaint paint;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050090 paint.setAlpha(layer->getAlpha());
91 paint.setBlendMode(layer->getMode());
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040092 paint.setColorFilter(layer->getColorSpaceWithFilter());
Stan Ilieva73b0be2017-10-06 10:16:58 -040093 const bool nonIdentityMatrix = !matrix.isIdentity();
94 if (nonIdentityMatrix) {
95 canvas->save();
96 canvas->concat(matrix);
97 }
Stan Ilievaa0a3312018-10-19 15:26:08 -040098 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
Stan Iliev1a025a72018-09-05 16:35:11 -040099 if (dstRect || srcRect) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400100 SkMatrix matrixInv;
101 if (!matrix.invert(&matrixInv)) {
102 matrixInv = matrix;
103 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400104 SkRect skiaSrcRect;
105 if (srcRect) {
106 skiaSrcRect = *srcRect;
107 } else {
108 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
109 }
110 matrixInv.mapRect(&skiaSrcRect);
111 SkRect skiaDestRect;
112 if (dstRect) {
113 skiaDestRect = *dstRect;
114 } else {
115 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
116 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400117 matrixInv.mapRect(&skiaDestRect);
Stan Ilievaa0a3312018-10-19 15:26:08 -0400118 // If (matrix is identity or an integer translation) and (src/dst buffers size match),
119 // then use nearest neighbor, otherwise use bilerp sampling.
120 // Integer translation is defined as when src rect and dst rect align fractionally.
121 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
122 // only for SrcOver blending and without color filter (readback uses Src blending).
John Reck0aff62d2018-11-26 16:41:34 -0800123 bool isIntegerTranslate = isBasicallyTranslate(totalMatrix)
Stan Ilievaa0a3312018-10-19 15:26:08 -0400124 && SkScalarFraction(skiaDestRect.fLeft + totalMatrix[SkMatrix::kMTransX])
125 == SkScalarFraction(skiaSrcRect.fLeft)
126 && SkScalarFraction(skiaDestRect.fTop + totalMatrix[SkMatrix::kMTransY])
127 == SkScalarFraction(skiaSrcRect.fTop);
128 if (layer->getForceFilter() || !isIntegerTranslate) {
129 paint.setFilterQuality(kLow_SkFilterQuality);
130 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400131 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint,
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400132 SkCanvas::kFast_SrcRectConstraint);
133 } else {
John Reck0aff62d2018-11-26 16:41:34 -0800134 if (layer->getForceFilter() || shouldFilter(totalMatrix)) {
Stan Ilievaa0a3312018-10-19 15:26:08 -0400135 paint.setFilterQuality(kLow_SkFilterQuality);
136 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400137 canvas->drawImage(layerImage.get(), 0, 0, &paint);
138 }
Stan Ilieva73b0be2017-10-06 10:16:58 -0400139 // restore the original matrix
140 if (nonIdentityMatrix) {
141 canvas->restore();
142 }
Stan Iliev021693b2016-10-17 16:26:15 -0400143 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500144
Ben Wagner6b62ac02018-05-29 14:16:02 -0400145 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400146}
147
John Reck1bcacfd2017-11-03 10:12:19 -0700148}; // namespace skiapipeline
149}; // namespace uirenderer
150}; // namespace android