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