SkGraphics.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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. #ifndef SkGraphics_DEFINED
  8. #define SkGraphics_DEFINED
  9. #include "SkRefCnt.h"
  10. class SkData;
  11. class SkImageGenerator;
  12. class SkTraceMemoryDump;
  13. class SK_API SkGraphics {
  14. public:
  15. /**
  16. * Call this at process initialization time if your environment does not
  17. * permit static global initializers that execute code.
  18. * Init() is thread-safe and idempotent.
  19. */
  20. static void Init();
  21. // We're in the middle of cleaning this up.
  22. static void Term() {}
  23. /**
  24. * Return the version numbers for the library. If the parameter is not
  25. * null, it is set to the version number.
  26. */
  27. static void GetVersion(int32_t* major, int32_t* minor, int32_t* patch);
  28. /**
  29. * Return the max number of bytes that should be used by the font cache.
  30. * If the cache needs to allocate more, it will purge previous entries.
  31. * This max can be changed by calling SetFontCacheLimit().
  32. */
  33. static size_t GetFontCacheLimit();
  34. /**
  35. * Specify the max number of bytes that should be used by the font cache.
  36. * If the cache needs to allocate more, it will purge previous entries.
  37. *
  38. * This function returns the previous setting, as if GetFontCacheLimit()
  39. * had be called before the new limit was set.
  40. */
  41. static size_t SetFontCacheLimit(size_t bytes);
  42. /**
  43. * Return the number of bytes currently used by the font cache.
  44. */
  45. static size_t GetFontCacheUsed();
  46. /**
  47. * Return the number of entries in the font cache.
  48. * A cache "entry" is associated with each typeface + pointSize + matrix.
  49. */
  50. static int GetFontCacheCountUsed();
  51. /**
  52. * Return the current limit to the number of entries in the font cache.
  53. * A cache "entry" is associated with each typeface + pointSize + matrix.
  54. */
  55. static int GetFontCacheCountLimit();
  56. /**
  57. * Set the limit to the number of entries in the font cache, and return
  58. * the previous value. If this new value is lower than the previous,
  59. * it will automatically try to purge entries to meet the new limit.
  60. */
  61. static int SetFontCacheCountLimit(int count);
  62. /*
  63. * Returns the maximum point size for text that may be cached.
  64. *
  65. * Sizes above this will be drawn directly from the font's outline.
  66. * Setting this to a large value may speed up drawing larger text (repeatedly),
  67. * but could cause the cache to purge other sizes more often.
  68. *
  69. * This value is a hint to the font engine, and the actual limit may be different due to
  70. * implementation specific details.
  71. */
  72. static int GetFontCachePointSizeLimit();
  73. /*
  74. * Set the maximum point size for text that may be cached, returning the previous value.
  75. *
  76. * Sizes above this will be drawn directly from the font's outline.
  77. * Setting this to a large value may speed up drawing larger text (repeatedly),
  78. * but could cause the cache to purge other sizes more often.
  79. *
  80. * This value is a hint to the font engine, and the actual limit may be different due to
  81. * implementation specific details.
  82. */
  83. static int SetFontCachePointSizeLimit(int maxPointSize);
  84. /**
  85. * For debugging purposes, this will attempt to purge the font cache. It
  86. * does not change the limit, but will cause subsequent font measures and
  87. * draws to be recreated, since they will no longer be in the cache.
  88. */
  89. static void PurgeFontCache();
  90. /**
  91. * Scaling bitmaps with the kHigh_SkFilterQuality setting is
  92. * expensive, so the result is saved in the global Scaled Image
  93. * Cache.
  94. *
  95. * This function returns the memory usage of the Scaled Image Cache.
  96. */
  97. static size_t GetResourceCacheTotalBytesUsed();
  98. /**
  99. * These functions get/set the memory usage limit for the resource cache, used for temporary
  100. * bitmaps and other resources. Entries are purged from the cache when the memory useage
  101. * exceeds this limit.
  102. */
  103. static size_t GetResourceCacheTotalByteLimit();
  104. static size_t SetResourceCacheTotalByteLimit(size_t newLimit);
  105. /**
  106. * For debugging purposes, this will attempt to purge the resource cache. It
  107. * does not change the limit.
  108. */
  109. static void PurgeResourceCache();
  110. /**
  111. * When the cachable entry is very lage (e.g. a large scaled bitmap), adding it to the cache
  112. * can cause most/all of the existing entries to be purged. To avoid the, the client can set
  113. * a limit for a single allocation. If a cacheable entry would have been cached, but its size
  114. * exceeds this limit, then we do not attempt to cache it at all.
  115. *
  116. * Zero is the default value, meaning we always attempt to cache entries.
  117. */
  118. static size_t GetResourceCacheSingleAllocationByteLimit();
  119. static size_t SetResourceCacheSingleAllocationByteLimit(size_t newLimit);
  120. /**
  121. * Dumps memory usage of caches using the SkTraceMemoryDump interface. See SkTraceMemoryDump
  122. * for usage of this method.
  123. */
  124. static void DumpMemoryStatistics(SkTraceMemoryDump* dump);
  125. /**
  126. * Free as much globally cached memory as possible. This will purge all private caches in Skia,
  127. * including font and image caches.
  128. *
  129. * If there are caches associated with GPU context, those will not be affected by this call.
  130. */
  131. static void PurgeAllCaches();
  132. /**
  133. * Applications with command line options may pass optional state, such
  134. * as cache sizes, here, for instance:
  135. * font-cache-limit=12345678
  136. *
  137. * The flags format is name=value[;name=value...] with no spaces.
  138. * This format is subject to change.
  139. */
  140. static void SetFlags(const char* flags);
  141. typedef std::unique_ptr<SkImageGenerator>
  142. (*ImageGeneratorFromEncodedDataFactory)(sk_sp<SkData>);
  143. /**
  144. * To instantiate images from encoded data, first looks at this runtime function-ptr. If it
  145. * exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr
  146. * or there is, but it returns NULL, then skia will call its internal default implementation.
  147. *
  148. * Returns the previous factory (which could be NULL).
  149. */
  150. static ImageGeneratorFromEncodedDataFactory
  151. SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory);
  152. };
  153. class SkAutoGraphics {
  154. public:
  155. SkAutoGraphics() {
  156. SkGraphics::Init();
  157. }
  158. };
  159. #endif