blob: e1da901f14588d3d4eff69f28ca4d72798cb316a [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextThreadSafeProxyPriv.h"
13#include "src/gpu/GrDrawingManager.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrMemoryPool.h"
16#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050017#include "src/gpu/GrSurfaceContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040018#include "src/gpu/GrSurfaceContextPriv.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050019#include "src/gpu/GrSurfacePriv.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); }
33#define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void)
Robert Phillipsdbaf3172019-02-06 15:12:53 -050034
Robert Phillipsa41c6852019-02-07 10:44:10 -050035sk_sp<const GrCaps> GrContextPriv::refCaps() const {
36 return fContext->refCaps();
37}
38
Robert Phillipsc5058a62019-02-15 12:52:59 -050039void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
40 fContext->addOnFlushCallbackObject(onFlushCBObject);
41}
42
Greg Daniel55f040b2020-02-13 15:38:32 +000043GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040044 const GrFlushInfo& info) {
45 ASSERT_SINGLE_OWNER
46 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
47 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
48 SkASSERT(numProxies >= 0);
49 SkASSERT(!numProxies || proxies);
50 for (int i = 0; i < numProxies; ++i) {
51 SkASSERT(proxies[i]);
52 ASSERT_OWNED_PROXY(proxies[i]);
53 }
54 return fContext->drawingManager()->flushSurfaces(
Greg Daniel9efe3862020-06-11 11:51:06 -040055 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050056}
57
Greg Daniel55f040b2020-02-13 15:38:32 +000058void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
59 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +000060}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050061
Adlai Holler7580ad42020-06-24 13:45:25 -040062void GrContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
63 GrRenderTargetProxy* newDest) {
64 fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest);
65}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050066
Robert Phillips43e7e4f2020-05-06 13:34:45 -040067bool GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050068 GrGpu* gpu = this->getGpu();
69 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040070 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050071 }
72
Robert Phillips43e7e4f2020-05-06 13:34:45 -040073 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050074}
75
76
Robert Phillipsdbaf3172019-02-06 15:12:53 -050077//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050078#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050079
80void GrContextPriv::dumpCacheStats(SkString* out) const {
81#if GR_CACHE_STATS
82 fContext->fResourceCache->dumpStats(out);
83#endif
84}
85
86void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
87 SkTArray<double>* values) const {
88#if GR_CACHE_STATS
89 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
90#endif
91}
92
93void GrContextPriv::printCacheStats() const {
94 SkString out;
95 this->dumpCacheStats(&out);
96 SkDebugf("%s", out.c_str());
97}
98
Robert Phillips273f1072020-05-05 13:03:07 -040099/////////////////////////////////////////////////
100void GrContextPriv::resetGpuStats() const {
101#if GR_GPU_STATS
102 fContext->fGpu->stats()->reset();
103#endif
104}
105
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500106void GrContextPriv::dumpGpuStats(SkString* out) const {
107#if GR_GPU_STATS
108 return fContext->fGpu->stats()->dump(out);
109#endif
110}
111
112void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
113 SkTArray<double>* values) const {
114#if GR_GPU_STATS
115 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
116#endif
117}
118
119void GrContextPriv::printGpuStats() const {
120 SkString out;
121 this->dumpGpuStats(&out);
122 SkDebugf("%s", out.c_str());
123}
124
Robert Phillips273f1072020-05-05 13:03:07 -0400125/////////////////////////////////////////////////
126void GrContextPriv::resetContextStats() const {
127#if GR_GPU_STATS
128 fContext->stats()->reset();
129#endif
130}
131
132void GrContextPriv::dumpContextStats(SkString* out) const {
133#if GR_GPU_STATS
134 return fContext->stats()->dump(out);
135#endif
136}
137
138void GrContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
139 SkTArray<double>* values) const {
140#if GR_GPU_STATS
141 return fContext->stats()->dumpKeyValuePairs(keys, values);
142#endif
143}
144
145void GrContextPriv::printContextStats() const {
146 SkString out;
147 this->dumpContextStats(&out);
148 SkDebugf("%s", out.c_str());
149}
150
151/////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500152sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
153 auto atlasManager = this->getAtlasManager();
154 if (!atlasManager) {
155 return nullptr;
156 }
157
158 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500159 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
160 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500161 return nullptr;
162 }
163
Greg Daniel7c165a42020-01-22 12:22:36 -0500164 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500165 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000166 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500167 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500168 return image;
169}
170
171void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
172 fContext->fResourceCache->purgeAllUnlocked();
173}
174
175void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
Greg Daniel0a2464f2020-05-14 15:45:44 -0400176 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500177 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500178}
179#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400180
181bool GrContextPriv::validPMUPMConversionExists() {
182 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400183
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184 if (!fContext->fDidTestPMConversions) {
185 fContext->fPMUPMConversionsRoundTrip =
186 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
187 fContext->fDidTestPMConversions = true;
188 }
189
190 // The PM<->UPM tests fail or succeed together so we only need to check one.
191 return fContext->fPMUPMConversionsRoundTrip;
192}
193
194std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
195 std::unique_ptr<GrFragmentProcessor> fp) {
196 ASSERT_SINGLE_OWNER
197 // We should have already called this->priv().validPMUPMConversionExists() in this case
198 SkASSERT(fContext->fDidTestPMConversions);
199 // ...and it should have succeeded
200 SkASSERT(this->validPMUPMConversionExists());
201
202 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
203}
204
205std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
206 std::unique_ptr<GrFragmentProcessor> fp) {
207 ASSERT_SINGLE_OWNER
208 // We should have already called this->priv().validPMUPMConversionExists() in this case
209 SkASSERT(fContext->fDidTestPMConversions);
210 // ...and it should have succeeded
211 SkASSERT(this->validPMUPMConversionExists());
212
213 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
214}