| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef OUTLINE_H |
| 17 |
#define OUTLINE_H |
| 18 |
|
| 19 |
#include <SkPath.h> |
| 20 |
|
| 21 |
#include "Rect.h" |
| 22 |
#include "utils/MathUtils.h" |
| 23 |
|
| 24 |
namespace android { |
| 25 |
namespace uirenderer { |
| 26 |
|
| 27 |
class Outline { |
| 28 |
public: |
| 29 |
enum class Type { None = 0, Empty = 1, Path = 2, RoundRect = 3 }; |
| 30 |
|
| 31 |
Outline() : mShouldClip(false), mType(Type::None), mRadius(0), mAlpha(0.0f) {} |
| 32 |
|
| 33 |
void setRoundRect(int left, int top, int right, int bottom, float radius, float alpha) { |
| 34 |
mAlpha = alpha; |
| 35 |
if (mType == Type::RoundRect && left == mBounds.left && right == mBounds.right && |
| 36 |
top == mBounds.top && bottom == mBounds.bottom && radius == mRadius) { |
| 37 |
|
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
mType = Type::RoundRect; |
| 42 |
mBounds.set(left, top, right, bottom); |
| 43 |
mRadius = radius; |
| 44 |
|
| 45 |
|
| 46 |
if (mPath.countVerbs() > 10) { |
| 47 |
mPath.reset(); |
| 48 |
} else { |
| 49 |
mPath.rewind(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
if (MathUtils::isPositive(radius)) { |
| 54 |
mPath.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), radius, radius); |
| 55 |
} else { |
| 56 |
mPath.addRect(left, top, right, bottom); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void setPath(const SkPath* outline, float alpha) { |
| 61 |
if (!outline) { |
| 62 |
setEmpty(); |
| 63 |
return; |
| 64 |
} |
| 65 |
mType = Type::Path; |
| 66 |
mPath = *outline; |
| 67 |
mBounds.set(outline->getBounds()); |
| 68 |
mAlpha = alpha; |
| 69 |
} |
| 70 |
|
| 71 |
void setEmpty() { |
| 72 |
mType = Type::Empty; |
| 73 |
mPath.reset(); |
| 74 |
mAlpha = 0.0f; |
| 75 |
} |
| 76 |
|
| 77 |
void setNone() { |
| 78 |
mType = Type::None; |
| 79 |
mPath.reset(); |
| 80 |
mAlpha = 0.0f; |
| 81 |
} |
| 82 |
|
| 83 |
bool isEmpty() const { return mType == Type::Empty; } |
| 84 |
|
| 85 |
float getAlpha() const { return mAlpha; } |
| 86 |
|
| 87 |
void setShouldClip(bool clip) { mShouldClip = clip; } |
| 88 |
|
| 89 |
bool getShouldClip() const { return mShouldClip; } |
| 90 |
|
| 91 |
bool willClip() const { return mShouldClip; } |
| 92 |
|
| 93 |
bool willComplexClip() const { |
| 94 |
return mShouldClip && (mType != Type::RoundRect || MathUtils::isPositive(mRadius)); |
| 95 |
} |
| 96 |
|
| 97 |
bool getAsRoundRect(Rect* outRect, float* outRadius) const { |
| 98 |
if (mType == Type::RoundRect) { |
| 99 |
outRect->set(mBounds); |
| 100 |
*outRadius = mRadius; |
| 101 |
return true; |
| 102 |
} |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
const SkPath* getPath() const { |
| 107 |
if (mType == Type::None || mType == Type::Empty) return nullptr; |
| 108 |
|
| 109 |
return &mPath; |
| 110 |
} |
| 111 |
|
| 112 |
Type getType() const { return mType; } |
| 113 |
|
| 114 |
const Rect& getBounds() const { return mBounds; } |
| 115 |
|
| 116 |
float getRadius() const { return mRadius; } |
| 117 |
|
| 118 |
private: |
| 119 |
bool mShouldClip; |
| 120 |
Type mType; |
| 121 |
Rect mBounds; |
| 122 |
float mRadius; |
| 123 |
float mAlpha; |
| 124 |
SkPath mPath; |
| 125 |
}; |
| 126 |
|
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#endif |