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" |
| 12 | #include "GrDrawContext.h" |
| 13 | #include "SkCanvas.h" |
| 14 | #include "SkSurface.h" |
| 15 | |
| 16 | // using anonymous namespace because these functions are used as template params. |
| 17 | namespace { |
| 18 | /** convert 0..1 srgb value to 0..1 linear */ |
| 19 | float srgb_to_linear(float srgb) { |
| 20 | if (srgb <= 0.04045f) { |
| 21 | return srgb / 12.92f; |
| 22 | } else { |
| 23 | return powf((srgb + 0.055f) / 1.055f, 2.4f); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** convert 0..1 linear value to 0..1 srgb */ |
| 28 | float linear_to_srgb(float linear) { |
| 29 | if (linear <= 0.0031308) { |
| 30 | return linear * 12.92f; |
| 31 | } else { |
| 32 | return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static bool check_value(U8CPU value, U8CPU expected, U8CPU error) { |
| 38 | if (value >= expected) { |
| 39 | return (value - expected) <= error; |
| 40 | } else { |
| 41 | return (expected - value) <= error; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void read_and_check_pixels(skiatest::Reporter* reporter, GrTexture* texture, U8CPU expected, |
| 46 | U8CPU error, const char* subtestName) { |
| 47 | int w = texture->width(); |
| 48 | int h = texture->height(); |
| 49 | SkAutoTMalloc<uint32_t> readData(w * h); |
| 50 | memset(readData.get(), 0, sizeof(uint32_t) * w * h); |
| 51 | if (!texture->readPixels(0, 0, w, h, texture->config(), readData.get())) { |
| 52 | ERRORF(reporter, "Could not read pixels for %s.", subtestName); |
| 53 | return; |
| 54 | } |
| 55 | for (int j = 0; j < h; ++j) { |
| 56 | for (int i = 0; i < w; ++i) { |
| 57 | uint32_t read = readData[j * w + i]; |
| 58 | |
| 59 | bool success = |
| 60 | check_value(read & 0xff, expected, error) && |
| 61 | check_value((read >> 8) & 0xff, expected, error) && |
| 62 | check_value((read >> 16) & 0xff, expected, error); |
| 63 | |
| 64 | if (!success) { |
| 65 | ERRORF(reporter, "Expected 0xff%02x%02x%02x, read back as 0x%08x in %s at %d, %d.", |
| 66 | expected, expected, expected, read, subtestName, i, j); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) { |
| 74 | GrContext* context = ctxInfo.grContext(); |
| 75 | if (!context->caps()->srgbSupport()) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | const int rtS = 16; |
| 80 | const int texS = rtS * 2; |
| 81 | |
| 82 | // Fill texture with a dither of black and 60% sRGB (~ 32.5% linear) gray. Although there is |
| 83 | // only one likely failure mode (doing a direct downsample of the sRGB values), this pattern |
| 84 | // maximizes the minimum error across all three conceivable failure modes: |
| 85 | // 1) Likely incorrect: |
| 86 | // (A + B) / 2 |
| 87 | // 2) No input decode, decode output: |
| 88 | // linear_to_srgb((A + B) / 2) |
| 89 | // 3) Decode input, no output encode: |
| 90 | // (srgb_to_linear(A) + srgb_to_linear(B)) / 2 |
| 91 | |
| 92 | const U8CPU srgb60 = sk_float_round2int(0.6f * 255.0f); |
| 93 | static const SkPMColor colors[2] = { |
| 94 | SkPackARGB32(0xFF, srgb60, srgb60, srgb60), |
| 95 | SkPackARGB32(0xFF, 0x00, 0x00, 0x00) |
| 96 | }; |
| 97 | uint32_t texData[texS * texS]; |
| 98 | for (int y = 0; y < texS; ++y) { |
| 99 | for (int x = 0; x < texS; ++x) { |
| 100 | texData[y * texS + x] = colors[(x + y) % 2]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // We can be pretty generous with the error detection, thanks to the choice of input. |
| 105 | // The closest likely failure mode is off by > 0.1, so anything that encodes within |
| 106 | // 10/255 of optimal is more than good enough for this test. |
| 107 | const U8CPU expectedSRGB = sk_float_round2int( |
| 108 | linear_to_srgb(srgb_to_linear(srgb60 / 255.0f) / 2.0f) * 255.0f); |
| 109 | const U8CPU expectedLinear = srgb60 / 2; |
| 110 | const U8CPU error = 10; |
| 111 | |
| 112 | // Create our test texture |
| 113 | GrSurfaceDesc desc; |
| 114 | desc.fFlags = kNone_GrSurfaceFlags; |
| 115 | desc.fConfig = kSkiaGamma8888_GrPixelConfig; |
| 116 | desc.fWidth = texS; |
| 117 | desc.fHeight = texS; |
| 118 | |
| 119 | GrTextureProvider* texProvider = context->textureProvider(); |
| 120 | SkAutoTUnref<GrTexture> texture(texProvider->createTexture(desc, SkBudgeted::kNo, texData, 0)); |
| 121 | |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 122 | // Create two draw contexts (L32 and S32) |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 123 | SkSurfaceProps l32Props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 124 | SkSurfaceProps s32Props(SkSurfaceProps::kGammaCorrect_Flag, |
| 125 | SkSurfaceProps::kLegacyFontHost_InitType); |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 126 | sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 127 | sk_sp<GrDrawContext> l32DrawContext = context->newDrawContext(SkBackingFit::kExact, rtS, rtS, |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 128 | kSkia8888_GrPixelConfig, |
| 129 | nullptr, |
| 130 | 0, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 131 | kDefault_GrSurfaceOrigin, |
| 132 | &l32Props); |
| 133 | sk_sp<GrDrawContext> s32DrawContext = context->newDrawContext(SkBackingFit::kExact, rtS, rtS, |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 134 | kSkiaGamma8888_GrPixelConfig, |
| 135 | std::move(srgbColorSpace), |
| 136 | 0, |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 137 | kDefault_GrSurfaceOrigin, |
| 138 | &s32Props); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 139 | |
| 140 | SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS)); |
| 141 | GrNoClip noClip; |
| 142 | GrPaint paint; |
| 143 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
| 144 | GrTextureParams mipMapParams(SkShader::kRepeat_TileMode, GrTextureParams::kMipMap_FilterMode); |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 145 | paint.addColorTextureProcessor(texture, nullptr, SkMatrix::MakeScale(0.5f), mipMapParams); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 146 | |
| 147 | // 1) Draw texture to S32 surface (should generate/use sRGB mips) |
| 148 | paint.setGammaCorrect(true); |
| 149 | s32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect); |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 150 | read_and_check_pixels(reporter, s32DrawContext->asTexture().get(), expectedSRGB, error, |
| 151 | "first render of sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 152 | |
| 153 | // 2) Draw texture to L32 surface (should generate/use linear mips) |
| 154 | paint.setGammaCorrect(false); |
| 155 | l32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect); |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 156 | read_and_check_pixels(reporter, l32DrawContext->asTexture().get(), expectedLinear, error, |
| 157 | "re-render as linear"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 158 | |
| 159 | // 3) Go back to sRGB |
| 160 | paint.setGammaCorrect(true); |
| 161 | s32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect); |
bsalomon | 9061aa4 | 2016-07-19 08:49:41 -0700 | [diff] [blame] | 162 | read_and_check_pixels(reporter, s32DrawContext->asTexture().get(), expectedSRGB, error, |
| 163 | "re-render as sRGB"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 164 | } |
| 165 | #endif |