blob: d2da06b31f6878efdfaae7fc64697cd4bec17be5 [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 Jawad6701a602021-02-23 18:14:22 -080036 StretchEffect(const SkRect& area, const SkVector& direction, float maxStretchAmount)
37 : stretchArea(area), maxStretchAmount(maxStretchAmount), mStretchDirection(direction) {}
38
39 StretchEffect() {}
40
John Reck5cb290b2021-02-01 13:47:31 -050041 bool isEmpty() const {
Nader Jawad6701a602021-02-23 18:14:22 -080042 return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y());
John Reck5cb290b2021-02-01 13:47:31 -050043 }
44
45 void setEmpty() {
46 *this = StretchEffect{};
47 }
48
Nader Jawad6701a602021-02-23 18:14:22 -080049 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 Reck5cb290b2021-02-01 13:47:31 -050057 void mergeWith(const StretchEffect& other) {
58 if (other.isEmpty()) {
59 return;
60 }
61 if (isEmpty()) {
62 *this = other;
63 return;
64 }
Nader Jawad6701a602021-02-23 18:14:22 -080065 setStretchDirection(mStretchDirection + other.mStretchDirection);
John Reck5cb290b2021-02-01 13:47:31 -050066 if (isEmpty()) {
67 return setEmpty();
68 }
69 stretchArea.join(other.stretchArea);
70 maxStretchAmount = std::max(maxStretchAmount, other.maxStretchAmount);
71 }
72
Nader Jawad6701a602021-02-23 18:14:22 -080073 sk_sp<SkImageFilter> getImageFilter(const sk_sp<SkImage>& snapshotImage) const;
John Reck5cb290b2021-02-01 13:47:31 -050074
75 SkRect stretchArea {0, 0, 0, 0};
John Reck5cb290b2021-02-01 13:47:31 -050076 float maxStretchAmount = 0;
Nader Jawad6701a602021-02-23 18:14:22 -080077
78 void setStretchDirection(const SkVector& direction) {
79 mStretchFilter = nullptr;
80 mStretchDirection = direction;
81 }
82
83 const SkVector getStretchDirection() const { return mStretchDirection; }
84
85private:
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 Reck5cb290b2021-02-01 13:47:31 -050090};
91
92} // namespace android::uirenderer