Change SkColor4f to RGBA channel order

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2093763003

Review-Url: https://codereview.chromium.org/2093763003
This commit is contained in:
brianosman 2016-06-24 06:31:47 -07:00 committed by Commit bot
parent 7bde5659d3
commit e074d1fa6a
7 changed files with 37 additions and 34 deletions

View File

@ -99,10 +99,10 @@ DEF_SIMPLE_GM(color4shader, canvas, 1024, 260) {
mat.set3x3(0, 1, 0, 0, 0, 1, 1, 0, 0);
const SkColor4f colors[] {
{ 1, 1, 0, 0 },
{ 1, 0, 1, 0 },
{ 1, 0, 0, 1 },
{ 1, 0.5, 0.5, 0.5 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 1 },
{ 0.5, 0.5, 0.5, 1 },
};
SkPaint paint;

View File

@ -166,10 +166,10 @@ struct SkPM4f;
* The float values are 0...1 unpremultiplied
*/
struct SkColor4f {
float fA;
float fR;
float fG;
float fB;
float fA;
bool operator==(const SkColor4f& other) const {
return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
@ -178,14 +178,16 @@ struct SkColor4f {
return !(*this == other);
}
const float* vec() const { return &fA; }
float* vec() { return &fA; }
const float* vec() const { return &fR; }
float* vec() { return &fR; }
static SkColor4f Pin(float a, float r, float g, float b);
static SkColor4f Pin(float r, float g, float b, float a);
static SkColor4f FromColor(SkColor);
SkColor toSkColor() const;
SkColor4f pin() const {
return Pin(fA, fR, fG, fB);
return Pin(fR, fG, fB, fA);
}
SkPM4f premul() const;

View File

@ -120,7 +120,7 @@ SkColor4f SkPM4f::unpremul() const {
return { 0, 0, 0, 0 };
} else {
float invAlpha = 1 / alpha;
return { alpha, fVec[R] * invAlpha, fVec[G] * invAlpha, fVec[B] * invAlpha };
return { fVec[R] * invAlpha, fVec[G] * invAlpha, fVec[B] * invAlpha, alpha };
}
}
@ -155,7 +155,7 @@ void SkPM4f::assertIsUnit() const {
///////////////////////////////////////////////////////////////////////////////////////////////////
SkColor4f SkColor4f::FromColor(SkColor c) {
Sk4f value = SkNx_shuffle<3,2,1,0>(SkNx_cast<float>(Sk4b::Load(&c)));
Sk4f value = SkNx_shuffle<2,1,0,3>(SkNx_cast<float>(Sk4b::Load(&c)));
SkColor4f c4;
(value * Sk4f(1.0f / 255)).store(&c4);
if (gTreatSkColorAsSRGB) {
@ -166,19 +166,23 @@ SkColor4f SkColor4f::FromColor(SkColor c) {
return c4;
}
SkColor4f SkColor4f::Pin(float a, float r, float g, float b) {
SkColor SkColor4f::toSkColor() const {
SkColor result;
Sk4f value = SkNx_shuffle<2, 1, 0, 3>(Sk4f::Load(this->vec()));
SkNx_cast<uint8_t>(value * Sk4f(255) + Sk4f(0.5f)).store(&result);
return result;
}
SkColor4f SkColor4f::Pin(float r, float g, float b, float a) {
SkColor4f c4;
Sk4f::Min(Sk4f::Max(Sk4f(a, r, g, b), Sk4f(0)), Sk4f(1)).store(c4.vec());
Sk4f::Min(Sk4f::Max(Sk4f(r, g, b, a), Sk4f(0)), Sk4f(1)).store(c4.vec());
return c4;
}
SkPM4f SkColor4f::premul() const {
auto src = Sk4f::Load(this->pin().vec());
float srcAlpha = src[0]; // need the pinned version of our alpha
src = src * Sk4f(1, srcAlpha, srcAlpha, srcAlpha);
float srcAlpha = src[3]; // need the pinned version of our alpha
src = src * Sk4f(srcAlpha, srcAlpha, srcAlpha, 1);
// ARGB -> RGBA
Sk4f dst = SkNx_shuffle<1,2,3,0>(src);
return SkPM4f::From4f(dst);
return SkPM4f::From4f(src);
}

View File

@ -133,10 +133,10 @@ SkColor4Shader::SkColor4Shader(const SkColor4f& color, sk_sp<SkColorSpace> space
sk_sp<SkFlattenable> SkColor4Shader::CreateProc(SkReadBuffer& buffer) {
SkColor4f color;
color.fA = buffer.readScalar(); // readFloat()
color.fR = buffer.readScalar();
color.fR = buffer.readScalar(); // readFloat()
color.fG = buffer.readScalar();
color.fB = buffer.readScalar();
color.fA = buffer.readScalar();
if (buffer.readBool()) {
// TODO how do we unflatten colorspaces
}
@ -144,10 +144,10 @@ sk_sp<SkFlattenable> SkColor4Shader::CreateProc(SkReadBuffer& buffer) {
}
void SkColor4Shader::flatten(SkWriteBuffer& buffer) const {
buffer.writeScalar(fColor4.fA); // writeFloat()
buffer.writeScalar(fColor4.fR);
buffer.writeScalar(fColor4.fR); // writeFloat()
buffer.writeScalar(fColor4.fG);
buffer.writeScalar(fColor4.fB);
buffer.writeScalar(fColor4.fA);
buffer.writeBool(false); // TODO how do we flatten colorspaces?
}
@ -232,7 +232,7 @@ sk_sp<GrFragmentProcessor> SkColor4Shader::asFragmentProcessor(GrContext*, const
void SkColor4Shader::toString(SkString* str) const {
str->append("SkColor4Shader: (");
str->append("ARGB:");
str->append("RGBA:");
for (int i = 0; i < 4; ++i) {
str->appendf(" %g", fColor4.vec()[i]);
}

View File

@ -228,10 +228,7 @@ bool SkPixmap::erase(const SkColor4f& origColor, const SkIRect* subset) const {
const SkColor4f color = origColor.pin();
if (kRGBA_F16_SkColorType != pm.colorType()) {
Sk4f c4 = Sk4f::Load(color.vec());
SkColor c;
SkNx_cast<uint8_t>(c4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
return pm.erase(c);
return pm.erase(color.toSkColor());
}
const uint64_t half4 = color.premul().toF16();

View File

@ -43,7 +43,7 @@ DEF_TEST(color_half_float, reporter) {
pm.alloc(info);
REPORTER_ASSERT(reporter, pm.getSafeSize() == SkToSizeT(w * h * sizeof(uint64_t)));
SkColor4f c4 { 0.5f, 1, 0.5f, 0.25f };
SkColor4f c4 { 1, 0.5f, 0.25f, 0.5f };
pm.erase(c4);
SkPM4f origpm4 = c4.premul();

View File

@ -37,13 +37,13 @@ DEF_TEST(SkColor4f_FromColor, reporter) {
SkColor fC;
SkColor4f fC4;
} recs[] = {
{ SK_ColorBLACK, { 1, 0, 0, 0 } },
{ SK_ColorBLACK, { 0, 0, 0, 1 } },
{ SK_ColorWHITE, { 1, 1, 1, 1 } },
{ SK_ColorRED, { 1, 1, 0, 0 } },
{ SK_ColorGREEN, { 1, 0, 1, 0 } },
{ SK_ColorBLUE, { 1, 0, 0, 1 } },
{ SK_ColorRED, { 1, 0, 0, 1 } },
{ SK_ColorGREEN, { 0, 1, 0, 1 } },
{ SK_ColorBLUE, { 0, 0, 1, 1 } },
{ 0, { 0, 0, 0, 0 } },
{ 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } },
{ 0x55AAFF00, { 2/3.0f, 1, 0, 1 / 3.0f } },
};
for (const auto& r : recs) {
@ -58,7 +58,7 @@ DEF_TEST(Color4f_premul, reporter) {
for (int i = 0; i < 1000000; ++i) {
// First just test opaque colors, so that the premul should be exact
SkColor4f c4 {
1, rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1()
rand.nextUScalar1(), rand.nextUScalar1(), rand.nextUScalar1(), 1
};
SkPM4f pm4 = c4.premul();
REPORTER_ASSERT(reporter, pm4.a() == c4.fA);