blob: cc2fbea5df9a52965463e959acecc39ad9a7e5ef [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextThreadSafeProxyPriv.h"
13#include "src/gpu/GrDrawingManager.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrMemoryPool.h"
16#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46e366a2019-12-16 14:38:36 -050017#include "src/gpu/GrSurfaceContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040018#include "src/gpu/GrSurfaceContextPriv.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050019#include "src/gpu/GrSurfacePriv.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)
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040031#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050032 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->singleOwner());)
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040033#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
34#define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void)
Robert Phillipsdbaf3172019-02-06 15:12:53 -050035
Robert Phillipsa41c6852019-02-07 10:44:10 -050036sk_sp<const GrCaps> GrContextPriv::refCaps() const {
37 return fContext->refCaps();
38}
39
Robert Phillipsc5058a62019-02-15 12:52:59 -050040void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
41 fContext->addOnFlushCallbackObject(onFlushCBObject);
42}
43
Greg Daniel55f040b2020-02-13 15:38:32 +000044GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040045 const GrFlushInfo& info) {
46 ASSERT_SINGLE_OWNER
47 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
48 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
49 SkASSERT(numProxies >= 0);
50 SkASSERT(!numProxies || proxies);
51 for (int i = 0; i < numProxies; ++i) {
52 SkASSERT(proxies[i]);
53 ASSERT_OWNED_PROXY(proxies[i]);
54 }
55 return fContext->drawingManager()->flushSurfaces(
Greg Daniel55f040b2020-02-13 15:38:32 +000056 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050057}
58
Greg Daniel55f040b2020-02-13 15:38:32 +000059void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
60 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
Brian Salomon693bc2b2019-05-09 13:48:00 +000061}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050062
Chris Dalton6b498102019-08-01 14:14:52 -060063void GrContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
64 fContext->drawingManager()->moveRenderTasksToDDL(ddl);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050065}
66
Chris Dalton6b498102019-08-01 14:14:52 -060067void GrContextPriv::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
68 GrRenderTargetProxy* newDest) {
69 fContext->drawingManager()->copyRenderTasksFromDDL(ddl, newDest);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050070}
71
Robert Phillips7b0ed552020-02-20 12:45:19 -050072void GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
73 GrGpu* gpu = this->getGpu();
74 if (!gpu) {
75 return;
76 }
77
78 gpu->compile(desc, info);
79}
80
81
Robert Phillipsdbaf3172019-02-06 15:12:53 -050082//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050083
84#if GR_TEST_UTILS
85void GrContextPriv::resetGpuStats() const {
86#if GR_GPU_STATS
87 fContext->fGpu->stats()->reset();
88#endif
89}
90
91void GrContextPriv::dumpCacheStats(SkString* out) const {
92#if GR_CACHE_STATS
93 fContext->fResourceCache->dumpStats(out);
94#endif
95}
96
97void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
98 SkTArray<double>* values) const {
99#if GR_CACHE_STATS
100 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
101#endif
102}
103
104void GrContextPriv::printCacheStats() const {
105 SkString out;
106 this->dumpCacheStats(&out);
107 SkDebugf("%s", out.c_str());
108}
109
110void GrContextPriv::dumpGpuStats(SkString* out) const {
111#if GR_GPU_STATS
112 return fContext->fGpu->stats()->dump(out);
113#endif
114}
115
116void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
117 SkTArray<double>* values) const {
118#if GR_GPU_STATS
119 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
120#endif
121}
122
123void GrContextPriv::printGpuStats() const {
124 SkString out;
125 this->dumpGpuStats(&out);
126 SkDebugf("%s", out.c_str());
127}
128
129void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
Robert Phillips2184fb72019-02-21 16:11:41 -0500130 fContext->priv().getTextBlobCache()->setBudget(bytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500131}
132
133sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
134 auto atlasManager = this->getAtlasManager();
135 if (!atlasManager) {
136 return nullptr;
137 }
138
139 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500140 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
141 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500142 return nullptr;
143 }
144
Greg Daniel7c165a42020-01-22 12:22:36 -0500145 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500146 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000147 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500148 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500149 return image;
150}
151
152void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
153 fContext->fResourceCache->purgeAllUnlocked();
154}
155
156void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
157 fContext->flush();
Robert Phillips292a6b22019-02-14 14:49:02 -0500158 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500159}
160#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400161
162bool GrContextPriv::validPMUPMConversionExists() {
163 ASSERT_SINGLE_OWNER
164 if (!fContext->fDidTestPMConversions) {
165 fContext->fPMUPMConversionsRoundTrip =
166 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
167 fContext->fDidTestPMConversions = true;
168 }
169
170 // The PM<->UPM tests fail or succeed together so we only need to check one.
171 return fContext->fPMUPMConversionsRoundTrip;
172}
173
174std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
175 std::unique_ptr<GrFragmentProcessor> fp) {
176 ASSERT_SINGLE_OWNER
177 // We should have already called this->priv().validPMUPMConversionExists() in this case
178 SkASSERT(fContext->fDidTestPMConversions);
179 // ...and it should have succeeded
180 SkASSERT(this->validPMUPMConversionExists());
181
182 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
183}
184
185std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
186 std::unique_ptr<GrFragmentProcessor> fp) {
187 ASSERT_SINGLE_OWNER
188 // We should have already called this->priv().validPMUPMConversionExists() in this case
189 SkASSERT(fContext->fDidTestPMConversions);
190 // ...and it should have succeeded
191 SkASSERT(this->validPMUPMConversionExists());
192
193 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
194}