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" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 13 | #include "GrRenderTargetContext.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 15 | #include "SkGr.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 16 | #include "SkSurface.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 17 | #include "gl/GrGLGpu.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 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 */ |
| 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 */ |
| 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 | |
| 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 | |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 48 | void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context, |
| 49 | U8CPU expected, const SkImageInfo& dstInfo, |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 50 | U8CPU error, const char* subtestName) { |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 51 | int w = dstInfo.width(); |
| 52 | int h = dstInfo.height(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 53 | SkAutoTMalloc<uint32_t> readData(w * h); |
| 54 | memset(readData.get(), 0, sizeof(uint32_t) * w * h); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 55 | |
| 56 | if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 57 | ERRORF(reporter, "Could not read pixels for %s.", subtestName); |
| 58 | return; |
| 59 | } |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 60 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 61 | for (int j = 0; j < h; ++j) { |
| 62 | for (int i = 0; i < w; ++i) { |
| 63 | uint32_t read = readData[j * w + i]; |
| 64 | |
| 65 | bool success = |
| 66 | check_value(read & 0xff, expected, error) && |
| 67 | check_value((read >> 8) & 0xff, expected, error) && |
| 68 | check_value((read >> 16) & 0xff, expected, error); |
| 69 | |
| 70 | if (!success) { |
| 71 | ERRORF(reporter, "Expected 0xff%02x%02x%02x, read back as 0x%08x in %s at %d, %d.", |
| 72 | expected, expected, expected, read, subtestName, i, j); |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) { |
| 80 | GrContext* context = ctxInfo.grContext(); |
| 81 | if (!context->caps()->srgbSupport()) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | const int rtS = 16; |
| 86 | const int texS = rtS * 2; |
| 87 | |
| 88 | // Fill texture with a dither of black and 60% sRGB (~ 32.5% linear) gray. Although there is |
| 89 | // only one likely failure mode (doing a direct downsample of the sRGB values), this pattern |
| 90 | // maximizes the minimum error across all three conceivable failure modes: |
| 91 | // 1) Likely incorrect: |
| 92 | // (A + B) / 2 |
| 93 | // 2) No input decode, decode output: |
| 94 | // linear_to_srgb((A + B) / 2) |
| 95 | // 3) Decode input, no output encode: |
| 96 | // (srgb_to_linear(A) + srgb_to_linear(B)) / 2 |
| 97 | |
| 98 | const U8CPU srgb60 = sk_float_round2int(0.6f * 255.0f); |
| 99 | static const SkPMColor colors[2] = { |
| 100 | SkPackARGB32(0xFF, srgb60, srgb60, srgb60), |
| 101 | SkPackARGB32(0xFF, 0x00, 0x00, 0x00) |
| 102 | }; |
| 103 | uint32_t texData[texS * texS]; |
| 104 | for (int y = 0; y < texS; ++y) { |
| 105 | for (int x = 0; x < texS; ++x) { |
| 106 | texData[y * texS + x] = colors[(x + y) % 2]; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // We can be pretty generous with the error detection, thanks to the choice of input. |
| 111 | // The closest likely failure mode is off by > 0.1, so anything that encodes within |
| 112 | // 10/255 of optimal is more than good enough for this test. |
| 113 | const U8CPU expectedSRGB = sk_float_round2int( |
| 114 | linear_to_srgb(srgb_to_linear(srgb60 / 255.0f) / 2.0f) * 255.0f); |
| 115 | const U8CPU expectedLinear = srgb60 / 2; |
| 116 | const U8CPU error = 10; |
| 117 | |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 118 | const SkImageInfo iiSRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType, |
| 119 | kPremul_SkAlphaType, |
| 120 | SkColorSpace::MakeSRGB()); |
| 121 | const SkImageInfo iiRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType, |
| 122 | kPremul_SkAlphaType); |
| 123 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 124 | // Create our test texture |
| 125 | GrSurfaceDesc desc; |
| 126 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 127 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 128 | desc.fWidth = texS; |
| 129 | desc.fHeight = texS; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 130 | desc.fConfig = kSRGBA_8888_GrPixelConfig; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 131 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 132 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 133 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(resourceProvider, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 134 | desc, SkBudgeted::kNo, |
| 135 | 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 | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 139 | sk_sp<GrRenderTargetContext> l32RenderTargetContext = context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 140 | SkBackingFit::kExact, rtS, rtS, kRGBA_8888_GrPixelConfig, nullptr); |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 141 | sk_sp<GrRenderTargetContext> s32RenderTargetContext = context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 142 | SkBackingFit::kExact, rtS, rtS, kSRGBA_8888_GrPixelConfig, std::move(srgbColorSpace)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 143 | |
| 144 | SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS)); |
| 145 | GrNoClip noClip; |
| 146 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 147 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 148 | GrSamplerState mipMapSamplerState(GrSamplerState::WrapMode::kRepeat, |
| 149 | GrSamplerState::Filter::kMipMap); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 150 | paint.addColorTextureProcessor(std::move(proxy), SkMatrix::MakeScale(rtS), mipMapSamplerState); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 151 | |
| 152 | // 1) Draw texture to S32 surface (should generate/use sRGB mips) |
| 153 | paint.setGammaCorrect(true); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 154 | s32RenderTargetContext->drawRect(noClip, GrPaint::Clone(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 155 | read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 156 | "first render of sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 157 | |
| 158 | // 2) Draw texture to L32 surface (should generate/use linear mips) |
| 159 | paint.setGammaCorrect(false); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 160 | l32RenderTargetContext->drawRect(noClip, GrPaint::Clone(paint), GrAA::kNo, SkMatrix::I(), rect); |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 161 | |
| 162 | // Right now, this test only runs on GL (because Vulkan doesn't support legacy mip-mapping |
| 163 | // skbug.com/5048). On GL, we may not have sRGB decode support. In that case, rendering sRGB |
| 164 | // textures to a legacy surface produces nonsense, so this part of the test is meaningless. |
| 165 | // |
brianosman | 851c238 | 2016-12-07 10:03:25 -0800 | [diff] [blame] | 166 | // We also skip this part of the test on command buffer (via srgbDecodeDisableAffectsMipmaps), |
| 167 | // because that implementation of the extension doesn't ensure that mips respect the setting. |
| 168 | // |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 169 | // TODO: Once Vulkan supports legacy mip-mapping, we can promote this to GrCaps. Right now, |
| 170 | // Vulkan has most of the functionality, but not the mip-mapping part that's being tested here. |
| 171 | GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->getGpu()); |
brianosman | 851c238 | 2016-12-07 10:03:25 -0800 | [diff] [blame] | 172 | if (glGpu->glCaps().srgbDecodeDisableSupport() && |
| 173 | glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) { |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 174 | read_and_check_pixels(reporter, l32RenderTargetContext.get(), expectedLinear, iiRGBA, |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 175 | error, "re-render as linear"); |
| 176 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 177 | |
| 178 | // 3) Go back to sRGB |
| 179 | paint.setGammaCorrect(true); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 180 | s32RenderTargetContext->drawRect(noClip, std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 181 | read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 182 | "re-render as sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 183 | } |
| 184 | #endif |