aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/shaders/gradients/SkTwoPointConicalGradient_gpu.cpp
blob: 975f7bb38128c9cea13e7ceca71746d5f3a84f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "SkTwoPointConicalGradient.h"

#if SK_SUPPORT_GPU
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLProgramDataManager.h"
#include "glsl/GrGLSLUniformHandler.h"
#include "SkTwoPointConicalGradient_gpu.h"

// For brevity
typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;

class TwoPointConicalEffect : public GrGradientEffect {
public:
    class DegeneratedGLSLProcessor; // radial (center0 == center1) or strip (r0 == r1) case
    class FocalGLSLProcessor; // all other cases where we can derive a focal point

    enum Type {
        kRadial_Type,
        kStrip_Type,
        kFocal_Type
    };

    struct Data {
        SkScalar    fRadius0;
        SkScalar    fDiffRadius;
        Type        fType;
        bool        fIsSwapped;

        // Construct from the shader, and set the matrix accordingly
        Data(const SkTwoPointConicalGradient& shader, SkMatrix& matrix);

        bool operator== (const Data& d) const {
            return fRadius0 == d.fRadius0 && fDiffRadius == d.fDiffRadius && fType == d.fType &&
                   fIsSwapped == d.fIsSwapped;
        }
    };

    static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args, const Data& data);

    SkScalar diffRadius() const { return fData.fDiffRadius; }
    SkScalar r0() const { return fData.fRadius0; }
    SkScalar r1() const { return fData.fRadius0 + fData.fDiffRadius; }

    const char* name() const override { return "Two-Point Conical Gradient"; }

    // Whether the focal point (0, 0) is on the end circle with center (1, 0) and radius r1. If this
    // is true, it's as if an aircraft is flying at Mach 1 and all circles (soundwaves) will go
    // through the focal point (aircraft). In our previous implementations, this was known as the
    // edge case where the inside circle touches the outside circle (on the focal point). If we were
    // to solve for t bruteforcely using a quadratic equation, this case implies that the quadratic
    // equation degenerates to a linear equation.
    bool isFocalOnCircle() const { return SkScalarNearlyZero(1 - this->r1()); }
    bool isSwapped() const { return fData.fIsSwapped; }

    Type getType() const { return fData.fType; }

    // Whether the t we solved is always valid (so we don't need to check r(t) > 0).
    bool isWellBehaved() const { return !this->isFocalOnCircle() && this->r1() > 1; }

    // Whether r0 == 0 so it's focal without any transformation
    bool isNativelyFocal() const { return SkScalarNearlyZero(fData.fRadius0); }

    bool isRadiusIncreasing() const { return fData.fDiffRadius > 0; }

protected:
    void onGetGLSLProcessorKey(const GrShaderCaps& c, GrProcessorKeyBuilder* b) const override {
        INHERITED::onGetGLSLProcessorKey(c, b);
        uint32_t key = 0;
        key |= fData.fType;
        SkASSERT(key < (1 << 2));
        key |= (this->isFocalOnCircle() << 2);
        key |= (this->isWellBehaved() << 3);
        key |= (this->isRadiusIncreasing() << 4);
        key |= (this->isNativelyFocal() << 5);
        key |= (this->isSwapped() << 6);
        SkASSERT(key < (1 << 7));
        b->add32(key);
    }


    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;

    std::unique_ptr<GrFragmentProcessor> clone() const override {
        return std::unique_ptr<GrFragmentProcessor>(new TwoPointConicalEffect(*this));
    }

    bool onIsEqual(const GrFragmentProcessor& sBase) const override {
        const TwoPointConicalEffect& s = sBase.cast<TwoPointConicalEffect>();
        return (INHERITED::onIsEqual(sBase) && fData == s.fData);
    }

    explicit TwoPointConicalEffect(const CreateArgs& args, const Data data)
        : INHERITED(kTwoPointConicalEffect_ClassID, args,
            false /* opaque: draws transparent black outside of the cone. */)
        , fData(data) {}

    explicit TwoPointConicalEffect(const TwoPointConicalEffect& that)
            : INHERITED(that)
            , fData(that.fData) {}

    GR_DECLARE_FRAGMENT_PROCESSOR_TEST

    Data fData;

    typedef GrGradientEffect INHERITED;
};

