blob: 0c096b317e68d9a4b1781540d639a451434d00ec [file] [log] [blame]
Brian Osman1c467772021-07-02 13:11:15 +00001/*
2 * Copyright 2018 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/**************************************************************************************************
9 *** This file was autogenerated from GrConfigConversionEffect.fp; do not modify.
10 **************************************************************************************************/
11#include "GrConfigConversionEffect.h"
12
13#include "src/core/SkUtils.h"
14#include "src/gpu/GrTexture.h"
15#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
16#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
17#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
18#include "src/sksl/SkSLCPP.h"
19#include "src/sksl/SkSLUtil.h"
20class GrGLSLConfigConversionEffect : public GrGLSLFragmentProcessor {
21public:
22 GrGLSLConfigConversionEffect() {}
23 void emitCode(EmitArgs& args) override {
24 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
25 const GrConfigConversionEffect& _outer = args.fFp.cast<GrConfigConversionEffect>();
26 (void)_outer;
27 auto pmConversion = _outer.pmConversion;
28 (void)pmConversion;
29 SkString _sample0 = this->invokeChild(0, args);
30 fragBuilder->codeAppendf(
31 R"SkSL(half4 color = floor(%s * 255.0 + 0.5) / 255.0;
32@switch (%d) {
33 case 0:
34 color.xyz = floor((color.xyz * color.w) * 255.0 + 0.5) / 255.0;
35 break;
36 case 1:
37 color.xyz = color.w <= 0.0 ? half3(0.0) : floor((color.xyz / color.w) * 255.0 + 0.5) / 255.0;
38 break;
39}
40return color;
41)SkSL",
42 _sample0.c_str(),
43 (int)_outer.pmConversion);
44 }
45
46private:
47 void onSetData(const GrGLSLProgramDataManager& pdman,
48 const GrFragmentProcessor& _proc) override {}
49};
50std::unique_ptr<GrGLSLFragmentProcessor> GrConfigConversionEffect::onMakeProgramImpl() const {
51 return std::make_unique<GrGLSLConfigConversionEffect>();
52}
53void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
54 GrProcessorKeyBuilder* b) const {
55 b->addBits(1, (uint32_t)pmConversion, "pmConversion");
56}
57bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& other) const {
58 const GrConfigConversionEffect& that = other.cast<GrConfigConversionEffect>();
59 (void)that;
60 if (pmConversion != that.pmConversion) return false;
61 return true;
62}
63GrConfigConversionEffect::GrConfigConversionEffect(const GrConfigConversionEffect& src)
64 : INHERITED(kGrConfigConversionEffect_ClassID, src.optimizationFlags())
65 , pmConversion(src.pmConversion) {
66 this->cloneAndRegisterAllChildProcessors(src);
67}
68std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::clone() const {
69 return std::make_unique<GrConfigConversionEffect>(*this);
70}
71#if GR_TEST_UTILS
72SkString GrConfigConversionEffect::onDumpInfo() const {
73 return SkStringPrintf("(pmConversion=%d)", (int)pmConversion);
74}
75#endif
76GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
77#if GR_TEST_UTILS
78std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(
79 GrProcessorTestData* data) {
80 PMConversion pmConv =
81 static_cast<PMConversion>(data->fRandom->nextRangeU(0, (int)PMConversion::kLast));
82 return std::unique_ptr<GrFragmentProcessor>(
83 new GrConfigConversionEffect(GrProcessorUnitTest::MakeChildFP(data), pmConv));
84}
85#endif
86
87bool GrConfigConversionEffect::TestForPreservingPMConversions(GrDirectContext* dContext) {
88 static constexpr int kSize = 256;
89 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
90 uint32_t* srcData = data.get();
91
92 // Fill with every possible premultiplied A, color channel value. There will be 256-y
93 // duplicate values in row y. We set r, g, and b to the same value since they are handled
94 // identically.
95 for (int y = 0; y < kSize; ++y) {
96 for (int x = 0; x < kSize; ++x) {
97 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]);
98 color[3] = y;
99 color[2] = std::min(x, y);
100 color[1] = std::min(x, y);
101 color[0] = std::min(x, y);
102 }
103 }
104
105 const SkImageInfo pmII =
106 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
107 const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType);
108
109 auto readSFC = GrSurfaceFillContext::Make(dContext, upmII, SkBackingFit::kExact);
110 auto tempSFC = GrSurfaceFillContext::Make(dContext, pmII, SkBackingFit::kExact);
111 if (!readSFC || !tempSFC) {
112 return false;
113 }
114
115 // This function is only ever called if we are in a GrDirectContext since we are
116 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
117 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
118 SkBitmap bitmap;
119 bitmap.installPixels(pmII, srcData, 4 * kSize);
120 bitmap.setImmutable();
121
122 auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap));
123 if (!dataView) {
124 return false;
125 }
126
127 uint32_t* firstRead = data.get() + kSize * kSize;
128 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
129 std::fill_n(firstRead, kSize * kSize, 0);
130 std::fill_n(secondRead, kSize * kSize, 0);
131
132 GrPixmap firstReadPM(upmII, firstRead, kSize * sizeof(uint32_t));
133 GrPixmap secondReadPM(upmII, secondRead, kSize * sizeof(uint32_t));
134
135 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
136 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
137 // We then verify that two reads produced the same values.
138
139 auto fp1 = GrConfigConversionEffect::Make(
140 GrTextureEffect::Make(std::move(dataView), bitmap.alphaType()),
141 PMConversion::kToUnpremul);
142 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1));
143 if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) {
144 return false;
145 }
146
147 auto fp2 = GrConfigConversionEffect::Make(
148 GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType()),
149 PMConversion::kToPremul);
150 tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2));
151
152 auto fp3 = GrConfigConversionEffect::Make(
153 GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType()),
154 PMConversion::kToUnpremul);
155 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3));
156
157 if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) {
158 return false;
159 }
160
161 for (int y = 0; y < kSize; ++y) {
162 for (int x = 0; x <= y; ++x) {
163 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
164 return false;
165 }
166 }
167 }
168
169 return true;
170}