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" |
| 11 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetContext.h" |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 13 | #include "gl/GrGLGpu.h" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
| 15 | #include "SkSurface.h" |
| 16 | |
| 17 | // using anonymous namespace because these functions are used as template params. |
| 18 | namespace { |
| 19 | /** convert 0..1 srgb value to 0..1 linear */ |
| 20 | float 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 */ |
| 29 | float 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 | |
| 38 | static 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 | |
| 46 | void 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 | |
| 74 | DEF_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 Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 116 | desc.fConfig = kSRGBA_8888_GrPixelConfig; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 117 | desc.fWidth = texS; |
| 118 | desc.fHeight = texS; |
| 119 | |
| 120 | GrTextureProvider* texProvider = context->textureProvider(); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 121 | sk_sp<GrTexture> texture(texProvider->createTexture(desc, SkBudgeted::kNo, texData, 0)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 122 | |
Brian Salomon | aeac5d8 | 2016-10-29 00:13:29 -0400 | [diff] [blame] | 123 | // Create two render target contexts (L32 and S32) |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 124 | sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 125 | 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)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 129 | |
| 130 | SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS)); |
| 131 | GrNoClip noClip; |
| 132 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 133 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 134 | GrSamplerParams mipMapParams(SkShader::kRepeat_TileMode, GrSamplerParams::kMipMap_FilterMode); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 135 | paint.addColorTextureProcessor(texture.get(), nullptr, SkMatrix::MakeScale(0.5f), mipMapParams); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 136 | |
| 137 | // 1) Draw texture to S32 surface (should generate/use sRGB mips) |
| 138 | paint.setGammaCorrect(true); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 139 | s32RenderTargetContext->drawRect(noClip, paint, GrAA::kNo, SkMatrix::I(), rect); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 140 | read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 141 | "first render of sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 142 | |
| 143 | // 2) Draw texture to L32 surface (should generate/use linear mips) |
| 144 | paint.setGammaCorrect(false); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 145 | l32RenderTargetContext->drawRect(noClip, paint, GrAA::kNo, SkMatrix::I(), rect); |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 146 | |
| 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 | // |
brianosman | 851c238 | 2016-12-07 10:03:25 -0800 | [diff] [blame] | 151 | // 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 | // |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 154 | // 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()); |
brianosman | 851c238 | 2016-12-07 10:03:25 -0800 | [diff] [blame] | 157 | if (glGpu->glCaps().srgbDecodeDisableSupport() && |
| 158 | glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) { |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 159 | read_and_check_pixels(reporter, l32RenderTargetContext->asTexture().get(), expectedLinear, |
| 160 | error, "re-render as linear"); |
| 161 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 162 | |
| 163 | // 3) Go back to sRGB |
| 164 | paint.setGammaCorrect(true); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 165 | s32RenderTargetContext->drawRect(noClip, paint, GrAA::kNo, SkMatrix::I(), rect); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 166 | read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 167 | "re-render as sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 168 | } |
| 169 | #endif |