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