blob: 2da7102658452626404f678fc181c2d4dbcbe734 [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"
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrClip.h"
brianosman33f6b3f2016-06-02 05:49:21 -070012#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050014#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
brianosman33f6b3f2016-06-02 05:49:21 -070016#include "SkCanvas.h"
Robert Phillips301431d2017-03-29 12:08:49 -040017#include "SkGr.h"
brianosman33f6b3f2016-06-02 05:49:21 -070018#include "SkSurface.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050019#include "gl/GrGLGpu.h"
brianosman33f6b3f2016-06-02 05:49:21 -070020
21// using anonymous namespace because these functions are used as template params.
22namespace {
23/** convert 0..1 srgb value to 0..1 linear */
24float srgb_to_linear(float srgb) {
25 if (srgb <= 0.04045f) {
26 return srgb / 12.92f;
27 } else {
28 return powf((srgb + 0.055f) / 1.055f, 2.4f);
29 }
30}
31
32/** convert 0..1 linear value to 0..1 srgb */
33float linear_to_srgb(float linear) {
34 if (linear <= 0.0031308) {
35 return linear * 12.92f;
36 } else {
37 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
38 }
39}
40}
41
42static bool check_value(U8CPU value, U8CPU expected, U8CPU error) {
43 if (value >= expected) {
44 return (value - expected) <= error;
45 } else {
46 return (expected - value) <= error;
47 }
48}
49
Robert Phillips301431d2017-03-29 12:08:49 -040050void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
51 U8CPU expected, const SkImageInfo& dstInfo,
brianosman33f6b3f2016-06-02 05:49:21 -070052 U8CPU error, const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -040053 int w = dstInfo.width();
54 int h = dstInfo.height();
brianosman33f6b3f2016-06-02 05:49:21 -070055 SkAutoTMalloc<uint32_t> readData(w * h);
56 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -040057
58 if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
brianosman33f6b3f2016-06-02 05:49:21 -070059 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
60 return;
61 }
Robert Phillips301431d2017-03-29 12:08:49 -040062
brianosman33f6b3f2016-06-02 05:49:21 -070063 for (int j = 0; j < h; ++j) {
64 for (int i = 0; i < w; ++i) {
65 uint32_t read = readData[j * w + i];
66
67 bool success =
68 check_value(read & 0xff, expected, error) &&
69 check_value((read >> 8) & 0xff, expected, error) &&
70 check_value((read >> 16) & 0xff, expected, error);
71
72 if (!success) {
73 ERRORF(reporter, "Expected 0xff%02x%02x%02x, read back as 0x%08x in %s at %d, %d.",
74 expected, expected, expected, read, subtestName, i, j);
75 return;
76 }
77 }
78 }
79}
80
81DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) {
82 GrContext* context = ctxInfo.grContext();
Brian Salomonc7fe0f72018-05-11 10:14:21 -040083 if (!context->contextPriv().caps()->srgbSupport()) {
brianosman33f6b3f2016-06-02 05:49:21 -070084 return;
85 }
86
87 const int rtS = 16;
88 const int texS = rtS * 2;
89
90 // Fill texture with a dither of black and 60% sRGB (~ 32.5% linear) gray. Although there is
91 // only one likely failure mode (doing a direct downsample of the sRGB values), this pattern
92 // maximizes the minimum error across all three conceivable failure modes:
93 // 1) Likely incorrect:
94 // (A + B) / 2
95 // 2) No input decode, decode output:
96 // linear_to_srgb((A + B) / 2)
97 // 3) Decode input, no output encode:
98 // (srgb_to_linear(A) + srgb_to_linear(B)) / 2
99
100 const U8CPU srgb60 = sk_float_round2int(0.6f * 255.0f);
101 static const SkPMColor colors[2] = {
102 SkPackARGB32(0xFF, srgb60, srgb60, srgb60),
103 SkPackARGB32(0xFF, 0x00, 0x00, 0x00)
104 };
105 uint32_t texData[texS * texS];
106 for (int y = 0; y < texS; ++y) {
107 for (int x = 0; x < texS; ++x) {
108 texData[y * texS + x] = colors[(x + y) % 2];
109 }
110 }
111
112 // We can be pretty generous with the error detection, thanks to the choice of input.
113 // The closest likely failure mode is off by > 0.1, so anything that encodes within
114 // 10/255 of optimal is more than good enough for this test.
115 const U8CPU expectedSRGB = sk_float_round2int(
116 linear_to_srgb(srgb_to_linear(srgb60 / 255.0f) / 2.0f) * 255.0f);
117 const U8CPU expectedLinear = srgb60 / 2;
118 const U8CPU error = 10;
119
Robert Phillips301431d2017-03-29 12:08:49 -0400120 const SkImageInfo iiSRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType,
121 kPremul_SkAlphaType,
122 SkColorSpace::MakeSRGB());
123 const SkImageInfo iiRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType,
124 kPremul_SkAlphaType);
125
brianosman33f6b3f2016-06-02 05:49:21 -0700126 // Create our test texture
127 GrSurfaceDesc desc;
128 desc.fFlags = kNone_GrSurfaceFlags;
brianosman33f6b3f2016-06-02 05:49:21 -0700129 desc.fWidth = texS;
130 desc.fHeight = texS;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400131 desc.fConfig = kSRGBA_8888_GrPixelConfig;
brianosman33f6b3f2016-06-02 05:49:21 -0700132
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500133 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomon58389b92018-03-07 13:01:25 -0500134 sk_sp<GrTextureProxy> proxy =
135 proxyProvider->createTextureProxy(desc, SkBudgeted::kNo, texData, 0);
brianosman33f6b3f2016-06-02 05:49:21 -0700136
Brian Salomonaeac5d82016-10-29 00:13:29 -0400137 // Create two render target contexts (L32 and S32)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500138 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500139 sk_sp<GrRenderTargetContext> l32RenderTargetContext =
140 context->contextPriv().makeDeferredRenderTargetContext(
141 SkBackingFit::kExact, rtS, rtS,
142 kRGBA_8888_GrPixelConfig, nullptr);
143 sk_sp<GrRenderTargetContext> s32RenderTargetContext =
144 context->contextPriv().makeDeferredRenderTargetContext(
145 SkBackingFit::kExact, rtS, rtS,
146 kSRGBA_8888_GrPixelConfig, std::move(srgbColorSpace));
brianosman33f6b3f2016-06-02 05:49:21 -0700147
148 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS));
149 GrNoClip noClip;
150 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400151 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400152 GrSamplerState mipMapSamplerState(GrSamplerState::WrapMode::kRepeat,
153 GrSamplerState::Filter::kMipMap);
Brian Osman2240be92017-10-18 13:15:13 -0400154 paint.addColorTextureProcessor(std::move(proxy), SkMatrix::MakeScale(rtS), mipMapSamplerState);
brianosman33f6b3f2016-06-02 05:49:21 -0700155
156 // 1) Draw texture to S32 surface (should generate/use sRGB mips)
157 paint.setGammaCorrect(true);
Brian Salomonb74ef032017-08-10 12:46:01 -0400158 s32RenderTargetContext->drawRect(noClip, GrPaint::Clone(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillips301431d2017-03-29 12:08:49 -0400159 read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error,
bsalomon9061aa42016-07-19 08:49:41 -0700160 "first render of sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700161
162 // 2) Draw texture to L32 surface (should generate/use linear mips)
163 paint.setGammaCorrect(false);
Brian Salomonb74ef032017-08-10 12:46:01 -0400164 l32RenderTargetContext->drawRect(noClip, GrPaint::Clone(paint), GrAA::kNo, SkMatrix::I(), rect);
brianosman20471892016-12-02 06:43:32 -0800165
166 // Right now, this test only runs on GL (because Vulkan doesn't support legacy mip-mapping
167 // skbug.com/5048). On GL, we may not have sRGB decode support. In that case, rendering sRGB
168 // textures to a legacy surface produces nonsense, so this part of the test is meaningless.
169 //
brianosman851c2382016-12-07 10:03:25 -0800170 // We also skip this part of the test on command buffer (via srgbDecodeDisableAffectsMipmaps),
171 // because that implementation of the extension doesn't ensure that mips respect the setting.
172 //
brianosman20471892016-12-02 06:43:32 -0800173 // TODO: Once Vulkan supports legacy mip-mapping, we can promote this to GrCaps. Right now,
174 // Vulkan has most of the functionality, but not the mip-mapping part that's being tested here.
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500175 GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
brianosman851c2382016-12-07 10:03:25 -0800176 if (glGpu->glCaps().srgbDecodeDisableSupport() &&
177 glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) {
Robert Phillips301431d2017-03-29 12:08:49 -0400178 read_and_check_pixels(reporter, l32RenderTargetContext.get(), expectedLinear, iiRGBA,
brianosman20471892016-12-02 06:43:32 -0800179 error, "re-render as linear");
180 }
brianosman33f6b3f2016-06-02 05:49:21 -0700181
182 // 3) Go back to sRGB
183 paint.setGammaCorrect(true);
Brian Salomon82f44312017-01-11 13:42:54 -0500184 s32RenderTargetContext->drawRect(noClip, std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillips301431d2017-03-29 12:08:49 -0400185 read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error,
bsalomon9061aa42016-07-19 08:49:41 -0700186 "re-render as sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700187}
188#endif