• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "src/gpu/GrContextPriv.h"
9 
10 #include "include/gpu/GrContextThreadSafeProxy.h"
11 #include "include/gpu/GrTexture.h"
12 #include "src/gpu/GrAuditTrail.h"
13 #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"
18 #include "src/gpu/GrSurfaceContext.h"
19 #include "src/gpu/GrSurfaceContextPriv.h"
20 #include "src/gpu/GrSurfacePriv.h"
21 #include "src/gpu/SkGr.h"
22 #include "src/gpu/effects/GrSkSLFP.h"
23 #include "src/gpu/effects/generated/GrConfigConversionEffect.h"
24 #include "src/gpu/text/GrTextBlobCache.h"
25 #include "src/image/SkImage_Base.h"
26 #include "src/image/SkImage_Gpu.h"
27 
28 #define ASSERT_OWNED_PROXY(P) \
29     SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
30 #define ASSERT_SINGLE_OWNER \
31     SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->singleOwner());)
32 #define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
33 #define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void)
34 
refCaps() const35 sk_sp<const GrCaps> GrContextPriv::refCaps() const {
36     return fContext->refCaps();
37 }
38 
addOnFlushCallbackObject(GrOnFlushCallbackObject * onFlushCBObject)39 void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
40     fContext->addOnFlushCallbackObject(onFlushCBObject);
41 }
42 
flushSurfaces(GrSurfaceProxy * proxies[],int numProxies,const GrFlushInfo & info)43 GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
44                                                    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(
55             proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info);
56 }
57 
flushSurface(GrSurfaceProxy * proxy)58 void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
59     this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
60 }
61 
moveRenderTasksToDDL(SkDeferredDisplayList * ddl)62 void GrContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
63     fContext->drawingManager()->moveRenderTasksToDDL(ddl);
64 }
65 
copyRenderTasksFromDDL(const SkDeferredDisplayList * ddl,GrRenderTargetProxy * newDest)66 void GrContextPriv::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
67                                            GrRenderTargetProxy* newDest) {
68     fContext->drawingManager()->copyRenderTasksFromDDL(ddl, newDest);
69 }
70 
compile(const GrProgramDesc & desc,const GrProgramInfo & info)71 void GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
72     GrGpu* gpu = this->getGpu();
73     if (!gpu) {
74         return;
75     }
76 
77     gpu->compile(desc, info);
78 }
79 
80 
81 //////////////////////////////////////////////////////////////////////////////
82 
83 #if GR_TEST_UTILS
resetGpuStats() const84 void GrContextPriv::resetGpuStats() const {
85 #if GR_GPU_STATS
86     fContext->fGpu->stats()->reset();
87 #endif
88 }
89 
dumpCacheStats(SkString * out) const90 void GrContextPriv::dumpCacheStats(SkString* out) const {
91 #if GR_CACHE_STATS
92     fContext->fResourceCache->dumpStats(out);
93 #endif
94 }
95 
dumpCacheStatsKeyValuePairs(SkTArray<SkString> * keys,SkTArray<double> * values) const96 void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
97                                                 SkTArray<double>* values) const {
98 #if GR_CACHE_STATS
99     fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
100 #endif
101 }
102 
printCacheStats() const103 void GrContextPriv::printCacheStats() const {
104     SkString out;
105     this->dumpCacheStats(&out);
106     SkDebugf("%s", out.c_str());
107 }
108 
dumpGpuStats(SkString * out) const109 void GrContextPriv::dumpGpuStats(SkString* out) const {
110 #if GR_GPU_STATS
111     return fContext->fGpu->stats()->dump(out);
112 #endif
113 }
114 
dumpGpuStatsKeyValuePairs(SkTArray<SkString> * keys,SkTArray<double> * values) const115 void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
116                                               SkTArray<double>* values) const {
117 #if GR_GPU_STATS
118     return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
119 #endif
120 }
121 
printGpuStats() const122 void GrContextPriv::printGpuStats() const {
123     SkString out;
124     this->dumpGpuStats(&out);
125     SkDebugf("%s", out.c_str());
126 }
127 
testingOnly_setTextBlobCacheLimit(size_t bytes)128 void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
129     fContext->priv().getTextBlobCache()->setBudget(bytes);
130 }
131 
testingOnly_getFontAtlasImage(GrMaskFormat format,unsigned int index)132 sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
133     auto atlasManager = this->getAtlasManager();
134     if (!atlasManager) {
135         return nullptr;
136     }
137 
138     unsigned int numActiveProxies;
139     const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
140     if (index >= numActiveProxies || !views || !views[index].proxy()) {
141         return nullptr;
142     }
143 
144     SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
145     SkASSERT(views[index].proxy()->priv().isExact());
146     sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
147                                          views[index], colorType, kPremul_SkAlphaType, nullptr));
148     return image;
149 }
150 
testingOnly_purgeAllUnlockedResources()151 void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
152     fContext->fResourceCache->purgeAllUnlocked();
153 }
154 
testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject * cb)155 void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
156     fContext->flush();
157     fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
158 }
159 #endif
160 
validPMUPMConversionExists()161 bool GrContextPriv::validPMUPMConversionExists() {
162     ASSERT_SINGLE_OWNER
163     if (!fContext->fDidTestPMConversions) {
164         fContext->fPMUPMConversionsRoundTrip =
165                 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
166         fContext->fDidTestPMConversions = true;
167     }
168 
169     // The PM<->UPM tests fail or succeed together so we only need to check one.
170     return fContext->fPMUPMConversionsRoundTrip;
171 }
172 
createPMToUPMEffect(std::unique_ptr<GrFragmentProcessor> fp)173 std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
174         std::unique_ptr<GrFragmentProcessor> fp) {
175     ASSERT_SINGLE_OWNER
176     // We should have already called this->priv().validPMUPMConversionExists() in this case
177     SkASSERT(fContext->fDidTestPMConversions);
178     // ...and it should have succeeded
179     SkASSERT(this->validPMUPMConversionExists());
180 
181     return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
182 }
183 
createUPMToPMEffect(std::unique_ptr<GrFragmentProcessor> fp)184 std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
185         std::unique_ptr<GrFragmentProcessor> fp) {
186     ASSERT_SINGLE_OWNER
187     // We should have already called this->priv().validPMUPMConversionExists() in this case
188     SkASSERT(fContext->fDidTestPMConversions);
189     // ...and it should have succeeded
190     SkASSERT(this->validPMUPMConversionExists());
191 
192     return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
193 }
194