blob: 6eb4b2114a1fcdc0139e99c6c85f76071d06ac1b [file] [log] [blame]
brianosman33f6b3f2016-06-02 05:49:21 -07001/*
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.
17namespace {
18/** convert 0..1 srgb value to 0..1 linear */
19float 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 */
28float 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
37static 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
45void 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
73DEF_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
bsalomon9061aa42016-07-19 08:49:41 -0700122 // Create two draw contexts (L32 and S32)
brianosmandfe4f2e2016-07-21 13:28:36 -0700123 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
robertphillips6738c702016-07-27 12:13:51 -0700124 sk_sp<GrDrawContext> l32DrawContext = context->makeDrawContext(SkBackingFit::kExact, rtS, rtS,
125 kSkia8888_GrPixelConfig,
126 nullptr);
127 sk_sp<GrDrawContext> s32DrawContext = context->makeDrawContext(SkBackingFit::kExact, rtS, rtS,
128 kSkiaGamma8888_GrPixelConfig,
129 std::move(srgbColorSpace));
brianosman33f6b3f2016-06-02 05:49:21 -0700130
131 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS));
132 GrNoClip noClip;
133 GrPaint paint;
134 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
135 GrTextureParams mipMapParams(SkShader::kRepeat_TileMode, GrTextureParams::kMipMap_FilterMode);
brianosman54f30c12016-07-18 10:53:52 -0700136 paint.addColorTextureProcessor(texture, nullptr, SkMatrix::MakeScale(0.5f), mipMapParams);
brianosman33f6b3f2016-06-02 05:49:21 -0700137
138 // 1) Draw texture to S32 surface (should generate/use sRGB mips)
139 paint.setGammaCorrect(true);
140 s32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect);
bsalomon9061aa42016-07-19 08:49:41 -0700141 read_and_check_pixels(reporter, s32DrawContext->asTexture().get(), expectedSRGB, error,
142 "first render of sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700143
144 // 2) Draw texture to L32 surface (should generate/use linear mips)
145 paint.setGammaCorrect(false);
146 l32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect);
bsalomon9061aa42016-07-19 08:49:41 -0700147 read_and_check_pixels(reporter, l32DrawContext->asTexture().get(), expectedLinear, error,
148 "re-render as linear");
brianosman33f6b3f2016-06-02 05:49:21 -0700149
150 // 3) Go back to sRGB
151 paint.setGammaCorrect(true);
152 s32DrawContext->drawRect(noClip, paint, SkMatrix::I(), rect);
bsalomon9061aa42016-07-19 08:49:41 -0700153 read_and_check_pixels(reporter, s32DrawContext->asTexture().get(), expectedSRGB, error,
154 "re-render as sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700155}
156#endif