blob: a0f5d49379156c3135dad61756e8fdbd2b02effd [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 Daniel46e366a2019-12-16 14:38:36 -050018#include "src/gpu/GrSurfaceContext.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/SkGr.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050022#include "src/gpu/effects/GrSkSLFP.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 Phillipsc5058a62019-02-15 12:52:59 -050039void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
40 fContext->addOnFlushCallbackObject(onFlushCBObject);
41}
42
Brian Salomonbf6b9792019-08-21 09:38:10 -040043std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -040044 SkBackingFit fit,
45 int width,
46 int height,
Brian Salomond6287472019-06-24 15:50:07 -040047 GrColorType colorType,
48 sk_sp<SkColorSpace> colorSpace,
49 int sampleCnt,
50 GrMipMapped mipMapped,
51 GrSurfaceOrigin origin,
52 const SkSurfaceProps* surfaceProps,
53 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040054 GrProtected isProtected) {
Brian Salomon27ae52c2019-07-03 11:27:44 -040055 return fContext->makeDeferredRenderTargetContext(fit, width, height, colorType,
Robert Phillipsb97da532019-02-12 15:24:12 -050056 std::move(colorSpace), sampleCnt, mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040057 origin, surfaceProps, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -050058}
59
Brian Salomonbf6b9792019-08-21 09:38:10 -040060std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
Brian Salomon27ae52c2019-07-03 11:27:44 -040061 SkBackingFit fit, int width, int height, GrColorType colorType,
62 sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
Brian Osmanc2486592019-09-20 13:32:51 -040063 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
64 GrProtected isProtected) {
Brian Salomond6287472019-06-24 15:50:07 -040065 return fContext->makeDeferredRenderTargetContextWithFallback(
Brian Salomon27ae52c2019-07-03 11:27:44 -040066 fit, width, height, colorType, std::move(colorSpace), sampleCnt, mipMapped, origin,
Brian Osmanc2486592019-09-20 13:32:51 -040067 surfaceProps, budgeted, isProtected);
Robert Phillips6f0e02f2019-02-13 11:02:28 -050068}
69
Brian Salomonbf6b9792019-08-21 09:38:10 -040070std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -040071 const GrBackendTexture& tex,
72 GrSurfaceOrigin origin,
73 int sampleCnt,
74 GrColorType colorType,
75 sk_sp<SkColorSpace> colorSpace,
76 const SkSurfaceProps* props,
77 ReleaseProc releaseProc,
78 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040079 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -050080 SkASSERT(sampleCnt > 0);
81
82 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->wrapRenderableBackendTexture(
Robert Phillips0902c982019-07-16 07:47:56 -040083 tex, origin, sampleCnt, colorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
84 releaseProc, releaseCtx));
Robert Phillipsdbaf3172019-02-06 15:12:53 -050085 if (!proxy) {
86 return nullptr;
87 }
88
Brian Salomond6287472019-06-24 15:50:07 -040089 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
Robert Phillipsdbaf3172019-02-06 15:12:53 -050090 std::move(colorSpace), props);
91}
92
Brian Salomonbf6b9792019-08-21 09:38:10 -040093std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -040094 const GrBackendRenderTarget& backendRT,
95 GrSurfaceOrigin origin,
96 GrColorType colorType,
97 sk_sp<SkColorSpace> colorSpace,
98 const SkSurfaceProps* surfaceProps,
99 ReleaseProc releaseProc,
100 ReleaseContext releaseCtx) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400101 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500102
103 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400104 backendRT, colorType, origin, releaseProc, releaseCtx);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500105 if (!proxy) {
106 return nullptr;
107 }
108
Brian Salomond6287472019-06-24 15:50:07 -0400109 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
110 std::move(colorSpace), surfaceProps);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500111}
112
Brian Salomonbf6b9792019-08-21 09:38:10 -0400113std::unique_ptr<GrRenderTargetContext>
114GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(const GrBackendTexture& tex,
115 GrSurfaceOrigin origin,
116 int sampleCnt,
117 GrColorType colorType,
118 sk_sp<SkColorSpace> colorSpace,
119 const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400120 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500121 SkASSERT(sampleCnt > 0);
122 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400123 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, colorType,
124 origin, sampleCnt));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500125 if (!proxy) {
126 return nullptr;
127 }
128
Brian Salomond6287472019-06-24 15:50:07 -0400129 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
130 std::move(colorSpace), props);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500131}
132
Brian Salomonbf6b9792019-08-21 09:38:10 -0400133std::unique_ptr<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext(
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500134 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400135 ASSERT_SINGLE_OWNER
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500136 sk_sp<GrSurfaceProxy> proxy(
137 this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo));
138 if (!proxy) {
139 return nullptr;
140 }
141
Brian Salomond6287472019-06-24 15:50:07 -0400142 return this->drawingManager()->makeRenderTargetContext(
143 std::move(proxy),
144 SkColorTypeToGrColorType(imageInfo.colorType()),
145 imageInfo.refColorSpace(),
146 props);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500147}
148
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400149GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
150 const GrFlushInfo& info) {
151 ASSERT_SINGLE_OWNER
152 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
153 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
154 SkASSERT(numProxies >= 0);
155 SkASSERT(!numProxies || proxies);
156 for (int i = 0; i < numProxies; ++i) {
157 SkASSERT(proxies[i]);
158 ASSERT_OWNED_PROXY(proxies[i]);
159 }
160 return fContext->drawingManager()->flushSurfaces(
161 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500162}
163
Brian Salomon693bc2b2019-05-09 13:48:00 +0000164void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400165 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +0000166}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500167
Chris Dalton6b498102019-08-01 14:14:52 -0600168void GrContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
169 fContext->drawingManager()->moveRenderTasksToDDL(ddl);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500170}
171
Chris Dalton6b498102019-08-01 14:14:52 -0600172void GrContextPriv::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
173 GrRenderTargetProxy* newDest) {
174 fContext->drawingManager()->copyRenderTasksFromDDL(ddl, newDest);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500175}
176
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500177//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500178
179#if GR_TEST_UTILS
180void GrContextPriv::resetGpuStats() const {
181#if GR_GPU_STATS
182 fContext->fGpu->stats()->reset();
183#endif
184}
185
186void GrContextPriv::dumpCacheStats(SkString* out) const {
187#if GR_CACHE_STATS
188 fContext->fResourceCache->dumpStats(out);
189#endif
190}
191
192void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
193 SkTArray<double>* values) const {
194#if GR_CACHE_STATS
195 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
196#endif
197}
198
199void GrContextPriv::printCacheStats() const {
200 SkString out;
201 this->dumpCacheStats(&out);
202 SkDebugf("%s", out.c_str());
203}
204
205void GrContextPriv::dumpGpuStats(SkString* out) const {
206#if GR_GPU_STATS
207 return fContext->fGpu->stats()->dump(out);
208#endif
209}
210
211void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
212 SkTArray<double>* values) const {
213#if GR_GPU_STATS
214 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
215#endif
216}
217
218void GrContextPriv::printGpuStats() const {
219 SkString out;
220 this->dumpGpuStats(&out);
221 SkDebugf("%s", out.c_str());
222}
223
224void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
Robert Phillips2184fb72019-02-21 16:11:41 -0500225 fContext->priv().getTextBlobCache()->setBudget(bytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500226}
227
228sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
229 auto atlasManager = this->getAtlasManager();
230 if (!atlasManager) {
231 return nullptr;
232 }
233
234 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500235 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
236 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500237 return nullptr;
238 }
239
Greg Daniel9715b6c2019-12-10 15:03:10 -0500240 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000241 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel81b98972019-12-13 11:09:43 -0500242 kPremul_SkAlphaType, views[index], nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500243 return image;
244}
245
246void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
247 fContext->fResourceCache->purgeAllUnlocked();
248}
249
250void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
251 fContext->flush();
Robert Phillips292a6b22019-02-14 14:49:02 -0500252 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500253}
254#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400255
256bool GrContextPriv::validPMUPMConversionExists() {
257 ASSERT_SINGLE_OWNER
258 if (!fContext->fDidTestPMConversions) {
259 fContext->fPMUPMConversionsRoundTrip =
260 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
261 fContext->fDidTestPMConversions = true;
262 }
263
264 // The PM<->UPM tests fail or succeed together so we only need to check one.
265 return fContext->fPMUPMConversionsRoundTrip;
266}
267
268std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
269 std::unique_ptr<GrFragmentProcessor> fp) {
270 ASSERT_SINGLE_OWNER
271 // We should have already called this->priv().validPMUPMConversionExists() in this case
272 SkASSERT(fContext->fDidTestPMConversions);
273 // ...and it should have succeeded
274 SkASSERT(this->validPMUPMConversionExists());
275
276 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
277}
278
279std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
280 std::unique_ptr<GrFragmentProcessor> fp) {
281 ASSERT_SINGLE_OWNER
282 // We should have already called this->priv().validPMUPMConversionExists() in this case
283 SkASSERT(fContext->fDidTestPMConversions);
284 // ...and it should have succeeded
285 SkASSERT(this->validPMUPMConversionExists());
286
287 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
288}