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