blob: d6188dc40b4e5b35bd7c8409572f9773e0d3d959 [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,
97 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted) {
Brian Salomond6287472019-06-24 15:50:07 -040098 return fContext->makeDeferredRenderTargetContextWithFallback(
Brian Salomon27ae52c2019-07-03 11:27:44 -040099 fit, width, height, colorType, std::move(colorSpace), sampleCnt, mipMapped, origin,
100 surfaceProps, budgeted);
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500101}
102
Brian Salomonbf6b9792019-08-21 09:38:10 -0400103std::unique_ptr<GrTextureContext> GrContextPriv::makeBackendTextureContext(
104 const GrBackendTexture& tex,
105 GrSurfaceOrigin origin,
106 GrColorType colorType,
107 SkAlphaType alphaType,
108 sk_sp<SkColorSpace> colorSpace) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400109 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500110
111 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400112 tex, colorType, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500113 if (!proxy) {
114 return nullptr;
115 }
116
Brian Salomond6287472019-06-24 15:50:07 -0400117 return this->drawingManager()->makeTextureContext(std::move(proxy), colorType, alphaType,
Brian Salomone7499c72019-06-24 12:12:36 -0400118 std::move(colorSpace));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500119}
120
Brian Salomonbf6b9792019-08-21 09:38:10 -0400121std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400122 const GrBackendTexture& tex,
123 GrSurfaceOrigin origin,
124 int sampleCnt,
125 GrColorType colorType,
126 sk_sp<SkColorSpace> colorSpace,
127 const SkSurfaceProps* props,
128 ReleaseProc releaseProc,
129 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400130 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500131 SkASSERT(sampleCnt > 0);
132
133 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->wrapRenderableBackendTexture(
Robert Phillips0902c982019-07-16 07:47:56 -0400134 tex, origin, sampleCnt, colorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
135 releaseProc, releaseCtx));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500136 if (!proxy) {
137 return nullptr;
138 }
139
Brian Salomond6287472019-06-24 15:50:07 -0400140 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500141 std::move(colorSpace), props);
142}
143
Brian Salomonbf6b9792019-08-21 09:38:10 -0400144std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400145 const GrBackendRenderTarget& backendRT,
146 GrSurfaceOrigin origin,
147 GrColorType colorType,
148 sk_sp<SkColorSpace> colorSpace,
149 const SkSurfaceProps* surfaceProps,
150 ReleaseProc releaseProc,
151 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400152 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500153
154 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400155 backendRT, colorType, origin, releaseProc, releaseCtx);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500156 if (!proxy) {
157 return nullptr;
158 }
159
Brian Salomond6287472019-06-24 15:50:07 -0400160 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
161 std::move(colorSpace), surfaceProps);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500162}
163
Brian Salomonbf6b9792019-08-21 09:38:10 -0400164std::unique_ptr<GrRenderTargetContext>
165GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(const GrBackendTexture& tex,
166 GrSurfaceOrigin origin,
167 int sampleCnt,
168 GrColorType colorType,
169 sk_sp<SkColorSpace> colorSpace,
170 const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400171 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500172 SkASSERT(sampleCnt > 0);
173 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400174 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, colorType,
175 origin, sampleCnt));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500176 if (!proxy) {
177 return nullptr;
178 }
179
Brian Salomond6287472019-06-24 15:50:07 -0400180 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
181 std::move(colorSpace), props);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500182}
183
Brian Salomonbf6b9792019-08-21 09:38:10 -0400184std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext(
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500185 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400186 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500187 sk_sp<GrSurfaceProxy> proxy(
188 this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo));
189 if (!proxy) {
190 return nullptr;
191 }
192
Brian Salomond6287472019-06-24 15:50:07 -0400193 return this->drawingManager()->makeRenderTargetContext(
194 std::move(proxy),
195 SkColorTypeToGrColorType(imageInfo.colorType()),
196 imageInfo.refColorSpace(),
197 props);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500198}
199
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400200GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
201 const GrFlushInfo& info) {
202 ASSERT_SINGLE_OWNER
203 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
204 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
205 SkASSERT(numProxies >= 0);
206 SkASSERT(!numProxies || proxies);
207 for (int i = 0; i < numProxies; ++i) {
208 SkASSERT(proxies[i]);
209 ASSERT_OWNED_PROXY(proxies[i]);
210 }
211 return fContext->drawingManager()->flushSurfaces(
212 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500213}
214
Brian Salomon693bc2b2019-05-09 13:48:00 +0000215void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400216 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +0000217}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500218
Chris Dalton6b498102019-08-01 14:14:52 -0600219void GrContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
220 fContext->drawingManager()->moveRenderTasksToDDL(ddl);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500221}
222
Chris Dalton6b498102019-08-01 14:14:52 -0600223void GrContextPriv::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
224 GrRenderTargetProxy* newDest) {
225 fContext->drawingManager()->copyRenderTasksFromDDL(ddl, newDest);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500226}
227
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500228//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500229
230#if GR_TEST_UTILS
231void GrContextPriv::resetGpuStats() const {
232#if GR_GPU_STATS
233 fContext->fGpu->stats()->reset();
234#endif
235}
236
237void GrContextPriv::dumpCacheStats(SkString* out) const {
238#if GR_CACHE_STATS
239 fContext->fResourceCache->dumpStats(out);
240#endif
241}
242
243void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
244 SkTArray<double>* values) const {
245#if GR_CACHE_STATS
246 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
247#endif
248}
249
250void GrContextPriv::printCacheStats() const {
251 SkString out;
252 this->dumpCacheStats(&out);
253 SkDebugf("%s", out.c_str());
254}
255
256void GrContextPriv::dumpGpuStats(SkString* out) const {
257#if GR_GPU_STATS
258 return fContext->fGpu->stats()->dump(out);
259#endif
260}
261
262void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
263 SkTArray<double>* values) const {
264#if GR_GPU_STATS
265 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
266#endif
267}
268
269void GrContextPriv::printGpuStats() const {
270 SkString out;
271 this->dumpGpuStats(&out);
272 SkDebugf("%s", out.c_str());
273}
274
275void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
Robert Phillips2184fb72019-02-21 16:11:41 -0500276 fContext->priv().getTextBlobCache()->setBudget(bytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500277}
278
279sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
280 auto atlasManager = this->getAtlasManager();
281 if (!atlasManager) {
282 return nullptr;
283 }
284
285 unsigned int numActiveProxies;
286 const sk_sp<GrTextureProxy>* proxies = atlasManager->getProxies(format, &numActiveProxies);
287 if (index >= numActiveProxies || !proxies || !proxies[index]) {
288 return nullptr;
289 }
290
291 SkASSERT(proxies[index]->priv().isExact());
292 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
293 kPremul_SkAlphaType, proxies[index], nullptr));
294 return image;
295}
296
297void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
298 fContext->fResourceCache->purgeAllUnlocked();
299}
300
301void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
302 fContext->flush();
Robert Phillips292a6b22019-02-14 14:49:02 -0500303 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500304}
305#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400306
307bool GrContextPriv::validPMUPMConversionExists() {
308 ASSERT_SINGLE_OWNER
309 if (!fContext->fDidTestPMConversions) {
310 fContext->fPMUPMConversionsRoundTrip =
311 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
312 fContext->fDidTestPMConversions = true;
313 }
314
315 // The PM<->UPM tests fail or succeed together so we only need to check one.
316 return fContext->fPMUPMConversionsRoundTrip;
317}
318
319std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
320 std::unique_ptr<GrFragmentProcessor> fp) {
321 ASSERT_SINGLE_OWNER
322 // We should have already called this->priv().validPMUPMConversionExists() in this case
323 SkASSERT(fContext->fDidTestPMConversions);
324 // ...and it should have succeeded
325 SkASSERT(this->validPMUPMConversionExists());
326
327 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
328}
329
330std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
331 std::unique_ptr<GrFragmentProcessor> fp) {
332 ASSERT_SINGLE_OWNER
333 // We should have already called this->priv().validPMUPMConversionExists() in this case
334 SkASSERT(fContext->fDidTestPMConversions);
335 // ...and it should have succeeded
336 SkASSERT(this->validPMUPMConversionExists());
337
338 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
339}
Robert Phillipscb1adb42019-06-10 15:09:34 -0400340
341//////////////////////////////////////////////////////////////////////////////
342
343#include "src/core/SkMipMap.h"
344
345GrBackendTexture GrContextPriv::createBackendTexture(const SkPixmap srcData[], int numLevels,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400346 GrRenderable renderable,
347 GrProtected isProtected) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400348 if (!fContext->asDirectContext()) {
349 return {};
350 }
351
352 if (this->abandoned()) {
353 return {};
354 }
355
356 if (!srcData || !numLevels) {
357 return {};
358 }
359
360 int baseWidth = srcData[0].width();
361 int baseHeight = srcData[0].height();
362 SkColorType colorType = srcData[0].colorType();
363
364 if (numLevels > 1) {
365 if (numLevels != SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1) {
366 return {};
367 }
368
369 int currentWidth = baseWidth;
370 int currentHeight = baseHeight;
371 for (int i = 1; i < numLevels; ++i) {
372 currentWidth = SkTMax(1, currentWidth / 2);
373 currentHeight = SkTMax(1, currentHeight / 2);
374
375 if (srcData[i].colorType() != colorType) {
376 return {};
377 }
378
379 if (srcData[i].width() != currentWidth || srcData[i].height() != currentHeight) {
380 return {};
381 }
382 }
383 }
384
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400385 GrBackendFormat backendFormat = fContext->defaultBackendFormat(colorType, renderable);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400386 if (!backendFormat.isValid()) {
387 return {};
388 }
389
390 GrGpu* gpu = fContext->fGpu.get();
391
392 // TODO: propagate the array of pixmaps interface to GrGpu
393 return gpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
394 GrMipMapped::kNo, // TODO: use real mipmap setting here
395 renderable, srcData[0].addr(), srcData[0].rowBytes(),
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400396 nullptr, isProtected);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400397}