1 /*
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 "Properties.h"
20 #include "utils/MathUtils.h"
21 
22 #include <SkImage.h>
23 #include <SkImageFilter.h>
24 #include <SkMatrix.h>
25 #include <SkPoint.h>
26 #include <SkRect.h>
27 #include <SkRuntimeEffect.h>
28 
29 namespace android::uirenderer {
30 
31 class StretchEffect {
32 public:
33 
StretchEffect(const SkVector & direction,float maxStretchAmountX,float maxStretchAmountY)34     StretchEffect(const SkVector& direction,
35                   float maxStretchAmountX,
36                   float maxStretchAmountY)
37             : maxStretchAmountX(maxStretchAmountX)
38             , maxStretchAmountY(maxStretchAmountY)
39             , mStretchDirection(direction) { }
40 
StretchEffect()41     StretchEffect() {}
42 
isEmpty()43     bool isEmpty() const { return isZero(mStretchDirection.x()) && isZero(mStretchDirection.y()); }
44 
setEmpty()45     void setEmpty() {
46         *this = StretchEffect{};
47     }
48 
49     StretchEffect& operator=(const StretchEffect& other) {
50         this->mStretchDirection = other.mStretchDirection;
51         this->maxStretchAmountX = other.maxStretchAmountX;
52         this->maxStretchAmountY = other.maxStretchAmountY;
53         return *this;
54     }
55 
56     bool operator==(const StretchEffect& other) const {
57         return mStretchDirection == other.mStretchDirection &&
58                 maxStretchAmountX == other.maxStretchAmountX &&
59                 maxStretchAmountY == other.maxStretchAmountY;
60     }
61 
mergeWith(const StretchEffect & other)62     void mergeWith(const StretchEffect& other) {
63         if (other.isEmpty()) {
64             return;
65         }
66         if (isEmpty()) {
67             *this = other;
68             return;
69         }
70         mStretchDirection += other.mStretchDirection;
71         if (isEmpty()) {
72             return setEmpty();
73         }
74         maxStretchAmountX = std::max(maxStretchAmountX, other.maxStretchAmountX);
75         maxStretchAmountY = std::max(maxStretchAmountY, other.maxStretchAmountY);
76     }
77 
78     /**
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 
94     sk_sp<SkShader> getShader(float width, float height, const sk_sp<SkImage>& snapshotImage,
95                               const SkMatrix* matrix) const;
96 
97     float maxStretchAmountX = 0;
98     float maxStretchAmountY = 0;
99 
getStretchDirection()100     const SkVector getStretchDirection() const { return mStretchDirection; }
101 
makeLinearStretch(float width,float height)102     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 
requiresLayer()110     bool requiresLayer() const {
111         return !isEmpty();
112     }
113 
clear()114     void clear() {
115         mBuilder = nullptr;
116     }
117 
118 private:
119     // 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.
isZero(float value)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;
131     static sk_sp<SkRuntimeEffect> getStretchEffect();
132     mutable SkVector mStretchDirection{0, 0};
133     mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
134 };
135 
136 } // namespace android::uirenderer
137