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 | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame^] | 36 | StretchEffect(const SkRect& area, const SkVector& direction, float maxStretchAmount) |
| 37 | : stretchArea(area), maxStretchAmount(maxStretchAmount), mStretchDirection(direction) {} |
| 38 | |
| 39 | StretchEffect() {} |
| 40 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 41 | bool isEmpty() const { |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame^] | 42 | return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y()); |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 43 | } |
| 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) { |
| 50 | this->stretchArea = other.stretchArea; |
| 51 | this->mStretchDirection = other.mStretchDirection; |
| 52 | this->mStretchFilter = nullptr; |
| 53 | this->maxStretchAmount = other.maxStretchAmount; |
| 54 | return *this; |
| 55 | } |
| 56 | |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 57 | void mergeWith(const StretchEffect& other) { |
| 58 | if (other.isEmpty()) { |
| 59 | return; |
| 60 | } |
| 61 | if (isEmpty()) { |
| 62 | *this = other; |
| 63 | return; |
| 64 | } |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame^] | 65 | setStretchDirection(mStretchDirection + other.mStretchDirection); |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 66 | if (isEmpty()) { |
| 67 | return setEmpty(); |
| 68 | } |
| 69 | stretchArea.join(other.stretchArea); |
| 70 | maxStretchAmount = std::max(maxStretchAmount, other.maxStretchAmount); |
| 71 | } |
| 72 | |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame^] | 73 | sk_sp<SkImageFilter> getImageFilter(const sk_sp<SkImage>& snapshotImage) const; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 74 | |
| 75 | SkRect stretchArea {0, 0, 0, 0}; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 76 | float maxStretchAmount = 0; |
Nader Jawad | 6701a60 | 2021-02-23 18:14:22 -0800 | [diff] [blame^] | 77 | |
| 78 | void setStretchDirection(const SkVector& direction) { |
| 79 | mStretchFilter = nullptr; |
| 80 | mStretchDirection = direction; |
| 81 | } |
| 82 | |
| 83 | const SkVector getStretchDirection() const { return mStretchDirection; } |
| 84 | |
| 85 | private: |
| 86 | static sk_sp<SkRuntimeEffect> getStretchEffect(); |
| 87 | mutable SkVector mStretchDirection{0, 0}; |
| 88 | mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder; |
| 89 | mutable sk_sp<SkImageFilter> mStretchFilter; |
John Reck | 5cb290b | 2021-02-01 13:47:31 -0500 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace android::uirenderer |