blob: e2c5393be7cbe10225c3ecf9a29627b3641f4917 [file] [log] [blame]
robertphillips4fd74ae2016-08-03 14:26:53 -07001/*
2 * Copyright 2016 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#ifndef GrDirectContextPriv_DEFINED
9#define GrDirectContextPriv_DEFINED
robertphillips4fd74ae2016-08-03 14:26:53 -070010
Brian Osmana5842bc2021-05-11 13:41:46 -040011#include "include/core/SkSpan.h"
Robert Phillips80bfda82020-11-12 09:23:36 -050012#include "include/core/SkSurface.h"
Robert Phillips4e105e22020-07-16 09:18:50 -040013#include "include/gpu/GrDirectContext.h"
Robert Phillipscc44feb2021-07-06 12:21:37 -040014#include "src/gpu/BaseDevice.h"
robertphillips4fd74ae2016-08-03 14:26:53 -070015
Robert Phillipse19babf2020-04-06 13:57:30 -040016class GrAtlasManager;
Greg Daniel4065d452018-11-16 15:43:41 -050017class GrBackendFormat;
Greg Danielbcf612b2017-05-01 13:50:58 +000018class GrBackendRenderTarget;
Robert Phillips33bf2b52021-08-02 11:14:38 -040019class GrImageInfo;
Herb Derbye32e1ab2020-10-27 10:29:46 -040020class GrMemoryPool;
Brian Salomond17f6582017-07-19 18:28:58 -040021class GrOnFlushCallbackObject;
Robert Phillipse19babf2020-04-06 13:57:30 -040022class GrRenderTargetProxy;
Greg Danield85f97d2017-03-07 13:37:21 -050023class GrSemaphore;
Robert Phillipse2f7d182016-12-15 09:23:05 -050024class GrSurfaceProxy;
25
Robert Phillips62000362018-02-01 09:10:04 -050026class SkDeferredDisplayList;
Robert Phillipsbc429442019-02-20 08:26:03 -050027class SkTaskGroup;
Robert Phillips62000362018-02-01 09:10:04 -050028
Adlai Hollera0693042020-10-14 11:23:11 -040029/** Class that adds methods to GrDirectContext that are only intended for use internal to Skia.
30 This class is purely a privileged window into GrDirectContext. It should never have additional
robertphillips4fd74ae2016-08-03 14:26:53 -070031 data members or virtual methods. */
Adlai Hollera0693042020-10-14 11:23:11 -040032class GrDirectContextPriv {
robertphillips4fd74ae2016-08-03 14:26:53 -070033public:
Robert Phillips4217ea72019-01-30 13:08:28 -050034
35 // from GrContext_Base
Robert Phillipsfd0d9702019-02-01 10:19:42 -050036 uint32_t contextID() const { return fContext->contextID(); }
Robert Phillips4217ea72019-01-30 13:08:28 -050037
Robert Phillipsfe0963c2019-02-07 13:25:07 -050038 bool matches(GrContext_Base* candidate) const { return fContext->matches(candidate); }
39
Robert Phillipsc1541ae2019-02-04 12:05:37 -050040 const GrContextOptions& options() const { return fContext->options(); }
41
Robert Phillipsbb606772019-02-04 17:50:57 -050042 const GrCaps* caps() const { return fContext->caps(); }
Robert Phillipsa41c6852019-02-07 10:44:10 -050043 sk_sp<const GrCaps> refCaps() const;
Robert Phillipsbb606772019-02-04 17:50:57 -050044
Robert Phillipsa41c6852019-02-07 10:44:10 -050045 GrImageContext* asImageContext() { return fContext->asImageContext(); }
46 GrRecordingContext* asRecordingContext() { return fContext->asRecordingContext(); }
Robert Phillipsa41c6852019-02-07 10:44:10 -050047
Adlai Holler17776e32021-02-18 13:09:36 -050048 // from GrRecordingContext
Robert Phillipsa41c6852019-02-07 10:44:10 -050049 GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
50 const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
51
52 /** This is only useful for debug purposes */
53 SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
Robert Phillips4217ea72019-01-30 13:08:28 -050054
55 // from GrRecordingContext
Robert Phillips6f0e02f2019-02-13 11:02:28 -050056 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
Michael Ludwig28b0c5d2019-12-19 14:51:00 -050057
Michael Ludwig2c316bd2019-12-19 14:50:44 -050058 SkArenaAlloc* recordTimeAllocator() { return fContext->arenas().recordTimeAllocator(); }
Michael Ludwig28b0c5d2019-12-19 14:51:00 -050059 GrRecordingContext::Arenas arenas() { return fContext->arenas(); }
Robert Phillipsd6841482019-02-08 10:29:20 -050060
Robert Phillips4d932d12020-04-09 08:58:52 -040061 GrStrikeCache* getGrStrikeCache() { return fContext->fStrikeCache.get(); }
Robert Phillips2184fb72019-02-21 16:11:41 -050062 GrTextBlobCache* getTextBlobCache() { return fContext->getTextBlobCache(); }
63
Robert Phillipsd464feb2020-10-08 11:00:02 -040064 GrThreadSafeCache* threadSafeCache() { return fContext->threadSafeCache(); }
Robert Phillips12d06a32020-09-16 12:31:34 -040065
Robert Phillipsc5058a62019-02-15 12:52:59 -050066 /**
67 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
68 *
69 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
70 * ensure its lifetime is tied to that of the context.
71 */
72 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
73
Robert Phillipsa92913e2021-07-12 16:31:52 -040074 GrAuditTrail* auditTrail() { return fContext->fAuditTrail.get(); }
Robert Phillips4217ea72019-01-30 13:08:28 -050075
Robert Phillipse42edcc2017-12-13 11:50:22 -050076 /**
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040077 * Finalizes all pending reads and writes to the surfaces and also performs an MSAA resolves
78 * if necessary. The GrSurfaceProxy array is treated as a hint. If it is supplied the context
79 * will guarantee that the draws required for those proxies are flushed but it could do more.
80 * If no array is provided then all current work will be flushed.
Robert Phillips7ee385e2017-03-30 08:02:11 -040081 *
82 * It is not necessary to call this before reading the render target via Skia/GrContext.
83 * GrContext will detect when it must perform a resolve before reading pixels back from the
84 * surface or using it as a texture.
85 */
Robert Phillips80bfda82020-11-12 09:23:36 -050086 GrSemaphoresSubmitted flushSurfaces(
87 SkSpan<GrSurfaceProxy*>,
88 SkSurface::BackendSurfaceAccess = SkSurface::BackendSurfaceAccess::kNoAccess,
89 const GrFlushInfo& = {},
90 const GrBackendSurfaceMutableState* newState = nullptr);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040091
Robert Phillips80bfda82020-11-12 09:23:36 -050092 /** Version of above that flushes for a single proxy. Null is allowed. */
93 GrSemaphoresSubmitted flushSurface(
94 GrSurfaceProxy* proxy,
95 SkSurface::BackendSurfaceAccess access = SkSurface::BackendSurfaceAccess::kNoAccess,
96 const GrFlushInfo& info = {},
97 const GrBackendSurfaceMutableState* newState = nullptr) {
98 size_t size = proxy ? 1 : 0;
99 return this->flushSurfaces({&proxy, size}, access, info, newState);
100 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400101
Greg Daniel6eb8c242019-06-05 10:22:24 -0400102 /**
103 * Returns true if createPMToUPMEffect and createUPMToPMEffect will succeed. In other words,
104 * did we find a pair of round-trip preserving conversion effects?
105 */
106 bool validPMUPMConversionExists();
Robert Phillipse78b7252017-04-06 07:59:41 -0400107
108 /**
Brian Osman4c886ee2021-07-07 13:34:50 -0400109 * These functions create premul <-> unpremul effects, using specialized round-trip effects.
Robert Phillipse78b7252017-04-06 07:59:41 -0400110 */
Greg Daniel6eb8c242019-06-05 10:22:24 -0400111 std::unique_ptr<GrFragmentProcessor> createPMToUPMEffect(std::unique_ptr<GrFragmentProcessor>);
112 std::unique_ptr<GrFragmentProcessor> createUPMToPMEffect(std::unique_ptr<GrFragmentProcessor>);
Robert Phillipse78b7252017-04-06 07:59:41 -0400113
Brian Osman51279982017-08-23 10:12:00 -0400114 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
115
Adlai Holler9555f292020-10-09 09:41:14 -0400116 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider.get(); }
117 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider.get(); }
Robert Phillips6be756b2018-01-16 15:07:54 -0500118
Adlai Holler9555f292020-10-09 09:41:14 -0400119 GrResourceCache* getResourceCache() { return fContext->fResourceCache.get(); }
Robert Phillips6be756b2018-01-16 15:07:54 -0500120
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500121 GrGpu* getGpu() { return fContext->fGpu.get(); }
122 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
123
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500124 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500125 GrAtlasManager* getAtlasManager() {
126 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500127 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500128
Robert Phillips5edf5102020-08-10 16:30:36 -0400129 // This accessor should only ever be called by the GrOpFlushState.
130 GrSmallPathAtlasMgr* getSmallPathAtlasMgr() {
131 return fContext->onGetSmallPathAtlasMgr();
132 }
133
Robert Phillipseb54bb52021-01-08 17:20:18 -0500134 void createDDLTask(sk_sp<const SkDeferredDisplayList>,
135 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -0500136 SkIPoint offset);
Robert Phillips62000362018-02-01 09:10:04 -0500137
Robert Phillips43e7e4f2020-05-06 13:34:45 -0400138 bool compile(const GrProgramDesc&, const GrProgramInfo&);
Robert Phillips979b2232020-02-20 10:47:29 -0500139
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500140 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400141 GrContextOptions::ShaderErrorHandler* getShaderErrorHandler() const {
142 return fContext->fShaderErrorHandler;
143 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500144
Brian Salomon9241a6d2019-10-03 13:26:54 -0400145 GrClientMappedBufferManager* clientMappedBufferManager() {
146 return fContext->fMappedBufferManager.get();
147 }
148
Robert Phillipscc44feb2021-07-06 12:21:37 -0400149 sk_sp<skgpu::BaseDevice> createDevice(GrColorType,
150 sk_sp<GrSurfaceProxy>,
151 sk_sp<SkColorSpace>,
152 GrSurfaceOrigin,
153 const SkSurfaceProps&,
154 skgpu::BaseDevice::InitContents);
155 sk_sp<skgpu::BaseDevice> createDevice(SkBudgeted,
156 const SkImageInfo&,
157 SkBackingFit,
158 int sampleCount,
159 GrMipmapped,
160 GrProtected,
161 GrSurfaceOrigin,
162 const SkSurfaceProps&,
163 skgpu::BaseDevice::InitContents);
Robert Phillips516405c2021-06-04 16:37:30 -0400164
Robert Phillips33bf2b52021-08-02 11:14:38 -0400165 std::unique_ptr<GrSurfaceContext> makeSC(GrSurfaceProxyView readView, const GrColorInfo&);
166
167 std::unique_ptr<GrSurfaceFillContext> makeSFC(GrImageInfo,
168 SkBackingFit = SkBackingFit::kExact,
169 int sampleCount = 1,
170 GrMipmapped = GrMipmapped::kNo,
171 GrProtected = GrProtected::kNo,
172 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
173 SkBudgeted = SkBudgeted::kYes);
174
175 std::unique_ptr<GrSurfaceFillContext> makeSFCWithFallback(
176 GrImageInfo,
177 SkBackingFit = SkBackingFit::kExact,
178 int sampleCount = 1,
179 GrMipmapped = GrMipmapped::kNo,
180 GrProtected = GrProtected::kNo,
181 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
182 SkBudgeted = SkBudgeted::kYes);
183
184 std::unique_ptr<GrSurfaceFillContext> makeSFCFromBackendTexture(
185 GrColorInfo,
186 const GrBackendTexture&,
187 int sampleCount,
188 GrSurfaceOrigin,
189 sk_sp<GrRefCntedCallback> releaseHelper);
190
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500191#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500192 /** Reset GPU stats */
Robert Phillips273f1072020-05-05 13:03:07 -0400193 void resetGpuStats() const;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500194
195 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
196 void dumpCacheStats(SkString*) const;
197 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
198 void printCacheStats() const;
199
200 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
201 void dumpGpuStats(SkString*) const;
202 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
203 void printGpuStats() const;
204
Robert Phillips273f1072020-05-05 13:03:07 -0400205 /** These are only active if GR_GPU_STATS == 1. */
206 void resetContextStats() const;
207 void dumpContextStats(SkString*) const;
208 void dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
209 void printContextStats() const;
210
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500211 /** Get pointer to atlas texture for given mask format. Note that this wraps an
212 actively mutating texture in an SkImage. This could yield unexpected results
213 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500214 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500215
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500216 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
217#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500218
robertphillips4fd74ae2016-08-03 14:26:53 -0700219private:
Adlai Hollera0693042020-10-14 11:23:11 -0400220 explicit GrDirectContextPriv(GrDirectContext* context) : fContext(context) {}
221 GrDirectContextPriv(const GrDirectContextPriv&) = delete;
222 GrDirectContextPriv& operator=(const GrDirectContextPriv&) = delete;
robertphillips4fd74ae2016-08-03 14:26:53 -0700223
224 // No taking addresses of this type.
Adlai Hollera0693042020-10-14 11:23:11 -0400225 const GrDirectContextPriv* operator&() const;
226 GrDirectContextPriv* operator&();
robertphillips4fd74ae2016-08-03 14:26:53 -0700227
Adlai Holler53cf44c2020-10-13 17:40:21 -0400228 GrDirectContext* fContext;
robertphillips4fd74ae2016-08-03 14:26:53 -0700229
Adlai Holler53cf44c2020-10-13 17:40:21 -0400230 friend class GrDirectContext; // to construct/copy this type.
robertphillips4fd74ae2016-08-03 14:26:53 -0700231};
232
Adlai Hollera0693042020-10-14 11:23:11 -0400233inline GrDirectContextPriv GrDirectContext::priv() { return GrDirectContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700234
Adlai Hollera0693042020-10-14 11:23:11 -0400235// NOLINTNEXTLINE(readability-const-return-type)
236inline const GrDirectContextPriv GrDirectContext::priv() const {
237 return GrDirectContextPriv(const_cast<GrDirectContext*>(this));
robertphillips4fd74ae2016-08-03 14:26:53 -0700238}
239
240#endif