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