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