blob: 145946171e48b657e7a2ad95323afceb9bb7043e [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"
17#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050018#include "src/gpu/GrSurfaceContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040019#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.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 Phillipsa0e248d2020-11-11 09:39:27 -050069void GrDirectContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
70 GrRenderTargetProxy* newDest) {
71 fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest);
Adlai Holler7580ad42020-06-24 13:45:25 -040072}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050073
Adlai Hollera0693042020-10-14 11:23:11 -040074bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050075 GrGpu* gpu = this->getGpu();
76 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040077 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050078 }
79
Robert Phillips43e7e4f2020-05-06 13:34:45 -040080 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050081}
82
83
Robert Phillipsdbaf3172019-02-06 15:12:53 -050084//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050085#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050086
Adlai Hollera0693042020-10-14 11:23:11 -040087void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050088#if GR_CACHE_STATS
89 fContext->fResourceCache->dumpStats(out);
90#endif
91}
92
Adlai Hollera0693042020-10-14 11:23:11 -040093void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
94 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050095#if GR_CACHE_STATS
96 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
97#endif
98}
99
Adlai Hollera0693042020-10-14 11:23:11 -0400100void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500101 SkString out;
102 this->dumpCacheStats(&out);
103 SkDebugf("%s", out.c_str());
104}
105
Robert Phillips273f1072020-05-05 13:03:07 -0400106/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400107void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400108#if GR_GPU_STATS
109 fContext->fGpu->stats()->reset();
110#endif
111}
112
Adlai Hollera0693042020-10-14 11:23:11 -0400113void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500114#if GR_GPU_STATS
115 return fContext->fGpu->stats()->dump(out);
116#endif
117}
118
Adlai Hollera0693042020-10-14 11:23:11 -0400119void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
120 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500121#if GR_GPU_STATS
122 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
123#endif
124}
125
Adlai Hollera0693042020-10-14 11:23:11 -0400126void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500127 SkString out;
128 this->dumpGpuStats(&out);
129 SkDebugf("%s", out.c_str());
130}
131
Robert Phillips273f1072020-05-05 13:03:07 -0400132/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400133void GrDirectContextPriv::resetContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400134#if GR_GPU_STATS
135 fContext->stats()->reset();
136#endif
137}
138
Adlai Hollera0693042020-10-14 11:23:11 -0400139void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400140#if GR_GPU_STATS
141 return fContext->stats()->dump(out);
142#endif
143}
144
Adlai Hollera0693042020-10-14 11:23:11 -0400145void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
146 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400147#if GR_GPU_STATS
148 return fContext->stats()->dumpKeyValuePairs(keys, values);
149#endif
150}
151
Adlai Hollera0693042020-10-14 11:23:11 -0400152void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400153 SkString out;
154 this->dumpContextStats(&out);
155 SkDebugf("%s", out.c_str());
156}
157
158/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400159sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
160 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500161 auto atlasManager = this->getAtlasManager();
162 if (!atlasManager) {
163 return nullptr;
164 }
165
166 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500167 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
168 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500169 return nullptr;
170 }
171
Greg Daniel7c165a42020-01-22 12:22:36 -0500172 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500173 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000174 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500175 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500176 return image;
177}
178
Adlai Hollera0693042020-10-14 11:23:11 -0400179void GrDirectContextPriv::testingOnly_purgeAllUnlockedResources() {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500180 fContext->fResourceCache->purgeAllUnlocked();
181}
182
Adlai Hollera0693042020-10-14 11:23:11 -0400183void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
184 GrOnFlushCallbackObject* cb) {
185 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500186 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500187}
188#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400189
Adlai Hollera0693042020-10-14 11:23:11 -0400190bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400191 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400192
Greg Daniel6eb8c242019-06-05 10:22:24 -0400193 if (!fContext->fDidTestPMConversions) {
194 fContext->fPMUPMConversionsRoundTrip =
Adlai Hollera0693042020-10-14 11:23:11 -0400195 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196 fContext->fDidTestPMConversions = true;
197 }
198
199 // The PM<->UPM tests fail or succeed together so we only need to check one.
200 return fContext->fPMUPMConversionsRoundTrip;
201}
202
Adlai Hollera0693042020-10-14 11:23:11 -0400203std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400204 std::unique_ptr<GrFragmentProcessor> fp) {
205 ASSERT_SINGLE_OWNER
206 // We should have already called this->priv().validPMUPMConversionExists() in this case
207 SkASSERT(fContext->fDidTestPMConversions);
208 // ...and it should have succeeded
209 SkASSERT(this->validPMUPMConversionExists());
210
211 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
212}
213
Adlai Hollera0693042020-10-14 11:23:11 -0400214std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400215 std::unique_ptr<GrFragmentProcessor> fp) {
216 ASSERT_SINGLE_OWNER
217 // We should have already called this->priv().validPMUPMConversionExists() in this case
218 SkASSERT(fContext->fDidTestPMConversions);
219 // ...and it should have succeeded
220 SkASSERT(this->validPMUPMConversionExists());
221
222 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
223}