blob: 79451927cc6c0c56967a5374ff8d2e98e17924a6 [file] [log] [blame]
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrContextPriv.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContextThreadSafeProxy.h"
11#include "include/gpu/GrTexture.h"
12#include "include/private/GrSkSLFPFactoryCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#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/GrSurfacePriv.h"
19#include "src/gpu/GrTextureContext.h"
20#include "src/gpu/SkGr.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/effects/generated/GrConfigConversionEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/text/GrTextBlobCache.h"
23#include "src/image/SkImage_Base.h"
24#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050025
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040026#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsdbaf3172019-02-06 15:12:53 -050027 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040028#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050029 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->singleOwner());)
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040030#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
31#define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void)
Robert Phillipsdbaf3172019-02-06 15:12:53 -050032
Robert Phillipsa41c6852019-02-07 10:44:10 -050033sk_sp<const GrCaps> GrContextPriv::refCaps() const {
34 return fContext->refCaps();
35}
36
Robert Phillipsdbaf3172019-02-06 15:12:53 -050037sk_sp<GrSkSLFPFactoryCache> GrContextPriv::fpFactoryCache() {
38 return fContext->fpFactoryCache();
39}
40
41sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
Robert Phillipsd6841482019-02-08 10:29:20 -050042 return fContext->refOpMemoryPool();
Robert Phillipsdbaf3172019-02-06 15:12:53 -050043}
44
Robert Phillipsc5058a62019-02-15 12:52:59 -050045void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
46 fContext->addOnFlushCallbackObject(onFlushCBObject);
47}
48
Robert Phillips292a6b22019-02-14 14:49:02 -050049sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(
50 sk_sp<GrSurfaceProxy> proxy,
51 sk_sp<SkColorSpace> colorSpace,
52 const SkSurfaceProps* props) {
53 return fContext->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
54}
55
56sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(
57 const GrBackendFormat& format,
58 const GrSurfaceDesc& dstDesc,
59 GrSurfaceOrigin origin,
60 GrMipMapped mipMapped,
61 SkBackingFit fit,
62 SkBudgeted isDstBudgeted,
63 sk_sp<SkColorSpace> colorSpace,
64 const SkSurfaceProps* props) {
65 return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
66 isDstBudgeted, std::move(colorSpace), props);
67}
68
Robert Phillipsb97da532019-02-12 15:24:12 -050069sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Robert Phillips6f0e02f2019-02-13 11:02:28 -050070 const GrBackendFormat& format,
71 SkBackingFit fit,
72 int width, int height,
73 GrPixelConfig config,
74 sk_sp<SkColorSpace> colorSpace,
75 int sampleCnt,
76 GrMipMapped mipMapped,
77 GrSurfaceOrigin origin,
78 const SkSurfaceProps* surfaceProps,
79 SkBudgeted budgeted) {
Robert Phillipsb97da532019-02-12 15:24:12 -050080 return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config,
81 std::move(colorSpace), sampleCnt, mipMapped,
82 origin, surfaceProps, budgeted);
83}
84
Robert Phillips6f0e02f2019-02-13 11:02:28 -050085sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
86 const GrBackendFormat& format,
87 SkBackingFit fit,
88 int width, int height,
89 GrPixelConfig config,
90 sk_sp<SkColorSpace> colorSpace,
91 int sampleCnt,
92 GrMipMapped mipMapped,
93 GrSurfaceOrigin origin,
94 const SkSurfaceProps* surfaceProps,
95 SkBudgeted budgeted) {
96 return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config,
97 std::move(colorSpace), sampleCnt,
98 mipMapped, origin, surfaceProps,
99 budgeted);
100}
101
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500102sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
103 GrSurfaceOrigin origin,
104 sk_sp<SkColorSpace> colorSpace) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400105 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500106
107 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
108 tex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
109 if (!proxy) {
110 return nullptr;
111 }
112
113 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
114}
115
116sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
117 const GrBackendTexture& tex,
118 GrSurfaceOrigin origin,
119 int sampleCnt,
120 sk_sp<SkColorSpace> colorSpace,
121 const SkSurfaceProps* props,
122 ReleaseProc releaseProc,
123 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400124 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500125 SkASSERT(sampleCnt > 0);
126
127 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->wrapRenderableBackendTexture(
128 tex, origin, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
129 releaseCtx));
130 if (!proxy) {
131 return nullptr;
132 }
133
134 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
135 std::move(colorSpace), props);
136}
137
138sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
139 const GrBackendRenderTarget& backendRT,
140 GrSurfaceOrigin origin,
141 sk_sp<SkColorSpace> colorSpace,
142 const SkSurfaceProps* surfaceProps,
143 ReleaseProc releaseProc,
144 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400145 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500146
147 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(
148 backendRT, origin, releaseProc, releaseCtx);
149 if (!proxy) {
150 return nullptr;
151 }
152
153 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
154 std::move(colorSpace),
155 surfaceProps);
156}
157
158sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
159 const GrBackendTexture& tex,
160 GrSurfaceOrigin origin,
161 int sampleCnt,
162 sk_sp<SkColorSpace> colorSpace,
163 const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400164 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500165 SkASSERT(sampleCnt > 0);
166 sk_sp<GrSurfaceProxy> proxy(
167 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
168 if (!proxy) {
169 return nullptr;
170 }
171
172 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
173 std::move(colorSpace),
174 props);
175}
176
177sk_sp<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext(
178 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400179 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500180 sk_sp<GrSurfaceProxy> proxy(
181 this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo));
182 if (!proxy) {
183 return nullptr;
184 }
185
186 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
187 imageInfo.refColorSpace(),
188 props);
189}
190
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400191GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
192 const GrFlushInfo& info) {
193 ASSERT_SINGLE_OWNER
194 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
195 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
196 SkASSERT(numProxies >= 0);
197 SkASSERT(!numProxies || proxies);
198 for (int i = 0; i < numProxies; ++i) {
199 SkASSERT(proxies[i]);
200 ASSERT_OWNED_PROXY(proxies[i]);
201 }
202 return fContext->drawingManager()->flushSurfaces(
203 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500204}
205
Brian Salomon693bc2b2019-05-09 13:48:00 +0000206void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400207 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +0000208}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500209
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500210void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500211 fContext->drawingManager()->moveOpListsToDDL(ddl);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500212}
213
214void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
215 GrRenderTargetProxy* newDest) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500216 fContext->drawingManager()->copyOpListsFromDDL(ddl, newDest);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500217}
218
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500219//////////////////////////////////////////////////////////////////////////////
220#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500221#include "src/utils/SkJSONWriter.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500222SkString GrContextPriv::dump() const {
223 SkDynamicMemoryWStream stream;
224 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
225 writer.beginObject();
226
227 static const char* kBackendStr[] = {
228 "Metal",
229 "OpenGL",
230 "Vulkan",
231 "Mock",
232 };
233 GR_STATIC_ASSERT(0 == (unsigned)GrBackendApi::kMetal);
234 GR_STATIC_ASSERT(1 == (unsigned)GrBackendApi::kOpenGL);
235 GR_STATIC_ASSERT(2 == (unsigned)GrBackendApi::kVulkan);
236 GR_STATIC_ASSERT(3 == (unsigned)GrBackendApi::kMock);
237 writer.appendString("backend", kBackendStr[(unsigned)fContext->backend()]);
238
239 writer.appendName("caps");
240 fContext->caps()->dumpJSON(&writer);
241
242 writer.appendName("gpu");
243 fContext->fGpu->dumpJSON(&writer);
244
245 // Flush JSON to the memory stream
246 writer.endObject();
247 writer.flush();
248
249 // Null terminate the JSON data in the memory stream
250 stream.write8(0);
251
252 // Allocate a string big enough to hold all the data, then copy out of the stream
253 SkString result(stream.bytesWritten());
254 stream.copyToAndReset(result.writable_str());
255 return result;
256}
257#endif
258
259#if GR_TEST_UTILS
260void GrContextPriv::resetGpuStats() const {
261#if GR_GPU_STATS
262 fContext->fGpu->stats()->reset();
263#endif
264}
265
266void GrContextPriv::dumpCacheStats(SkString* out) const {
267#if GR_CACHE_STATS
268 fContext->fResourceCache->dumpStats(out);
269#endif
270}
271
272void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
273 SkTArray<double>* values) const {
274#if GR_CACHE_STATS
275 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
276#endif
277}
278
279void GrContextPriv::printCacheStats() const {
280 SkString out;
281 this->dumpCacheStats(&out);
282 SkDebugf("%s", out.c_str());
283}
284
285void GrContextPriv::dumpGpuStats(SkString* out) const {
286#if GR_GPU_STATS
287 return fContext->fGpu->stats()->dump(out);
288#endif
289}
290
291void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
292 SkTArray<double>* values) const {
293#if GR_GPU_STATS
294 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
295#endif
296}
297
298void GrContextPriv::printGpuStats() const {
299 SkString out;
300 this->dumpGpuStats(&out);
301 SkDebugf("%s", out.c_str());
302}
303
304void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
Robert Phillips2184fb72019-02-21 16:11:41 -0500305 fContext->priv().getTextBlobCache()->setBudget(bytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500306}
307
308sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
309 auto atlasManager = this->getAtlasManager();
310 if (!atlasManager) {
311 return nullptr;
312 }
313
314 unsigned int numActiveProxies;
315 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(format, &numActiveProxies);
316 if (index >= numActiveProxies || !proxies || !proxies[index]) {
317 return nullptr;
318 }
319
320 SkASSERT(proxies[index]->priv().isExact());
321 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
322 kPremul_SkAlphaType, proxies[index], nullptr));
323 return image;
324}
325
326void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
327 fContext->fResourceCache->purgeAllUnlocked();
328}
329
330void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
331 fContext->flush();
Robert Phillips292a6b22019-02-14 14:49:02 -0500332 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500333}
334#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335
336bool GrContextPriv::validPMUPMConversionExists() {
337 ASSERT_SINGLE_OWNER
338 if (!fContext->fDidTestPMConversions) {
339 fContext->fPMUPMConversionsRoundTrip =
340 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
341 fContext->fDidTestPMConversions = true;
342 }
343
344 // The PM<->UPM tests fail or succeed together so we only need to check one.
345 return fContext->fPMUPMConversionsRoundTrip;
346}
347
348std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
349 std::unique_ptr<GrFragmentProcessor> fp) {
350 ASSERT_SINGLE_OWNER
351 // We should have already called this->priv().validPMUPMConversionExists() in this case
352 SkASSERT(fContext->fDidTestPMConversions);
353 // ...and it should have succeeded
354 SkASSERT(this->validPMUPMConversionExists());
355
356 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
357}
358
359std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
360 std::unique_ptr<GrFragmentProcessor> fp) {
361 ASSERT_SINGLE_OWNER
362 // We should have already called this->priv().validPMUPMConversionExists() in this case
363 SkASSERT(fContext->fDidTestPMConversions);
364 // ...and it should have succeeded
365 SkASSERT(this->validPMUPMConversionExists());
366
367 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
368}