blob: 0387c3b39ccd69a80c3f80362af05a679fa14aa8 [file] [log] [blame]
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05001/*
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 Nicholas839872c2017-10-05 12:36:59 -04008@header {
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009 #include "include/gpu/GrContext.h"
10 #include "src/gpu/GrClip.h"
11 #include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040012 #include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013 #include "src/gpu/GrProxyProvider.h"
14 #include "src/gpu/GrRenderTargetContext.h"
Ethan Nicholas839872c2017-10-05 12:36:59 -040015}
16
17@class {
Ethan Nicholas839872c2017-10-05 12:36:59 -040018 static bool TestForPreservingPMConversions(GrContext* context) {
19 static constexpr int kSize = 256;
Brian Salomond6287472019-06-24 15:50:07 -040020 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
Ethan Nicholas839872c2017-10-05 12:36:59 -040021 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 Osman7a9dd392018-04-30 14:17:14 -040038 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
39 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
Ethan Nicholas839872c2017-10-05 12:36:59 -040040
41 const SkImageInfo ii = SkImageInfo::Make(kSize, kSize,
42 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
43
Brian Salomonbf6b9792019-08-21 09:38:10 -040044 auto readRTC =
Brian Salomon27ae52c2019-07-03 11:27:44 -040045 context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
Robert Phillips9da87e02019-02-04 13:26:26 -050046 kSize, kSize,
Brian Salomonbf6b9792019-08-21 09:38:10 -040047 kColorType, nullptr);
48 auto tempRTC =
Brian Salomon27ae52c2019-07-03 11:27:44 -040049 context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
Robert Phillips9da87e02019-02-04 13:26:26 -050050 kSize, kSize,
Brian Salomonbf6b9792019-08-21 09:38:10 -040051 kColorType, nullptr);
Ethan Nicholas839872c2017-10-05 12:36:59 -040052 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
53 return false;
54 }
Greg Danielf44cb482018-02-27 14:26:32 -050055 // Adding discard to appease vulkan validation warning about loading uninitialized data on
56 // draw
57 readRTC->discard();
58
Robert Phillips9da87e02019-02-04 13:26:26 -050059 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips579f0942018-01-08 14:53:35 -050060
Greg Daniel6e9a4f62018-03-15 17:00:06 -040061 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 Phillips50299de2019-11-23 15:20:57 +000067 GrColorType dataColorType = SkColorTypeToGrColorType(image->colorType());
Greg Daniel6e9a4f62018-03-15 17:00:06 -040068 sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy(std::move(image),
Greg Daniel6e9a4f62018-03-15 17:00:06 -040069 1,
70 SkBudgeted::kYes,
71 SkBackingFit::kExact);
Ethan Nicholas839872c2017-10-05 12:36:59 -040072 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 Nicholasaae47c82017-11-10 15:34:03 -050086 new GrConfigConversionEffect(PMConversion::kToUnpremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040087 std::unique_ptr<GrFragmentProcessor> upmToPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050088 new GrConfigConversionEffect(PMConversion::kToPremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040089
Robert Phillips50299de2019-11-23 15:20:57 +000090 paint1.addColorTextureProcessor(dataProxy, dataColorType, SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -040091 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 Salomon1d435302019-07-01 13:05:28 -040096 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -040097 return false;
98 }
99
Greg Danielf44cb482018-02-27 14:26:32 -0500100 // Adding discard to appease vulkan validation warning about loading uninitialized data on
101 // draw
102 tempRTC->discard();
103
Robert Phillips50299de2019-11-23 15:20:57 +0000104 paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), kColorType, SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400105 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 Phillips50299de2019-11-23 15:20:57 +0000111 paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), kColorType, SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400112 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 Salomon1d435302019-07-01 13:05:28 -0400118 if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -0400119 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 Nicholasaae47c82017-11-10 15:34:03 -0500146layout(key) in PMConversion pmConversion;
Ethan Nicholas839872c2017-10-05 12:36:59 -0400147
148@emitCode {
149 fragBuilder->forceHighPrecision();
150}
151
152void 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 Nicholasaae47c82017-11-10 15:34:03 -0500158 case PMConversion::kToPremul:
Ethan Nicholas839872c2017-10-05 12:36:59 -0400159 sk_OutColor.rgb = floor(sk_OutColor.rgb * sk_OutColor.a * 255 + 0.5) / 255;
160 break;
161
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500162 case PMConversion::kToUnpremul:
Ethan Nicholas839872c2017-10-05 12:36:59 -0400163 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 Nicholasaae47c82017-11-10 15:34:03 -0500171 PMConversion pmConv = static_cast<PMConversion>(data->fRandom->nextULessThan(
172 (int) PMConversion::kPMConversionCnt));
Ethan Nicholas839872c2017-10-05 12:36:59 -0400173 return std::unique_ptr<GrFragmentProcessor>(new GrConfigConversionEffect(pmConv));
174}