blob: 833f4531d63968a9495c0a8c9cb724deabeeca71 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrContextPriv.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"
Mike Klein4b432fa2019-06-06 11:44:05 -050020#include "src/gpu/GrSurfacePriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.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"
Greg Daniel6eb8c242019-06-05 10:22:24 -040024#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); }
34#define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void)
Robert Phillipsdbaf3172019-02-06 15:12:53 -050035
Robert Phillipsa41c6852019-02-07 10:44:10 -050036sk_sp<const GrCaps> GrContextPriv::refCaps() const {
37 return fContext->refCaps();
38}
39
Robert Phillipsc5058a62019-02-15 12:52:59 -050040void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
41 fContext->addOnFlushCallbackObject(onFlushCBObject);
42}
43
Greg Daniel55f040b2020-02-13 15:38:32 +000044GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040045 const GrFlushInfo& info) {
46 ASSERT_SINGLE_OWNER
47 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
48 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
49 SkASSERT(numProxies >= 0);
50 SkASSERT(!numProxies || proxies);
51 for (int i = 0; i < numProxies; ++i) {
52 SkASSERT(proxies[i]);
53 ASSERT_OWNED_PROXY(proxies[i]);
54 }
55 return fContext->drawingManager()->flushSurfaces(
Greg Daniel9efe3862020-06-11 11:51:06 -040056 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050057}
58
Greg Daniel55f040b2020-02-13 15:38:32 +000059void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
60 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +000061}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050062
Adlai Holler7580ad42020-06-24 13:45:25 -040063void GrContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
64 GrRenderTargetProxy* newDest) {
65 fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest);
66}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050067
Robert Phillips43e7e4f2020-05-06 13:34:45 -040068bool GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050069 GrGpu* gpu = this->getGpu();
70 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040071 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050072 }
73
Robert Phillips43e7e4f2020-05-06 13:34:45 -040074 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050075}
76
77
Robert Phillipsdbaf3172019-02-06 15:12:53 -050078//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050079#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050080
81void GrContextPriv::dumpCacheStats(SkString* out) const {
82#if GR_CACHE_STATS
83 fContext->fResourceCache->dumpStats(out);
84#endif
85}
86
87void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
88 SkTArray<double>* values) const {
89#if GR_CACHE_STATS
90 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
91#endif
92}
93
94void GrContextPriv::printCacheStats() const {
95 SkString out;
96 this->dumpCacheStats(&out);
97 SkDebugf("%s", out.c_str());
98}
99
Robert Phillips273f1072020-05-05 13:03:07 -0400100/////////////////////////////////////////////////
101void GrContextPriv::resetGpuStats() const {
102#if GR_GPU_STATS
103 fContext->fGpu->stats()->reset();
104#endif
105}
106
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500107void GrContextPriv::dumpGpuStats(SkString* out) const {
108#if GR_GPU_STATS
109 return fContext->fGpu->stats()->dump(out);
110#endif
111}
112
113void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
114 SkTArray<double>* values) const {
115#if GR_GPU_STATS
116 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
117#endif
118}
119
120void GrContextPriv::printGpuStats() const {
121 SkString out;
122 this->dumpGpuStats(&out);
123 SkDebugf("%s", out.c_str());
124}
125
Robert Phillips273f1072020-05-05 13:03:07 -0400126/////////////////////////////////////////////////
127void GrContextPriv::resetContextStats() const {
128#if GR_GPU_STATS
129 fContext->stats()->reset();
130#endif
131}
132
133void GrContextPriv::dumpContextStats(SkString* out) const {
134#if GR_GPU_STATS
135 return fContext->stats()->dump(out);
136#endif
137}
138
139void GrContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
140 SkTArray<double>* values) const {
141#if GR_GPU_STATS
142 return fContext->stats()->dumpKeyValuePairs(keys, values);
143#endif
144}
145
146void GrContextPriv::printContextStats() const {
147 SkString out;
148 this->dumpContextStats(&out);
149 SkDebugf("%s", out.c_str());
150}
151
152/////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500153sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
154 auto atlasManager = this->getAtlasManager();
155 if (!atlasManager) {
156 return nullptr;
157 }
158
159 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500160 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
161 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500162 return nullptr;
163 }
164
Greg Daniel7c165a42020-01-22 12:22:36 -0500165 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500166 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000167 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500168 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500169 return image;
170}
171
172void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
173 fContext->fResourceCache->purgeAllUnlocked();
174}
175
176void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
Greg Daniel0a2464f2020-05-14 15:45:44 -0400177 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500178 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500179}
180#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400181
182bool GrContextPriv::validPMUPMConversionExists() {
183 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400184
Robert Phillips4e105e22020-07-16 09:18:50 -0400185 // CONTEXT TODO: remove this downcast when this class becomes GrDirectContextPriv
186 auto direct = GrAsDirectContext(fContext);
187 SkASSERT(direct);
188
Greg Daniel6eb8c242019-06-05 10:22:24 -0400189 if (!fContext->fDidTestPMConversions) {
190 fContext->fPMUPMConversionsRoundTrip =
Robert Phillips4e105e22020-07-16 09:18:50 -0400191 GrConfigConversionEffect::TestForPreservingPMConversions(direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400192 fContext->fDidTestPMConversions = true;
193 }
194
195 // The PM<->UPM tests fail or succeed together so we only need to check one.
196 return fContext->fPMUPMConversionsRoundTrip;
197}
198
199std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
200 std::unique_ptr<GrFragmentProcessor> fp) {
201 ASSERT_SINGLE_OWNER
202 // We should have already called this->priv().validPMUPMConversionExists() in this case
203 SkASSERT(fContext->fDidTestPMConversions);
204 // ...and it should have succeeded
205 SkASSERT(this->validPMUPMConversionExists());
206
207 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
208}
209
210std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
211 std::unique_ptr<GrFragmentProcessor> fp) {
212 ASSERT_SINGLE_OWNER
213 // We should have already called this->priv().validPMUPMConversionExists() in this case
214 SkASSERT(fContext->fDidTestPMConversions);
215 // ...and it should have succeeded
216 SkASSERT(this->validPMUPMConversionExists());
217
218 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
219}