blob: 95df89ea7e0313463d2c302642158ad91935448e [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;
Brian Salomond6287472019-06-24 15:50:07 -040027 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
Ethan Nicholas839872c2017-10-05 12:36:59 -040028 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
Mike Kleind6ab77a2019-03-21 08:18:24 -050029 uint32_t* srcData = data.get();
30 uint32_t* firstRead = data.get() + kSize * kSize;
31 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
Robert Phillips757914d2017-01-25 15:48:30 -050032
Ethan Nicholas839872c2017-10-05 12:36:59 -040033 // Fill with every possible premultiplied A, color channel value. There will be 256-y
34 // duplicate values in row y. We set r, g, and b to the same value since they are handled
35 // identically.
36 for (int y = 0; y < kSize; ++y) {
37 for (int x = 0; x < kSize; ++x) {
38 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]);
Mike Kleind6ab77a2019-03-21 08:18:24 -050039 color[3] = y;
40 color[2] = SkTMin(x, y);
41 color[1] = SkTMin(x, y);
42 color[0] = SkTMin(x, y);
Ethan Nicholas839872c2017-10-05 12:36:59 -040043 }
44 }
Brian Salomon7b9b3262018-04-27 17:38:06 -040045 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
46 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000047
Ethan Nicholas839872c2017-10-05 12:36:59 -040048 const SkImageInfo ii =
49 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
50
Brian Salomonbf6b9792019-08-21 09:38:10 -040051 auto readRTC = context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, kSize,
52 kSize, kColorType, nullptr);
53 auto tempRTC = context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, kSize,
54 kSize, kColorType, nullptr);
Ethan Nicholas839872c2017-10-05 12:36:59 -040055 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
56 return false;
57 }
Greg Danielf44cb482018-02-27 14:26:32 -050058 // Adding discard to appease vulkan validation warning about loading uninitialized data on
59 // draw
60 readRTC->discard();
61
Robert Phillips9da87e02019-02-04 13:26:26 -050062 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips579f0942018-01-08 14:53:35 -050063
Greg Daniel6e9a4f62018-03-15 17:00:06 -040064 SkPixmap pixmap(ii, srcData, 4 * kSize);
65
66 // This function is only ever called if we are in a GrContext that has a GrGpu since we are
67 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
68 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
69 sk_sp<SkImage> image = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
70
71 sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy(
Brian Salomon96b383a2019-08-13 16:55:41 -040072 std::move(image), 1, SkBudgeted::kYes, SkBackingFit::kExact);
Ethan Nicholas839872c2017-10-05 12:36:59 -040073 if (!dataProxy) {
74 return false;
75 }
76
77 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
78
79 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
80 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
81 // We then verify that two reads produced the same values.
82
Mike Kleind6ab77a2019-03-21 08:18:24 -050083 GrPaint paint1;
84 GrPaint paint2;
85 GrPaint paint3;
Ethan Nicholas839872c2017-10-05 12:36:59 -040086 std::unique_ptr<GrFragmentProcessor> pmToUPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050087 new GrConfigConversionEffect(PMConversion::kToUnpremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040088 std::unique_ptr<GrFragmentProcessor> upmToPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050089 new GrConfigConversionEffect(PMConversion::kToPremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040090
Brian Osman2240be92017-10-18 13:15:13 -040091 paint1.addColorTextureProcessor(dataProxy, SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -040092 paint1.addColorFragmentProcessor(pmToUPM->clone());
93 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
94
Mike Kleind6ab77a2019-03-21 08:18:24 -050095 readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect,
96 kRect);
Brian Salomon1d435302019-07-01 13:05:28 -040097 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -040098 return false;
99 }
100
Greg Danielf44cb482018-02-27 14:26:32 -0500101 // Adding discard to appease vulkan validation warning about loading uninitialized data on
102 // draw
103 tempRTC->discard();
104
Brian Osman2240be92017-10-18 13:15:13 -0400105 paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400106 paint2.addColorFragmentProcessor(std::move(upmToPM));
107 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
108
Mike Kleind6ab77a2019-03-21 08:18:24 -0500109 tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect,
110 kRect);
Ethan Nicholas839872c2017-10-05 12:36:59 -0400111
Brian Osman2240be92017-10-18 13:15:13 -0400112 paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400113 paint3.addColorFragmentProcessor(std::move(pmToUPM));
114 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
115
Mike Kleind6ab77a2019-03-21 08:18:24 -0500116 readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect,
117 kRect);
Ethan Nicholas839872c2017-10-05 12:36:59 -0400118
Brian Salomon1d435302019-07-01 13:05:28 -0400119 if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -0400120 return false;
121 }
122
123 for (int y = 0; y < kSize; ++y) {
124 for (int x = 0; x <= y; ++x) {
125 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
126 return false;
127 }
128 }
129 }
130
131 return true;
132 }
Ethan Nicholas839872c2017-10-05 12:36:59 -0400133
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 Salomonaff329b2017-08-11 09:40:37 -0400144 std::unique_ptr<GrFragmentProcessor> clone() const override;
Mike Kleind6ab77a2019-03-21 08:18:24 -0500145 const char* name() const override { return "ConfigConversionEffect"; }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400146 PMConversion pmConversion;
Brian Osman28804f32017-04-20 10:24:36 -0400147
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000148private:
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500149 GrConfigConversionEffect(PMConversion pmConversion)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400150 : INHERITED(kGrConfigConversionEffect_ClassID, kNone_OptimizationFlags)
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400151 , pmConversion(pmConversion) {}
egdaniel57d3b032015-11-13 11:57:27 -0800152 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
Brian Salomon94efbf52016-11-29 13:43:05 -0500153 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700154 bool onIsEqual(const GrFragmentProcessor&) const override;
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400155 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Brian Osmanee805322017-04-05 10:09:00 -0400156 typedef GrFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000157};
Ethan Nicholas839872c2017-10-05 12:36:59 -0400158#endif