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" |
| 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" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 20 | #include "src/gpu/GrTexture.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 | 88b2961 | 2020-11-16 15:15:08 -0500 | [diff] [blame^] | 70 | GrRenderTargetProxy* newDest, |
| 71 | SkIPoint offset) { |
| 72 | fContext->drawingManager()->createDDLTask(std::move(ddl), 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 |
| 116 | return fContext->fGpu->stats()->dump(out); |
| 117 | #endif |
| 118 | } |
| 119 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 120 | void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 121 | SkTArray<double>* values) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 122 | #if GR_GPU_STATS |
| 123 | return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values); |
| 124 | #endif |
| 125 | } |
| 126 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 127 | void GrDirectContextPriv::printGpuStats() const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 128 | SkString out; |
| 129 | this->dumpGpuStats(&out); |
| 130 | SkDebugf("%s", out.c_str()); |
| 131 | } |
| 132 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 133 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 134 | void GrDirectContextPriv::resetContextStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 135 | #if GR_GPU_STATS |
| 136 | fContext->stats()->reset(); |
| 137 | #endif |
| 138 | } |
| 139 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 140 | void GrDirectContextPriv::dumpContextStats(SkString* out) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 141 | #if GR_GPU_STATS |
| 142 | return fContext->stats()->dump(out); |
| 143 | #endif |
| 144 | } |
| 145 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 146 | void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 147 | SkTArray<double>* values) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 148 | #if GR_GPU_STATS |
| 149 | return fContext->stats()->dumpKeyValuePairs(keys, values); |
| 150 | #endif |
| 151 | } |
| 152 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 153 | void GrDirectContextPriv::printContextStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 154 | SkString out; |
| 155 | this->dumpContextStats(&out); |
| 156 | SkDebugf("%s", out.c_str()); |
| 157 | } |
| 158 | |
| 159 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 160 | sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, |
| 161 | unsigned int index) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 162 | auto atlasManager = this->getAtlasManager(); |
| 163 | if (!atlasManager) { |
| 164 | return nullptr; |
| 165 | } |
| 166 | |
| 167 | unsigned int numActiveProxies; |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 168 | const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies); |
| 169 | if (index >= numActiveProxies || !views || !views[index].proxy()) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 170 | return nullptr; |
| 171 | } |
| 172 | |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 173 | SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format)); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 174 | SkASSERT(views[index].proxy()->priv().isExact()); |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 175 | sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID, |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 176 | views[index], colorType, kPremul_SkAlphaType, nullptr)); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 177 | return image; |
| 178 | } |
| 179 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 180 | void GrDirectContextPriv::testingOnly_purgeAllUnlockedResources() { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 181 | fContext->fResourceCache->purgeAllUnlocked(); |
| 182 | } |
| 183 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 184 | void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject( |
| 185 | GrOnFlushCallbackObject* cb) { |
| 186 | fContext->flushAndSubmit(); |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 187 | fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 188 | } |
| 189 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 190 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 191 | bool GrDirectContextPriv::validPMUPMConversionExists() { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 192 | ASSERT_SINGLE_OWNER |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 193 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 194 | if (!fContext->fDidTestPMConversions) { |
| 195 | fContext->fPMUPMConversionsRoundTrip = |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 196 | GrConfigConversionEffect::TestForPreservingPMConversions(fContext); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 197 | fContext->fDidTestPMConversions = true; |
| 198 | } |
| 199 | |
| 200 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
| 201 | return fContext->fPMUPMConversionsRoundTrip; |
| 202 | } |
| 203 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 204 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 205 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 206 | ASSERT_SINGLE_OWNER |
| 207 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
| 208 | SkASSERT(fContext->fDidTestPMConversions); |
| 209 | // ...and it should have succeeded |
| 210 | SkASSERT(this->validPMUPMConversionExists()); |
| 211 | |
| 212 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul); |
| 213 | } |
| 214 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 215 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 216 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 217 | ASSERT_SINGLE_OWNER |
| 218 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
| 219 | SkASSERT(fContext->fDidTestPMConversions); |
| 220 | // ...and it should have succeeded |
| 221 | SkASSERT(this->validPMUPMConversionExists()); |
| 222 | |
| 223 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
| 224 | } |