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_RendererProvider_DEFINED 9 #define skgpu_graphite_RendererProvider_DEFINED 10 11 #include "include/core/SkPathTypes.h" 12 #include "include/core/SkVertices.h" 13 #include "src/gpu/graphite/Renderer.h" 14 15 #include <vector> 16 17 namespace skgpu::graphite { 18 19 class Caps; 20 class StaticBufferManager; 21 22 /** 23 * Graphite defines a limited set of renderers in order to increase the likelihood of batching 24 * across draw calls, and reducing the number of shader permutations required. These Renderers are 25 * stateless singletons and remain alive for the life of the Context and its Recorders. 26 * 27 * Because Renderers are immutable and the defined Renderers are created at context initialization, 28 * RendererProvider is trivially thread-safe. 29 */ 30 class RendererProvider { 31 public: 32 // TODO: Add configuration options to disable "optimization" renderers in favor of the more 33 // general case, or renderers that won't be used by the application. When that's added, these 34 // functions could return null. 35 36 // Path rendering for fills and strokes stencilTessellatedCurvesAndTris(SkPathFillType type)37 const Renderer* stencilTessellatedCurvesAndTris(SkPathFillType type) const { 38 return &fStencilTessellatedCurves[(int) type]; 39 } stencilTessellatedWedges(SkPathFillType type)40 const Renderer* stencilTessellatedWedges(SkPathFillType type) const { 41 return &fStencilTessellatedWedges[(int) type]; 42 } convexTessellatedWedges()43 const Renderer* convexTessellatedWedges() const { return &fConvexTessellatedWedges; } tessellatedStrokes()44 const Renderer* tessellatedStrokes() const { return &fTessellatedStrokes; } 45 46 // Atlas'ed text rendering bitmapText()47 const Renderer* bitmapText() const { return &fBitmapText; } sdfText(bool useLCDText)48 const Renderer* sdfText(bool useLCDText) const { return &fSDFText[useLCDText]; } 49 50 // Mesh rendering vertices(SkVertices::VertexMode mode,bool hasColors,bool hasTexCoords)51 const Renderer* vertices(SkVertices::VertexMode mode, bool hasColors, bool hasTexCoords) const { 52 SkASSERT(mode != SkVertices::kTriangleFan_VertexMode); // Should be converted to kTriangles 53 bool triStrip = mode == SkVertices::kTriangleStrip_VertexMode; 54 return &fVertices[4*triStrip + 2*hasColors + hasTexCoords]; 55 } 56 57 // Filled and stroked [r]rects and per-edge AA quadrilaterals analyticRRect()58 const Renderer* analyticRRect() const { return &fAnalyticRRect; } 59 60 // TODO: May need to add support for inverse filled strokes (need to check SVG spec if this is a 61 // real thing). 62 63 // Iterate over all available Renderers to combine with specified paint combinations when 64 // pre-compiling pipelines. renderers()65 SkSpan<const Renderer* const> renderers() const { 66 return {fRenderers.data(), fRenderers.size()}; 67 } 68 69 const RenderStep* lookup(uint32_t uniqueID) const; 70 71 private: 72 static constexpr int kPathTypeCount = 4; 73 static constexpr int kVerticesCount = 8; // 2 modes * 2 color configs * 2 tex coord configs 74 75 friend class Context; // for ctor 76 77 // TODO: Take in caps that determines which Renderers to use for each category 78 RendererProvider(const Caps*, StaticBufferManager* bufferManager); 79 80 // Cannot be moved or copied 81 RendererProvider(const RendererProvider&) = delete; 82 RendererProvider(RendererProvider&&) = delete; 83 84 // Renderers are composed of 1+ steps, and some steps can be shared by multiple Renderers. 85 // Renderers don't keep their RenderSteps alive so RendererProvider holds them here. 86 std::vector<std::unique_ptr<RenderStep>> fRenderSteps; 87 88 // NOTE: Keep all Renderers dense to support automatically completing 'fRenderers'. 89 Renderer fStencilTessellatedCurves[kPathTypeCount]; 90 Renderer fStencilTessellatedWedges[kPathTypeCount]; 91 Renderer fConvexTessellatedWedges; 92 Renderer fTessellatedStrokes; 93 94 Renderer fBitmapText; 95 Renderer fSDFText[2]; // bool isLCD 96 97 Renderer fAnalyticRRect; 98 99 Renderer fVertices[kVerticesCount]; 100 101 // Aggregate of all enabled Renderers for convenient iteration when pre-compiling 102 std::vector<const Renderer*> fRenderers; 103 }; 104 105 } // namespace skgpu::graphite 106 107 #endif // skgpu_graphite_RendererProvider_DEFINED 108