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 | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 23 | #include <SkPoint.h> |
| 24 | #include <SkRect.h> |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 25 | #include <SkRuntimeEffect.h> |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 26 | |
| 27 | namespace android::uirenderer { |
| 28 | |
| 29 | // TODO: Inherit from base RenderEffect type? |
| 30 | class StretchEffect { |
| 31 | public: |
| 32 | enum class StretchInterpolator { |
| 33 | SmoothStep, |
| 34 | }; |
| 35 | |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 36 | StretchEffect(const SkRect& area, const SkVector& direction, float maxStretchAmountX, |
| 37 | float maxStretchAmountY) |
| 38 | : stretchArea(area) |
| 39 | , maxStretchAmountX(maxStretchAmountX) |
| 40 | , maxStretchAmountY(maxStretchAmountY) |
| 41 | , mStretchDirection(direction) {} |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 42 | |
| 43 | StretchEffect() {} |
| 44 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 45 | bool isEmpty() const { |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 46 | return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y()); |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void setEmpty() { |
| 50 | *this = StretchEffect{}; |
| 51 | } |
| 52 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 53 | StretchEffect& operator=(const StretchEffect& other) { |
| 54 | this->stretchArea = other.stretchArea; |
| 55 | this->mStretchDirection = other.mStretchDirection; |
Nader Jawad | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 56 | this->mStretchShader = other.mStretchShader; |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 57 | this->maxStretchAmountX = other.maxStretchAmountX; |
| 58 | this->maxStretchAmountY = other.maxStretchAmountY; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 59 | return *this; |
| 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 | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 70 | setStretchDirection(mStretchDirection + other.mStretchDirection); |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 71 | if (isEmpty()) { |
| 72 | return setEmpty(); |
| 73 | } |
| 74 | stretchArea.join(other.stretchArea); |
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 | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 79 | sk_sp<SkShader> getShader(const sk_sp<SkImage>& snapshotImage) const; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 80 | |
| 81 | SkRect stretchArea {0, 0, 0, 0}; |
Nader Jawad | a225d21 | 2021-03-17 15:12:58 -0700 | [diff] [blame] | 82 | float maxStretchAmountX = 0; |
| 83 | float maxStretchAmountY = 0; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 84 | |
| 85 | void setStretchDirection(const SkVector& direction) { |
Nader Jawad | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 86 | mStretchShader = nullptr; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame] | 87 | mStretchDirection = direction; |
| 88 | } |
| 89 | |
| 90 | const SkVector getStretchDirection() const { return mStretchDirection; } |
| 91 | |
| 92 | private: |
| 93 | static sk_sp<SkRuntimeEffect> getStretchEffect(); |
| 94 | mutable SkVector mStretchDirection{0, 0}; |
| 95 | mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder; |
Nader Jawad | 8f5d66b | 2021-04-07 16:05:13 -0700 | [diff] [blame] | 96 | mutable sk_sp<SkShader> mStretchShader; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace android::uirenderer |