blob: f855454aba01c867617b9d85c153341d38027c6a [file] [log] [blame]
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001/*
Ethan Nicholas130fb3f2018-02-01 12:14:34 -05002 * Copyright 2018 Google Inc.
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00003 *
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 Nicholas130fb3f2018-02-01 12:14:34 -05008/**************************************************************************************************
9 *** This file was autogenerated from GrConfigConversionEffect.fp; do not modify.
10 **************************************************************************************************/
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000011#ifndef GrConfigConversionEffect_DEFINED
12#define GrConfigConversionEffect_DEFINED
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkTypes.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/GrContext.h"
16#include "src/gpu/GrClip.h"
17#include "src/gpu/GrContextPriv.h"
18#include "src/gpu/GrProxyProvider.h"
19#include "src/gpu/GrRenderTargetContext.h"
20
21#include "src/gpu/GrCoordTransform.h"
22#include "src/gpu/GrFragmentProcessor.h"
Brian Osmanee805322017-04-05 10:09:00 -040023class GrConfigConversionEffect : public GrFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000024public:
Ethan Nicholas839872c2017-10-05 12:36:59 -040025 static bool TestForPreservingPMConversions(GrContext* context) {
Mike Kleind6ab77a2019-03-21 08:18:24 -050026 static constexpr int kSize = 256;
27 static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -040028 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
29 const GrBackendFormat format = context->priv().caps()->getBackendFormatFromGrColorType(
30 kColorType, GrSRGBEncoded::kNo);
Ethan Nicholas839872c2017-10-05 12:36:59 -040031 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
Mike Kleind6ab77a2019-03-21 08:18:24 -050032 uint32_t* srcData = data.get();
33 uint32_t* firstRead = data.get() + kSize * kSize;
34 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
Robert Phillips757914d2017-01-25 15:48:30 -050035
Ethan Nicholas839872c2017-10-05 12:36:59 -040036 // Fill with every possible premultiplied A, color channel value. There will be 256-y
37 // duplicate values in row y. We set r, g, and b to the same value since they are handled
38 // identically.
39 for (int y = 0; y < kSize; ++y) {
40 for (int x = 0; x < kSize; ++x) {
41 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]);
Mike Kleind6ab77a2019-03-21 08:18:24 -050042 color[3] = y;
43 color[2] = SkTMin(x, y);
44 color[1] = SkTMin(x, y);
45 color[0] = SkTMin(x, y);
Ethan Nicholas839872c2017-10-05 12:36:59 -040046 }
47 }
Brian Salomon7b9b3262018-04-27 17:38:06 -040048 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
49 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000050
Ethan Nicholas839872c2017-10-05 12:36:59 -040051 const SkImageInfo ii =
52 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
53
Robert Phillips9da87e02019-02-04 13:26:26 -050054 sk_sp<GrRenderTargetContext> readRTC(context->priv().makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -040055 format, SkBackingFit::kExact, kSize, kSize, kConfig, kColorType, nullptr));
Robert Phillips9da87e02019-02-04 13:26:26 -050056 sk_sp<GrRenderTargetContext> tempRTC(context->priv().makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -040057 format, SkBackingFit::kExact, kSize, kSize, kConfig, kColorType, nullptr));
Ethan Nicholas839872c2017-10-05 12:36:59 -040058 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
59 return false;
60 }
Greg Danielf44cb482018-02-27 14:26:32 -050061 // Adding discard to appease vulkan validation warning about loading uninitialized data on
62 // draw
63 readRTC->discard();
64
Robert Phillips9da87e02019-02-04 13:26:26 -050065 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips579f0942018-01-08 14:53:35 -050066
Greg Daniel6e9a4f62018-03-15 17:00:06 -040067 SkPixmap pixmap(ii, srcData, 4 * kSize);
68
69 // This function is only ever called if we are in a GrContext that has a GrGpu since we are
70 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
71 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
72 sk_sp<SkImage> image = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
73
74 sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy(
75 std::move(image), kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact);
Ethan Nicholas839872c2017-10-05 12:36:59 -040076 if (!dataProxy) {
77 return false;
78 }
79
80 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
81
82 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
83 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
84 // We then verify that two reads produced the same values.
85
Mike Kleind6ab77a2019-03-21 08:18:24 -050086 GrPaint paint1;
87 GrPaint paint2;
88 GrPaint paint3;
Ethan Nicholas839872c2017-10-05 12:36:59 -040089 std::unique_ptr<GrFragmentProcessor> pmToUPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050090 new GrConfigConversionEffect(PMConversion::kToUnpremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040091 std::unique_ptr<GrFragmentProcessor> upmToPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050092 new GrConfigConversionEffect(PMConversion::kToPremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040093
Brian Osman2240be92017-10-18 13:15:13 -040094 paint1.addColorTextureProcessor(dataProxy, SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -040095 paint1.addColorFragmentProcessor(pmToUPM->clone());
96 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
97
Mike Kleind6ab77a2019-03-21 08:18:24 -050098 readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect,
99 kRect);
Brian Salomon1d435302019-07-01 13:05:28 -0400100 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -0400101 return false;
102 }
103
Greg Danielf44cb482018-02-27 14:26:32 -0500104 // Adding discard to appease vulkan validation warning about loading uninitialized data on
105 // draw
106 tempRTC->discard();
107
Brian Osman2240be92017-10-18 13:15:13 -0400108 paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400109 paint2.addColorFragmentProcessor(std::move(upmToPM));
110 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
111
Mike Kleind6ab77a2019-03-21 08:18:24 -0500112 tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect,
113 kRect);
Ethan Nicholas839872c2017-10-05 12:36:59 -0400114
Brian Osman2240be92017-10-18 13:15:13 -0400115 paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400116 paint3.addColorFragmentProcessor(std::move(pmToUPM));
117 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
118
Mike Kleind6ab77a2019-03-21 08:18:24 -0500119 readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect,
120 kRect);
Ethan Nicholas839872c2017-10-05 12:36:59 -0400121
Brian Salomon1d435302019-07-01 13:05:28 -0400122 if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -0400123 return false;
124 }
125
126 for (int y = 0; y < kSize; ++y) {
127 for (int x = 0; x <= y; ++x) {
128 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
129 return false;
130 }
131 }
132 }
133
134 return true;
135 }
Ethan Nicholas839872c2017-10-05 12:36:59 -0400136
137 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
138 PMConversion pmConversion) {
139 if (!fp) {
140 return nullptr;
141 }
142 std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion));
143 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {std::move(fp), std::move(ccFP)};
144 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
145 }
146 GrConfigConversionEffect(const GrConfigConversionEffect& src);
Brian Salomonaff329b2017-08-11 09:40:37 -0400147 std::unique_ptr<GrFragmentProcessor> clone() const override;
Mike Kleind6ab77a2019-03-21 08:18:24 -0500148 const char* name() const override { return "ConfigConversionEffect"; }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400149 PMConversion pmConversion;
Brian Osman28804f32017-04-20 10:24:36 -0400150
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000151private:
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500152 GrConfigConversionEffect(PMConversion pmConversion)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400153 : INHERITED(kGrConfigConversionEffect_ClassID, kNone_OptimizationFlags)
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400154 , pmConversion(pmConversion) {}
egdaniel57d3b032015-11-13 11:57:27 -0800155 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
Brian Salomon94efbf52016-11-29 13:43:05 -0500156 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700157 bool onIsEqual(const GrFragmentProcessor&) const override;
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400158 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Brian Osmanee805322017-04-05 10:09:00 -0400159 typedef GrFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000160};
Ethan Nicholas839872c2017-10-05 12:36:59 -0400161#endif