• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
27      *  Return the max number of bytes that should be used by the font cache.
28      *  If the cache needs to allocate more, it will purge previous entries.
29      *  This max can be changed by calling SetFontCacheLimit().
30      */
31     static size_t GetFontCacheLimit();
32 
33     /**
34      *  Specify the max number of bytes that should be used by the font cache.
35      *  If the cache needs to allocate more, it will purge previous entries.
36      *
37      *  This function returns the previous setting, as if GetFontCacheLimit()
38      *  had be called before the new limit was set.
39      */
40     static size_t SetFontCacheLimit(size_t bytes);
41 
42     /**
43      *  Return the number of bytes currently used by the font cache.
44      */
45     static size_t GetFontCacheUsed();
46 
47     /**
48      *  Return the number of entries in the font cache.
49      *  A cache "entry" is associated with each typeface + pointSize + matrix.
50      */
51     static int GetFontCacheCountUsed();
52 
53     /**
54      *  Return the current limit to the number of entries in the font cache.
55      *  A cache "entry" is associated with each typeface + pointSize + matrix.
56      */
57     static int GetFontCacheCountLimit();
58 
59     /**
60      *  Set the limit to the number of entries in the font cache, and return
61      *  the previous value. If this new value is lower than the previous,
62      *  it will automatically try to purge entries to meet the new limit.
63      */
64     static int SetFontCacheCountLimit(int count);
65 
66     /**
67      *  For debugging purposes, this will attempt to purge the font cache. It
68      *  does not change the limit, but will cause subsequent font measures and
69      *  draws to be recreated, since they will no longer be in the cache.
70      */
71     static void PurgeFontCache();
72 
73     /**
74      *  This function returns the memory used for temporary images and other resources.
75      */
76     static size_t GetResourceCacheTotalBytesUsed();
77 
78     /**
79      *  These functions get/set the memory usage limit for the resource cache, used for temporary
80      *  bitmaps and other resources. Entries are purged from the cache when the memory useage
81      *  exceeds this limit.
82      */
83     static size_t GetResourceCacheTotalByteLimit();
84     static size_t SetResourceCacheTotalByteLimit(size_t newLimit);
85 
86     /**
87      *  For debugging purposes, this will attempt to purge the resource cache. It
88      *  does not change the limit.
89      */
90     static void PurgeResourceCache();
91 
92     /**
93      *  When the cachable entry is very lage (e.g. a large scaled bitmap), adding it to the cache
94      *  can cause most/all of the existing entries to be purged. To avoid the, the client can set
95      *  a limit for a single allocation. If a cacheable entry would have been cached, but its size
96      *  exceeds this limit, then we do not attempt to cache it at all.
97      *
98      *  Zero is the default value, meaning we always attempt to cache entries.
99      */
100     static size_t GetResourceCacheSingleAllocationByteLimit();
101     static size_t SetResourceCacheSingleAllocationByteLimit(size_t newLimit);
102 
103     /**
104      *  Dumps memory usage of caches using the SkTraceMemoryDump interface. See SkTraceMemoryDump
105      *  for usage of this method.
106      */
107     static void DumpMemoryStatistics(SkTraceMemoryDump* dump);
108 
109     /**
110      *  Free as much globally cached memory as possible. This will purge all private caches in Skia,
111      *  including font and image caches.
112      *
113      *  If there are caches associated with GPU context, those will not be affected by this call.
114      */
115     static void PurgeAllCaches();
116 
117     /**
118      *  Applications with command line options may pass optional state, such
119      *  as cache sizes, here, for instance:
120      *  font-cache-limit=12345678
121      *
122      *  The flags format is name=value[;name=value...] with no spaces.
123      *  This format is subject to change.
124      */
125     static void SetFlags(const char* flags);
126 
127     typedef std::unique_ptr<SkImageGenerator>
128                                             (*ImageGeneratorFromEncodedDataFactory)(sk_sp<SkData>);
129 
130     /**
131      *  To instantiate images from encoded data, first looks at this runtime function-ptr. If it
132      *  exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr
133      *  or there is, but it returns NULL, then skia will call its internal default implementation.
134      *
135      *  Returns the previous factory (which could be NULL).
136      */
137     static ImageGeneratorFromEncodedDataFactory
138                     SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory);
139 
140     /**
141      *  Call early in main() to allow Skia to use a JIT to accelerate CPU-bound operations.
142      */
143     static void AllowJIT();
144 };
145 
146 class SkAutoGraphics {
147 public:
SkAutoGraphics()148     SkAutoGraphics() {
149         SkGraphics::Init();
150     }
151 };
152 
153 #endif
154