blob: 301a6f9bf47365d2785d793cdddd6848b7deac53 [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"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014
Ethan Nicholas839872c2017-10-05 12:36:59 -040015#include "GrClip.h"
16#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050017#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050018#include "GrProxyProvider.h"
Ethan Nicholas839872c2017-10-05 12:36:59 -040019#include "GrRenderTargetContext.h"
Mike Kleind9187c02018-09-07 17:32:47 +000020#include "GrFragmentProcessor.h"
21#include "GrCoordTransform.h"
Brian Osmanee805322017-04-05 10:09:00 -040022class GrConfigConversionEffect : public GrFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000023public:
Ethan Nicholas839872c2017-10-05 12:36:59 -040024 static bool TestForPreservingPMConversions(GrContext* context) {
25 static constexpr int kSize = 256;
26 static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -050027 static constexpr SkColorType kColorType = kRGBA_8888_SkColorType;
28 const GrBackendFormat format =
29 context->contextPriv().caps()->getBackendFormatFromColorType(kColorType);
Ethan Nicholas839872c2017-10-05 12:36:59 -040030 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
31 uint32_t* srcData = data.get();
32 uint32_t* firstRead = data.get() + kSize * kSize;
33 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
Robert Phillips757914d2017-01-25 15:48:30 -050034
Ethan Nicholas839872c2017-10-05 12:36:59 -040035 // Fill with every possible premultiplied A, color channel value. There will be 256-y
36 // duplicate values in row y. We set r, g, and b to the same value since they are handled
37 // identically.
38 for (int y = 0; y < kSize; ++y) {
39 for (int x = 0; x < kSize; ++x) {
40 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize * y + x]);
41 color[3] = y;
42 color[2] = SkTMin(x, y);
43 color[1] = SkTMin(x, y);
44 color[0] = SkTMin(x, y);
45 }
46 }
Brian Salomon7b9b3262018-04-27 17:38:06 -040047 memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
48 memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000049
Ethan Nicholas839872c2017-10-05 12:36:59 -040050 const SkImageInfo ii =
51 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
52
Robert Phillips0c4b7b12018-03-06 08:20:37 -050053 sk_sp<GrRenderTargetContext> readRTC(context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -050054 format, SkBackingFit::kExact, kSize, kSize, kConfig, nullptr));
Robert Phillips0c4b7b12018-03-06 08:20:37 -050055 sk_sp<GrRenderTargetContext> tempRTC(context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -050056 format, SkBackingFit::kExact, kSize, kSize, kConfig, nullptr));
Ethan Nicholas839872c2017-10-05 12:36:59 -040057 if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
58 return false;
59 }
Greg Danielf44cb482018-02-27 14:26:32 -050060 // Adding discard to appease vulkan validation warning about loading uninitialized data on
61 // draw
62 readRTC->discard();
63
Robert Phillips579f0942018-01-08 14:53:35 -050064 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
65
Greg Daniel6e9a4f62018-03-15 17:00:06 -040066 SkPixmap pixmap(ii, srcData, 4 * kSize);
67
68 // This function is only ever called if we are in a GrContext that has a GrGpu since we are
69 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
70 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
71 sk_sp<SkImage> image = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
72
73 sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy(
74 std::move(image), kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact);
Ethan Nicholas839872c2017-10-05 12:36:59 -040075 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(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050089 new GrConfigConversionEffect(PMConversion::kToUnpremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040090 std::unique_ptr<GrFragmentProcessor> upmToPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050091 new GrConfigConversionEffect(PMConversion::kToPremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040092
Brian Osman2240be92017-10-18 13:15:13 -040093 paint1.addColorTextureProcessor(dataProxy, SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -040094 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
Greg Danielf44cb482018-02-27 14:26:32 -0500103 // Adding discard to appease vulkan validation warning about loading uninitialized data on
104 // draw
105 tempRTC->discard();
106
Brian Osman2240be92017-10-18 13:15:13 -0400107 paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400108 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
Brian Osman2240be92017-10-18 13:15:13 -0400114 paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), SkMatrix::I());
Ethan Nicholas839872c2017-10-05 12:36:59 -0400115 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 }
Michael Ludwiga4275592018-08-31 10:52:47 -0400135 const PMConversion& pmConversion() const { return fPmConversion; }
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;
Ethan Nicholas839872c2017-10-05 12:36:59 -0400148 const char* name() const override { return "ConfigConversionEffect"; }
Brian Osman28804f32017-04-20 10:24:36 -0400149
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000150private:
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500151 GrConfigConversionEffect(PMConversion pmConversion)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400152 : INHERITED(kGrConfigConversionEffect_ClassID, kNone_OptimizationFlags)
153 , fPmConversion(pmConversion) {}
egdaniel57d3b032015-11-13 11:57:27 -0800154 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
Brian Salomon94efbf52016-11-29 13:43:05 -0500155 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700156 bool onIsEqual(const GrFragmentProcessor&) const override;
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400157 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500158 PMConversion fPmConversion;
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