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 | |
John Reck | 09d9cca | 2021-05-10 19:34:58 -0400 | [diff] [blame] | 19 | #include "Properties.h" |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 20 | #include "utils/MathUtils.h" |
| 21 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 22 | #include <SkImage.h> |
| 23 | #include <SkImageFilter.h> |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame] | 24 | #include <SkMatrix.h> |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 25 | #include <SkPoint.h> |
| 26 | #include <SkRect.h> |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 27 | #include <SkRuntimeEffect.h> |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 28 | |
| 29 | namespace android::uirenderer { |
| 30 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 31 | class StretchEffect { |
| 32 | public: |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 33 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 34 | StretchEffect(const SkVector& direction, |
| 35 | float maxStretchAmountX, |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 36 | float maxStretchAmountY) |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 37 | : maxStretchAmountX(maxStretchAmountX) |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 38 | , maxStretchAmountY(maxStretchAmountY) |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 39 | , mStretchDirection(direction) { } |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 40 | |
| 41 | StretchEffect() {} |
| 42 | |
George Mount | 22a9f9c | 2021-06-23 18:29:41 +0000 | [diff] [blame] | 43 | bool isEmpty() const { return isZero(mStretchDirection.x()) && isZero(mStretchDirection.y()); } |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 44 | |
| 45 | void setEmpty() { |
| 46 | *this = StretchEffect{}; |
| 47 | } |
| 48 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 49 | StretchEffect& operator=(const StretchEffect& other) { |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 50 | this->mStretchDirection = other.mStretchDirection; |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 51 | this->maxStretchAmountX = other.maxStretchAmountX; |
| 52 | this->maxStretchAmountY = other.maxStretchAmountY; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 53 | return *this; |
| 54 | } |
| 55 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 56 | bool operator==(const StretchEffect& other) const { |
| 57 | return mStretchDirection == other.mStretchDirection && |
| 58 | maxStretchAmountX == other.maxStretchAmountX && |
| 59 | maxStretchAmountY == other.maxStretchAmountY; |
| 60 | } |
| 61 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 62 | void mergeWith(const StretchEffect& other) { |
| 63 | if (other.isEmpty()) { |
| 64 | return; |
| 65 | } |
| 66 | if (isEmpty()) { |
| 67 | *this = other; |
| 68 | return; |
| 69 | } |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 70 | mStretchDirection += other.mStretchDirection; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 71 | if (isEmpty()) { |
| 72 | return setEmpty(); |
| 73 | } |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 74 | maxStretchAmountX = std::max(maxStretchAmountX, other.maxStretchAmountX); |
| 75 | maxStretchAmountY = std::max(maxStretchAmountY, other.maxStretchAmountY); |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Nader Jawad | bc34186 | 2021-05-06 10:48:18 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Return the stretched x position given the normalized x position with |
| 80 | * the current horizontal stretch direction |
| 81 | * @param normalizedX x position on the input texture from 0 to 1 |
| 82 | * @return x position when the horizontal stretch direction applied |
| 83 | */ |
| 84 | float computeStretchedPositionX(float normalizedX) const; |
| 85 | |
| 86 | /** |
| 87 | * Return the stretched y position given the normalized y position with |
| 88 | * the current horizontal stretch direction |
| 89 | * @param normalizedX y position on the input texture from 0 to 1 |
| 90 | * @return y position when the horizontal stretch direction applied |
| 91 | */ |
| 92 | float computeStretchedPositionY(float normalizedY) const; |
| 93 | |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 94 | sk_sp<SkShader> getShader(float width, float height, const sk_sp<SkImage>& snapshotImage, |
| 95 | const SkMatrix* matrix) const; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 96 | |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 97 | float maxStretchAmountX = 0; |
| 98 | float maxStretchAmountY = 0; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 99 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 100 | const SkVector getStretchDirection() const { return mStretchDirection; } |
| 101 | |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame] | 102 | SkMatrix makeLinearStretch(float width, float height) const { |
| 103 | SkMatrix matrix; |
| 104 | auto [sX, sY] = getStretchDirection(); |
| 105 | matrix.setScale(1 + std::abs(sX), 1 + std::abs(sY), sX > 0 ? 0 : width, |
| 106 | sY > 0 ? 0 : height); |
| 107 | return matrix; |
| 108 | } |
| 109 | |
John Reck | 09d9cca | 2021-05-10 19:34:58 -0400 | [diff] [blame] | 110 | bool requiresLayer() const { |
Nader Jawad | c401b23 | 2021-05-21 12:52:05 -0700 | [diff] [blame] | 111 | return !isEmpty(); |
John Reck | 09d9cca | 2021-05-10 19:34:58 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Nader Jawad | 3206cce | 2021-06-23 11:48:58 -0700 | [diff] [blame] | 114 | void clear() { |
| 115 | mBuilder = nullptr; |
| 116 | } |
| 117 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 118 | private: |
George Mount | 22a9f9c | 2021-06-23 18:29:41 +0000 | [diff] [blame] | 119 | // The epsilon for StretchEffect is less than in MathUtils because |
| 120 | // the range is 0-1 for an entire screen and should be significantly |
| 121 | // less than 1 pixel for a smooth stretch animation. |
| 122 | inline static bool isZero(float value) { |
| 123 | // Using fabsf is more performant as ARM computes |
| 124 | // fabsf in a single instruction. |
| 125 | return fabsf(value) <= NON_ZERO_EPSILON; |
| 126 | } |
| 127 | // This should be good for 1/25,000 of a screen and should be good for |
| 128 | // screens with less than ~8000 pixels in one dimension with only 1/4 pixel |
| 129 | // cut-off. |
| 130 | static constexpr float NON_ZERO_EPSILON = 0.00004f; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 131 | static sk_sp<SkRuntimeEffect> getStretchEffect(); |
| 132 | mutable SkVector mStretchDirection{0, 0}; |
| 133 | mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace android::uirenderer |