Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrColorTableEffect.cpp
Go to the documentation of this file.
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
9
22#include "src/base/SkRandom.h"
29#include "src/gpu/ganesh/SkGr.h"
32
33#include <cstdint>
34#include <tuple>
35#include <utility>
36
38
39ColorTableEffect::ColorTableEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
41 // Not bothering with table-specific optimizations.
42 : GrFragmentProcessor(kColorTableEffect_ClassID, kNone_OptimizationFlags) {
43 this->registerChild(GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType),
45 this->registerChild(std::move(inputFP));
46}
47
48ColorTableEffect::ColorTableEffect(const ColorTableEffect& that) : GrFragmentProcessor(that) {}
49
50std::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
75std::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
97
98#if defined(GR_TEST_UTILS)
99std::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(),
125 props);
126 SkASSERT(success);
127 return std::move(fp);
128}
129#endif
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
@ kUnknown_SkAlphaType
uninitialized
Definition SkAlphaType.h:27
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
std::tuple< GrSurfaceProxyView, GrColorType > GrMakeCachedBitmapProxyView(GrRecordingContext *rContext, const SkBitmap &bitmap, std::string_view label, skgpu::Mipmapped mipmapped)
Definition SkGr.cpp:188
constexpr uint8_t SkToU8(S x)
Definition SkTo.h:22
static std::unique_ptr< GrFragmentProcessor > Make(std::unique_ptr< GrFragmentProcessor > inputFP, GrRecordingContext *context, const SkBitmap &bitmap)
std::unique_ptr< ProgramImpl > onMakeProgramImpl() const override
void codeAppendf(const char format[],...) SK_PRINTF_LIKE(2
static std::unique_ptr< GrFragmentProcessor > Make(GrSurfaceProxyView, SkAlphaType, const SkMatrix &=SkMatrix::I(), GrSamplerState::Filter=GrSamplerState::Filter::kNearest, GrSamplerState::MipmapMode mipmapMode=GrSamplerState::MipmapMode::kNone)
static sk_sp< SkColorFilter > TableARGB(const uint8_t tableA[256], const uint8_t tableR[256], const uint8_t tableG[256], const uint8_t tableB[256])
static SampleUsage Explicit()
const char * c_str() const
Definition SkString.h:133
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
static bool b
struct MyStruct a[10]
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
std::unique_ptr< GrFragmentProcessor > Make(const SkMaskFilter *maskfilter, const GrFPArgs &args, const SkMatrix &ctm)
const uint32_t fp