blob: 4261796001f897f82687931323404bbdb6bade7e [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"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
brianosman33f6b3f2016-06-02 05:49:21 -070013#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;
Brian Osman777b5632016-10-14 09:16:21 -0400115 desc.fConfig = kSRGBA_8888_GrPixelConfig;
brianosman33f6b3f2016-06-02 05:49:21 -0700116 desc.fWidth = texS;
117 desc.fHeight = texS;
118
119 GrTextureProvider* texProvider = context->textureProvider();
Hal Canary342b7ac2016-11-04 11:49:42 -0400120 sk_sp<GrTexture> texture(texProvider->createTexture(desc, SkBudgeted::kNo, texData, 0));
brianosman33f6b3f2016-06-02 05:49:21 -0700121
Brian Salomonaeac5d82016-10-29 00:13:29 -0400122 // Create two render target contexts (L32 and S32)
Brian Osman526972e2016-10-24 09:24:02 -0400123 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
Brian Osman11052242016-10-27 14:47:55 -0400124 sk_sp<GrRenderTargetContext> l32RenderTargetContext = context->makeRenderTargetContext(
125 SkBackingFit::kExact, rtS, rtS, kRGBA_8888_GrPixelConfig, nullptr);
126 sk_sp<GrRenderTargetContext> s32RenderTargetContext = context->makeRenderTargetContext(
127 SkBackingFit::kExact, rtS, rtS, kSRGBA_8888_GrPixelConfig, std::move(srgbColorSpace));
brianosman33f6b3f2016-06-02 05:49:21 -0700128
129 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS));
130 GrNoClip noClip;
131 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400132 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosman33f6b3f2016-06-02 05:49:21 -0700133 GrTextureParams mipMapParams(SkShader::kRepeat_TileMode, GrTextureParams::kMipMap_FilterMode);
Hal Canary342b7ac2016-11-04 11:49:42 -0400134 paint.addColorTextureProcessor(texture.get(), nullptr, SkMatrix::MakeScale(0.5f), mipMapParams);
brianosman33f6b3f2016-06-02 05:49:21 -0700135
136 // 1) Draw texture to S32 surface (should generate/use sRGB mips)
137 paint.setGammaCorrect(true);
Brian Osman11052242016-10-27 14:47:55 -0400138 s32RenderTargetContext->drawRect(noClip, paint, SkMatrix::I(), rect);
139 read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
bsalomon9061aa42016-07-19 08:49:41 -0700140 "first render of sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700141
142 // 2) Draw texture to L32 surface (should generate/use linear mips)
143 paint.setGammaCorrect(false);
Brian Osman11052242016-10-27 14:47:55 -0400144 l32RenderTargetContext->drawRect(noClip, paint, SkMatrix::I(), rect);
145 read_and_check_pixels(reporter, l32RenderTargetContext->asTexture().get(), expectedLinear,
146 error, "re-render as linear");
brianosman33f6b3f2016-06-02 05:49:21 -0700147
148 // 3) Go back to sRGB
149 paint.setGammaCorrect(true);
Brian Osman11052242016-10-27 14:47:55 -0400150 s32RenderTargetContext->drawRect(noClip, paint, SkMatrix::I(), rect);
151 read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
bsalomon9061aa42016-07-19 08:49:41 -0700152 "re-render as sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700153}
154#endif