Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrContextPriv.h" |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrContextThreadSafeProxy.h" |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame^] | 11 | #include "include/gpu/GrDirectContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrSurfaceContextPriv.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrSurfacePriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 21 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/SkGr.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 23 | #include "src/gpu/effects/GrSkSLFP.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 24 | #include "src/gpu/effects/generated/GrConfigConversionEffect.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 25 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/text/GrTextBlobCache.h" |
| 27 | #include "src/image/SkImage_Base.h" |
| 28 | #include "src/image/SkImage_Gpu.h" |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 29 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 30 | #define ASSERT_OWNED_PROXY(P) \ |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 31 | SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext) |
Adlai Holler | 33dbd65 | 2020-06-01 12:35:42 -0400 | [diff] [blame] | 32 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->singleOwner()) |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 33 | #define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); } |
| 34 | #define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void) |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 35 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 36 | sk_sp<const GrCaps> GrContextPriv::refCaps() const { |
| 37 | return fContext->refCaps(); |
| 38 | } |
| 39 | |
Robert Phillips | c5058a6 | 2019-02-15 12:52:59 -0500 | [diff] [blame] | 40 | void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 41 | fContext->addOnFlushCallbackObject(onFlushCBObject); |
| 42 | } |
| 43 | |
Greg Daniel | 55f040b | 2020-02-13 15:38:32 +0000 | [diff] [blame] | 44 | GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies, |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 45 | 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 Daniel | 9efe386 | 2020-06-11 11:51:06 -0400 | [diff] [blame] | 56 | proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Greg Daniel | 55f040b | 2020-02-13 15:38:32 +0000 | [diff] [blame] | 59 | void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) { |
| 60 | this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {}); |
Brian Salomon | 693bc2b | 2019-05-09 13:48:00 +0000 | [diff] [blame] | 61 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 62 | |
Adlai Holler | 7580ad4 | 2020-06-24 13:45:25 -0400 | [diff] [blame] | 63 | void GrContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl, |
| 64 | GrRenderTargetProxy* newDest) { |
| 65 | fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest); |
| 66 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 67 | |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 68 | bool GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 69 | GrGpu* gpu = this->getGpu(); |
| 70 | if (!gpu) { |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 71 | return false; |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 74 | return gpu->compile(desc, info); |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 78 | ////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 79 | #if GR_TEST_UTILS |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 80 | |
| 81 | void GrContextPriv::dumpCacheStats(SkString* out) const { |
| 82 | #if GR_CACHE_STATS |
| 83 | fContext->fResourceCache->dumpStats(out); |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | void 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 | |
| 94 | void GrContextPriv::printCacheStats() const { |
| 95 | SkString out; |
| 96 | this->dumpCacheStats(&out); |
| 97 | SkDebugf("%s", out.c_str()); |
| 98 | } |
| 99 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 100 | ///////////////////////////////////////////////// |
| 101 | void GrContextPriv::resetGpuStats() const { |
| 102 | #if GR_GPU_STATS |
| 103 | fContext->fGpu->stats()->reset(); |
| 104 | #endif |
| 105 | } |
| 106 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 107 | void GrContextPriv::dumpGpuStats(SkString* out) const { |
| 108 | #if GR_GPU_STATS |
| 109 | return fContext->fGpu->stats()->dump(out); |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | void 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 | |
| 120 | void GrContextPriv::printGpuStats() const { |
| 121 | SkString out; |
| 122 | this->dumpGpuStats(&out); |
| 123 | SkDebugf("%s", out.c_str()); |
| 124 | } |
| 125 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 126 | ///////////////////////////////////////////////// |
| 127 | void GrContextPriv::resetContextStats() const { |
| 128 | #if GR_GPU_STATS |
| 129 | fContext->stats()->reset(); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | void GrContextPriv::dumpContextStats(SkString* out) const { |
| 134 | #if GR_GPU_STATS |
| 135 | return fContext->stats()->dump(out); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | void 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 | |
| 146 | void GrContextPriv::printContextStats() const { |
| 147 | SkString out; |
| 148 | this->dumpContextStats(&out); |
| 149 | SkDebugf("%s", out.c_str()); |
| 150 | } |
| 151 | |
| 152 | ///////////////////////////////////////////////// |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 153 | sk_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 Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 160 | const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies); |
| 161 | if (index >= numActiveProxies || !views || !views[index].proxy()) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 162 | return nullptr; |
| 163 | } |
| 164 | |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 165 | SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format)); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 166 | SkASSERT(views[index].proxy()->priv().isExact()); |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 167 | sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID, |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 168 | views[index], colorType, kPremul_SkAlphaType, nullptr)); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 169 | return image; |
| 170 | } |
| 171 | |
| 172 | void GrContextPriv::testingOnly_purgeAllUnlockedResources() { |
| 173 | fContext->fResourceCache->purgeAllUnlocked(); |
| 174 | } |
| 175 | |
| 176 | void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) { |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 177 | fContext->flushAndSubmit(); |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 178 | fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 179 | } |
| 180 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 181 | |
| 182 | bool GrContextPriv::validPMUPMConversionExists() { |
| 183 | ASSERT_SINGLE_OWNER |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 184 | |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame^] | 185 | // CONTEXT TODO: remove this downcast when this class becomes GrDirectContextPriv |
| 186 | auto direct = GrAsDirectContext(fContext); |
| 187 | SkASSERT(direct); |
| 188 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 189 | if (!fContext->fDidTestPMConversions) { |
| 190 | fContext->fPMUPMConversionsRoundTrip = |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame^] | 191 | GrConfigConversionEffect::TestForPreservingPMConversions(direct); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 192 | 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 | |
| 199 | std::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 | |
| 210 | std::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 | } |