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