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 "GrContext.h"
12 #include "GrSurfaceContext.h"
13 #include "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
explicitlyAllocateGPUResources()41 bool explicitlyAllocateGPUResources() const {
42 return fContext->explicitlyAllocateGPUResources();
43 }
44
caps()45 const GrCaps* caps() const { return fContext->caps(); }
46 sk_sp<const GrCaps> refCaps() const;
47
48 sk_sp<GrSkSLFPFactoryCache> fpFactoryCache();
49
asImageContext()50 GrImageContext* asImageContext() { return fContext->asImageContext(); }
asRecordingContext()51 GrRecordingContext* asRecordingContext() { return fContext->asRecordingContext(); }
asDirectContext()52 GrContext* asDirectContext() { return fContext->asDirectContext(); }
53
54 // from GrImageContext
proxyProvider()55 GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
proxyProvider()56 const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
57
abandoned()58 bool abandoned() const { return fContext->abandoned(); }
59
60 /** This is only useful for debug purposes */
SkDEBUGCODE(GrSingleOwner * singleOwner ()const{ return fContext->singleOwner(); } )61 SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
62
63 // from GrRecordingContext
64 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
65
66 sk_sp<GrOpMemoryPool> refOpMemoryPool();
opMemoryPool()67 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
68
getGrStrikeCache()69 GrStrikeCache* getGrStrikeCache() { return fContext->getGrStrikeCache(); }
getTextBlobCache()70 GrTextBlobCache* getTextBlobCache() { return fContext->getTextBlobCache(); }
71
72 /**
73 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
74 *
75 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
76 * ensure its lifetime is tied to that of the context.
77 */
78 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
79
80 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
81 sk_sp<SkColorSpace> = nullptr,
82 const SkSurfaceProps* = nullptr);
83
84 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
85 const GrSurfaceDesc&,
86 GrSurfaceOrigin,
87 GrMipMapped,
88 SkBackingFit,
89 SkBudgeted,
90 sk_sp<SkColorSpace> colorSpace = nullptr,
91 const SkSurfaceProps* = nullptr);
92
93 /*
94 * Create a new render target context backed by a deferred-style
95 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
96 * renderTargetContexts created via this entry point.
97 */
98 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
99 const GrBackendFormat& format,
100 SkBackingFit fit,
101 int width, int height,
102 GrPixelConfig config,
103 sk_sp<SkColorSpace> colorSpace,
104 int sampleCnt = 1,
105 GrMipMapped = GrMipMapped::kNo,
106 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
107 const SkSurfaceProps* surfaceProps = nullptr,
108 SkBudgeted = SkBudgeted::kYes);
109
110 /*
111 * This method will attempt to create a renderTargetContext that has, at least, the number of
112 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
113 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
114 * SRGB-ness will be preserved.
115 */
116 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
117 const GrBackendFormat& format,
118 SkBackingFit fit,
119 int width, int height,
120 GrPixelConfig config,
121 sk_sp<SkColorSpace> colorSpace,
122 int sampleCnt = 1,
123 GrMipMapped = GrMipMapped::kNo,
124 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
125 const SkSurfaceProps* surfaceProps = nullptr,
126 SkBudgeted budgeted = SkBudgeted::kYes);
127
auditTrail()128 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
129
130 /**
131 * Create a GrContext without a resource cache
132 */
133 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
134
135 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
136 GrSurfaceOrigin origin,
137 sk_sp<SkColorSpace> colorSpace);
138
139 // These match the definitions in SkSurface & GrSurface.h, for whence they came
140 typedef void* ReleaseContext;
141 typedef void (*ReleaseProc)(ReleaseContext);
142
143 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
144 const GrBackendTexture& tex,
145 GrSurfaceOrigin origin,
146 int sampleCnt,
147 sk_sp<SkColorSpace> colorSpace,
148 const SkSurfaceProps* = nullptr,
149 ReleaseProc = nullptr,
150 ReleaseContext = nullptr);
151
152 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
153 const GrBackendRenderTarget&,
154 GrSurfaceOrigin origin,
155 sk_sp<SkColorSpace> colorSpace,
156 const SkSurfaceProps* = nullptr,
157 ReleaseProc = nullptr,
158 ReleaseContext = nullptr);
159
160 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
161 const GrBackendTexture& tex,
162 GrSurfaceOrigin origin,
163 int sampleCnt,
164 sk_sp<SkColorSpace> colorSpace,
165 const SkSurfaceProps* = nullptr);
166
167 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
168 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
169
170 /**
171 * Call to ensure all drawing to the context has been issued to the
172 * underlying 3D API.
173 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
174 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
175 * provided then all current work will be flushed.
176 */
177 void flush(GrSurfaceProxy*);
178
179 /**
180 * After this returns any pending writes to the surface will have been issued to the
181 * backend 3D API.
182 */
183 void flushSurfaceWrites(GrSurfaceProxy*);
184
185 /**
186 * After this returns any pending reads or writes to the surface will have been issued to the
187 * backend 3D API.
188 */
189 void flushSurfaceIO(GrSurfaceProxy*);
190
191 /**
192 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
193 * if necessary.
194 *
195 * It is not necessary to call this before reading the render target via Skia/GrContext.
196 * GrContext will detect when it must perform a resolve before reading pixels back from the
197 * surface or using it as a texture.
198 */
199 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
200
201 /**
202 * These flags can be used with the read/write pixels functions below.
203 */
204 enum PixelOpsFlags {
205 /** The GrContext will not be flushed before the surface read or write. This means that
206 the read or write may occur before previous draws have executed. */
207 kDontFlush_PixelOpsFlag = 0x1,
208 /** Any surface writes should be flushed to the backend 3D API after the surface operation
209 is complete */
210 kFlushWrites_PixelOp = 0x2,
211 /** The src for write or dst read is unpremultiplied. This is only respected if both the
212 config src and dst configs are an RGBA/BGRA 8888 format. */
213 kUnpremul_PixelOpsFlag = 0x4,
214 };
215
216 /**
217 * Reads a rectangle of pixels from a surface.
218 *
219 * @param src the surface context to read from.
220 * @param left left edge of the rectangle to read (inclusive)
221 * @param top top edge of the rectangle to read (inclusive)
222 * @param width width of rectangle to read in pixels.
223 * @param height height of rectangle to read in pixels.
224 * @param dstColorType the color type of the destination buffer
225 * @param dstColorSpace color space of the destination buffer
226 * @param buffer memory to read the rectangle into.
227 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
228 * packed.
229 * @param pixelOpsFlags see PixelOpsFlags enum above.
230 *
231 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
232 * pixel configs
233 */
234 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
235 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
236 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
237
238 /**
239 * Writes a rectangle of pixels to a surface.
240 *
241 * @param dst the surface context to write to.
242 * @param left left edge of the rectangle to write (inclusive)
243 * @param top top edge of the rectangle to write (inclusive)
244 * @param width width of rectangle to write in pixels.
245 * @param height height of rectangle to write in pixels.
246 * @param srcColorType the color type of the source buffer
247 * @param srcColorSpace color space of the source buffer
248 * @param buffer memory to read pixels from
249 * @param rowBytes number of bytes between consecutive rows. Zero
250 * means rows are tightly packed.
251 * @param pixelOpsFlags see PixelOpsFlags enum above.
252 * @return true if the write succeeded, false if not. The write can fail because of an
253 * unsupported combination of surface and src configs.
254 */
255 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
256 GrColorType srcColorType, SkColorSpace* srcColorSpace,
257 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
258
getTaskGroup()259 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
260
resourceProvider()261 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
resourceProvider()262 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
263
getResourceCache()264 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
265
getGpu()266 GrGpu* getGpu() { return fContext->fGpu.get(); }
getGpu()267 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
268
269 // This accessor should only ever be called by the GrOpFlushState.
getAtlasManager()270 GrAtlasManager* getAtlasManager() {
271 return fContext->onGetAtlasManager();
272 }
273
274 void moveOpListsToDDL(SkDeferredDisplayList*);
275 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
276
getPersistentCache()277 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
278
279 #ifdef SK_ENABLE_DUMP_GPU
280 /** Returns a string with detailed information about the context & GPU, in JSON format. */
281 SkString dump() const;
282 #endif
283
284 #if GR_TEST_UTILS
285 /** Reset GPU stats */
286 void resetGpuStats() const ;
287
288 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
289 void dumpCacheStats(SkString*) const;
290 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
291 void printCacheStats() const;
292
293 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
294 void dumpGpuStats(SkString*) const;
295 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
296 void printGpuStats() const;
297
298 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
299 this is for testing only */
300 void testingOnly_setTextBlobCacheLimit(size_t bytes);
301
302 /** Get pointer to atlas texture for given mask format. Note that this wraps an
303 actively mutating texture in an SkImage. This could yield unexpected results
304 if it gets cached or used more generally. */
305 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
306
307 /**
308 * Purge all the unlocked resources from the cache.
309 * This entry point is mainly meant for timing texture uploads
310 * and is not defined in normal builds of Skia.
311 */
312 void testingOnly_purgeAllUnlockedResources();
313
314 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
315 #endif
316
317 private:
GrContextPriv(GrContext * context)318 explicit GrContextPriv(GrContext* context) : fContext(context) {}
319 GrContextPriv(const GrContextPriv&); // unimpl
320 GrContextPriv& operator=(const GrContextPriv&); // unimpl
321
322 // No taking addresses of this type.
323 const GrContextPriv* operator&() const;
324 GrContextPriv* operator&();
325
326 GrContext* fContext;
327
328 friend class GrContext; // to construct/copy this type.
329 };
330
priv()331 inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
332
priv()333 inline const GrContextPriv GrContext::priv() const {
334 return GrContextPriv(const_cast<GrContext*>(this));
335 }
336
337 #endif
338