GrDisableColorXP.cpp 2.7 KB

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