Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 8 | @header { |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/gpu/GrContext.h" |
| 10 | #include "src/gpu/GrClip.h" |
| 11 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrProxyProvider.h" |
| 14 | #include "src/gpu/GrRenderTargetContext.h" |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | @class { |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 18 | static bool TestForPreservingPMConversions(GrContext* context) { |
| 19 | static constexpr int kSize = 256; |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 20 | static constexpr GrColorType kColorType = GrColorType::kRGBA_8888; |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 21 | SkAutoTMalloc<uint32_t> data(kSize * kSize * 3); |
| 22 | uint32_t* srcData = data.get(); |
| 23 | uint32_t* firstRead = data.get() + kSize * kSize; |
| 24 | uint32_t* secondRead = data.get() + 2 * kSize * kSize; |
| 25 | |
| 26 | // Fill with every possible premultiplied A, color channel value. There will be 256-y |
| 27 | // duplicate values in row y. We set r, g, and b to the same value since they are handled |
| 28 | // identically. |
| 29 | for (int y = 0; y < kSize; ++y) { |
| 30 | for (int x = 0; x < kSize; ++x) { |
| 31 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]); |
| 32 | color[3] = y; |
| 33 | color[2] = SkTMin(x, y); |
| 34 | color[1] = SkTMin(x, y); |
| 35 | color[0] = SkTMin(x, y); |
| 36 | } |
| 37 | } |
Brian Osman | 7a9dd39 | 2018-04-30 14:17:14 -0400 | [diff] [blame] | 38 | memset(firstRead, 0, kSize * kSize * sizeof(uint32_t)); |
| 39 | memset(secondRead, 0, kSize * kSize * sizeof(uint32_t)); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 40 | |
| 41 | const SkImageInfo ii = SkImageInfo::Make(kSize, kSize, |
| 42 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 43 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 44 | auto readRTC = |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 45 | context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 46 | kSize, kSize, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 47 | kColorType, nullptr); |
| 48 | auto tempRTC = |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 49 | context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 50 | kSize, kSize, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 51 | kColorType, nullptr); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 52 | if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) { |
| 53 | return false; |
| 54 | } |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 55 | // Adding discard to appease vulkan validation warning about loading uninitialized data on |
| 56 | // draw |
| 57 | readRTC->discard(); |
| 58 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 59 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 579f094 | 2018-01-08 14:53:35 -0500 | [diff] [blame] | 60 | |
Greg Daniel | 6e9a4f6 | 2018-03-15 17:00:06 -0400 | [diff] [blame] | 61 | SkPixmap pixmap(ii, srcData, 4 * kSize); |
| 62 | |
| 63 | // This function is only ever called if we are in a GrContext that has a GrGpu since we are |
| 64 | // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't |
| 65 | // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr. |
| 66 | sk_sp<SkImage> image = SkImage::MakeFromRaster(pixmap, nullptr, nullptr); |
Robert Phillips | 50299de | 2019-11-23 15:20:57 +0000 | [diff] [blame^] | 67 | GrColorType dataColorType = SkColorTypeToGrColorType(image->colorType()); |
Greg Daniel | 6e9a4f6 | 2018-03-15 17:00:06 -0400 | [diff] [blame] | 68 | sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy(std::move(image), |
Greg Daniel | 6e9a4f6 | 2018-03-15 17:00:06 -0400 | [diff] [blame] | 69 | 1, |
| 70 | SkBudgeted::kYes, |
| 71 | 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 | |
Robert Phillips | 50299de | 2019-11-23 15:20:57 +0000 | [diff] [blame^] | 90 | paint1.addColorTextureProcessor(dataProxy, dataColorType, 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); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 96 | if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) { |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 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 | |
Robert Phillips | 50299de | 2019-11-23 15:20:57 +0000 | [diff] [blame^] | 104 | paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), kColorType, 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 | |
Robert Phillips | 50299de | 2019-11-23 15:20:57 +0000 | [diff] [blame^] | 111 | paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), kColorType, 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 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 118 | if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) { |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 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 | } |
| 132 | } |
| 133 | |
| 134 | @make { |
| 135 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 136 | PMConversion pmConversion) { |
| 137 | if (!fp) { |
| 138 | return nullptr; |
| 139 | } |
| 140 | std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion)); |
| 141 | std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), std::move(ccFP) }; |
| 142 | return GrFragmentProcessor::RunInSeries(fpPipeline, 2); |
| 143 | } |
| 144 | } |
| 145 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 146 | layout(key) in PMConversion pmConversion; |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 147 | |
| 148 | @emitCode { |
| 149 | fragBuilder->forceHighPrecision(); |
| 150 | } |
| 151 | |
| 152 | void main() { |
| 153 | // Aggressively round to the nearest exact (N / 255) floating point value. This lets us find a |
| 154 | // round-trip preserving pair on some GPUs that do odd byte to float conversion. |
| 155 | sk_OutColor = floor(sk_InColor * 255 + 0.5) / 255; |
| 156 | |
| 157 | @switch (pmConversion) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 158 | case PMConversion::kToPremul: |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 159 | sk_OutColor.rgb = floor(sk_OutColor.rgb * sk_OutColor.a * 255 + 0.5) / 255; |
| 160 | break; |
| 161 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 162 | case PMConversion::kToUnpremul: |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 163 | sk_OutColor.rgb = sk_OutColor.a <= 0.0 ? |
| 164 | half3(0) : |
| 165 | floor(sk_OutColor.rgb / sk_OutColor.a * 255 + 0.5) / 255; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @test(data) { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 171 | PMConversion pmConv = static_cast<PMConversion>(data->fRandom->nextULessThan( |
| 172 | (int) PMConversion::kPMConversionCnt)); |
Ethan Nicholas | 839872c | 2017-10-05 12:36:59 -0400 | [diff] [blame] | 173 | return std::unique_ptr<GrFragmentProcessor>(new GrConfigConversionEffect(pmConv)); |
| 174 | } |