OLD | NEW |
(Empty) | |
| 1 /* |
| 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 <initializer_list> |
| 9 #include "Test.h" |
| 10 |
| 11 #if SK_SUPPORT_GPU |
| 12 #include "GrContext.h" |
| 13 #include "GrTexture.h" |
| 14 #include "GrTextureProvider.h" |
| 15 |
| 16 #include "SkUtils.h" |
| 17 |
| 18 // using anonymous namespace because these functions are used as template param
s. |
| 19 namespace { |
| 20 /** convert 0..1 linear value to 0..1 srgb */ |
| 21 float linear_to_srgb(float linear) { |
| 22 if (linear <= 0.0031308) { |
| 23 return linear * 12.92f; |
| 24 } else { |
| 25 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f; |
| 26 } |
| 27 } |
| 28 } |
| 29 |
| 30 bool check_gamma(uint32_t src, uint32_t dst, float gamma, float error, uint32_t*
expected) { |
| 31 if (SkScalarNearlyEqual(gamma, 1.f)) { |
| 32 *expected = src; |
| 33 return src == dst; |
| 34 } else { |
| 35 bool result = true; |
| 36 uint32_t expectedColor = src & 0xff000000; |
| 37 |
| 38 // Alpha should always be exactly preserved. |
| 39 if ((src & 0xff000000) != (dst & 0xff000000)) { |
| 40 result = false; |
| 41 } |
| 42 |
| 43 for (int c = 0; c < 3; ++c) { |
| 44 uint8_t srcComponent = (src & (0xff << (c * 8))) >> (c * 8); |
| 45 float lower = SkTMax(0.f, (float)srcComponent - error); |
| 46 float upper = SkTMin(255.f, (float)srcComponent + error); |
| 47 if (SkScalarNearlyEqual(gamma, 1.0f / 2.2f)) { |
| 48 lower = linear_to_srgb(lower / 255.f); |
| 49 upper = linear_to_srgb(upper / 255.f); |
| 50 } else { |
| 51 lower = powf(lower / 255.f, gamma); |
| 52 upper = powf(upper / 255.f, gamma); |
| 53 } |
| 54 SkASSERT(lower >= 0.f && lower <= 255.f); |
| 55 SkASSERT(upper >= 0.f && upper <= 255.f); |
| 56 uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8); |
| 57 if (dstComponent < SkScalarFloorToInt(lower * 255.f) || |
| 58 dstComponent > SkScalarCeilToInt(upper * 255.f)) { |
| 59 result = false; |
| 60 } |
| 61 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 127
.5f); |
| 62 expectedColor |= expectedComponent << (c * 8); |
| 63 } |
| 64 |
| 65 *expected = expectedColor; |
| 66 return result; |
| 67 } |
| 68 } |
| 69 |
| 70 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) { |
| 71 GrContext* context = ctxInfo.fGrContext; |
| 72 static const int kW = 10; |
| 73 static const int kH = 10; |
| 74 static const size_t kRowBytes = sizeof(uint32_t) * kW; |
| 75 |
| 76 GrSurfaceDesc baseDesc; |
| 77 baseDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 78 baseDesc.fWidth = kW; |
| 79 baseDesc.fHeight = kH; |
| 80 |
| 81 SkAutoTMalloc<uint32_t> srcPixels(kW * kH); |
| 82 for (int i = 0; i < kW * kH; ++i) { |
| 83 srcPixels.get()[i] = i; |
| 84 } |
| 85 |
| 86 SkAutoTMalloc<uint32_t> dstPixels(kW * kH); |
| 87 for (int i = 0; i < kW * kH; ++i) { |
| 88 dstPixels.get()[i] = ~i; |
| 89 } |
| 90 |
| 91 SkAutoTMalloc<uint32_t> read(kW * kH); |
| 92 |
| 93 // We allow more error on GPUs with lower precision shader variables. |
| 94 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f :
0.5f; |
| 95 |
| 96 for (auto sOrigin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
}) { |
| 97 for (auto dOrigin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOri
gin }) { |
| 98 for (auto sFlags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFla
gs }) { |
| 99 for (auto gamma : { 1.0f, 1.0f / 1.8f, 1.0f / 2.2f }) { |
| 100 GrSurfaceDesc srcDesc = baseDesc; |
| 101 srcDesc.fOrigin = sOrigin; |
| 102 srcDesc.fFlags = sFlags; |
| 103 GrSurfaceDesc dstDesc = baseDesc; |
| 104 dstDesc.fOrigin = dOrigin; |
| 105 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 106 |
| 107 SkAutoTUnref<GrTexture> src( |
| 108 context->textureProvider()->createTexture(srcDesc, SkBud
geted::kNo, |
| 109 srcPixels.get(
), |
| 110 kRowBytes)); |
| 111 SkAutoTUnref<GrTexture> dst( |
| 112 context->textureProvider()->createTexture(dstDesc, SkBud
geted::kNo, |
| 113 dstPixels.get(
), |
| 114 kRowBytes)); |
| 115 if (!src || !dst) { |
| 116 ERRORF(reporter, "Could not create surfaces for copy sur
face test."); |
| 117 continue; |
| 118 } |
| 119 |
| 120 bool result = context->applyGamma(dst->asRenderTarget(), src
, gamma); |
| 121 |
| 122 // To make the copied src rect correct we would apply any ds
t clipping |
| 123 // back to the src rect, but we don't use it again so don't
bother. |
| 124 if (!result) { |
| 125 ERRORF(reporter, "Unexpected failure from applyGamma."); |
| 126 continue; |
| 127 } |
| 128 |
| 129 sk_memset32(read.get(), 0, kW * kH); |
| 130 if (!dst->readPixels(0, 0, kW, kH, baseDesc.fConfig, read.ge
t(), kRowBytes)) { |
| 131 ERRORF(reporter, "Error calling readPixels"); |
| 132 continue; |
| 133 } |
| 134 |
| 135 bool abort = false; |
| 136 // Validate that pixels were copied/transformed correctly. |
| 137 for (int y = 0; y < kH && !abort; ++y) { |
| 138 for (int x = 0; x < kW && !abort; ++x) { |
| 139 uint32_t r = read.get()[y * kW + x]; |
| 140 uint32_t s = srcPixels.get()[y * kW + x]; |
| 141 uint32_t expected; |
| 142 if (!check_gamma(s, r, gamma, error, &expected)) { |
| 143 ERRORF(reporter, "Expected dst %d,%d to contain
0x%08x " |
| 144 "from src 0x%08x and gamma %f. Got %08x", |
| 145 x, y, expected, s, gamma, r); |
| 146 abort = true; |
| 147 break; |
| 148 } |
| 149 } |
| 150 } |
| 151 } |
| 152 } |
| 153 } |
| 154 } |
| 155 } |
| 156 #endif |
OLD | NEW |