xref: /aosp_15_r20/external/skia/src/gpu/ganesh/effects/GrColorTableEffect.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
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 "src/gpu/ganesh/effects/GrColorTableEffect.h"
9 
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkColorSpace.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkString.h"
16 #include "include/core/SkSurfaceProps.h"
17 #include "include/gpu/GpuTypes.h"
18 #include "include/private/SkSLSampleUsage.h"
19 #include "include/private/base/SkAssert.h"
20 #include "include/private/base/SkTo.h"
21 #include "include/private/gpu/ganesh/GrTypesPriv.h"
22 #include "src/base/SkRandom.h"
23 #include "src/gpu/ganesh/GrColorInfo.h"
24 #include "src/gpu/ganesh/GrFragmentProcessor.h"
25 #include "src/gpu/ganesh/GrFragmentProcessors.h"
26 #include "src/gpu/ganesh/GrProcessorUnitTest.h"
27 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
28 #include "src/gpu/ganesh/GrTestUtils.h"
29 #include "src/gpu/ganesh/SkGr.h"
30 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
31 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
32 
33 #include <cstdint>
34 #include <tuple>
35 #include <utility>
36 
37 class GrRecordingContext;
38 
ColorTableEffect(std::unique_ptr<GrFragmentProcessor> inputFP,GrSurfaceProxyView view)39 ColorTableEffect::ColorTableEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
40                                    GrSurfaceProxyView view)
41         // Not bothering with table-specific optimizations.
42         : GrFragmentProcessor(kColorTableEffect_ClassID, kNone_OptimizationFlags) {
43     this->registerChild(GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType),
44                         SkSL::SampleUsage::Explicit());
45     this->registerChild(std::move(inputFP));
46 }
47 
ColorTableEffect(const ColorTableEffect & that)48 ColorTableEffect::ColorTableEffect(const ColorTableEffect& that) : GrFragmentProcessor(that) {}
49 
onMakeProgramImpl() const50 std::unique_ptr<GrFragmentProcessor::ProgramImpl> ColorTableEffect::onMakeProgramImpl() const {
51     class Impl : public ProgramImpl {
52     public:
53         void emitCode(EmitArgs& args) override {
54             GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
55             SkString inputColor = this->invokeChild(kInputFPIndex, args);
56             SkString a = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.a, 0.5)");
57             SkString r = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.r, 1.5)");
58             SkString g = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.g, 2.5)");
59             SkString b = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.b, 3.5)");
60             fragBuilder->codeAppendf(
61                     "half4 coord = 255 * unpremul(%s) + 0.5;\n"
62                     "half4 color = half4(%s.a, %s.a, %s.a, 1);\n"
63                     "return color * %s.a;\n",
64                     inputColor.c_str(),
65                     r.c_str(),
66                     g.c_str(),
67                     b.c_str(),
68                     a.c_str());
69         }
70     };
71 
72     return std::make_unique<Impl>();
73 }
74 
Make(std::unique_ptr<GrFragmentProcessor> inputFP,GrRecordingContext * context,const SkBitmap & bitmap)75 std::unique_ptr<GrFragmentProcessor> ColorTableEffect::Make(
76         std::unique_ptr<GrFragmentProcessor> inputFP,
77         GrRecordingContext* context,
78         const SkBitmap& bitmap) {
79     SkASSERT(kPremul_SkAlphaType == bitmap.alphaType());
80     SkASSERT(bitmap.isImmutable());
81 
82     auto view = std::get<0>(GrMakeCachedBitmapProxyView(context,
83                                                         bitmap,
84                                                         /*label=*/"MakeColorTableEffect",
85                                                         skgpu::Mipmapped::kNo));
86     if (!view) {
87         return nullptr;
88     }
89 
90     return std::unique_ptr<GrFragmentProcessor>(
91             new ColorTableEffect(std::move(inputFP), std::move(view)));
92 }
93 
94 ///////////////////////////////////////////////////////////////////////////////
95 
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect)96 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect)
97 
98 #if defined(GPU_TEST_UTILS)
99 std::unique_ptr<GrFragmentProcessor> ColorTableEffect::TestCreate(GrProcessorTestData* d) {
100     int flags = 0;
101     uint8_t luts[256][4];
102     do {
103         for (int i = 0; i < 4; ++i) {
104             flags |= d->fRandom->nextBool() ? (1 << i) : 0;
105         }
106     } while (!flags);
107     for (int i = 0; i < 4; ++i) {
108         if (flags & (1 << i)) {
109             for (int j = 0; j < 256; ++j) {
110                 luts[j][i] = SkToU8(d->fRandom->nextBits(8));
111             }
112         }
113     }
114     auto filter(SkColorFilters::TableARGB((flags & (1 << 0)) ? luts[0] : nullptr,
115                                           (flags & (1 << 1)) ? luts[1] : nullptr,
116                                           (flags & (1 << 2)) ? luts[2] : nullptr,
117                                           (flags & (1 << 3)) ? luts[3] : nullptr));
118     sk_sp<SkColorSpace> colorSpace = GrTest::TestColorSpace(d->fRandom);
119     SkSurfaceProps props;  // default props for testing
120     auto [success, fp] = GrFragmentProcessors::Make(
121             d->context(),
122             filter.get(),
123             d->inputFP(),
124             GrColorInfo(GrColorType::kRGBA_8888, kUnknown_SkAlphaType, std::move(colorSpace)),
125             props);
126     SkASSERT(success);
127     return std::move(fp);
128 }
129 #endif
130