2016-05-05 19:24:31 +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.
|
|
|
|
*/
|
|
|
|
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkTypes.h"
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "GrCaps.h"
|
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrContextFactory.h"
|
2018-05-11 14:14:21 +00:00
|
|
|
#include "GrContextPriv.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "GrShaderCaps.h"
|
|
|
|
#include "GrTypes.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkBlendMode.h"
|
2016-07-28 22:17:34 +00:00
|
|
|
#include "SkCanvas.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkColor.h"
|
2017-01-26 14:32:33 +00:00
|
|
|
#include "SkColorFilter.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkImageInfo.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
2016-07-28 22:17:34 +00:00
|
|
|
#include "SkSurface.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkTemplates.h"
|
2016-05-05 19:24:31 +00:00
|
|
|
#include "SkUtils.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
2016-05-05 19:24:31 +00:00
|
|
|
|
|
|
|
/** convert 0..1 linear value to 0..1 srgb */
|
2017-01-26 14:32:33 +00:00
|
|
|
static float linear_to_srgb(float linear) {
|
2016-05-05 19:24:31 +00:00
|
|
|
if (linear <= 0.0031308) {
|
|
|
|
return linear * 12.92f;
|
|
|
|
} else {
|
|
|
|
return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
/** convert 0..1 srgb value to 0..1 linear */
|
|
|
|
static float srgb_to_linear(float srgb) {
|
|
|
|
if (srgb <= 0.04045f) {
|
|
|
|
return srgb / 12.92f;
|
2016-05-05 19:24:31 +00:00
|
|
|
} else {
|
2017-01-26 14:32:33 +00:00
|
|
|
return powf((srgb + 0.055f) / 1.055f, 2.4f);
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error,
|
|
|
|
uint32_t* expected) {
|
|
|
|
bool result = true;
|
|
|
|
uint32_t expectedColor = src & 0xff000000;
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
// Alpha should always be exactly preserved.
|
|
|
|
if ((src & 0xff000000) != (dst & 0xff000000)) {
|
|
|
|
result = false;
|
|
|
|
}
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2017-07-07 12:28:13 +00:00
|
|
|
// need to unpremul before we can perform srgb magic
|
|
|
|
float invScale = 0;
|
|
|
|
float alpha = SkGetPackedA32(src);
|
|
|
|
if (alpha) {
|
|
|
|
invScale = 255.0f / alpha;
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
for (int c = 0; c < 3; ++c) {
|
2017-07-07 12:28:13 +00:00
|
|
|
float srcComponent = ((src & (0xff << (c * 8))) >> (c * 8)) * invScale;
|
|
|
|
float lower = SkTMax(0.f, srcComponent - error);
|
|
|
|
float upper = SkTMin(255.f, srcComponent + error);
|
2017-01-26 14:32:33 +00:00
|
|
|
if (toSRGB) {
|
|
|
|
lower = linear_to_srgb(lower / 255.f);
|
|
|
|
upper = linear_to_srgb(upper / 255.f);
|
|
|
|
} else {
|
|
|
|
lower = srgb_to_linear(lower / 255.f);
|
|
|
|
upper = srgb_to_linear(upper / 255.f);
|
|
|
|
}
|
2017-07-07 12:28:13 +00:00
|
|
|
lower *= alpha;
|
|
|
|
upper *= alpha;
|
2017-01-26 14:32:33 +00:00
|
|
|
SkASSERT(lower >= 0.f && lower <= 255.f);
|
|
|
|
SkASSERT(upper >= 0.f && upper <= 255.f);
|
|
|
|
uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8);
|
2017-07-07 12:28:13 +00:00
|
|
|
if (dstComponent < SkScalarFloorToInt(lower) ||
|
|
|
|
dstComponent > SkScalarCeilToInt(upper)) {
|
2017-01-26 14:32:33 +00:00
|
|
|
result = false;
|
|
|
|
}
|
2017-07-07 12:28:13 +00:00
|
|
|
uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f);
|
2017-01-26 14:32:33 +00:00
|
|
|
expectedColor |= expectedComponent << (c * 8);
|
2016-05-05 19:24:31 +00:00
|
|
|
}
|
2017-01-26 14:32:33 +00:00
|
|
|
|
|
|
|
*expected = expectedColor;
|
|
|
|
return result;
|
2016-05-05 19:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
2017-07-07 12:28:13 +00:00
|
|
|
static const int kW = 256;
|
|
|
|
static const int kH = 256;
|
2016-05-05 19:24:31 +00:00
|
|
|
static const size_t kRowBytes = sizeof(uint32_t) * kW;
|
|
|
|
|
|
|
|
GrSurfaceDesc baseDesc;
|
|
|
|
baseDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
baseDesc.fWidth = kW;
|
|
|
|
baseDesc.fHeight = kH;
|
|
|
|
|
2016-07-28 22:17:34 +00:00
|
|
|
const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH);
|
|
|
|
|
2016-05-05 19:24:31 +00:00
|
|
|
SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
|
2017-07-07 12:28:13 +00:00
|
|
|
for (int y = 0; y < kH; ++y) {
|
|
|
|
for (int x = 0; x < kW; ++x) {
|
|
|
|
srcPixels.get()[y*kW+x] = SkPreMultiplyARGB(x, y, x, 0xFF);
|
|
|
|
}
|
2016-05-05 19:24:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 15:06:20 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.installPixels(ii, srcPixels.get(), kRowBytes);
|
2016-05-05 19:24:31 +00:00
|
|
|
|
|
|
|
SkAutoTMalloc<uint32_t> read(kW * kH);
|
|
|
|
|
|
|
|
// We allow more error on GPUs with lower precision shader variables.
|
2019-02-04 18:26:26 +00:00
|
|
|
float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
for (auto toSRGB : { false, true }) {
|
|
|
|
sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
if (!dst) {
|
|
|
|
ERRORF(reporter, "Could not create surfaces for copy surface test.");
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
SkCanvas* dstCanvas = dst->getCanvas();
|
2016-07-28 22:17:34 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
dstCanvas->clear(SK_ColorRED);
|
2019-03-04 16:00:10 +00:00
|
|
|
dst->flush();
|
2016-07-28 22:17:34 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
SkPaint gammaPaint;
|
|
|
|
gammaPaint.setBlendMode(SkBlendMode::kSrc);
|
2017-07-05 19:43:15 +00:00
|
|
|
gammaPaint.setColorFilter(toSRGB ? SkColorFilter::MakeLinearToSRGBGamma()
|
|
|
|
: SkColorFilter::MakeSRGBToLinearGamma());
|
2016-07-28 22:17:34 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint);
|
2019-03-04 16:00:10 +00:00
|
|
|
dst->flush();
|
2016-07-28 22:17:34 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
sk_memset32(read.get(), 0, kW * kH);
|
2017-07-21 18:24:29 +00:00
|
|
|
if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
|
2017-01-26 14:32:33 +00:00
|
|
|
ERRORF(reporter, "Error calling readPixels");
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-05 19:24:31 +00:00
|
|
|
|
2017-01-26 14:32:33 +00:00
|
|
|
bool abort = false;
|
|
|
|
// Validate that pixels were copied/transformed correctly.
|
|
|
|
for (int y = 0; y < kH && !abort; ++y) {
|
|
|
|
for (int x = 0; x < kW && !abort; ++x) {
|
|
|
|
uint32_t r = read.get()[y * kW + x];
|
|
|
|
uint32_t s = srcPixels.get()[y * kW + x];
|
|
|
|
uint32_t expected;
|
|
|
|
if (!check_gamma(s, r, toSRGB, error, &expected)) {
|
|
|
|
ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x "
|
|
|
|
"from src 0x%08x and mode %s. Got %08x", x, y, expected, s,
|
|
|
|
toSRGB ? "ToSRGB" : "ToLinear", r);
|
|
|
|
abort = true;
|
|
|
|
break;
|
2016-05-05 19:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|