blob: a49591582945444a87ceb52b946268f31b8c00d9 [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();
Robert Phillips4e105e22020-07-16 09:18:50 -040031 SkString _sample5748 = 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",
Robert Phillips4e105e22020-07-16 09:18:50 -040043 args.fOutputColor, _sample5748.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}
65GrConfigConversionEffect::GrConfigConversionEffect(const GrConfigConversionEffect& src)
Ethan Nicholasabff9562017-10-09 10:54:08 -040066 : INHERITED(kGrConfigConversionEffect_ClassID, src.optimizationFlags())
John Stilesb89e52e2020-07-07 14:25:50 -040067 , pmConversion(src.pmConversion) {
Brian Osman12c5d292020-07-13 16:11:35 -040068 this->cloneAndRegisterAllChildProcessors(src);
John Stilesb89e52e2020-07-07 14:25:50 -040069}
Brian Salomonaff329b2017-08-11 09:40:37 -040070std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::clone() const {
John Stilesfbd050b2020-08-03 13:21:46 -040071 return std::make_unique<GrConfigConversionEffect>(*this);
Brian Salomonfcc527b2017-07-26 12:21:21 -040072}
John Stiles47b4e222020-08-12 09:56:50 -040073#ifdef SK_DEBUG
74SkString GrConfigConversionEffect::dumpInfo() const {
75 return SkStringPrintf("ConfigConversionEffect(pmConversion=%d)", (int)pmConversion);
76}
77#endif
joshualittb0a8a372014-09-23 09:50:21 -070078GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
Hal Canary6f6961e2017-01-31 13:50:44 -050079#if GR_TEST_UTILS
Ethan Nicholas839872c2017-10-05 12:36:59 -040080std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(
81 GrProcessorTestData* data) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -050082 PMConversion pmConv = static_cast<PMConversion>(
83 data->fRandom->nextULessThan((int)PMConversion::kPMConversionCnt));
John Stilesb89e52e2020-07-07 14:25:50 -040084 return std::unique_ptr<GrFragmentProcessor>(
85 new GrConfigConversionEffect(GrProcessorUnitTest::MakeChildFP(data), pmConv));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000086}
Hal Canary6f6961e2017-01-31 13:50:44 -050087#endif
John Stilesb89e52e2020-07-07 14:25:50 -040088
Adlai Hollercf0d08e2020-08-11 12:36:10 +000089bool GrConfigConversionEffect::TestForPreservingPMConversions(GrDirectContext* context) {
John Stilesb89e52e2020-07-07 14:25:50 -040090 static constexpr int kSize = 256;
91 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
92 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
93 uint32_t* srcData = data.get();
94 uint32_t* firstRead = data.get() + kSize * kSize;
95 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
96
97 // Fill with every possible premultiplied A, color channel value. There will be 256-y
98 // duplicate values in row y. We set r, g, and b to the same value since they are handled
99 // identically.
100 for (int y = 0; y < kSize; ++y) {
101 for (int x = 0; x < kSize; ++x) {
102 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]);
103 color[3] = y;
104 color[2] = std::min(x, y);
105 color[1] = std::min(x, y);
106 color[0] = std::min(x, y);
107 }
108 }
109 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
110 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
111
112 const SkImageInfo ii =
113 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
114
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000115 auto readRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact,
John Stilesb89e52e2020-07-07 14:25:50 -0400116 {kSize, kSize});
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000117 auto tempRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact,
John Stilesb89e52e2020-07-07 14:25:50 -0400118 {kSize, kSize});
119 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
120 return false;
121 }
122 // Adding discard to appease vulkan validation warning about loading uninitialized data on
123 // draw
124 readRTC->discard();
125
126 // This function is only ever called if we are in a GrContext that has a GrGpu since we are
127 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
128 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
129 SkBitmap bitmap;
130 bitmap.installPixels(ii, srcData, 4 * kSize);
131 bitmap.setImmutable();
132
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000133 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400134 auto dataView = maker.view(GrMipmapped::kNo);
John Stilesb89e52e2020-07-07 14:25:50 -0400135 if (!dataView) {
136 return false;
137 }
138
139 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
140
141 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
142 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
143 // We then verify that two reads produced the same values.
144
145 GrPaint paint1;
John Stiles5933d7d2020-07-21 12:28:35 -0400146 paint1.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400147 GrTextureEffect::Make(std::move(dataView), kPremul_SkAlphaType),
148 PMConversion::kToUnpremul));
149 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
150
151 readRTC->fillRectToRect(nullptr, std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect, kRect);
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000152 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
John Stilesb89e52e2020-07-07 14:25:50 -0400153 return false;
154 }
155
156 // Adding discard to appease vulkan validation warning about loading uninitialized data on
157 // draw
158 tempRTC->discard();
159
160 GrPaint paint2;
John Stiles5933d7d2020-07-21 12:28:35 -0400161 paint2.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400162 GrTextureEffect::Make(readRTC->readSurfaceView(), kUnpremul_SkAlphaType),
163 PMConversion::kToPremul));
164 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
165
166 tempRTC->fillRectToRect(nullptr, std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect, kRect);
167
168 GrPaint paint3;
John Stiles5933d7d2020-07-21 12:28:35 -0400169 paint3.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400170 GrTextureEffect::Make(tempRTC->readSurfaceView(), kPremul_SkAlphaType),
171 PMConversion::kToUnpremul));
172 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
173
174 readRTC->fillRectToRect(nullptr, std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect, kRect);
175
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000176 if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
John Stilesb89e52e2020-07-07 14:25:50 -0400177 return false;
178 }
179
180 for (int y = 0; y < kSize; ++y) {
181 for (int x = 0; x <= y; ++x) {
182 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
183 return false;
184 }
185 }
186 }
187
188 return true;
189}