| 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 |
#include "src/gpu/graphite/DrawContext.h" |
| 8 |
|
| 9 |
#include "include/core/SkAlphaType.h" |
| 10 |
#include "include/core/SkColorType.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkSize.h" |
| 13 |
#include "include/gpu/GpuTypes.h" |
| 14 |
#include "include/gpu/graphite/Recorder.h" |
| 15 |
#include "include/private/base/SkAssert.h" |
| 16 |
#include "src/base/SkEnumBitMask.h" |
| 17 |
#include "src/core/SkColorData.h" |
| 18 |
#include "src/core/SkTraceEvent.h" |
| 19 |
#include "src/gpu/SkBackingFit.h" |
| 20 |
#include "src/gpu/Swizzle.h" |
| 21 |
#include "src/gpu/graphite/AtlasProvider.h" |
| 22 |
#include "src/gpu/graphite/Caps.h" |
| 23 |
#include "src/gpu/graphite/ComputePathAtlas.h" |
| 24 |
#include "src/gpu/graphite/DrawList.h" |
| 25 |
#include "src/gpu/graphite/DrawListBase.h" |
| 26 |
#include "src/gpu/graphite/DrawOrder.h" |
| 27 |
#include "src/gpu/graphite/DrawParams.h" |
| 28 |
#include "src/gpu/graphite/DrawPass.h" |
| 29 |
#include "src/gpu/graphite/Image_Graphite.h" |
| 30 |
#include "src/gpu/graphite/Log.h" |
| 31 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 32 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 33 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 34 |
#include "src/gpu/graphite/TextureProxy.h" |
| 35 |
#include "src/gpu/graphite/TextureProxyView.h" |
| 36 |
#include "src/gpu/graphite/TextureUtils.h" |
| 37 |
#include "src/gpu/graphite/geom/Rect.h" |
| 38 |
#include "src/gpu/graphite/task/ComputeTask.h" |
| 39 |
#include "src/gpu/graphite/task/DrawTask.h" |
| 40 |
#include "src/gpu/graphite/task/RenderPassTask.h" |
| 41 |
#include "src/gpu/graphite/task/Task.h" |
| 42 |
#include "src/gpu/graphite/task/UploadTask.h" |
| 43 |
|
| 44 |
#include <cstdint> |
| 45 |
#include <utility> |
| 46 |
|
| 47 |
namespace skgpu::graphite { |
| 48 |
|
| 49 |
class PaintParams; |
| 50 |
class Renderer; |
| 51 |
enum class DepthStencilFlags : int; |
| 52 |
|
| 53 |
sk_sp<DrawContext> DrawContext::Make(const Caps* caps, |
| 54 |
sk_sp<TextureProxy> target, |
| 55 |
SkISize deviceSize, |
| 56 |
const SkColorInfo& colorInfo, |
| 57 |
const SkSurfaceProps& props) { |
| 58 |
if (!target) { |
| 59 |
return nullptr; |
| 60 |
} |
| 61 |
// We don't render to unknown or unpremul alphatypes |
| 62 |
if (colorInfo.alphaType() == kUnknown_SkAlphaType || |
| 63 |
colorInfo.alphaType() == kUnpremul_SkAlphaType) { |
| 64 |
return nullptr; |
| 65 |
} |
| 66 |
if (!caps->isRenderable(target->textureInfo())) { |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
if (!caps->areColorTypeAndTextureInfoCompatible(colorInfo.colorType(), target->textureInfo())) { |
| 70 |
return nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
// Accept an approximate-fit texture, but make sure it's at least as large as the device's |
| 74 |
// logical size. |
| 75 |
// TODO: validate that the alpha type is compatible with the target's info |
| 76 |
SkASSERT(target->isFullyLazy() || (target->dimensions().width() >= deviceSize.width() && |
| 77 |
target->dimensions().height() >= deviceSize.height())); |
| 78 |
SkImageInfo imageInfo = SkImageInfo::Make(deviceSize, colorInfo); |
| 79 |
return sk_sp<DrawContext>(new DrawContext(caps, std::move(target), imageInfo, props)); |
| 80 |
} |
| 81 |
|
| 82 |
DrawContext::DrawContext(const Caps* caps, |
| 83 |
sk_sp<TextureProxy> target, |
| 84 |
const SkImageInfo& ii, |
| 85 |
const SkSurfaceProps& props) |
| 86 |
: fTarget(std::move(target)) |
| 87 |
, fImageInfo(ii) |
| 88 |
, fSurfaceProps(props) |
| 89 |
, fDstReadStrategy(caps->getDstReadStrategy()) |
| 90 |
, fSupportsHardwareAdvancedBlend(caps->supportsHardwareAdvancedBlending()) |
| 91 |
, fAdvancedBlendsRequireBarrier(caps->blendEquationSupport() == |
| 92 |
Caps::BlendEquationSupport::kAdvancedNoncoherent) |
| 93 |
, fCurrentDrawTask(sk_make_sp<DrawTask>(fTarget)) |
| 94 |
, fPendingDraws(std::make_unique<DrawList>()) |
| 95 |
, fPendingUploads(std::make_unique<UploadList>()) { |
| 96 |
// Must determine a valid strategy to use should a dst texture read be required. |
| 97 |
SkASSERT(fDstReadStrategy != DstReadStrategy::kNoneRequired); |
| 98 |
|
| 99 |
if (!caps->isTexturable(fTarget->textureInfo())) { |
| 100 |
fReadView = {}; // Presumably this DrawContext is rendering into a swap chain |
| 101 |
} else { |
| 102 |
Swizzle swizzle = caps->getReadSwizzle(ii.colorType(), fTarget->textureInfo()); |
| 103 |
fReadView = {fTarget, swizzle}; |
| 104 |
} |
| 105 |
// TBD - Will probably want DrawLists (and its internal commands) to come from an arena |
| 106 |
// that the DC manages. |
| 107 |
} |
| 108 |
|
| 109 |
DrawContext::~DrawContext() = default; |
| 110 |
|
| 111 |
void DrawContext::clear(const SkColor4f& clearColor) { |
| 112 |
this->resetForClearOrDiscard(); |
| 113 |
fPendingDraws->reset(LoadOp::kClear, clearColor); |
| 114 |
} |
| 115 |
|
| 116 |
void DrawContext::discard() { |
| 117 |
this->resetForClearOrDiscard(); |
| 118 |
fPendingDraws->reset(LoadOp::kDiscard); |
| 119 |
} |
| 120 |
|
| 121 |
void DrawContext::resetForClearOrDiscard() { |
| 122 |
// Non-loading operations on a fully lazy target can corrupt data beyond the DrawContext's |
| 123 |
// region so should be avoided. |
| 124 |
SkASSERT(!fTarget->isFullyLazy()); |
| 125 |
|
| 126 |
// NOTE: Eventually the current DrawTask should be reset, once there are no longer implicit |
| 127 |
// dependencies on atlas tasks between DrawContexts. When that's resolved, the only tasks in the |
| 128 |
// current DrawTask are those that directly impact the target, which becomes irrelevant with the |
| 129 |
// clear op overwriting it. For now, preserve the previous tasks that might include atlas |
| 130 |
// uploads that are not explicitly shared between DrawContexts. |
| 131 |
if (fComputePathAtlas) { |
| 132 |
fComputePathAtlas->reset(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
bool DrawContext::readsTexture(const TextureProxy* texture) const { |
| 137 |
if (fPendingDraws->samplesTexture(texture)) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
// visitProxies() before calling prepareResources() can revisit tasks in the general case |
| 142 |
// (e.g. processing everything in the root task list). In this case, the only tasks being |
| 143 |
// visited are pending tasks so their graph complexity should be minimal. |
| 144 |
bool notFound = fCurrentDrawTask->visitProxies( |
| 145 |
[texture](const TextureProxy* other) { |
| 146 |
// Return true to continue visiting, i.e. when we haven't found `texture` yet. |
| 147 |
return texture != other; |
| 148 |
}, /*readsOnly=*/true); |
| 149 |
|
| 150 |
return !notFound; // double negation means its found in a pending child task |
| 151 |
} |
| 152 |
|
| 153 |
void DrawContext::recordDraw(const Renderer* renderer, |
| 154 |
const Transform& localToDevice, |
| 155 |
const Geometry& geometry, |
| 156 |
const Clip& clip, |
| 157 |
DrawOrder ordering, |
| 158 |
UniquePaintParamsID paintID, |
| 159 |
SkEnumBitMask<DstUsage> dstUsage, |
| 160 |
PipelineDataGatherer* gatherer, |
| 161 |
const StrokeStyle* stroke) { |
| 162 |
SkASSERTF(SkIRect::MakeSize(this->imageInfo().dimensions()).contains(clip.scissor()), |
| 163 |
"Image %dx%d, scissor %d,%d,%d,%d", |
| 164 |
this->imageInfo().width(), this->imageInfo().height(), |
| 165 |
clip.scissor().left(), clip.scissor().top(), |
| 166 |
clip.scissor().right(), clip.scissor().bottom()); |
| 167 |
|
| 168 |
// Determine whether a draw requies a barrier |
| 169 |
BarrierType barrierBeforeDraws = BarrierType::kNone; |
| 170 |
if (fDstReadStrategy == DstReadStrategy::kReadFromInput && |
| 171 |
(dstUsage & DstUsage::kDstReadRequired)) { |
| 172 |
barrierBeforeDraws = BarrierType::kReadDstFromInput; |
| 173 |
} |
| 174 |
if ((dstUsage & DstUsage::kAdvancedBlend) && |
| 175 |
fSupportsHardwareAdvancedBlend && fAdvancedBlendsRequireBarrier) { |
| 176 |
// A draw should only read from the dst OR use hardware for advanced blend modes. |
| 177 |
SkASSERT(!(dstUsage & DstUsage::kDstReadRequired)); |
| 178 |
barrierBeforeDraws = BarrierType::kAdvancedNoncoherentBlend; |
| 179 |
} |
| 180 |
|
| 181 |
fPendingDraws->recordDraw(renderer, localToDevice, geometry, clip, ordering, paintID, dstUsage, |
| 182 |
barrierBeforeDraws, gatherer, stroke); |
| 183 |
} |
| 184 |
|
| 185 |
bool DrawContext::recordUpload(Recorder* recorder, |
| 186 |
sk_sp<TextureProxy> targetProxy, |
| 187 |
const SkColorInfo& srcColorInfo, |
| 188 |
const SkColorInfo& dstColorInfo, |
| 189 |
const UploadSource& source, |
| 190 |
const SkIRect& dstRect, |
| 191 |
std::unique_ptr<ConditionalUploadContext> condContext) { |
| 192 |
// Our caller should have clipped to the bounds of the surface already. |
| 193 |
SkASSERT(targetProxy->isFullyLazy() || |
| 194 |
SkIRect::MakeSize(targetProxy->dimensions()).contains(dstRect)); |
| 195 |
SkASSERT(source.isValid()); |
| 196 |
return fPendingUploads->recordUpload(recorder, |
| 197 |
std::move(targetProxy), |
| 198 |
srcColorInfo, |
| 199 |
dstColorInfo, |
| 200 |
source, |
| 201 |
dstRect, |
| 202 |
std::move(condContext)); |
| 203 |
} |
| 204 |
|
| 205 |
void DrawContext::recordDependency(sk_sp<Task> task) { |
| 206 |
SkASSERT(task); |
| 207 |
// Adding `task` to the current DrawTask directly means that it will execute after any previous |
| 208 |
// dependent tasks and after any previous calls to flush(), but everything else that's being |
| 209 |
// collected on the DrawContext will execute after `task` once the next flush() is performed. |
| 210 |
fCurrentDrawTask->addTask(std::move(task)); |
| 211 |
} |
| 212 |
|
| 213 |
PathAtlas* DrawContext::getComputePathAtlas(Recorder* recorder) { |
| 214 |
if (!fComputePathAtlas) { |
| 215 |
fComputePathAtlas = recorder->priv().atlasProvider()->createComputePathAtlas(recorder); |
| 216 |
} |
| 217 |
return fComputePathAtlas.get(); |
| 218 |
} |
| 219 |
|
| 220 |
void DrawContext::flush(Recorder* recorder) { |
| 221 |
if (fPendingUploads->size() > 0) { |
| 222 |
TRACE_EVENT_INSTANT1("skia.gpu", TRACE_FUNC, TRACE_EVENT_SCOPE_THREAD, |
| 223 |
"# uploads", fPendingUploads->size()); |
| 224 |
fCurrentDrawTask->addTask(UploadTask::Make(fPendingUploads.get())); |
| 225 |
// The UploadTask steals the collected upload instances, automatically resetting this list |
| 226 |
SkASSERT(fPendingUploads->size() == 0); |
| 227 |
} |
| 228 |
|
| 229 |
// Generate compute dispatches that render into the atlas texture used by pending draws. |
| 230 |
// TODO: Once compute atlas caching is implemented, DrawContext might not hold onto to this |
| 231 |
// at which point a recordDispatch() could be added and it stores a pending dispatches list that |
| 232 |
// much like how uploads are handled. In that case, Device would be responsible for triggering |
| 233 |
// the recording of dispatches, but that may happen naturally in AtlasProvider::recordUploads(). |
| 234 |
if (fComputePathAtlas) { |
| 235 |
ComputeTask::DispatchGroupList dispatches; |
| 236 |
if (fComputePathAtlas->recordDispatches(recorder, &dispatches)) { |
| 237 |
// For now this check is valid as all coverage mask draws involve dispatches |
| 238 |
SkASSERT(fPendingDraws->hasCoverageMaskDraws()); |
| 239 |
|
| 240 |
fCurrentDrawTask->addTask(ComputeTask::Make(std::move(dispatches))); |
| 241 |
} // else no pending compute work needed to be recorded |
| 242 |
|
| 243 |
fComputePathAtlas->reset(); |
| 244 |
} // else platform doesn't support compute or atlas was never initialized. |
| 245 |
|
| 246 |
if (!fPendingDraws->modifiesTarget()) { |
| 247 |
// Nothing will be rasterized to the target that warrants a RenderPassTask, but we preserve |
| 248 |
// any added uploads or compute tasks since those could also affect the target w/o |
| 249 |
// rasterizing anything directly. |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
// Extract certain properties from DrawList relevant for DrawTask construction before |
| 254 |
// relinquishing the pending draw list to the DrawPass constructor. |
| 255 |
SkIRect dstReadPixelBounds = fPendingDraws->dstReadBounds().makeRoundOut().asSkIRect(); |
| 256 |
const bool drawsRequireMSAA = fPendingDraws->drawsRequireMSAA(); |
| 257 |
const SkEnumBitMask<DepthStencilFlags> dsFlags = fPendingDraws->depthStencilFlags(); |
| 258 |
// Determine the optimal dst read strategy for the drawpass given pending draw characteristics |
| 259 |
const DstReadStrategy drawPassDstReadStrategy = fPendingDraws->drawsReadDst() |
| 260 |
? this->dstReadStrategy() |
| 261 |
: DstReadStrategy::kNoneRequired; |
| 262 |
|
| 263 |
// Convert the pending draws and load/store ops into a DrawPass that will be executed after |
| 264 |
// the collected uploads and compute dispatches. |
| 265 |
// TODO: At this point, there's only ever one DrawPass in a RenderPassTask to a target. When |
| 266 |
// subpasses are implemented, they will either be collected alongside fPendingDraws or added |
| 267 |
// to the RenderPassTask separately. |
| 268 |
std::unique_ptr<DrawPass> pass = fPendingDraws->snapDrawPass(recorder, |
| 269 |
fTarget, |
| 270 |
this->imageInfo(), |
| 271 |
drawPassDstReadStrategy); |
| 272 |
SkASSERT(!fPendingDraws->modifiesTarget()); // Should be drained into `pass`. |
| 273 |
|
| 274 |
if (pass) { |
| 275 |
SkASSERT(fTarget.get() == pass->target()); |
| 276 |
|
| 277 |
// If any paint used within the DrawPass reads from the dst texture (indicated by nonempty |
| 278 |
// dstReadPixelBounds) and the dstReadStrategy is kTextureCopy, then add a CopyTask. |
| 279 |
sk_sp<TextureProxy> dstCopy; |
| 280 |
if (!dstReadPixelBounds.isEmpty() && |
| 281 |
drawPassDstReadStrategy == DstReadStrategy::kTextureCopy) { |
| 282 |
TRACE_EVENT_INSTANT0("skia.gpu", "DrawPass requires dst copy", |
| 283 |
TRACE_EVENT_SCOPE_THREAD); |
| 284 |
sk_sp<Image> imageCopy = Image::Copy( |
| 285 |
recorder, |
| 286 |
this, |
| 287 |
fReadView, |
| 288 |
fImageInfo.colorInfo(), |
| 289 |
dstReadPixelBounds, |
| 290 |
Budgeted::kYes, |
| 291 |
Mipmapped::kNo, |
| 292 |
SkBackingFit::kApprox, |
| 293 |
"DstCopy"); |
| 294 |
if (!imageCopy) { |
| 295 |
SKGPU_LOG_W("DrawContext::flush Image::Copy failed, draw pass dropped!"); |
| 296 |
return; |
| 297 |
} |
| 298 |
dstCopy = imageCopy->textureProxyView().refProxy(); |
| 299 |
SkASSERT(dstCopy); |
| 300 |
} |
| 301 |
|
| 302 |
const Caps* caps = recorder->priv().caps(); |
| 303 |
auto [loadOp, storeOp] = pass->ops(); |
| 304 |
auto writeSwizzle = caps->getWriteSwizzle(this->colorInfo().colorType(), |
| 305 |
fTarget->textureInfo()); |
| 306 |
|
| 307 |
RenderPassDesc desc = RenderPassDesc::Make(caps, fTarget->textureInfo(), loadOp, storeOp, |
| 308 |
dsFlags, |
| 309 |
pass->clearColor(), |
| 310 |
drawsRequireMSAA, |
| 311 |
writeSwizzle, |
| 312 |
drawPassDstReadStrategy); |
| 313 |
|
| 314 |
RenderPassTask::DrawPassList passes; |
| 315 |
passes.emplace_back(std::move(pass)); |
| 316 |
fCurrentDrawTask->addTask(RenderPassTask::Make(std::move(passes), desc, fTarget, |
| 317 |
std::move(dstCopy), dstReadPixelBounds)); |
| 318 |
if (fTarget->mipmapped() == Mipmapped::kYes) { |
| 319 |
if (!GenerateMipmaps(recorder, this, fTarget, fImageInfo.colorInfo())) { |
| 320 |
SKGPU_LOG_W("DrawContext::flush GenerateMipmaps failed, draw pass dropped!"); |
| 321 |
return; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
// else pass creation failed, DrawPass will have logged why. Don't discard the previously |
| 326 |
// accumulated tasks, however, since they may represent operations on an atlas that other |
| 327 |
// DrawContexts now implicitly depend on. |
| 328 |
} |
| 329 |
|
| 330 |
sk_sp<Task> DrawContext::snapDrawTask() { |
| 331 |
if (!fCurrentDrawTask->hasTasks()) { |
| 332 |
return nullptr; |
| 333 |
} |
| 334 |
|
| 335 |
sk_sp<Task> snappedTask = std::move(fCurrentDrawTask); |
| 336 |
fCurrentDrawTask = sk_make_sp<DrawTask>(fTarget); |
| 337 |
return snappedTask; |
| 338 |
} |
| 339 |
|
| 340 |
} // namespace skgpu::graphite |