John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 1 | /* |
| 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 Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 21 | #include <SkImage.h> |
| 22 | #include <SkImageFilter.h> |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame^] | 23 | #include <SkMatrix.h> |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 24 | #include <SkPoint.h> |
| 25 | #include <SkRect.h> |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 26 | #include <SkRuntimeEffect.h> |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 27 | |
| 28 | namespace android::uirenderer { |
| 29 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 30 | class StretchEffect { |
| 31 | public: |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 32 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 33 | StretchEffect(const SkVector& direction, |
| 34 | float maxStretchAmountX, |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 35 | float maxStretchAmountY) |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 36 | : maxStretchAmountX(maxStretchAmountX) |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 37 | , maxStretchAmountY(maxStretchAmountY) |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 38 | , mStretchDirection(direction) { } |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 39 | |
| 40 | StretchEffect() {} |
| 41 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 42 | bool isEmpty() const { |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 43 | return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y()); |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void setEmpty() { |
| 47 | *this = StretchEffect{}; |
| 48 | } |
| 49 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 50 | StretchEffect& operator=(const StretchEffect& other) { |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 51 | this->mStretchDirection = other.mStretchDirection; |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 52 | this->maxStretchAmountX = other.maxStretchAmountX; |
| 53 | this->maxStretchAmountY = other.maxStretchAmountY; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 54 | return *this; |
| 55 | } |
| 56 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 57 | bool operator==(const StretchEffect& other) const { |
| 58 | return mStretchDirection == other.mStretchDirection && |
| 59 | maxStretchAmountX == other.maxStretchAmountX && |
| 60 | maxStretchAmountY == other.maxStretchAmountY; |
| 61 | } |
| 62 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 63 | void mergeWith(const StretchEffect& other) { |
| 64 | if (other.isEmpty()) { |
| 65 | return; |
| 66 | } |
| 67 | if (isEmpty()) { |
| 68 | *this = other; |
| 69 | return; |
| 70 | } |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 71 | mStretchDirection += other.mStretchDirection; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 72 | if (isEmpty()) { |
| 73 | return setEmpty(); |
| 74 | } |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 75 | maxStretchAmountX = std::max(maxStretchAmountX, other.maxStretchAmountX); |
| 76 | maxStretchAmountY = std::max(maxStretchAmountY, other.maxStretchAmountY); |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Nader Jawad | bc34186 | 2021-05-06 10:48:18 -0700 | [diff] [blame] | 79 | /** |
| 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 Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 95 | sk_sp<SkShader> getShader(float width, float height, |
| 96 | const sk_sp<SkImage>& snapshotImage) const; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 97 | |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 98 | float maxStretchAmountX = 0; |
| 99 | float maxStretchAmountY = 0; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 100 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 101 | const SkVector getStretchDirection() const { return mStretchDirection; } |
| 102 | |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame^] | 103 | 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 Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 111 | private: |
| 112 | static sk_sp<SkRuntimeEffect> getStretchEffect(); |
| 113 | mutable SkVector mStretchDirection{0, 0}; |
| 114 | mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace android::uirenderer |