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