brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 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" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 11 | #include "GrClip.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 16 | #include "SkCanvas.h" |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 17 | #include "SkGr.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 18 | #include "SkSurface.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 19 | #include "gl/GrGLGpu.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 20 | |
| 21 | // using anonymous namespace because these functions are used as template params. |
| 22 | namespace { |
| 23 | /** convert 0..1 srgb value to 0..1 linear */ |
| 24 | float 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 */ |
| 33 | float 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 | |
| 42 | static 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 Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 50 | void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context, |
| 51 | U8CPU expected, const SkImageInfo& dstInfo, |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 52 | U8CPU error, const char* subtestName) { |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 53 | int w = dstInfo.width(); |
| 54 | int h = dstInfo.height(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 55 | SkAutoTMalloc<uint32_t> readData(w * h); |
| 56 | memset(readData.get(), 0, sizeof(uint32_t) * w * h); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 57 | |
| 58 | if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 59 | ERRORF(reporter, "Could not read pixels for %s.", subtestName); |
| 60 | return; |
| 61 | } |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 62 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 63 | 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 | |
| 81 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) { |
| 82 | GrContext* context = ctxInfo.grContext(); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 83 | if (!context->contextPriv().caps()->srgbSupport()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 84 | 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 Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 120 | 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 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 126 | // Create our test texture |
| 127 | GrSurfaceDesc desc; |
| 128 | desc.fFlags = kNone_GrSurfaceFlags; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 129 | desc.fWidth = texS; |
| 130 | desc.fHeight = texS; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 131 | desc.fConfig = kSRGBA_8888_GrPixelConfig; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 132 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 133 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 134 | sk_sp<GrTextureProxy> proxy = |
| 135 | proxyProvider->createTextureProxy(desc, SkBudgeted::kNo, texData, 0); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 136 | |
Brian Salomon | aeac5d8 | 2016-10-29 00:13:29 -0400 | [diff] [blame] | 137 | // Create two render target contexts (L32 and S32) |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 138 | sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB(); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 139 | 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)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 147 | |
| 148 | SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS)); |
| 149 | GrNoClip noClip; |
| 150 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 151 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 152 | GrSamplerState mipMapSamplerState(GrSamplerState::WrapMode::kRepeat, |
| 153 | GrSamplerState::Filter::kMipMap); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 154 | paint.addColorTextureProcessor(std::move(proxy), SkMatrix::MakeScale(rtS), mipMapSamplerState); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 155 | |
| 156 | // 1) Draw texture to S32 surface (should generate/use sRGB mips) |
| 157 | paint.setGammaCorrect(true); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 158 | s32RenderTargetContext->drawRect(noClip, GrPaint::Clone(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 159 | read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 160 | "first render of sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 161 | |
| 162 | // 2) Draw texture to L32 surface (should generate/use linear mips) |
| 163 | paint.setGammaCorrect(false); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 164 | l32RenderTargetContext->drawRect(noClip, GrPaint::Clone(paint), GrAA::kNo, SkMatrix::I(), rect); |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 165 | |
| 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 | // |
brianosman | 851c238 | 2016-12-07 10:03:25 -0800 | [diff] [blame] | 170 | // 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 | // |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 173 | // 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 Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 175 | GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu()); |
brianosman | 851c238 | 2016-12-07 10:03:25 -0800 | [diff] [blame] | 176 | if (glGpu->glCaps().srgbDecodeDisableSupport() && |
| 177 | glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) { |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 178 | read_and_check_pixels(reporter, l32RenderTargetContext.get(), expectedLinear, iiRGBA, |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 179 | error, "re-render as linear"); |
| 180 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 181 | |
| 182 | // 3) Go back to sRGB |
| 183 | paint.setGammaCorrect(true); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 184 | s32RenderTargetContext->drawRect(noClip, std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 185 | read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 186 | "re-render as sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 187 | } |
| 188 | #endif |