Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6be4c2d

Browse files
committed
made dlcolor store floats
1 parent 3576956 commit 6be4c2d

File tree

3 files changed

+75
-34
lines changed

3 files changed

+75
-34
lines changed

display_list/dl_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ class DisplayListBuilder final : public virtual DlCanvas,
805805

806806
// kAnyColor is a non-opaque and non-transparent color that will not
807807
// trigger any short-circuit tests about the results of a blend.
808-
static constexpr DlColor kAnyColor = DlColor::kMidGrey().withAlpha(0x80);
808+
static constexpr DlColor kAnyColor = DlColor::kMidGrey().withAlphaF(0.5f);
809809
static_assert(!kAnyColor.isOpaque());
810810
static_assert(!kAnyColor.isTransparent());
811811
static DlColor GetEffectiveColor(const DlPaint& paint,

display_list/dl_color.h

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,32 @@
99

1010
namespace flutter {
1111

12+
enum class DlColorSpace { kSRGB = 0, kExtendedSRGB = 1, kDisplayP3 = 2 };
13+
1214
struct DlColor {
1315
public:
14-
constexpr DlColor() : argb_(0xFF000000) {}
15-
constexpr explicit DlColor(uint32_t argb) : argb_(argb) {}
16+
constexpr DlColor()
17+
: alpha_(0.f),
18+
red_(0.f),
19+
green_(0.f),
20+
blue_(0.f),
21+
color_space_(DlColorSpace::kSRGB) {}
22+
constexpr explicit DlColor(uint32_t argb)
23+
: alpha_((argb >> 24) / 255.f),
24+
red_((argb >> 16) / 255.f),
25+
green_((argb >> 8) / 255.f),
26+
blue_((argb >> 0) / 255.f),
27+
color_space_(DlColorSpace::kSRGB) {}
28+
constexpr explicit DlColor(float alpha,
29+
float red,
30+
float green,
31+
float blue,
32+
DlColorSpace colorspace)
33+
: alpha_(alpha),
34+
red_(red),
35+
green_(green),
36+
blue_(blue),
37+
color_space_(colorspace) {}
1638

1739
/// @brief Construct a 32 bit color from floating point R, G, B, and A color
1840
/// channels.
@@ -29,10 +51,7 @@ struct DlColor {
2951
DlScalar r,
3052
DlScalar g,
3153
DlScalar b) {
32-
return DlColor(toC(a) << 24 | //
33-
toC(r) << 16 | //
34-
toC(g) << 8 | //
35-
toC(b));
54+
return DlColor(a, r, g, b, DlColorSpace::kSRGB);
3655
}
3756

3857
static constexpr uint8_t toAlpha(DlScalar opacity) { return toC(opacity); }
@@ -58,41 +77,55 @@ struct DlColor {
5877
static constexpr DlColor kCornflowerBlue() {return DlColor(0xFF6495ED);};
5978
// clang-format on
6079

61-
constexpr bool isOpaque() const { return getAlpha() == 0xFF; }
62-
constexpr bool isTransparent() const { return getAlpha() == 0; }
80+
constexpr bool isOpaque() const { return alpha_ >= 1.f; }
81+
constexpr bool isTransparent() const { return alpha_ <= 0.f; }
82+
83+
constexpr int getAlpha() const { return toC(alpha_); }
84+
constexpr int getRed() const { return toC(red_); }
85+
constexpr int getGreen() const { return toC(green_); }
86+
constexpr int getBlue() const { return toC(blue_); }
6387

64-
constexpr int getAlpha() const { return argb_ >> 24; }
65-
constexpr int getRed() const { return (argb_ >> 16) & 0xFF; }
66-
constexpr int getGreen() const { return (argb_ >> 8) & 0xFF; }
67-
constexpr int getBlue() const { return argb_ & 0xFF; }
88+
constexpr DlScalar getAlphaF() const { return alpha_; }
89+
constexpr DlScalar getRedF() const { return red_; }
90+
constexpr DlScalar getGreenF() const { return green_; }
91+
constexpr DlScalar getBlueF() const { return blue_; }
6892

69-
constexpr DlScalar getAlphaF() const { return toF(getAlpha()); }
70-
constexpr DlScalar getRedF() const { return toF(getRed()); }
71-
constexpr DlScalar getGreenF() const { return toF(getGreen()); }
72-
constexpr DlScalar getBlueF() const { return toF(getBlue()); }
93+
constexpr DlColorSpace getColorSpace() const { return color_space_; }
7394

7495
constexpr uint32_t premultipliedArgb() const {
7596
if (isOpaque()) {
76-
return argb_;
97+
return argb();
7798
}
7899
DlScalar f = getAlphaF();
79-
return (argb_ & 0xFF000000) | //
100+
return (argb() & 0xFF000000) | //
80101
toC(getRedF() * f) << 16 | //
81102
toC(getGreenF() * f) << 8 | //
82103
toC(getBlueF() * f);
83104
}
84105

85106
constexpr DlColor withAlpha(uint8_t alpha) const { //
86-
return DlColor((argb_ & 0x00FFFFFF) | (alpha << 24));
107+
return DlColor((argb() & 0x00FFFFFF) | (alpha << 24));
87108
}
88109
constexpr DlColor withRed(uint8_t red) const { //
89-
return DlColor((argb_ & 0xFF00FFFF) | (red << 16));
110+
return DlColor((argb() & 0xFF00FFFF) | (red << 16));
90111
}
91112
constexpr DlColor withGreen(uint8_t green) const { //
92-
return DlColor((argb_ & 0xFFFF00FF) | (green << 8));
113+
return DlColor((argb() & 0xFFFF00FF) | (green << 8));
93114
}
94115
constexpr DlColor withBlue(uint8_t blue) const { //
95-
return DlColor((argb_ & 0xFFFFFF00) | (blue << 0));
116+
return DlColor((argb() & 0xFFFFFF00) | (blue << 0));
117+
}
118+
constexpr DlColor withAlphaF(float alpha) const { //
119+
return DlColor(alpha, red_, green_, blue_, color_space_);
120+
}
121+
constexpr DlColor withRedF(float red) const { //
122+
return DlColor(alpha_, red, green_, blue_, color_space_);
123+
}
124+
constexpr DlColor withGreenF(float green) const { //
125+
return DlColor(alpha_, red_, green, blue_, color_space_);
126+
}
127+
constexpr DlColor withBlueF(float blue) const { //
128+
return DlColor(alpha_, red_, green_, blue, color_space_);
96129
}
97130

98131
constexpr DlColor modulateOpacity(DlScalar opacity) const {
@@ -101,15 +134,24 @@ struct DlColor {
101134
: withAlpha(round(getAlpha() * opacity));
102135
}
103136

104-
constexpr uint32_t argb() const { return argb_; }
137+
constexpr uint32_t argb() const {
138+
return toC(alpha_) << 24 | //
139+
toC(red_) << 16 | //
140+
toC(green_) << 8 | //
141+
toC(blue_) << 0;
142+
}
105143

106-
bool operator==(DlColor const& other) const { return argb_ == other.argb_; }
107-
bool operator!=(DlColor const& other) const { return argb_ != other.argb_; }
108-
bool operator==(uint32_t const& other) const { return argb_ == other; }
109-
bool operator!=(uint32_t const& other) const { return argb_ != other; }
144+
bool operator==(DlColor const& other) const { return argb() == other.argb(); }
145+
bool operator!=(DlColor const& other) const { return argb() != other.argb(); }
146+
bool operator==(uint32_t const& other) const { return argb() == other; }
147+
bool operator!=(uint32_t const& other) const { return argb() != other; }
110148

111149
private:
112-
uint32_t argb_;
150+
float alpha_;
151+
float red_;
152+
float green_;
153+
float blue_;
154+
DlColorSpace color_space_;
113155

114156
static constexpr DlScalar toF(uint8_t comp) { return comp * (1.0f / 255); }
115157
static constexpr uint8_t toC(DlScalar fComp) { return round(fComp * 255); }

display_list/effects/dl_color_source.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,10 @@ class DlGradientColorSourceBase : public DlMatrixColorSourceBase {
327327
stop_count_ != other_base->stop_count_) {
328328
return false;
329329
}
330-
static_assert(sizeof(colors()[0]) == 4);
331-
static_assert(sizeof(stops()[0]) == 4);
332-
int num_bytes = stop_count_ * 4;
333-
return (memcmp(colors(), other_base->colors(), num_bytes) == 0 &&
334-
memcmp(stops(), other_base->stops(), num_bytes) == 0);
330+
return (memcmp(colors(), other_base->colors(),
331+
stop_count_ * sizeof(colors()[0])) == 0 &&
332+
memcmp(stops(), other_base->stops(),
333+
stop_count_ * sizeof(stops()[0])) == 0);
335334
}
336335

337336
void store_color_stops(void* pod,

0 commit comments

Comments
 (0)