blob: b00a1a270656d8880f421ab0b2653aa0da7325e8 [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.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"
Brian Salomoneebe7352020-12-09 16:37:04 -050019#include "src/gpu/GrSurfaceDrawContext.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 Osman1c467772021-07-02 13:11:15 +000024#include "src/gpu/effects/generated/GrConfigConversionEffect.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
Adlai Hollera0693042020-10-14 11:23:11 -0400195bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400197
Greg Daniel6eb8c242019-06-05 10:22:24 -0400198 if (!fContext->fDidTestPMConversions) {
Brian Osman1c467772021-07-02 13:11:15 +0000199 fContext->fPMUPMConversionsRoundTrip =
200 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400201 fContext->fDidTestPMConversions = true;
202 }
203
204 // The PM<->UPM tests fail or succeed together so we only need to check one.
205 return fContext->fPMUPMConversionsRoundTrip;
206}
207
Adlai Hollera0693042020-10-14 11:23:11 -0400208std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400209 std::unique_ptr<GrFragmentProcessor> fp) {
210 ASSERT_SINGLE_OWNER
211 // We should have already called this->priv().validPMUPMConversionExists() in this case
212 SkASSERT(fContext->fDidTestPMConversions);
213 // ...and it should have succeeded
214 SkASSERT(this->validPMUPMConversionExists());
215
Brian Osman1c467772021-07-02 13:11:15 +0000216 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400217}
218
Adlai Hollera0693042020-10-14 11:23:11 -0400219std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400220 std::unique_ptr<GrFragmentProcessor> fp) {
221 ASSERT_SINGLE_OWNER
222 // We should have already called this->priv().validPMUPMConversionExists() in this case
223 SkASSERT(fContext->fDidTestPMConversions);
224 // ...and it should have succeeded
225 SkASSERT(this->validPMUPMConversionExists());
226
Brian Osman1c467772021-07-02 13:11:15 +0000227 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400228}
Robert Phillips516405c2021-06-04 16:37:30 -0400229
230sk_sp<SkBaseGpuDevice> GrDirectContextPriv::createDevice(GrColorType colorType,
231 sk_sp<GrSurfaceProxy> proxy,
232 sk_sp<SkColorSpace> colorSpace,
233 GrSurfaceOrigin origin,
234 const SkSurfaceProps& props,
235 SkBaseGpuDevice::InitContents init) {
236 return fContext->GrRecordingContext::priv().createDevice(colorType, std::move(proxy),
237 std::move(colorSpace),
238 origin, props, init);
239}
240
241sk_sp<SkBaseGpuDevice> GrDirectContextPriv::createDevice(SkBudgeted budgeted,
242 const SkImageInfo& ii,
243 SkBackingFit fit,
244 int sampleCount,
245 GrMipmapped mipmapped,
246 GrProtected isProtected,
247 GrSurfaceOrigin origin,
248 const SkSurfaceProps& props,
249 SkBaseGpuDevice::InitContents init) {
250 return fContext->GrRecordingContext::priv().createDevice(budgeted, ii, fit, sampleCount,
251 mipmapped, isProtected,
252 origin, props, init);
253}