1/*
2 * Copyright 2019 Google LLC
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
8in fragmentProcessor inputFP;
9layout(key) in bool clampToPremul;
10
11@optimizationFlags {
12    ProcessorOptimizationFlags(inputFP.get()) & (kConstantOutputForConstantInput_OptimizationFlag |
13                                                 kPreservesOpaqueInput_OptimizationFlag)
14}
15
16half4 clampedPM(half4 inputColor) {
17    half alpha = saturate(inputColor.a);
18    return half4(clamp(inputColor.rgb, 0, alpha), alpha);
19}
20
21half4 main() {
22    half4 inputColor = sample(inputFP);
23    return clampToPremul ? clampedPM(inputColor) : saturate(inputColor);
24}
25
26@class {
27    SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
28        SkPMColor4f input = ConstantOutputForConstantInput(this->childProcessor(0), inColor);
29        float clampedAlpha = SkTPin(input.fA, 0.f, 1.f);
30        float clampVal = clampToPremul ? clampedAlpha : 1.f;
31        return {SkTPin(input.fR, 0.f, clampVal),
32                SkTPin(input.fG, 0.f, clampVal),
33                SkTPin(input.fB, 0.f, clampVal),
34                clampedAlpha};
35    }
36}
37
38@test(d) {
39    return GrClampFragmentProcessor::Make(d->inputFP(), d->fRandom->nextBool());
40}
41