1 /* 2 * Copyright 2022 Google LLC 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 skgpu_graphite_FactoryFunctions_DEFINED 9 #define skgpu_graphite_FactoryFunctions_DEFINED 10 11 #include "include/core/SkBlendMode.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkSpan.h" 14 #include "include/effects/SkRuntimeEffect.h" 15 16 namespace skgpu::graphite { 17 18 class PrecompileBase; 19 class PrecompileBlender; 20 class PrecompileColorFilter; 21 class PrecompileImageFilter; 22 class PrecompileMaskFilter; 23 class PrecompileShader; 24 25 // All of these factory functions will be moved elsewhere once the pre-compile API becomes public 26 27 //-------------------------------------------------------------------------------------------------- 28 // This will move to be beside SkShaders in include/core/SkShader.h 29 class PrecompileShaders { 30 public: 31 //TODO: Add Empty? - see skbug.com/12165 32 static sk_sp<PrecompileShader> Color(); 33 static sk_sp<PrecompileShader> Blend(SkSpan<const sk_sp<PrecompileBlender>> blenders, 34 SkSpan<const sk_sp<PrecompileShader>> dsts, 35 SkSpan<const sk_sp<PrecompileShader>> srcs); 36 static sk_sp<PrecompileShader> Blend(SkSpan<SkBlendMode> blendModes, 37 SkSpan<const sk_sp<PrecompileShader>> dsts, 38 SkSpan<const sk_sp<PrecompileShader>> srcs); 39 // TODO: add an SkShaders::Image to match this and SkImageFilters (skbug.com/13440) 40 static sk_sp<PrecompileShader> Image(); 41 42 // TODO: make SkGradientShader match this convention (skbug.com/13438) 43 static sk_sp<PrecompileShader> LinearGradient(); 44 static sk_sp<PrecompileShader> RadialGradient(); 45 static sk_sp<PrecompileShader> TwoPointConicalGradient(); 46 static sk_sp<PrecompileShader> SweepGradient(); 47 48 // TODO: hide these? The issue here is that, in the main Skia API, these are only accessed 49 // via makeWithLocalMatrix and makeWithColorFilter. However, in the combination API, clients 50 // may want to create a set of these (i.e., pass SkSpans to the factory functions vs 51 // just single options). 52 static sk_sp<PrecompileShader> LocalMatrix(sk_sp<PrecompileShader> wrapped); 53 static sk_sp<PrecompileShader> ColorFilter(sk_sp<PrecompileShader>, 54 sk_sp<PrecompileColorFilter>); 55 56 private: 57 PrecompileShaders() = delete; 58 }; 59 60 //-------------------------------------------------------------------------------------------------- 61 // Initially this will go next to SkMaskFilter in include/core/SkMaskFilter.h but the 62 // SkMaskFilter::MakeBlur factory should be split out or removed. This namespace will follow 63 // where ever that factory goes. 64 class PrecompileMaskFilters { 65 public: 66 // TODO: change SkMaskFilter::MakeBlur to match this and SkImageFilters::Blur (skbug.com/13441) 67 static sk_sp<PrecompileMaskFilter> Blur(); 68 69 private: 70 PrecompileMaskFilters() = delete; 71 }; 72 73 //-------------------------------------------------------------------------------------------------- 74 // This will move to be beside SkColorFilters in include/core/SkColorFilter.h 75 class PrecompileColorFilters { 76 public: 77 // This encompasses both variants of SkColorFilters::Blend 78 static sk_sp<PrecompileColorFilter> Blend(); 79 80 // This encompasses both variants of SkColorFilters::Matrix 81 static sk_sp<PrecompileColorFilter> Matrix(); 82 83 // This encompasses both variants of SkColorFilters::HSLAMatrix 84 static sk_sp<PrecompileColorFilter> HSLAMatrix(); 85 86 // TODO: Compose, LinearToSRGBGamma/SRGBToLinearGamma, Lerp, Table(ARGB), Lighting 87 88 private: 89 PrecompileColorFilters() = delete; 90 }; 91 92 //-------------------------------------------------------------------------------------------------- 93 // This will move to be beside SkImageFilters in include/effects/SkImageFilters.h 94 class PrecompileImageFilters { 95 public: 96 static sk_sp<PrecompileImageFilter> Blur(); 97 static sk_sp<PrecompileImageFilter> Image(); 98 // TODO: AlphaThreshold, Arithmetic, Blend (2 kinds), ColorFilter, Compose, DisplacementMap, 99 // DropShadow, DropShadowOnly, Magnifier, MatrixConvolution, MatrixTransform, Merge, Offset, 100 // Picture, Runtime, Shader, Tile, Dilate, Erode, DistantLitDiffuse, PointLitDiffuse, 101 // SpotLitDiffuse, DistantLitSpecular, PointLitSpecular, SpotLitSpecular 102 103 private: 104 PrecompileImageFilters() = delete; 105 }; 106 107 //-------------------------------------------------------------------------------------------------- 108 // Object that allows passing a SkPrecompileShader, SkPrecompileColorFilter or 109 // SkPrecompileBlender as a child 110 // 111 // This will moved to be on SkRuntimeEffect 112 class PrecompileChildPtr { 113 public: 114 PrecompileChildPtr() = default; 115 PrecompileChildPtr(sk_sp<PrecompileShader>); 116 PrecompileChildPtr(sk_sp<PrecompileColorFilter>); 117 PrecompileChildPtr(sk_sp<PrecompileBlender>); 118 119 // Asserts that the SkPrecompileBase is either null, or one of the legal derived types 120 PrecompileChildPtr(sk_sp<PrecompileBase>); 121 122 std::optional<SkRuntimeEffect::ChildType> type() const; 123 124 PrecompileShader* shader() const; 125 PrecompileColorFilter* colorFilter() const; 126 PrecompileBlender* blender() const; base()127 PrecompileBase* base() const { return fChild.get(); } 128 129 private: 130 sk_sp<PrecompileBase> fChild; 131 }; 132 133 using PrecompileChildOptions = SkSpan<const PrecompileChildPtr>; 134 135 // TODO: the precompile RuntimeEffects are handling their child options different from the 136 // rest of the precompile system! 137 138 // These will move to be on SkRuntimeEffect to parallel makeShader, makeColorFilter and 139 // makeBlender 140 sk_sp<PrecompileShader> MakePrecompileShader( 141 sk_sp<SkRuntimeEffect> effect, 142 SkSpan<const PrecompileChildOptions> childOptions = {}); 143 144 sk_sp<PrecompileColorFilter> MakePrecompileColorFilter( 145 sk_sp<SkRuntimeEffect> effect, 146 SkSpan<const PrecompileChildOptions> childOptions = {}); 147 148 sk_sp<PrecompileBlender> MakePrecompileBlender( 149 sk_sp<SkRuntimeEffect> effect, 150 SkSpan<const PrecompileChildOptions> childOptions = {}); 151 152 } // namespace skgpu::graphite 153 154 #endif // skgpu_graphite_FactoryFunctions_DEFINED 155