blob: 5507b43637c74cf665220502da0bc2452e901bfe [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 Salomonbf6b9792019-08-21 09:38:10 -040051std::unique_ptr<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(
52 sk_sp<GrSurfaceProxy> proxy,
53 GrColorType colorType,
54 SkAlphaType alphaType,
55 sk_sp<SkColorSpace> colorSpace,
56 const SkSurfaceProps* props) {
Brian Salomond6287472019-06-24 15:50:07 -040057 return fContext->makeWrappedSurfaceContext(std::move(proxy), colorType, alphaType,
58 std::move(colorSpace), props);
Robert Phillips292a6b22019-02-14 14:49:02 -050059}
60
Brian Salomonbf6b9792019-08-21 09:38:10 -040061std::unique_ptr<GrTextureContext> GrContextPriv::makeDeferredTextureContext(
62 SkBackingFit fit,
63 int width,
64 int height,
65 GrColorType colorType,
66 SkAlphaType alphaType,
67 sk_sp<SkColorSpace> colorSpace,
68 GrMipMapped mipMapped,
69 GrSurfaceOrigin origin,
70 SkBudgeted budgeted,
71 GrProtected isProtected) {
Brian Salomon947efe22019-07-16 15:36:11 -040072 return fContext->makeDeferredTextureContext(fit, width, height, colorType, alphaType,
73 std::move(colorSpace), mipMapped, origin, budgeted,
74 isProtected);
Robert Phillips292a6b22019-02-14 14:49:02 -050075}
76
Brian Salomonbf6b9792019-08-21 09:38:10 -040077std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -040078 SkBackingFit fit,
79 int width,
80 int height,
Brian Salomond6287472019-06-24 15:50:07 -040081 GrColorType colorType,
82 sk_sp<SkColorSpace> colorSpace,
83 int sampleCnt,
84 GrMipMapped mipMapped,
85 GrSurfaceOrigin origin,
86 const SkSurfaceProps* surfaceProps,
87 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040088 GrProtected isProtected) {
Brian Salomon27ae52c2019-07-03 11:27:44 -040089 return fContext->makeDeferredRenderTargetContext(fit, width, height, colorType,
Robert Phillipsb97da532019-02-12 15:24:12 -050090 std::move(colorSpace), sampleCnt, mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040091 origin, surfaceProps, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -050092}
93
Brian Salomonbf6b9792019-08-21 09:38:10 -040094std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
Brian Salomon27ae52c2019-07-03 11:27:44 -040095 SkBackingFit fit, int width, int height, GrColorType colorType,
96 sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
Brian Osmanc2486592019-09-20 13:32:51 -040097 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
98 GrProtected isProtected) {
Brian Salomond6287472019-06-24 15:50:07 -040099 return fContext->makeDeferredRenderTargetContextWithFallback(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400100 fit, width, height, colorType, std::move(colorSpace), sampleCnt, mipMapped, origin,
Brian Osmanc2486592019-09-20 13:32:51 -0400101 surfaceProps, budgeted, isProtected);
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500102}
103
Brian Salomonbf6b9792019-08-21 09:38:10 -0400104std::unique_ptr<GrTextureContext> GrContextPriv::makeBackendTextureContext(
105 const GrBackendTexture& tex,
106 GrSurfaceOrigin origin,
107 GrColorType colorType,
108 SkAlphaType alphaType,
109 sk_sp<SkColorSpace> colorSpace) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400110 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500111
112 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400113 tex, colorType, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500114 if (!proxy) {
115 return nullptr;
116 }
117
Brian Salomond6287472019-06-24 15:50:07 -0400118 return this->drawingManager()->makeTextureContext(std::move(proxy), colorType, alphaType,
Brian Salomone7499c72019-06-24 12:12:36 -0400119 std::move(colorSpace));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500120}
121
Brian Salomonbf6b9792019-08-21 09:38:10 -0400122std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400123 const GrBackendTexture& tex,
124 GrSurfaceOrigin origin,
125 int sampleCnt,
126 GrColorType colorType,
127 sk_sp<SkColorSpace> colorSpace,
128 const SkSurfaceProps* props,
129 ReleaseProc releaseProc,
130 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400131 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500132 SkASSERT(sampleCnt > 0);
133
134 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->wrapRenderableBackendTexture(
Robert Phillips0902c982019-07-16 07:47:56 -0400135 tex, origin, sampleCnt, colorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
136 releaseProc, releaseCtx));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500137 if (!proxy) {
138 return nullptr;
139 }
140
Brian Salomond6287472019-06-24 15:50:07 -0400141 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500142 std::move(colorSpace), props);
143}
144
Brian Salomonbf6b9792019-08-21 09:38:10 -0400145std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400146 const GrBackendRenderTarget& backendRT,
147 GrSurfaceOrigin origin,
148 GrColorType colorType,
149 sk_sp<SkColorSpace> colorSpace,
150 const SkSurfaceProps* surfaceProps,
151 ReleaseProc releaseProc,
152 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400153 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500154
155 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400156 backendRT, colorType, origin, releaseProc, releaseCtx);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500157 if (!proxy) {
158 return nullptr;
159 }
160
Brian Salomond6287472019-06-24 15:50:07 -0400161 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
162 std::move(colorSpace), surfaceProps);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500163}
164
Brian Salomonbf6b9792019-08-21 09:38:10 -0400165std::unique_ptr<GrRenderTargetContext>
166GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(const GrBackendTexture& tex,
167 GrSurfaceOrigin origin,
168 int sampleCnt,
169 GrColorType colorType,
170 sk_sp<SkColorSpace> colorSpace,
171 const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400172 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500173 SkASSERT(sampleCnt > 0);
174 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400175 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, colorType,
176 origin, sampleCnt));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500177 if (!proxy) {
178 return nullptr;
179 }
180
Brian Salomond6287472019-06-24 15:50:07 -0400181 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
182 std::move(colorSpace), props);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500183}
184
Brian Salomonbf6b9792019-08-21 09:38:10 -0400185std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext(
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500186 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400187 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500188 sk_sp<GrSurfaceProxy> proxy(
189 this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo));
190 if (!proxy) {
191 return nullptr;
192 }
193
Brian Salomond6287472019-06-24 15:50:07 -0400194 return this->drawingManager()->makeRenderTargetContext(
195 std::move(proxy),
196 SkColorTypeToGrColorType(imageInfo.colorType()),
197 imageInfo.refColorSpace(),
198 props);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500199}
200
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400201GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
202 const GrFlushInfo& info) {
203 ASSERT_SINGLE_OWNER
204 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
205 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
206 SkASSERT(numProxies >= 0);
207 SkASSERT(!numProxies || proxies);
208 for (int i = 0; i < numProxies; ++i) {
209 SkASSERT(proxies[i]);
210 ASSERT_OWNED_PROXY(proxies[i]);
211 }
212 return fContext->drawingManager()->flushSurfaces(
213 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500214}
215
Brian Salomon693bc2b2019-05-09 13:48:00 +0000216void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400217 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +0000218}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500219
Chris Dalton6b498102019-08-01 14:14:52 -0600220void GrContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
221 fContext->drawingManager()->moveRenderTasksToDDL(ddl);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500222}
223
Chris Dalton6b498102019-08-01 14:14:52 -0600224void GrContextPriv::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
225 GrRenderTargetProxy* newDest) {
226 fContext->drawingManager()->copyRenderTasksFromDDL(ddl, newDest);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500227}
228
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500229//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500230
231#if GR_TEST_UTILS
232void GrContextPriv::resetGpuStats() const {
233#if GR_GPU_STATS
234 fContext->fGpu->stats()->reset();
235#endif
236}
237
238void GrContextPriv::dumpCacheStats(SkString* out) const {
239#if GR_CACHE_STATS
240 fContext->fResourceCache->dumpStats(out);
241#endif
242}
243
244void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
245 SkTArray<double>* values) const {
246#if GR_CACHE_STATS
247 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
248#endif
249}
250
251void GrContextPriv::printCacheStats() const {
252 SkString out;
253 this->dumpCacheStats(&out);
254 SkDebugf("%s", out.c_str());
255}
256
257void GrContextPriv::dumpGpuStats(SkString* out) const {
258#if GR_GPU_STATS
259 return fContext->fGpu->stats()->dump(out);
260#endif
261}
262
263void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
264 SkTArray<double>* values) const {
265#if GR_GPU_STATS
266 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
267#endif
268}
269
270void GrContextPriv::printGpuStats() const {
271 SkString out;
272 this->dumpGpuStats(&out);
273 SkDebugf("%s", out.c_str());
274}
275
276void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
Robert Phillips2184fb72019-02-21 16:11:41 -0500277 fContext->priv().getTextBlobCache()->setBudget(bytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500278}
279
280sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
281 auto atlasManager = this->getAtlasManager();
282 if (!atlasManager) {
283 return nullptr;
284 }
285
286 unsigned int numActiveProxies;
287 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(format, &numActiveProxies);
288 if (index >= numActiveProxies || !proxies || !proxies[index]) {
289 return nullptr;
290 }
291
292 SkASSERT(proxies[index]->priv().isExact());
293 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
294 kPremul_SkAlphaType, proxies[index], nullptr));
295 return image;
296}
297
298void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
299 fContext->fResourceCache->purgeAllUnlocked();
300}
301
302void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
303 fContext->flush();
Robert Phillips292a6b22019-02-14 14:49:02 -0500304 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500305}
306#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400307
308bool GrContextPriv::validPMUPMConversionExists() {
309 ASSERT_SINGLE_OWNER
310 if (!fContext->fDidTestPMConversions) {
311 fContext->fPMUPMConversionsRoundTrip =
312 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
313 fContext->fDidTestPMConversions = true;
314 }
315
316 // The PM<->UPM tests fail or succeed together so we only need to check one.
317 return fContext->fPMUPMConversionsRoundTrip;
318}
319
320std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
321 std::unique_ptr<GrFragmentProcessor> fp) {
322 ASSERT_SINGLE_OWNER
323 // We should have already called this->priv().validPMUPMConversionExists() in this case
324 SkASSERT(fContext->fDidTestPMConversions);
325 // ...and it should have succeeded
326 SkASSERT(this->validPMUPMConversionExists());
327
328 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
329}
330
331std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
332 std::unique_ptr<GrFragmentProcessor> fp) {
333 ASSERT_SINGLE_OWNER
334 // We should have already called this->priv().validPMUPMConversionExists() in this case
335 SkASSERT(fContext->fDidTestPMConversions);
336 // ...and it should have succeeded
337 SkASSERT(this->validPMUPMConversionExists());
338
339 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
340}
Robert Phillipscb1adb42019-06-10 15:09:34 -0400341
342//////////////////////////////////////////////////////////////////////////////
343
344#include "src/core/SkMipMap.h"
345
346GrBackendTexture GrContextPriv::createBackendTexture(const SkPixmap srcData[], int numLevels,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400347 GrRenderable renderable,
348 GrProtected isProtected) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400349 if (!fContext->asDirectContext()) {
350 return {};
351 }
352
353 if (this->abandoned()) {
354 return {};
355 }
356
357 if (!srcData || !numLevels) {
358 return {};
359 }
360
361 int baseWidth = srcData[0].width();
362 int baseHeight = srcData[0].height();
363 SkColorType colorType = srcData[0].colorType();
364
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400365 GrBackendFormat backendFormat = fContext->defaultBackendFormat(colorType, renderable);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400366
367 GrGpu* gpu = fContext->fGpu.get();
368
Robert Phillipscb1adb42019-06-10 15:09:34 -0400369 return gpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
Robert Phillips8a480572019-09-24 11:55:29 -0400370 numLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
Robert Phillips57ef6802019-09-23 10:12:47 -0400371 renderable, srcData, numLevels, nullptr, isProtected);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400372}