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, 2.2f)) { | |
48 lower = linear_to_srgb(lower / 255.f); | |
49 upper = linear_to_srgb(upper / 255.f); | |
50 } else { | |
51 float invGamma = 1.f / gamma; | |
52 lower = powf(lower / 255.f, invGamma); | |
53 upper = powf(upper / 255.f, invGamma); | |
54 } | |
55 SkASSERT(lower >= 0.f && lower <= 255.f); | |
56 SkASSERT(upper >= 0.f && upper <= 255.f); | |
57 uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8); | |
58 if (dstComponent < SkScalarFloorToInt(lower * 255.f) || | |
59 dstComponent > SkScalarCeilToInt(upper * 255.f)) { | |
60 result = false; | |
61 } | |
62 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 127 .5f); | |
63 expectedColor |= expectedComponent << (c * 8); | |
64 } | |
65 | |
66 *expected = expectedColor; | |
67 return result; | |
68 } | |
69 } | |
70 | |
71 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) { | |
72 GrContext* context = ctxInfo.fGrContext; | |
73 static const int kW = 10; | |
74 static const int kH = 10; | |
75 static const size_t kRowBytes = sizeof(uint32_t) * kW; | |
76 | |
77 GrSurfaceDesc baseDesc; | |
78 baseDesc.fConfig = kRGBA_8888_GrPixelConfig; | |
79 baseDesc.fWidth = kW; | |
80 baseDesc.fHeight = kH; | |
81 | |
82 SkAutoTMalloc<uint32_t> srcPixels(kW * kH); | |
83 for (int i = 0; i < kW * kH; ++i) { | |
84 srcPixels.get()[i] = i; | |
85 } | |
86 | |
87 SkAutoTMalloc<uint32_t> dstPixels(kW * kH); | |
88 for (int i = 0; i < kW * kH; ++i) { | |
89 dstPixels.get()[i] = ~i; | |
90 } | |
91 | |
92 SkAutoTMalloc<uint32_t> read(kW * kH); | |
93 | |
94 // We allow more error on GPUs with lower precision shader variables. | |
95 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f; | |
96 | |
97 for (auto sOrigin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) { | |
98 for (auto dOrigin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOri gin }) { | |
99 for (auto sFlags : { kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFla gs }) { | |
100 for (auto gamma : { 1.0f, 1.8f, 2.2f }) { | |
Brian Osman
2016/05/02 21:02:45
Tests identity, sRGB and arbitrary gamma (to exerc
| |
101 GrSurfaceDesc srcDesc = baseDesc; | |
102 srcDesc.fOrigin = sOrigin; | |
103 srcDesc.fFlags = sFlags; | |
104 GrSurfaceDesc dstDesc = baseDesc; | |
105 dstDesc.fOrigin = dOrigin; | |
106 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; | |
107 | |
108 SkAutoTUnref<GrTexture> src( | |
109 context->textureProvider()->createTexture(srcDesc, SkBud geted::kNo, | |
110 srcPixels.get( ), | |
111 kRowBytes)); | |
112 SkAutoTUnref<GrTexture> dst( | |
113 context->textureProvider()->createTexture(dstDesc, SkBud geted::kNo, | |
114 dstPixels.get( ), | |
115 kRowBytes)); | |
116 if (!src || !dst) { | |
117 ERRORF(reporter, "Could not create surfaces for copy sur face test."); | |
118 continue; | |
119 } | |
120 | |
121 bool result = context->applyGamma(dst->asRenderTarget(), src , gamma); | |
122 | |
123 // To make the copied src rect correct we would apply any ds t clipping | |
124 // back to the src rect, but we don't use it again so don't bother. | |
125 if (!result) { | |
126 ERRORF(reporter, "Unexpected failure from applyGamma."); | |
127 continue; | |
128 } | |
129 | |
130 sk_memset32(read.get(), 0, kW * kH); | |
131 if (!dst->readPixels(0, 0, kW, kH, baseDesc.fConfig, read.ge t(), kRowBytes)) { | |
132 ERRORF(reporter, "Error calling readPixels"); | |
133 continue; | |
134 } | |
135 | |
136 bool abort = false; | |
137 // Validate that pixels were copied/transformed correctly. | |
138 for (int y = 0; y < kH && !abort; ++y) { | |
139 for (int x = 0; x < kW && !abort; ++x) { | |
140 uint32_t r = read.get()[y * kW + x]; | |
141 uint32_t s = srcPixels.get()[y * kW + x]; | |
142 uint32_t expected; | |
143 if (!check_gamma(s, r, gamma, error, &expected)) { | |
144 ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x " | |
145 "from src 0x%08x and gamma %f. Got %08x", | |
146 x, y, expected, s, gamma, r); | |
147 abort = true; | |
148 break; | |
149 } | |
150 } | |
151 } | |
152 } | |
153 } | |
154 } | |
155 } | |
156 } | |
157 #endif | |
OLD | NEW |