blob: 4e38451d03f4813789e824e4c316665189baf96e [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}
joshualittb0a8a372014-09-23 09:50:21 -070073GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
Hal Canary6f6961e2017-01-31 13:50:44 -050074#if GR_TEST_UTILS
Ethan Nicholas839872c2017-10-05 12:36:59 -040075std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(
76 GrProcessorTestData* data) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -050077 PMConversion pmConv = static_cast<PMConversion>(
78 data->fRandom->nextULessThan((int)PMConversion::kPMConversionCnt));
John Stilesb89e52e2020-07-07 14:25:50 -040079 return std::unique_ptr<GrFragmentProcessor>(
80 new GrConfigConversionEffect(GrProcessorUnitTest::MakeChildFP(data), pmConv));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000081}
Hal Canary6f6961e2017-01-31 13:50:44 -050082#endif
John Stilesb89e52e2020-07-07 14:25:50 -040083
Adlai Hollercf0d08e2020-08-11 12:36:10 +000084bool GrConfigConversionEffect::TestForPreservingPMConversions(GrDirectContext* context) {
John Stilesb89e52e2020-07-07 14:25:50 -040085 static constexpr int kSize = 256;
86 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
87 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
88 uint32_t* srcData = data.get();
89 uint32_t* firstRead = data.get() + kSize * kSize;
90 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
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 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
105 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
106
107 const SkImageInfo ii =
108 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
109
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000110 auto readRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact,
John Stilesb89e52e2020-07-07 14:25:50 -0400111 {kSize, kSize});
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000112 auto tempRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact,
John Stilesb89e52e2020-07-07 14:25:50 -0400113 {kSize, kSize});
114 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
115 return false;
116 }
117 // Adding discard to appease vulkan validation warning about loading uninitialized data on
118 // draw
119 readRTC->discard();
120
121 // This function is only ever called if we are in a GrContext that has a GrGpu since we are
122 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
123 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
124 SkBitmap bitmap;
125 bitmap.installPixels(ii, srcData, 4 * kSize);
126 bitmap.setImmutable();
127
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000128 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400129 auto dataView = maker.view(GrMipmapped::kNo);
John Stilesb89e52e2020-07-07 14:25:50 -0400130 if (!dataView) {
131 return false;
132 }
133
134 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
135
136 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
137 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
138 // We then verify that two reads produced the same values.
139
140 GrPaint paint1;
John Stiles5933d7d2020-07-21 12:28:35 -0400141 paint1.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400142 GrTextureEffect::Make(std::move(dataView), kPremul_SkAlphaType),
143 PMConversion::kToUnpremul));
144 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
145
146 readRTC->fillRectToRect(nullptr, std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect, kRect);
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000147 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
John Stilesb89e52e2020-07-07 14:25:50 -0400148 return false;
149 }
150
151 // Adding discard to appease vulkan validation warning about loading uninitialized data on
152 // draw
153 tempRTC->discard();
154
155 GrPaint paint2;
John Stiles5933d7d2020-07-21 12:28:35 -0400156 paint2.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400157 GrTextureEffect::Make(readRTC->readSurfaceView(), kUnpremul_SkAlphaType),
158 PMConversion::kToPremul));
159 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
160
161 tempRTC->fillRectToRect(nullptr, std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect, kRect);
162
163 GrPaint paint3;
John Stiles5933d7d2020-07-21 12:28:35 -0400164 paint3.setColorFragmentProcessor(GrConfigConversionEffect::Make(
John Stilesb89e52e2020-07-07 14:25:50 -0400165 GrTextureEffect::Make(tempRTC->readSurfaceView(), kPremul_SkAlphaType),
166 PMConversion::kToUnpremul));
167 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
168
169 readRTC->fillRectToRect(nullptr, std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect, kRect);
170
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000171 if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
John Stilesb89e52e2020-07-07 14:25:50 -0400172 return false;
173 }
174
175 for (int y = 0; y < kSize; ++y) {
176 for (int x = 0; x <= y; ++x) {
177 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
178 return false;
179 }
180 }
181 }
182
183 return true;
184}