GR_DEFINE_FRAGMENT_PROCESSOR_TEST(TwoPointConicalEffect);

#if GR_TEST_UTILS

// TODO (liyuqian): remove this and the friend declaration once the bug skia:7436 is fixed.
class DumpHelper {
public:
    static void Dump(GrGradientEffect::RandomGradientParams& params) {
        if (params.fColorSpace) {
            auto csData = params.fColorSpace->serialize();
            DumpData("csData", csData->data(), csData->size());
            SkDebugf("sk_sp<SkColorSpace> colorSpace = SkColorSpace::Deserialize(csData, %d);\n",
                     csData->size());
        } else {
            SkDebugf("sk_sp<SkColorSpace> colorSpace = nullptr;");
        }

        auto csBackup = params.fColorSpace;
        params.fColorSpace = nullptr; // make sure that we won't dump an illegal sk_sp
        DumpData("paramsData", &params, sizeof(GrGradientEffect::RandomGradientParams));
        SkDebugf("auto& params = *(RandomGradientParams*)(paramsData);\n");
        SkDebugf("params.fColorSpace = colorSpace;\n");
        SkDebugf("if (params.fStops != nullptr) { params.fStops = params.fStopStorage; }\n");
        params.fColorSpace = csBackup;
    }

    static void DumpData(const char* name, const void* data, int size) {
        SkDebugf("char %s[%d] = {", name, size);
        for(int i = 0; i < size; ++i) {
            SkDebugf("%s%d", i == 0 ? "" : ", ", static_cast<const char*>(data)[i]);
        }
        SkDebugf("};\n");
    }

    static SkString Hex(float f) {
        return SkStringPrintf("SkBits2Float(0x%08x)", SkFloat2Bits(f));
    }
};


std::unique_ptr<GrFragmentProcessor> TwoPointConicalEffect::TestCreate(
        GrProcessorTestData* d) {
    SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
    SkPoint center2 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
    SkScalar radius1 = d->fRandom->nextUScalar1();
    SkScalar radius2 = d->fRandom->nextUScalar1();

    constexpr int   kTestTypeMask           = (1 << 2) - 1,
                    kTestNativelyFocalBit   = (1 << 2),
                    kTestFocalOnCircleBit   = (1 << 3),
                    kTestSwappedBit         = (1 << 4);
                    // We won't treat isWellDefined and isRadiusIncreasing specially beacuse they
                    // should have high probability to be turned on and off as we're getting random
                    // radii and centers.

    int mask = d->fRandom->nextU();
    int type = mask & kTestTypeMask;
    if (type == TwoPointConicalEffect::kRadial_Type) {
        center2 = center1;
        // Make sure that the radii are different
        if (SkScalarNearlyZero(radius1 - radius2)) {
            radius2 += .1f;
        }
    } else if (type == TwoPointConicalEffect::kStrip_Type) {
        radius1 = SkTMax(radius1, .1f); // Make sure that the radius is non-zero
        radius2 = radius1;
        // Make sure that the centers are different
        if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
            center2.fX += .1f;
        }
    } else { // kFocal_Type
        // Make sure that the centers are different
        if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
            center2.fX += .1f;
        }

        if (kTestNativelyFocalBit & mask) {
            radius1 = 0;
        }
        if (kTestFocalOnCircleBit & mask) {
            radius2 = radius1 + SkPoint::Distance(center1, center2);
        }
        if (kTestSwappedBit & mask) {
            std::swap(radius1, radius2);
            radius2 = 0;
        }

        // Make sure that the radii are different
        if (SkScalarNearlyZero(radius1 - radius2)) {
            radius2 += .1f;
        }
    }

    if (SkScalarNearlyZero(radius1 - radius2) &&
            SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
        radius2 += .1f; // make sure that we're not degenerated
    }

    RandomGradientParams params(d->fRandom);
    auto shader = params.fUseColors4f ?
        SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2,
                                              params.fColors4f, params.fColorSpace, params.fStops,
                                              params.fColorCount, params.fTileMode) :
        SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2,
                                              params.fColors, params.fStops,
                                              params.fColorCount, params.fTileMode);
    GrTest::TestAsFPArgs asFPArgs(d);
    std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());

