blob: c49d53af94eba3a86cd9571203dababd416ddb8c [file] [log] [blame]
John Reck5cb290b2021-02-01 13:47:31 -05001/*
2 * Copyright (C) 2021 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#pragma once
18
19#include "utils/MathUtils.h"
20
Nader Jawad6701a602021-02-23 18:14:22 -080021#include <SkImage.h>
22#include <SkImageFilter.h>
John Reck8ed00dc2021-05-10 13:09:27 -040023#include <SkMatrix.h>
John Reck5cb290b2021-02-01 13:47:31 -050024#include <SkPoint.h>
25#include <SkRect.h>
Nader Jawad6701a602021-02-23 18:14:22 -080026#include <SkRuntimeEffect.h>
John Reck5cb290b2021-02-01 13:47:31 -050027
28namespace android::uirenderer {
29
John Reck5cb290b2021-02-01 13:47:31 -050030class StretchEffect {
31public:
John Reck5cb290b2021-02-01 13:47:31 -050032
Nader Jawad197743f2021-04-19 19:45:13 -070033 StretchEffect(const SkVector& direction,
34 float maxStretchAmountX,
Nader Jawada225d212021-03-17 15:12:58 -070035 float maxStretchAmountY)
Nader Jawad197743f2021-04-19 19:45:13 -070036 : maxStretchAmountX(maxStretchAmountX)
Nader Jawada225d212021-03-17 15:12:58 -070037 , maxStretchAmountY(maxStretchAmountY)
Nader Jawad197743f2021-04-19 19:45:13 -070038 , mStretchDirection(direction) { }
Nader Jawad6701a602021-02-23 18:14:22 -080039
40 StretchEffect() {}
41
John Reck5cb290b2021-02-01 13:47:31 -050042 bool isEmpty() const {
Nader Jawad6701a602021-02-23 18:14:22 -080043 return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y());
John Reck5cb290b2021-02-01 13:47:31 -050044 }
45
46 void setEmpty() {
47 *this = StretchEffect{};
48 }
49
Nader Jawad6701a602021-02-23 18:14:22 -080050 StretchEffect& operator=(const StretchEffect& other) {
Nader Jawad6701a602021-02-23 18:14:22 -080051 this->mStretchDirection = other.mStretchDirection;
Nader Jawada225d212021-03-17 15:12:58 -070052 this->maxStretchAmountX = other.maxStretchAmountX;
53 this->maxStretchAmountY = other.maxStretchAmountY;
Nader Jawad6701a602021-02-23 18:14:22 -080054 return *this;
55 }
56
Nader Jawad197743f2021-04-19 19:45:13 -070057 bool operator==(const StretchEffect& other) const {
58 return mStretchDirection == other.mStretchDirection &&
59 maxStretchAmountX == other.maxStretchAmountX &&
60 maxStretchAmountY == other.maxStretchAmountY;
61 }
62
John Reck5cb290b2021-02-01 13:47:31 -050063 void mergeWith(const StretchEffect& other) {
64 if (other.isEmpty()) {
65 return;
66 }
67 if (isEmpty()) {
68 *this = other;
69 return;
70 }
Nader Jawad197743f2021-04-19 19:45:13 -070071 mStretchDirection += other.mStretchDirection;
John Reck5cb290b2021-02-01 13:47:31 -050072 if (isEmpty()) {
73 return setEmpty();
74 }
Nader Jawada225d212021-03-17 15:12:58 -070075 maxStretchAmountX = std::max(maxStretchAmountX, other.maxStretchAmountX);
76 maxStretchAmountY = std::max(maxStretchAmountY, other.maxStretchAmountY);
John Reck5cb290b2021-02-01 13:47:31 -050077 }
78
Nader Jawadbc341862021-05-06 10:48:18 -070079 /**
80 * Return the stretched x position given the normalized x position with
81 * the current horizontal stretch direction
82 * @param normalizedX x position on the input texture from 0 to 1
83 * @return x position when the horizontal stretch direction applied
84 */
85 float computeStretchedPositionX(float normalizedX) const;
86
87 /**
88 * Return the stretched y position given the normalized y position with
89 * the current horizontal stretch direction
90 * @param normalizedX y position on the input texture from 0 to 1
91 * @return y position when the horizontal stretch direction applied
92 */
93 float computeStretchedPositionY(float normalizedY) const;
94
Nader Jawad197743f2021-04-19 19:45:13 -070095 sk_sp<SkShader> getShader(float width, float height,
96 const sk_sp<SkImage>& snapshotImage) const;
John Reck5cb290b2021-02-01 13:47:31 -050097
Nader Jawada225d212021-03-17 15:12:58 -070098 float maxStretchAmountX = 0;
99 float maxStretchAmountY = 0;
Nader Jawad6701a602021-02-23 18:14:22 -0800100
Nader Jawad6701a602021-02-23 18:14:22 -0800101 const SkVector getStretchDirection() const { return mStretchDirection; }
102
John Reck8ed00dc2021-05-10 13:09:27 -0400103 SkMatrix makeLinearStretch(float width, float height) const {
104 SkMatrix matrix;
105 auto [sX, sY] = getStretchDirection();
106 matrix.setScale(1 + std::abs(sX), 1 + std::abs(sY), sX > 0 ? 0 : width,
107 sY > 0 ? 0 : height);
108 return matrix;
109 }
110
Nader Jawad6701a602021-02-23 18:14:22 -0800111private:
112 static sk_sp<SkRuntimeEffect> getStretchEffect();
113 mutable SkVector mStretchDirection{0, 0};
114 mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
John Reck5cb290b2021-02-01 13:47:31 -0500115};
116
117} // namespace android::uirenderer