blob: 4b895d3e01ee9d6b7a297c492bc364b57cace595 [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
Chris Dalton6b498102019-08-01 14:14:52 -060062void GrContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
63 fContext->drawingManager()->moveRenderTasksToDDL(ddl);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050064}
65
Adlai Holler7580ad42020-06-24 13:45:25 -040066void GrContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
67 GrRenderTargetProxy* newDest) {
68 fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest);
69}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050070
Robert Phillips43e7e4f2020-05-06 13:34:45 -040071bool GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050072 GrGpu* gpu = this->getGpu();
73 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040074 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050075 }
76
Robert Phillips43e7e4f2020-05-06 13:34:45 -040077 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050078}
79
80
Robert Phillipsdbaf3172019-02-06 15:12:53 -050081//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050082#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050083
84void GrContextPriv::dumpCacheStats(SkString* out) const {
85#if GR_CACHE_STATS
86 fContext->fResourceCache->dumpStats(out);
87#endif
88}
89
90void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
91 SkTArray<double>* values) const {
92#if GR_CACHE_STATS
93 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
94#endif
95}
96
97void GrContextPriv::printCacheStats() const {
98 SkString out;
99 this->dumpCacheStats(&out);
100 SkDebugf("%s", out.c_str());
101}
102
Robert Phillips273f1072020-05-05 13:03:07 -0400103/////////////////////////////////////////////////
104void GrContextPriv::resetGpuStats() const {
105#if GR_GPU_STATS
106 fContext->fGpu->stats()->reset();
107#endif
108}
109
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500110void GrContextPriv::dumpGpuStats(SkString* out) const {
111#if GR_GPU_STATS
112 return fContext->fGpu->stats()->dump(out);
113#endif
114}
115
116void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
117 SkTArray<double>* values) const {
118#if GR_GPU_STATS
119 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
120#endif
121}
122
123void GrContextPriv::printGpuStats() const {
124 SkString out;
125 this->dumpGpuStats(&out);
126 SkDebugf("%s", out.c_str());
127}
128
Robert Phillips273f1072020-05-05 13:03:07 -0400129/////////////////////////////////////////////////
130void GrContextPriv::resetContextStats() const {
131#if GR_GPU_STATS
132 fContext->stats()->reset();
133#endif
134}
135
136void GrContextPriv::dumpContextStats(SkString* out) const {
137#if GR_GPU_STATS
138 return fContext->stats()->dump(out);
139#endif
140}
141
142void GrContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
143 SkTArray<double>* values) const {
144#if GR_GPU_STATS
145 return fContext->stats()->dumpKeyValuePairs(keys, values);
146#endif
147}
148
149void GrContextPriv::printContextStats() const {
150 SkString out;
151 this->dumpContextStats(&out);
152 SkDebugf("%s", out.c_str());
153}
154
155/////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500156sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
157 auto atlasManager = this->getAtlasManager();
158 if (!atlasManager) {
159 return nullptr;
160 }
161
162 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500163 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
164 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500165 return nullptr;
166 }
167
Greg Daniel7c165a42020-01-22 12:22:36 -0500168 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500169 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000170 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500171 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500172 return image;
173}
174
175void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
176 fContext->fResourceCache->purgeAllUnlocked();
177}
178
179void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
Greg Daniel0a2464f2020-05-14 15:45:44 -0400180 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500181 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500182}
183#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184
185bool GrContextPriv::validPMUPMConversionExists() {
186 ASSERT_SINGLE_OWNER
187 if (!fContext->fDidTestPMConversions) {
188 fContext->fPMUPMConversionsRoundTrip =
189 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
190 fContext->fDidTestPMConversions = true;
191 }
192
193 // The PM<->UPM tests fail or succeed together so we only need to check one.
194 return fContext->fPMUPMConversionsRoundTrip;
195}
196
197std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
198 std::unique_ptr<GrFragmentProcessor> fp) {
199 ASSERT_SINGLE_OWNER
200 // We should have already called this->priv().validPMUPMConversionExists() in this case
201 SkASSERT(fContext->fDidTestPMConversions);
202 // ...and it should have succeeded
203 SkASSERT(this->validPMUPMConversionExists());
204
205 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
206}
207
208std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
209 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
216 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
217}