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 #include "src/gpu/ganesh/SurfaceFillContext.h"
9
10 #include "include/core/SkBlendMode.h"
11 #include "include/gpu/ganesh/GrRecordingContext.h"
12 #include "include/private/base/SingleOwner.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkPoint_impl.h"
15 #include "include/private/gpu/ganesh/GrTypesPriv.h"
16 #include "include/gpu/ganesh/GrDirectContext.h"
17 #include "src/core/SkTraceEvent.h"
18 #include "src/gpu/Swizzle.h"
19 #include "src/gpu/ganesh/GrAppliedClip.h"
20 #include "src/gpu/ganesh/GrAuditTrail.h"
21 #include "src/gpu/ganesh/GrCaps.h"
22 #include "src/gpu/ganesh/GrDrawingManager.h"
23 #include "src/gpu/ganesh/GrDstProxyView.h"
24 #include "src/gpu/ganesh/GrPaint.h"
25 #include "src/gpu/ganesh/GrProcessorSet.h"
26 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
27 #include "src/gpu/ganesh/GrRenderTask.h"
28 #include "src/gpu/ganesh/GrScissorState.h"
29 #include "src/gpu/ganesh/GrTextureResolveManager.h"
30 #include "src/gpu/ganesh/GrTracing.h"
31 #include "src/gpu/ganesh/effects/GrMatrixEffect.h"
32 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
33 #include "src/gpu/ganesh/geometry/GrRect.h"
34 #include "src/gpu/ganesh/ops/ClearOp.h"
35 #include "src/gpu/ganesh/ops/FillRectOp.h"
36 #include "src/gpu/ganesh/ops/GrDrawOp.h"
37
38 #include <cstdint>
39
40 #define ASSERT_SINGLE_OWNER SKGPU_ASSERT_SINGLE_OWNER(this->singleOwner())
41 #define RETURN_IF_ABANDONED if (fContext->abandoned()) { return; }
42
43 class AutoCheckFlush {
44 public:
AutoCheckFlush(GrDrawingManager * drawingManager)45 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
46 SkASSERT(fDrawingManager);
47 }
~AutoCheckFlush()48 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
49
50 private:
51 GrDrawingManager* fDrawingManager;
52 };
53
54 namespace skgpu::ganesh {
55
56 // In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
57 // OpsTask to be picked up and added to by SurfaceFillContext lower in the call
58 // stack. When this occurs with a closed OpsTask, a new one will be allocated
59 // when the SurfaceFillContext attempts to use it (via getOpsTask).
SurfaceFillContext(GrRecordingContext * rContext,GrSurfaceProxyView readView,GrSurfaceProxyView writeView,const GrColorInfo & colorInfo)60 SurfaceFillContext::SurfaceFillContext(GrRecordingContext* rContext,
61 GrSurfaceProxyView readView,
62 GrSurfaceProxyView writeView,
63 const GrColorInfo& colorInfo)
64 : SurfaceContext(rContext, std::move(readView), colorInfo)
65 , fWriteView(std::move(writeView)) {
66 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
67 SkASSERT(this->origin() == fWriteView.origin());
68
69 fOpsTask = sk_ref_sp(rContext->priv().drawingManager()->getLastOpsTask(this->asSurfaceProxy()));
70
71 SkDEBUGCODE(this->validate();)
72 }
73
getOpsTask()74 OpsTask* SurfaceFillContext::getOpsTask() {
75 ASSERT_SINGLE_OWNER
76 SkDEBUGCODE(this->validate();)
77
78 if (!fOpsTask || fOpsTask->isClosed()) {
79 this->replaceOpsTask();
80 }
81 SkASSERT(!fOpsTask->isClosed());
82 return fOpsTask.get();
83 }
84
discard()85 void SurfaceFillContext::discard() {
86 ASSERT_SINGLE_OWNER
87 RETURN_IF_ABANDONED
88 SkDEBUGCODE(this->validate();)
89 GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "discard", fContext);
90
91 AutoCheckFlush acf(this->drawingManager());
92
93 this->getOpsTask()->discard();
94 }
95
resolveMSAA()96 void SurfaceFillContext::resolveMSAA() {
97 ASSERT_SINGLE_OWNER
98 RETURN_IF_ABANDONED
99 SkDEBUGCODE(this->validate();)
100 GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "resolveMSAA", fContext);
101
102 AutoCheckFlush acf(this->drawingManager());
103
104 this->drawingManager()->newTextureResolveRenderTask(this->asSurfaceProxyRef(),
105 GrSurfaceProxy::ResolveFlags::kMSAA,
106 *this->caps());
107 }
108
fillRectWithFP(const SkIRect & dstRect,std::unique_ptr<GrFragmentProcessor> fp)109 void SurfaceFillContext::fillRectWithFP(const SkIRect& dstRect,
110 std::unique_ptr<GrFragmentProcessor> fp) {
111 ASSERT_SINGLE_OWNER
112 RETURN_IF_ABANDONED
113 SkDEBUGCODE(this->validate();)
114 GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "fillRectWithFP", fContext);
115
116 AutoCheckFlush acf(this->drawingManager());
117
118 GrPaint paint;
119 paint.setColorFragmentProcessor(std::move(fp));
120 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
121 auto op = FillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
122 SkRect::Make(dstRect));
123 this->addDrawOp(std::move(op));
124 }
125
fillRectWithFP(const SkIRect & dstRect,const SkMatrix & localMatrix,std::unique_ptr<GrFragmentProcessor> fp)126 void SurfaceFillContext::fillRectWithFP(const SkIRect& dstRect,
127 const SkMatrix& localMatrix,
128 std::unique_ptr<GrFragmentProcessor> fp) {
129 fp = GrMatrixEffect::Make(localMatrix, std::move(fp));
130 this->fillRectWithFP(dstRect, std::move(fp));
131 }
132
blitTexture(GrSurfaceProxyView view,const SkIRect & srcRect,const SkIPoint & dstPoint)133 bool SurfaceFillContext::blitTexture(GrSurfaceProxyView view,
134 const SkIRect& srcRect,
135 const SkIPoint& dstPoint) {
136 SkASSERT(view.asTextureProxy());
137
138 SkIPoint clippedDstPoint = dstPoint;
139 SkIRect clippedSrcRect = srcRect;
140 if (!GrClipSrcRectAndDstPoint(this->dimensions(),
141 &clippedDstPoint,
142 view.dimensions(),
143 &clippedSrcRect)) {
144 return false;
145 }
146
147 SkIRect clippedDstRect = SkIRect::MakePtSize(clippedDstPoint, clippedSrcRect.size());
148
149 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
150 this->fillRectToRectWithFP(SkRect::Make(clippedSrcRect), clippedDstRect, std::move(fp));
151 return true;
152 }
153
refRenderTask()154 sk_sp<GrRenderTask> SurfaceFillContext::refRenderTask() {
155 return sk_ref_sp(this->getOpsTask());
156 }
157
replaceOpsTask()158 OpsTask* SurfaceFillContext::replaceOpsTask() {
159 sk_sp<OpsTask> newOpsTask = this->drawingManager()->newOpsTask(this->writeSurfaceView(),
160 this->arenas());
161 this->willReplaceOpsTask(fOpsTask.get(), newOpsTask.get());
162 fOpsTask = std::move(newOpsTask);
163 return fOpsTask.get();
164 }
165
ClearToGrPaint(std::array<float,4> color,GrPaint * paint)166 void SurfaceFillContext::ClearToGrPaint(std::array<float, 4> color, GrPaint* paint) {
167 paint->setColor4f({color[0], color[1], color[2], color[3]});
168 if (color[3] == 1.f) {
169 // Can just rely on the src-over blend mode to do the right thing.
170 // This may improve batching.
171 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
172 } else {
173 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
174 // were src blended
175 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
176 }
177 }
178
addOp(GrOp::Owner op)179 void SurfaceFillContext::addOp(GrOp::Owner op) {
180 auto direct = fContext->priv().asDirectContext();
181 if (direct && op) {
182 op->setGrOpTag(direct->getCurrentGrResourceTag());
183 }
184 GrDrawingManager* drawingMgr = this->drawingManager();
185 this->getOpsTask()->addOp(drawingMgr,
186 std::move(op),
187 GrTextureResolveManager(drawingMgr),
188 *this->caps());
189 }
190
191
addDrawOp(GrOp::Owner owner)192 void SurfaceFillContext::addDrawOp(GrOp::Owner owner) {
193 GrDrawOp* op = static_cast<GrDrawOp*>(owner.get());
194 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
195 auto clip = GrAppliedClip::Disabled();
196 const GrCaps& caps = *this->caps();
197 GrProcessorSet::Analysis analysis = op->finalize(caps, &clip, clampType);
198 SkASSERT(!op->usesStencil());
199 SkASSERT(!analysis.requiresDstTexture());
200 SkRect bounds = owner->bounds();
201 // We shouldn't have coverage AA or hairline draws in fill contexts.
202 SkASSERT(!op->hasAABloat() && !op->hasZeroArea());
203 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
204 return;
205 }
206 op->setClippedBounds(op->bounds());
207 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
208
209 GrDstProxyView dstProxyView;
210 this->getOpsTask()->addDrawOp(fContext->priv().drawingManager(),
211 std::move(owner),
212 op->usesMSAA(),
213 analysis,
214 std::move(clip),
215 dstProxyView,
216 GrTextureResolveManager(this->drawingManager()),
217 caps);
218 }
219
internalClear(const SkIRect * scissor,std::array<float,4> color,bool upgradePartialToFull)220 void SurfaceFillContext::internalClear(const SkIRect* scissor,
221 std::array<float, 4> color,
222 bool upgradePartialToFull) {
223 ASSERT_SINGLE_OWNER
224 RETURN_IF_ABANDONED
225 SkDEBUGCODE(this->validate();)
226 GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "clear", fContext);
227
228 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
229 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
230 // supports then. Drawing an axis-aligned rect is the fallback path.
231 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
232 if (scissor && !scissorState.set(*scissor)) {
233 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
234 // except clear ops are not draw ops).
235 return;
236 }
237
238 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
239 // the test. We only do this when the clear would be handled by a load op or natively.
240 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
241 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
242 this->caps()->shouldInitializeTextures())) {
243 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
244 // only clear the entire target if we knew it had not been cleared before. As
245 // is this could end up doing a lot of redundant clears.
246 scissorState.setDisabled();
247 } else {
248 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
249 // render target to overflow into any approx-fit padding of the backing store dimensions
250 scissorState.relaxTest(this->dimensions());
251 }
252 }
253
254 if (!scissorState.enabled()) {
255 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
256 // discard all prior ops in the current task since the color buffer will be overwritten.
257 auto opsTask = this->getOpsTask();
258 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
259 !this->caps()->performColorClearsAsDraws()) {
260 color = this->writeSurfaceView().swizzle().applyTo(color);
261 // The op list was emptied and native clears are allowed, so just use the load op
262 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
263 return;
264 } else {
265 // Will use an op for the clear, reset the load op to discard since the op will
266 // blow away the color buffer contents
267 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
268 }
269 }
270
271 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
272 // as a load op.
273 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
274 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
275 if (clearAsDraw) {
276 GrPaint paint;
277 ClearToGrPaint(color, &paint);
278 auto op = FillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
279 SkRect::Make(scissorState.rect()));
280 this->addDrawOp(std::move(op));
281 } else {
282 color = this->writeSurfaceView().swizzle().applyTo(color);
283 this->addOp(ClearOp::MakeColor(fContext, scissorState, color));
284 }
285 }
286
287 #ifdef SK_DEBUG
onValidate() const288 void SurfaceFillContext::onValidate() const {
289 if (fOpsTask && !fOpsTask->isClosed()) {
290 SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
291 }
292 }
293 #endif
294
295 } // namespace skgpu::ganesh
296