1 /* 2 * Copyright 2012 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 GrProcessor_DEFINED 9 #define GrProcessor_DEFINED 10 11 #include "include/core/SkMath.h" 12 #include "include/core/SkString.h" 13 #include "src/gpu/GrColor.h" 14 #include "src/gpu/GrGpuBuffer.h" 15 #include "src/gpu/GrProcessorUnitTest.h" 16 #include "src/gpu/GrSamplerState.h" 17 #include "src/gpu/GrShaderVar.h" 18 #include "src/gpu/GrSurfaceProxyPriv.h" 19 #include "src/gpu/GrTextureProxy.h" 20 21 class GrContext; 22 class GrResourceProvider; 23 24 /** 25 * Used by processors to build their keys. It incorporates each per-processor key into a larger 26 * shader key. 27 */ 28 class GrProcessorKeyBuilder { 29 public: GrProcessorKeyBuilder(SkTArray<unsigned char,true> * data)30 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) { 31 SkASSERT(0 == fData->count() % sizeof(uint32_t)); 32 } 33 add32(uint32_t v)34 void add32(uint32_t v) { 35 ++fCount; 36 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); 37 } 38 39 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next 40 add*() call. */ add32n(int count)41 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { 42 SkASSERT(count > 0); 43 fCount += count; 44 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); 45 } 46 size()47 size_t size() const { return sizeof(uint32_t) * fCount; } 48 49 private: 50 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. 51 int fCount; // number of uint32_ts added to fData by the processor. 52 }; 53 54 /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be 55 immutable: after being constructed, their fields may not change. 56 57 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an 58 processor must reach 0 before the thread terminates and the pool is destroyed. 59 */ 60 class GrProcessor { 61 public: 62 enum ClassID { 63 kBigKeyProcessor_ClassID, 64 kBlockInputFragmentProcessor_ClassID, 65 kButtCapStrokedCircleGeometryProcessor_ClassID, 66 kCircleGeometryProcessor_ClassID, 67 kCircularRRectEffect_ClassID, 68 kClockwiseTestProcessor_ClassID, 69 kColorTableEffect_ClassID, 70 kComposeOneFragmentProcessor_ClassID, 71 kComposeTwoFragmentProcessor_ClassID, 72 kCoverageSetOpXP_ClassID, 73 kCubicStrokeProcessor_ClassID, 74 kCustomXP_ClassID, 75 kDashingCircleEffect_ClassID, 76 kDashingLineEffect_ClassID, 77 kDefaultGeoProc_ClassID, 78 kDIEllipseGeometryProcessor_ClassID, 79 kDisableColorXP_ClassID, 80 kTwoPointConicalEffect_ClassID, 81 kEllipseGeometryProcessor_ClassID, 82 kEllipticalRRectEffect_ClassID, 83 kGP_ClassID, 84 kVertexColorSpaceBenchGP_ClassID, 85 kGrAARectEffect_ClassID, 86 kGrAlphaThresholdFragmentProcessor_ClassID, 87 kGrArithmeticFP_ClassID, 88 kGrBicubicEffect_ClassID, 89 kGrBitmapTextGeoProc_ClassID, 90 kGrBlurredEdgeFragmentProcessor_ClassID, 91 kGrCCClipProcessor_ClassID, 92 kGrCCPathProcessor_ClassID, 93 kGrCircleBlurFragmentProcessor_ClassID, 94 kGrCircleEffect_ClassID, 95 kGrClampedGradientEffect_ClassID, 96 kGrColorMatrixFragmentProcessor_ClassID, 97 kGrColorSpaceXformEffect_ClassID, 98 kGrComposeLerpEffect_ClassID, 99 kGrComposeLerpRedEffect_ClassID, 100 kGrConfigConversionEffect_ClassID, 101 kGrConicEffect_ClassID, 102 kGrConstColorProcessor_ClassID, 103 kGrConvexPolyEffect_ClassID, 104 kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID, 105 kGrDiffuseLightingEffect_ClassID, 106 kGrDisplacementMapEffect_ClassID, 107 kGrDistanceFieldA8TextGeoProc_ClassID, 108 kGrDistanceFieldLCDTextGeoProc_ClassID, 109 kGrDistanceFieldPathGeoProc_ClassID, 110 kGrDitherEffect_ClassID, 111 kGrDualIntervalGradientColorizer_ClassID, 112 kGrEllipseEffect_ClassID, 113 kGrFillRRectOp_Processor_ClassID, 114 kGrGaussianConvolutionFragmentProcessor_ClassID, 115 kGrGSCoverageProcessor_ClassID, 116 kGrImprovedPerlinNoiseEffect_ClassID, 117 kGrIncrDecrWindingCountXP_ClassID, 118 kGrLightingEffect_ClassID, 119 kGrLinearGradient_ClassID, 120 kGrLinearGradientLayout_ClassID, 121 kGrLumaColorFilterEffect_ClassID, 122 kGrMagnifierEffect_ClassID, 123 kGrMatrixConvolutionEffect_ClassID, 124 kGrMeshTestProcessor_ClassID, 125 kGrMorphologyEffect_ClassID, 126 kGrMixerEffect_ClassID, 127 kGrOverdrawFragmentProcessor_ClassID, 128 kGrOverrideInputFragmentProcessor_ClassID, 129 kGrPathProcessor_ClassID, 130 kGrPerlinNoise2Effect_ClassID, 131 kGrPipelineDynamicStateTestProcessor_ClassID, 132 kGrPremulInputFragmentProcessor_ClassID, 133 kGrQuadEffect_ClassID, 134 kGrRadialGradient_ClassID, 135 kGrRadialGradientLayout_ClassID, 136 kGrRectBlurEffect_ClassID, 137 kGrRRectBlurEffect_ClassID, 138 kGrRRectShadowGeoProc_ClassID, 139 kGrResolveWindingCountXP_ClassID, 140 kGrSimpleTextureEffect_ClassID, 141 kGrSingleIntervalGradientColorizer_ClassID, 142 kGrSkSLFP_ClassID, 143 kGrSpecularLightingEffect_ClassID, 144 kGrSRGBEffect_ClassID, 145 kGrSampleMaskProcessor_ClassID, 146 kGrSweepGradient_ClassID, 147 kGrSweepGradientLayout_ClassID, 148 kGrTextureDomainEffect_ClassID, 149 kGrTextureGradientColorizer_ClassID, 150 kGrTiledGradientEffect_ClassID, 151 kGrTwoPointConicalGradientLayout_ClassID, 152 kGrUnpremulInputFragmentProcessor_ClassID, 153 kGrUnrolledBinaryGradientColorizer_ClassID, 154 kGrVSCoverageProcessor_ClassID, 155 kGrYUVtoRGBEffect_ClassID, 156 kHighContrastFilterEffect_ClassID, 157 kInstanceProcessor_ClassID, 158 kLatticeGP_ClassID, 159 kLumaColorFilterEffect_ClassID, 160 kMSAAQuadProcessor_ClassID, 161 kPDLCDXferProcessor_ClassID, 162 kPorterDuffXferProcessor_ClassID, 163 kPremulFragmentProcessor_ClassID, 164 kQuadEdgeEffect_ClassID, 165 kQuadPerEdgeAAGeometryProcessor_ClassID, 166 kReplaceInputFragmentProcessor_ClassID, 167 kRRectsGaussianEdgeFP_ClassID, 168 kSampleLocationsTestProcessor_ClassID, 169 kSeriesFragmentProcessor_ClassID, 170 kShaderPDXferProcessor_ClassID, 171 kStencilResolveProcessor_ClassID, 172 kFwidthSquircleTestProcessor_ClassID, 173 kSwizzleFragmentProcessor_ClassID, 174 kTestFP_ClassID, 175 kTextureGeometryProcessor_ClassID, 176 kFlatNormalsFP_ClassID, 177 kMappedNormalsFP_ClassID, 178 kLightingFP_ClassID, 179 kLinearStrokeProcessor_ClassID, 180 }; 181 182 virtual ~GrProcessor() = default; 183 184 /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader 185 code. */ 186 virtual const char* name() const = 0; 187 188 /** Human-readable dump of all information */ 189 #ifdef SK_DEBUG dumpInfo()190 virtual SkString dumpInfo() const { 191 SkString str; 192 str.appendf("Missing data"); 193 return str; 194 } 195 #else dumpInfo()196 SkString dumpInfo() const { return SkString("<Processor information unavailable>"); } 197 #endif 198 199 /** 200 * Custom shader features provided by the framework. These require special handling when 201 * preparing shaders, so a processor must call setWillUseCustomFeature() from its constructor if 202 * it intends to use one. 203 */ 204 enum class CustomFeatures { 205 kNone = 0, 206 kSampleLocations = 1 << 0, 207 }; 208 209 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(CustomFeatures); 210 requestedFeatures()211 CustomFeatures requestedFeatures() const { return fRequestedFeatures; } 212 213 void* operator new(size_t size); 214 void operator delete(void* target); 215 new(size_t size,void * placement)216 void* operator new(size_t size, void* placement) { 217 return ::operator new(size, placement); 218 } delete(void * target,void * placement)219 void operator delete(void* target, void* placement) { 220 ::operator delete(target, placement); 221 } 222 223 /** Helper for down-casting to a GrProcessor subclass */ cast()224 template <typename T> const T& cast() const { return *static_cast<const T*>(this); } 225 classID()226 ClassID classID() const { return fClassID; } 227 228 protected: GrProcessor(ClassID classID)229 GrProcessor(ClassID classID) : fClassID(classID) {} 230 GrProcessor(const GrProcessor&) = delete; 231 GrProcessor& operator=(const GrProcessor&) = delete; 232 setWillUseCustomFeature(CustomFeatures feature)233 void setWillUseCustomFeature(CustomFeatures feature) { fRequestedFeatures |= feature; } resetCustomFeatures()234 void resetCustomFeatures() { fRequestedFeatures = CustomFeatures::kNone; } 235 236 const ClassID fClassID; 237 CustomFeatures fRequestedFeatures = CustomFeatures::kNone; 238 }; 239 240 GR_MAKE_BITFIELD_CLASS_OPS(GrProcessor::CustomFeatures); 241 242 #endif 243