1 /*
2 * Copyright 2014 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 "effects/GrDisableColorXP.h"
9 #include "GrPipeline.h"
10 #include "GrProcessor.h"
11 #include "glsl/GrGLSLFragmentShaderBuilder.h"
12 #include "glsl/GrGLSLProgramDataManager.h"
13 #include "glsl/GrGLSLXferProcessor.h"
14
15 /**
16 * This xfer processor disables color writing. Thus color and coverage and ignored and no blending
17 * occurs. This XP is usful for things like stenciling.
18 */
19 class DisableColorXP : public GrXferProcessor {
20 public:
DisableColorXP()21 DisableColorXP()
22 : INHERITED(kDisableColorXP_ClassID) {}
23
name() const24 const char* name() const override { return "Disable Color"; }
25
26 GrGLSLXferProcessor* createGLSLInstance() const override;
27
28 private:
29
30 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
31
32 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override;
33
onIsEqual(const GrXferProcessor & xpBase) const34 bool onIsEqual(const GrXferProcessor& xpBase) const override {
35 return true;
36 }
37
38 typedef GrXferProcessor INHERITED;
39 };
40
41 ///////////////////////////////////////////////////////////////////////////////
42
43 class GLDisableColorXP : public GrGLSLXferProcessor {
44 public:
GLDisableColorXP(const GrProcessor &)45 GLDisableColorXP(const GrProcessor&) {}
46
~GLDisableColorXP()47 ~GLDisableColorXP() override {}
48
GenKey(const GrProcessor &,const GrShaderCaps &,GrProcessorKeyBuilder *)49 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
50
51 private:
emitOutputsForBlendState(const EmitArgs & args)52 void emitOutputsForBlendState(const EmitArgs& args) override {
53 // This emit code should be empty. However, on the nexus 6 there is a driver bug where if
54 // you do not give gl_FragColor a value, the gl context is lost and we end up drawing
55 // nothing. So this fix just sets the gl_FragColor arbitrarily to 0.
56 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
57 fragBuilder->codeAppendf("%s = half4(0);", args.fOutputPrimary);
58 }
59
onSetData(const GrGLSLProgramDataManager &,const GrXferProcessor &)60 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
61
62 typedef GrGLSLXferProcessor INHERITED;
63 };
64
65 ///////////////////////////////////////////////////////////////////////////////
66
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const67 void DisableColorXP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
68 GLDisableColorXP::GenKey(*this, caps, b);
69 }
70
createGLSLInstance() const71 GrGLSLXferProcessor* DisableColorXP::createGLSLInstance() const { return new GLDisableColorXP(*this); }
72
onGetBlendInfo(GrXferProcessor::BlendInfo * blendInfo) const73 void DisableColorXP::onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const {
74 blendInfo->fWriteColor = false;
75 }
76
77 ///////////////////////////////////////////////////////////////////////////////
makeXferProcessor(const GrProcessorAnalysisColor &,GrProcessorAnalysisCoverage,bool hasMixedSamples,const GrCaps & caps,GrClampType clampType) const78 sk_sp<const GrXferProcessor> GrDisableColorXPFactory::makeXferProcessor(
79 const GrProcessorAnalysisColor&,
80 GrProcessorAnalysisCoverage,
81 bool hasMixedSamples,
82 const GrCaps& caps,
83 GrClampType clampType) const {
84 return sk_sp<const GrXferProcessor>(new DisableColorXP);
85 }
86
87 GR_DEFINE_XP_FACTORY_TEST(GrDisableColorXPFactory);
88
89 #if GR_TEST_UTILS
TestGet(GrProcessorTestData *)90 const GrXPFactory* GrDisableColorXPFactory::TestGet(GrProcessorTestData*) {
91 return GrDisableColorXPFactory::Get();
92 }
93 #endif
94