blob: 9f0712fd45f51795fbbfe193dab85b47fef7b499 [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"
Greg Daniel46e366a2019-12-16 14:38:36 -050017#include "src/gpu/GrSurfaceContext.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050018#include "src/gpu/GrSurfaceDrawContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrTexture.h"
Robert Phillipsae67c522021-03-03 11:03:38 -050020#include "src/gpu/GrThreadSafePipelineBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/SkGr.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050022#include "src/gpu/effects/GrSkSLFP.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040023#include "src/gpu/effects/generated/GrConfigConversionEffect.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040024#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/text/GrTextBlobCache.h"
26#include "src/image/SkImage_Base.h"
27#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050028
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040029#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsdbaf3172019-02-06 15:12:53 -050030 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Adlai Holler33dbd652020-06-01 12:35:42 -040031#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->singleOwner())
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040032#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
Robert Phillipsdbaf3172019-02-06 15:12:53 -050033
Adlai Hollera0693042020-10-14 11:23:11 -040034sk_sp<const GrCaps> GrDirectContextPriv::refCaps() const {
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 return fContext->refCaps();
36}
37
Adlai Hollera0693042020-10-14 11:23:11 -040038void GrDirectContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
Robert Phillipsc5058a62019-02-15 12:52:59 -050039 fContext->addOnFlushCallbackObject(onFlushCBObject);
40}
41
Robert Phillips80bfda82020-11-12 09:23:36 -050042GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(
43 SkSpan<GrSurfaceProxy*> proxies,
44 SkSurface::BackendSurfaceAccess access,
45 const GrFlushInfo& info,
46 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040047 ASSERT_SINGLE_OWNER
Adlai Hollera0693042020-10-14 11:23:11 -040048 GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", fContext);
Robert Phillips80bfda82020-11-12 09:23:36 -050049
50 if (fContext->abandoned()) {
51 if (info.fSubmittedProc) {
52 info.fSubmittedProc(info.fSubmittedContext, false);
53 }
54 if (info.fFinishedProc) {
55 info.fFinishedProc(info.fFinishedContext);
56 }
57 return GrSemaphoresSubmitted::kNo;
58 }
59
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050060#ifdef SK_DEBUG
61 for (GrSurfaceProxy* proxy : proxies) {
62 SkASSERT(proxy);
63 ASSERT_OWNED_PROXY(proxy);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040064 }
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050065#endif
Robert Phillips80bfda82020-11-12 09:23:36 -050066 return fContext->drawingManager()->flushSurfaces(proxies, access, info, newState);
Brian Salomon693bc2b2019-05-09 13:48:00 +000067}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050068
Robert Phillips07f675d2020-11-16 13:44:01 -050069void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -050070 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -050071 SkIPoint offset) {
Robert Phillipseb54bb52021-01-08 17:20:18 -050072 fContext->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset);
Adlai Holler7580ad42020-06-24 13:45:25 -040073}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050074
Adlai Hollera0693042020-10-14 11:23:11 -040075bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050076 GrGpu* gpu = this->getGpu();
77 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040078 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050079 }
80
Robert Phillips43e7e4f2020-05-06 13:34:45 -040081 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050082}
83
84
Robert Phillipsdbaf3172019-02-06 15:12:53 -050085//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050086#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050087
Adlai Hollera0693042020-10-14 11:23:11 -040088void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050089#if GR_CACHE_STATS
90 fContext->fResourceCache->dumpStats(out);
91#endif
92}
93
Adlai Hollera0693042020-10-14 11:23:11 -040094void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
95 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050096#if GR_CACHE_STATS
97 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
98#endif
99}
100
Adlai Hollera0693042020-10-14 11:23:11 -0400101void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500102 SkString out;
103 this->dumpCacheStats(&out);
104 SkDebugf("%s", out.c_str());
105}
106
Robert Phillips273f1072020-05-05 13:03:07 -0400107/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400108void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400109#if GR_GPU_STATS
110 fContext->fGpu->stats()->reset();
111#endif
112}
113
Adlai Hollera0693042020-10-14 11:23:11 -0400114void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500115#if GR_GPU_STATS
Robert Phillipsae67c522021-03-03 11:03:38 -0500116 fContext->fGpu->stats()->dump(out);
117 if (auto builder = fContext->fGpu->pipelineBuilder()) {
118 builder->stats()->dump(out);
119 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500120#endif
121}
122
Adlai Hollera0693042020-10-14 11:23:11 -0400123void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
124 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500125#if GR_GPU_STATS
Robert Phillipsae67c522021-03-03 11:03:38 -0500126 fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
127 if (auto builder = fContext->fGpu->pipelineBuilder()) {
128 builder->stats()->dumpKeyValuePairs(keys, values);
129 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500130#endif
131}
132
Adlai Hollera0693042020-10-14 11:23:11 -0400133void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500134 SkString out;
135 this->dumpGpuStats(&out);
136 SkDebugf("%s", out.c_str());
137}
138
Robert Phillips273f1072020-05-05 13:03:07 -0400139/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400140void GrDirectContextPriv::resetContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400141#if GR_GPU_STATS
142 fContext->stats()->reset();
143#endif
144}
145
Adlai Hollera0693042020-10-14 11:23:11 -0400146void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400147#if GR_GPU_STATS
148 return fContext->stats()->dump(out);
149#endif
150}
151
Adlai Hollera0693042020-10-14 11:23:11 -0400152void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
153 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400154#if GR_GPU_STATS
155 return fContext->stats()->dumpKeyValuePairs(keys, values);
156#endif
157}
158
Adlai Hollera0693042020-10-14 11:23:11 -0400159void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400160 SkString out;
161 this->dumpContextStats(&out);
162 SkDebugf("%s", out.c_str());
163}
164
165/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400166sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
167 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500168 auto atlasManager = this->getAtlasManager();
169 if (!atlasManager) {
170 return nullptr;
171 }
172
173 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500174 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
175 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500176 return nullptr;
177 }
178
Greg Daniel7c165a42020-01-22 12:22:36 -0500179 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500180 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon9a56eb72021-04-20 16:52:11 -0400181 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(fContext),
182 kNeedNewImageUniqueID,
183 views[index],
184 SkColorInfo(colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500185}
186
Adlai Hollera0693042020-10-14 11:23:11 -0400187void GrDirectContextPriv::testingOnly_purgeAllUnlockedResources() {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500188 fContext->fResourceCache->purgeAllUnlocked();
189}
190
Adlai Hollera0693042020-10-14 11:23:11 -0400191void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
192 GrOnFlushCallbackObject* cb) {
193 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500194 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500195}
196#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400197
Adlai Hollera0693042020-10-14 11:23:11 -0400198bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400199 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400200
Greg Daniel6eb8c242019-06-05 10:22:24 -0400201 if (!fContext->fDidTestPMConversions) {
202 fContext->fPMUPMConversionsRoundTrip =
Adlai Hollera0693042020-10-14 11:23:11 -0400203 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400204 fContext->fDidTestPMConversions = true;
205 }
206
207 // The PM<->UPM tests fail or succeed together so we only need to check one.
208 return fContext->fPMUPMConversionsRoundTrip;
209}
210
Adlai Hollera0693042020-10-14 11:23:11 -0400211std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212 std::unique_ptr<GrFragmentProcessor> fp) {
213 ASSERT_SINGLE_OWNER
214 // We should have already called this->priv().validPMUPMConversionExists() in this case
215 SkASSERT(fContext->fDidTestPMConversions);
216 // ...and it should have succeeded
217 SkASSERT(this->validPMUPMConversionExists());
218
219 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
220}
221
Adlai Hollera0693042020-10-14 11:23:11 -0400222std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400223 std::unique_ptr<GrFragmentProcessor> fp) {
224 ASSERT_SINGLE_OWNER
225 // We should have already called this->priv().validPMUPMConversionExists() in this case
226 SkASSERT(fContext->fDidTestPMConversions);
227 // ...and it should have succeeded
228 SkASSERT(this->validPMUPMConversionExists());
229
230 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
231}