1 /* 2 * Copyright 2017 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 GrTextureEffect_DEFINED 9 #define GrTextureEffect_DEFINED 10 11 #include "include/core/SkImageInfo.h" 12 #include "include/core/SkMatrix.h" 13 #include "src/gpu/GrFragmentProcessor.h" 14 #include "src/gpu/GrSurfaceProxyView.h" 15 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" 16 17 class GrTextureEffect : public GrFragmentProcessor { 18 public: 19 inline static constexpr float kDefaultBorder[4] = {0}; 20 21 /** Make from a filter. The sampler will be configured with clamp mode. */ 22 static std::unique_ptr<GrFragmentProcessor> Make( 23 GrSurfaceProxyView, 24 SkAlphaType, 25 const SkMatrix& = SkMatrix::I(), 26 GrSamplerState::Filter = GrSamplerState::Filter::kNearest, 27 GrSamplerState::MipmapMode mipmapMode = GrSamplerState::MipmapMode::kNone); 28 29 /** 30 * Make from a full GrSamplerState. Caps are required to determine support for kClampToBorder. 31 * This will be emulated in the shader if there is no hardware support. 32 */ 33 static std::unique_ptr<GrFragmentProcessor> Make(GrSurfaceProxyView, SkAlphaType, 34 const SkMatrix&, GrSamplerState, 35 const GrCaps& caps, 36 const float border[4] = kDefaultBorder); 37 38 /** 39 * Makes a texture effect that samples a subset of a texture. The wrap modes of the 40 * GrSampleState are applied to the subset in the shader rather than using HW samplers. 41 * The 'subset' parameter specifies the texels in the base level. The shader code will 42 * avoid allowing linear filtering to read outside the texel window. However, if MIP 43 * filtering is used and a shader invocation reads from a level other than the base 44 * then it may read texel values that were computed from in part from base level texels 45 * outside the window. More specifically, we treat the MIP map case exactly like the 46 * linear case in terms of how the final texture coords are computed. If 47 * alwaysUseShaderTileMode is true then MakeSubset won't attempt to use HW wrap modes if the 48 * subset contains the entire texture. 49 */ 50 static std::unique_ptr<GrFragmentProcessor> MakeSubset(GrSurfaceProxyView, 51 SkAlphaType, 52 const SkMatrix&, 53 GrSamplerState, 54 const SkRect& subset, 55 const GrCaps& caps, 56 const float border[4] = kDefaultBorder, 57 bool alwaysUseShaderTileMode = false); 58 59 /** 60 * The same as above but also takes a 'domain' that specifies any known limit on the post- 61 * matrix texture coords that will be used to sample the texture. Specifying this requires 62 * knowledge of how this effect will be nested into a paint, the local coords used with the 63 * draw, etc. It is only used to attempt to optimize away the shader subset calculations. 64 */ 65 static std::unique_ptr<GrFragmentProcessor> MakeSubset(GrSurfaceProxyView, 66 SkAlphaType, 67 const SkMatrix&, 68 GrSamplerState, 69 const SkRect& subset, 70 const SkRect& domain, 71 const GrCaps& caps, 72 const float border[4] = kDefaultBorder); 73 74 /** 75 * Like MakeSubset() but always uses kLinear filtering. MakeSubset() uses the subset rect 76 * dimensions to determine the period of the wrap mode (for repeat and mirror). Once it computes 77 * the wrapped texture coordinate inside subset rect it further clamps it to a 0.5 inset rect of 78 * subset. When subset is an integer rectangle this clamping avoids the hw linear filtering from 79 * reading texels just outside the subset rect. This factory allows a custom inset clamping 80 * distance rather than 0.5, allowing those neighboring texels to influence the linear filtering 81 * sample result. If there is a known restriction on the post-matrix texture coords it can be 82 * specified using domain. 83 */ 84 static std::unique_ptr<GrFragmentProcessor> MakeCustomLinearFilterInset( 85 GrSurfaceProxyView, 86 SkAlphaType, 87 const SkMatrix&, 88 GrSamplerState::WrapMode wx, 89 GrSamplerState::WrapMode wy, 90 const SkRect& subset, 91 const SkRect* domain, 92 SkVector inset, 93 const GrCaps& caps, 94 const float border[4] = kDefaultBorder); 95 96 std::unique_ptr<GrFragmentProcessor> clone() const override; 97 name()98 const char* name() const override { return "TextureEffect"; } 99 samplerState()100 GrSamplerState samplerState() const { return fSamplerState; } 101 texture()102 GrTexture* texture() const { return fView.asTextureProxy()->peekTexture(); } 103 view()104 const GrSurfaceProxyView& view() const { return fView; } 105 106 // Gets a matrix that is concat'ed by wrapping GrMatrixEffect that handles y-flip and coord 107 // normalization if required. This matrix is not always known when we make the GrTextureEffect 108 // because of fully-lazy proxies. Hence, this method exists to allow this concat to happen 109 // after proxy instantiation with coordination from GrMatrixEffect. 110 SkMatrix coordAdjustmentMatrix() const; 111 112 class Impl : public ProgramImpl { 113 public: 114 void emitCode(EmitArgs&) override; 115 setSamplerHandle(GrGLSLShaderBuilder::SamplerHandle handle)116 void setSamplerHandle(GrGLSLShaderBuilder::SamplerHandle handle) { 117 fSamplerHandle = handle; 118 } 119 120 private: 121 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; 122 123 UniformHandle fSubsetUni; 124 UniformHandle fClampUni; 125 UniformHandle fIDimsUni; 126 UniformHandle fBorderUni; 127 GrGLSLShaderBuilder::SamplerHandle fSamplerHandle; 128 }; 129 130 private: 131 struct Sampling; 132 133 /** 134 * Possible implementation of wrap mode in shader code. Some modes are specialized by 135 * filter. 136 */ 137 enum class ShaderMode : uint16_t { 138 kNone, // Using HW mode 139 kClamp, // Shader based clamp, no filter specialization 140 kRepeat_Nearest_None, // Simple repeat for nearest sampling, no mipmapping 141 kRepeat_Linear_None, // Filter the subset boundary for kRepeat mode, no mip mapping 142 kRepeat_Linear_Mipmap, // Logic for linear filtering and LOD selection with kRepeat mode. 143 kRepeat_Nearest_Mipmap, // Logic for nearest filtering and LOD selection with kRepeat mode. 144 kMirrorRepeat, // Mirror repeat (doesn't depend on filter)) 145 kClampToBorder_Nearest, // Logic for hard transition to border color when not filtering. 146 kClampToBorder_Filter, // Logic for fading to border color when filtering. 147 }; 148 static ShaderMode GetShaderMode(GrSamplerState::WrapMode, 149 GrSamplerState::Filter, 150 GrSamplerState::MipmapMode); 151 static bool ShaderModeIsClampToBorder(ShaderMode); 152 // To keep things a little simpler, when we have filtering logic in the shader we 153 // operate on unnormalized texture coordinates. We will add a uniform that stores 154 // {1/w, 1/h} in a float2 and normalizes after the mode is handled if the texture 155 // is not rectangle. 156 static bool ShaderModeRequiresUnormCoord(ShaderMode); 157 158 GrSurfaceProxyView fView; 159 GrSamplerState fSamplerState; 160 float fBorder[4]; 161 SkRect fSubset; 162 SkRect fClamp; 163 ShaderMode fShaderModes[2]; 164 165 inline GrTextureEffect(GrSurfaceProxyView, SkAlphaType, const Sampling&); 166 167 explicit GrTextureEffect(const GrTextureEffect& src); 168 169 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override; 170 171 void onAddToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; 172 173 bool onIsEqual(const GrFragmentProcessor&) const override; 174 175 bool matrixEffectShouldNormalize() const; 176 hasClampToBorderShaderMode()177 bool hasClampToBorderShaderMode() const { 178 return ShaderModeIsClampToBorder(fShaderModes[0]) || 179 ShaderModeIsClampToBorder(fShaderModes[1]); 180 } 181 182 GR_DECLARE_FRAGMENT_PROCESSOR_TEST 183 184 using INHERITED = GrFragmentProcessor; 185 }; 186 #endif 187