blob: 9729774cd0aab53334d2f317e3c6bba639cfb69a [file] [log] [blame]
bsalomon16921ec2015-07-30 15:34:56 -07001/*
2 * Copyright 2015 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
bsalomon16921ec2015-07-30 15:34:56 -070010#include "GrCaps.h"
kkinnunen15302832015-12-01 04:35:26 -080011#include "GrContext.h"
Robert Phillips301431d2017-03-29 12:08:49 -040012#include "GrContextPriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050013#include "GrResourceProvider.h"
Robert Phillips301431d2017-03-29 12:08:49 -040014#include "GrSurfaceContext.h"
kkinnunen15302832015-12-01 04:35:26 -080015#include "SkCanvas.h"
Robert Phillips301431d2017-03-29 12:08:49 -040016#include "SkGr.h"
kkinnunen15302832015-12-01 04:35:26 -080017#include "SkSurface.h"
bsalomon16921ec2015-07-30 15:34:56 -070018
19// using anonymous namespace because these functions are used as template params.
20namespace {
21/** convert 0..1 srgb value to 0..1 linear */
22float srgb_to_linear(float srgb) {
23 if (srgb <= 0.04045f) {
24 return srgb / 12.92f;
25 } else {
26 return powf((srgb + 0.055f) / 1.055f, 2.4f);
27 }
28}
29
30/** convert 0..1 linear value to 0..1 srgb */
31float linear_to_srgb(float linear) {
32 if (linear <= 0.0031308) {
33 return linear * 12.92f;
34 } else {
35 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
36 }
37}
38}
39
40/** tests a conversion with an error tolerance */
41template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
42 float error) {
43 // alpha should always be exactly preserved.
44 if ((input & 0xff000000) != (output & 0xff000000)) {
45 return false;
46 }
47
48 for (int c = 0; c < 3; ++c) {
49 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
50 float lower = SkTMax(0.f, (float) inputComponent - error);
51 float upper = SkTMin(255.f, (float) inputComponent + error);
52 lower = CONVERT(lower / 255.f);
53 upper = CONVERT(upper / 255.f);
54 SkASSERT(lower >= 0.f && lower <= 255.f);
55 SkASSERT(upper >= 0.f && upper <= 255.f);
56 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
57 if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
58 outputComponent > SkScalarCeilToInt(upper * 255.f)) {
59 return false;
60 }
61 }
62 return true;
63}
64
65/** tests a forward and backward conversion with an error tolerance */
66template <float (*FORWARD)(float), float (*BACKWARD)(float)>
67static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
68 // alpha should always be exactly preserved.
69 if ((input & 0xff000000) != (output & 0xff000000)) {
70 return false;
71 }
72
73 for (int c = 0; c < 3; ++c) {
74 uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
75 float lower = SkTMax(0.f, (float) inputComponent - error);
76 float upper = SkTMin(255.f, (float) inputComponent + error);
77 lower = FORWARD(lower / 255.f);
78 upper = FORWARD(upper / 255.f);
79 SkASSERT(lower >= 0.f && lower <= 255.f);
80 SkASSERT(upper >= 0.f && upper <= 255.f);
81 uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
82 uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
83 lower = SkTMax(0.f, (float) lowerComponent - error);
84 upper = SkTMin(255.f, (float) upperComponent + error);
85 lower = BACKWARD(lowerComponent / 255.f);
86 upper = BACKWARD(upperComponent / 255.f);
87 SkASSERT(lower >= 0.f && lower <= 255.f);
88 SkASSERT(upper >= 0.f && upper <= 255.f);
89 upperComponent = SkScalarCeilToInt(upper * 255.f);
90 lowerComponent = SkScalarFloorToInt(lower * 255.f);
91
92 uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
93 if (outputComponent < lowerComponent || outputComponent > upperComponent) {
94 return false;
95 }
96 }
97 return true;
98}
99
100static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
101 return check_conversion<srgb_to_linear>(srgb, linear, error);
102}
103
104static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
105 return check_conversion<linear_to_srgb>(linear, srgb, error);
106}
107
108static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
109 return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
110}
111
112static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
113 return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
114}
115
116typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
117
Robert Phillips301431d2017-03-29 12:08:49 -0400118void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
119 uint32_t* origData,
120 const SkImageInfo& dstInfo, CheckFn checker, float error,
bsalomon16921ec2015-07-30 15:34:56 -0700121 const char* subtestName) {
Robert Phillips301431d2017-03-29 12:08:49 -0400122 int w = dstInfo.width();
123 int h = dstInfo.height();
bsalomon16921ec2015-07-30 15:34:56 -0700124 SkAutoTMalloc<uint32_t> readData(w * h);
125 memset(readData.get(), 0, sizeof(uint32_t) * w * h);
Robert Phillips301431d2017-03-29 12:08:49 -0400126
127 if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
bsalomon16921ec2015-07-30 15:34:56 -0700128 ERRORF(reporter, "Could not read pixels for %s.", subtestName);
129 return;
130 }
Robert Phillips301431d2017-03-29 12:08:49 -0400131
bsalomon16921ec2015-07-30 15:34:56 -0700132 for (int j = 0; j < h; ++j) {
133 for (int i = 0; i < w; ++i) {
134 uint32_t orig = origData[j * w + i];
135 uint32_t read = readData[j * w + i];
136
137 if (!checker(orig, read, error)) {
138 ERRORF(reporter, "Expected 0x%08x, read back as 0x%08x in %s at %d, %d).",
139 orig, read, subtestName, i, j);
140 return;
141 }
142 }
143 }
144}
145
146// TODO: Add tests for copySurface between srgb/linear textures. Add tests for unpremul/premul
147// conversion during read/write along with srgb/linear conversions.
egdanielab527a52016-06-28 08:07:26 -0700148DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700149 GrContext* context = ctxInfo.grContext();
benjaminwagner7d974f52015-10-19 13:55:55 -0700150#if defined(GOOGLE3)
151 // Stack frame size is limited in GOOGLE3.
152 static const int kW = 63;
153 static const int kH = 63;
154#else
bsalomon16921ec2015-07-30 15:34:56 -0700155 static const int kW = 255;
156 static const int kH = 255;
benjaminwagner7d974f52015-10-19 13:55:55 -0700157#endif
bsalomon16921ec2015-07-30 15:34:56 -0700158 uint32_t origData[kW * kH];
159 for (int j = 0; j < kH; ++j) {
160 for (int i = 0; i < kW; ++i) {
161 origData[j * kW + i] = (j << 24) | (i << 16) | (i << 8) | i;
162 }
163 }
164
Robert Phillips301431d2017-03-29 12:08:49 -0400165 const SkImageInfo iiSRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
166 kPremul_SkAlphaType,
167 SkColorSpace::MakeSRGB());
168 const SkImageInfo iiRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
169 kPremul_SkAlphaType);
kkinnunen15302832015-12-01 04:35:26 -0800170 GrSurfaceDesc desc;
171 desc.fFlags = kRenderTarget_GrSurfaceFlag;
172 desc.fWidth = kW;
173 desc.fHeight = kH;
174 desc.fConfig = kSRGBA_8888_GrPixelConfig;
175 if (context->caps()->isConfigRenderable(desc.fConfig, false) &&
176 context->caps()->isConfigTexturable(desc.fConfig)) {
Robert Phillips301431d2017-03-29 12:08:49 -0400177
178 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
179 desc, SkBackingFit::kExact,
180 SkBudgeted::kNo);
181 if (!sContext) {
182 ERRORF(reporter, "Could not create SRGBA surface context.");
kkinnunen15302832015-12-01 04:35:26 -0800183 return;
bsalomon16921ec2015-07-30 15:34:56 -0700184 }
185
kkinnunen15302832015-12-01 04:35:26 -0800186 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f;
bsalomon16921ec2015-07-30 15:34:56 -0700187
kkinnunen15302832015-12-01 04:35:26 -0800188 // Write srgba data and read as srgba and then as rgba
Robert Phillips301431d2017-03-29 12:08:49 -0400189 if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
kkinnunen15302832015-12-01 04:35:26 -0800190 // For the all-srgba case, we allow a small error only for devices that have
191 // precision variation because the srgba data gets converted to linear and back in
192 // the shader.
Robert Phillips301431d2017-03-29 12:08:49 -0400193 float smallError = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.f : 0.0f;
194 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800195 check_srgb_to_linear_to_srgb_conversion, smallError,
196 "write/read srgba to srgba texture");
Robert Phillips301431d2017-03-29 12:08:49 -0400197 read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800198 check_srgb_to_linear_conversion, error,
199 "write srgba/read rgba with srgba texture");
200 } else {
201 ERRORF(reporter, "Could not write srgba data to srgba texture.");
202 }
bsalomon16921ec2015-07-30 15:34:56 -0700203
kkinnunen15302832015-12-01 04:35:26 -0800204 // Now verify that we can write linear data
Robert Phillips301431d2017-03-29 12:08:49 -0400205 if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
kkinnunen15302832015-12-01 04:35:26 -0800206 // We allow more error on GPUs with lower precision shader variables.
Robert Phillips301431d2017-03-29 12:08:49 -0400207 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800208 check_linear_to_srgb_conversion, error,
209 "write rgba/read srgba with srgba texture");
Robert Phillips301431d2017-03-29 12:08:49 -0400210 read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800211 check_linear_to_srgb_to_linear_conversion, error,
212 "write/read rgba with srgba texture");
213 } else {
214 ERRORF(reporter, "Could not write rgba data to srgba texture.");
215 }
bsalomon16921ec2015-07-30 15:34:56 -0700216
kkinnunen15302832015-12-01 04:35:26 -0800217 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips301431d2017-03-29 12:08:49 -0400218 sContext = context->contextPriv().makeDeferredSurfaceContext(desc, SkBackingFit::kExact,
219 SkBudgeted::kNo);
220 if (!sContext) {
221 ERRORF(reporter, "Could not create RGBA surface context.");
kkinnunen15302832015-12-01 04:35:26 -0800222 return;
223 }
bsalomon16921ec2015-07-30 15:34:56 -0700224
kkinnunen15302832015-12-01 04:35:26 -0800225 // Write srgba data to a rgba texture and read back as srgba and rgba
Robert Phillips301431d2017-03-29 12:08:49 -0400226 if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
227 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800228 check_srgb_to_linear_to_srgb_conversion, error,
229 "write/read srgba to rgba texture");
Robert Phillips301431d2017-03-29 12:08:49 -0400230 read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800231 check_srgb_to_linear_conversion, error,
232 "write srgba/read rgba to rgba texture");
233 } else {
234 ERRORF(reporter, "Could not write srgba data to rgba texture.");
235 }
bsalomon16921ec2015-07-30 15:34:56 -0700236
kkinnunen15302832015-12-01 04:35:26 -0800237 // Write rgba data to a rgba texture and read back as srgba
Robert Phillips301431d2017-03-29 12:08:49 -0400238 if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
239 read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
kkinnunen15302832015-12-01 04:35:26 -0800240 check_linear_to_srgb_conversion, 1.2f,
241 "write rgba/read srgba to rgba texture");
242 } else {
243 ERRORF(reporter, "Could not write rgba data to rgba texture.");
244 }
bsalomon16921ec2015-07-30 15:34:56 -0700245 }
246}
247#endif