xref: /aosp_15_r20/external/skia/include/core/SkGraphics.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "include/private/base/SkAPI.h"
13 
14 #include <cstddef>
15 #include <cstdint>
16 #include <memory>
17 
18 class SkData;
19 class SkImageGenerator;
20 class SkOpenTypeSVGDecoder;
21 class SkTraceMemoryDump;
22 
23 class SK_API SkGraphics {
24 public:
25     /**
26      *  Call this at process initialization time if your environment does not
27      *  permit static global initializers that execute code.
28      *  Init() is thread-safe and idempotent.
29      */
30     static void Init();
31 
32     /**
33      *  Return the max number of bytes that should be used by the font cache.
34      *  If the cache needs to allocate more, it will purge previous entries.
35      *  This max can be changed by calling SetFontCacheLimit().
36      */
37     static size_t GetFontCacheLimit();
38 
39     /**
40      *  Specify the max number of bytes that should be used by the font cache.
41      *  If the cache needs to allocate more, it will purge previous entries.
42      *
43      *  This function returns the previous setting, as if GetFontCacheLimit()
44      *  had be called before the new limit was set.
45      */
46     static size_t SetFontCacheLimit(size_t bytes);
47 
48     /**
49      *  Return the number of bytes currently used by the font cache.
50      */
51     static size_t GetFontCacheUsed();
52 
53     /**
54      *  Return the number of entries in the font cache.
55      *  A cache "entry" is associated with each typeface + pointSize + matrix.
56      */
57     static int GetFontCacheCountUsed();
58 
59     /**
60      *  Return the current limit to the number of entries in the font cache.
61      *  A cache "entry" is associated with each typeface + pointSize + matrix.
62      */
63     static int GetFontCacheCountLimit();
64 
65     /**
66      *  Set the limit to the number of entries in the font cache, and return
67      *  the previous value. If this new value is lower than the previous,
68      *  it will automatically try to purge entries to meet the new limit.
69      */
70     static int SetFontCacheCountLimit(int count);
71 
72     /**
73      *  Return the current limit to the number of entries in the typeface cache.
74      *  A cache "entry" is associated with each typeface.
75      */
76     static int GetTypefaceCacheCountLimit();
77 
78     /**
79      *  Set the limit to the number of entries in the typeface cache, and return
80      *  the previous value. Changes to this only take effect the next time
81      *  each cache object is modified.
82      */
83     static int SetTypefaceCacheCountLimit(int count);
84 
85     /**
86      *  For debugging purposes, this will attempt to purge the font cache. It
87      *  does not change the limit, but will cause subsequent font measures and
88      *  draws to be recreated, since they will no longer be in the cache.
89      */
90     static void PurgeFontCache();
91 
92     /**
93      *  If the strike cache is above the cache limit, attempt to purge strikes
94      *  with pinners. This should be called after clients release locks on
95      *  pinned strikes.
96      */
97     static void PurgePinnedFontCache();
98 
99     /**
100      *  This function returns the memory used for temporary images and other resources.
101      */
102     static size_t GetResourceCacheTotalBytesUsed();
103 
104     /**
105      *  These functions get/set the memory usage limit for the resource cache, used for temporary
106      *  bitmaps and other resources. Entries are purged from the cache when the memory useage
107      *  exceeds this limit.
108      */
109     static size_t GetResourceCacheTotalByteLimit();
110     static size_t SetResourceCacheTotalByteLimit(size_t newLimit);
111 
112     /**
113      *  For debugging purposes, this will attempt to purge the resource cache. It
114      *  does not change the limit.
115      */
116     static void PurgeResourceCache();
117 
118     /**
119      *  When the cachable entry is very lage (e.g. a large scaled bitmap), adding it to the cache
120      *  can cause most/all of the existing entries to be purged. To avoid the, the client can set
121      *  a limit for a single allocation. If a cacheable entry would have been cached, but its size
122      *  exceeds this limit, then we do not attempt to cache it at all.
123      *
124      *  Zero is the default value, meaning we always attempt to cache entries.
125      */
126     static size_t GetResourceCacheSingleAllocationByteLimit();
127     static size_t SetResourceCacheSingleAllocationByteLimit(size_t newLimit);
128 
129     /**
130      *  Dumps memory usage of caches using the SkTraceMemoryDump interface. See SkTraceMemoryDump
131      *  for usage of this method.
132      */
133     static void DumpMemoryStatistics(SkTraceMemoryDump* dump);
134 
135     /**
136      *  Free as much globally cached memory as possible. This will purge all private caches in Skia,
137      *  including font and image caches.
138      *
139      *  If there are caches associated with GPU context, those will not be affected by this call.
140      */
141     static void PurgeAllCaches();
142 
143     typedef std::unique_ptr<SkImageGenerator>
144                                             (*ImageGeneratorFromEncodedDataFactory)(sk_sp<SkData>);
145 
146     /**
147      *  To instantiate images from encoded data, first looks at this runtime function-ptr. If it
148      *  exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr
149      *  or there is, but it returns NULL, then skia will call its internal default implementation.
150      *
151      *  Returns the previous factory (which could be NULL).
152      */
153     static ImageGeneratorFromEncodedDataFactory
154                     SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory);
155 
156     /**
157      *  To draw OpenType SVG data, Skia will look at this runtime function pointer. If this function
158      *  pointer is set, the SkTypeface implementations which support OpenType SVG will call this
159      *  function to create an SkOpenTypeSVGDecoder to decode the OpenType SVG and draw it as needed.
160      *  If this function is not set, the SkTypeface implementations will generally not support
161      *  OpenType SVG and attempt to use other glyph representations if available.
162      */
163     using OpenTypeSVGDecoderFactory =
164             std::unique_ptr<SkOpenTypeSVGDecoder> (*)(const uint8_t* svg, size_t length);
165     static OpenTypeSVGDecoderFactory SetOpenTypeSVGDecoderFactory(OpenTypeSVGDecoderFactory);
166     static OpenTypeSVGDecoderFactory GetOpenTypeSVGDecoderFactory();
167 };
168 
169 #endif
170