blob: 8c01854b40a36fa8c80defe04fc7741ff8190cba [file] [log] [blame]
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05008/**************************************************************************************************
9 *** This file was autogenerated from GrConfigConversionEffect.fp; do not modify.
10 **************************************************************************************************/
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000011#include "GrConfigConversionEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012
John Stiles45f5b032020-07-27 17:31:29 -040013#include "src/core/SkUtils.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000014#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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"
Ethan Nicholas839872c2017-10-05 12:36:59 -040020class GrGLSLConfigConversionEffect : public GrGLSLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000021public:
Ethan Nicholas839872c2017-10-05 12:36:59 -040022 GrGLSLConfigConversionEffect() {}
robertphillips9cdb9922016-02-03 12:25:40 -080023 void emitCode(EmitArgs& args) override {
Mike Kleind6ab77a2019-03-21 08:18:24 -050024 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
25 const GrConfigConversionEffect& _outer = args.fFp.cast<GrConfigConversionEffect>();
Ethan Nicholas839872c2017-10-05 12:36:59 -040026 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040027 auto pmConversion = _outer.pmConversion;
Ethan Nicholas82399462017-10-16 12:35:44 -040028 (void)pmConversion;
joshualitt30ba4362014-08-21 20:18:45 -070029
Ethan Nicholasf7b88202017-09-18 14:10:39 -040030 fragBuilder->forceHighPrecision();
John Stiles6e7cfaf2020-11-03 14:07:38 -050031 SkString _sample0 = this->invokeChild(0, args);
Ethan Nicholas839872c2017-10-05 12:36:59 -040032 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040033 R"SkSL(%s = floor(%s * 255.0 + 0.5) / 255.0;
34@switch (%d) {
35 case 0:
36 %s.xyz = floor((%s.xyz * %s.w) * 255.0 + 0.5) / 255.0;
37 break;
38 case 1:
39 %s.xyz = %s.w <= 0.0 ? half3(0.0) : floor((%s.xyz / %s.w) * 255.0 + 0.5) / 255.0;
40 break;
41}
42)SkSL",
John Stiles6e7cfaf2020-11-03 14:07:38 -050043 args.fOutputColor, _sample0.c_str(), (int)_outer.pmConversion, args.fOutputColor,
Mike Kleind6ab77a2019-03-21 08:18:24 -050044 args.fOutputColor, args.fOutputColor, args.fOutputColor, args.fOutputColor,
45 args.fOutputColor, args.fOutputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000046 }
47
48private:
Ethan Nicholas839872c2017-10-05 12:36:59 -040049 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050050 const GrFragmentProcessor& _proc) override {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000051};
Ethan Nicholas839872c2017-10-05 12:36:59 -040052GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const {
53 return new GrGLSLConfigConversionEffect();
54}
Mike Kleind6ab77a2019-03-21 08:18:24 -050055void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas839872c2017-10-05 12:36:59 -040056 GrProcessorKeyBuilder* b) const {
John Stiles45f5b032020-07-27 17:31:29 -040057 b->add32((uint32_t)pmConversion);
Ethan Nicholas839872c2017-10-05 12:36:59 -040058}
59bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& other) const {
60 const GrConfigConversionEffect& that = other.cast<GrConfigConversionEffect>();
61 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040062 if (pmConversion != that.pmConversion) return false;
Ethan Nicholas839872c2017-10-05 12:36:59 -040063 return true;
64}
John Stiles735a5a72020-08-26 10:21:10 -040065bool GrConfigConversionEffect::usesExplicitReturn() const { return false; }
Ethan Nicholas839872c2017-10-05 12:36:59 -040066GrConfigConversionEffect::GrConfigConversionEffect(const GrConfigConversionEffect& src)
Ethan Nicholasabff9562017-10-09 10:54:08 -040067 : INHERITED(kGrConfigConversionEffect_ClassID, src.optimizationFlags())
John Stilesb89e52e2020-07-07 14:25:50 -040068 , pmConversion(src.pmConversion) {
Brian Osman12c5d292020-07-13 16:11:35 -040069 this->cloneAndRegisterAllChildProcessors(src);
John Stilesb89e52e2020-07-07 14:25:50 -040070}
Brian Salomonaff329b2017-08-11 09:40:37 -040071std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -040072 return std::make_unique<GrConfigConversionEffect>(*this);
Brian Salomonfcc527b2017-07-26 12:21:21 -040073}
John Stiles8d9bf642020-08-12 15:07:45 -040074#if GR_TEST_UTILS
John Stilescab58862020-08-12 15:47:06 -040075SkString GrConfigConversionEffect::onDumpInfo() const {
76 return SkStringPrintf("(pmConversion=%d)", (int)pmConversion);
John Stiles47b4e222020-08-12 09:56:50 -040077}
78#endif
joshualittb0a8a372014-09-23 09:50:21 -070079GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
Hal Canary6f6961e2017-01-31 13:50:44 -050080#if GR_TEST_UTILS
Ethan Nicholas839872c2017-10-05 12:36:59 -040081std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(
82 GrProcessorTestData* data) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -050083 PMConversion pmConv = static_cast<PMConversion>(
84 data->fRandom->nextULessThan((int)PMConversion::kPMConversionCnt));
John Stilesb89e52e2020-07-07 14:25:50 -040085 return std::unique_ptr<GrFragmentProcessor>(
86 new GrConfigConversionEffect(GrProcessorUnitTest::MakeChildFP(data), pmConv));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000087}
Hal Canary6f6961e2017-01-31 13:50:44 -050088#endif
John Stilesb89e52e2020-07-07 14:25:50 -040089
Adlai Hollerc95b5892020-08-11 12:02:22 -040090bool GrConfigConversionEffect::TestForPreservingPMConversions(GrDirectContext* dContext) {
John Stilesb89e52e2020-07-07 14:25:50 -040091 static constexpr int kSize = 256;
92 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
93 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
94 uint32_t* srcData = data.get();
95 uint32_t* firstRead = data.get() + kSize * kSize;
96 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
97
98 // Fill with every possible premultiplied A, color channel value. There will be 256-y
99 // duplicate values in row y. We set r, g, and b to the same value since they are handled
100 // identically.
101 for (int y = 0; y < kSize; ++y) {
102 for (int x = 0; x < kSize; ++x) {
103 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]);
104 color[3] = y;
105 color[2] = std::min(x, y);
106 color[1] = std::min(x, y);
107 color[0] = std::min(x, y);
108 }
109 }
110 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
111 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
112
113 const SkImageInfo ii =
114 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
115
Adlai Hollerc95b5892020-08-11 12:02:22 -0400116 auto readRTC = GrRenderTargetContext::Make(dContext, kColorType, nullptr, SkBackingFit::kExact,
John Stilesb89e52e2020-07-07 14:25:50 -0400117 {kSize, kSize});
Adlai Hollerc95b5892020-08-11 12:02:22 -0400118 auto tempRTC = GrRenderTargetContext::Make(dContext, kColorType, nullptr, SkBackingFit::kExact,
John Stilesb89e52e2020-07-07 14:25:50 -0400119 {kSize, kSize});
120 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
121 return false;
122 }
123 // Adding discard to appease vulkan validation warning about loading uninitialized data on
124 // draw
125 readRTC->discard();
126
Adlai Holler3b124702020-10-14 14:07:31 -0400127 // This function is only ever called if we are in a GrDirectContext since we are
John Stilesb89e52e2020-07-07 14:25:50 -0400128 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
129 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
130 SkBitmap bitmap;
131 bitmap.installPixels(ii, srcData, 4 * kSize);
132 bitmap.setImmutable();
133
Adlai Hollerc95b5892020-08-11 12:02:22 -0400134 GrBitmapTextureMaker maker(dContext, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400135 auto dataView = maker.view(GrMipmapped::kNo);
John Stilesb89e52e2020-07-07 14:25:50 -0400136 if (!dataView) {
137 return false;
138 }
139
140 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
141
142 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
143 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
144 // We then verify that two reads produced the same values.
145
146 GrPaint paint1;
John Stiles5933d7d2020-07-21 12:28:35 -0400147 paint1.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400148 GrTextureEffect::Make(std::move(dataView), kPremul_SkAlphaType),
149 PMConversion::kToUnpremul));
150 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
151
152 readRTC->fillRectToRect(nullptr, std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect, kRect);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400153 if (!readRTC->readPixels(dContext, ii, firstRead, 0, {0, 0})) {
John Stilesb89e52e2020-07-07 14:25:50 -0400154 return false;
155 }
156
157 // Adding discard to appease vulkan validation warning about loading uninitialized data on
158 // draw
159 tempRTC->discard();
160
161 GrPaint paint2;
John Stiles5933d7d2020-07-21 12:28:35 -0400162 paint2.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400163 GrTextureEffect::Make(readRTC->readSurfaceView(), kUnpremul_SkAlphaType),
164 PMConversion::kToPremul));
165 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
166
167 tempRTC->fillRectToRect(nullptr, std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect, kRect);
168
169 GrPaint paint3;
John Stiles5933d7d2020-07-21 12:28:35 -0400170 paint3.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400171 GrTextureEffect::Make(tempRTC->readSurfaceView(), kPremul_SkAlphaType),
172 PMConversion::kToUnpremul));
173 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
174
175 readRTC->fillRectToRect(nullptr, std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect, kRect);
176
Adlai Hollerc95b5892020-08-11 12:02:22 -0400177 if (!readRTC->readPixels(dContext, ii, secondRead, 0, {0, 0})) {
John Stilesb89e52e2020-07-07 14:25:50 -0400178 return false;
179 }
180
181 for (int y = 0; y < kSize; ++y) {
182 for (int x = 0; x <= y; ++x) {
183 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
184 return false;
185 }
186 }
187 }
188
189 return true;
190}