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 "include/core/SkBitmap.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkColorPriv.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTypes.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "include/private/GrTypesPriv.h"
22 #include "include/private/SkTemplates.h"
23 #include "src/core/SkOpts.h"
24 #include "src/gpu/GrCaps.h"
25 #include "src/gpu/GrDirectContextPriv.h"
26 #include "src/gpu/GrShaderCaps.h"
27 #include "tests/Test.h"
28 #include "tools/gpu/GrContextFactory.h"
29
30 #include <math.h>
31 #include <initializer_list>
32
33 /** convert 0..1 linear value to 0..1 srgb */
linear_to_srgb(float linear)34 static float linear_to_srgb(float linear) {
35 if (linear <= 0.0031308) {
36 return linear * 12.92f;
37 } else {
38 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
39 }
40 }
41
42 /** convert 0..1 srgb value to 0..1 linear */
srgb_to_linear(float srgb)43 static float srgb_to_linear(float srgb) {
44 if (srgb <= 0.04045f) {
45 return srgb / 12.92f;
46 } else {
47 return powf((srgb + 0.055f) / 1.055f, 2.4f);
48 }
49 }
50
check_gamma(uint32_t src,uint32_t dst,bool toSRGB,float error,uint32_t * expected)51 bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error,
52 uint32_t* expected) {
53 bool result = true;
54 uint32_t expectedColor = src & 0xff000000;
55
56 // Alpha should always be exactly preserved.
57 if ((src & 0xff000000) != (dst & 0xff000000)) {
58 result = false;
59 }
60
61 // need to unpremul before we can perform srgb magic
62 float invScale = 0;
63 float alpha = SkGetPackedA32(src);
64 if (alpha) {
65 invScale = 255.0f / alpha;
66 }
67
68 for (int c = 0; c < 3; ++c) {
69 float srcComponent = ((src & (0xff << (c * 8))) >> (c * 8)) * invScale;
70 float lower = std::max(0.f, srcComponent - error);
71 float upper = std::min(255.f, srcComponent + error);
72 if (toSRGB) {
73 lower = linear_to_srgb(lower / 255.f);
74 upper = linear_to_srgb(upper / 255.f);
75 } else {
76 lower = srgb_to_linear(lower / 255.f);
77 upper = srgb_to_linear(upper / 255.f);
78 }
79 lower *= alpha;
80 upper *= alpha;
81 SkASSERT(lower >= 0.f && lower <= 255.f);
82 SkASSERT(upper >= 0.f && upper <= 255.f);
83 uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8);
84 if (dstComponent < SkScalarFloorToInt(lower) ||
85 dstComponent > SkScalarCeilToInt(upper)) {
86 result = false;
87 }
88 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f);
89 expectedColor |= expectedComponent << (c * 8);
90 }
91
92 *expected = expectedColor;
93 return result;
94 }
95
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma,reporter,ctxInfo)96 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) {
97 auto context = ctxInfo.directContext();
98 static constexpr SkISize kBaseSize{256, 256};
99 static const size_t kRowBytes = sizeof(uint32_t) * kBaseSize.fWidth;
100
101 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kBaseSize);
102
103 SkAutoTMalloc<uint32_t> srcPixels(kBaseSize.area());
104 for (int y = 0; y < kBaseSize.fHeight; ++y) {
105 for (int x = 0; x < kBaseSize.fWidth; ++x) {
106 srcPixels.get()[y*kBaseSize.fWidth+x] = SkPreMultiplyARGB(x, y, x, 0xFF);
107 }
108 }
109
110 SkBitmap bm;
111 bm.installPixels(ii, srcPixels.get(), kRowBytes);
112 auto img = bm.asImage();
113
114 SkAutoTMalloc<uint32_t> read(kBaseSize.area());
115
116 // We allow more error on GPUs with lower precision shader variables.
117 float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
118
119 for (auto toSRGB : { false, true }) {
120 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
121
122 if (!dst) {
123 ERRORF(reporter, "Could not create surfaces for copy surface test.");
124 continue;
125 }
126
127 SkCanvas* dstCanvas = dst->getCanvas();
128
129 dstCanvas->clear(SK_ColorRED);
130 dst->flushAndSubmit();
131
132 SkPaint gammaPaint;
133 gammaPaint.setBlendMode(SkBlendMode::kSrc);
134 gammaPaint.setColorFilter(toSRGB ? SkColorFilters::LinearToSRGBGamma()
135 : SkColorFilters::SRGBToLinearGamma());
136
137 dstCanvas->drawImage(img, 0, 0, SkSamplingOptions(), &gammaPaint);
138 dst->flushAndSubmit();
139
140 sk_memset32(read.get(), 0, kBaseSize.fWidth * kBaseSize.fHeight);
141 if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
142 ERRORF(reporter, "Error calling readPixels");
143 continue;
144 }
145
146 bool abort = false;
147 // Validate that pixels were copied/transformed correctly.
148 for (int y = 0; y < kBaseSize.fHeight && !abort; ++y) {
149 for (int x = 0; x < kBaseSize.fWidth && !abort; ++x) {
150 uint32_t r = read.get()[y * kBaseSize.fWidth + x];
151 uint32_t s = srcPixels.get()[y * kBaseSize.fWidth + x];
152 uint32_t expected;
153 if (!check_gamma(s, r, toSRGB, error, &expected)) {
154 ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x "
155 "from src 0x%08x and mode %s. Got %08x", x, y, expected, s,
156 toSRGB ? "ToSRGB" : "ToLinear", r);
157 abort = true;
158 break;
159 }
160 }
161 }
162 }
163 }
164