blob: 40bbc97832650d27abe2389e83378cc31f865457 [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
Ethan Nicholas839872c2017-10-05 12:36:59 -040013#include "SkTypes.h"
14#if SK_SUPPORT_GPU
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000015
Ethan Nicholas839872c2017-10-05 12:36:59 -040016#include "GrClip.h"
17#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050018#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050019#include "GrProxyProvider.h"
Ethan Nicholas839872c2017-10-05 12:36:59 -040020#include "GrRenderTargetContext.h"
Brian Osmanee805322017-04-05 10:09:00 -040021#include "GrFragmentProcessor.h"
Ethan Nicholas839872c2017-10-05 12:36:59 -040022#include "GrCoordTransform.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) {
26 static constexpr int kSize = 256;
27 static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
28 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
29 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]);
39 color[3] = y;
40 color[2] = SkTMin(x, y);
41 color[1] = SkTMin(x, y);
42 color[0] = SkTMin(x, y);
43 }
44 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000045
Ethan Nicholas839872c2017-10-05 12:36:59 -040046 const SkImageInfo ii =
47 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
48
49 sk_sp<GrRenderTargetContext> readRTC(context->makeDeferredRenderTargetContext(
50 SkBackingFit::kExact, kSize, kSize, kConfig, nullptr));
51 sk_sp<GrRenderTargetContext> tempRTC(context->makeDeferredRenderTargetContext(
52 SkBackingFit::kExact, kSize, kSize, kConfig, nullptr));
53 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
54 return false;
55 }
Greg Danielf44cb482018-02-27 14:26:32 -050056 // Adding discard to appease vulkan validation warning about loading uninitialized data on
57 // draw
58 readRTC->discard();
59
Ethan Nicholas839872c2017-10-05 12:36:59 -040060 GrSurfaceDesc desc;
Ethan Nicholas839872c2017-10-05 12:36:59 -040061 desc.fWidth = kSize;
62 desc.fHeight = kSize;
63 desc.fConfig = kConfig;
64
Robert Phillips579f0942018-01-08 14:53:35 -050065 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
66
Brian Salomon2a4f9832018-03-03 22:43:43 -050067 sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy(
68 desc, kTopLeft_GrSurfaceOrigin, SkBudgeted::kYes, data, 0);
Ethan Nicholas839872c2017-10-05 12:36:59 -040069 if (!dataProxy) {
70 return false;
71 }
72
73 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
74
75 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
76 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
77 // We then verify that two reads produced the same values.
78
79 GrPaint paint1;
80 GrPaint paint2;
81 GrPaint paint3;
82 std::unique_ptr<GrFragmentProcessor> pmToUPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050083 new GrConfigConversionEffect(PMConversion::kToUnpremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040084 std::unique_ptr<GrFragmentProcessor> upmToPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050085 new GrConfigConversionEffect(PMConversion::kToPremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040086
Brian Osman2240be92017-10-18 13:15:13 -040087 paint1.addColorTextureProcessor(dataProxy, SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -040088 paint1.addColorFragmentProcessor(pmToUPM->clone());
89 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
90
91 readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect,
92 kRect);
93 if (!readRTC->readPixels(ii, firstRead, 0, 0, 0)) {
94 return false;
95 }
96
Greg Danielf44cb482018-02-27 14:26:32 -050097 // Adding discard to appease vulkan validation warning about loading uninitialized data on
98 // draw
99 tempRTC->discard();
100
Brian Osman2240be92017-10-18 13:15:13 -0400101 paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400102 paint2.addColorFragmentProcessor(std::move(upmToPM));
103 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
104
105 tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect,
106 kRect);
107
Brian Osman2240be92017-10-18 13:15:13 -0400108 paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400109 paint3.addColorFragmentProcessor(std::move(pmToUPM));
110 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
111
112 readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect,
113 kRect);
114
115 if (!readRTC->readPixels(ii, secondRead, 0, 0, 0)) {
116 return false;
117 }
118
119 for (int y = 0; y < kSize; ++y) {
120 for (int x = 0; x <= y; ++x) {
121 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
122 return false;
123 }
124 }
125 }
126
127 return true;
128 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500129 PMConversion pmConversion() const { return fPmConversion; }
Ethan Nicholas839872c2017-10-05 12:36:59 -0400130
131 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
132 PMConversion pmConversion) {
133 if (!fp) {
134 return nullptr;
135 }
136 std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion));
137 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {std::move(fp), std::move(ccFP)};
138 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
139 }
140 GrConfigConversionEffect(const GrConfigConversionEffect& src);
Brian Salomonaff329b2017-08-11 09:40:37 -0400141 std::unique_ptr<GrFragmentProcessor> clone() const override;
Ethan Nicholas839872c2017-10-05 12:36:59 -0400142 const char* name() const override { return "ConfigConversionEffect"; }
Brian Osman28804f32017-04-20 10:24:36 -0400143
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000144private:
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500145 GrConfigConversionEffect(PMConversion pmConversion)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400146 : INHERITED(kGrConfigConversionEffect_ClassID, kNone_OptimizationFlags)
147 , fPmConversion(pmConversion) {}
egdaniel57d3b032015-11-13 11:57:27 -0800148 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
Brian Salomon94efbf52016-11-29 13:43:05 -0500149 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700150 bool onIsEqual(const GrFragmentProcessor&) const override;
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400151 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500152 PMConversion fPmConversion;
Brian Osmanee805322017-04-05 10:09:00 -0400153 typedef GrFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000154};
Ethan Nicholas839872c2017-10-05 12:36:59 -0400155#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000156#endif