blob: 702d82dbd94692293fa23c73bcbf9482ffaec7bf [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 Phillips43e7e4f2020-05-06 13:34:45 -040072bool GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050073 GrGpu* gpu = this->getGpu();
74 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040075 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050076 }
77
Robert Phillips43e7e4f2020-05-06 13:34:45 -040078 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050079}
80
81
Robert Phillipsdbaf3172019-02-06 15:12:53 -050082//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050083#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050084
85void GrContextPriv::dumpCacheStats(SkString* out) const {
86#if GR_CACHE_STATS
87 fContext->fResourceCache->dumpStats(out);
88#endif
89}
90
91void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
92 SkTArray<double>* values) const {
93#if GR_CACHE_STATS
94 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
95#endif
96}
97
98void GrContextPriv::printCacheStats() const {
99 SkString out;
100 this->dumpCacheStats(&out);
101 SkDebugf("%s", out.c_str());
102}
103
Robert Phillips273f1072020-05-05 13:03:07 -0400104/////////////////////////////////////////////////
105void GrContextPriv::resetGpuStats() const {
106#if GR_GPU_STATS
107 fContext->fGpu->stats()->reset();
108#endif
109}
110
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500111void GrContextPriv::dumpGpuStats(SkString* out) const {
112#if GR_GPU_STATS
113 return fContext->fGpu->stats()->dump(out);
114#endif
115}
116
117void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
118 SkTArray<double>* values) const {
119#if GR_GPU_STATS
120 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
121#endif
122}
123
124void GrContextPriv::printGpuStats() const {
125 SkString out;
126 this->dumpGpuStats(&out);
127 SkDebugf("%s", out.c_str());
128}
129
Robert Phillips273f1072020-05-05 13:03:07 -0400130/////////////////////////////////////////////////
131void GrContextPriv::resetContextStats() const {
132#if GR_GPU_STATS
133 fContext->stats()->reset();
134#endif
135}
136
137void GrContextPriv::dumpContextStats(SkString* out) const {
138#if GR_GPU_STATS
139 return fContext->stats()->dump(out);
140#endif
141}
142
143void GrContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
144 SkTArray<double>* values) const {
145#if GR_GPU_STATS
146 return fContext->stats()->dumpKeyValuePairs(keys, values);
147#endif
148}
149
150void GrContextPriv::printContextStats() const {
151 SkString out;
152 this->dumpContextStats(&out);
153 SkDebugf("%s", out.c_str());
154}
155
156/////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500157void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
Robert Phillips2184fb72019-02-21 16:11:41 -0500158 fContext->priv().getTextBlobCache()->setBudget(bytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500159}
160
161sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
162 auto atlasManager = this->getAtlasManager();
163 if (!atlasManager) {
164 return nullptr;
165 }
166
167 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500168 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
169 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500170 return nullptr;
171 }
172
Greg Daniel7c165a42020-01-22 12:22:36 -0500173 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500174 SkASSERT(views[index].proxy()->priv().isExact());
Brian Salomon729fc0c2019-09-30 16:33:11 +0000175 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
Greg Daniel7c165a42020-01-22 12:22:36 -0500176 views[index], colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500177 return image;
178}
179
180void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
181 fContext->fResourceCache->purgeAllUnlocked();
182}
183
184void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
185 fContext->flush();
Robert Phillips292a6b22019-02-14 14:49:02 -0500186 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500187}
188#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400189
190bool GrContextPriv::validPMUPMConversionExists() {
191 ASSERT_SINGLE_OWNER
192 if (!fContext->fDidTestPMConversions) {
193 fContext->fPMUPMConversionsRoundTrip =
194 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
195 fContext->fDidTestPMConversions = true;
196 }
197
198 // The PM<->UPM tests fail or succeed together so we only need to check one.
199 return fContext->fPMUPMConversionsRoundTrip;
200}
201
202std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
203 std::unique_ptr<GrFragmentProcessor> fp) {
204 ASSERT_SINGLE_OWNER
205 // We should have already called this->priv().validPMUPMConversionExists() in this case
206 SkASSERT(fContext->fDidTestPMConversions);
207 // ...and it should have succeeded
208 SkASSERT(this->validPMUPMConversionExists());
209
210 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
211}
212
213std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
214 std::unique_ptr<GrFragmentProcessor> fp) {
215 ASSERT_SINGLE_OWNER
216 // We should have already called this->priv().validPMUPMConversionExists() in this case
217 SkASSERT(fContext->fDidTestPMConversions);
218 // ...and it should have succeeded
219 SkASSERT(this->validPMUPMConversionExists());
220
221 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
222}