blob: 15de5ced7c5a473eb5b2a2055f8f1aa088e37f86 [file] [log] [blame]
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001/*
2 * Copyright 2019 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
Adlai Hollera0693042020-10-14 11:23:11 -04008#include "src/gpu/GrDirectContextPriv.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContextThreadSafeProxy.h"
Robert Phillips4e105e22020-07-16 09:18:50 -040011#include "include/gpu/GrDirectContext.h"
Brian Osman4c886ee2021-07-07 13:34:50 -040012#include "src/core/SkRuntimeEffectPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrContextThreadSafeProxyPriv.h"
15#include "src/gpu/GrDrawingManager.h"
16#include "src/gpu/GrGpu.h"
17#include "src/gpu/GrMemoryPool.h"
Robert Phillips516405c2021-06-04 16:37:30 -040018#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050019#include "src/gpu/GrSurfaceContext.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050020#include "src/gpu/GrSurfaceDrawContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.h"
Robert Phillipsae67c522021-03-03 11:03:38 -050022#include "src/gpu/GrThreadSafePipelineBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050024#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman4c886ee2021-07-07 13:34:50 -040025#include "src/gpu/effects/GrTextureEffect.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040026#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/text/GrTextBlobCache.h"
28#include "src/image/SkImage_Base.h"
29#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050030
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040031#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsdbaf3172019-02-06 15:12:53 -050032 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Adlai Holler33dbd652020-06-01 12:35:42 -040033#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->singleOwner())
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040034#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
Robert Phillipsdbaf3172019-02-06 15:12:53 -050035
Adlai Hollera0693042020-10-14 11:23:11 -040036sk_sp<const GrCaps> GrDirectContextPriv::refCaps() const {
Robert Phillipsa41c6852019-02-07 10:44:10 -050037 return fContext->refCaps();
38}
39
Adlai Hollera0693042020-10-14 11:23:11 -040040void GrDirectContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
Robert Phillipsc5058a62019-02-15 12:52:59 -050041 fContext->addOnFlushCallbackObject(onFlushCBObject);
42}
43
Robert Phillips80bfda82020-11-12 09:23:36 -050044GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(
45 SkSpan<GrSurfaceProxy*> proxies,
46 SkSurface::BackendSurfaceAccess access,
47 const GrFlushInfo& info,
48 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040049 ASSERT_SINGLE_OWNER
Adlai Hollera0693042020-10-14 11:23:11 -040050 GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", fContext);
Robert Phillips80bfda82020-11-12 09:23:36 -050051
52 if (fContext->abandoned()) {
53 if (info.fSubmittedProc) {
54 info.fSubmittedProc(info.fSubmittedContext, false);
55 }
56 if (info.fFinishedProc) {
57 info.fFinishedProc(info.fFinishedContext);
58 }
59 return GrSemaphoresSubmitted::kNo;
60 }
61
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050062#ifdef SK_DEBUG
63 for (GrSurfaceProxy* proxy : proxies) {
64 SkASSERT(proxy);
65 ASSERT_OWNED_PROXY(proxy);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040066 }
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050067#endif
Robert Phillips80bfda82020-11-12 09:23:36 -050068 return fContext->drawingManager()->flushSurfaces(proxies, access, info, newState);
Brian Salomon693bc2b2019-05-09 13:48:00 +000069}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050070
Robert Phillips07f675d2020-11-16 13:44:01 -050071void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -050072 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -050073 SkIPoint offset) {
Robert Phillipseb54bb52021-01-08 17:20:18 -050074 fContext->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset);
Adlai Holler7580ad42020-06-24 13:45:25 -040075}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050076
Adlai Hollera0693042020-10-14 11:23:11 -040077bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050078 GrGpu* gpu = this->getGpu();
79 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040080 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050081 }
82
Robert Phillips43e7e4f2020-05-06 13:34:45 -040083 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050084}
85
86
Robert Phillipsdbaf3172019-02-06 15:12:53 -050087//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050088#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050089
Adlai Hollera0693042020-10-14 11:23:11 -040090void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050091#if GR_CACHE_STATS
92 fContext->fResourceCache->dumpStats(out);
93#endif
94}
95
Adlai Hollera0693042020-10-14 11:23:11 -040096void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
97 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050098#if GR_CACHE_STATS
99 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
100#endif
101}
102
Adlai Hollera0693042020-10-14 11:23:11 -0400103void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500104 SkString out;
105 this->dumpCacheStats(&out);
106 SkDebugf("%s", out.c_str());
107}
108
Robert Phillips273f1072020-05-05 13:03:07 -0400109/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400110void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400111#if GR_GPU_STATS
112 fContext->fGpu->stats()->reset();
113#endif
114}
115
Adlai Hollera0693042020-10-14 11:23:11 -0400116void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500117#if GR_GPU_STATS
Robert Phillipsae67c522021-03-03 11:03:38 -0500118 fContext->fGpu->stats()->dump(out);
119 if (auto builder = fContext->fGpu->pipelineBuilder()) {
120 builder->stats()->dump(out);
121 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500122#endif
123}
124
Adlai Hollera0693042020-10-14 11:23:11 -0400125void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
126 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500127#if GR_GPU_STATS
Robert Phillipsae67c522021-03-03 11:03:38 -0500128 fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
129 if (auto builder = fContext->fGpu->pipelineBuilder()) {
130 builder->stats()->dumpKeyValuePairs(keys, values);
131 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500132#endif
133}
134
Adlai Hollera0693042020-10-14 11:23:11 -0400135void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500136 SkString out;
137 this->dumpGpuStats(&out);
138 SkDebugf("%s", out.c_str());
139}
140
Robert Phillips273f1072020-05-05 13:03:07 -0400141/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400142void GrDirectContextPriv::resetContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400143#if GR_GPU_STATS
144 fContext->stats()->reset();
145#endif
146}
147
Adlai Hollera0693042020-10-14 11:23:11 -0400148void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400149#if GR_GPU_STATS
150 return fContext->stats()->dump(out);
151#endif
152}
153
Adlai Hollera0693042020-10-14 11:23:11 -0400154void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
155 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400156#if GR_GPU_STATS
157 return fContext->stats()->dumpKeyValuePairs(keys, values);
158#endif
159}
160
Adlai Hollera0693042020-10-14 11:23:11 -0400161void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400162 SkString out;
163 this->dumpContextStats(&out);
164 SkDebugf("%s", out.c_str());
165}
166
167/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400168sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
169 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500170 auto atlasManager = this->getAtlasManager();
171 if (!atlasManager) {
172 return nullptr;
173 }
174
175 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500176 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
177 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500178 return nullptr;
179 }
180
Greg Daniel7c165a42020-01-22 12:22:36 -0500181 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500182 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon9a56eb72021-04-20 16:52:11 -0400183 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(fContext),
184 kNeedNewImageUniqueID,
185 views[index],
186 SkColorInfo(colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500187}
188
Adlai Hollera0693042020-10-14 11:23:11 -0400189void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
190 GrOnFlushCallbackObject* cb) {
191 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500192 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500193}
194#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400195
Brian Osman4c886ee2021-07-07 13:34:50 -0400196// Both of these effects aggressively round to the nearest exact (N / 255) floating point values.
197// This lets us find a round-trip preserving pair on some GPUs that do odd byte to float conversion.
198static std::unique_ptr<GrFragmentProcessor> make_premul_effect(
199 std::unique_ptr<GrFragmentProcessor> fp) {
200 if (!fp) {
201 return nullptr;
202 }
203
204 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
205 half4 main(half4 color) {
206 color = floor(color * 255 + 0.5) / 255;
207 color.rgb = floor(color.rgb * color.a * 255 + 0.5) / 255;
208 return color;
209 }
210 )");
211
212 fp = GrSkSLFP::Make(effect, "ToPremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
213 return GrFragmentProcessor::HighPrecision(std::move(fp));
214}
215
216static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect(
217 std::unique_ptr<GrFragmentProcessor> fp) {
218 if (!fp) {
219 return nullptr;
220 }
221
222 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
223 half4 main(half4 color) {
224 color = floor(color * 255 + 0.5) / 255;
225 color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255;
226 return color;
227 }
228 )");
229
230 fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
231 return GrFragmentProcessor::HighPrecision(std::move(fp));
232}
233
234static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) {
235 static constexpr int kSize = 256;
236 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
237 uint32_t* srcData = data.get();
238
239 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
240 // values in row y. We set r, g, and b to the same value since they are handled identically.
241 for (int y = 0; y < kSize; ++y) {
242 for (int x = 0; x < kSize; ++x) {
243 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
244 color[3] = y;
245 color[2] = std::min(x, y);
246 color[1] = std::min(x, y);
247 color[0] = std::min(x, y);
248 }
249 }
250
251 const SkImageInfo pmII =
252 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
253 const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType);
254
255 auto readSFC = GrSurfaceFillContext::Make(dContext, upmII, SkBackingFit::kExact);
256 auto tempSFC = GrSurfaceFillContext::Make(dContext, pmII, SkBackingFit::kExact);
257 if (!readSFC || !tempSFC) {
258 return false;
259 }
260
261 // This function is only ever called if we are in a GrDirectContext since we are calling read
262 // pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the
263 // pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
264 SkBitmap bitmap;
265 bitmap.installPixels(pmII, srcData, 4 * kSize);
266 bitmap.setImmutable();
267
268 auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap));
269 if (!dataView) {
270 return false;
271 }
272
273 uint32_t* firstRead = data.get() + kSize*kSize;
274 uint32_t* secondRead = data.get() + 2*kSize*kSize;
275 std::fill_n( firstRead, kSize*kSize, 0);
276 std::fill_n(secondRead, kSize*kSize, 0);
277
278 GrPixmap firstReadPM( upmII, firstRead, kSize*sizeof(uint32_t));
279 GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t));
280
281 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
282 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
283 // We then verify that two reads produced the same values.
284
285 auto fp1 = make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), bitmap.alphaType()));
286 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1));
287 if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) {
288 return false;
289 }
290
291 auto fp2 = make_premul_effect(
292 GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType()));
293 tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2));
294
295 auto fp3 = make_unpremul_effect(
296 GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType()));
297 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3));
298
299 if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) {
300 return false;
301 }
302
303 for (int y = 0; y < kSize; ++y) {
304 for (int x = 0; x <= y; ++x) {
305 if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) {
306 return false;
307 }
308 }
309 }
310
311 return true;
312}
313
Adlai Hollera0693042020-10-14 11:23:11 -0400314bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400315 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400316
Greg Daniel6eb8c242019-06-05 10:22:24 -0400317 if (!fContext->fDidTestPMConversions) {
Brian Osman4c886ee2021-07-07 13:34:50 -0400318 fContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(fContext);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400319 fContext->fDidTestPMConversions = true;
320 }
321
322 // The PM<->UPM tests fail or succeed together so we only need to check one.
323 return fContext->fPMUPMConversionsRoundTrip;
324}
325
Adlai Hollera0693042020-10-14 11:23:11 -0400326std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400327 std::unique_ptr<GrFragmentProcessor> fp) {
328 ASSERT_SINGLE_OWNER
329 // We should have already called this->priv().validPMUPMConversionExists() in this case
330 SkASSERT(fContext->fDidTestPMConversions);
331 // ...and it should have succeeded
332 SkASSERT(this->validPMUPMConversionExists());
333
Brian Osman4c886ee2021-07-07 13:34:50 -0400334 return make_unpremul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335}
336
Adlai Hollera0693042020-10-14 11:23:11 -0400337std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400338 std::unique_ptr<GrFragmentProcessor> fp) {
339 ASSERT_SINGLE_OWNER
340 // We should have already called this->priv().validPMUPMConversionExists() in this case
341 SkASSERT(fContext->fDidTestPMConversions);
342 // ...and it should have succeeded
343 SkASSERT(this->validPMUPMConversionExists());
344
Brian Osman4c886ee2021-07-07 13:34:50 -0400345 return make_premul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400346}
Robert Phillips516405c2021-06-04 16:37:30 -0400347
Robert Phillipscc44feb2021-07-06 12:21:37 -0400348sk_sp<skgpu::BaseDevice> GrDirectContextPriv::createDevice(GrColorType colorType,
349 sk_sp<GrSurfaceProxy> proxy,
350 sk_sp<SkColorSpace> colorSpace,
351 GrSurfaceOrigin origin,
352 const SkSurfaceProps& props,
353 skgpu::BaseDevice::InitContents init) {
Robert Phillips516405c2021-06-04 16:37:30 -0400354 return fContext->GrRecordingContext::priv().createDevice(colorType, std::move(proxy),
355 std::move(colorSpace),
356 origin, props, init);
357}
358
Robert Phillipscc44feb2021-07-06 12:21:37 -0400359sk_sp<skgpu::BaseDevice> GrDirectContextPriv::createDevice(SkBudgeted budgeted,
360 const SkImageInfo& ii,
361 SkBackingFit fit,
362 int sampleCount,
363 GrMipmapped mipmapped,
364 GrProtected isProtected,
365 GrSurfaceOrigin origin,
366 const SkSurfaceProps& props,
367 skgpu::BaseDevice::InitContents init) {
Robert Phillips516405c2021-06-04 16:37:30 -0400368 return fContext->GrRecordingContext::priv().createDevice(budgeted, ii, fit, sampleCount,
369 mipmapped, isProtected,
370 origin, props, init);
371}