blob: 41788b6ba55d1160b2aa2e8395a7872cb795e11e [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 Sollenbergerd01b5912018-10-19 15:55:33 -040073 paint.setColorFilter(layer->getColorFilter());
Stan Ilieva73b0be2017-10-06 10:16:58 -040074 const bool nonIdentityMatrix = !matrix.isIdentity();
75 if (nonIdentityMatrix) {
76 canvas->save();
77 canvas->concat(matrix);
78 }
Stan Ilievaa0a3312018-10-19 15:26:08 -040079 const SkMatrix& totalMatrix = canvas->getTotalMatrix();
Stan Iliev1a025a72018-09-05 16:35:11 -040080 if (dstRect || srcRect) {
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040081 SkMatrix matrixInv;
82 if (!matrix.invert(&matrixInv)) {
83 matrixInv = matrix;
84 }
Stan Iliev1a025a72018-09-05 16:35:11 -040085 SkRect skiaSrcRect;
86 if (srcRect) {
87 skiaSrcRect = *srcRect;
88 } else {
89 skiaSrcRect = SkRect::MakeIWH(layerWidth, layerHeight);
90 }
91 matrixInv.mapRect(&skiaSrcRect);
92 SkRect skiaDestRect;
93 if (dstRect) {
94 skiaDestRect = *dstRect;
95 } else {
96 skiaDestRect = SkRect::MakeIWH(layerWidth, layerHeight);
97 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -040098 matrixInv.mapRect(&skiaDestRect);
Stan Ilievaa0a3312018-10-19 15:26:08 -040099 // If (matrix is identity or an integer translation) and (src/dst buffers size match),
100 // then use nearest neighbor, otherwise use bilerp sampling.
101 // Integer translation is defined as when src rect and dst rect align fractionally.
102 // Skia TextureOp has the above logic build-in, but not NonAAFillRectOp. TextureOp works
103 // only for SrcOver blending and without color filter (readback uses Src blending).
104 bool isIntegerTranslate = totalMatrix.isTranslate()
105 && SkScalarFraction(skiaDestRect.fLeft + totalMatrix[SkMatrix::kMTransX])
106 == SkScalarFraction(skiaSrcRect.fLeft)
107 && SkScalarFraction(skiaDestRect.fTop + totalMatrix[SkMatrix::kMTransY])
108 == SkScalarFraction(skiaSrcRect.fTop);
109 if (layer->getForceFilter() || !isIntegerTranslate) {
110 paint.setFilterQuality(kLow_SkFilterQuality);
111 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400112 canvas->drawImageRect(layerImage.get(), skiaSrcRect, skiaDestRect, &paint,
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400113 SkCanvas::kFast_SrcRectConstraint);
114 } else {
Stan Ilievaa0a3312018-10-19 15:26:08 -0400115 bool isIntegerTranslate = totalMatrix.isTranslate()
116 && SkScalarIsInt(totalMatrix[SkMatrix::kMTransX])
117 && SkScalarIsInt(totalMatrix[SkMatrix::kMTransY]);
118 if (layer->getForceFilter() || !isIntegerTranslate) {
119 paint.setFilterQuality(kLow_SkFilterQuality);
120 }
Leon Scroggins III1a12ab22018-03-26 15:00:49 -0400121 canvas->drawImage(layerImage.get(), 0, 0, &paint);
122 }
Stan Ilieva73b0be2017-10-06 10:16:58 -0400123 // restore the original matrix
124 if (nonIdentityMatrix) {
125 canvas->restore();
126 }
Stan Iliev021693b2016-10-17 16:26:15 -0400127 }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500128
Ben Wagner6b62ac02018-05-29 14:16:02 -0400129 return layerImage != nullptr;
Stan Iliev021693b2016-10-17 16:26:15 -0400130}
131
John Reck1bcacfd2017-11-03 10:12:19 -0700132}; // namespace skiapipeline
133}; // namespace uirenderer
134}; // namespace android