blob: 2297f7ea6d9b4dfb49a4d60b675c642f0d4e7adc [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
Greg Daniel456f9b52020-03-05 19:14:18 +000013#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
15#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
17#include "src/sksl/SkSLCPP.h"
18#include "src/sksl/SkSLUtil.h"
Ethan Nicholas839872c2017-10-05 12:36:59 -040019class GrGLSLConfigConversionEffect : public GrGLSLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000020public:
Ethan Nicholas839872c2017-10-05 12:36:59 -040021 GrGLSLConfigConversionEffect() {}
robertphillips9cdb9922016-02-03 12:25:40 -080022 void emitCode(EmitArgs& args) override {
Mike Kleind6ab77a2019-03-21 08:18:24 -050023 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24 const GrConfigConversionEffect& _outer = args.fFp.cast<GrConfigConversionEffect>();
Ethan Nicholas839872c2017-10-05 12:36:59 -040025 (void)_outer;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040026 auto pmConversion = _outer.pmConversion;
Ethan Nicholas82399462017-10-16 12:35:44 -040027 (void)pmConversion;
joshualitt30ba4362014-08-21 20:18:45 -070028
Ethan Nicholasf7b88202017-09-18 14:10:39 -040029 fragBuilder->forceHighPrecision();
Robert Phillips4e105e22020-07-16 09:18:50 -040030 SkString _sample5748 = this->invokeChild(0, args);
Ethan Nicholas839872c2017-10-05 12:36:59 -040031 fragBuilder->codeAppendf(
John Stiles50819422020-06-18 13:00:38 -040032 R"SkSL(%s = floor(%s * 255.0 + 0.5) / 255.0;
33@switch (%d) {
34 case 0:
35 %s.xyz = floor((%s.xyz * %s.w) * 255.0 + 0.5) / 255.0;
36 break;
37 case 1:
38 %s.xyz = %s.w <= 0.0 ? half3(0.0) : floor((%s.xyz / %s.w) * 255.0 + 0.5) / 255.0;
39 break;
40}
41)SkSL",
Robert Phillips4e105e22020-07-16 09:18:50 -040042 args.fOutputColor, _sample5748.c_str(), (int)_outer.pmConversion, args.fOutputColor,
Mike Kleind6ab77a2019-03-21 08:18:24 -050043 args.fOutputColor, args.fOutputColor, args.fOutputColor, args.fOutputColor,
44 args.fOutputColor, args.fOutputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000045 }
46
47private:
Ethan Nicholas839872c2017-10-05 12:36:59 -040048 void onSetData(const GrGLSLProgramDataManager& pdman,
Mike Kleind6ab77a2019-03-21 08:18:24 -050049 const GrFragmentProcessor& _proc) override {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000050};
Ethan Nicholas839872c2017-10-05 12:36:59 -040051GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const {
52 return new GrGLSLConfigConversionEffect();
53}
Mike Kleind6ab77a2019-03-21 08:18:24 -050054void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
Ethan Nicholas839872c2017-10-05 12:36:59 -040055 GrProcessorKeyBuilder* b) const {
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040056 b->add32((int32_t)pmConversion);
Ethan Nicholas839872c2017-10-05 12:36:59 -040057}
58bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& other) const {
59 const GrConfigConversionEffect& that = other.cast<GrConfigConversionEffect>();
60 (void)that;
Ethan Nicholasbcd51e82019-04-09 10:40:41 -040061 if (pmConversion != that.pmConversion) return false;
Ethan Nicholas839872c2017-10-05 12:36:59 -040062 return true;
63}
64GrConfigConversionEffect::GrConfigConversionEffect(const GrConfigConversionEffect& src)
Ethan Nicholasabff9562017-10-09 10:54:08 -040065 : INHERITED(kGrConfigConversionEffect_ClassID, src.optimizationFlags())
John Stilesb89e52e2020-07-07 14:25:50 -040066 , pmConversion(src.pmConversion) {
Brian Osman12c5d292020-07-13 16:11:35 -040067 this->cloneAndRegisterAllChildProcessors(src);
John Stilesb89e52e2020-07-07 14:25:50 -040068}
Brian Salomonaff329b2017-08-11 09:40:37 -040069std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::clone() const {
Ethan Nicholas839872c2017-10-05 12:36:59 -040070 return std::unique_ptr<GrFragmentProcessor>(new GrConfigConversionEffect(*this));
Brian Salomonfcc527b2017-07-26 12:21:21 -040071}
joshualittb0a8a372014-09-23 09:50:21 -070072GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
Hal Canary6f6961e2017-01-31 13:50:44 -050073#if GR_TEST_UTILS
Ethan Nicholas839872c2017-10-05 12:36:59 -040074std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(
75 GrProcessorTestData* data) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -050076 PMConversion pmConv = static_cast<PMConversion>(
77 data->fRandom->nextULessThan((int)PMConversion::kPMConversionCnt));
John Stilesb89e52e2020-07-07 14:25:50 -040078 return std::unique_ptr<GrFragmentProcessor>(
79 new GrConfigConversionEffect(GrProcessorUnitTest::MakeChildFP(data), pmConv));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000080}
Hal Canary6f6961e2017-01-31 13:50:44 -050081#endif
John Stilesb89e52e2020-07-07 14:25:50 -040082
Robert Phillips4e105e22020-07-16 09:18:50 -040083bool GrConfigConversionEffect::TestForPreservingPMConversions(GrDirectContext* context) {
John Stilesb89e52e2020-07-07 14:25:50 -040084 static constexpr int kSize = 256;
85 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
86 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
87 uint32_t* srcData = data.get();
88 uint32_t* firstRead = data.get() + kSize * kSize;
89 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
90
91 // Fill with every possible premultiplied A, color channel value. There will be 256-y
92 // duplicate values in row y. We set r, g, and b to the same value since they are handled
93 // identically.
94 for (int y = 0; y < kSize; ++y) {
95 for (int x = 0; x < kSize; ++x) {
96 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]);
97 color[3] = y;
98 color[2] = std::min(x, y);
99 color[1] = std::min(x, y);
100 color[0] = std::min(x, y);
101 }
102 }
103 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
104 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
105
106 const SkImageInfo ii =
107 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
108
109 auto readRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact,
110 {kSize, kSize});
111 auto tempRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact,
112 {kSize, kSize});
113 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
114 return false;
115 }
116 // Adding discard to appease vulkan validation warning about loading uninitialized data on
117 // draw
118 readRTC->discard();
119
120 // This function is only ever called if we are in a GrContext that has a GrGpu since we are
121 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
122 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
123 SkBitmap bitmap;
124 bitmap.installPixels(ii, srcData, 4 * kSize);
125 bitmap.setImmutable();
126
127 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400128 auto dataView = maker.view(GrMipmapped::kNo);
John Stilesb89e52e2020-07-07 14:25:50 -0400129 if (!dataView) {
130 return false;
131 }
132
133 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
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 GrPaint paint1;
140 paint1.addColorFragmentProcessor(GrConfigConversionEffect::Make(
141 GrTextureEffect::Make(std::move(dataView), kPremul_SkAlphaType),
142 PMConversion::kToUnpremul));
143 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
144
145 readRTC->fillRectToRect(nullptr, std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect, kRect);
146 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
147 return false;
148 }
149
150 // Adding discard to appease vulkan validation warning about loading uninitialized data on
151 // draw
152 tempRTC->discard();
153
154 GrPaint paint2;
155 paint2.addColorFragmentProcessor(GrConfigConversionEffect::Make(
156 GrTextureEffect::Make(readRTC->readSurfaceView(), kUnpremul_SkAlphaType),
157 PMConversion::kToPremul));
158 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
159
160 tempRTC->fillRectToRect(nullptr, std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect, kRect);
161
162 GrPaint paint3;
163 paint3.addColorFragmentProcessor(GrConfigConversionEffect::Make(
164 GrTextureEffect::Make(tempRTC->readSurfaceView(), kPremul_SkAlphaType),
165 PMConversion::kToUnpremul));
166 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
167
168 readRTC->fillRectToRect(nullptr, std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect, kRect);
169
170 if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
171 return false;
172 }
173
174 for (int y = 0; y < kSize; ++y) {
175 for (int x = 0; x <= y; ++x) {
176 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
177 return false;
178 }
179 }
180 }
181
182 return true;
183}