bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 1 | /* |
| 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 |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 14 | #include "GrSurfaceContext.h" |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 15 | #include "SkCanvas.h" |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 16 | #include "SkGr.h" |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 17 | #include "SkSurface.h" |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 18 | |
| 19 | // using anonymous namespace because these functions are used as template params. |
| 20 | namespace { |
| 21 | /** convert 0..1 srgb value to 0..1 linear */ |
| 22 | float 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 */ |
| 31 | float 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 */ |
| 41 | template <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 */ |
| 66 | template <float (*FORWARD)(float), float (*BACKWARD)(float)> |
| 67 | static 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 | |
| 100 | static 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 | |
| 104 | static 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 | |
| 108 | static 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 | |
| 112 | static 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 | |
| 116 | typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error); |
| 117 | |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 118 | void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context, |
| 119 | uint32_t* origData, |
| 120 | const SkImageInfo& dstInfo, CheckFn checker, float error, |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 121 | const char* subtestName) { |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 122 | int w = dstInfo.width(); |
| 123 | int h = dstInfo.height(); |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 124 | SkAutoTMalloc<uint32_t> readData(w * h); |
| 125 | memset(readData.get(), 0, sizeof(uint32_t) * w * h); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 126 | |
| 127 | if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) { |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 128 | ERRORF(reporter, "Could not read pixels for %s.", subtestName); |
| 129 | return; |
| 130 | } |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 131 | |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 132 | 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. |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 148 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SRGBReadWritePixels, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 149 | GrContext* context = ctxInfo.grContext(); |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 150 | #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 |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 155 | static const int kW = 255; |
| 156 | static const int kH = 255; |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 157 | #endif |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 158 | 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 Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 165 | 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); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 170 | GrSurfaceDesc desc; |
| 171 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame^] | 172 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 173 | desc.fWidth = kW; |
| 174 | desc.fHeight = kH; |
| 175 | desc.fConfig = kSRGBA_8888_GrPixelConfig; |
| 176 | if (context->caps()->isConfigRenderable(desc.fConfig, false) && |
| 177 | context->caps()->isConfigTexturable(desc.fConfig)) { |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 178 | |
| 179 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext( |
| 180 | desc, SkBackingFit::kExact, |
| 181 | SkBudgeted::kNo); |
| 182 | if (!sContext) { |
| 183 | ERRORF(reporter, "Could not create SRGBA surface context."); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 184 | return; |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 185 | } |
| 186 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 187 | float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f; |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 188 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 189 | // Write srgba data and read as srgba and then as rgba |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 190 | if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 191 | // For the all-srgba case, we allow a small error only for devices that have |
| 192 | // precision variation because the srgba data gets converted to linear and back in |
| 193 | // the shader. |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 194 | float smallError = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.f : 0.0f; |
| 195 | read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 196 | check_srgb_to_linear_to_srgb_conversion, smallError, |
| 197 | "write/read srgba to srgba texture"); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 198 | read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 199 | check_srgb_to_linear_conversion, error, |
| 200 | "write srgba/read rgba with srgba texture"); |
| 201 | } else { |
| 202 | ERRORF(reporter, "Could not write srgba data to srgba texture."); |
| 203 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 204 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 205 | // Now verify that we can write linear data |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 206 | if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 207 | // We allow more error on GPUs with lower precision shader variables. |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 208 | read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 209 | check_linear_to_srgb_conversion, error, |
| 210 | "write rgba/read srgba with srgba texture"); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 211 | read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 212 | check_linear_to_srgb_to_linear_conversion, error, |
| 213 | "write/read rgba with srgba texture"); |
| 214 | } else { |
| 215 | ERRORF(reporter, "Could not write rgba data to srgba texture."); |
| 216 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 217 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 218 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 219 | sContext = context->contextPriv().makeDeferredSurfaceContext(desc, SkBackingFit::kExact, |
| 220 | SkBudgeted::kNo); |
| 221 | if (!sContext) { |
| 222 | ERRORF(reporter, "Could not create RGBA surface context."); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 223 | return; |
| 224 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 225 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 226 | // Write srgba data to a rgba texture and read back as srgba and rgba |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 227 | if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) { |
| 228 | read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 229 | check_srgb_to_linear_to_srgb_conversion, error, |
| 230 | "write/read srgba to rgba texture"); |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 231 | read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 232 | check_srgb_to_linear_conversion, error, |
| 233 | "write srgba/read rgba to rgba texture"); |
| 234 | } else { |
| 235 | ERRORF(reporter, "Could not write srgba data to rgba texture."); |
| 236 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 237 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 238 | // Write rgba data to a rgba texture and read back as srgba |
Robert Phillips | 301431d | 2017-03-29 12:08:49 -0400 | [diff] [blame] | 239 | if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) { |
| 240 | read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 241 | check_linear_to_srgb_conversion, 1.2f, |
| 242 | "write rgba/read srgba to rgba texture"); |
| 243 | } else { |
| 244 | ERRORF(reporter, "Could not write rgba data to rgba texture."); |
| 245 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | #endif |