#ifdef SK_DEBUG
    if (!fp) {
        auto h = DumpHelper::Hex;
        SkDebugf("SkPoint  center1 = {%s, %s};\n", h(center1.fX).c_str(), h(center1.fY).c_str());
        SkDebugf("SkPoint  center2 = {%s, %s};\n", h(center2.fX).c_str(), h(center2.fY).c_str());
        SkDebugf("SkScalar radius1 = %s, radius2 = %s;\n", h(radius1).c_str(), h(radius2).c_str());
        DumpHelper::Dump(params);
    }
#endif

    GrAlwaysAssert(fp);
    return fp;
}
#endif

//////////////////////////////////////////////////////////////////////////////
// DegeneratedGLSLProcessor
//////////////////////////////////////////////////////////////////////////////

class TwoPointConicalEffect::DegeneratedGLSLProcessor : public GrGradientEffect::GLSLProcessor {
protected:
    void emitCode(EmitArgs& args) override {
        const TwoPointConicalEffect& ge = args.fFp.cast<TwoPointConicalEffect>();
        GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
        this->emitUniforms(uniformHandler, ge);
        fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
                                               "Conical2FSParams");

        SkString p0; // r0 for radial case, r0^2 for strip case
        p0.appendf("%s", uniformHandler->getUniformVariable(fParamUni).getName().c_str());
        const char* tName = "t"; // the gradient

        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
        SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
        const char* p = coords2D.c_str();

        if (ge.getType() == kRadial_Type) {
            fragBuilder->codeAppendf("half %s = length(%s) - %s;", tName, p, p0.c_str());
        } else {
            // output will default to transparent black (we simply won't write anything
            // else to it if invalid, instead of discarding or returning prematurely)
            fragBuilder->codeAppendf("%s = half4(0.0,0.0,0.0,0.0);", args.fOutputColor);
            fragBuilder->codeAppendf("half temp = %s - %s.y * %s.y;", p0.c_str(), p, p);
            fragBuilder->codeAppendf("if (temp >= 0) {");
            fragBuilder->codeAppendf("half %s = %s.x + sqrt(temp);", tName, p);
        }
        this->emitColor(fragBuilder,
                        uniformHandler,
                        args.fShaderCaps,
                        ge,
                        tName,
                        args.fOutputColor,
                        args.fInputColor,
                        args.fTexSamplers);

        if (ge.getType() != kRadial_Type) {
            fragBuilder->codeAppendf("}");
        }
    }

    void onSetData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& p) override {
        INHERITED::onSetData(pdman, p);
        const TwoPointConicalEffect& data = p.cast<TwoPointConicalEffect>();
        // kRadialType should imply r1 - r0 = 1 (after our transformation) so r0 = r0 / (r1 - r0)
        SkASSERT(data.getType() == kStrip_Type || SkScalarNearlyZero(data.r1() - data.r0() - 1));
        pdman.set1f(fParamUni, data.getType() == kRadial_Type ? data.r0() : data.r0() * data.r0());
    }

    UniformHandle fParamUni;

private:
    typedef GrGradientEffect::GLSLProcessor INHERITED;
};

//////////////////////////////////////////////////////////////////////////////
// FocalGLSLProcessor
//////////////////////////////////////////////////////////////////////////////

class TwoPointConicalEffect::FocalGLSLProcessor : public GrGradientEffect::GLSLProcessor {
protected:
    void emitCode(EmitArgs& args) override {
        const TwoPointConicalEffect& ge = args.fFp.cast<TwoPointConicalEffect>();
        GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
        this->emitUniforms(uniformHandler, ge);
        fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
                                               "Conical2FSParams");

