Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrContext.h
1 /*
2  * Copyright 2010 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 GrContext_DEFINED
9 #define GrContext_DEFINED
10 
11 #include "GrCaps.h"
12 #include "GrColor.h"
13 #include "SkMatrix.h"
14 #include "SkPathEffect.h"
15 #include "SkTypes.h"
16 #include "../private/GrAuditTrail.h"
17 #include "../private/GrSingleOwner.h"
18 
19 class GrAtlasGlyphCache;
20 struct GrContextOptions;
21 class GrContextPriv;
23 class GrDrawingManager;
24 struct GrDrawOpAtlasConfig;
25 class GrRenderTargetContext;
26 class GrFragmentProcessor;
27 class GrGpu;
28 class GrIndexBuffer;
29 class GrOvalRenderer;
30 class GrPath;
31 class GrPipelineBuilder;
32 class GrResourceEntry;
33 class GrResourceCache;
34 class GrResourceProvider;
35 class GrSamplerParams;
36 class GrSurfaceProxy;
37 class GrTextBlobCache;
38 class GrTextContext;
39 class GrTextureProxy;
40 class GrVertexBuffer;
41 class GrSwizzle;
42 class SkTraceMemoryDump;
43 
44 class SkImage;
45 class SkSurfaceProps;
46 
47 class SK_API GrContext : public SkRefCnt {
48 public:
52  static GrContext* Create(GrBackend, GrBackendContext, const GrContextOptions& options);
53  static GrContext* Create(GrBackend, GrBackendContext);
54 
55  virtual ~GrContext();
56 
57  sk_sp<GrContextThreadSafeProxy> threadSafeProxy();
58 
67  void resetContext(uint32_t state = kAll_GrBackendState);
68 
73  typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
74 
80  void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
81  CleanUpData* entry = fCleanUpData.push();
82 
83  entry->fFunc = cleanUp;
84  entry->fInfo = info;
85  }
86 
98  void abandonContext();
99 
109  void releaseResourcesAndAbandonContext();
110 
112  // Resource Cache
113 
122  void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
123 
132  void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
133 
137  size_t getResourceCachePurgeableBytes() const;
138 
148  void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
149 
154  void freeGpuResources();
155 
161  void purgeAllUnlockedResources();
162 
167  void purgeResourcesNotUsedInMs(std::chrono::milliseconds ms);
168 
179  void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
180 
182  const GrCaps* caps() const { return fCaps; }
183 
195  int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
196 
197  /*
198  * Create a new render target context backed by a deferred-style
199  * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
200  * renderTargetContexts created via this entry point.
201  */
202  sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
203  SkBackingFit fit,
204  int width, int height,
205  GrPixelConfig config,
206  sk_sp<SkColorSpace> colorSpace,
207  int sampleCnt = 0,
208  GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
209  const SkSurfaceProps* surfaceProps = nullptr,
210  SkBudgeted = SkBudgeted::kYes);
211  /*
212  * This method will attempt to create a renderTargetContext that has, at least, the number of
213  * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
214  * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
215  * SRGB-ness will be preserved.
216  */
217  sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
218  SkBackingFit fit,
219  int width, int height,
220  GrPixelConfig config,
221  sk_sp<SkColorSpace> colorSpace,
222  int sampleCnt = 0,
223  GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
224  const SkSurfaceProps* surfaceProps = nullptr,
225  SkBudgeted budgeted = SkBudgeted::kYes);
226 
228  // Misc.
229 
234  void flush();
235 
239  uint32_t uniqueID() { return fUniqueID; }
240 
242  // Functions intended for internal use only.
243  GrGpu* getGpu() { return fGpu; }
244  const GrGpu* getGpu() const { return fGpu; }
245  GrAtlasGlyphCache* getAtlasGlyphCache() { return fAtlasGlyphCache; }
246  GrTextBlobCache* getTextBlobCache() { return fTextBlobCache.get(); }
247  bool abandoned() const;
248  GrResourceProvider* resourceProvider() { return fResourceProvider; }
249  const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
250  GrResourceCache* getResourceCache() { return fResourceCache; }
251 
253  void resetGpuStats() const ;
254 
256  void dumpCacheStats(SkString*) const;
257  void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
258  void printCacheStats() const;
259 
261  void dumpGpuStats(SkString*) const;
262  void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
263  void printGpuStats() const;
264 
267  void setTextBlobCacheLimit_ForTesting(size_t bytes);
268 
271  void setTextContextAtlasSizes_ForTesting(const GrDrawOpAtlasConfig* configs);
272 
274  void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
275 
279  sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format);
280 
281  GrAuditTrail* getAuditTrail() { return &fAuditTrail; }
282 
284  SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } )
285 
286  // Provides access to functions that aren't part of the public API.
287  GrContextPriv contextPriv();
288  const GrContextPriv contextPriv() const;
289 
290 private:
291  GrGpu* fGpu;
292  const GrCaps* fCaps;
293  GrResourceCache* fResourceCache;
294  GrResourceProvider* fResourceProvider;
295 
296  sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
297 
298  GrAtlasGlyphCache* fAtlasGlyphCache;
299  std::unique_ptr<GrTextBlobCache> fTextBlobCache;
300 
301  bool fDisableGpuYUVConversion;
302  bool fDidTestPMConversions;
303  // true if the PM/UPM conversion succeeded; false otherwise
304  bool fPMUPMConversionsRoundTrip;
305 
306  // In debug builds we guard against improper thread handling
307  // This guard is passed to the GrDrawingManager and, from there to all the
308  // GrRenderTargetContexts. It is also passed to the GrResourceProvider and SkGpuDevice.
309  mutable GrSingleOwner fSingleOwner;
310 
311  struct CleanUpData {
312  PFCleanUpFunc fFunc;
313  void* fInfo;
314  };
315 
316  SkTDArray<CleanUpData> fCleanUpData;
317 
318  const uint32_t fUniqueID;
319 
320  std::unique_ptr<GrDrawingManager> fDrawingManager;
321 
322  GrAuditTrail fAuditTrail;
323 
324  GrBackend fBackend;
325 
326  // TODO: have the GrClipStackClip use renderTargetContexts and rm this friending
327  friend class GrContextPriv;
328 
329  GrContext(); // init must be called after the constructor.
330  bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
331 
338  bool useConfigConversionEffect);
340  bool useConfigConversionEffect);
341 
346  bool validPMUPMConversionExists();
347 
352  static void TextBlobCacheOverBudgetCB(void* data);
353 
354  typedef SkRefCnt INHERITED;
355 };
356 
362 private:
363  GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID)
364  : fCaps(std::move(caps))
365  , fContextUniqueID(uniqueID) {}
366 
367  sk_sp<const GrCaps> fCaps;
368  uint32_t fContextUniqueID;
369 
370  friend class GrContext;
371  friend class SkImage;
372 
373  typedef SkRefCnt INHERITED;
374 };
375 
376 #endif
Definition: GrContextOptions.h:14
Represents the capabilities of a GrContext.
Definition: GrCaps.h:24
const GrCaps * caps() const
Access the context capabilities.
Definition: GrContext.h:182
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:51
Represents the filtering and tile modes used to access a texture.
Definition: GrSamplerParams.h:17
Definition: SkRefCnt.h:125
uint32_t uniqueID()
An ID associated with this context, guaranteed to be unique.
Definition: GrContext.h:239
Definition: GrContext.h:47
void addCleanUp(PFCleanUpFunc cleanUp, void *info)
Add a function to be called from within GrContext's destructor.
Definition: GrContext.h:80
Interface for memory tracing.
Definition: SkTraceMemoryDump.h:20
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:48
Definition: GrContext.h:311
Light weight class for managing strings.
Definition: SkString.h:121
Can be used to perform actions related to the generating GrContext in a thread safe manner...
Definition: GrContext.h:361