blob: ba08749a9ed3bf3dc84547e1b8b24b11997ee3a9 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextThreadSafeProxyPriv.h"
14#include "src/gpu/GrDrawingManager.h"
15#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrMemoryPool.h"
Robert Phillips516405c2021-06-04 16:37:30 -040017#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050018#include "src/gpu/GrSurfaceContext.h"
Robert Phillips400f52e2021-07-26 13:23:10 -040019#include "src/gpu/GrSurfaceFillContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.h"
Robert Phillipsae67c522021-03-03 11:03:38 -050021#include "src/gpu/GrThreadSafePipelineBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/SkGr.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050023#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman4c886ee2021-07-07 13:34:50 -040024#include "src/gpu/effects/GrTextureEffect.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/text/GrTextBlobCache.h"
27#include "src/image/SkImage_Base.h"
28#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050029
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040030#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsdbaf3172019-02-06 15:12:53 -050031 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Adlai Holler33dbd652020-06-01 12:35:42 -040032#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->singleOwner())
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040033#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
Robert Phillipsdbaf3172019-02-06 15:12:53 -050034
Adlai Hollera0693042020-10-14 11:23:11 -040035sk_sp<const GrCaps> GrDirectContextPriv::refCaps() const {
Robert Phillipsa41c6852019-02-07 10:44:10 -050036 return fContext->refCaps();
37}
38
Adlai Hollera0693042020-10-14 11:23:11 -040039void GrDirectContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
Robert Phillipsc5058a62019-02-15 12:52:59 -050040 fContext->addOnFlushCallbackObject(onFlushCBObject);
41}
42
Robert Phillips80bfda82020-11-12 09:23:36 -050043GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(
44 SkSpan<GrSurfaceProxy*> proxies,
45 SkSurface::BackendSurfaceAccess access,
46 const GrFlushInfo& info,
47 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040048 ASSERT_SINGLE_OWNER
Adlai Hollera0693042020-10-14 11:23:11 -040049 GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", fContext);
Robert Phillips80bfda82020-11-12 09:23:36 -050050
51 if (fContext->abandoned()) {
52 if (info.fSubmittedProc) {
53 info.fSubmittedProc(info.fSubmittedContext, false);
54 }
55 if (info.fFinishedProc) {
56 info.fFinishedProc(info.fFinishedContext);
57 }
58 return GrSemaphoresSubmitted::kNo;
59 }
60
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050061#ifdef SK_DEBUG
62 for (GrSurfaceProxy* proxy : proxies) {
63 SkASSERT(proxy);
64 ASSERT_OWNED_PROXY(proxy);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040065 }
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050066#endif
Robert Phillips80bfda82020-11-12 09:23:36 -050067 return fContext->drawingManager()->flushSurfaces(proxies, access, info, newState);
Brian Salomon693bc2b2019-05-09 13:48:00 +000068}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050069
Robert Phillips07f675d2020-11-16 13:44:01 -050070void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -050071 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -050072 SkIPoint offset) {
Robert Phillipseb54bb52021-01-08 17:20:18 -050073 fContext->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset);
Adlai Holler7580ad42020-06-24 13:45:25 -040074}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050075
Adlai Hollera0693042020-10-14 11:23:11 -040076bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050077 GrGpu* gpu = this->getGpu();
78 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040079 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050080 }
81
Robert Phillips43e7e4f2020-05-06 13:34:45 -040082 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050083}
84
85
Robert Phillipsdbaf3172019-02-06 15:12:53 -050086//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050087#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050088
Adlai Hollera0693042020-10-14 11:23:11 -040089void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050090#if GR_CACHE_STATS
91 fContext->fResourceCache->dumpStats(out);
92#endif
93}
94
Adlai Hollera0693042020-10-14 11:23:11 -040095void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
96 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050097#if GR_CACHE_STATS
98 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
99#endif
100}
101
Adlai Hollera0693042020-10-14 11:23:11 -0400102void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500103 SkString out;
104 this->dumpCacheStats(&out);
105 SkDebugf("%s", out.c_str());
106}
107
Robert Phillips273f1072020-05-05 13:03:07 -0400108/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400109void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400110#if GR_GPU_STATS
111 fContext->fGpu->stats()->reset();
112#endif
113}
114
Adlai Hollera0693042020-10-14 11:23:11 -0400115void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500116#if GR_GPU_STATS
Robert Phillipsae67c522021-03-03 11:03:38 -0500117 fContext->fGpu->stats()->dump(out);
118 if (auto builder = fContext->fGpu->pipelineBuilder()) {
119 builder->stats()->dump(out);
120 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500121#endif
122}
123
Adlai Hollera0693042020-10-14 11:23:11 -0400124void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
125 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500126#if GR_GPU_STATS
Robert Phillipsae67c522021-03-03 11:03:38 -0500127 fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
128 if (auto builder = fContext->fGpu->pipelineBuilder()) {
129 builder->stats()->dumpKeyValuePairs(keys, values);
130 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500131#endif
132}
133
Adlai Hollera0693042020-10-14 11:23:11 -0400134void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500135 SkString out;
136 this->dumpGpuStats(&out);
137 SkDebugf("%s", out.c_str());
138}
139
Robert Phillips273f1072020-05-05 13:03:07 -0400140/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400141void GrDirectContextPriv::resetContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400142#if GR_GPU_STATS
143 fContext->stats()->reset();
144#endif
145}
146
Adlai Hollera0693042020-10-14 11:23:11 -0400147void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400148#if GR_GPU_STATS
149 return fContext->stats()->dump(out);
150#endif
151}
152
Adlai Hollera0693042020-10-14 11:23:11 -0400153void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
154 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400155#if GR_GPU_STATS
156 return fContext->stats()->dumpKeyValuePairs(keys, values);
157#endif
158}
159
Adlai Hollera0693042020-10-14 11:23:11 -0400160void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400161 SkString out;
162 this->dumpContextStats(&out);
163 SkDebugf("%s", out.c_str());
164}
165
166/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400167sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
168 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500169 auto atlasManager = this->getAtlasManager();
170 if (!atlasManager) {
171 return nullptr;
172 }
173
174 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500175 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
176 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500177 return nullptr;
178 }
179
Greg Daniel7c165a42020-01-22 12:22:36 -0500180 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500181 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon9a56eb72021-04-20 16:52:11 -0400182 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(fContext),
183 kNeedNewImageUniqueID,
184 views[index],
185 SkColorInfo(colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500186}
187
Adlai Hollera0693042020-10-14 11:23:11 -0400188void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
189 GrOnFlushCallbackObject* cb) {
190 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500191 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500192}
193#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400194
Brian Osman4c886ee2021-07-07 13:34:50 -0400195// Both of these effects aggressively round to the nearest exact (N / 255) floating point values.
196// This lets us find a round-trip preserving pair on some GPUs that do odd byte to float conversion.
197static std::unique_ptr<GrFragmentProcessor> make_premul_effect(
198 std::unique_ptr<GrFragmentProcessor> fp) {
199 if (!fp) {
200 return nullptr;
201 }
202
203 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
204 half4 main(half4 color) {
205 color = floor(color * 255 + 0.5) / 255;
206 color.rgb = floor(color.rgb * color.a * 255 + 0.5) / 255;
207 return color;
208 }
209 )");
210
211 fp = GrSkSLFP::Make(effect, "ToPremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
212 return GrFragmentProcessor::HighPrecision(std::move(fp));
213}
214
215static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect(
216 std::unique_ptr<GrFragmentProcessor> fp) {
217 if (!fp) {
218 return nullptr;
219 }
220
221 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
222 half4 main(half4 color) {
223 color = floor(color * 255 + 0.5) / 255;
224 color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255;
225 return color;
226 }
227 )");
228
229 fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
230 return GrFragmentProcessor::HighPrecision(std::move(fp));
231}
232
233static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) {
234 static constexpr int kSize = 256;
235 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
236 uint32_t* srcData = data.get();
237
238 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
239 // values in row y. We set r, g, and b to the same value since they are handled identically.
240 for (int y = 0; y < kSize; ++y) {
241 for (int x = 0; x < kSize; ++x) {
242 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
243 color[3] = y;
244 color[2] = std::min(x, y);
245 color[1] = std::min(x, y);
246 color[0] = std::min(x, y);
247 }
248 }
249
250 const SkImageInfo pmII =
251 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
252 const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType);
253
254 auto readSFC = GrSurfaceFillContext::Make(dContext, upmII, SkBackingFit::kExact);
255 auto tempSFC = GrSurfaceFillContext::Make(dContext, pmII, SkBackingFit::kExact);
256 if (!readSFC || !tempSFC) {
257 return false;
258 }
259
260 // This function is only ever called if we are in a GrDirectContext since we are calling read
261 // pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the
262 // pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
263 SkBitmap bitmap;
264 bitmap.installPixels(pmII, srcData, 4 * kSize);
265 bitmap.setImmutable();
266
267 auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap));
268 if (!dataView) {
269 return false;
270 }
271
272 uint32_t* firstRead = data.get() + kSize*kSize;
273 uint32_t* secondRead = data.get() + 2*kSize*kSize;
274 std::fill_n( firstRead, kSize*kSize, 0);
275 std::fill_n(secondRead, kSize*kSize, 0);
276
277 GrPixmap firstReadPM( upmII, firstRead, kSize*sizeof(uint32_t));
278 GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t));
279
280 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
281 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
282 // We then verify that two reads produced the same values.
283
284 auto fp1 = make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), bitmap.alphaType()));
285 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1));
286 if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) {
287 return false;
288 }
289
290 auto fp2 = make_premul_effect(
291 GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType()));
292 tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2));
293
294 auto fp3 = make_unpremul_effect(
295 GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType()));
296 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3));
297
298 if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) {
299 return false;
300 }
301
302 for (int y = 0; y < kSize; ++y) {
303 for (int x = 0; x <= y; ++x) {
304 if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) {
305 return false;
306 }
307 }
308 }
309
310 return true;
311}
312
Adlai Hollera0693042020-10-14 11:23:11 -0400313bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400315
Greg Daniel6eb8c242019-06-05 10:22:24 -0400316 if (!fContext->fDidTestPMConversions) {
Brian Osman4c886ee2021-07-07 13:34:50 -0400317 fContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(fContext);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400318 fContext->fDidTestPMConversions = true;
319 }
320
321 // The PM<->UPM tests fail or succeed together so we only need to check one.
322 return fContext->fPMUPMConversionsRoundTrip;
323}
324
Adlai Hollera0693042020-10-14 11:23:11 -0400325std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400326 std::unique_ptr<GrFragmentProcessor> fp) {
327 ASSERT_SINGLE_OWNER
328 // We should have already called this->priv().validPMUPMConversionExists() in this case
329 SkASSERT(fContext->fDidTestPMConversions);
330 // ...and it should have succeeded
331 SkASSERT(this->validPMUPMConversionExists());
332
Brian Osman4c886ee2021-07-07 13:34:50 -0400333 return make_unpremul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400334}
335
Adlai Hollera0693042020-10-14 11:23:11 -0400336std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400337 std::unique_ptr<GrFragmentProcessor> fp) {
338 ASSERT_SINGLE_OWNER
339 // We should have already called this->priv().validPMUPMConversionExists() in this case
340 SkASSERT(fContext->fDidTestPMConversions);
341 // ...and it should have succeeded
342 SkASSERT(this->validPMUPMConversionExists());
343
Brian Osman4c886ee2021-07-07 13:34:50 -0400344 return make_premul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400345}
Robert Phillips516405c2021-06-04 16:37:30 -0400346
Robert Phillipscc44feb2021-07-06 12:21:37 -0400347sk_sp<skgpu::BaseDevice> GrDirectContextPriv::createDevice(GrColorType colorType,
348 sk_sp<GrSurfaceProxy> proxy,
349 sk_sp<SkColorSpace> colorSpace,
350 GrSurfaceOrigin origin,
351 const SkSurfaceProps& props,
352 skgpu::BaseDevice::InitContents init) {
Robert Phillips516405c2021-06-04 16:37:30 -0400353 return fContext->GrRecordingContext::priv().createDevice(colorType, std::move(proxy),
354 std::move(colorSpace),
355 origin, props, init);
356}
357
Robert Phillipscc44feb2021-07-06 12:21:37 -0400358sk_sp<skgpu::BaseDevice> GrDirectContextPriv::createDevice(SkBudgeted budgeted,
359 const SkImageInfo& ii,
360 SkBackingFit fit,
361 int sampleCount,
362 GrMipmapped mipmapped,
363 GrProtected isProtected,
364 GrSurfaceOrigin origin,
365 const SkSurfaceProps& props,
366 skgpu::BaseDevice::InitContents init) {
Robert Phillips516405c2021-06-04 16:37:30 -0400367 return fContext->GrRecordingContext::priv().createDevice(budgeted, ii, fit, sampleCount,
368 mipmapped, isProtected,
369 origin, props, init);
370}