blob: 79d86911ff297667f570b9977c2678c93612b6a5 [file] [log] [blame]
brianosman33f6b3f2016-06-02 05:49:21 -07001/*
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 "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
brianosman20471892016-12-02 06:43:32 -080013#include "gl/GrGLGpu.h"
brianosman33f6b3f2016-06-02 05:49:21 -070014#include "SkCanvas.h"
15#include "SkSurface.h"
16
17// using anonymous namespace because these functions are used as template params.
18namespace {
19/** convert 0..1 srgb value to 0..1 linear */
20float srgb_to_linear(float srgb) {
21 if (srgb <= 0.04045f) {
22 return srgb / 12.92f;
23 } else {
24 return powf((srgb + 0.055f) / 1.055f, 2.4f);
25 }
26}
27
28/** convert 0..1 linear value to 0..1 srgb */
29float linear_to_srgb(float linear) {
30 if (linear <= 0.0031308) {
31 return linear * 12.92f;
32 } else {
33 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
34 }
35}
36}
37
38static bool check_value(U8CPU value, U8CPU expected, U8CPU error) {
39 if (value >= expected) {
40 return (value - expected) <= error;
41 } else {
42 return (expected - value) <= error;
43 }
44}
45
46void read_and_check_pixels(skiatest::Reporter* reporter, GrTexture* texture, U8CPU expected,
47 U8CPU error, const char* subtestName) {
48 int w = texture->width();
49 int h = texture->height();
50 SkAutoTMalloc<uint32_t> readData(w * h);
51 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
52 if (!texture->readPixels(0, 0, w, h, texture->config(), readData.get())) {
53 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
54 return;
55 }
56 for (int j = 0; j < h; ++j) {
57 for (int i = 0; i < w; ++i) {
58 uint32_t read = readData[j * w + i];
59
60 bool success =
61 check_value(read & 0xff, expected, error) &&
62 check_value((read >> 8) & 0xff, expected, error) &&
63 check_value((read >> 16) & 0xff, expected, error);
64
65 if (!success) {
66 ERRORF(reporter, "Expected 0xff%02x%02x%02x, read back as 0x%08x in %s at %d, %d.",
67 expected, expected, expected, read, subtestName, i, j);
68 return;
69 }
70 }
71 }
72}
73
74DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) {
75 GrContext* context = ctxInfo.grContext();
76 if (!context->caps()->srgbSupport()) {
77 return;
78 }
79
80 const int rtS = 16;
81 const int texS = rtS * 2;
82
83 // Fill texture with a dither of black and 60% sRGB (~ 32.5% linear) gray. Although there is
84 // only one likely failure mode (doing a direct downsample of the sRGB values), this pattern
85 // maximizes the minimum error across all three conceivable failure modes:
86 // 1) Likely incorrect:
87 // (A + B) / 2
88 // 2) No input decode, decode output:
89 // linear_to_srgb((A + B) / 2)
90 // 3) Decode input, no output encode:
91 // (srgb_to_linear(A) + srgb_to_linear(B)) / 2
92
93 const U8CPU srgb60 = sk_float_round2int(0.6f * 255.0f);
94 static const SkPMColor colors[2] = {
95 SkPackARGB32(0xFF, srgb60, srgb60, srgb60),
96 SkPackARGB32(0xFF, 0x00, 0x00, 0x00)
97 };
98 uint32_t texData[texS * texS];
99 for (int y = 0; y < texS; ++y) {
100 for (int x = 0; x < texS; ++x) {
101 texData[y * texS + x] = colors[(x + y) % 2];
102 }
103 }
104
105 // We can be pretty generous with the error detection, thanks to the choice of input.
106 // The closest likely failure mode is off by > 0.1, so anything that encodes within
107 // 10/255 of optimal is more than good enough for this test.
108 const U8CPU expectedSRGB = sk_float_round2int(
109 linear_to_srgb(srgb_to_linear(srgb60 / 255.0f) / 2.0f) * 255.0f);
110 const U8CPU expectedLinear = srgb60 / 2;
111 const U8CPU error = 10;
112
113 // Create our test texture
114 GrSurfaceDesc desc;
115 desc.fFlags = kNone_GrSurfaceFlags;
Brian Osman777b5632016-10-14 09:16:21 -0400116 desc.fConfig = kSRGBA_8888_GrPixelConfig;
brianosman33f6b3f2016-06-02 05:49:21 -0700117 desc.fWidth = texS;
118 desc.fHeight = texS;
119
120 GrTextureProvider* texProvider = context->textureProvider();
Hal Canary342b7ac2016-11-04 11:49:42 -0400121 sk_sp<GrTexture> texture(texProvider->createTexture(desc, SkBudgeted::kNo, texData, 0));
brianosman33f6b3f2016-06-02 05:49:21 -0700122
Brian Salomonaeac5d82016-10-29 00:13:29 -0400123 // Create two render target contexts (L32 and S32)
Brian Osman526972e2016-10-24 09:24:02 -0400124 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
Brian Osman11052242016-10-27 14:47:55 -0400125 sk_sp<GrRenderTargetContext> l32RenderTargetContext = context->makeRenderTargetContext(
126 SkBackingFit::kExact, rtS, rtS, kRGBA_8888_GrPixelConfig, nullptr);
127 sk_sp<GrRenderTargetContext> s32RenderTargetContext = context->makeRenderTargetContext(
128 SkBackingFit::kExact, rtS, rtS, kSRGBA_8888_GrPixelConfig, std::move(srgbColorSpace));
brianosman33f6b3f2016-06-02 05:49:21 -0700129
130 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS));
131 GrNoClip noClip;
132 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400133 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon514baff2016-11-17 15:17:07 -0500134 GrSamplerParams mipMapParams(SkShader::kRepeat_TileMode, GrSamplerParams::kMipMap_FilterMode);
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000135 paint.addColorTextureProcessor(texture.get(), nullptr, SkMatrix::MakeScale(0.5f), mipMapParams);
brianosman33f6b3f2016-06-02 05:49:21 -0700136
137 // 1) Draw texture to S32 surface (should generate/use sRGB mips)
138 paint.setGammaCorrect(true);
Brian Salomon82f44312017-01-11 13:42:54 -0500139 s32RenderTargetContext->drawRect(noClip, GrPaint(paint), GrAA::kNo, SkMatrix::I(), rect);
Brian Osman11052242016-10-27 14:47:55 -0400140 read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
bsalomon9061aa42016-07-19 08:49:41 -0700141 "first render of sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700142
143 // 2) Draw texture to L32 surface (should generate/use linear mips)
144 paint.setGammaCorrect(false);
Brian Salomon82f44312017-01-11 13:42:54 -0500145 l32RenderTargetContext->drawRect(noClip, GrPaint(paint), GrAA::kNo, SkMatrix::I(), rect);
brianosman20471892016-12-02 06:43:32 -0800146
147 // Right now, this test only runs on GL (because Vulkan doesn't support legacy mip-mapping
148 // skbug.com/5048). On GL, we may not have sRGB decode support. In that case, rendering sRGB
149 // textures to a legacy surface produces nonsense, so this part of the test is meaningless.
150 //
brianosman851c2382016-12-07 10:03:25 -0800151 // We also skip this part of the test on command buffer (via srgbDecodeDisableAffectsMipmaps),
152 // because that implementation of the extension doesn't ensure that mips respect the setting.
153 //
brianosman20471892016-12-02 06:43:32 -0800154 // TODO: Once Vulkan supports legacy mip-mapping, we can promote this to GrCaps. Right now,
155 // Vulkan has most of the functionality, but not the mip-mapping part that's being tested here.
156 GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->getGpu());
brianosman851c2382016-12-07 10:03:25 -0800157 if (glGpu->glCaps().srgbDecodeDisableSupport() &&
158 glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) {
brianosman20471892016-12-02 06:43:32 -0800159 read_and_check_pixels(reporter, l32RenderTargetContext->asTexture().get(), expectedLinear,
160 error, "re-render as linear");
161 }
brianosman33f6b3f2016-06-02 05:49:21 -0700162
163 // 3) Go back to sRGB
164 paint.setGammaCorrect(true);
Brian Salomon82f44312017-01-11 13:42:54 -0500165 s32RenderTargetContext->drawRect(noClip, std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Brian Osman11052242016-10-27 14:47:55 -0400166 read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
bsalomon9061aa42016-07-19 08:49:41 -0700167 "re-render as sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700168}
169#endif