xref: /aosp_15_r20/external/skia/tests/SRGBTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkAlphaType.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkTypes.h"
13 #include "src/base/SkArenaAlloc.h"
14 #include "src/core/SkColorSpaceXformSteps.h"
15 #include "src/core/SkRasterPipeline.h"
16 #include "src/core/SkRasterPipelineOpContexts.h"
17 #include "src/core/SkRasterPipelineOpList.h"
18 #include "tests/Test.h"
19 
20 #include <cstdint>
21 #include <cstring>
22 
DEF_TEST(srgb_roundtrip,r)23 DEF_TEST(srgb_roundtrip, r) {
24     uint32_t reds[256];
25     for (int i = 0; i < 256; i++) {
26         reds[i] = i;
27     }
28 
29     SkRasterPipeline_MemoryCtx ptr = { reds, 0 };
30 
31     sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
32                         linear = sRGB->makeLinearGamma();
33     const SkAlphaType upm = kUnpremul_SkAlphaType;
34 
35     SkColorSpaceXformSteps linearize{  sRGB.get(),upm,  linear.get(),upm},
36                            reencode {linear.get(),upm,    sRGB.get(),upm};
37 
38     SkRasterPipeline_<256> p;
39     p.append(SkRasterPipelineOp::load_8888,  &ptr);
40     linearize.apply(&p);
41     reencode .apply(&p);
42     p.append(SkRasterPipelineOp::store_8888, &ptr);
43 
44     p.run(0,0,256,1);
45 
46     for (int i = 0; i < 256; i++) {
47         if (reds[i] != (uint32_t)i) {
48             ERRORF(r, "%d doesn't round trip, %u", i, reds[i]);
49         }
50     }
51 }
52 
DEF_TEST(srgb_edge_cases,r)53 DEF_TEST(srgb_edge_cases, r) {
54     // We need to run at least 4 pixels to make sure we hit all specializations.
55     float colors[4][4] = { {0,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
56     auto& color = colors[0];
57 
58     SkRasterPipeline_MemoryCtx dst = { &color, 0 };
59 
60     sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
61                         linear = sRGB->makeLinearGamma();
62     const SkAlphaType upm = kUnpremul_SkAlphaType;
63 
64     SkColorSpaceXformSteps steps {linear.get(),upm,    sRGB.get(),upm};
65 
66     SkSTArenaAlloc<256> alloc;
67     SkRasterPipeline p(&alloc);
68     p.appendConstantColor(&alloc, color);
69     steps.apply(&p);
70     p.append(SkRasterPipelineOp::store_f32, &dst);
71     p.run(0,0,4,1);
72 
73     if (color[0] != 0.0f) {
74         ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color[0]);
75     }
76     if (color[1] != 1.0f) {
77         float f = color[1];
78         uint32_t x;
79         memcpy(&x, &f, 4);
80         ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color[1], x);
81     }
82 }
83 
84 // Linearize and then re-encode pixel values, testing that the output is close to the input.
DEF_TEST(srgb_roundtrip_extended,r)85 DEF_TEST(srgb_roundtrip_extended, r) {
86     static const int kSteps = 128;
87     SkColor4f rgba[kSteps];
88 
89     auto expected = [=](int i) {
90         float scale = 10000.0f / (3*kSteps);
91         return SkColor4f{
92             (3*i+0) * scale,
93             (3*i+1) * scale,
94             (3*i+2) * scale,
95             1.0f,
96         };
97     };
98 
99     for (int i = 0; i < kSteps; i++) {
100         rgba[i] = expected(i);
101     }
102 
103     SkRasterPipeline_MemoryCtx ptr = { rgba, 0 };
104 
105     sk_sp<SkColorSpace> cs = SkColorSpace::MakeSRGB();
106     sk_sp<SkColorSpace> linear = cs->makeLinearGamma();
107     const SkAlphaType upm = kUnpremul_SkAlphaType;
108 
109     SkColorSpaceXformSteps linearize{    cs.get(),upm,  linear.get(),upm},
110                            reencode {linear.get(),upm,      cs.get(),upm};
111 
112     SkRasterPipeline_<256> p;
113     p.append(SkRasterPipelineOp::load_f32,  &ptr);
114     linearize.apply(&p);
115     reencode .apply(&p);
116     p.append(SkRasterPipelineOp::store_f32, &ptr);
117     p.run(0,0,kSteps,1);
118 
119     auto close = [=](float x, float y) {
120         return x == y
121             || (x/y < 1.001f && y/x < 1.001f);
122     };
123 
124     for (int i = 0; i < kSteps; i++) {
125         SkColor4f want = expected(i);
126     #if 0
127         SkDebugf("got %g %g %g, want %g %g %g\n",
128                  rgba[i].fR, rgba[i].fG, rgba[i].fB,
129                  want.fR, want.fG, want.fB);
130     #endif
131         REPORTER_ASSERT(r, close(rgba[i].fR, want.fR));
132         REPORTER_ASSERT(r, close(rgba[i].fG, want.fG));
133         REPORTER_ASSERT(r, close(rgba[i].fB, want.fB));
134     }
135 }
136