blob: 8221b41ff4e50dfc9f55abcf98ba67135b633628 [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 Reck5cb290b2021-02-01 13:47:31 -050023#include <SkPoint.h>
24#include <SkRect.h>
Nader Jawad6701a602021-02-23 18:14:22 -080025#include <SkRuntimeEffect.h>
John Reck5cb290b2021-02-01 13:47:31 -050026
27namespace android::uirenderer {
28
29// TODO: Inherit from base RenderEffect type?
30class StretchEffect {
31public:
32 enum class StretchInterpolator {
33 SmoothStep,
34 };
35
Nader Jawada225d212021-03-17 15:12:58 -070036 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 Jawad6701a602021-02-23 18:14:22 -080042
43 StretchEffect() {}
44
John Reck5cb290b2021-02-01 13:47:31 -050045 bool isEmpty() const {
Nader Jawad6701a602021-02-23 18:14:22 -080046 return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y());
John Reck5cb290b2021-02-01 13:47:31 -050047 }
48
49 void setEmpty() {
50 *this = StretchEffect{};
51 }
52
Nader Jawad6701a602021-02-23 18:14:22 -080053 StretchEffect& operator=(const StretchEffect& other) {
54 this->stretchArea = other.stretchArea;
55 this->mStretchDirection = other.mStretchDirection;
56 this->mStretchFilter = nullptr;
Nader Jawada225d212021-03-17 15:12:58 -070057 this->maxStretchAmountX = other.maxStretchAmountX;
58 this->maxStretchAmountY = other.maxStretchAmountY;
Nader Jawad6701a602021-02-23 18:14:22 -080059 return *this;
60 }
61
John Reck5cb290b2021-02-01 13:47:31 -050062 void mergeWith(const StretchEffect& other) {
63 if (other.isEmpty()) {
64 return;
65 }
66 if (isEmpty()) {
67 *this = other;
68 return;
69 }
Nader Jawad6701a602021-02-23 18:14:22 -080070 setStretchDirection(mStretchDirection + other.mStretchDirection);
John Reck5cb290b2021-02-01 13:47:31 -050071 if (isEmpty()) {
72 return setEmpty();
73 }
74 stretchArea.join(other.stretchArea);
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 Jawad6701a602021-02-23 18:14:22 -080079 sk_sp<SkImageFilter> getImageFilter(const sk_sp<SkImage>& snapshotImage) const;
John Reck5cb290b2021-02-01 13:47:31 -050080
81 SkRect stretchArea {0, 0, 0, 0};
Nader Jawada225d212021-03-17 15:12:58 -070082 float maxStretchAmountX = 0;
83 float maxStretchAmountY = 0;
Nader Jawad6701a602021-02-23 18:14:22 -080084
85 void setStretchDirection(const SkVector& direction) {
86 mStretchFilter = nullptr;
87 mStretchDirection = direction;
88 }
89
90 const SkVector getStretchDirection() const { return mStretchDirection; }
91
92private:
93 static sk_sp<SkRuntimeEffect> getStretchEffect();
94 mutable SkVector mStretchDirection{0, 0};
95 mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
96 mutable sk_sp<SkImageFilter> mStretchFilter;
John Reck5cb290b2021-02-01 13:47:31 -050097};
98
99} // namespace android::uirenderer