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 "src/gpu/GrPipeline.h"
9 #include "src/gpu/GrProcessor.h"
10 #include "src/gpu/GrShaderCaps.h"
11 #include "src/gpu/effects/GrDisableColorXP.h"
12 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
14 #include "src/gpu/glsl/GrGLSLXferProcessor.h"
15
16 /**
17 * This xfer processor disables color writing. Thus color and coverage and ignored and no blending
18 * occurs. This XP is usful for things like stenciling.
19 */
20 class DisableColorXP : public GrXferProcessor {
21 public:
DisableColorXP()22 DisableColorXP() : INHERITED(kDisableColorXP_ClassID) {}
23
24 private:
name() const25 const char* name() const override { return "Disable Color"; }
onIsEqual(const GrXferProcessor & xpBase) const26 bool onIsEqual(const GrXferProcessor& xpBase) const override { return true; }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const27 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
28 return; // No key.
29 }
onGetBlendInfo(GrXferProcessor::BlendInfo * blendInfo) const30 void onGetBlendInfo(GrXferProcessor::BlendInfo* blendInfo) const override {
31 blendInfo->fWriteColor = false;
32 }
33 GrGLSLXferProcessor* createGLSLInstance() const override;
34
35 typedef GrXferProcessor INHERITED;
36 };
37
38 class GLDisableColorXP : public GrGLSLXferProcessor {
39 private:
emitOutputsForBlendState(const EmitArgs & args)40 void emitOutputsForBlendState(const EmitArgs& args) override {
41 if (args.fShaderCaps->mustWriteToFragColor()) {
42 // This emit code should be empty. However, on the nexus 6 there is a driver bug where
43 // if you do not give gl_FragColor a value, the gl context is lost and we end up drawing
44 // nothing. So this fix just sets the gl_FragColor arbitrarily to 0.
45 // https://bugs.chromium.org/p/chromium/issues/detail?id=445377
46 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
47 fragBuilder->codeAppendf("%s = half4(0);", args.fOutputPrimary);
48 }
49 }
50
emitOutputSwizzle(GrGLSLXPFragmentBuilder *,const GrSwizzle &,const char *,const char *) const51 void emitOutputSwizzle(
52 GrGLSLXPFragmentBuilder*, const GrSwizzle&, const char*, const char*) const override {
53 // Don't write any swizzling. This makes sure the final shader does not output a color.
54 return;
55 }
56
onSetData(const GrGLSLProgramDataManager &,const GrXferProcessor &)57 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) override {}
58
59 typedef GrGLSLXferProcessor INHERITED;
60 };
61
createGLSLInstance() const62 GrGLSLXferProcessor* DisableColorXP::createGLSLInstance() const {
63 return new GLDisableColorXP();
64 }
65
MakeXferProcessor()66 sk_sp<const GrXferProcessor> GrDisableColorXPFactory::MakeXferProcessor() {
67 return sk_make_sp<DisableColorXP>();
68 }
69
70 GR_DEFINE_XP_FACTORY_TEST(GrDisableColorXPFactory);
71
72 #if GR_TEST_UTILS
TestGet(GrProcessorTestData *)73 const GrXPFactory* GrDisableColorXPFactory::TestGet(GrProcessorTestData*) {
74 return GrDisableColorXPFactory::Get();
75 }
76 #endif
77