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