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 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrDirectContextPriv.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" |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrSurfaceContext.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrSurfaceDrawContext.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 19 | #include "src/gpu/GrTexture.h" |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 20 | #include "src/gpu/GrThreadSafePipelineBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/SkGr.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 22 | #include "src/gpu/effects/GrSkSLFP.h" |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 23 | #include "src/gpu/effects/generated/GrConfigConversionEffect.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 24 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/text/GrTextBlobCache.h" |
| 26 | #include "src/image/SkImage_Base.h" |
| 27 | #include "src/image/SkImage_Gpu.h" |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 28 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 29 | #define ASSERT_OWNED_PROXY(P) \ |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 30 | SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext) |
Adlai Holler | 33dbd65 | 2020-06-01 12:35:42 -0400 | [diff] [blame] | 31 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->singleOwner()) |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 32 | #define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 33 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 34 | sk_sp<const GrCaps> GrDirectContextPriv::refCaps() const { |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 35 | return fContext->refCaps(); |
| 36 | } |
| 37 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 38 | void GrDirectContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
Robert Phillips | c5058a6 | 2019-02-15 12:52:59 -0500 | [diff] [blame] | 39 | fContext->addOnFlushCallbackObject(onFlushCBObject); |
| 40 | } |
| 41 | |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 42 | GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces( |
| 43 | SkSpan<GrSurfaceProxy*> proxies, |
| 44 | SkSurface::BackendSurfaceAccess access, |
| 45 | const GrFlushInfo& info, |
| 46 | const GrBackendSurfaceMutableState* newState) { |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 47 | ASSERT_SINGLE_OWNER |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 48 | GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", fContext); |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 49 | |
| 50 | if (fContext->abandoned()) { |
| 51 | if (info.fSubmittedProc) { |
| 52 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 53 | } |
| 54 | if (info.fFinishedProc) { |
| 55 | info.fFinishedProc(info.fFinishedContext); |
| 56 | } |
| 57 | return GrSemaphoresSubmitted::kNo; |
| 58 | } |
| 59 | |
Adlai Holler | c2bfcff | 2020-11-06 15:39:36 -0500 | [diff] [blame] | 60 | #ifdef SK_DEBUG |
| 61 | for (GrSurfaceProxy* proxy : proxies) { |
| 62 | SkASSERT(proxy); |
| 63 | ASSERT_OWNED_PROXY(proxy); |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 64 | } |
Adlai Holler | c2bfcff | 2020-11-06 15:39:36 -0500 | [diff] [blame] | 65 | #endif |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 66 | return fContext->drawingManager()->flushSurfaces(proxies, access, info, newState); |
Brian Salomon | 693bc2b | 2019-05-09 13:48:00 +0000 | [diff] [blame] | 67 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 68 | |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 69 | void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl, |
Robert Phillips | eb54bb5 | 2021-01-08 17:20:18 -0500 | [diff] [blame] | 70 | sk_sp<GrRenderTargetProxy> newDest, |
Robert Phillips | 88b2961 | 2020-11-16 15:15:08 -0500 | [diff] [blame] | 71 | SkIPoint offset) { |
Robert Phillips | eb54bb5 | 2021-01-08 17:20:18 -0500 | [diff] [blame] | 72 | fContext->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset); |
Adlai Holler | 7580ad4 | 2020-06-24 13:45:25 -0400 | [diff] [blame] | 73 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 74 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 75 | bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 76 | GrGpu* gpu = this->getGpu(); |
| 77 | if (!gpu) { |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 78 | return false; |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 81 | return gpu->compile(desc, info); |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 85 | ////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 86 | #if GR_TEST_UTILS |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 87 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 88 | void GrDirectContextPriv::dumpCacheStats(SkString* out) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 89 | #if GR_CACHE_STATS |
| 90 | fContext->fResourceCache->dumpStats(out); |
| 91 | #endif |
| 92 | } |
| 93 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 94 | void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 95 | SkTArray<double>* values) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 96 | #if GR_CACHE_STATS |
| 97 | fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values); |
| 98 | #endif |
| 99 | } |
| 100 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 101 | void GrDirectContextPriv::printCacheStats() const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 102 | SkString out; |
| 103 | this->dumpCacheStats(&out); |
| 104 | SkDebugf("%s", out.c_str()); |
| 105 | } |
| 106 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 107 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 108 | void GrDirectContextPriv::resetGpuStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 109 | #if GR_GPU_STATS |
| 110 | fContext->fGpu->stats()->reset(); |
| 111 | #endif |
| 112 | } |
| 113 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 114 | void GrDirectContextPriv::dumpGpuStats(SkString* out) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 115 | #if GR_GPU_STATS |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 116 | fContext->fGpu->stats()->dump(out); |
| 117 | if (auto builder = fContext->fGpu->pipelineBuilder()) { |
| 118 | builder->stats()->dump(out); |
| 119 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 120 | #endif |
| 121 | } |
| 122 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 123 | void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 124 | SkTArray<double>* values) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 125 | #if GR_GPU_STATS |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 126 | fContext->fGpu->stats()->dumpKeyValuePairs(keys, values); |
| 127 | if (auto builder = fContext->fGpu->pipelineBuilder()) { |
| 128 | builder->stats()->dumpKeyValuePairs(keys, values); |
| 129 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 130 | #endif |
| 131 | } |
| 132 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 133 | void GrDirectContextPriv::printGpuStats() const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 134 | SkString out; |
| 135 | this->dumpGpuStats(&out); |
| 136 | SkDebugf("%s", out.c_str()); |
| 137 | } |
| 138 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 139 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 140 | void GrDirectContextPriv::resetContextStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 141 | #if GR_GPU_STATS |
| 142 | fContext->stats()->reset(); |
| 143 | #endif |
| 144 | } |
| 145 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 146 | void GrDirectContextPriv::dumpContextStats(SkString* out) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 147 | #if GR_GPU_STATS |
| 148 | return fContext->stats()->dump(out); |
| 149 | #endif |
| 150 | } |
| 151 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 152 | void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 153 | SkTArray<double>* values) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 154 | #if GR_GPU_STATS |
| 155 | return fContext->stats()->dumpKeyValuePairs(keys, values); |
| 156 | #endif |
| 157 | } |
| 158 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 159 | void GrDirectContextPriv::printContextStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 160 | SkString out; |
| 161 | this->dumpContextStats(&out); |
| 162 | SkDebugf("%s", out.c_str()); |
| 163 | } |
| 164 | |
| 165 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 166 | sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, |
| 167 | unsigned int index) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 168 | auto atlasManager = this->getAtlasManager(); |
| 169 | if (!atlasManager) { |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
| 173 | unsigned int numActiveProxies; |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 174 | const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies); |
| 175 | if (index >= numActiveProxies || !views || !views[index].proxy()) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 176 | return nullptr; |
| 177 | } |
| 178 | |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 179 | SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format)); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 180 | SkASSERT(views[index].proxy()->priv().isExact()); |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 181 | sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID, |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 182 | views[index], colorType, kPremul_SkAlphaType, nullptr)); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 183 | return image; |
| 184 | } |
| 185 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 186 | void GrDirectContextPriv::testingOnly_purgeAllUnlockedResources() { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 187 | fContext->fResourceCache->purgeAllUnlocked(); |
| 188 | } |
| 189 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 190 | void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject( |
| 191 | GrOnFlushCallbackObject* cb) { |
| 192 | fContext->flushAndSubmit(); |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 193 | fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 194 | } |
| 195 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 196 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 197 | bool GrDirectContextPriv::validPMUPMConversionExists() { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 198 | ASSERT_SINGLE_OWNER |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 199 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 200 | if (!fContext->fDidTestPMConversions) { |
| 201 | fContext->fPMUPMConversionsRoundTrip = |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 202 | GrConfigConversionEffect::TestForPreservingPMConversions(fContext); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 203 | fContext->fDidTestPMConversions = true; |
| 204 | } |
| 205 | |
| 206 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
| 207 | return fContext->fPMUPMConversionsRoundTrip; |
| 208 | } |
| 209 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 210 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 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::kToUnpremul); |
| 219 | } |
| 220 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 221 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 222 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 223 | ASSERT_SINGLE_OWNER |
| 224 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
| 225 | SkASSERT(fContext->fDidTestPMConversions); |
| 226 | // ...and it should have succeeded |
| 227 | SkASSERT(this->validPMUPMConversionExists()); |
| 228 | |
| 229 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
| 230 | } |