OLD | NEW |
(Empty) | |
| 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, U8C
PU 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. A
lthough there is |
| 83 // only one likely failure mode (doing a direct downsample of the sRGB value
s), 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 |
| 122 // Create two surfaces (L32 and S32) |
| 123 GrSurfaceDesc l32Desc; |
| 124 l32Desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 125 l32Desc.fConfig = kSkia8888_GrPixelConfig; |
| 126 l32Desc.fWidth = rtS; |
| 127 l32Desc.fHeight = rtS; |
| 128 |
| 129 GrSurfaceDesc s32Desc = l32Desc; |
| 130 s32Desc.fConfig = kSkiaGamma8888_GrPixelConfig; |
| 131 |
| 132 SkAutoTUnref<GrTexture> l32Texture(texProvider->createTexture(l32Desc, SkBud
geted::kNo)); |
| 133 SkAutoTUnref<GrTexture> s32Texture(texProvider->createTexture(s32Desc, SkBud
geted::kNo)); |
| 134 |
| 135 SkSurfaceProps l32Props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 136 SkSurfaceProps s32Props(SkSurfaceProps::kGammaCorrect_Flag, |
| 137 SkSurfaceProps::kLegacyFontHost_InitType); |
| 138 |
| 139 sk_sp<GrDrawContext> l32DrawContext( |
| 140 context->drawContext(sk_ref_sp(l32Texture->asRenderTarget()), &l32Props)
); |
| 141 sk_sp<GrDrawContext> s32DrawContext( |
| 142 context->drawContext(sk_ref_sp(s32Texture->asRenderTarget()), &s32Props)
); |
| 143 |
| 144 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS)); |
| 145 GrNoClip noClip; |
| 146 GrPaint paint; |
| 147 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
| 148 GrTextureParams mipMapParams(SkShader::kRepeat_TileMode, GrTextureParams::kM
ipMap_FilterMode); |
| 149 paint.addColorTextureProcessor(texture, SkMatrix::MakeScale(0.5f), mipMapPar
ams); |
| 150 |
| 151 // 1) Draw texture to S32 surface (should generate/use sRGB mips) |
| 152 paint.setGammaCorrect(true); |
| 153 s32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect); |
| 154 read_and_check_pixels(reporter, s32Texture, expectedSRGB, error, "first rend
er of sRGB"); |
| 155 |
| 156 // 2) Draw texture to L32 surface (should generate/use linear mips) |
| 157 paint.setGammaCorrect(false); |
| 158 l32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect); |
| 159 read_and_check_pixels(reporter, l32Texture, expectedLinear, error, "re-rende
r as linear"); |
| 160 |
| 161 // 3) Go back to sRGB |
| 162 paint.setGammaCorrect(true); |
| 163 s32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect); |
| 164 read_and_check_pixels(reporter, s32Texture, expectedSRGB, error, "re-render
as sRGB"); |
| 165 } |
| 166 #endif |
OLD | NEW |