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_DrawContext_DEFINED 9 #define skgpu_DrawContext_DEFINED 10 11 #include "include/core/SkImageInfo.h" 12 #include "include/core/SkRefCnt.h" 13 14 #include "experimental/graphite/src/DrawList.h" 15 #include "experimental/graphite/src/DrawOrder.h" 16 #include "experimental/graphite/src/DrawTypes.h" 17 #include "experimental/graphite/src/UploadTask.h" 18 19 #include <vector> 20 21 class SkPixmap; 22 23 namespace skgpu { 24 25 class BoundsManager; 26 class Recorder; 27 class Shape; 28 class Transform; 29 30 class DrawPass; 31 class Task; 32 class TextureProxy; 33 34 /** 35 * DrawContext records draw commands into a specific Surface, via a general task graph 36 * representing GPU work and their inter-dependencies. 37 */ 38 class DrawContext final : public SkRefCnt { 39 public: 40 static sk_sp<DrawContext> Make(sk_sp<TextureProxy> target, 41 sk_sp<SkColorSpace> colorSpace, 42 SkColorType colorType, 43 SkAlphaType alphaType); 44 45 ~DrawContext() override; 46 imageInfo()47 const SkImageInfo& imageInfo() const { return fImageInfo; } target()48 TextureProxy* target() { return fTarget.get(); } target()49 const TextureProxy* target() const { return fTarget.get(); } 50 pendingDrawCount()51 int pendingDrawCount() const { return fPendingDraws->drawCount(); } 52 53 // TODO: need color/depth clearing functions (so DCL will probably need those too) 54 55 void clear(const SkColor4f& clearColor); 56 57 void stencilAndFillPath(const Transform& localToDevice, 58 const Shape& shape, 59 const Clip& clip, 60 DrawOrder order, 61 const PaintParams* paint); 62 63 void fillConvexPath(const Transform& localToDevice, 64 const Shape& shape, 65 const Clip& clip, 66 DrawOrder order, 67 const PaintParams* paint); 68 69 void strokePath(const Transform& localToDevice, 70 const Shape& shape, 71 const StrokeParams& stroke, 72 const Clip& clip, 73 DrawOrder order, 74 const PaintParams* paint); 75 76 bool writePixels(Recorder* recorder, 77 const SkPixmap& src, 78 SkIPoint dstPt); 79 80 // Ends the current DrawList being accumulated by the SDC, converting it into an optimized and 81 // immutable DrawPass. The DrawPass will be ordered after any other snapped DrawPasses or 82 // appended DrawPasses from a child SDC. A new DrawList is started to record subsequent drawing 83 // operations. 84 // 85 // If 'occlusionCuller' is null, then culling is skipped when converting the DrawList into a 86 // DrawPass. 87 // TBD - should this also return the task so the caller can point to it with its own 88 // dependencies? Or will that be mostly automatic based on draws and proxy refs? 89 void snapDrawPass(Recorder*, const BoundsManager* occlusionCuller); 90 91 // TBD: snapRenderPassTask() might not need to be public, and could be spec'ed to require that 92 // snapDrawPass() must have been called first. A lot of it will depend on how the task graph is 93 // managed. 94 95 // Ends the current DrawList if needed, as in 'snapDrawPass', and moves the new DrawPass and all 96 // prior accumulated DrawPasses into a RenderPassTask that can be drawn and depended on. The 97 // caller is responsible for configuring the returned Tasks's dependencies. 98 // 99 // Returns null if there are no pending commands or draw passes to move into a task. 100 sk_sp<Task> snapRenderPassTask(Recorder*, const BoundsManager* occlusionCuller); 101 102 // Ends the current UploadList if needed, and moves the accumulated Uploads into an UploadTask 103 // that can be drawn and depended on. The caller is responsible for configuring the returned 104 // Tasks's dependencies. 105 // 106 // Returns null if there are no pending uploads to move into a task. 107 // 108 // TODO: see if we can merge transfers into this 109 sk_sp<Task> snapUploadTask(Recorder*); 110 111 private: 112 DrawContext(sk_sp<TextureProxy>, const SkImageInfo&); 113 114 sk_sp<TextureProxy> fTarget; 115 SkImageInfo fImageInfo; 116 117 // Stores the most immediately recorded draws into the SDC's surface. This list is mutable and 118 // can be appended to, or have its commands rewritten if they are inlined into a parent SDC. 119 std::unique_ptr<DrawList> fPendingDraws; 120 // Load and store information for the current pending draws. 121 LoadOp fPendingLoadOp = LoadOp::kLoad; 122 StoreOp fPendingStoreOp = StoreOp::kStore; 123 std::array<float, 4> fPendingClearColor = { 0, 0, 0, 0 }; 124 125 // Stores previously snapped DrawPasses of this DC, or inlined child DCs whose content 126 // couldn't have been copied directly to fPendingDraws. While each DrawPass is immutable, the 127 // list of DrawPasses is not final until there is an external dependency on the SDC's content 128 // that requires it to be resolved as its own render pass (vs. inlining the SDC's passes into a 129 // parent's render pass). 130 // TODO: It will be easier to debug/understand the DrawPass structure of a context if 131 // consecutive DrawPasses to the same target are stored in a DrawPassChain. A DrawContext with 132 // multiple DrawPassChains is then clearly accumulating subpasses across multiple targets. 133 std::vector<std::unique_ptr<DrawPass>> fDrawPasses; 134 135 // Stores the most immediately recorded uploads into Textures. This list is mutable and 136 // can be appended to, or have its commands rewritten if they are inlined into a parent DC. 137 std::unique_ptr<UploadList> fPendingUploads; 138 }; 139 140 } // namespace skgpu 141 142 #endif // skgpu_DrawContext_DEFINED 143