| 1 |
/* |
| 2 |
* Copyright 2021 Google Inc. |
| 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/ganesh/ops/AtlasRenderTask.h" |
| 8 |
|
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkScalar.h" |
| 13 |
#include "include/core/SkSize.h" |
| 14 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 15 |
#include "include/private/base/SkDebug.h" |
| 16 |
#include "include/private/base/SkPoint_impl.h" |
| 17 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 18 |
#include "src/core/SkIPoint16.h" |
| 19 |
#include "src/gpu/ganesh/GrGpu.h" |
| 20 |
#include "src/gpu/ganesh/GrNativeRect.h" |
| 21 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 22 |
#include "src/gpu/ganesh/GrPaint.h" |
| 23 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 24 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 25 |
#include "src/gpu/ganesh/GrRenderTask.h" |
| 26 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 27 |
#include "src/gpu/ganesh/GrSurfaceProxyPriv.h" |
| 28 |
#include "src/gpu/ganesh/GrUserStencilSettings.h" |
| 29 |
#include "src/gpu/ganesh/GrXferProcessor.h" |
| 30 |
#include "src/gpu/ganesh/geometry/GrQuad.h" |
| 31 |
#include "src/gpu/ganesh/ops/FillPathFlags.h" |
| 32 |
#include "src/gpu/ganesh/ops/FillRectOp.h" |
| 33 |
#include "src/gpu/ganesh/ops/GrDrawOp.h" |
| 34 |
#include "src/gpu/ganesh/ops/PathStencilCoverOp.h" |
| 35 |
|
| 36 |
#include <initializer_list> |
| 37 |
|
| 38 |
namespace skgpu::ganesh { |
| 39 |
|
| 40 |
AtlasRenderTask::AtlasRenderTask(GrRecordingContext* rContext, |
| 41 |
sk_sp<GrArenas> arenas, |
| 42 |
std::unique_ptr<GrDynamicAtlas> dynamicAtlas) |
| 43 |
: OpsTask(rContext->priv().drawingManager(), |
| 44 |
dynamicAtlas->writeView(*rContext->priv().caps()), |
| 45 |
rContext->priv().auditTrail(), |
| 46 |
std::move(arenas)) |
| 47 |
, fDynamicAtlas(std::move(dynamicAtlas)) { |
| 48 |
} |
| 49 |
|
| 50 |
bool AtlasRenderTask::addPath(const SkMatrix& viewMatrix, const SkPath& path, |
| 51 |
SkIPoint pathDevTopLeft, int widthInAtlas, int heightInAtlas, |
| 52 |
bool transposedInAtlas, SkIPoint16* locationInAtlas) { |
| 53 |
SkASSERT(!this->isClosed()); |
| 54 |
SkASSERT(this->isEmpty()); |
| 55 |
SkASSERT(!fDynamicAtlas->isInstantiated()); // Paths can't be added after instantiate(). |
| 56 |
|
| 57 |
// Check for room in the list first and return false if prior draws need to be flushed first. |
| 58 |
if (GrFillRuleForSkPath(path) == GrFillRule::kNonzero) { |
| 59 |
if (!fWindingPathList.canAdd(path)) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
} else { |
| 63 |
if (!fEvenOddPathList.canAdd(path)) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if (!fDynamicAtlas->addRect(widthInAtlas, heightInAtlas, locationInAtlas)) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
SkMatrix pathToAtlasMatrix = viewMatrix; |
| 73 |
if (transposedInAtlas) { |
| 74 |
std::swap(pathToAtlasMatrix[0], pathToAtlasMatrix[3]); |
| 75 |
std::swap(pathToAtlasMatrix[1], pathToAtlasMatrix[4]); |
| 76 |
float tx=pathToAtlasMatrix.getTranslateX(), ty=pathToAtlasMatrix.getTranslateY(); |
| 77 |
pathToAtlasMatrix.setTranslateX(ty - pathDevTopLeft.y() + locationInAtlas->x()); |
| 78 |
pathToAtlasMatrix.setTranslateY(tx - pathDevTopLeft.x() + locationInAtlas->y()); |
| 79 |
} else { |
| 80 |
pathToAtlasMatrix.postTranslate(locationInAtlas->x() - pathDevTopLeft.x(), |
| 81 |
locationInAtlas->y() - pathDevTopLeft.y()); |
| 82 |
} |
| 83 |
|
| 84 |
if (GrFillRuleForSkPath(path) == GrFillRule::kNonzero) { |
| 85 |
fWindingPathList.add(&fPathDrawAllocator, pathToAtlasMatrix, path); |
| 86 |
} else { |
| 87 |
fEvenOddPathList.add(&fPathDrawAllocator, pathToAtlasMatrix, path); |
| 88 |
} |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
GrRenderTask::ExpectedOutcome AtlasRenderTask::onMakeClosed(GrRecordingContext* rContext, |
| 93 |
SkIRect* targetUpdateBounds) { |
| 94 |
// We don't add our ops until now, at which point we know the atlas is done being built. |
| 95 |
SkASSERT(this->isEmpty()); |
| 96 |
SkASSERT(!fDynamicAtlas->isInstantiated()); // Instantiation happens after makeClosed(). |
| 97 |
|
| 98 |
const GrCaps& caps = *rContext->priv().caps(); |
| 99 |
|
| 100 |
// Set our dimensions now. OpsTask will need them when we add our ops. |
| 101 |
this->target(0)->priv().setLazyDimensions(fDynamicAtlas->drawBounds()); |
| 102 |
this->target(0)->asRenderTargetProxy()->setNeedsStencil(); |
| 103 |
SkRect drawRect = target(0)->getBoundsRect(); |
| 104 |
|
| 105 |
// Clear the atlas. |
| 106 |
if (caps.performColorClearsAsDraws() || caps.performStencilClearsAsDraws()) { |
| 107 |
this->setColorLoadOp(GrLoadOp::kDiscard); |
| 108 |
this->setInitialStencilContent(StencilContent::kDontCare); |
| 109 |
|
| 110 |
constexpr static GrUserStencilSettings kClearStencil( |
| 111 |
GrUserStencilSettings::StaticInit< |
| 112 |
0x0000, |
| 113 |
GrUserStencilTest::kAlways, |
| 114 |
0xffff, |
| 115 |
GrUserStencilOp::kReplace, |
| 116 |
GrUserStencilOp::kReplace, |
| 117 |
0xffff>()); |
| 118 |
|
| 119 |
this->stencilAtlasRect(rContext, drawRect, SK_PMColor4fTRANSPARENT, &kClearStencil); |
| 120 |
} else { |
| 121 |
this->setColorLoadOp(GrLoadOp::kClear); |
| 122 |
this->setInitialStencilContent(StencilContent::kUserBitsCleared); |
| 123 |
} |
| 124 |
|
| 125 |
// Add ops to stencil the atlas paths. |
| 126 |
for (const auto* pathList : {&fWindingPathList, &fEvenOddPathList}) { |
| 127 |
if (pathList->pathCount() > 0) { |
| 128 |
auto op = GrOp::Make<PathStencilCoverOp>( |
| 129 |
rContext, |
| 130 |
pathList->pathDrawList(), |
| 131 |
pathList->totalCombinedPathVerbCnt(), |
| 132 |
pathList->pathCount(), |
| 133 |
GrPaint(), |
| 134 |
GrAAType::kMSAA, |
| 135 |
FillPathFlags::kStencilOnly, |
| 136 |
drawRect); |
| 137 |
this->addAtlasDrawOp(std::move(op), caps); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Finally, draw a fullscreen rect to cover our stencilled paths. |
| 142 |
const GrUserStencilSettings* stencil; |
| 143 |
if (caps.discardStencilValuesAfterRenderPass()) { |
| 144 |
constexpr static GrUserStencilSettings kTestStencil( |
| 145 |
GrUserStencilSettings::StaticInit< |
| 146 |
0x0000, |
| 147 |
GrUserStencilTest::kNotEqual, |
| 148 |
0xffff, |
| 149 |
GrUserStencilOp::kKeep, |
| 150 |
GrUserStencilOp::kKeep, |
| 151 |
0xffff>()); |
| 152 |
|
| 153 |
// This is the final op in the task. Since Ganesh is planning to discard the stencil values |
| 154 |
// anyway, there is no need to reset the stencil values back to 0. |
| 155 |
stencil = &kTestStencil; |
| 156 |
} else { |
| 157 |
constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 158 |
GrUserStencilSettings::StaticInit< |
| 159 |
0x0000, |
| 160 |
GrUserStencilTest::kNotEqual, |
| 161 |
0xffff, |
| 162 |
GrUserStencilOp::kZero, |
| 163 |
GrUserStencilOp::kKeep, |
| 164 |
0xffff>()); |
| 165 |
|
| 166 |
// Outset the cover rect to make extra sure we clear every stencil value touched by the |
| 167 |
// atlas. |
| 168 |
drawRect.outset(1, 1); |
| 169 |
stencil = &kTestAndResetStencil; |
| 170 |
} |
| 171 |
this->stencilAtlasRect(rContext, drawRect, SK_PMColor4fWHITE, stencil); |
| 172 |
|
| 173 |
this->OpsTask::onMakeClosed(rContext, targetUpdateBounds); |
| 174 |
|
| 175 |
// Don't mark msaa dirty. Since this op defers being closed, the drawing manager's dirty |
| 176 |
// tracking doesn't work anyway. We will just resolve msaa manually during onExecute. |
| 177 |
return ExpectedOutcome::kTargetUnchanged; |
| 178 |
} |
| 179 |
|
| 180 |
void AtlasRenderTask::stencilAtlasRect(GrRecordingContext* rContext, const SkRect& rect, |
| 181 |
const SkPMColor4f& color, |
| 182 |
const GrUserStencilSettings* stencil) { |
| 183 |
GrPaint paint; |
| 184 |
paint.setColor4f(color); |
| 185 |
paint.setXPFactory(GrXPFactory::FromBlendMode(SkBlendMode::kSrc)); |
| 186 |
GrQuad quad(rect); |
| 187 |
DrawQuad drawQuad{quad, quad, GrQuadAAFlags::kAll}; |
| 188 |
auto op = FillRectOp::Make(rContext, std::move(paint), GrAAType::kMSAA, &drawQuad, stencil); |
| 189 |
this->addAtlasDrawOp(std::move(op), *rContext->priv().caps()); |
| 190 |
} |
| 191 |
|
| 192 |
void AtlasRenderTask::addAtlasDrawOp(GrOp::Owner op, const GrCaps& caps) { |
| 193 |
SkASSERT(!this->isClosed()); |
| 194 |
|
| 195 |
auto drawOp = static_cast<GrDrawOp*>(op.get()); |
| 196 |
SkDEBUGCODE(drawOp->fAddDrawOpCalled = true;) |
| 197 |
|
| 198 |
auto processorAnalysis = drawOp->finalize(caps, nullptr, |
| 199 |
GrColorTypeClampType(fDynamicAtlas->colorType())); |
| 200 |
SkASSERT(!processorAnalysis.requiresDstTexture()); |
| 201 |
SkASSERT(!processorAnalysis.usesNonCoherentHWBlending()); |
| 202 |
|
| 203 |
drawOp->setClippedBounds(drawOp->bounds()); |
| 204 |
this->recordOp(std::move(op), true/*usesMSAA*/, processorAnalysis, nullptr, nullptr, caps); |
| 205 |
} |
| 206 |
|
| 207 |
bool AtlasRenderTask::onExecute(GrOpFlushState* flushState) { |
| 208 |
if (!this->OpsTask::onExecute(flushState)) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
if (this->target(0)->requiresManualMSAAResolve()) { |
| 212 |
// Since atlases don't get closed until they are done being built, the drawingManager |
| 213 |
// doesn't detect that they need an MSAA resolve. Do it here manually. |
| 214 |
auto nativeRect = GrNativeRect::MakeIRectRelativeTo( |
| 215 |
GrDynamicAtlas::kTextureOrigin, |
| 216 |
this->target(0)->backingStoreDimensions().height(), |
| 217 |
SkIRect::MakeSize(fDynamicAtlas->drawBounds())); |
| 218 |
flushState->gpu()->resolveRenderTarget(this->target(0)->peekRenderTarget(), nativeRect); |
| 219 |
} |
| 220 |
return true; |
| 221 |
} |
| 222 |
|
| 223 |
} // namespace skgpu::ganesh |