blob: c991d7d6f74baf80d0e9ef949c117acc871f395f [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 Hollera0693042020-10-14 11:23:11 -040042GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
43 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);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040047 SkASSERT(numProxies >= 0);
48 SkASSERT(!numProxies || proxies);
49 for (int i = 0; i < numProxies; ++i) {
50 SkASSERT(proxies[i]);
51 ASSERT_OWNED_PROXY(proxies[i]);
52 }
53 return fContext->drawingManager()->flushSurfaces(
Greg Daniel9efe3862020-06-11 11:51:06 -040054 proxies, numProxies, 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) {
Greg Daniel55f040b2020-02-13 15:38:32 +000058 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +000059}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050060
Adlai Hollera0693042020-10-14 11:23:11 -040061void GrDirectContextPriv::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
62 GrRenderTargetProxy* newDest) {
Adlai Holler7580ad42020-06-24 13:45:25 -040063 fContext->drawingManager()->copyRenderTasksFromDDL(std::move(ddl), newDest);
64}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050065
Adlai Hollera0693042020-10-14 11:23:11 -040066bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050067 GrGpu* gpu = this->getGpu();
68 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040069 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050070 }
71
Robert Phillips43e7e4f2020-05-06 13:34:45 -040072 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050073}
74
75
Robert Phillipsdbaf3172019-02-06 15:12:53 -050076//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050077#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050078
Adlai Hollera0693042020-10-14 11:23:11 -040079void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050080#if GR_CACHE_STATS
81 fContext->fResourceCache->dumpStats(out);
82#endif
83}
84
Adlai Hollera0693042020-10-14 11:23:11 -040085void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
86 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050087#if GR_CACHE_STATS
88 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
89#endif
90}
91
Adlai Hollera0693042020-10-14 11:23:11 -040092void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050093 SkString out;
94 this->dumpCacheStats(&out);
95 SkDebugf("%s", out.c_str());
96}
97
Robert Phillips273f1072020-05-05 13:03:07 -040098/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -040099void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400100#if GR_GPU_STATS
101 fContext->fGpu->stats()->reset();
102#endif
103}
104
Adlai Hollera0693042020-10-14 11:23:11 -0400105void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500106#if GR_GPU_STATS
107 return fContext->fGpu->stats()->dump(out);
108#endif
109}
110
Adlai Hollera0693042020-10-14 11:23:11 -0400111void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
112 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500113#if GR_GPU_STATS
114 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
115#endif
116}
117
Adlai Hollera0693042020-10-14 11:23:11 -0400118void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500119 SkString out;
120 this->dumpGpuStats(&out);
121 SkDebugf("%s", out.c_str());
122}
123
Robert Phillips273f1072020-05-05 13:03:07 -0400124/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400125void GrDirectContextPriv::resetContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400126#if GR_GPU_STATS
127 fContext->stats()->reset();
128#endif
129}
130
Adlai Hollera0693042020-10-14 11:23:11 -0400131void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400132#if GR_GPU_STATS
133 return fContext->stats()->dump(out);
134#endif
135}
136
Adlai Hollera0693042020-10-14 11:23:11 -0400137void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
138 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400139#if GR_GPU_STATS
140 return fContext->stats()->dumpKeyValuePairs(keys, values);
141#endif
142}
143
Adlai Hollera0693042020-10-14 11:23:11 -0400144void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400145 SkString out;
146 this->dumpContextStats(&out);
147 SkDebugf("%s", out.c_str());
148}
149
150/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400151sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
152 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500153 auto atlasManager = this->getAtlasManager();
154 if (!atlasManager) {
155 return nullptr;
156 }
157
158 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500159 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
160 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500161 return nullptr;
162 }
163
Greg Daniel7c165a42020-01-22 12:22:36 -0500164 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500165 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000166 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500167 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500168 return image;
169}
170
Adlai Hollera0693042020-10-14 11:23:11 -0400171void GrDirectContextPriv::testingOnly_purgeAllUnlockedResources() {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500172 fContext->fResourceCache->purgeAllUnlocked();
173}
174
Adlai Hollera0693042020-10-14 11:23:11 -0400175void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
176 GrOnFlushCallbackObject* cb) {
177 fContext->flushAndSubmit();
Robert Phillips292a6b22019-02-14 14:49:02 -0500178 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500179}
180#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400181
Adlai Hollera0693042020-10-14 11:23:11 -0400182bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400183 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400184
Greg Daniel6eb8c242019-06-05 10:22:24 -0400185 if (!fContext->fDidTestPMConversions) {
186 fContext->fPMUPMConversionsRoundTrip =
Adlai Hollera0693042020-10-14 11:23:11 -0400187 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400188 fContext->fDidTestPMConversions = true;
189 }
190
191 // The PM<->UPM tests fail or succeed together so we only need to check one.
192 return fContext->fPMUPMConversionsRoundTrip;
193}
194
Adlai Hollera0693042020-10-14 11:23:11 -0400195std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196 std::unique_ptr<GrFragmentProcessor> fp) {
197 ASSERT_SINGLE_OWNER
198 // We should have already called this->priv().validPMUPMConversionExists() in this case
199 SkASSERT(fContext->fDidTestPMConversions);
200 // ...and it should have succeeded
201 SkASSERT(this->validPMUPMConversionExists());
202
203 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
204}
205
Adlai Hollera0693042020-10-14 11:23:11 -0400206std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400207 std::unique_ptr<GrFragmentProcessor> fp) {
208 ASSERT_SINGLE_OWNER
209 // We should have already called this->priv().validPMUPMConversionExists() in this case
210 SkASSERT(fContext->fDidTestPMConversions);
211 // ...and it should have succeeded
212 SkASSERT(this->validPMUPMConversionExists());
213
214 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
215}