2016-01-22 09:04:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-04-08 21:24:37 +00:00
|
|
|
#include "SkBitmapProcShader.h"
|
2016-01-22 09:04:29 +00:00
|
|
|
#include "SkColor.h"
|
2016-01-23 22:19:06 +00:00
|
|
|
#include "SkColorMatrixFilter.h"
|
2016-01-24 03:37:56 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkImage.h"
|
2016-02-18 20:39:14 +00:00
|
|
|
#include "SkPM4f.h"
|
2016-01-24 03:37:56 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
|
2016-01-22 09:04:29 +00:00
|
|
|
#include "Test.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
|
2016-01-24 03:37:56 +00:00
|
|
|
const float kTolerance = 1.0f / (1 << 20);
|
|
|
|
|
|
|
|
static bool nearly_equal(float a, float b, float tol = kTolerance) {
|
|
|
|
SkASSERT(tol >= 0);
|
|
|
|
return fabsf(a - b) <= tol;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nearly_equal(const SkPM4f a, const SkPM4f& b, float tol = kTolerance) {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!nearly_equal(a.fVec[i], b.fVec[i], tol)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-22 09:04:29 +00:00
|
|
|
DEF_TEST(SkColor4f_FromColor, reporter) {
|
|
|
|
const struct {
|
|
|
|
SkColor fC;
|
|
|
|
SkColor4f fC4;
|
|
|
|
} recs[] = {
|
|
|
|
{ SK_ColorBLACK, { 1, 0, 0, 0 } },
|
|
|
|
{ SK_ColorWHITE, { 1, 1, 1, 1 } },
|
|
|
|
{ SK_ColorRED, { 1, 1, 0, 0 } },
|
|
|
|
{ SK_ColorGREEN, { 1, 0, 1, 0 } },
|
|
|
|
{ SK_ColorBLUE, { 1, 0, 0, 1 } },
|
|
|
|
{ 0, { 0, 0, 0, 0 } },
|
|
|
|
{ 0x55AAFF00, { 1/3.0f, 2/3.0f, 1, 0 } },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& r : recs) {
|
|
|
|
SkColor4f c4 = SkColor4f::FromColor(r.fC);
|
|
|
|
REPORTER_ASSERT(reporter, c4 == r.fC4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:37:56 +00:00
|
|
|
DEF_TEST(Color4f_premul, reporter) {
|
2016-01-22 09:04:29 +00:00
|
|
|
SkRandom rand;
|
|
|
|
|
|
|
|
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()
|
|
|
|
};
|
|
|
|
SkPM4f pm4 = c4.premul();
|
2016-03-08 18:09:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, pm4.a() == c4.fA);
|
|
|
|
REPORTER_ASSERT(reporter, pm4.r() == c4.fA * c4.fR);
|
|
|
|
REPORTER_ASSERT(reporter, pm4.g() == c4.fA * c4.fG);
|
|
|
|
REPORTER_ASSERT(reporter, pm4.b() == c4.fA * c4.fB);
|
2016-01-22 09:04:29 +00:00
|
|
|
|
|
|
|
// We compare with a tolerance, in case our premul multiply is implemented at slightly
|
|
|
|
// different precision than the test code.
|
|
|
|
c4.fA = rand.nextUScalar1();
|
|
|
|
pm4 = c4.premul();
|
|
|
|
REPORTER_ASSERT(reporter, pm4.fVec[SK_A_INDEX] == c4.fA);
|
2016-03-08 18:09:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(pm4.r(), c4.fA * c4.fR));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(pm4.g(), c4.fA * c4.fG));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(pm4.b(), c4.fA * c4.fB));
|
2016-01-22 09:04:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-24 03:37:56 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
static sk_sp<SkColorFilter> make_mode_cf() {
|
|
|
|
return SkColorFilter::MakeModeFilter(0xFFBB8855, SkXfermode::kPlus_Mode);
|
2016-01-24 19:15:14 +00:00
|
|
|
}
|
2016-01-24 03:37:56 +00:00
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
static sk_sp<SkColorFilter> make_mx_cf() {
|
2016-01-24 19:15:14 +00:00
|
|
|
const float mx[] = {
|
|
|
|
0.5f, 0, 0, 0, 0.1f,
|
|
|
|
0, 0.5f, 0, 0, 0.2f,
|
|
|
|
0, 0, 1, 0, -0.1f,
|
|
|
|
0, 0, 0, 1, 0,
|
|
|
|
};
|
2016-03-22 17:17:23 +00:00
|
|
|
return SkColorFilter::MakeMatrixFilterRowMajor255(mx);
|
2016-01-24 19:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
static sk_sp<SkColorFilter> make_compose_cf() {
|
|
|
|
return SkColorFilter::MakeComposeFilter(make_mode_cf(), make_mx_cf());
|
2016-01-24 19:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 22:47:34 +00:00
|
|
|
static sk_sp<SkShader> make_color_sh() { return SkShader::MakeColorShader(0xFFBB8855); }
|
2016-01-24 19:15:14 +00:00
|
|
|
|
2016-03-09 22:47:34 +00:00
|
|
|
static sk_sp<SkShader> make_image_sh() {
|
2016-01-24 03:37:56 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
|
|
|
|
const SkPMColor pixels[] {
|
|
|
|
SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
|
|
|
|
SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
|
|
|
|
SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
|
|
|
|
SkPackARGB32(0xFF, 0xBB, 0x88, 0x55),
|
|
|
|
};
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(SkImage::MakeRasterCopy(SkPixmap(info, pixels, sizeof(SkPMColor) * 2)));
|
2016-03-09 22:47:34 +00:00
|
|
|
return image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
2016-01-24 03:37:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 22:47:34 +00:00
|
|
|
static sk_sp<SkShader> make_grad_sh() {
|
2016-03-08 18:09:18 +00:00
|
|
|
#if 0
|
2016-01-24 03:37:56 +00:00
|
|
|
const SkPoint pts[] {{ 0, 0 }, { 100, 100 }};
|
|
|
|
const SkColor colors[] { SK_ColorRED, SK_ColorBLUE };
|
|
|
|
return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2016-03-08 18:09:18 +00:00
|
|
|
#else
|
|
|
|
// TODO: need to convert new gradient code to enforce PM4f --> RGBA order
|
|
|
|
return make_color_sh();
|
|
|
|
#endif
|
2016-01-24 03:37:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 22:47:34 +00:00
|
|
|
static sk_sp<SkShader> make_cf_sh() {
|
2016-03-22 17:17:23 +00:00
|
|
|
return make_color_sh()->makeWithColorFilter(make_mx_cf());
|
2016-01-24 19:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 18:09:18 +00:00
|
|
|
static bool compare_spans(const SkPM4f span4f[], const SkPMColor span4b[], int count,
|
|
|
|
float tolerance = 1.0f/255) {
|
2016-01-24 03:37:56 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
SkPM4f c0 = SkPM4f::FromPMColor(span4b[i]);
|
|
|
|
SkPM4f c1 = span4f[i];
|
2016-03-08 18:09:18 +00:00
|
|
|
if (!nearly_equal(c0, c1, tolerance)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-24 03:37:56 +00:00
|
|
|
}
|
2016-03-08 18:09:18 +00:00
|
|
|
return true;
|
2016-01-24 03:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Color4f_shader, reporter) {
|
|
|
|
struct {
|
2016-03-09 22:47:34 +00:00
|
|
|
sk_sp<SkShader> (*fFact)();
|
|
|
|
bool fSupports4f;
|
|
|
|
float fTolerance;
|
2016-01-24 03:37:56 +00:00
|
|
|
} recs[] = {
|
2016-02-23 21:26:28 +00:00
|
|
|
{ make_color_sh, true, 1.0f/255 },
|
|
|
|
// PMColor 4f gradients are interpolated in 255-multiplied values, so we need a
|
|
|
|
// slightly relaxed tolerance to accommodate the cumulative precision deviation.
|
|
|
|
{ make_grad_sh, true, 1.001f/255 },
|
|
|
|
{ make_image_sh, false, 1.0f/255 },
|
|
|
|
{ make_cf_sh, true, 1.0f/255 },
|
2016-01-24 03:37:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
for (const auto& rec : recs) {
|
2016-04-08 21:24:37 +00:00
|
|
|
uint32_t storage[kSkBlitterContextSize];
|
2016-03-09 22:47:34 +00:00
|
|
|
paint.setShader(rec.fFact());
|
2016-02-23 01:19:04 +00:00
|
|
|
// Encourage 4f context selection. At some point we may need
|
|
|
|
// to instantiate two separate contexts for optimal 4b/4f selection.
|
|
|
|
const SkShader::ContextRec contextRec(paint, SkMatrix::I(), nullptr,
|
|
|
|
SkShader::ContextRec::kPM4f_DstType);
|
2016-02-22 18:39:41 +00:00
|
|
|
SkASSERT(paint.getShader()->contextSize(contextRec) <= sizeof(storage));
|
|
|
|
SkShader::Context* ctx = paint.getShader()->createContext(contextRec, storage);
|
2016-02-24 03:02:20 +00:00
|
|
|
if (rec.fSupports4f) {
|
2016-01-24 03:37:56 +00:00
|
|
|
const int N = 100;
|
|
|
|
SkPM4f buffer4f[N];
|
|
|
|
ctx->shadeSpan4f(0, 0, buffer4f, N);
|
|
|
|
SkPMColor buffer4b[N];
|
|
|
|
ctx->shadeSpan(0, 0, buffer4b, N);
|
2016-03-08 18:09:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, compare_spans(buffer4f, buffer4b, N, rec.fTolerance));
|
2016-01-24 03:37:56 +00:00
|
|
|
}
|
|
|
|
ctx->~Context();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Color4f_colorfilter, reporter) {
|
|
|
|
struct {
|
2016-03-22 17:17:23 +00:00
|
|
|
sk_sp<SkColorFilter> (*fFact)();
|
|
|
|
bool fSupports4f;
|
|
|
|
const char* fName;
|
2016-01-24 03:37:56 +00:00
|
|
|
} recs[] = {
|
2016-03-08 18:09:18 +00:00
|
|
|
{ make_mode_cf, true, "mode" },
|
|
|
|
{ make_mx_cf, true, "matrix" },
|
|
|
|
{ make_compose_cf, true, "compose" },
|
2016-01-24 03:37:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// prepare the src
|
|
|
|
const int N = 100;
|
|
|
|
SkPMColor src4b[N];
|
|
|
|
SkPM4f src4f[N];
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
src4b[i] = SkPreMultiplyColor(rand.nextU());
|
|
|
|
src4f[i] = SkPM4f::FromPMColor(src4b[i]);
|
|
|
|
}
|
|
|
|
// confirm that our srcs are (nearly) equal
|
2016-03-08 18:09:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, compare_spans(src4f, src4b, N));
|
2016-01-24 03:37:56 +00:00
|
|
|
|
|
|
|
for (const auto& rec : recs) {
|
2016-03-22 17:17:23 +00:00
|
|
|
auto filter(rec.fFact());
|
2016-02-19 21:38:53 +00:00
|
|
|
SkPMColor dst4b[N];
|
|
|
|
filter->filterSpan(src4b, N, dst4b);
|
|
|
|
SkPM4f dst4f[N];
|
|
|
|
filter->filterSpan4f(src4f, N, dst4f);
|
2016-03-08 18:09:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, compare_spans(dst4f, dst4b, N));
|
2016-01-24 03:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-08 20:56:56 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
typedef SkPM4f (*SkXfermodeProc4f)(const SkPM4f& src, const SkPM4f& dst);
|
|
|
|
|
|
|
|
static bool compare_procs(SkXfermodeProc proc32, SkXfermodeProc4f proc4f) {
|
|
|
|
const float kTolerance = 1.0f / 255;
|
|
|
|
|
|
|
|
const SkColor colors[] = {
|
|
|
|
0, 0xFF000000, 0xFFFFFFFF, 0x80FF0000
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto s32 : colors) {
|
|
|
|
SkPMColor s_pm32 = SkPreMultiplyColor(s32);
|
|
|
|
SkPM4f s_pm4f = SkColor4f::FromColor(s32).premul();
|
|
|
|
for (auto d32 : colors) {
|
|
|
|
SkPMColor d_pm32 = SkPreMultiplyColor(d32);
|
|
|
|
SkPM4f d_pm4f = SkColor4f::FromColor(d32).premul();
|
|
|
|
|
|
|
|
SkPMColor r32 = proc32(s_pm32, d_pm32);
|
|
|
|
SkPM4f r4f = proc4f(s_pm4f, d_pm4f);
|
|
|
|
|
|
|
|
SkPM4f r32_4f = SkPM4f::FromPMColor(r32);
|
|
|
|
if (!nearly_equal(r4f, r32_4f, kTolerance)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that our Proc and Proc4f return (nearly) the same results
|
|
|
|
//
|
|
|
|
DEF_TEST(Color4f_xfermode_proc4f, reporter) {
|
|
|
|
// TODO: extend xfermodes so that all cases can be tested.
|
|
|
|
//
|
|
|
|
for (int mode = SkXfermode::kClear_Mode; mode <= SkXfermode::kScreen_Mode; ++mode) {
|
|
|
|
SkXfermodeProc proc32 = SkXfermode::GetProc((SkXfermode::Mode)mode);
|
|
|
|
SkXfermodeProc4f proc4f = SkXfermode::GetProc4f((SkXfermode::Mode)mode);
|
|
|
|
REPORTER_ASSERT(reporter, compare_procs(proc32, proc4f));
|
|
|
|
}
|
|
|
|
}
|