1 | /* |
2 | * Copyright 2016 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 | #include "include/core/SkColorSpace.h" |
9 | #include "src/core/SkColorSpacePriv.h" |
10 | #include "src/gpu/GrColorSpaceXform.h" |
11 | #include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h" |
12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
14 | |
15 | sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(SkColorSpace* src, SkAlphaType srcAT, |
16 | SkColorSpace* dst, SkAlphaType dstAT) { |
17 | SkColorSpaceXformSteps steps(src, srcAT, dst, dstAT); |
18 | return steps.flags.mask() == 0 ? nullptr /* Noop transform */ |
19 | : sk_make_sp<GrColorSpaceXform>(steps); |
20 | } |
21 | |
22 | bool GrColorSpaceXform::Equals(const GrColorSpaceXform* a, const GrColorSpaceXform* b) { |
23 | if (a == b) { |
24 | return true; |
25 | } |
26 | |
27 | if (!a || !b || a->fSteps.flags.mask() != b->fSteps.flags.mask()) { |
28 | return false; |
29 | } |
30 | |
31 | if (a->fSteps.flags.linearize && |
32 | 0 != memcmp(&a->fSteps.srcTF, &b->fSteps.srcTF, sizeof(a->fSteps.srcTF))) { |
33 | return false; |
34 | } |
35 | |
36 | if (a->fSteps.flags.gamut_transform && |
37 | 0 != memcmp(&a->fSteps.src_to_dst_matrix, &b->fSteps.src_to_dst_matrix, |
38 | sizeof(a->fSteps.src_to_dst_matrix))) { |
39 | return false; |
40 | } |
41 | |
42 | if (a->fSteps.flags.encode && |
43 | 0 != memcmp(&a->fSteps.dstTFInv, &b->fSteps.dstTFInv, sizeof(a->fSteps.dstTFInv))) { |
44 | return false; |
45 | } |
46 | |
47 | return true; |
48 | } |
49 | |
50 | SkColor4f GrColorSpaceXform::apply(const SkColor4f& srcColor) { |
51 | SkColor4f result = srcColor; |
52 | fSteps.apply(result.vec()); |
53 | return result; |
54 | } |
55 | |
56 | ////////////////////////////////////////////////////////////////////////////// |
57 | |
58 | class GrGLColorSpaceXformEffect : public GrGLSLFragmentProcessor { |
59 | public: |
60 | void emitCode(EmitArgs& args) override { |
61 | const GrColorSpaceXformEffect& proc = args.fFp.cast<GrColorSpaceXformEffect>(); |
62 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
63 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
64 | |
65 | fColorSpaceHelper.emitCode(uniformHandler, proc.colorXform()); |
66 | |
67 | SkString childColor = this->invokeChild(0, args); |
68 | |
69 | SkString xformedColor; |
70 | fragBuilder->appendColorGamutXform(&xformedColor, childColor.c_str(), &fColorSpaceHelper); |
71 | fragBuilder->codeAppendf("%s = %s;" , args.fOutputColor, xformedColor.c_str()); |
72 | } |
73 | |
74 | private: |
75 | void onSetData(const GrGLSLProgramDataManager& pdman, |
76 | const GrFragmentProcessor& fp) override { |
77 | const GrColorSpaceXformEffect& proc = fp.cast<GrColorSpaceXformEffect>(); |
78 | fColorSpaceHelper.setData(pdman, proc.colorXform()); |
79 | } |
80 | |
81 | GrGLSLColorSpaceXformHelper fColorSpaceHelper; |
82 | |
83 | typedef GrGLSLFragmentProcessor INHERITED; |
84 | }; |
85 | |
86 | ////////////////////////////////////////////////////////////////////////////// |
87 | |
88 | GrColorSpaceXformEffect::GrColorSpaceXformEffect(std::unique_ptr<GrFragmentProcessor> child, |
89 | sk_sp<GrColorSpaceXform> colorXform) |
90 | : INHERITED(kGrColorSpaceXformEffect_ClassID, OptFlags(child.get())) |
91 | , fColorXform(std::move(colorXform)) { |
92 | this->registerChild(std::move(child)); |
93 | } |
94 | |
95 | GrColorSpaceXformEffect::GrColorSpaceXformEffect(const GrColorSpaceXformEffect& that) |
96 | : INHERITED(kGrColorSpaceXformEffect_ClassID, that.optimizationFlags()) |
97 | , fColorXform(that.fColorXform) { |
98 | this->cloneAndRegisterAllChildProcessors(that); |
99 | } |
100 | |
101 | std::unique_ptr<GrFragmentProcessor> GrColorSpaceXformEffect::clone() const { |
102 | return std::unique_ptr<GrFragmentProcessor>(new GrColorSpaceXformEffect(*this)); |
103 | } |
104 | |
105 | bool GrColorSpaceXformEffect::onIsEqual(const GrFragmentProcessor& s) const { |
106 | const GrColorSpaceXformEffect& other = s.cast<GrColorSpaceXformEffect>(); |
107 | return GrColorSpaceXform::Equals(fColorXform.get(), other.fColorXform.get()); |
108 | } |
109 | |
110 | void GrColorSpaceXformEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
111 | GrProcessorKeyBuilder* b) const { |
112 | b->add32(GrColorSpaceXform::XformKey(fColorXform.get())); |
113 | } |
114 | |
115 | GrGLSLFragmentProcessor* GrColorSpaceXformEffect::onCreateGLSLInstance() const { |
116 | return new GrGLColorSpaceXformEffect(); |
117 | } |
118 | |
119 | GrFragmentProcessor::OptimizationFlags GrColorSpaceXformEffect::OptFlags( |
120 | const GrFragmentProcessor* child) { |
121 | return (child ? ProcessorOptimizationFlags(child) : kAll_OptimizationFlags) & |
122 | (kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
123 | kPreservesOpaqueInput_OptimizationFlag | |
124 | kConstantOutputForConstantInput_OptimizationFlag); |
125 | } |
126 | |
127 | SkPMColor4f GrColorSpaceXformEffect::constantOutputForConstantInput( |
128 | const SkPMColor4f& input) const { |
129 | const auto c0 = ConstantOutputForConstantInput(this->childProcessor(0), input); |
130 | return this->fColorXform->apply(c0.unpremul()).premul(); |
131 | } |
132 | |
133 | std::unique_ptr<GrFragmentProcessor> GrColorSpaceXformEffect::Make( |
134 | std::unique_ptr<GrFragmentProcessor> child, |
135 | SkColorSpace* src, SkAlphaType srcAT, |
136 | SkColorSpace* dst, SkAlphaType dstAT) { |
137 | return Make(std::move(child), GrColorSpaceXform::Make(src, srcAT, dst, dstAT)); |
138 | } |
139 | |
140 | std::unique_ptr<GrFragmentProcessor> GrColorSpaceXformEffect::Make( |
141 | std::unique_ptr<GrFragmentProcessor> child, |
142 | sk_sp<GrColorSpaceXform> colorXform) { |
143 | if (!colorXform) { |
144 | return child; |
145 | } |
146 | |
147 | return std::unique_ptr<GrFragmentProcessor>(new GrColorSpaceXformEffect(std::move(child), |
148 | std::move(colorXform))); |
149 | } |
150 | |