blob: 61537f0b5a2c485a45cb6fc59d018d1f264c9a87 [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
John Reck5cb290b2021-02-01 13:47:31 -050029class StretchEffect {
30public:
John Reck5cb290b2021-02-01 13:47:31 -050031
Nader Jawad197743f2021-04-19 19:45:13 -070032 StretchEffect(const SkVector& direction,
33 float maxStretchAmountX,
Nader Jawada225d212021-03-17 15:12:58 -070034 float maxStretchAmountY)
Nader Jawad197743f2021-04-19 19:45:13 -070035 : maxStretchAmountX(maxStretchAmountX)
Nader Jawada225d212021-03-17 15:12:58 -070036 , maxStretchAmountY(maxStretchAmountY)
Nader Jawad197743f2021-04-19 19:45:13 -070037 , mStretchDirection(direction) { }
Nader Jawad6701a602021-02-23 18:14:22 -080038
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) {
Nader Jawad6701a602021-02-23 18:14:22 -080050 this->mStretchDirection = other.mStretchDirection;
Nader Jawada225d212021-03-17 15:12:58 -070051 this->maxStretchAmountX = other.maxStretchAmountX;
52 this->maxStretchAmountY = other.maxStretchAmountY;
Nader Jawad6701a602021-02-23 18:14:22 -080053 return *this;
54 }
55
Nader Jawad197743f2021-04-19 19:45:13 -070056 bool operator==(const StretchEffect& other) const {
57 return mStretchDirection == other.mStretchDirection &&
58 maxStretchAmountX == other.maxStretchAmountX &&
59 maxStretchAmountY == other.maxStretchAmountY;
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 Jawad197743f2021-04-19 19:45:13 -070070 mStretchDirection += other.mStretchDirection;
John Reck5cb290b2021-02-01 13:47:31 -050071 if (isEmpty()) {
72 return setEmpty();
73 }
Nader Jawada225d212021-03-17 15:12:58 -070074 maxStretchAmountX = std::max(maxStretchAmountX, other.maxStretchAmountX);
75 maxStretchAmountY = std::max(maxStretchAmountY, other.maxStretchAmountY);
John Reck5cb290b2021-02-01 13:47:31 -050076 }
77
Nader Jawad197743f2021-04-19 19:45:13 -070078 sk_sp<SkShader> getShader(float width, float height,
79 const sk_sp<SkImage>& snapshotImage) const;
John Reck5cb290b2021-02-01 13:47:31 -050080
Nader Jawada225d212021-03-17 15:12:58 -070081 float maxStretchAmountX = 0;
82 float maxStretchAmountY = 0;
Nader Jawad6701a602021-02-23 18:14:22 -080083
Nader Jawad6701a602021-02-23 18:14:22 -080084 const SkVector getStretchDirection() const { return mStretchDirection; }
85
86private:
87 static sk_sp<SkRuntimeEffect> getStretchEffect();
88 mutable SkVector mStretchDirection{0, 0};
89 mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
John Reck5cb290b2021-02-01 13:47:31 -050090};
91
92} // namespace android::uirenderer