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