1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#if SK_SUPPORT_GPU
#include "GrCaps.h"
#include "GrContext.h"
#include "GrContextPriv.h"
#include "GrSurfaceContext.h"
#include "SkCanvas.h"
#include "SkGr.h"
#include "SkSurface.h"
// using anonymous namespace because these functions are used as template params.
namespace {
/** convert 0..1 srgb value to 0..1 linear */
float srgb_to_linear(float srgb) {
if (srgb <= 0.04045f) {
return srgb / 12.92f;
} else {
return powf((srgb + 0.055f) / 1.055f, 2.4f);
}
}
/** convert 0..1 linear value to 0..1 srgb */
float linear_to_srgb(float linear) {
if (linear <= 0.0031308) {
return linear * 12.92f;
} else {
return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
}
}
}
/** tests a conversion with an error tolerance */
template <float (*CONVERT)(float)> static bool check_conversion(uint32_t input, uint32_t output,
float error) {
// alpha should always be exactly preserved.
if ((input & 0xff000000) != (output & 0xff000000)) {
return false;
}
for (int c = 0; c < 3; ++c) {
uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
float lower = SkTMax(0.f, (float) inputComponent - error);
float upper = SkTMin(255.f, (float) inputComponent + error);
lower = CONVERT(lower / 255.f);
upper = CONVERT(upper / 255.f);
SkASSERT(lower >= 0.f && lower <= 255.f);
SkASSERT(upper >= 0.f && upper <= 255.f);
uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
if (outputComponent < SkScalarFloorToInt(lower * 255.f) ||
outputComponent > SkScalarCeilToInt(upper * 255.f)) {
return false;
}
}
return true;
}
/** tests a forward and backward conversion with an error tolerance */
template <float (*FORWARD)(float), float (*BACKWARD)(float)>
static bool check_double_conversion(uint32_t input, uint32_t output, float error) {
// alpha should always be exactly preserved.
if ((input & 0xff000000) != (output & 0xff000000)) {
return false;
}
for (int c = 0; c < 3; ++c) {
uint8_t inputComponent = (uint8_t) ((input & (0xff << (c*8))) >> (c*8));
float lower = SkTMax(0.f, (float) inputComponent - error);
float upper = SkTMin(255.f, (float) inputComponent + error);
lower = FORWARD(lower / 255.f);
upper = FORWARD(upper / 255.f);
SkASSERT(lower >= 0.f && lower <= 255.f);
SkASSERT(upper >= 0.f && upper <= 255.f);
uint8_t upperComponent = SkScalarCeilToInt(upper * 255.f);
uint8_t lowerComponent = SkScalarFloorToInt(lower * 255.f);
lower = SkTMax(0.f, (float) lowerComponent - error);
upper = SkTMin(255.f, (float) upperComponent + error);
lower = BACKWARD(lowerComponent / 255.f);
upper = BACKWARD(upperComponent / 255.f);
SkASSERT(lower >= 0.f && lower <= 255.f);
SkASSERT(upper >= 0.f && upper <= 255.f);
upperComponent = SkScalarCeilToInt(upper * 255.f);
lowerComponent = SkScalarFloorToInt(lower * 255.f);
uint8_t outputComponent = (output & (0xff << (c*8))) >> (c*8);
if (outputComponent < lowerComponent || outputComponent > upperComponent) {
return false;
}
}
return true;
}
static bool check_srgb_to_linear_conversion(uint32_t srgb, uint32_t linear, float error) {
return check_conversion<srgb_to_linear>(srgb, linear, error);
}
static bool check_linear_to_srgb_conversion(uint32_t linear, uint32_t srgb, float error) {
return check_conversion<linear_to_srgb>(linear, srgb, error);
}
static bool check_linear_to_srgb_to_linear_conversion(uint32_t input, uint32_t output, float error) {
return check_double_conversion<linear_to_srgb, srgb_to_linear>(input, output, error);
}
static bool check_srgb_to_linear_to_srgb_conversion(uint32_t input, uint32_t output, float error) {
return check_double_conversion<srgb_to_linear, linear_to_srgb>(input, output, error);
}
typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
uint32_t* origData,
const SkImageInfo& dstInfo, CheckFn checker, float error,
const char* subtestName) {
int w = dstInfo.width();
int h = dstInfo.height();
SkAutoTMalloc<uint32_t> readData(w * h);
memset(readData.get(), 0, sizeof(uint32_t) * w * h);
if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
ERRORF(reporter, "Could not read pixels for %s.", subtestName);
return;
}
for (int j = 0; j < h; ++j) {
for (int i = 0; i < w; ++i) {
uint32_t orig = origData[j * w + i];
uint32_t read = readData[j * w + i];
if (!checker(orig, read, error)) {
ERRORF(reporter, "Expected 0x%08x, read back as 0x%08x in %s at %d, %d).",
orig, read, subtestName, i, j);
return;
}
}
}
}
// TODO: Add tests for copySurface between srgb/linear textures. Add tests for unpremul/premul
// conversion during read/write along with srgb/linear conversions.
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) {
GrContext* context = ctxInfo.grContext();
#if defined(SK_BUILD_FOR_GOOGLE3)
// Stack frame size is limited in SK_BUILD_FOR_GOOGLE3.
static const int kW = 63;
static const int kH = 63;
#else
static const int kW = 255;
static const int kH = 255;
#endif
uint32_t origData[kW * kH];
for (int j = 0; j < kH; ++j) {
for (int i = 0; i < kW; ++i) {
origData[j * kW + i] = (j << 24) | (i << 16) | (i << 8) | i;
}
}
const SkImageInfo iiSRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
kPremul_SkAlphaType,
SkColorSpace::MakeSRGB());
const SkImageInfo iiRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
kPremul_SkAlphaType);
GrSurfaceDesc desc;
desc.fFlags = kRenderTarget_GrSurfaceFlag;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fWidth = kW;
desc.fHeight = kH;
desc.fConfig = kSRGBA_8888_GrPixelConfig;
if (context->caps()->isConfigRenderable(desc.fConfig) &&
context->caps()->isConfigTexturable(desc.fConfig)) {
sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
desc, GrMipMapped::kNo,
SkBackingFit::kExact,
SkBudgeted::kNo);
if (!sContext) {
ERRORF(reporter, "Could not create SRGBA surface context.");
return;
}
float error = context->caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
// Write srgba data and read as srgba and then as rgba
if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
// For the all-srgba case, we allow a small error only for devices that have
// precision variation because the srgba data gets converted to linear and back in
// the shader.
float smallError = context->caps()->shaderCaps()->halfIs32Bits() ? 0.0f : 1.f;
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
check_srgb_to_linear_to_srgb_conversion, smallError,
"write/read srgba to srgba texture");
read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
check_srgb_to_linear_conversion, error,
"write srgba/read rgba with srgba texture");
} else {
ERRORF(reporter, "Could not write srgba data to srgba texture.");
}
// Now verify that we can write linear data
if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
// We allow more error on GPUs with lower precision shader variables.
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
check_linear_to_srgb_conversion, error,
"write rgba/read srgba with srgba texture");
read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
check_linear_to_srgb_to_linear_conversion, error,
"write/read rgba with srgba texture");
} else {
ERRORF(reporter, "Could not write rgba data to srgba texture.");
}
desc.fConfig = kRGBA_8888_GrPixelConfig;
sContext = context->contextPriv().makeDeferredSurfaceContext(desc,
GrMipMapped::kNo,
SkBackingFit::kExact,
SkBudgeted::kNo);
if (!sContext) {
ERRORF(reporter, "Could not create RGBA surface context.");
return;
}
// Write srgba data to a rgba texture and read back as srgba and rgba
if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
#if 0
// We don't support this conversion (read from untagged source into tagged destination.
// If we decide there is a meaningful way to implement this, restore this test.
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
check_srgb_to_linear_to_srgb_conversion, error,
"write/read srgba to rgba texture");
#endif
// We expect the sRGB -> linear write to do no sRGB conversion (to match the behavior of
// drawing tagged sources). skbug.com/6547. So the data we read should still contain
// sRGB encoded values.
//
// srgb_to_linear_to_srgb is a proxy for the expected identity transform.
read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
check_srgb_to_linear_to_srgb_conversion, error,
"write srgba/read rgba to rgba texture");
} else {
ERRORF(reporter, "Could not write srgba data to rgba texture.");
}
// Write rgba data to a rgba texture and read back as srgba
if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
check_linear_to_srgb_conversion, 1.2f,
"write rgba/read srgba to rgba texture");
} else {
ERRORF(reporter, "Could not write rgba data to rgba texture.");
}
}
}
#endif
|