2015-07-30 22:34:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
#if SK_SUPPORT_GPU
|
2015-12-01 12:35:26 +00:00
|
|
|
#include "GrCaps.h"
|
|
|
|
#include "GrContext.h"
|
2017-03-29 16:08:49 +00:00
|
|
|
#include "GrContextPriv.h"
|
|
|
|
#include "GrSurfaceContext.h"
|
2015-07-30 22:34:56 +00:00
|
|
|
#include "SkCanvas.h"
|
2017-03-29 16:08:49 +00:00
|
|
|
#include "SkGr.h"
|
2015-07-30 22:34:56 +00:00
|
|
|
#include "SkSurface.h"
|
|
|
|
|
|
|
|
// using anonymous namespace because these functions are used as template params.
|
|
|
|
namespace {
|
|
|
|
/** convert 0..1 srgb value to 0..1 linear */
|
|
|
|
float srgb_to_linear(float srgb) {
|
|
|
|
if (srgb <= 0.04045f) {
|
|
|
|
return srgb / 12.92f;
|
|
|
|
} else {
|
|
|
|
return powf((srgb + 0.055f) / 1.055f, 2.4f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** convert 0..1 linear value to 0..1 srgb */
|
|
|
|
float linear_to_srgb(float linear) {
|
|
|
|
if (linear <= 0.0031308) {
|
|
|
|
return linear * 12.92f;
|
|
|
|
} else {
|
|
|
|
return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** tests a conversion with an error tolerance */
|
|
|
|
template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
|
|
|
|
float error) {
|
|
|
|
// alpha should always be exactly preserved.
|
|
|
|
if ((input & 0xff000000) != (output & 0xff000000)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int c = 0; c < 3; ++c) {
|
|
|
|
uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
|
|
|
|
float lower = SkTMax(0.f, (float) inputComponent - error);
|
|
|
|
float upper = SkTMin(255.f, (float) inputComponent + error);
|
|
|
|
lower = CONVERT(lower / 255.f);
|
|
|
|
upper = CONVERT(upper / 255.f);
|
|
|
|
SkASSERT(lower >= 0.f && lower <= 255.f);
|
|
|
|
SkASSERT(upper >= 0.f && upper <= 255.f);
|
|
|
|
uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
|
|
|
|
if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
|
|
|
|
outputComponent > SkScalarCeilToInt(upper * 255.f)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** tests a forward and backward conversion with an error tolerance */
|
|
|
|
template <float (*FORWARD)(float), float (*BACKWARD)(float)>
|
|
|
|
static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
|
|
|
|
// alpha should always be exactly preserved.
|
|
|
|
if ((input & 0xff000000) != (output & 0xff000000)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int c = 0; c < 3; ++c) {
|
|
|
|
uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
|
|
|
|
float lower = SkTMax(0.f, (float) inputComponent - error);
|
|
|
|
float upper = SkTMin(255.f, (float) inputComponent + error);
|
|
|
|
lower = FORWARD(lower / 255.f);
|
|
|
|
upper = FORWARD(upper / 255.f);
|
|
|
|
SkASSERT(lower >= 0.f && lower <= 255.f);
|
|
|
|
SkASSERT(upper >= 0.f && upper <= 255.f);
|
|
|
|
uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
|
|
|
|
uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
|
|
|
|
lower = SkTMax(0.f, (float) lowerComponent - error);
|
|
|
|
upper = SkTMin(255.f, (float) upperComponent + error);
|
|
|
|
lower = BACKWARD(lowerComponent / 255.f);
|
|
|
|
upper = BACKWARD(upperComponent / 255.f);
|
|
|
|
SkASSERT(lower >= 0.f && lower <= 255.f);
|
|
|
|
SkASSERT(upper >= 0.f && upper <= 255.f);
|
|
|
|
upperComponent = SkScalarCeilToInt(upper * 255.f);
|
|
|
|
lowerComponent = SkScalarFloorToInt(lower * 255.f);
|
|
|
|
|
|
|
|
uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
|
|
|
|
if (outputComponent < lowerComponent || outputComponent > upperComponent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
|
|
|
|
return check_conversion<srgb_to_linear>(srgb, linear, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
|
|
|
|
return check_conversion<linear_to_srgb>(linear, srgb, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
|
|
|
|
return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
|
|
|
|
return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
|
|
|
|
|
2017-03-29 16:08:49 +00:00
|
|
|
void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
|
|
|
|
uint32_t* origData,
|
|
|
|
const SkImageInfo& dstInfo, CheckFn checker, float error,
|
2015-07-30 22:34:56 +00:00
|
|
|
const char* subtestName) {
|
2017-03-29 16:08:49 +00:00
|
|
|
int w = dstInfo.width();
|
|
|
|
int h = dstInfo.height();
|
2015-07-30 22:34:56 +00:00
|
|
|
SkAutoTMalloc<uint32_t> readData(w * h);
|
|
|
|
memset(readData.get(), 0, sizeof(uint32_t) * w * h);
|
2017-03-29 16:08:49 +00:00
|
|
|
|
|
|
|
if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
|
2015-07-30 22:34:56 +00:00
|
|
|
ERRORF(reporter, "Could not read pixels for %s.", subtestName);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-29 16:08:49 +00:00
|
|
|
|
2015-07-30 22:34:56 +00:00
|
|
|
for (int j = 0; j < h; ++j) {
|
|
|
|
for (int i = 0; i < w; ++i) {
|
|
|
|
uint32_t orig = origData[j * w + i];
|
|
|
|
uint32_t read = readData[j * w + i];
|
|
|
|
|
|
|
|
if (!checker(orig, read, error)) {
|
|
|
|
ERRORF(reporter, "Expected 0x%08x, read back as 0x%08x in %s at %d, %d).",
|
|
|
|
orig, read, subtestName, i, j);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add tests for copySurface between srgb/linear textures. Add tests for unpremul/premul
|
|
|
|
// conversion during read/write along with srgb/linear conversions.
|
2016-06-28 15:07:26 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
2017-12-19 14:09:33 +00:00
|
|
|
#if defined(SK_BUILD_FOR_GOOGLE3)
|
|
|
|
// Stack frame size is limited in SK_BUILD_FOR_GOOGLE3.
|
2015-10-19 20:55:55 +00:00
|
|
|
static const int kW = 63;
|
|
|
|
static const int kH = 63;
|
|
|
|
#else
|
2015-07-30 22:34:56 +00:00
|
|
|
static const int kW = 255;
|
|
|
|
static const int kH = 255;
|
2015-10-19 20:55:55 +00:00
|
|
|
#endif
|
2015-07-30 22:34:56 +00:00
|
|
|
uint32_t origData[kW * kH];
|
|
|
|
for (int j = 0; j < kH; ++j) {
|
|
|
|
for (int i = 0; i < kW; ++i) {
|
|
|
|
origData[j * kW + i] = (j << 24) | (i << 16) | (i << 8) | i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:08:49 +00:00
|
|
|
const SkImageInfo iiSRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType,
|
|
|
|
SkColorSpace::MakeSRGB());
|
|
|
|
const SkImageInfo iiRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
2015-12-01 12:35:26 +00:00
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
2017-04-06 11:59:41 +00:00
|
|
|
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
2015-12-01 12:35:26 +00:00
|
|
|
desc.fWidth = kW;
|
|
|
|
desc.fHeight = kH;
|
|
|
|
desc.fConfig = kSRGBA_8888_GrPixelConfig;
|
|
|
|
if (context->caps()->isConfigRenderable(desc.fConfig, false) &&
|
|
|
|
context->caps()->isConfigTexturable(desc.fConfig)) {
|
2017-03-29 16:08:49 +00:00
|
|
|
|
|
|
|
sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
|
2017-10-30 17:39:09 +00:00
|
|
|
desc, GrMipMapped::kNo,
|
|
|
|
SkBackingFit::kExact,
|
|
|
|
SkBudgeted::kNo);
|
2017-03-29 16:08:49 +00:00
|
|
|
if (!sContext) {
|
|
|
|
ERRORF(reporter, "Could not create SRGBA surface context.");
|
2015-12-01 12:35:26 +00:00
|
|
|
return;
|
2015-07-30 22:34:56 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 01:27:09 +00:00
|
|
|
float error = context->caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
|
2015-12-01 12:35:26 +00:00
|
|
|
|
|
|
|
// Write srgba data and read as srgba and then as rgba
|
2017-03-29 16:08:49 +00:00
|
|
|
if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
|
2015-12-01 12:35:26 +00:00
|
|
|
// For the all-srgba case, we allow a small error only for devices that have
|
|
|
|
// precision variation because the srgba data gets converted to linear and back in
|
|
|
|
// the shader.
|
2017-11-16 01:27:09 +00:00
|
|
|
float smallError = context->caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
|
2017-03-29 16:08:49 +00:00
|
|
|
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
|
2015-12-01 12:35:26 +00:00
|
|
|
check_srgb_to_linear_to_srgb_conversion, smallError,
|
|
|
|
"write/read srgba to srgba texture");
|
2017-03-29 16:08:49 +00:00
|
|
|
read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
|
2015-12-01 12:35:26 +00:00
|
|
|
check_srgb_to_linear_conversion, error,
|
|
|
|
"write srgba/read rgba with srgba texture");
|
|
|
|
} else {
|
|
|
|
ERRORF(reporter, "Could not write srgba data to srgba texture.");
|
|
|
|
}
|
2015-07-30 22:34:56 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// Now verify that we can write linear data
|
2017-03-29 16:08:49 +00:00
|
|
|
if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
|
2015-12-01 12:35:26 +00:00
|
|
|
// We allow more error on GPUs with lower precision shader variables.
|
2017-03-29 16:08:49 +00:00
|
|
|
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
|
2015-12-01 12:35:26 +00:00
|
|
|
check_linear_to_srgb_conversion, error,
|
|
|
|
"write rgba/read srgba with srgba texture");
|
2017-03-29 16:08:49 +00:00
|
|
|
read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
|
2015-12-01 12:35:26 +00:00
|
|
|
check_linear_to_srgb_to_linear_conversion, error,
|
|
|
|
"write/read rgba with srgba texture");
|
|
|
|
} else {
|
|
|
|
ERRORF(reporter, "Could not write rgba data to srgba texture.");
|
|
|
|
}
|
2015-07-30 22:34:56 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
desc.fConfig = kRGBA_8888_GrPixelConfig;
|
2017-10-30 17:39:09 +00:00
|
|
|
sContext = context->contextPriv().makeDeferredSurfaceContext(desc,
|
|
|
|
GrMipMapped::kNo,
|
|
|
|
SkBackingFit::kExact,
|
2017-03-29 16:08:49 +00:00
|
|
|
SkBudgeted::kNo);
|
|
|
|
if (!sContext) {
|
|
|
|
ERRORF(reporter, "Could not create RGBA surface context.");
|
2015-12-01 12:35:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-30 22:34:56 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// Write srgba data to a rgba texture and read back as srgba and rgba
|
2017-03-29 16:08:49 +00:00
|
|
|
if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
|
2017-04-25 19:01:46 +00:00
|
|
|
#if 0
|
|
|
|
// We don't support this conversion (read from untagged source into tagged destination.
|
|
|
|
// If we decide there is a meaningful way to implement this, restore this test.
|
2017-03-29 16:08:49 +00:00
|
|
|
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
|
2015-12-01 12:35:26 +00:00
|
|
|
check_srgb_to_linear_to_srgb_conversion, error,
|
|
|
|
"write/read srgba to rgba texture");
|
2017-04-25 19:01:46 +00:00
|
|
|
#endif
|
|
|
|
// We expect the sRGB -> linear write to do no sRGB conversion (to match the behavior of
|
|
|
|
// drawing tagged sources). skbug.com/6547. So the data we read should still contain
|
|
|
|
// sRGB encoded values.
|
|
|
|
//
|
|
|
|
// srgb_to_linear_to_srgb is a proxy for the expected identity transform.
|
2017-03-29 16:08:49 +00:00
|
|
|
read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
|
2017-04-25 19:01:46 +00:00
|
|
|
check_srgb_to_linear_to_srgb_conversion, error,
|
2015-12-01 12:35:26 +00:00
|
|
|
"write srgba/read rgba to rgba texture");
|
|
|
|
} else {
|
|
|
|
ERRORF(reporter, "Could not write srgba data to rgba texture.");
|
|
|
|
}
|
2015-07-30 22:34:56 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// Write rgba data to a rgba texture and read back as srgba
|
2017-03-29 16:08:49 +00:00
|
|
|
if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
|
|
|
|
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
|
2015-12-01 12:35:26 +00:00
|
|
|
check_linear_to_srgb_conversion, 1.2f,
|
|
|
|
"write rgba/read srgba to rgba texture");
|
|
|
|
} else {
|
|
|
|
ERRORF(reporter, "Could not write rgba data to rgba texture.");
|
|
|
|
}
|
2015-07-30 22:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|