1 /* 2 * Copyright 2021 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_DrawContext_DEFINED 9 #define skgpu_graphite_DrawContext_DEFINED 10 11 #include "include/core/SkImageInfo.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkSurfaceProps.h" 14 15 #include "src/gpu/graphite/AttachmentTypes.h" 16 #include "src/gpu/graphite/DrawList.h" 17 #include "src/gpu/graphite/DrawOrder.h" 18 #include "src/gpu/graphite/DrawTypes.h" 19 #include "src/gpu/graphite/UploadTask.h" 20 21 #include <vector> 22 23 #ifdef SK_ENABLE_PIET_GPU 24 #include "src/gpu/graphite/PietRenderTask.h" 25 namespace skgpu::piet { 26 class Scene; 27 } 28 #endif 29 30 class SkPixmap; 31 32 namespace skgpu::graphite { 33 34 class Geometry; 35 class Recorder; 36 class Transform; 37 38 class AtlasManager; 39 class Caps; 40 class DrawPass; 41 class Task; 42 class TextureProxy; 43 class TextureProxyView; 44 45 /** 46 * DrawContext records draw commands into a specific Surface, via a general task graph 47 * representing GPU work and their inter-dependencies. 48 */ 49 class DrawContext final : public SkRefCnt { 50 public: 51 static sk_sp<DrawContext> Make(sk_sp<TextureProxy> target, 52 SkISize deviceSize, 53 const SkColorInfo&, 54 const SkSurfaceProps&); 55 56 ~DrawContext() override; 57 imageInfo()58 const SkImageInfo& imageInfo() const { return fImageInfo; } colorInfo()59 const SkColorInfo& colorInfo() const { return fImageInfo.colorInfo(); } target()60 TextureProxy* target() { return fTarget.get(); } target()61 const TextureProxy* target() const { return fTarget.get(); } 62 63 TextureProxyView readSurfaceView(const Caps*); 64 surfaceProps()65 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; } 66 pendingDrawCount()67 int pendingDrawCount() const { return fPendingDraws->drawCount(); } 68 69 void clear(const SkColor4f& clearColor); 70 71 void recordDraw(const Renderer* renderer, 72 const Transform& localToDevice, 73 const Geometry& geometry, 74 const Clip& clip, 75 DrawOrder ordering, 76 const PaintParams* paint, 77 const StrokeStyle* stroke); 78 79 bool recordTextUploads(AtlasManager*); 80 bool recordUpload(Recorder* recorder, 81 sk_sp<TextureProxy> targetProxy, 82 const SkColorInfo& srcColorInfo, 83 const SkColorInfo& dstColorInfo, 84 const std::vector<MipLevel>& levels, 85 const SkIRect& dstRect, 86 std::unique_ptr<ConditionalUploadContext>); 87 88 #ifdef SK_ENABLE_PIET_GPU 89 bool recordPietSceneRender(Recorder* recorder, 90 sk_sp<TextureProxy> targetProxy, 91 sk_sp<const skgpu::piet::Scene> pietScene); 92 #endif 93 94 // Ends the current DrawList being accumulated by the SDC, converting it into an optimized and 95 // immutable DrawPass. The DrawPass will be ordered after any other snapped DrawPasses or 96 // appended DrawPasses from a child SDC. A new DrawList is started to record subsequent drawing 97 // operations. 98 // 99 // TBD - Should this take a special occluder list to filter the DrawList? 100 // TBD - should this also return the task so the caller can point to it with its own 101 // dependencies? Or will that be mostly automatic based on draws and proxy refs? 102 void snapDrawPass(Recorder*); 103 104 // TBD: snapRenderPassTask() might not need to be public, and could be spec'ed to require that 105 // snapDrawPass() must have been called first. A lot of it will depend on how the task graph is 106 // managed. 107 108 // Ends the current DrawList if needed, as in 'snapDrawPass', and moves the new DrawPass and all 109 // prior accumulated DrawPasses into a RenderPassTask that can be drawn and depended on. The 110 // caller is responsible for configuring the returned Tasks's dependencies. 111 // 112 // Returns null if there are no pending commands or draw passes to move into a task. 113 sk_sp<Task> snapRenderPassTask(Recorder*); 114 115 // Ends the current UploadList if needed, and moves the accumulated Uploads into an UploadTask 116 // that can be drawn and depended on. The caller is responsible for configuring the returned 117 // Tasks's dependencies. 118 // 119 // Returns null if there are no pending uploads to move into a task. 120 // 121 // TODO: see if we can merge transfers into this 122 sk_sp<Task> snapUploadTask(Recorder*); 123 124 #ifdef SK_ENABLE_PIET_GPU 125 sk_sp<Task> snapPietRenderTask(Recorder*); 126 #endif 127 128 private: 129 DrawContext(sk_sp<TextureProxy>, const SkImageInfo&, const SkSurfaceProps&); 130 131 sk_sp<TextureProxy> fTarget; 132 SkImageInfo fImageInfo; 133 const SkSurfaceProps fSurfaceProps; 134 135 // Stores the most immediately recorded draws into the SDC's surface. This list is mutable and 136 // can be appended to, or have its commands rewritten if they are inlined into a parent SDC. 137 std::unique_ptr<DrawList> fPendingDraws; 138 // Load and store information for the current pending draws. 139 LoadOp fPendingLoadOp = LoadOp::kLoad; 140 StoreOp fPendingStoreOp = StoreOp::kStore; 141 std::array<float, 4> fPendingClearColor = { 0, 0, 0, 0 }; 142 143 // Stores previously snapped DrawPasses of this DC, or inlined child DCs whose content 144 // couldn't have been copied directly to fPendingDraws. While each DrawPass is immutable, the 145 // list of DrawPasses is not final until there is an external dependency on the SDC's content 146 // that requires it to be resolved as its own render pass (vs. inlining the SDC's passes into a 147 // parent's render pass). 148 // TODO: It will be easier to debug/understand the DrawPass structure of a context if 149 // consecutive DrawPasses to the same target are stored in a DrawPassChain. A DrawContext with 150 // multiple DrawPassChains is then clearly accumulating subpasses across multiple targets. 151 std::vector<std::unique_ptr<DrawPass>> fDrawPasses; 152 153 // Stores the most immediately recorded uploads into Textures. This list is mutable and 154 // can be appended to, or have its commands rewritten if they are inlined into a parent DC. 155 std::unique_ptr<UploadList> fPendingUploads; 156 157 #ifdef SK_ENABLE_PIET_GPU 158 std::vector<PietRenderInstance> fPendingPietRenders; 159 #endif 160 }; 161 162 } // namespace skgpu::graphite 163 164 #endif // skgpu_graphite_DrawContext_DEFINED 165