reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkGraphics_DEFINED |
| 9 | #define SkGraphics_DEFINED |
| 10 | |
| 11 | #include "SkTypes.h" |
| 12 | |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 13 | class SkData; |
| 14 | class SkImageGenerator; |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame^] | 15 | class SkTraceMemoryDump; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 16 | |
reed@google.com | eb9a9bf | 2012-04-23 13:43:30 +0000 | [diff] [blame] | 17 | class SK_API SkGraphics { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | public: |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Call this at process initialization time if your environment does not |
mtklein | 8317a18 | 2015-07-30 07:30:16 -0700 | [diff] [blame] | 21 | * permit static global initializers that execute code. |
| 22 | * Init() is thread-safe and idempotent. |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 23 | */ |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 24 | static void Init(); |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 25 | |
caryclark@google.com | f86ab84 | 2011-12-16 17:11:17 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Call this to release any memory held privately, such as the font cache. |
| 28 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | static void Term(); |
| 30 | |
reed@google.com | 77407ca | 2011-11-08 13:48:32 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Return the version numbers for the library. If the parameter is not |
| 33 | * null, it is set to the version number. |
reed@android.com | 9aa8b32 | 2010-04-13 13:22:54 +0000 | [diff] [blame] | 34 | */ |
| 35 | static void GetVersion(int32_t* major, int32_t* minor, int32_t* patch); |
| 36 | |
reed@google.com | 77407ca | 2011-11-08 13:48:32 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Return the max number of bytes that should be used by the font cache. |
| 39 | * If the cache needs to allocate more, it will purge previous entries. |
| 40 | * This max can be changed by calling SetFontCacheLimit(). |
| 41 | */ |
| 42 | static size_t GetFontCacheLimit(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 43 | |
reed@google.com | 77407ca | 2011-11-08 13:48:32 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Specify the max number of bytes that should be used by the font cache. |
| 46 | * If the cache needs to allocate more, it will purge previous entries. |
| 47 | * |
| 48 | * This function returns the previous setting, as if GetFontCacheLimit() |
| 49 | * had be called before the new limit was set. |
| 50 | */ |
| 51 | static size_t SetFontCacheLimit(size_t bytes); |
| 52 | |
reed@google.com | 073c907 | 2011-11-08 20:03:48 +0000 | [diff] [blame] | 53 | /** |
reed@google.com | 79a1c34ee | 2012-07-30 13:08:01 +0000 | [diff] [blame] | 54 | * Return the number of bytes currently used by the font cache. |
| 55 | */ |
| 56 | static size_t GetFontCacheUsed(); |
| 57 | |
| 58 | /** |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 59 | * Return the number of entries in the font cache. |
| 60 | * A cache "entry" is associated with each typeface + pointSize + matrix. |
| 61 | */ |
| 62 | static int GetFontCacheCountUsed(); |
| 63 | |
| 64 | /** |
| 65 | * Return the current limit to the number of entries in the font cache. |
| 66 | * A cache "entry" is associated with each typeface + pointSize + matrix. |
| 67 | */ |
| 68 | static int GetFontCacheCountLimit(); |
| 69 | |
| 70 | /** |
| 71 | * Set the limit to the number of entries in the font cache, and return |
| 72 | * the previous value. If this new value is lower than the previous, |
| 73 | * it will automatically try to purge entries to meet the new limit. |
| 74 | */ |
| 75 | static int SetFontCacheCountLimit(int count); |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 76 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 77 | /** |
reed@google.com | 073c907 | 2011-11-08 20:03:48 +0000 | [diff] [blame] | 78 | * For debugging purposes, this will attempt to purge the font cache. It |
| 79 | * does not change the limit, but will cause subsequent font measures and |
| 80 | * draws to be recreated, since they will no longer be in the cache. |
| 81 | */ |
| 82 | static void PurgeFontCache(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 83 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 84 | /** |
reed | e7903c7 | 2015-03-16 10:26:13 -0700 | [diff] [blame] | 85 | * Scaling bitmaps with the kHigh_SkFilterQuality setting is |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 86 | * expensive, so the result is saved in the global Scaled Image |
| 87 | * Cache. |
| 88 | * |
| 89 | * This function returns the memory usage of the Scaled Image Cache. |
| 90 | */ |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 91 | static size_t GetResourceCacheTotalBytesUsed(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 92 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 93 | /** |
| 94 | * These functions get/set the memory usage limit for the resource cache, used for temporary |
| 95 | * bitmaps and other resources. Entries are purged from the cache when the memory useage |
| 96 | * exceeds this limit. |
| 97 | */ |
| 98 | static size_t GetResourceCacheTotalByteLimit(); |
| 99 | static size_t SetResourceCacheTotalByteLimit(size_t newLimit); |
| 100 | |
| 101 | /** |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 102 | * For debugging purposes, this will attempt to purge the resource cache. It |
| 103 | * does not change the limit. |
| 104 | */ |
| 105 | static void PurgeResourceCache(); |
| 106 | |
| 107 | /** |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 108 | * When the cachable entry is very lage (e.g. a large scaled bitmap), adding it to the cache |
| 109 | * can cause most/all of the existing entries to be purged. To avoid the, the client can set |
| 110 | * a limit for a single allocation. If a cacheable entry would have been cached, but its size |
| 111 | * exceeds this limit, then we do not attempt to cache it at all. |
| 112 | * |
| 113 | * Zero is the default value, meaning we always attempt to cache entries. |
| 114 | */ |
| 115 | static size_t GetResourceCacheSingleAllocationByteLimit(); |
| 116 | static size_t SetResourceCacheSingleAllocationByteLimit(size_t newLimit); |
| 117 | |
caryclark@google.com | 54c782c | 2011-11-21 20:42:14 +0000 | [diff] [blame] | 118 | /** |
ssid | 33c594c | 2015-08-27 09:23:54 -0700 | [diff] [blame^] | 119 | * Dumps memory usage of caches using the SkTraceMemoryDump interface. See SkTraceMemoryDump |
| 120 | * for usage of this method. |
| 121 | */ |
| 122 | static void DumpMemoryStatistics(SkTraceMemoryDump* dump); |
| 123 | |
| 124 | /** |
caryclark@google.com | 54c782c | 2011-11-21 20:42:14 +0000 | [diff] [blame] | 125 | * Applications with command line options may pass optional state, such |
| 126 | * as cache sizes, here, for instance: |
| 127 | * font-cache-limit=12345678 |
| 128 | * |
| 129 | * The flags format is name=value[;name=value...] with no spaces. |
| 130 | * This format is subject to change. |
| 131 | */ |
| 132 | static void SetFlags(const char* flags); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 133 | |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 134 | /** |
| 135 | * Return the max number of bytes that should be used by the thread-local |
| 136 | * font cache. |
| 137 | * If the cache needs to allocate more, it will purge previous entries. |
| 138 | * This max can be changed by calling SetFontCacheLimit(). |
| 139 | * |
| 140 | * If this thread has never called SetTLSFontCacheLimit, or has called it |
| 141 | * with 0, then this thread is using the shared font cache. In that case, |
| 142 | * this function will always return 0, and the caller may want to call |
| 143 | * GetFontCacheLimit. |
| 144 | */ |
| 145 | static size_t GetTLSFontCacheLimit(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 146 | |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 147 | /** |
| 148 | * Specify the max number of bytes that should be used by the thread-local |
reed@google.com | 803c67d | 2012-05-17 13:50:36 +0000 | [diff] [blame] | 149 | * font cache. If this value is 0, then this thread will use the shared |
| 150 | * global font cache. |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 151 | */ |
reed@google.com | 803c67d | 2012-05-17 13:50:36 +0000 | [diff] [blame] | 152 | static void SetTLSFontCacheLimit(size_t bytes); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 153 | |
| 154 | typedef SkImageGenerator* (*ImageGeneratorFromEncodedFactory)(SkData*); |
| 155 | |
| 156 | /** |
| 157 | * To instantiate images from encoded data, first looks at this runtime function-ptr. If it |
| 158 | * exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr |
| 159 | * or there is, but it returns NULL, then skia will call its internal default implementation. |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 160 | * |
| 161 | * Returns the previous factory (which could be NULL). |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 162 | */ |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 163 | static ImageGeneratorFromEncodedFactory |
| 164 | SetImageGeneratorFromEncodedFactory(ImageGeneratorFromEncodedFactory); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 167 | class SkAutoGraphics { |
| 168 | public: |
| 169 | SkAutoGraphics() { |
| 170 | SkGraphics::Init(); |
| 171 | } |
| 172 | ~SkAutoGraphics() { |
| 173 | SkGraphics::Term(); |
| 174 | } |
| 175 | }; |
| 176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | #endif |