1 /*
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
8 #ifndef GrContextPriv_DEFINED
9 #define GrContextPriv_DEFINED
10
11 #include "include/gpu/GrContext.h"
12 #include "src/gpu/GrSurfaceContext.h"
13 #include "src/gpu/text/GrAtlasManager.h"
14
15 class GrBackendFormat;
16 class GrBackendRenderTarget;
17 class GrOpMemoryPool;
18 class GrOnFlushCallbackObject;
19 class GrSemaphore;
20 class GrSkSLFPFactory;
21 class GrSkSLFPFactoryCache;
22 class GrSurfaceProxy;
23 class GrTextureContext;
24
25 class SkDeferredDisplayList;
26 class SkTaskGroup;
27
28 /** Class that adds methods to GrContext that are only intended for use internal to Skia.
29 This class is purely a privileged window into GrContext. It should never have additional
30 data members or virtual methods. */
31 class GrContextPriv {
32 public:
33
34 // from GrContext_Base
contextID()35 uint32_t contextID() const { return fContext->contextID(); }
36
matches(GrContext_Base * candidate)37 bool matches(GrContext_Base* candidate) const { return fContext->matches(candidate); }
38
options()39 const GrContextOptions& options() const { return fContext->options(); }
40
caps()41 const GrCaps* caps() const { return fContext->caps(); }
42 sk_sp<const GrCaps> refCaps() const;
43
44 sk_sp<GrSkSLFPFactoryCache> fpFactoryCache();
45
asImageContext()46 GrImageContext* asImageContext() { return fContext->asImageContext(); }
asRecordingContext()47 GrRecordingContext* asRecordingContext() { return fContext->asRecordingContext(); }
asDirectContext()48 GrContext* asDirectContext() { return fContext->asDirectContext(); }
49
50 // from GrImageContext
proxyProvider()51 GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
proxyProvider()52 const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
53
abandoned()54 bool abandoned() const { return fContext->abandoned(); }
55
56 /** This is only useful for debug purposes */
SkDEBUGCODE(GrSingleOwner * singleOwner ()const{ return fContext->singleOwner(); } )57 SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
58
59 // from GrRecordingContext
60 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
61
62 sk_sp<GrOpMemoryPool> refOpMemoryPool();
opMemoryPool()63 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
64
getGrStrikeCache()65 GrStrikeCache* getGrStrikeCache() { return fContext->getGrStrikeCache(); }
getTextBlobCache()66 GrTextBlobCache* getTextBlobCache() { return fContext->getTextBlobCache(); }
67
68 /**
69 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
70 *
71 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
72 * ensure its lifetime is tied to that of the context.
73 */
74 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
75
76 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
77 GrColorType,
78 SkAlphaType,
79 sk_sp<SkColorSpace> = nullptr,
80 const SkSurfaceProps* = nullptr);
81
82 /** Create a new texture context backed by a deferred-style GrTextureProxy. */
83 sk_sp<GrTextureContext> makeDeferredTextureContext(
84 SkBackingFit,
85 int width,
86 int height,
87 GrColorType,
88 SkAlphaType,
89 sk_sp<SkColorSpace>,
90 GrMipMapped = GrMipMapped::kNo,
91 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
92 SkBudgeted = SkBudgeted::kYes,
93 GrProtected = GrProtected::kNo);
94
95 /*
96 * Create a new render target context backed by a deferred-style
97 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
98 * renderTargetContexts created via this entry point.
99 */
100 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
101 SkBackingFit fit,
102 int width,
103 int height,
104 GrColorType,
105 sk_sp<SkColorSpace> colorSpace,
106 int sampleCnt = 1,
107 GrMipMapped = GrMipMapped::kNo,
108 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
109 const SkSurfaceProps* surfaceProps = nullptr,
110 SkBudgeted = SkBudgeted::kYes,
111 GrProtected isProtected = GrProtected::kNo);
112
113 /*
114 * This method will attempt to create a renderTargetContext that has, at least, the number of
115 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
116 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
117 * SRGB-ness will be preserved.
118 */
119 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
120 SkBackingFit fit,
121 int width,
122 int height,
123 GrColorType,
124 sk_sp<SkColorSpace> colorSpace,
125 int sampleCnt = 1,
126 GrMipMapped = GrMipMapped::kNo,
127 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
128 const SkSurfaceProps* surfaceProps = nullptr,
129 SkBudgeted budgeted = SkBudgeted::kYes);
130
auditTrail()131 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
132
133 /**
134 * Create a GrContext without a resource cache
135 */
136 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
137
138 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture&,
139 GrSurfaceOrigin,
140 GrColorType,
141 SkAlphaType,
142 sk_sp<SkColorSpace>);
143
144 // These match the definitions in SkSurface & GrSurface.h, for whence they came
145 typedef void* ReleaseContext;
146 typedef void (*ReleaseProc)(ReleaseContext);
147
148 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
149 const GrBackendTexture& tex,
150 GrSurfaceOrigin origin,
151 int sampleCnt,
152 GrColorType,
153 sk_sp<SkColorSpace> colorSpace,
154 const SkSurfaceProps* = nullptr,
155 ReleaseProc = nullptr,
156 ReleaseContext = nullptr);
157
158 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
159 const GrBackendRenderTarget&,
160 GrSurfaceOrigin origin,
161 GrColorType,
162 sk_sp<SkColorSpace> colorSpace,
163 const SkSurfaceProps* = nullptr,
164 ReleaseProc = nullptr,
165 ReleaseContext = nullptr);
166
167 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
168 const GrBackendTexture& tex,
169 GrSurfaceOrigin origin,
170 int sampleCnt,
171 GrColorType,
172 sk_sp<SkColorSpace> colorSpace,
173 const SkSurfaceProps* = nullptr);
174
175 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
176 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
177
178 /**
179 * Finalizes all pending reads and writes to the surfaces and also performs an MSAA resolves
180 * if necessary. The GrSurfaceProxy array is treated as a hint. If it is supplied the context
181 * will guarantee that the draws required for those proxies are flushed but it could do more.
182 * If no array is provided then all current work will be flushed.
183 *
184 * It is not necessary to call this before reading the render target via Skia/GrContext.
185 * GrContext will detect when it must perform a resolve before reading pixels back from the
186 * surface or using it as a texture.
187 */
188 GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy*[], int numProxies, const GrFlushInfo&);
189
190 /** Version of above that flushes for a single proxy and uses a default GrFlushInfo. Null is
191 * allowed. */
192 void flushSurface(GrSurfaceProxy*);
193
194 /**
195 * Returns true if createPMToUPMEffect and createUPMToPMEffect will succeed. In other words,
196 * did we find a pair of round-trip preserving conversion effects?
197 */
198 bool validPMUPMConversionExists();
199
200 /**
201 * These functions create premul <-> unpremul effects, using the specialized round-trip effects
202 * from GrConfigConversionEffect.
203 */
204 std::unique_ptr<GrFragmentProcessor> createPMToUPMEffect(std::unique_ptr<GrFragmentProcessor>);
205 std::unique_ptr<GrFragmentProcessor> createUPMToPMEffect(std::unique_ptr<GrFragmentProcessor>);
206
getTaskGroup()207 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
208
resourceProvider()209 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
resourceProvider()210 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
211
getResourceCache()212 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
213
getGpu()214 GrGpu* getGpu() { return fContext->fGpu.get(); }
getGpu()215 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
216
217 // This accessor should only ever be called by the GrOpFlushState.
getAtlasManager()218 GrAtlasManager* getAtlasManager() {
219 return fContext->onGetAtlasManager();
220 }
221
222 void moveRenderTasksToDDL(SkDeferredDisplayList*);
223 void copyRenderTasksFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
224
getPersistentCache()225 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
getShaderErrorHandler()226 GrContextOptions::ShaderErrorHandler* getShaderErrorHandler() const {
227 return fContext->fShaderErrorHandler;
228 }
229
230 #if GR_TEST_UTILS
231 /** Reset GPU stats */
232 void resetGpuStats() const ;
233
234 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
235 void dumpCacheStats(SkString*) const;
236 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
237 void printCacheStats() const;
238
239 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
240 void dumpGpuStats(SkString*) const;
241 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
242 void printGpuStats() const;
243
244 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
245 this is for testing only */
246 void testingOnly_setTextBlobCacheLimit(size_t bytes);
247
248 /** Get pointer to atlas texture for given mask format. Note that this wraps an
249 actively mutating texture in an SkImage. This could yield unexpected results
250 if it gets cached or used more generally. */
251 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
252
253 /**
254 * Purge all the unlocked resources from the cache.
255 * This entry point is mainly meant for timing texture uploads
256 * and is not defined in normal builds of Skia.
257 */
258 void testingOnly_purgeAllUnlockedResources();
259
260 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
261 #endif
262
263 // If possible, create a backend texture initialized with the provided pixmap data. The client
264 // should ensure that the returned backend texture is valid.
265 // If successful, the created backend texture will be compatible with the provided
266 // pixmap(s).
267 // If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
268 // the data for all the mipmap levels must be provided.
269 GrBackendTexture createBackendTexture(const SkPixmap srcData[], int numLevels,
270 GrRenderable, GrProtected);
271
272 private:
GrContextPriv(GrContext * context)273 explicit GrContextPriv(GrContext* context) : fContext(context) {}
274 GrContextPriv(const GrContextPriv&); // unimpl
275 GrContextPriv& operator=(const GrContextPriv&); // unimpl
276
277 // No taking addresses of this type.
278 const GrContextPriv* operator&() const;
279 GrContextPriv* operator&();
280
281 GrContext* fContext;
282
283 friend class GrContext; // to construct/copy this type.
284 };
285
priv()286 inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
287
priv()288 inline const GrContextPriv GrContext::priv() const {
289 return GrContextPriv(const_cast<GrContext*>(this));
290 }
291
292 #endif
293