blob: 10b59c864feef655649be94d9493f0f70f3a7faa [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
Adlai Hollera0693042020-10-14 11:23:11 -04008#include "src/gpu/GrDirectContextPriv.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContextThreadSafeProxy.h"
Robert Phillips4e105e22020-07-16 09:18:50 -040011#include "include/gpu/GrDirectContext.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"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.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"
Robert Phillipse19babf2020-04-06 13:57:30 -040024#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/text/GrTextBlobCache.h"
26#include "src/image/SkImage_Base.h"
27#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050028
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040029#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsdbaf3172019-02-06 15:12:53 -050030 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Adlai Holler33dbd652020-06-01 12:35:42 -040031#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->singleOwner())
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040032#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
Robert Phillipsdbaf3172019-02-06 15:12:53 -050033
Adlai Hollera0693042020-10-14 11:23:11 -040034sk_sp<const GrCaps> GrDirectContextPriv::refCaps() const {
Robert Phillipsa41c6852019-02-07 10:44:10 -050035 return fContext->refCaps();
36}
37
Adlai Hollera0693042020-10-14 11:23:11 -040038void GrDirectContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
Robert Phillipsc5058a62019-02-15 12:52:59 -050039 fContext->addOnFlushCallbackObject(onFlushCBObject);
40}
41
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050042GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(SkSpan<GrSurfaceProxy*> proxies,
Adlai Hollera0693042020-10-14 11:23:11 -040043 const GrFlushInfo& info) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040044 ASSERT_SINGLE_OWNER
45 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
Adlai Hollera0693042020-10-14 11:23:11 -040046 GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", fContext);
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050047#ifdef SK_DEBUG
48 for (GrSurfaceProxy* proxy : proxies) {
49 SkASSERT(proxy);
50 ASSERT_OWNED_PROXY(proxy);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040051 }
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050052#endif
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040053 return fContext->drawingManager()->flushSurfaces(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050054 proxies, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050055}
56
Adlai Hollera0693042020-10-14 11:23:11 -040057void GrDirectContextPriv::flushSurface(GrSurfaceProxy* proxy) {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050058 size_t size = proxy ? 1 : 0;
59 this->flushSurfaces({&proxy, size}, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +000060}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050061
Adlai Hollera0693042020-10-14 11:23:11 -040062void GrDirectContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
63 GrRenderTargetProxy* newDest) {
Adlai Holler7580ad42020-06-24 13:45:25 -040064 fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest);
65}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050066
Adlai Hollera0693042020-10-14 11:23:11 -040067bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050068 GrGpu* gpu = this->getGpu();
69 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040070 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050071 }
72
Robert Phillips43e7e4f2020-05-06 13:34:45 -040073 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050074}
75
76
Robert Phillipsdbaf3172019-02-06 15:12:53 -050077//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050078#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050079
Adlai Hollera0693042020-10-14 11:23:11 -040080void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050081#if GR_CACHE_STATS
82 fContext->fResourceCache->dumpStats(out);
83#endif
84}
85
Adlai Hollera0693042020-10-14 11:23:11 -040086void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
87 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050088#if GR_CACHE_STATS
89 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
90#endif
91}
92
Adlai Hollera0693042020-10-14 11:23:11 -040093void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050094 SkString out;
95 this->dumpCacheStats(&out);
96 SkDebugf("%s", out.c_str());
97}
98
Robert Phillips273f1072020-05-05 13:03:07 -040099/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400100void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400101#if GR_GPU_STATS
102 fContext->fGpu->stats()->reset();
103#endif
104}
105
Adlai Hollera0693042020-10-14 11:23:11 -0400106void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500107#if GR_GPU_STATS
108 return fContext->fGpu->stats()->dump(out);
109#endif
110}
111
Adlai Hollera0693042020-10-14 11:23:11 -0400112void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
113 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500114#if GR_GPU_STATS
115 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
116#endif
117}
118
Adlai Hollera0693042020-10-14 11:23:11 -0400119void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500120 SkString out;
121 this->dumpGpuStats(&out);
122 SkDebugf("%s", out.c_str());
123}
124
Robert Phillips273f1072020-05-05 13:03:07 -0400125/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400126void GrDirectContextPriv::resetContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400127#if GR_GPU_STATS
128 fContext->stats()->reset();
129#endif
130}
131
Adlai Hollera0693042020-10-14 11:23:11 -0400132void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400133#if GR_GPU_STATS
134 return fContext->stats()->dump(out);
135#endif
136}
137
Adlai Hollera0693042020-10-14 11:23:11 -0400138void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
139 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400140#if GR_GPU_STATS
141 return fContext->stats()->dumpKeyValuePairs(keys, values);
142#endif
143}
144
Adlai Hollera0693042020-10-14 11:23:11 -0400145void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400146 SkString out;
147 this->dumpContextStats(&out);
148 SkDebugf("%s", out.c_str());
149}
150
151/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400152sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
153 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500154 auto atlasManager = this->getAtlasManager();
155 if (!atlasManager) {
156 return nullptr;
157 }
158
159 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500160 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
161 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500162 return nullptr;
163 }
164
Greg Daniel7c165a42020-01-22 12:22:36 -0500165 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500166 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000167 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500168 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500169 return image;
170}
171
Adlai Hollera0693042020-10-14 11:23:11 -0400172void GrDirectContextPriv::testingOnly_purgeAllUnlockedResources() {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500173 fContext->fResourceCache->purgeAllUnlocked();
174}
175
Adlai Hollera0693042020-10-14 11:23:11 -0400176void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
177 GrOnFlushCallbackObject* cb) {
178 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500179 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500180}
181#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400182
Adlai Hollera0693042020-10-14 11:23:11 -0400183bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400185
Greg Daniel6eb8c242019-06-05 10:22:24 -0400186 if (!fContext->fDidTestPMConversions) {
187 fContext->fPMUPMConversionsRoundTrip =
Adlai Hollera0693042020-10-14 11:23:11 -0400188 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400189 fContext->fDidTestPMConversions = true;
190 }
191
192 // The PM<->UPM tests fail or succeed together so we only need to check one.
193 return fContext->fPMUPMConversionsRoundTrip;
194}
195
Adlai Hollera0693042020-10-14 11:23:11 -0400196std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400197 std::unique_ptr<GrFragmentProcessor> fp) {
198 ASSERT_SINGLE_OWNER
199 // We should have already called this->priv().validPMUPMConversionExists() in this case
200 SkASSERT(fContext->fDidTestPMConversions);
201 // ...and it should have succeeded
202 SkASSERT(this->validPMUPMConversionExists());
203
204 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
205}
206
Adlai Hollera0693042020-10-14 11:23:11 -0400207std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400208 std::unique_ptr<GrFragmentProcessor> fp) {
209 ASSERT_SINGLE_OWNER
210 // We should have already called this->priv().validPMUPMConversionExists() in this case
211 SkASSERT(fContext->fDidTestPMConversions);
212 // ...and it should have succeeded
213 SkASSERT(this->validPMUPMConversionExists());
214
215 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
216}