| 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 |
#include "src/gpu/ganesh/image/GrMippedBitmap.h" |
| 33 |
|
| 34 |
#include <cstdint> |
| 35 |
#include <tuple> |
| 36 |
#include <utility> |
| 37 |
|
| 38 |
class GrRecordingContext; |
| 39 |
|
| 40 |
ColorTableEffect::ColorTableEffect(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 41 |
GrSurfaceProxyView view) |
| 42 |
// Not bothering with table-specific optimizations. |
| 43 |
: GrFragmentProcessor(kColorTableEffect_ClassID, kNone_OptimizationFlags) { |
| 44 |
this->registerChild(GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType), |
| 45 |
SkSL::SampleUsage::Explicit()); |
| 46 |
this->registerChild(std::move(inputFP)); |
| 47 |
} |
| 48 |
|
| 49 |
ColorTableEffect::ColorTableEffect(const ColorTableEffect& that) : GrFragmentProcessor(that) {} |
| 50 |
|
| 51 |
std::unique_ptr<GrFragmentProcessor::ProgramImpl> ColorTableEffect::onMakeProgramImpl() const { |
| 52 |
class Impl : public ProgramImpl { |
| 53 |
public: |
| 54 |
void emitCode(EmitArgs& args) override { |
| 55 |
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 56 |
SkString inputColor = this->invokeChild(kInputFPIndex, args); |
| 57 |
SkString a = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.a, 0.5)"); |
| 58 |
SkString r = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.r, 1.5)"); |
| 59 |
SkString g = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.g, 2.5)"); |
| 60 |
SkString b = this->invokeChild(kTexEffectFPIndex, args, "half2(coord.b, 3.5)"); |
| 61 |
fragBuilder->codeAppendf( |
| 62 |
"half4 coord = 255 * unpremul(%s) + 0.5;\n" |
| 63 |
"half4 color = half4(%s.a, %s.a, %s.a, 1);\n" |
| 64 |
"return color * %s.a;\n", |
| 65 |
inputColor.c_str(), |
| 66 |
r.c_str(), |
| 67 |
g.c_str(), |
| 68 |
b.c_str(), |
| 69 |
a.c_str()); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
return std::make_unique<Impl>(); |
| 74 |
} |
| 75 |
|
| 76 |
std::unique_ptr<GrFragmentProcessor> ColorTableEffect::Make( |
| 77 |
std::unique_ptr<GrFragmentProcessor> inputFP, |
| 78 |
GrRecordingContext* context, |
| 79 |
const GrMippedBitmap& bitmap) { |
| 80 |
SkASSERT(kPremul_SkAlphaType == bitmap.alphaType()); |
| 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 |
|
| 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->surfaceDrawContext(), |
| 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 |