blob: f6172b9e35dbaedfa6cebdbc948a6e153160c00c [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"
Weston Tracey074414f2020-05-29 12:55:06 +000018#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#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
Brian Salomonbc074a62020-03-18 10:06:13 -040071 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomonecbb0fb2020-02-28 18:07:32 -050072 auto dataView = maker.view(GrMipMapped::kNo);
73 if (!dataView) {
Ethan Nicholas839872c2017-10-05 12:36:59 -040074 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
Greg Danielcc104db2020-02-03 14:17:08 -050091 paint1.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050092 GrTextureEffect::Make(std::move(dataView), kPremul_SkAlphaType));
Ethan Nicholas839872c2017-10-05 12:36:59 -040093 paint1.addColorFragmentProcessor(pmToUPM->clone());
94 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
95
Weston Tracey074414f2020-05-29 12:55:06 +000096 readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect,
97 kRect);
Brian Salomon1d435302019-07-01 13:05:28 -040098 if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
Ethan Nicholas839872c2017-10-05 12:36:59 -040099 return false;
100 }
101
Greg Danielf44cb482018-02-27 14:26:32 -0500102 // Adding discard to appease vulkan validation warning about loading uninitialized data on
103 // draw
104 tempRTC->discard();
105
Brian Salomonbfb72112020-01-13 10:51:50 -0500106 paint2.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -0500107 GrTextureEffect::Make(readRTC->readSurfaceView(), kUnpremul_SkAlphaType));
Ethan Nicholas839872c2017-10-05 12:36:59 -0400108 paint2.addColorFragmentProcessor(std::move(upmToPM));
109 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
110
Weston Tracey074414f2020-05-29 12:55:06 +0000111 tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect,
112 kRect);
Ethan Nicholas839872c2017-10-05 12:36:59 -0400113
Brian Salomonbfb72112020-01-13 10:51:50 -0500114 paint3.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -0500115 GrTextureEffect::Make(tempRTC->readSurfaceView(), kPremul_SkAlphaType));
Ethan Nicholas839872c2017-10-05 12:36:59 -0400116 paint3.addColorFragmentProcessor(std::move(pmToUPM));
117 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
118
Weston Tracey074414f2020-05-29 12:55:06 +0000119 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