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 GrDrawingManager* drawingMgr = this->drawingManager();
115 this->getOpsTask()->addOp(drawingMgr,
116 std::move(op),
117 GrTextureResolveManager(drawingMgr),
118 *this->caps());
119 }
120
getOpsTask()121 OpsTask* SurfaceFillContext::getOpsTask() {
122 ASSERT_SINGLE_OWNER
123 SkDEBUGCODE(this->validate();)
124
125 if (!fOpsTask || fOpsTask->isClosed()) {
126 this->replaceOpsTask();
127 }
128 SkASSERT(!fOpsTask->isClosed());
129 return fOpsTask.get();
130 }
131
refRenderTask()132 sk_sp<GrRenderTask> SurfaceFillContext::refRenderTask() {
133 return sk_ref_sp(this->getOpsTask());
134 }
135
replaceOpsTask()136 OpsTask* SurfaceFillContext::replaceOpsTask() {
137 sk_sp<OpsTask> newOpsTask = this->drawingManager()->newOpsTask(
138 this->writeSurfaceView(), this->arenas(), fFlushTimeOpsTask);
139 this->willReplaceOpsTask(fOpsTask.get(), newOpsTask.get());
140 fOpsTask = std::move(newOpsTask);
141 return fOpsTask.get();
142 }
143
144 #ifdef SK_DEBUG
onValidate() const145 void SurfaceFillContext::onValidate() const {
146 if (fOpsTask && !fOpsTask->isClosed()) {
147 SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
148 }
149 }
150 #endif
151
discard()152 void SurfaceFillContext::discard() {
153 ASSERT_SINGLE_OWNER
154 RETURN_IF_ABANDONED
155 SkDEBUGCODE(this->validate();)
156 GR_CREATE_TRACE_MARKER_CONTEXT("v1::SurfaceFillContext", "discard", fContext);
157
158 AutoCheckFlush acf(this->drawingManager());
159
160 this->getOpsTask()->discard();
161 }
162
internalClear(const SkIRect * scissor,std::array<float,4> color,bool upgradePartialToFull)163 void SurfaceFillContext::internalClear(const SkIRect* scissor,
164 std::array<float, 4> color,
165 bool upgradePartialToFull) {
166 ASSERT_SINGLE_OWNER
167 RETURN_IF_ABANDONED
168 SkDEBUGCODE(this->validate();)
169 GR_CREATE_TRACE_MARKER_CONTEXT("v1::SurfaceFillContext", "clear", fContext);
170
171 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
172 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
173 // supports then. Drawing an axis-aligned rect is the fallback path.
174 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
175 if (scissor && !scissorState.set(*scissor)) {
176 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
177 // except clear ops are not draw ops).
178 return;
179 }
180
181 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
182 // the test. We only do this when the clear would be handled by a load op or natively.
183 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
184 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
185 this->caps()->shouldInitializeTextures())) {
186 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
187 // only clear the entire target if we knew it had not been cleared before. As
188 // is this could end up doing a lot of redundant clears.
189 scissorState.setDisabled();
190 } else {
191 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
192 // render target to overflow into any approx-fit padding of the backing store dimensions
193 scissorState.relaxTest(this->dimensions());
194 }
195 }
196
197 if (!scissorState.enabled()) {
198 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
199 // discard all prior ops in the current task since the color buffer will be overwritten.
200 auto opsTask = this->getOpsTask();
201 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
202 !this->caps()->performColorClearsAsDraws()) {
203 color = this->writeSurfaceView().swizzle().applyTo(color);
204 // The op list was emptied and native clears are allowed, so just use the load op
205 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
206 return;
207 } else {
208 // Will use an op for the clear, reset the load op to discard since the op will
209 // blow away the color buffer contents
210 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
211 }
212 }
213
214 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
215 // as a load op.
216 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
217 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
218 if (clearAsDraw) {
219 GrPaint paint;
220 ClearToGrPaint(color, &paint);
221 auto op = FillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
222 SkRect::Make(scissorState.rect()));
223 this->addDrawOp(std::move(op));
224 } else {
225 color = this->writeSurfaceView().swizzle().applyTo(color);
226 this->addOp(ClearOp::MakeColor(fContext, scissorState, color));
227 }
228 }
229
blitTexture(GrSurfaceProxyView view,const SkIRect & srcRect,const SkIPoint & dstPoint)230 bool SurfaceFillContext::blitTexture(GrSurfaceProxyView view,
231 const SkIRect& srcRect,
232 const SkIPoint& dstPoint) {
233 SkASSERT(view.asTextureProxy());
234 SkIRect clippedSrcRect;
235 SkIPoint clippedDstPoint;
236 if (!GrClipSrcRectAndDstPoint(this->dimensions(),
237 view.dimensions(),
238 srcRect,
239 dstPoint,
240 &clippedSrcRect,
241 &clippedDstPoint)) {
242 return false;
243 }
244
245 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
246 auto dstRect = SkIRect::MakePtSize(clippedDstPoint, clippedSrcRect.size());
247 auto srcRectF = SkRect::Make(clippedSrcRect);
248 this->fillRectToRectWithFP(srcRectF, dstRect, std::move(fp));
249 return true;
250 }
251
252 } // namespace skgpu::v1
253