blob: 7eb640447f6158adf5a7054e4ebec56a5751eb56 [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
George Mount22a9f9c2021-06-23 18:29:41 +000043 bool isEmpty() const { return isZero(mStretchDirection.x()) && isZero(mStretchDirection.y()); }
John Reck5cb290b2021-02-01 13:47:31 -050044
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 Jawadbc341862021-05-06 10:48:18 -070078 /**
79 * Return the stretched x position given the normalized x position with
80 * the current horizontal stretch direction
81 * @param normalizedX x position on the input texture from 0 to 1
82 * @return x position when the horizontal stretch direction applied
83 */
84 float computeStretchedPositionX(float normalizedX) const;
85
86 /**
87 * Return the stretched y position given the normalized y position with
88 * the current horizontal stretch direction
89 * @param normalizedX y position on the input texture from 0 to 1
90 * @return y position when the horizontal stretch direction applied
91 */
92 float computeStretchedPositionY(float normalizedY) const;
93
Nader Jawad6aff4812021-06-09 10:14:43 -070094 sk_sp<SkShader> getShader(float width, float height, const sk_sp<SkImage>& snapshotImage,
95 const SkMatrix* matrix) const;
John Reck5cb290b2021-02-01 13:47:31 -050096
Nader Jawada225d212021-03-17 15:12:58 -070097 float maxStretchAmountX = 0;
98 float maxStretchAmountY = 0;
Nader Jawad6701a602021-02-23 18:14:22 -080099
Nader Jawad6701a602021-02-23 18:14:22 -0800100 const SkVector getStretchDirection() const { return mStretchDirection; }
101
John Reck8ed00dc2021-05-10 13:09:27 -0400102 SkMatrix makeLinearStretch(float width, float height) const {
103 SkMatrix matrix;
104 auto [sX, sY] = getStretchDirection();
105 matrix.setScale(1 + std::abs(sX), 1 + std::abs(sY), sX > 0 ? 0 : width,
106 sY > 0 ? 0 : height);
107 return matrix;
108 }
109
John Reck09d9cca2021-05-10 19:34:58 -0400110 bool requiresLayer() const {
Nader Jawadc401b232021-05-21 12:52:05 -0700111 return !isEmpty();
John Reck09d9cca2021-05-10 19:34:58 -0400112 }
113
Nader Jawad3206cce2021-06-23 11:48:58 -0700114 void clear() {
115 mBuilder = nullptr;
116 }
117
Nader Jawad6701a602021-02-23 18:14:22 -0800118private:
George Mount22a9f9c2021-06-23 18:29:41 +0000119 // The epsilon for StretchEffect is less than in MathUtils because
120 // the range is 0-1 for an entire screen and should be significantly
121 // less than 1 pixel for a smooth stretch animation.
122 inline static bool isZero(float value) {
123 // Using fabsf is more performant as ARM computes
124 // fabsf in a single instruction.
125 return fabsf(value) <= NON_ZERO_EPSILON;
126 }
127 // This should be good for 1/25,000 of a screen and should be good for
128 // screens with less than ~8000 pixels in one dimension with only 1/4 pixel
129 // cut-off.
130 static constexpr float NON_ZERO_EPSILON = 0.00004f;
Nader Jawad6701a602021-02-23 18:14:22 -0800131 static sk_sp<SkRuntimeEffect> getStretchEffect();
132 mutable SkVector mStretchDirection{0, 0};
133 mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
John Reck5cb290b2021-02-01 13:47:31 -0500134};
135
136} // namespace android::uirenderer