1/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8@header {
9    #include "include/gpu/GrContext.h"
10    #include "src/gpu/GrBitmapTextureMaker.h"
11    #include "src/gpu/GrClip.h"
12    #include "src/gpu/GrContextPriv.h"
13    #include "src/gpu/GrImageInfo.h"
14    #include "src/gpu/GrRenderTargetContext.h"
15}
16
17@class {
18    static bool TestForPreservingPMConversions(GrContext* context) {
19        static constexpr int kSize = 256;
20        static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
21        SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
22        uint32_t* srcData = data.get();
23        uint32_t* firstRead = data.get() + kSize * kSize;
24        uint32_t* secondRead = data.get() + 2 * kSize * kSize;
25
26        // Fill with every possible premultiplied A, color channel value. There will be 256-y
27        // duplicate values in row y. We set r, g, and b to the same value since they are handled
28        // identically.
29        for (int y = 0; y < kSize; ++y) {
30            for (int x = 0; x < kSize; ++x) {
31                uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
32                color[3] = y;
33                color[2] = std::min(x, y);
34                color[1] = std::min(x, y);
35                color[0] = std::min(x, y);
36            }
37        }
38        memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
39        memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
40
41        const SkImageInfo ii = SkImageInfo::Make(kSize, kSize,
42                                                 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
43
44        auto readRTC = GrRenderTargetContext::Make(
45                context, kColorType, nullptr, SkBackingFit::kExact, {kSize, kSize});
46        auto tempRTC = GrRenderTargetContext::Make(
47                context, kColorType, nullptr, SkBackingFit::kExact, {kSize, kSize});
48        if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
49            return false;
50        }
51        // Adding discard to appease vulkan validation warning about loading uninitialized data on
52        // draw
53        readRTC->discard();
54
55        // This function is only ever called if we are in a GrContext that has a GrGpu since we are
56        // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
57        // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
58        SkBitmap bitmap;
59        bitmap.installPixels(ii, srcData, 4 * kSize);
60        bitmap.setImmutable();
61
62        GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
63        auto dataView = maker.view(GrMipMapped::kNo);
64        if (!dataView) {
65            return false;
66        }
67
68        static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
69
70        // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
71        // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
72        // We then verify that two reads produced the same values.
73
74        GrPaint paint1;
75        GrPaint paint2;
76        GrPaint paint3;
77        std::unique_ptr<GrFragmentProcessor> pmToUPM(
78                new GrConfigConversionEffect(PMConversion::kToUnpremul));
79        std::unique_ptr<GrFragmentProcessor> upmToPM(
80                new GrConfigConversionEffect(PMConversion::kToPremul));
81
82        paint1.addColorFragmentProcessor(GrTextureEffect::Make(std::move(dataView),
83                                                               kPremul_SkAlphaType));
84        paint1.addColorFragmentProcessor(pmToUPM->clone());
85        paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
86
87        readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect,
88                                kRect);
89        if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
90            return false;
91        }
92
93        // Adding discard to appease vulkan validation warning about loading uninitialized data on
94        // draw
95        tempRTC->discard();
96
97        paint2.addColorFragmentProcessor(GrTextureEffect::Make(readRTC->readSurfaceView(),
98                                                               kUnpremul_SkAlphaType));
99        paint2.addColorFragmentProcessor(std::move(upmToPM));
100        paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
101
102        tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect,
103                                kRect);
104
105        paint3.addColorFragmentProcessor(GrTextureEffect::Make(tempRTC->readSurfaceView(),
106                                                               kPremul_SkAlphaType));
107        paint3.addColorFragmentProcessor(std::move(pmToUPM));
108        paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
109
110        readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect,
111                                kRect);
112
113        if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
114            return false;
115        }
116
117        for (int y = 0; y < kSize; ++y) {
118            for (int x = 0; x <= y; ++x) {
119                if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
120                    return false;
121                }
122            }
123        }
124
125        return true;
126    }
127}
128
129@make {
130    static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
131                                                     PMConversion pmConversion) {
132        if (!fp) {
133            return nullptr;
134        }
135        std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion));
136        std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), std::move(ccFP) };
137        return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
138    }
139}
140
141layout(key) in PMConversion pmConversion;
142
143@emitCode {
144    fragBuilder->forceHighPrecision();
145}
146
147void main() {
148    // Aggressively round to the nearest exact (N / 255) floating point value. This lets us find a
149    // round-trip preserving pair on some GPUs that do odd byte to float conversion.
150    sk_OutColor = floor(sk_InColor * 255 + 0.5) / 255;
151
152    @switch (pmConversion) {
153        case PMConversion::kToPremul:
154            sk_OutColor.rgb = floor(sk_OutColor.rgb * sk_OutColor.a * 255 + 0.5) / 255;
155            break;
156
157        case PMConversion::kToUnpremul:
158            sk_OutColor.rgb = sk_OutColor.a <= 0.0 ?
159                                          half3(0) :
160                                          floor(sk_OutColor.rgb / sk_OutColor.a * 255 + 0.5) / 255;
161            break;
162    }
163}
164
165@test(data) {
166    PMConversion pmConv = static_cast<PMConversion>(data->fRandom->nextULessThan(
167                                                             (int) PMConversion::kPMConversionCnt));
168    return std::unique_ptr<GrFragmentProcessor>(new GrConfigConversionEffect(pmConv));
169}
170