| 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/SkColor.h" |
| 12 |
#include "include/core/SkImageInfo.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkSurfaceProps.h" |
| 15 |
#include "src/gpu/graphite/DrawListBase.h" |
| 16 |
#include "src/gpu/graphite/PaintParams.h" |
| 17 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 18 |
#include "src/gpu/graphite/TextureProxy.h" |
| 19 |
#include "src/gpu/graphite/TextureProxyView.h" |
| 20 |
|
| 21 |
#include <array> |
| 22 |
#include <memory> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
struct SkIRect; |
| 26 |
struct SkISize; |
| 27 |
|
| 28 |
namespace skgpu::graphite { |
| 29 |
|
| 30 |
class Caps; |
| 31 |
class Clip; |
| 32 |
class ComputePathAtlas; |
| 33 |
class ConditionalUploadContext; |
| 34 |
class DrawOrder; |
| 35 |
class DrawTask; |
| 36 |
class Geometry; |
| 37 |
class PaintParams; |
| 38 |
class PathAtlas; |
| 39 |
class Recorder; |
| 40 |
class Renderer; |
| 41 |
class StrokeStyle; |
| 42 |
class Task; |
| 43 |
class Transform; |
| 44 |
class UploadList; |
| 45 |
class UploadSource; |
| 46 |
|
| 47 |
/** |
| 48 |
* DrawContext records draw commands into a specific Surface, via a general task graph |
| 49 |
* representing GPU work and their inter-dependencies. |
| 50 |
*/ |
| 51 |
class DrawContext final : public SkRefCnt { |
| 52 |
public: |
| 53 |
static sk_sp<DrawContext> Make(const Caps* caps, |
| 54 |
sk_sp<TextureProxy> target, |
| 55 |
SkISize deviceSize, |
| 56 |
const SkColorInfo&, |
| 57 |
const SkSurfaceProps&); |
| 58 |
|
| 59 |
~DrawContext() override; |
| 60 |
|
| 61 |
const SkImageInfo& imageInfo() const { return fImageInfo; } |
| 62 |
const SkColorInfo& colorInfo() const { return fImageInfo.colorInfo(); } |
| 63 |
TextureProxy* target() { return fTarget.get(); } |
| 64 |
const TextureProxy* target() const { return fTarget.get(); } |
| 65 |
sk_sp<TextureProxy> refTarget() const { return fTarget; } |
| 66 |
|
| 67 |
// May be null if the target is not texturable. |
| 68 |
const TextureProxyView& readSurfaceView() const { return fReadView; } |
| 69 |
|
| 70 |
const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; } |
| 71 |
|
| 72 |
int pendingRenderSteps() const { return fPendingDraws->renderStepCount(); } |
| 73 |
|
| 74 |
bool modifiesTarget() const { return fPendingDraws->modifiesTarget(); } |
| 75 |
|
| 76 |
bool readsTexture(const TextureProxy*) const; |
| 77 |
|
| 78 |
void clear(const SkColor4f& clearColor); |
| 79 |
void discard(); |
| 80 |
|
| 81 |
void recordDraw(const Renderer* renderer, |
| 82 |
const Transform& localToDevice, |
| 83 |
const Geometry& geometry, |
| 84 |
const Clip& clip, |
| 85 |
DrawOrder ordering, |
| 86 |
UniquePaintParamsID paintID, |
| 87 |
SkEnumBitMask<DstUsage> dstUsage, |
| 88 |
PipelineDataGatherer* gatherer, |
| 89 |
const StrokeStyle* stroke); |
| 90 |
|
| 91 |
bool recordUpload(Recorder* recorder, |
| 92 |
sk_sp<TextureProxy> targetProxy, |
| 93 |
const SkColorInfo& srcColorInfo, |
| 94 |
const SkColorInfo& dstColorInfo, |
| 95 |
const UploadSource& source, |
| 96 |
const SkIRect& dstRect, |
| 97 |
std::unique_ptr<ConditionalUploadContext>); |
| 98 |
|
| 99 |
// Add a Task that will be executed *before* any of the pending draws and uploads are |
| 100 |
// executed as part of the next flush(). |
| 101 |
void recordDependency(sk_sp<Task>); |
| 102 |
|
| 103 |
// Returns the transient path atlas that uses compute to accumulate coverage masks for atlas |
| 104 |
// draws recorded to this SDC. The atlas gets created lazily upon request. Returns nullptr |
| 105 |
// if compute path generation is not supported. |
| 106 |
PathAtlas* getComputePathAtlas(Recorder*); |
| 107 |
|
| 108 |
// Moves all accumulated pending recorded operations (draws and uploads), and any other |
| 109 |
// dependent tasks into the DrawTask currently being built. |
| 110 |
void flush(Recorder*); |
| 111 |
|
| 112 |
// Returns the current DrawTask to the caller, so all pending draws and uploads (if flush() |
| 113 |
// was not immediately called prior to this) and subsequently recorded draws and uploads will |
| 114 |
// go into a new DrawTask. |
| 115 |
sk_sp<Task> snapDrawTask(); |
| 116 |
|
| 117 |
// Returns the dst read strategy to use when/if a paint requires a dst read |
| 118 |
DstReadStrategy dstReadStrategy() const { return fDstReadStrategy; } |
| 119 |
|
| 120 |
private: |
| 121 |
DrawContext(const Caps*, sk_sp<TextureProxy>, const SkImageInfo&, const SkSurfaceProps&); |
| 122 |
|
| 123 |
void resetForClearOrDiscard(); |
| 124 |
|
| 125 |
sk_sp<TextureProxy> fTarget; |
| 126 |
TextureProxyView fReadView; |
| 127 |
SkImageInfo fImageInfo; |
| 128 |
const SkSurfaceProps fSurfaceProps; |
| 129 |
|
| 130 |
// Does *not* reflect whether a dst read is needed by the DrawLists - simply specifies the |
| 131 |
// strategies to use should any encountered paint require it. |
| 132 |
const DstReadStrategy fDstReadStrategy; |
| 133 |
const bool fSupportsHardwareAdvancedBlend; |
| 134 |
const bool fAdvancedBlendsRequireBarrier; |
| 135 |
|
| 136 |
// The in-progress DrawTask that will be snapped and returned when some external requirement |
| 137 |
// must depend on the contents of this DrawContext's target. As higher-level Skia operations |
| 138 |
// are recorded, it can be necessary to flush pending draws and uploads into the task list. |
| 139 |
// This provides a place to reset scratch textures or buffers as their previous state will have |
| 140 |
// been consumed by the flushed tasks rendering to this DrawContext's target. |
| 141 |
sk_sp<DrawTask> fCurrentDrawTask; |
| 142 |
|
| 143 |
// Stores the most immediately recorded draws and uploads into the DrawContext's target. These |
| 144 |
// are collected outside of the DrawTask so that encoder switches can be minimized when |
| 145 |
// flushing. |
| 146 |
std::unique_ptr<DrawListBase> fPendingDraws; |
| 147 |
std::unique_ptr<UploadList> fPendingUploads; |
| 148 |
|
| 149 |
// Accumulates atlas coverage masks generated by compute dispatches that are required by one or |
| 150 |
// more entries in `fPendingDraws`. When pending draws are snapped into a new DrawPass, a |
| 151 |
// compute dispatch group gets recorded which schedules the accumulated masks to get drawn into |
| 152 |
// an atlas texture. The accumulated masks are then cleared which frees up the atlas for |
| 153 |
// future draws. |
| 154 |
// |
| 155 |
// TODO: Currently every PathAtlas contains a single texture. If multiple snapped draw |
| 156 |
// passes resulted in multiple ComputePathAtlas dispatch groups, the later dispatches would |
| 157 |
// overwrite the atlas texture since all compute tasks are scheduled before render tasks. This |
| 158 |
// is currently not an issue since there is only one DrawPass per flush but we may want to |
| 159 |
// either support one atlas texture per DrawPass or record the dispatches once per |
| 160 |
// RenderPassTask rather than DrawPass. |
| 161 |
std::unique_ptr<ComputePathAtlas> fComputePathAtlas; |
| 162 |
}; |
| 163 |
|
| 164 |
} // namespace skgpu::graphite |
| 165 |
|
| 166 |
#endif // skgpu_graphite_DrawContext_DEFINED |