1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "Test.h"
9 #if SK_SUPPORT_GPU
10 #include "GrCaps.h"
11 #include "GrClip.h"
12 #include "GrContext.h"
13 #include "GrRenderTargetContext.h"
14 #include "GrResourceProvider.h"
15 #include "SkCanvas.h"
16 #include "SkSurface.h"
17 #include "gl/GrGLGpu.h"
18
19 // using anonymous namespace because these functions are used as template params.
20 namespace {
21 /** convert 0..1 srgb value to 0..1 linear */
srgb_to_linear(float srgb)22 float srgb_to_linear(float srgb) {
23 if (srgb <= 0.04045f) {
24 return srgb / 12.92f;
25 } else {
26 return powf((srgb + 0.055f) / 1.055f, 2.4f);
27 }
28 }
29
30 /** convert 0..1 linear value to 0..1 srgb */
linear_to_srgb(float linear)31 float linear_to_srgb(float linear) {
32 if (linear <= 0.0031308) {
33 return linear * 12.92f;
34 } else {
35 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
36 }
37 }
38 }
39
check_value(U8CPU value,U8CPU expected,U8CPU error)40 static bool check_value(U8CPU value, U8CPU expected, U8CPU error) {
41 if (value >= expected) {
42 return (value - expected) <= error;
43 } else {
44 return (expected - value) <= error;
45 }
46 }
47
read_and_check_pixels(skiatest::Reporter * reporter,GrTexture * texture,U8CPU expected,U8CPU error,const char * subtestName)48 void read_and_check_pixels(skiatest::Reporter* reporter, GrTexture* texture, U8CPU expected,
49 U8CPU error, const char* subtestName) {
50 int w = texture->width();
51 int h = texture->height();
52 SkAutoTMalloc<uint32_t> readData(w * h);
53 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
54 if (!texture->readPixels(0, 0, w, h, texture->config(), readData.get())) {
55 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
56 return;
57 }
58 for (int j = 0; j < h; ++j) {
59 for (int i = 0; i < w; ++i) {
60 uint32_t read = readData[j * w + i];
61
62 bool success =
63 check_value(read & 0xff, expected, error) &&
64 check_value((read >> 8) & 0xff, expected, error) &&
65 check_value((read >> 16) & 0xff, expected, error);
66
67 if (!success) {
68 ERRORF(reporter, "Expected 0xff%02x%02x%02x, read back as 0x%08x in %s at %d, %d.",
69 expected, expected, expected, read, subtestName, i, j);
70 return;
71 }
72 }
73 }
74 }
75
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps,reporter,ctxInfo)76 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) {
77 GrContext* context = ctxInfo.grContext();
78 if (!context->caps()->srgbSupport()) {
79 return;
80 }
81
82 const int rtS = 16;
83 const int texS = rtS * 2;
84
85 // Fill texture with a dither of black and 60% sRGB (~ 32.5% linear) gray. Although there is
86 // only one likely failure mode (doing a direct downsample of the sRGB values), this pattern
87 // maximizes the minimum error across all three conceivable failure modes:
88 // 1) Likely incorrect:
89 // (A + B) / 2
90 // 2) No input decode, decode output:
91 // linear_to_srgb((A + B) / 2)
92 // 3) Decode input, no output encode:
93 // (srgb_to_linear(A) + srgb_to_linear(B)) / 2
94
95 const U8CPU srgb60 = sk_float_round2int(0.6f * 255.0f);
96 static const SkPMColor colors[2] = {
97 SkPackARGB32(0xFF, srgb60, srgb60, srgb60),
98 SkPackARGB32(0xFF, 0x00, 0x00, 0x00)
99 };
100 uint32_t texData[texS * texS];
101 for (int y = 0; y < texS; ++y) {
102 for (int x = 0; x < texS; ++x) {
103 texData[y * texS + x] = colors[(x + y) % 2];
104 }
105 }
106
107 // We can be pretty generous with the error detection, thanks to the choice of input.
108 // The closest likely failure mode is off by > 0.1, so anything that encodes within
109 // 10/255 of optimal is more than good enough for this test.
110 const U8CPU expectedSRGB = sk_float_round2int(
111 linear_to_srgb(srgb_to_linear(srgb60 / 255.0f) / 2.0f) * 255.0f);
112 const U8CPU expectedLinear = srgb60 / 2;
113 const U8CPU error = 10;
114
115 // Create our test texture
116 GrSurfaceDesc desc;
117 desc.fFlags = kNone_GrSurfaceFlags;
118 desc.fConfig = kSRGBA_8888_GrPixelConfig;
119 desc.fWidth = texS;
120 desc.fHeight = texS;
121
122 GrResourceProvider* resourceProvider = context->resourceProvider();
123 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(resourceProvider,
124 desc, SkBudgeted::kNo,
125 texData, 0);
126
127 // Create two render target contexts (L32 and S32)
128 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
129 sk_sp<GrRenderTargetContext> l32RenderTargetContext = context->makeRenderTargetContext(
130 SkBackingFit::kExact, rtS, rtS, kRGBA_8888_GrPixelConfig, nullptr);
131 sk_sp<GrRenderTargetContext> s32RenderTargetContext = context->makeRenderTargetContext(
132 SkBackingFit::kExact, rtS, rtS, kSRGBA_8888_GrPixelConfig, std::move(srgbColorSpace));
133
134 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS));
135 GrNoClip noClip;
136 GrPaint paint;
137 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
138 GrSamplerParams mipMapParams(SkShader::kRepeat_TileMode, GrSamplerParams::kMipMap_FilterMode);
139 paint.addColorTextureProcessor(context->resourceProvider(), std::move(proxy),
140 nullptr, SkMatrix::MakeScale(rtS), mipMapParams);
141
142 // 1) Draw texture to S32 surface (should generate/use sRGB mips)
143 paint.setGammaCorrect(true);
144 s32RenderTargetContext->drawRect(noClip, GrPaint(paint), GrAA::kNo, SkMatrix::I(), rect);
145 read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
146 "first render of sRGB");
147
148 // 2) Draw texture to L32 surface (should generate/use linear mips)
149 paint.setGammaCorrect(false);
150 l32RenderTargetContext->drawRect(noClip, GrPaint(paint), GrAA::kNo, SkMatrix::I(), rect);
151
152 // Right now, this test only runs on GL (because Vulkan doesn't support legacy mip-mapping
153 // skbug.com/5048). On GL, we may not have sRGB decode support. In that case, rendering sRGB
154 // textures to a legacy surface produces nonsense, so this part of the test is meaningless.
155 //
156 // We also skip this part of the test on command buffer (via srgbDecodeDisableAffectsMipmaps),
157 // because that implementation of the extension doesn't ensure that mips respect the setting.
158 //
159 // TODO: Once Vulkan supports legacy mip-mapping, we can promote this to GrCaps. Right now,
160 // Vulkan has most of the functionality, but not the mip-mapping part that's being tested here.
161 GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->getGpu());
162 if (glGpu->glCaps().srgbDecodeDisableSupport() &&
163 glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) {
164 read_and_check_pixels(reporter, l32RenderTargetContext->asTexture().get(), expectedLinear,
165 error, "re-render as linear");
166 }
167
168 // 3) Go back to sRGB
169 paint.setGammaCorrect(true);
170 s32RenderTargetContext->drawRect(noClip, std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
171 read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
172 "re-render as sRGB");
173 }
174 #endif
175