| 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/GrDirectContext.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/GrTexture.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/GrAtlasManager.h" |
| 25 | #include "src/gpu/text/GrTextBlobCache.h" |
| 26 | #include "src/image/SkImage_Base.h" |
| 27 | #include "src/image/SkImage_Gpu.h" |
| 28 | |
| 29 | #define ASSERT_OWNED_PROXY(P) \ |
| 30 | SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext) |
| 31 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(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 | |
| 35 | sk_sp<const GrCaps> GrContextPriv::refCaps() const { |
| 36 | return fContext->refCaps(); |
| 37 | } |
| 38 | |
| 39 | void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 40 | fContext->addOnFlushCallbackObject(onFlushCBObject); |
| 41 | } |
| 42 | |
| 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, nullptr); |
| 56 | } |
| 57 | |
| 58 | void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) { |
| 59 | this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {}); |
| 60 | } |
| 61 | |
| 62 | void GrContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl, |
| 63 | GrRenderTargetProxy* newDest) { |
| 64 | fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest); |
| 65 | } |
| 66 | |
| 67 | bool GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) { |
| 68 | GrGpu* gpu = this->getGpu(); |
| 69 | if (!gpu) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return gpu->compile(desc, info); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | ////////////////////////////////////////////////////////////////////////////// |
| 78 | #if GR_TEST_UTILS |
| 79 | |
| 80 | void GrContextPriv::dumpCacheStats(SkString* out) const { |
| 81 | #if GR_CACHE_STATS |
| 82 | fContext->fResourceCache->dumpStats(out); |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | void 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 | |
| 93 | void GrContextPriv::printCacheStats() const { |
| 94 | SkString out; |
| 95 | this->dumpCacheStats(&out); |
| 96 | SkDebugf("%s" , out.c_str()); |
| 97 | } |
| 98 | |
| 99 | ///////////////////////////////////////////////// |
| 100 | void GrContextPriv::resetGpuStats() const { |
| 101 | #if GR_GPU_STATS |
| 102 | fContext->fGpu->stats()->reset(); |
| 103 | #endif |
| 104 | } |
| 105 | |
| 106 | void GrContextPriv::dumpGpuStats(SkString* out) const { |
| 107 | #if GR_GPU_STATS |
| 108 | return fContext->fGpu->stats()->dump(out); |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | void 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 | |
| 119 | void GrContextPriv::printGpuStats() const { |
| 120 | SkString out; |
| 121 | this->dumpGpuStats(&out); |
| 122 | SkDebugf("%s" , out.c_str()); |
| 123 | } |
| 124 | |
| 125 | ///////////////////////////////////////////////// |
| 126 | void GrContextPriv::resetContextStats() const { |
| 127 | #if GR_GPU_STATS |
| 128 | fContext->stats()->reset(); |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | void GrContextPriv::dumpContextStats(SkString* out) const { |
| 133 | #if GR_GPU_STATS |
| 134 | return fContext->stats()->dump(out); |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | void 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 | |
| 145 | void GrContextPriv::printContextStats() const { |
| 146 | SkString out; |
| 147 | this->dumpContextStats(&out); |
| 148 | SkDebugf("%s" , out.c_str()); |
| 149 | } |
| 150 | |
| 151 | ///////////////////////////////////////////////// |
| 152 | sk_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; |
| 159 | const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies); |
| 160 | if (index >= numActiveProxies || !views || !views[index].proxy()) { |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
| 164 | SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format)); |
| 165 | SkASSERT(views[index].proxy()->priv().isExact()); |
| 166 | sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID, |
| 167 | views[index], colorType, kPremul_SkAlphaType, nullptr)); |
| 168 | return image; |
| 169 | } |
| 170 | |
| 171 | void GrContextPriv::testingOnly_purgeAllUnlockedResources() { |
| 172 | fContext->fResourceCache->purgeAllUnlocked(); |
| 173 | } |
| 174 | |
| 175 | void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) { |
| 176 | fContext->flushAndSubmit(); |
| 177 | fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb); |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | bool GrContextPriv::validPMUPMConversionExists() { |
| 182 | ASSERT_SINGLE_OWNER |
| 183 | |
| 184 | // CONTEXT TODO: remove this downcast when this class becomes GrDirectContextPriv |
| 185 | auto direct = GrAsDirectContext(fContext); |
| 186 | SkASSERT(direct); |
| 187 | |
| 188 | if (!fContext->fDidTestPMConversions) { |
| 189 | fContext->fPMUPMConversionsRoundTrip = |
| 190 | GrConfigConversionEffect::TestForPreservingPMConversions(direct); |
| 191 | fContext->fDidTestPMConversions = true; |
| 192 | } |
| 193 | |
| 194 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
| 195 | return fContext->fPMUPMConversionsRoundTrip; |
| 196 | } |
| 197 | |
| 198 | std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect( |
| 199 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 200 | ASSERT_SINGLE_OWNER |
| 201 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
| 202 | SkASSERT(fContext->fDidTestPMConversions); |
| 203 | // ...and it should have succeeded |
| 204 | SkASSERT(this->validPMUPMConversionExists()); |
| 205 | |
| 206 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul); |
| 207 | } |
| 208 | |
| 209 | std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect( |
| 210 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 211 | ASSERT_SINGLE_OWNER |
| 212 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
| 213 | SkASSERT(fContext->fDidTestPMConversions); |
| 214 | // ...and it should have succeeded |
| 215 | SkASSERT(this->validPMUPMConversionExists()); |
| 216 | |
| 217 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
| 218 | } |
| 219 | |