        SkString p0; // 1 / r1
        SkString p1; // r0 / (r1 - r0)
        p0.appendf("%s.x", uniformHandler->getUniformVariable(fParamUni).getName().c_str());
        p1.appendf("%s.y", uniformHandler->getUniformVariable(fParamUni).getName().c_str());
        const char* tName = "t"; // the gradient

        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
        SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
        const char* p = coords2D.c_str();

        if (ge.isFocalOnCircle()) {
            fragBuilder->codeAppendf("half %s_prime = dot(%s, %s) / %s.x;", tName, p, p, p);
        } else if (ge.isWellBehaved()) {
            // empty sign is positive
            char sign = ge.isRadiusIncreasing() ? ' ' : '-';
            fragBuilder->codeAppendf("half %s_prime = %clength(%s) - %s.x * %s;",
                    tName, sign, p, p, p0.c_str());
        } else {
            char sign = ge.isSwapped() ? '-' : ' ';
            fragBuilder->codeAppendf("half temp = %s.x * %s.x - %s.y * %s.y;", p, p, p, p);
            // Initialize t_prime to illegal state (where r(t) < 0)
            fragBuilder->codeAppendf("half %s_prime = %s;",
                    tName, ge.isRadiusIncreasing() ? "-1" : "1");

            // Only do sqrt if temp >= 0; this is significantly slower than checking temp >= 0 in
            // the if statement that checks r(t) >= 0. But GPU may break if we sqrt a negative
            // float. (Although I havevn't observed that on any devices so far, and the old approach
            // also does sqrt negative value without a check.) If the performance is really
            // critical, maybe we should just compute the area where temp and t_prime are always
            // valid and drop all these ifs.
            fragBuilder->codeAppendf("if (temp >= 0) {");
            fragBuilder->codeAppendf("%s_prime = (%csqrt(temp) - %s.x * %s);",
                    tName, sign, p, p0.c_str());
            fragBuilder->codeAppendf("}");
        }
        // "- 0" is much faster than "- p1" so we specialize the natively focal case where p1 = 0.
        fragBuilder->codeAppendf("half %s = %s_prime - %s;", tName, tName,
                ge.isNativelyFocal() ? "0" : p1.c_str());

        if (ge.isSwapped()) {
            fragBuilder->codeAppendf("%s = 1 - %s;", tName, tName);
        }

        if (!ge.isWellBehaved()) {
            // output will default to transparent black (we simply won't write anything
            // else to it if invalid, instead of discarding or returning prematurely)
            fragBuilder->codeAppendf("%s = half4(0.0,0.0,0.0,0.0);", args.fOutputColor);

            // r(t) must be nonnegative; we need to swap direction if r0 > r1 because we did a final
            // scale of r1 / (r1 - r0) that's negative if r0 > r1.
            char direction = ge.isRadiusIncreasing() ? '>' : '<';
            fragBuilder->codeAppendf("if (%s_prime %c= 0.0) {", tName, direction);
        }
        this->emitColor(fragBuilder,
                        uniformHandler,
                        args.fShaderCaps,
                        ge,
                        tName,
                        args.fOutputColor,
                        args.fInputColor,
                        args.fTexSamplers);
        if (!ge.isWellBehaved()) {
            fragBuilder->codeAppend("};");
        }
    }

    void onSetData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& p) override {
        INHERITED::onSetData(pdman, p);
        const TwoPointConicalEffect& data = p.cast<TwoPointConicalEffect>();
        SkScalar r0 = data.r0();
        SkScalar r1 = data.r1();
        pdman.set2f(fParamUni, 1 / r1, r0 / (r1 - r0));
    }

    UniformHandle fParamUni;

private:
    typedef GrGradientEffect::GLSLProcessor INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

GrGLSLFragmentProcessor* TwoPointConicalEffect::onCreateGLSLInstance() const {
    if (fData.fType == kRadial_Type || fData.fType == kStrip_Type) {
        return new DegeneratedGLSLProcessor;
    }
    return new FocalGLSLProcessor;
}

std::unique_ptr<GrFragmentProcessor> TwoPointConicalEffect::Make(
        const GrGradientEffect::CreateArgs& args, const Data& data) {
    return GrGradientEffect::AdjustFP(
            std::unique_ptr<TwoPointConicalEffect>(new TwoPointConicalEffect(args, data)),
            args);
}

