blob: 33d99f220887340fe6cbb686c97ea4f968c6a310 [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"
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrClip.h"
brianosman33f6b3f2016-06-02 05:49:21 -070012#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050014#include "GrResourceProvider.h"
brianosman33f6b3f2016-06-02 05:49:21 -070015#include "SkCanvas.h"
Robert Phillips301431d2017-03-29 12:08:49 -040016#include "SkGr.h"
brianosman33f6b3f2016-06-02 05:49:21 -070017#include "SkSurface.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050018#include "gl/GrGLGpu.h"
brianosman33f6b3f2016-06-02 05:49:21 -070019
20// using anonymous namespace because these functions are used as template params.
21namespace {
22/** convert 0..1 srgb value to 0..1 linear */
23float srgb_to_linear(float srgb) {
24 if (srgb <= 0.04045f) {
25 return srgb / 12.92f;
26 } else {
27 return powf((srgb + 0.055f) / 1.055f, 2.4f);
28 }
29}
30
31/** convert 0..1 linear value to 0..1 srgb */
32float linear_to_srgb(float linear) {
33 if (linear <= 0.0031308) {
34 return linear * 12.92f;
35 } else {
36 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
37 }
38}
39}
40
41static bool check_value(U8CPU value, U8CPU expected, U8CPU error) {
42 if (value >= expected) {
43 return (value - expected) <= error;
44 } else {
45 return (expected - value) <= error;
46 }
47}
48
Robert Phillips301431d2017-03-29 12:08:49 -040049void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
50 U8CPU expected, const SkImageInfo& dstInfo,
brianosman33f6b3f2016-06-02 05:49:21 -070051 U8CPU error, const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -040052 int w = dstInfo.width();
53 int h = dstInfo.height();
brianosman33f6b3f2016-06-02 05:49:21 -070054 SkAutoTMalloc<uint32_t> readData(w * h);
55 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -040056
57 if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
brianosman33f6b3f2016-06-02 05:49:21 -070058 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
59 return;
60 }
Robert Phillips301431d2017-03-29 12:08:49 -040061
brianosman33f6b3f2016-06-02 05:49:21 -070062 for (int j = 0; j < h; ++j) {
63 for (int i = 0; i < w; ++i) {
64 uint32_t read = readData[j * w + i];
65
66 bool success =
67 check_value(read & 0xff, expected, error) &&
68 check_value((read >> 8) & 0xff, expected, error) &&
69 check_value((read >> 16) & 0xff, expected, error);
70
71 if (!success) {
72 ERRORF(reporter, "Expected 0xff%02x%02x%02x, read back as 0x%08x in %s at %d, %d.",
73 expected, expected, expected, read, subtestName, i, j);
74 return;
75 }
76 }
77 }
78}
79
80DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SRGBMipMaps, reporter, ctxInfo) {
81 GrContext* context = ctxInfo.grContext();
82 if (!context->caps()->srgbSupport()) {
83 return;
84 }
85
86 const int rtS = 16;
87 const int texS = rtS * 2;
88
89 // Fill texture with a dither of black and 60% sRGB (~ 32.5% linear) gray. Although there is
90 // only one likely failure mode (doing a direct downsample of the sRGB values), this pattern
91 // maximizes the minimum error across all three conceivable failure modes:
92 // 1) Likely incorrect:
93 // (A + B) / 2
94 // 2) No input decode, decode output:
95 // linear_to_srgb((A + B) / 2)
96 // 3) Decode input, no output encode:
97 // (srgb_to_linear(A) + srgb_to_linear(B)) / 2
98
99 const U8CPU srgb60 = sk_float_round2int(0.6f * 255.0f);
100 static const SkPMColor colors[2] = {
101 SkPackARGB32(0xFF, srgb60, srgb60, srgb60),
102 SkPackARGB32(0xFF, 0x00, 0x00, 0x00)
103 };
104 uint32_t texData[texS * texS];
105 for (int y = 0; y < texS; ++y) {
106 for (int x = 0; x < texS; ++x) {
107 texData[y * texS + x] = colors[(x + y) % 2];
108 }
109 }
110
111 // We can be pretty generous with the error detection, thanks to the choice of input.
112 // The closest likely failure mode is off by > 0.1, so anything that encodes within
113 // 10/255 of optimal is more than good enough for this test.
114 const U8CPU expectedSRGB = sk_float_round2int(
115 linear_to_srgb(srgb_to_linear(srgb60 / 255.0f) / 2.0f) * 255.0f);
116 const U8CPU expectedLinear = srgb60 / 2;
117 const U8CPU error = 10;
118
Robert Phillips301431d2017-03-29 12:08:49 -0400119 const SkImageInfo iiSRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType,
120 kPremul_SkAlphaType,
121 SkColorSpace::MakeSRGB());
122 const SkImageInfo iiRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType,
123 kPremul_SkAlphaType);
124
brianosman33f6b3f2016-06-02 05:49:21 -0700125 // Create our test texture
126 GrSurfaceDesc desc;
127 desc.fFlags = kNone_GrSurfaceFlags;
Brian Osman777b5632016-10-14 09:16:21 -0400128 desc.fConfig = kSRGBA_8888_GrPixelConfig;
brianosman33f6b3f2016-06-02 05:49:21 -0700129 desc.fWidth = texS;
130 desc.fHeight = texS;
131
Brian Osman32342f02017-03-04 08:12:46 -0500132 GrResourceProvider* resourceProvider = context->resourceProvider();
Robert Phillips26c90e02017-03-14 14:39:29 -0400133 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(resourceProvider,
Robert Phillips32f28182017-02-28 16:20:03 -0500134 desc, SkBudgeted::kNo,
135 texData, 0);
brianosman33f6b3f2016-06-02 05:49:21 -0700136
Brian Salomonaeac5d82016-10-29 00:13:29 -0400137 // Create two render target contexts (L32 and S32)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500138 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400139 sk_sp<GrRenderTargetContext> l32RenderTargetContext = context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400140 SkBackingFit::kExact, rtS, rtS, kRGBA_8888_GrPixelConfig, nullptr);
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400141 sk_sp<GrRenderTargetContext> s32RenderTargetContext = context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400142 SkBackingFit::kExact, rtS, rtS, kSRGBA_8888_GrPixelConfig, std::move(srgbColorSpace));
brianosman33f6b3f2016-06-02 05:49:21 -0700143
144 SkRect rect = SkRect::MakeWH(SkIntToScalar(rtS), SkIntToScalar(rtS));
145 GrNoClip noClip;
146 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400147 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon514baff2016-11-17 15:17:07 -0500148 GrSamplerParams mipMapParams(SkShader::kRepeat_TileMode, GrSamplerParams::kMipMap_FilterMode);
Robert Phillips296b1cc2017-03-15 10:42:12 -0400149 paint.addColorTextureProcessor(context->resourceProvider(), std::move(proxy),
Robert Phillips32f28182017-02-28 16:20:03 -0500150 nullptr, SkMatrix::MakeScale(rtS), mipMapParams);
brianosman33f6b3f2016-06-02 05:49:21 -0700151
152 // 1) Draw texture to S32 surface (should generate/use sRGB mips)
153 paint.setGammaCorrect(true);
Brian Salomon82f44312017-01-11 13:42:54 -0500154 s32RenderTargetContext->drawRect(noClip, GrPaint(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillips301431d2017-03-29 12:08:49 -0400155 read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error,
bsalomon9061aa42016-07-19 08:49:41 -0700156 "first render of sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700157
158 // 2) Draw texture to L32 surface (should generate/use linear mips)
159 paint.setGammaCorrect(false);
Brian Salomon82f44312017-01-11 13:42:54 -0500160 l32RenderTargetContext->drawRect(noClip, GrPaint(paint), GrAA::kNo, SkMatrix::I(), rect);
brianosman20471892016-12-02 06:43:32 -0800161
162 // Right now, this test only runs on GL (because Vulkan doesn't support legacy mip-mapping
163 // skbug.com/5048). On GL, we may not have sRGB decode support. In that case, rendering sRGB
164 // textures to a legacy surface produces nonsense, so this part of the test is meaningless.
165 //
brianosman851c2382016-12-07 10:03:25 -0800166 // We also skip this part of the test on command buffer (via srgbDecodeDisableAffectsMipmaps),
167 // because that implementation of the extension doesn't ensure that mips respect the setting.
168 //
brianosman20471892016-12-02 06:43:32 -0800169 // TODO: Once Vulkan supports legacy mip-mapping, we can promote this to GrCaps. Right now,
170 // Vulkan has most of the functionality, but not the mip-mapping part that's being tested here.
171 GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->getGpu());
brianosman851c2382016-12-07 10:03:25 -0800172 if (glGpu->glCaps().srgbDecodeDisableSupport() &&
173 glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) {
Robert Phillips301431d2017-03-29 12:08:49 -0400174 read_and_check_pixels(reporter, l32RenderTargetContext.get(), expectedLinear, iiRGBA,
brianosman20471892016-12-02 06:43:32 -0800175 error, "re-render as linear");
176 }
brianosman33f6b3f2016-06-02 05:49:21 -0700177
178 // 3) Go back to sRGB
179 paint.setGammaCorrect(true);
Brian Salomon82f44312017-01-11 13:42:54 -0500180 s32RenderTargetContext->drawRect(noClip, std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillips301431d2017-03-29 12:08:49 -0400181 read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error,
bsalomon9061aa42016-07-19 08:49:41 -0700182 "re-render as sRGB");
brianosman33f6b3f2016-06-02 05:49:21 -0700183}
184#endif