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 | #ifndef GrConfigConversionEffect_DEFINED |
| 12 | #define GrConfigConversionEffect_DEFINED |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 13 | #include "SkTypes.h" |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 14 | |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 15 | #include "GrClip.h" |
| 16 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 17 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 18 | #include "GrProxyProvider.h" |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 19 | #include "GrRenderTargetContext.h" |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 20 | #include "GrFragmentProcessor.h" |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 21 | #include "GrCoordTransform.h" |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 22 | class GrConfigConversionEffect : public GrFragmentProcessor { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 23 | public: |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 24 | static bool TestForPreservingPMConversions(GrContext* context) { |
| 25 | static constexpr int kSize = 256; |
| 26 | static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig; |
| 27 | SkAutoTMalloc<uint32_t> data(kSize * kSize * 3); |
| 28 | uint32_t* srcData = data.get(); |
| 29 | uint32_t* firstRead = data.get() + kSize * kSize; |
| 30 | uint32_t* secondRead = data.get() + 2 * kSize * kSize; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 31 | |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 32 | // Fill with every possible premultiplied A, color channel value. There will be 256-y |
| 33 | // duplicate values in row y. We set r, g, and b to the same value since they are handled |
| 34 | // identically. |
| 35 | for (int y = 0; y < kSize; ++y) { |
| 36 | for (int x = 0; x < kSize; ++x) { |
| 37 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]); |
| 38 | color[3] = y; |
| 39 | color[2] = SkTMin(x, y); |
| 40 | color[1] = SkTMin(x, y); |
| 41 | color[0] = SkTMin(x, y); |
| 42 | } |
| 43 | } |
Brian Salomon | 7b9b326 | 2018-04-27 17:38:06 -0400 | [diff] [blame] | 44 | memset(firstRead, 0, kSize * kSize * sizeof(uint32_t)); |
| 45 | memset(secondRead, 0, kSize * kSize * sizeof(uint32_t)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 46 | |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 47 | const SkImageInfo ii = |
| 48 | SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 49 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 50 | sk_sp<GrRenderTargetContext> readRTC(context->contextPriv().makeDeferredRenderTargetContext( |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 51 | SkBackingFit::kExact, kSize, kSize, kConfig, nullptr)); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 52 | sk_sp<GrRenderTargetContext> tempRTC(context->contextPriv().makeDeferredRenderTargetContext( |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 53 | SkBackingFit::kExact, kSize, kSize, kConfig, nullptr)); |
| 54 | if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) { |
| 55 | return false; |
| 56 | } |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 57 | // Adding discard to appease vulkan validation warning about loading uninitialized data on |
| 58 | // draw |
| 59 | readRTC->discard(); |
| 60 | |
Robert Phillips | 579f094 | 2018-01-08 14:53:35 -0500 | [diff] [blame] | 61 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 62 | |
Greg Daniel | 6e9a4f6 | 2018-03-15 17:00:06 -0400 | [diff] [blame] | 63 | SkPixmap pixmap(ii, srcData, 4 * kSize); |
| 64 | |
| 65 | // This function is only ever called if we are in a GrContext that has a GrGpu since we are |
| 66 | // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't |
| 67 | // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr. |
| 68 | sk_sp<SkImage> image = SkImage::MakeFromRaster(pixmap, nullptr, nullptr); |
| 69 | |
| 70 | sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy( |
| 71 | std::move(image), kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 72 | if (!dataProxy) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | static const SkRect kRect = SkRect::MakeIWH(kSize, kSize); |
| 77 | |
| 78 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 79 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 80 | // We then verify that two reads produced the same values. |
| 81 | |
| 82 | GrPaint paint1; |
| 83 | GrPaint paint2; |
| 84 | GrPaint paint3; |
| 85 | std::unique_ptr<GrFragmentProcessor> pmToUPM( |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 86 | new GrConfigConversionEffect(PMConversion::kToUnpremul)); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 87 | std::unique_ptr<GrFragmentProcessor> upmToPM( |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 88 | new GrConfigConversionEffect(PMConversion::kToPremul)); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 89 | |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 90 | paint1.addColorTextureProcessor(dataProxy, SkMatrix::I()); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 91 | paint1.addColorFragmentProcessor(pmToUPM->clone()); |
| 92 | paint1.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 93 | |
| 94 | readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect, |
| 95 | kRect); |
| 96 | if (!readRTC->readPixels(ii, firstRead, 0, 0, 0)) { |
| 97 | return false; |
| 98 | } |
| 99 | |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 100 | // Adding discard to appease vulkan validation warning about loading uninitialized data on |
| 101 | // draw |
| 102 | tempRTC->discard(); |
| 103 | |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 104 | paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), SkMatrix::I()); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 105 | paint2.addColorFragmentProcessor(std::move(upmToPM)); |
| 106 | paint2.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 107 | |
| 108 | tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect, |
| 109 | kRect); |
| 110 | |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 111 | paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), SkMatrix::I()); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 112 | paint3.addColorFragmentProcessor(std::move(pmToUPM)); |
| 113 | paint3.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 114 | |
| 115 | readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect, |
| 116 | kRect); |
| 117 | |
| 118 | if (!readRTC->readPixels(ii, secondRead, 0, 0, 0)) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | for (int y = 0; y < kSize; ++y) { |
| 123 | for (int x = 0; x <= y; ++x) { |
| 124 | if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) { |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 132 | PMConversion pmConversion() const { return fPmConversion; } |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 133 | |
| 134 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 135 | PMConversion pmConversion) { |
| 136 | if (!fp) { |
| 137 | return nullptr; |
| 138 | } |
| 139 | std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion)); |
| 140 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {std::move(fp), std::move(ccFP)}; |
| 141 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 142 | } |
| 143 | GrConfigConversionEffect(const GrConfigConversionEffect& src); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 144 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 145 | const char* name() const override { return "ConfigConversionEffect"; } |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 146 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 147 | private: |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 148 | GrConfigConversionEffect(PMConversion pmConversion) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 149 | : INHERITED(kGrConfigConversionEffect_ClassID, kNone_OptimizationFlags) |
| 150 | , fPmConversion(pmConversion) {} |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 151 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 152 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 153 | bool onIsEqual(const GrFragmentProcessor&) const override; |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 154 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 155 | PMConversion fPmConversion; |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 156 | typedef GrFragmentProcessor INHERITED; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 157 | }; |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 158 | #endif |