blob: e387198c24bd9825c3aee2e66a3e058a74820ff3 [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.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 Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSkSLFPFactoryCache.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040019#include "src/gpu/GrSurfaceContextPriv.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050020#include "src/gpu/GrSurfacePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrTextureContext.h"
22#include "src/gpu/SkGr.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040023#include "src/gpu/effects/generated/GrConfigConversionEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/text/GrTextBlobCache.h"
25#include "src/image/SkImage_Base.h"
26#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050027
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040028#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsdbaf3172019-02-06 15:12:53 -050029 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040030#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050031 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->singleOwner());)
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040032#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
33#define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void)
Robert Phillipsdbaf3172019-02-06 15:12:53 -050034
Robert Phillipsa41c6852019-02-07 10:44:10 -050035sk_sp<const GrCaps> GrContextPriv::refCaps() const {
36 return fContext->refCaps();
37}
38
Robert Phillipsdbaf3172019-02-06 15:12:53 -050039sk_sp<GrSkSLFPFactoryCache> GrContextPriv::fpFactoryCache() {
40 return fContext->fpFactoryCache();
41}
42
43sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
Robert Phillipsd6841482019-02-08 10:29:20 -050044 return fContext->refOpMemoryPool();
Robert Phillipsdbaf3172019-02-06 15:12:53 -050045}
46
Robert Phillipsc5058a62019-02-15 12:52:59 -050047void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
48 fContext->addOnFlushCallbackObject(onFlushCBObject);
49}
50
Brian Salomone7499c72019-06-24 12:12:36 -040051sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
52 SkAlphaType alphaType,
53 sk_sp<SkColorSpace> colorSpace,
54 const SkSurfaceProps* props) {
55 return fContext->makeWrappedSurfaceContext(std::move(proxy), alphaType, std::move(colorSpace),
56 props);
Robert Phillips292a6b22019-02-14 14:49:02 -050057}
58
Brian Salomone7499c72019-06-24 12:12:36 -040059sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
60 const GrSurfaceDesc& dstDesc,
61 GrSurfaceOrigin origin,
62 GrMipMapped mipMapped,
63 SkBackingFit fit,
64 SkBudgeted isDstBudgeted,
65 SkAlphaType alphaType,
66 sk_sp<SkColorSpace> colorSpace,
67 const SkSurfaceProps* props) {
Robert Phillips292a6b22019-02-14 14:49:02 -050068 return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
Brian Salomone7499c72019-06-24 12:12:36 -040069 isDstBudgeted, alphaType, std::move(colorSpace),
70 props);
Robert Phillips292a6b22019-02-14 14:49:02 -050071}
72
Robert Phillipsb97da532019-02-12 15:24:12 -050073sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040074 const GrBackendFormat& format, SkBackingFit fit, int width, int height,
75 GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
76 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
77 GrProtected isProtected) {
Robert Phillipsb97da532019-02-12 15:24:12 -050078 return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config,
79 std::move(colorSpace), sampleCnt, mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040080 origin, surfaceProps, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -050081}
82
Robert Phillips6f0e02f2019-02-13 11:02:28 -050083sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
84 const GrBackendFormat& format,
85 SkBackingFit fit,
86 int width, int height,
87 GrPixelConfig config,
88 sk_sp<SkColorSpace> colorSpace,
89 int sampleCnt,
90 GrMipMapped mipMapped,
91 GrSurfaceOrigin origin,
92 const SkSurfaceProps* surfaceProps,
93 SkBudgeted budgeted) {
94 return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config,
95 std::move(colorSpace), sampleCnt,
96 mipMapped, origin, surfaceProps,
97 budgeted);
98}
99
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500100sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
101 GrSurfaceOrigin origin,
Brian Salomone7499c72019-06-24 12:12:36 -0400102 SkAlphaType alphaType,
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500103 sk_sp<SkColorSpace> colorSpace) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400104 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500105
106 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
107 tex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
108 if (!proxy) {
109 return nullptr;
110 }
111
Brian Salomone7499c72019-06-24 12:12:36 -0400112 return this->drawingManager()->makeTextureContext(std::move(proxy), alphaType,
113 std::move(colorSpace));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500114}
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}
Robert Phillipscb1adb42019-06-10 15:09:34 -0400369
370//////////////////////////////////////////////////////////////////////////////
371
372#include "src/core/SkMipMap.h"
373
374GrBackendTexture GrContextPriv::createBackendTexture(const SkPixmap srcData[], int numLevels,
375 GrRenderable renderable) {
376 if (!fContext->asDirectContext()) {
377 return {};
378 }
379
380 if (this->abandoned()) {
381 return {};
382 }
383
384 if (!srcData || !numLevels) {
385 return {};
386 }
387
388 int baseWidth = srcData[0].width();
389 int baseHeight = srcData[0].height();
390 SkColorType colorType = srcData[0].colorType();
391
392 if (numLevels > 1) {
393 if (numLevels != SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1) {
394 return {};
395 }
396
397 int currentWidth = baseWidth;
398 int currentHeight = baseHeight;
399 for (int i = 1; i < numLevels; ++i) {
400 currentWidth = SkTMax(1, currentWidth / 2);
401 currentHeight = SkTMax(1, currentHeight / 2);
402
403 if (srcData[i].colorType() != colorType) {
404 return {};
405 }
406
407 if (srcData[i].width() != currentWidth || srcData[i].height() != currentHeight) {
408 return {};
409 }
410 }
411 }
412
413 GrBackendFormat backendFormat = this->caps()->getBackendFormatFromColorType(colorType);
414 if (!backendFormat.isValid()) {
415 return {};
416 }
417
418 GrGpu* gpu = fContext->fGpu.get();
419
420 // TODO: propagate the array of pixmaps interface to GrGpu
421 return gpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
422 GrMipMapped::kNo, // TODO: use real mipmap setting here
423 renderable, srcData[0].addr(), srcData[0].rowBytes(),
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400424 nullptr, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400425}