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 8@header { 9 #include "GrClip.h" 10 #include "GrContext.h" 11 #include "GrContextPriv.h" 12 #include "GrProxyProvider.h" 13 #include "GrRenderTargetContext.h" 14} 15 16@class { 17 static bool TestForPreservingPMConversions(GrContext* context) { 18 static constexpr int kSize = 256; 19 static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig; 20 static constexpr SkColorType kColorType = kRGBA_8888_SkColorType; 21 const GrBackendFormat format = 22 context->priv().caps()->getBackendFormatFromColorType(kColorType); 23 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3); 24 uint32_t* srcData = data.get(); 25 uint32_t* firstRead = data.get() + kSize * kSize; 26 uint32_t* secondRead = data.get() + 2 * kSize * kSize; 27 28 // Fill with every possible premultiplied A, color channel value. There will be 256-y 29 // duplicate values in row y. We set r, g, and b to the same value since they are handled 30 // identically. 31 for (int y = 0; y < kSize; ++y) { 32 for (int x = 0; x < kSize; ++x) { 33 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]); 34 color[3] = y; 35 color[2] = SkTMin(x, y); 36 color[1] = SkTMin(x, y); 37 color[0] = SkTMin(x, y); 38 } 39 } 40 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t)); 41 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t)); 42 43 const SkImageInfo ii = SkImageInfo::Make(kSize, kSize, 44 kRGBA_8888_SkColorType, kPremul_SkAlphaType); 45 46 sk_sp<GrRenderTargetContext> readRTC( 47 context->priv().makeDeferredRenderTargetContext(format, SkBackingFit::kExact, 48 kSize, kSize, 49 kConfig, nullptr)); 50 sk_sp<GrRenderTargetContext> tempRTC( 51 context->priv().makeDeferredRenderTargetContext(format, SkBackingFit::kExact, 52 kSize, kSize, 53 kConfig, nullptr)); 54 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) { 55 return false; 56 } 57 // Adding discard to appease vulkan validation warning about loading uninitialized data on 58 // draw 59 readRTC->discard(); 60 61 GrProxyProvider* proxyProvider = context->priv().proxyProvider(); 62 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(std::move(image), 71 kNone_GrSurfaceFlags, 72 1, 73 SkBudgeted::kYes, 74 SkBackingFit::kExact); 75 if (!dataProxy) { 76 return false; 77 } 78 79 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize); 80 81 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw 82 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. 83 // We then verify that two reads produced the same values. 84 85 GrPaint paint1; 86 GrPaint paint2; 87 GrPaint paint3; 88 std::unique_ptr<GrFragmentProcessor> pmToUPM( 89 new GrConfigConversionEffect(PMConversion::kToUnpremul)); 90 std::unique_ptr<GrFragmentProcessor> upmToPM( 91 new GrConfigConversionEffect(PMConversion::kToPremul)); 92 93 paint1.addColorTextureProcessor(dataProxy, SkMatrix::I()); 94 paint1.addColorFragmentProcessor(pmToUPM->clone()); 95 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc); 96 97 readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect, 98 kRect); 99 if (!readRTC->readPixels(ii, firstRead, 0, 0, 0)) { 100 return false; 101 } 102 103 // Adding discard to appease vulkan validation warning about loading uninitialized data on 104 // draw 105 tempRTC->discard(); 106 107 paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), SkMatrix::I()); 108 paint2.addColorFragmentProcessor(std::move(upmToPM)); 109 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc); 110 111 tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect, 112 kRect); 113 114 paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), SkMatrix::I()); 115 paint3.addColorFragmentProcessor(std::move(pmToUPM)); 116 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc); 117 118 readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect, 119 kRect); 120 121 if (!readRTC->readPixels(ii, secondRead, 0, 0, 0)) { 122 return false; 123 } 124 125 for (int y = 0; y < kSize; ++y) { 126 for (int x = 0; x <= y; ++x) { 127 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) { 128 return false; 129 } 130 } 131 } 132 133 return true; 134 } 135} 136 137@make { 138 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp, 139 PMConversion pmConversion) { 140 if (!fp) { 141 return nullptr; 142 } 143 std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion)); 144 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), std::move(ccFP) }; 145 return GrFragmentProcessor::RunInSeries(fpPipeline, 2); 146 } 147} 148 149layout(key) in PMConversion pmConversion; 150 151@emitCode { 152 fragBuilder->forceHighPrecision(); 153} 154 155void main() { 156 // Aggressively round to the nearest exact (N / 255) floating point value. This lets us find a 157 // round-trip preserving pair on some GPUs that do odd byte to float conversion. 158 sk_OutColor = floor(sk_InColor * 255 + 0.5) / 255; 159 160 @switch (pmConversion) { 161 case PMConversion::kToPremul: 162 sk_OutColor.rgb = floor(sk_OutColor.rgb * sk_OutColor.a * 255 + 0.5) / 255; 163 break; 164 165 case PMConversion::kToUnpremul: 166 sk_OutColor.rgb = sk_OutColor.a <= 0.0 ? 167 half3(0) : 168 floor(sk_OutColor.rgb / sk_OutColor.a * 255 + 0.5) / 255; 169 break; 170 } 171} 172 173@test(data) { 174 PMConversion pmConv = static_cast<PMConversion>(data->fRandom->nextULessThan( 175 (int) PMConversion::kPMConversionCnt)); 176 return std::unique_ptr<GrFragmentProcessor>(new GrConfigConversionEffect(pmConv)); 177} 178