| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 34 |
StretchEffect(const SkVector& direction, |
| 35 |
float maxStretchAmountX, |
| 36 |
float maxStretchAmountY) |
| 37 |
: maxStretchAmountX(maxStretchAmountX) |
| 38 |
, maxStretchAmountY(maxStretchAmountY) |
| 39 |
, mStretchDirection(direction) { } |
| 40 |
|
| 41 |
StretchEffect() {} |
| 42 |
|
| 43 |
bool isEmpty() const { return isZero(mStretchDirection.x()) && isZero(mStretchDirection.y()); } |
| 44 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
float computeStretchedPositionX(float normalizedX) const; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 100 |
const SkVector getStretchDirection() const { return mStretchDirection; } |
| 101 |
|
| 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 |
|
| 110 |
bool requiresLayer() const { |
| 111 |
return !isEmpty(); |
| 112 |
} |
| 113 |
|
| 114 |
void clear() { |
| 115 |
mBuilder = nullptr; |
| 116 |
} |
| 117 |
|
| 118 |
private: |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
inline static bool isZero(float value) { |
| 123 |
|
| 124 |
|
| 125 |
return fabsf(value) <= NON_ZERO_EPSILON; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
} |