| 1 | /* |
| 2 | * Copyright 2015 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 | #ifndef GrGLSLXferProcessor_DEFINED |
| 9 | #define GrGLSLXferProcessor_DEFINED |
| 10 | |
| 11 | #include "include/core/SkPoint.h" |
| 12 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 13 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 14 | |
| 15 | class GrXferProcessor; |
| 16 | class GrGLSLXPBuilder; |
| 17 | class GrGLSLXPFragmentBuilder; |
| 18 | class GrShaderCaps; |
| 19 | class GrTexture; |
| 20 | |
| 21 | class GrGLSLXferProcessor { |
| 22 | public: |
| 23 | GrGLSLXferProcessor() {} |
| 24 | virtual ~GrGLSLXferProcessor() {} |
| 25 | |
| 26 | using SamplerHandle = GrGLSLUniformHandler::SamplerHandle; |
| 27 | |
| 28 | struct EmitArgs { |
| 29 | EmitArgs(GrGLSLXPFragmentBuilder* fragBuilder, |
| 30 | GrGLSLUniformHandler* uniformHandler, |
| 31 | const GrShaderCaps* caps, |
| 32 | const GrXferProcessor& xp, |
| 33 | const char* inputColor, |
| 34 | const char* inputCoverage, |
| 35 | const char* outputPrimary, |
| 36 | const char* outputSecondary, |
| 37 | const SamplerHandle dstTextureSamplerHandle, |
| 38 | GrSurfaceOrigin dstTextureOrigin, |
| 39 | const GrSwizzle& writeSwizzle) |
| 40 | : fXPFragBuilder(fragBuilder) |
| 41 | , fUniformHandler(uniformHandler) |
| 42 | , fShaderCaps(caps) |
| 43 | , fXP(xp) |
| 44 | , fInputColor(inputColor ? inputColor : "half4(1.0)" ) |
| 45 | , fInputCoverage(inputCoverage) |
| 46 | , fOutputPrimary(outputPrimary) |
| 47 | , fOutputSecondary(outputSecondary) |
| 48 | , fDstTextureSamplerHandle(dstTextureSamplerHandle) |
| 49 | , fDstTextureOrigin(dstTextureOrigin) |
| 50 | , fWriteSwizzle(writeSwizzle) {} |
| 51 | GrGLSLXPFragmentBuilder* fXPFragBuilder; |
| 52 | GrGLSLUniformHandler* fUniformHandler; |
| 53 | const GrShaderCaps* fShaderCaps; |
| 54 | const GrXferProcessor& fXP; |
| 55 | const char* fInputColor; |
| 56 | const char* fInputCoverage; |
| 57 | const char* fOutputPrimary; |
| 58 | const char* fOutputSecondary; |
| 59 | const SamplerHandle fDstTextureSamplerHandle; |
| 60 | GrSurfaceOrigin fDstTextureOrigin; |
| 61 | GrSwizzle fWriteSwizzle; |
| 62 | }; |
| 63 | /** |
| 64 | * This is similar to emitCode() in the base class, except it takes a full shader builder. |
| 65 | * This allows the effect subclass to emit vertex code. |
| 66 | */ |
| 67 | void emitCode(const EmitArgs&); |
| 68 | |
| 69 | /** A GrGLSLXferProcessor instance can be reused with any GrGLSLXferProcessor that produces |
| 70 | the same stage key; this function reads data from a GrGLSLXferProcessor and uploads any |
| 71 | uniform variables required by the shaders created in emitCode(). The GrXferProcessor |
| 72 | parameter is guaranteed to be of the same type that created this GrGLSLXferProcessor and |
| 73 | to have an identical processor key as the one that created this GrGLSLXferProcessor. This |
| 74 | function calls onSetData on the subclass of GrGLSLXferProcessor |
| 75 | */ |
| 76 | void setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp, |
| 77 | const GrTexture* dstTexture, const SkIPoint& dstTextureOffset); |
| 78 | |
| 79 | protected: |
| 80 | static void DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder, |
| 81 | const char* srcCoverage, |
| 82 | const char* dstColor, |
| 83 | const char* outColor, |
| 84 | const char* outColorSecondary, |
| 85 | const GrXferProcessor& proc); |
| 86 | |
| 87 | private: |
| 88 | /** |
| 89 | * Called by emitCode() when the XP will not be performing a dst read. This method is |
| 90 | * responsible for both blending and coverage. A subclass only needs to implement this method if |
| 91 | * it can construct a GrXferProcessor that will not read the dst color. |
| 92 | */ |
| 93 | virtual void emitOutputsForBlendState(const EmitArgs&) { |
| 94 | SK_ABORT("emitOutputsForBlendState not implemented." ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Called by emitCode() when the XP will perform a dst read. This method only needs to supply |
| 99 | * the blending logic. The base class applies coverage. A subclass only needs to implement this |
| 100 | * method if it can construct a GrXferProcessor that reads the dst color. |
| 101 | */ |
| 102 | virtual void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder*, |
| 103 | GrGLSLUniformHandler*, |
| 104 | const char* srcColor, |
| 105 | const char* srcCoverage, |
| 106 | const char* dstColor, |
| 107 | const char* outColor, |
| 108 | const char* outColorSecondary, |
| 109 | const GrXferProcessor&) { |
| 110 | SK_ABORT("emitBlendCodeForDstRead not implemented." ); |
| 111 | } |
| 112 | |
| 113 | virtual void emitWriteSwizzle(GrGLSLXPFragmentBuilder*, |
| 114 | const GrSwizzle&, |
| 115 | const char* outColor, |
| 116 | const char* outColorSecondary) const; |
| 117 | |
| 118 | virtual void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) = 0; |
| 119 | |
| 120 | GrGLSLProgramDataManager::UniformHandle fDstTopLeftUni; |
| 121 | GrGLSLProgramDataManager::UniformHandle fDstScaleUni; |
| 122 | }; |
| 123 | #endif |
| 124 | |