std::unique_ptr<GrFragmentProcessor> Gr2PtConicalGradientEffect::Make(
        const GrGradientEffect::CreateArgs& args) {
    const SkTwoPointConicalGradient& shader =
        *static_cast<const SkTwoPointConicalGradient*>(args.fShader);

    SkMatrix matrix;
    if (!shader.getLocalMatrix().invert(&matrix)) {
        return nullptr;
    }
    if (args.fMatrix) {
        SkMatrix inv;
        if (!args.fMatrix->invert(&inv)) {
            return nullptr;
        }
        matrix.postConcat(inv);
    }

    GrGradientEffect::CreateArgs newArgs(args.fContext, args.fShader, &matrix, args.fWrapMode,
        args.fDstColorSpace);
    // Data and matrix has to be prepared before constructing TwoPointConicalEffect so its parent
    // class can have the right matrix to work with during construction.
    TwoPointConicalEffect::Data data(shader, matrix);
    return TwoPointConicalEffect::Make(newArgs, data);
}

TwoPointConicalEffect::Data::Data(const SkTwoPointConicalGradient& shader, SkMatrix& matrix) {
    fIsSwapped = false;
    if (SkScalarNearlyZero(shader.getCenterX1())) {
        fType = kRadial_Type;
        SkScalar dr = shader.getDiffRadius();
        // Map center to (0, 0) and scale dr to 1
        matrix.postTranslate(-shader.getStartCenter().fX, -shader.getStartCenter().fY);
        matrix.postScale(1 / dr, 1 / dr);
        fRadius0 = shader.getStartRadius() / dr;
        fDiffRadius = 1;
    } else {
        // Map centers to (0, 0), (1, 0)
        const SkPoint centers[2] = { shader.getStartCenter(), shader.getEndCenter() };
        const SkPoint unitvec[2] = { { 0, 0 },{ 1, 0 } };
        SkMatrix gradientMatrix;
        // The radial case is already handled so this must succeed
        SkAssertResult(gradientMatrix.setPolyToPoly(centers, unitvec, 2));
        matrix.postConcat(gradientMatrix);
        fRadius0 = shader.getStartRadius() / shader.getCenterX1();
        fDiffRadius = shader.getDiffRadius() / shader.getCenterX1();

        if (SkScalarNearlyZero(shader.getDiffRadius())) {
            fType = kStrip_Type;
        } else { // focal case
            fType = kFocal_Type;
            if (SkScalarNearlyZero(shader.getEndRadius())) {
                // swap r0, r1
                matrix.postTranslate(-1, 0);
                matrix.postScale(-1, 1);
                fRadius0 = 0;
                fDiffRadius = -fDiffRadius;
                fIsSwapped = true;
            }

            // Map {focal point, (1, 0)} to {(0, 0), (1, 0)}
            SkScalar focalX = - fRadius0 / fDiffRadius;
            const SkPoint from[2]   = { {focalX, 0}, {1, 0} };
            const SkPoint to[2]     = { {0, 0}, {1, 0} };
            SkMatrix focalMatrix;
            focalMatrix.setPolyToPoly(from, to, 2);
            matrix.postConcat(focalMatrix);
            fRadius0 /= SkScalarAbs(1 - focalX);
            fDiffRadius /= SkScalarAbs(1 - focalX);

            SkScalar r0 = fRadius0;
            SkScalar r1 = fRadius0 + fDiffRadius;
            // The following transformations are not reflected on data; they're just to accelerate
            // the shader computation by saving some arithmatic operations.
            bool isFocalOnCircle = SkScalarNearlyZero(1 - r1);
            if (isFocalOnCircle) {
                matrix.postScale(0.5, 0.5); // r1 = 1 so r1 + 1 = 2 and 0.5 = 1 / (r1 + 1)
            } else {
                matrix.postScale(r1 / (r1 * r1 - 1), 1 / sqrt(SkScalarAbs(r1 * r1 - 1)));
            }
            matrix.postScale(r1 / (r1 - r0), r1 / (r1 - r0));
        }
    }
}

#endif