blob: 5e4b3ceaca7969c84b44df98390545a7cd6474c5 [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"
Mike Reed46f5c5f2020-02-20 15:42:29 -050014#include "include/core/SkM44.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/gpu/GrContext.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050017#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrClip.h"
19#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040020#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRenderTargetContext.h"
22
23#include "src/gpu/GrCoordTransform.h"
24#include "src/gpu/GrFragmentProcessor.h"
Brian Osmanee805322017-04-05 10:09:00 -040025class GrConfigConversionEffect : public GrFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000026public:
Ethan Nicholas839872c2017-10-05 12:36:59 -040027 static bool TestForPreservingPMConversions(GrContext* context) {
Mike Kleind6ab77a2019-03-21 08:18:24 -050028 static constexpr int kSize = 256;
Brian Salomond6287472019-06-24 15:50:07 -040029 static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
Ethan Nicholas839872c2017-10-05 12:36:59 -040030 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
Mike Kleind6ab77a2019-03-21 08:18:24 -050031 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]);
Mike Kleind6ab77a2019-03-21 08:18:24 -050041 color[3] = y;
Brian Osman788b9162020-02-07 10:36:46 -050042 color[2] = std::min(x, y);
43 color[1] = std::min(x, y);
44 color[0] = std::min(x, y);
Ethan Nicholas839872c2017-10-05 12:36:59 -040045 }
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
Greg Daniele20fcad2020-01-08 11:52:34 -050053 auto readRTC = GrRenderTargetContext::Make(context, kColorType, nullptr,
54 SkBackingFit::kExact, {kSize, kSize});
55 auto tempRTC = GrRenderTargetContext::Make(context, kColorType, nullptr,
56 SkBackingFit::kExact, {kSize, kSize});
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
Greg Daniel6e9a4f62018-03-15 17:00:06 -040064 // This function is only ever called if we are in a GrContext that has a GrGpu since we are
65 // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
66 // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
Greg Daniel6f5441a2020-01-28 17:02:49 -050067 SkBitmap bitmap;
68 bitmap.installPixels(ii, srcData, 4 * kSize);
69 bitmap.setImmutable();
70
71 GrBitmapTextureMaker maker(context, bitmap);
Greg Danielc61d7e32020-02-04 14:27:45 -050072 auto[dataView, ct] = maker.view(GrMipMapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -050073
Greg Danielcc104db2020-02-03 14:17:08 -050074 if (!dataView.proxy()) {
Ethan Nicholas839872c2017-10-05 12:36:59 -040075 return false;
76 }
77
78 static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
79
80 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
81 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
82 // We then verify that two reads produced the same values.
83
Mike Kleind6ab77a2019-03-21 08:18:24 -050084 GrPaint paint1;
85 GrPaint paint2;
86 GrPaint paint3;
Ethan Nicholas839872c2017-10-05 12:36:59 -040087 std::unique_ptr<GrFragmentProcessor> pmToUPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050088 new GrConfigConversionEffect(PMConversion::kToUnpremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040089 std::unique_ptr<GrFragmentProcessor> upmToPM(
Ethan Nicholasaae47c82017-11-10 15:34:03 -050090 new GrConfigConversionEffect(PMConversion::kToPremul));
Ethan Nicholas839872c2017-10-05 12:36:59 -040091
Greg Danielcc104db2020-02-03 14:17:08 -050092 paint1.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050093 GrTextureEffect::Make(std::move(dataView), kPremul_SkAlphaType));
Ethan Nicholas839872c2017-10-05 12:36:59 -040094 paint1.addColorFragmentProcessor(pmToUPM->clone());
95 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
96
Mike Kleind6ab77a2019-03-21 08:18:24 -050097 readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect,
98 kRect);
Brian Salomon1d435302019-07-01 13:05:28 -040099 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -0400100 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 Salomonbfb72112020-01-13 10:51:50 -0500107 paint2.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -0500108 GrTextureEffect::Make(readRTC->readSurfaceView(), kUnpremul_SkAlphaType));
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 Salomonbfb72112020-01-13 10:51:50 -0500115 paint3.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -0500116 GrTextureEffect::Make(tempRTC->readSurfaceView(), kPremul_SkAlphaType));
Ethan Nicholas839872c2017-10-05 12:36:59 -0400117 paint3.addColorFragmentProcessor(std::move(pmToUPM));
118 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
119
Mike Kleind6ab77a2019-03-21 08:18:24 -0500120 readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect,
121 kRect);
Ethan Nicholas839872c2017-10-05 12:36:59 -0400122
Brian Salomon1d435302019-07-01 13:05:28 -0400123 if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -0400124 return false;
125 }
126
127 for (int y = 0; y < kSize; ++y) {
128 for (int x = 0; x <= y; ++x) {
129 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
130 return false;
131 }
132 }
133 }
134
135 return true;
136 }
Ethan Nicholas839872c2017-10-05 12:36:59 -0400137
138 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
139 PMConversion pmConversion) {
140 if (!fp) {
141 return nullptr;
142 }
143 std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion));
144 std::unique_ptr<GrFragmentProcessor> fpPipeline[] = {std::move(fp), std::move(ccFP)};
145 return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
146 }
147 GrConfigConversionEffect(const GrConfigConversionEffect& src);
Brian Salomonaff329b2017-08-11 09:40:37 -0400148 std::unique_ptr<GrFragmentProcessor> clone() const override;
Mike Kleind6ab77a2019-03-21 08:18:24 -0500149 const char* name() const override { return "ConfigConversionEffect"; }
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400150 PMConversion pmConversion;
Brian Osman28804f32017-04-20 10:24:36 -0400151
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000152private:
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500153 GrConfigConversionEffect(PMConversion pmConversion)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400154 : INHERITED(kGrConfigConversionEffect_ClassID, kNone_OptimizationFlags)
Ethan Nicholasbcd51e82019-04-09 10:40:41 -0400155 , pmConversion(pmConversion) {}
egdaniel57d3b032015-11-13 11:57:27 -0800156 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
Brian Salomon94efbf52016-11-29 13:43:05 -0500157 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700158 bool onIsEqual(const GrFragmentProcessor&) const override;
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400159 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Brian Osmanee805322017-04-05 10:09:00 -0400160 typedef GrFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000161};
Ethan Nicholas839872c2017-10-05 12:36:59 -0400162#endif