Skia
2DGraphicsLibrary
 All Classes 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 "GrClip.h"
13 #include "GrColor.h"
14 #include "GrPaint.h"
15 #include "GrRenderTarget.h"
16 #include "GrTextureProvider.h"
17 #include "SkMatrix.h"
18 #include "SkPathEffect.h"
19 #include "SkTypes.h"
20 #include "../private/GrAuditTrail.h"
21 #include "../private/GrSingleOwner.h"
22 #include "../private/SkMutex.h"
23 
24 struct GrBatchAtlasConfig;
25 class GrBatchFontCache;
26 struct GrContextOptions;
27 class GrContextPriv;
29 class GrDrawingManager;
30 class GrDrawContext;
32 class GrGpu;
33 class GrIndexBuffer;
34 class GrOvalRenderer;
35 class GrPath;
36 class GrPipelineBuilder;
37 class GrResourceEntry;
38 class GrResourceCache;
39 class GrResourceProvider;
40 class GrTestTarget;
41 class GrTextBlobCache;
42 class GrTextContext;
43 class GrTextureParams;
44 class GrVertexBuffer;
45 class GrSwizzle;
46 class SkTraceMemoryDump;
47 
48 class SK_API GrContext : public SkRefCnt {
49 public:
53  static GrContext* Create(GrBackend, GrBackendContext, const GrContextOptions& options);
54  static GrContext* Create(GrBackend, GrBackendContext);
55 
59  static GrContext* CreateMockContext();
60 
61  virtual ~GrContext();
62 
63  GrContextThreadSafeProxy* threadSafeProxy();
64 
73  void resetContext(uint32_t state = kAll_GrBackendState);
74 
79  typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
80 
86  void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
87  CleanUpData* entry = fCleanUpData.push();
88 
89  entry->fFunc = cleanUp;
90  entry->fInfo = info;
91  }
92 
104  void abandonContext();
105 
115  void releaseResourcesAndAbandonContext();
116 
118  // Resource Cache
119 
128  void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
129 
138  void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
139 
149  void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
150 
151  GrTextureProvider* textureProvider() { return fTextureProvider; }
152  const GrTextureProvider* textureProvider() const { return fTextureProvider; }
153 
158  void freeGpuResources();
159 
165  void purgeAllUnlockedResources();
166 
168  const GrCaps* caps() const { return fCaps; }
169 
181  int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
182 
188  sk_sp<GrDrawContext> makeDrawContext(SkBackingFit fit,
189  int width, int height,
190  GrPixelConfig config,
191  sk_sp<SkColorSpace> colorSpace,
192  int sampleCnt = 0,
193  GrSurfaceOrigin origin = kDefault_GrSurfaceOrigin,
194  const SkSurfaceProps* surfaceProps = nullptr,
195  SkBudgeted = SkBudgeted::kYes);
196 
198  // Misc.
199 
203  enum FlushBits {
210  kDiscard_FlushBit = 0x2,
211  };
212 
219  void flush(int flagsBitfield = 0);
220 
221  void flushIfNecessary() {
222  if (fFlushToReduceCacheSize || this->caps()->immediateFlush()) {
223  this->flush();
224  }
225  }
226 
233  kDontFlush_PixelOpsFlag = 0x1,
236  kFlushWrites_PixelOp = 0x2,
239  kUnpremul_PixelOpsFlag = 0x4,
240  };
241 
258  bool readSurfacePixels(GrSurface* surface,
259  int left, int top, int width, int height,
260  GrPixelConfig config, void* buffer,
261  size_t rowBytes = 0,
262  uint32_t pixelOpsFlags = 0);
263 
279  bool writeSurfacePixels(GrSurface* surface,
280  int left, int top, int width, int height,
281  GrPixelConfig config, const void* buffer,
282  size_t rowBytes,
283  uint32_t pixelOpsFlags = 0);
284 
292  bool copySurface(GrSurface* dst,
293  GrSurface* src,
294  const SkIRect& srcRect,
295  const SkIPoint& dstPoint);
296 
299  bool copySurface(GrSurface* dst, GrSurface* src) {
300  return this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
301  SkIPoint::Make(0,0));
302  }
303 
307  void flushSurfaceWrites(GrSurface* surface);
308 
313  void flushSurfaceIO(GrSurface* surface);
314 
323  void prepareSurfaceForExternalIO(GrSurface*);
324 
328  uint32_t uniqueID() { return fUniqueID; }
329 
331  // Functions intended for internal use only.
332  GrGpu* getGpu() { return fGpu; }
333  const GrGpu* getGpu() const { return fGpu; }
334  GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; }
335  GrTextBlobCache* getTextBlobCache() { return fTextBlobCache; }
336  bool abandoned() const;
337  GrResourceProvider* resourceProvider() { return fResourceProvider; }
338  const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
339  GrResourceCache* getResourceCache() { return fResourceCache; }
340 
341  // Called by tests that draw directly to the context via GrDrawContext
342  void getTestTarget(GrTestTarget*, sk_sp<GrDrawContext>);
343 
345  void resetGpuStats() const ;
346 
348  void dumpCacheStats(SkString*) const;
349  void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
350  void printCacheStats() const;
351 
353  void dumpGpuStats(SkString*) const;
354  void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
355  void printGpuStats() const;
356 
359  void setTextBlobCacheLimit_ForTesting(size_t bytes);
360 
363  void setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs);
364 
366  void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
367 
369  GrTexture* getFontAtlasTexture(GrMaskFormat format);
370 
371  GrAuditTrail* getAuditTrail() { return &fAuditTrail; }
372 
374  SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } )
375 
376  // Provides access to functions that aren't part of the public API.
377  GrContextPriv contextPriv();
378  const GrContextPriv contextPriv() const;
379 
380 private:
381  GrGpu* fGpu;
382  const GrCaps* fCaps;
383  GrResourceCache* fResourceCache;
384  // this union exists because the inheritance of GrTextureProvider->GrResourceProvider
385  // is in a private header.
386  union {
387  GrResourceProvider* fResourceProvider;
388  GrTextureProvider* fTextureProvider;
389  };
390 
392 
393  GrBatchFontCache* fBatchFontCache;
394  SkAutoTDelete<GrTextBlobCache> fTextBlobCache;
395 
396  // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
397  bool fFlushToReduceCacheSize;
398  bool fDidTestPMConversions;
399  int fPMToUPMConversion;
400  int fUPMToPMConversion;
401  // The sw backend may call GrContext::readSurfacePixels on multiple threads
402  // We may transfer the responsibilty for using a mutex to the sw backend
403  // when there are fewer code paths that lead to a readSurfacePixels call
404  // from the sw backend. readSurfacePixels is reentrant in one case - when performing
405  // the PM conversions test. To handle this we do the PM conversions test outside
406  // of fReadPixelsMutex and use a separate mutex to guard it. When it re-enters
407  // readSurfacePixels it will grab fReadPixelsMutex and release it before the outer
408  // readSurfacePixels proceeds to grab it.
409  // TODO: Stop pretending to make GrContext thread-safe for sw rasterization and provide
410  // a mechanism to make a SkPicture safe for multithreaded sw rasterization.
411  SkMutex fReadPixelsMutex;
412  SkMutex fTestPMConversionsMutex;
413 
414  // In debug builds we guard against improper thread handling
415  // This guard is passed to the GrDrawingManager and, from there to all the
416  // GrDrawContexts. It is also passed to the GrTextureProvider and SkGpuDevice.
417  mutable GrSingleOwner fSingleOwner;
418 
419  struct CleanUpData {
420  PFCleanUpFunc fFunc;
421  void* fInfo;
422  };
423 
424  SkTDArray<CleanUpData> fCleanUpData;
425 
426  const uint32_t fUniqueID;
427 
428  SkAutoTDelete<GrDrawingManager> fDrawingManager;
429 
430  GrAuditTrail fAuditTrail;
431 
432  // TODO: have the GrClipStackClip use drawContexts and rm this friending
433  friend class GrClipStackClip; // for access to drawingManager
434  friend class GrDrawingManager; // for access to drawingManager for ProgramUnitTest
435  friend class GrContextPriv;
436 
437  GrDrawingManager* drawingManager() { return fDrawingManager; }
438 
439  GrContext(); // init must be called after the constructor.
440  bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
441 
442  void initMockContext();
443  void initCommon(const GrContextOptions&);
444 
450  sk_sp<GrFragmentProcessor> createPMToUPMEffect(GrTexture*, const GrSwizzle&,
451  const SkMatrix&) const;
452  sk_sp<GrFragmentProcessor> createUPMToPMEffect(GrTexture*, const GrSwizzle&,
453  const SkMatrix&) const;
457  void testPMConversionsIfNecessary(uint32_t flags);
460  bool didFailPMUPMConversionTest() const;
461 
466  static void OverBudgetCB(void* data);
467 
472  static void TextBlobCacheOverBudgetCB(void* data);
473 
474  typedef SkRefCnt INHERITED;
475 };
476 
482 private:
483  GrContextThreadSafeProxy(const GrCaps* caps, uint32_t uniqueID)
484  : fCaps(SkRef(caps))
485  , fContextUniqueID(uniqueID) {}
486 
488  uint32_t fContextUniqueID;
489 
490  friend class GrContext;
491  friend class SkImage;
492 
493  typedef SkRefCnt INHERITED;
494 };
495 
496 #endif
Definition: GrContextOptions.h:13
Provides custom fragment shader code.
Definition: GrFragmentProcessor.h:24
Represents the capabilities of a GrContext.
Definition: GrCaps.h:130
const GrCaps * caps() const
Access the context capabilities.
Definition: GrContext.h:168
Definition: GrTextureProvider.h:16
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:51
Represents the filtering and tile modes used to access a texture.
Definition: GrTextureParams.h:17
The SkMatrix class holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:26
Definition: SkRefCnt.h:135
uint32_t uniqueID()
An ID associated with this context, guaranteed to be unique.
Definition: GrContext.h:328
Definition: GrSurface.h:21
int width() const
Retrieves the width of the surface.
Definition: GrSurface.h:26
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
Definition: GrDrawContext.h:51
Definition: GrContext.h:48
int height() const
Retrieves the height of the surface.
Definition: GrSurface.h:31
void addCleanUp(PFCleanUpFunc cleanUp, void *info)
Add a function to be called from within GrContext's destructor.
Definition: GrContext.h:86
FlushBits
Flags that affect flush() behavior.
Definition: GrContext.h:203
Interface for memory tracing.
Definition: SkTraceMemoryDump.h:20
bool copySurface(GrSurface *dst, GrSurface *src)
Helper that copies the whole surface but fails when the two surfaces are not identically sized...
Definition: GrContext.h:299
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
Definition: GrTexture.h:19
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:45
SkIPoint holds two 32 bit integer coordinates.
Definition: SkPoint.h:40
Definition: GrContext.h:419
PixelOpsFlags
These flags can be used with the read/write pixels functions below.
Definition: GrContext.h:230
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:481