blob: 3b3067d2717f12e2f090e0f239a0c10996361fa7 [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
John Reck09d9cca2021-05-10 19:34:58 -040019#include "Properties.h"
John Reck5cb290b2021-02-01 13:47:31 -050020#include "utils/MathUtils.h"
21
Nader Jawad6701a602021-02-23 18:14:22 -080022#include <SkImage.h>
23#include <SkImageFilter.h>
John Reck8ed00dc2021-05-10 13:09:27 -040024#include <SkMatrix.h>
John Reck5cb290b2021-02-01 13:47:31 -050025#include <SkPoint.h>
26#include <SkRect.h>
Nader Jawad6701a602021-02-23 18:14:22 -080027#include <SkRuntimeEffect.h>
John Reck5cb290b2021-02-01 13:47:31 -050028
29namespace android::uirenderer {
30
John Reck5cb290b2021-02-01 13:47:31 -050031class StretchEffect {
32public:
John Reck5cb290b2021-02-01 13:47:31 -050033
Nader Jawad197743f2021-04-19 19:45:13 -070034 StretchEffect(const SkVector& direction,
35 float maxStretchAmountX,
Nader Jawada225d212021-03-17 15:12:58 -070036 float maxStretchAmountY)
Nader Jawad197743f2021-04-19 19:45:13 -070037 : maxStretchAmountX(maxStretchAmountX)
Nader Jawada225d212021-03-17 15:12:58 -070038 , maxStretchAmountY(maxStretchAmountY)
Nader Jawad197743f2021-04-19 19:45:13 -070039 , mStretchDirection(direction) { }
Nader Jawad6701a602021-02-23 18:14:22 -080040
41 StretchEffect() {}
42
John Reck5cb290b2021-02-01 13:47:31 -050043 bool isEmpty() const {
Nader Jawad6701a602021-02-23 18:14:22 -080044 return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y());
John Reck5cb290b2021-02-01 13:47:31 -050045 }
46
47 void setEmpty() {
48 *this = StretchEffect{};
49 }
50
Nader Jawad6701a602021-02-23 18:14:22 -080051 StretchEffect& operator=(const StretchEffect& other) {
Nader Jawad6701a602021-02-23 18:14:22 -080052 this->mStretchDirection = other.mStretchDirection;
Nader Jawada225d212021-03-17 15:12:58 -070053 this->maxStretchAmountX = other.maxStretchAmountX;
54 this->maxStretchAmountY = other.maxStretchAmountY;
Nader Jawad6701a602021-02-23 18:14:22 -080055 return *this;
56 }
57
Nader Jawad197743f2021-04-19 19:45:13 -070058 bool operator==(const StretchEffect& other) const {
59 return mStretchDirection == other.mStretchDirection &&
60 maxStretchAmountX == other.maxStretchAmountX &&
61 maxStretchAmountY == other.maxStretchAmountY;
62 }
63
John Reck5cb290b2021-02-01 13:47:31 -050064 void mergeWith(const StretchEffect& other) {
65 if (other.isEmpty()) {
66 return;
67 }
68 if (isEmpty()) {
69 *this = other;
70 return;
71 }
Nader Jawad197743f2021-04-19 19:45:13 -070072 mStretchDirection += other.mStretchDirection;
John Reck5cb290b2021-02-01 13:47:31 -050073 if (isEmpty()) {
74 return setEmpty();
75 }
Nader Jawada225d212021-03-17 15:12:58 -070076 maxStretchAmountX = std::max(maxStretchAmountX, other.maxStretchAmountX);
77 maxStretchAmountY = std::max(maxStretchAmountY, other.maxStretchAmountY);
John Reck5cb290b2021-02-01 13:47:31 -050078 }
79
Nader Jawadbc341862021-05-06 10:48:18 -070080 /**
81 * Return the stretched x position given the normalized x position with
82 * the current horizontal stretch direction
83 * @param normalizedX x position on the input texture from 0 to 1
84 * @return x position when the horizontal stretch direction applied
85 */
86 float computeStretchedPositionX(float normalizedX) const;
87
88 /**
89 * Return the stretched y position given the normalized y position with
90 * the current horizontal stretch direction
91 * @param normalizedX y position on the input texture from 0 to 1
92 * @return y position when the horizontal stretch direction applied
93 */
94 float computeStretchedPositionY(float normalizedY) const;
95
Nader Jawad197743f2021-04-19 19:45:13 -070096 sk_sp<SkShader> getShader(float width, float height,
97 const sk_sp<SkImage>& snapshotImage) const;
John Reck5cb290b2021-02-01 13:47:31 -050098
Nader Jawada225d212021-03-17 15:12:58 -070099 float maxStretchAmountX = 0;
100 float maxStretchAmountY = 0;
Nader Jawad6701a602021-02-23 18:14:22 -0800101
Nader Jawad6701a602021-02-23 18:14:22 -0800102 const SkVector getStretchDirection() const { return mStretchDirection; }
103
John Reck8ed00dc2021-05-10 13:09:27 -0400104 SkMatrix makeLinearStretch(float width, float height) const {
105 SkMatrix matrix;
106 auto [sX, sY] = getStretchDirection();
107 matrix.setScale(1 + std::abs(sX), 1 + std::abs(sY), sX > 0 ? 0 : width,
108 sY > 0 ? 0 : height);
109 return matrix;
110 }
111
John Reck09d9cca2021-05-10 19:34:58 -0400112 bool requiresLayer() const {
113 return !(isEmpty() ||
114 Properties::stretchEffectBehavior == StretchEffectBehavior::LinearScale);
115 }
116
Nader Jawad6701a602021-02-23 18:14:22 -0800117private:
118 static sk_sp<SkRuntimeEffect> getStretchEffect();
119 mutable SkVector mStretchDirection{0, 0};
120 mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
John Reck5cb290b2021-02-01 13:47:31 -0500121};
122
123} // namespace android::uirenderer