blob: 1c97f0c2918d6c985ea21b40b7495a8767965760 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkGraphics_DEFINED
9#define SkGraphics_DEFINED
10
Mike Reed185130c2017-02-15 15:14:16 -050011#include "SkRefCnt.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
reed1c846342015-07-09 11:47:36 -070013class SkData;
14class SkImageGenerator;
ssid33c594c2015-08-27 09:23:54 -070015class SkTraceMemoryDump;
reed1c846342015-07-09 11:47:36 -070016
reed@google.comeb9a9bf2012-04-23 13:43:30 +000017class SK_API SkGraphics {
reed@android.com8a1c16f2008-12-17 15:59:43 +000018public:
caryclark@google.comd26147a2011-12-15 14:16:43 +000019 /**
20 * Call this at process initialization time if your environment does not
mtklein8317a182015-07-30 07:30:16 -070021 * permit static global initializers that execute code.
22 * Init() is thread-safe and idempotent.
caryclark@google.comd26147a2011-12-15 14:16:43 +000023 */
reed@android.com5e5adfd2009-03-07 03:39:23 +000024 static void Init();
caryclark@google.comd26147a2011-12-15 14:16:43 +000025
mtkleinfe81e2d2015-09-09 07:35:42 -070026 // We're in the middle of cleaning this up.
27 static void Term() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000028
reed@google.com77407ca2011-11-08 13:48:32 +000029 /**
30 * Return the version numbers for the library. If the parameter is not
31 * null, it is set to the version number.
reed@android.com9aa8b322010-04-13 13:22:54 +000032 */
33 static void GetVersion(int32_t* major, int32_t* minor, int32_t* patch);
34
reed@google.com77407ca2011-11-08 13:48:32 +000035 /**
36 * Return the max number of bytes that should be used by the font cache.
37 * If the cache needs to allocate more, it will purge previous entries.
38 * This max can be changed by calling SetFontCacheLimit().
39 */
40 static size_t GetFontCacheLimit();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000041
reed@google.com77407ca2011-11-08 13:48:32 +000042 /**
43 * Specify the max number of bytes that should be used by the font cache.
44 * If the cache needs to allocate more, it will purge previous entries.
45 *
46 * This function returns the previous setting, as if GetFontCacheLimit()
47 * had be called before the new limit was set.
48 */
49 static size_t SetFontCacheLimit(size_t bytes);
50
reed@google.com073c9072011-11-08 20:03:48 +000051 /**
reed@google.com79a1c34ee2012-07-30 13:08:01 +000052 * Return the number of bytes currently used by the font cache.
53 */
54 static size_t GetFontCacheUsed();
55
56 /**
reed@google.combaed71f2013-09-26 19:28:27 +000057 * Return the number of entries in the font cache.
58 * A cache "entry" is associated with each typeface + pointSize + matrix.
59 */
60 static int GetFontCacheCountUsed();
61
62 /**
63 * Return the current limit to the number of entries in the font cache.
64 * A cache "entry" is associated with each typeface + pointSize + matrix.
65 */
66 static int GetFontCacheCountLimit();
67
68 /**
69 * Set the limit to the number of entries in the font cache, and return
70 * the previous value. If this new value is lower than the previous,
71 * it will automatically try to purge entries to meet the new limit.
72 */
73 static int SetFontCacheCountLimit(int count);
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +000074
Mike Reedd5bee5d2017-06-01 14:45:44 -040075 /*
76 * Returns the maximum point size for text that may be cached.
77 *
78 * Sizes above this will be drawn directly from the font's outline.
79 * Setting this to a large value may speed up drawing larger text (repeatedly),
80 * but could cause the cache to purge other sizes more often.
81 *
82 * This value is a hint to the font engine, and the actual limit may be different due to
83 * implementation specific details.
84 */
85 static int GetFontCachePointSizeLimit();
86
87 /*
88 * Set the maximum point size for text that may be cached, returning the previous value.
89 *
90 * Sizes above this will be drawn directly from the font's outline.
91 * Setting this to a large value may speed up drawing larger text (repeatedly),
92 * but could cause the cache to purge other sizes more often.
93 *
94 * This value is a hint to the font engine, and the actual limit may be different due to
95 * implementation specific details.
96 */
97 static int SetFontCachePointSizeLimit(int maxPointSize);
98
reed@google.combaed71f2013-09-26 19:28:27 +000099 /**
reed@google.com073c9072011-11-08 20:03:48 +0000100 * For debugging purposes, this will attempt to purge the font cache. It
101 * does not change the limit, but will cause subsequent font measures and
102 * draws to be recreated, since they will no longer be in the cache.
103 */
104 static void PurgeFontCache();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105
halcanary805ef152014-07-17 06:58:01 -0700106 /**
reede7903c72015-03-16 10:26:13 -0700107 * Scaling bitmaps with the kHigh_SkFilterQuality setting is
halcanary805ef152014-07-17 06:58:01 -0700108 * expensive, so the result is saved in the global Scaled Image
109 * Cache.
110 *
111 * This function returns the memory usage of the Scaled Image Cache.
112 */
reed011f39a2014-08-28 13:35:23 -0700113 static size_t GetResourceCacheTotalBytesUsed();
halcanary805ef152014-07-17 06:58:01 -0700114
reed011f39a2014-08-28 13:35:23 -0700115 /**
116 * These functions get/set the memory usage limit for the resource cache, used for temporary
117 * bitmaps and other resources. Entries are purged from the cache when the memory useage
118 * exceeds this limit.
119 */
120 static size_t GetResourceCacheTotalByteLimit();
121 static size_t SetResourceCacheTotalByteLimit(size_t newLimit);
122
123 /**
reed56b00d92014-09-11 12:22:34 -0700124 * For debugging purposes, this will attempt to purge the resource cache. It
125 * does not change the limit.
126 */
127 static void PurgeResourceCache();
128
129 /**
reed011f39a2014-08-28 13:35:23 -0700130 * When the cachable entry is very lage (e.g. a large scaled bitmap), adding it to the cache
131 * can cause most/all of the existing entries to be purged. To avoid the, the client can set
132 * a limit for a single allocation. If a cacheable entry would have been cached, but its size
133 * exceeds this limit, then we do not attempt to cache it at all.
134 *
135 * Zero is the default value, meaning we always attempt to cache entries.
136 */
137 static size_t GetResourceCacheSingleAllocationByteLimit();
138 static size_t SetResourceCacheSingleAllocationByteLimit(size_t newLimit);
139
caryclark@google.com54c782c2011-11-21 20:42:14 +0000140 /**
ssid33c594c2015-08-27 09:23:54 -0700141 * Dumps memory usage of caches using the SkTraceMemoryDump interface. See SkTraceMemoryDump
142 * for usage of this method.
143 */
144 static void DumpMemoryStatistics(SkTraceMemoryDump* dump);
145
146 /**
reed1d564482016-02-22 06:19:54 -0800147 * Free as much globally cached memory as possible. This will purge all private caches in Skia,
148 * including font and image caches.
149 *
150 * If there are caches associated with GPU context, those will not be affected by this call.
151 */
152 static void PurgeAllCaches();
153
154 /**
caryclark@google.com54c782c2011-11-21 20:42:14 +0000155 * Applications with command line options may pass optional state, such
156 * as cache sizes, here, for instance:
157 * font-cache-limit=12345678
158 *
159 * The flags format is name=value[;name=value...] with no spaces.
160 * This format is subject to change.
161 */
162 static void SetFlags(const char* flags);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000163
reed@google.com6172d672012-05-17 13:38:03 +0000164 /**
165 * Return the max number of bytes that should be used by the thread-local
166 * font cache.
167 * If the cache needs to allocate more, it will purge previous entries.
168 * This max can be changed by calling SetFontCacheLimit().
169 *
170 * If this thread has never called SetTLSFontCacheLimit, or has called it
171 * with 0, then this thread is using the shared font cache. In that case,
172 * this function will always return 0, and the caller may want to call
173 * GetFontCacheLimit.
174 */
175 static size_t GetTLSFontCacheLimit();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000176
reed@google.com6172d672012-05-17 13:38:03 +0000177 /**
178 * Specify the max number of bytes that should be used by the thread-local
reed@google.com803c67d2012-05-17 13:50:36 +0000179 * font cache. If this value is 0, then this thread will use the shared
180 * global font cache.
reed@google.com6172d672012-05-17 13:38:03 +0000181 */
reed@google.com803c67d2012-05-17 13:50:36 +0000182 static void SetTLSFontCacheLimit(size_t bytes);
reed1c846342015-07-09 11:47:36 -0700183
Mike Reed185130c2017-02-15 15:14:16 -0500184 typedef std::unique_ptr<SkImageGenerator>
185 (*ImageGeneratorFromEncodedDataFactory)(sk_sp<SkData>);
reed1c846342015-07-09 11:47:36 -0700186
187 /**
188 * To instantiate images from encoded data, first looks at this runtime function-ptr. If it
189 * exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr
190 * or there is, but it returns NULL, then skia will call its internal default implementation.
reedd7c05bf2015-07-09 14:08:49 -0700191 *
192 * Returns the previous factory (which could be NULL).
reed1c846342015-07-09 11:47:36 -0700193 */
Mike Reed185130c2017-02-15 15:14:16 -0500194 static ImageGeneratorFromEncodedDataFactory
195 SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196};
197
reed@android.com8015dd82009-06-21 00:49:18 +0000198class SkAutoGraphics {
199public:
200 SkAutoGraphics() {
201 SkGraphics::Init();
202 }
reed@android.com8015dd82009-06-21 00:49:18 +0000203};
204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205#endif