bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 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 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 8 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrConfigConversionEffect.fp; do not modify. |
| 10 | **************************************************************************************************/ |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 11 | #include "GrConfigConversionEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | |
John Stiles | 45f5b03 | 2020-07-27 17:31:29 -0400 | [diff] [blame] | 13 | #include "src/core/SkUtils.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 14 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 20 | class GrGLSLConfigConversionEffect : public GrGLSLFragmentProcessor { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 21 | public: |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 22 | GrGLSLConfigConversionEffect() {} |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 23 | void emitCode(EmitArgs& args) override { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 24 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 25 | const GrConfigConversionEffect& _outer = args.fFp.cast<GrConfigConversionEffect>(); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 26 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 27 | auto pmConversion = _outer.pmConversion; |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 28 | (void)pmConversion; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 29 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 30 | fragBuilder->forceHighPrecision(); |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 31 | SkString _sample5748 = this->invokeChild(0, args); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 32 | fragBuilder->codeAppendf( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 33 | 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 Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 43 | args.fOutputColor, _sample5748.c_str(), (int)_outer.pmConversion, args.fOutputColor, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 44 | args.fOutputColor, args.fOutputColor, args.fOutputColor, args.fOutputColor, |
| 45 | args.fOutputColor, args.fOutputColor); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | private: |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 49 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 50 | const GrFragmentProcessor& _proc) override {} |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 51 | }; |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 52 | GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const { |
| 53 | return new GrGLSLConfigConversionEffect(); |
| 54 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 55 | void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 56 | GrProcessorKeyBuilder* b) const { |
John Stiles | 45f5b03 | 2020-07-27 17:31:29 -0400 | [diff] [blame] | 57 | b->add32((uint32_t)pmConversion); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 58 | } |
| 59 | bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 60 | const GrConfigConversionEffect& that = other.cast<GrConfigConversionEffect>(); |
| 61 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 62 | if (pmConversion != that.pmConversion) return false; |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | GrConfigConversionEffect::GrConfigConversionEffect(const GrConfigConversionEffect& src) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 66 | : INHERITED(kGrConfigConversionEffect_ClassID, src.optimizationFlags()) |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 67 | , pmConversion(src.pmConversion) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 68 | this->cloneAndRegisterAllChildProcessors(src); |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 69 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 70 | std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::clone() const { |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 71 | return std::make_unique<GrConfigConversionEffect>(*this); |
Brian Salomon | fcc527b | 2017-07-26 12:21:21 -0400 | [diff] [blame] | 72 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 73 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect); |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 74 | #if GR_TEST_UTILS |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 75 | std::unique_ptr<GrFragmentProcessor> GrConfigConversionEffect::TestCreate( |
| 76 | GrProcessorTestData* data) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 77 | PMConversion pmConv = static_cast<PMConversion>( |
| 78 | data->fRandom->nextULessThan((int)PMConversion::kPMConversionCnt)); |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 79 | return std::unique_ptr<GrFragmentProcessor>( |
| 80 | new GrConfigConversionEffect(GrProcessorUnitTest::MakeChildFP(data), pmConv)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 81 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 82 | #endif |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 83 | |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame] | 84 | bool GrConfigConversionEffect::TestForPreservingPMConversions(GrDirectContext* context) { |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 85 | 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 Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame] | 110 | auto readRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact, |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 111 | {kSize, kSize}); |
Adlai Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame] | 112 | auto tempRTC = GrRenderTargetContext::Make(context, kColorType, nullptr, SkBackingFit::kExact, |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 113 | {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 Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame] | 128 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 129 | auto dataView = maker.view(GrMipmapped::kNo); |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 130 | 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 Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 141 | paint1.setColorFragmentProcessor(GrConfigConversionEffect::Make( |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 142 | 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 Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame] | 147 | if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) { |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 148 | 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 Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 156 | paint2.setColorFragmentProcessor(GrConfigConversionEffect::Make( |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 157 | 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 Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 164 | paint3.setColorFragmentProcessor(GrConfigConversionEffect::Make( |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 165 | 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 Holler | cf0d08e | 2020-08-11 12:36:10 +0000 | [diff] [blame] | 171 | if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) { |
John Stiles | b89e52e | 2020-07-07 14:25:50 -0400 | [diff] [blame] | 172 | 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 | } |