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 #include <memory> 14 15 class SkData; 16 class SkImageGenerator; 17 class SkOpenTypeSVGDecoder; 18 class SkTraceMemoryDump; 19 20 class SK_API SkGraphics { 21 public: 22 /** 23 * Call this at process initialization time if your environment does not 24 * permit static global initializers that execute code. 25 * Init() is thread-safe and idempotent. 26 */ 27 static void Init(); 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 * For debugging purposes, this will attempt to purge the font cache. It 71 * does not change the limit, but will cause subsequent font measures and 72 * draws to be recreated, since they will no longer be in the cache. 73 */ 74 static void PurgeFontCache(); 75 76 /** 77 * This function returns the memory used for temporary images and other resources. 78 */ 79 static size_t GetResourceCacheTotalBytesUsed(); 80 81 /** 82 * These functions get/set the memory usage limit for the resource cache, used for temporary 83 * bitmaps and other resources. Entries are purged from the cache when the memory useage 84 * exceeds this limit. 85 */ 86 static size_t GetResourceCacheTotalByteLimit(); 87 static size_t SetResourceCacheTotalByteLimit(size_t newLimit); 88 89 /** 90 * For debugging purposes, this will attempt to purge the resource cache. It 91 * does not change the limit. 92 */ 93 static void PurgeResourceCache(); 94 95 /** 96 * When the cachable entry is very lage (e.g. a large scaled bitmap), adding it to the cache 97 * can cause most/all of the existing entries to be purged. To avoid the, the client can set 98 * a limit for a single allocation. If a cacheable entry would have been cached, but its size 99 * exceeds this limit, then we do not attempt to cache it at all. 100 * 101 * Zero is the default value, meaning we always attempt to cache entries. 102 */ 103 static size_t GetResourceCacheSingleAllocationByteLimit(); 104 static size_t SetResourceCacheSingleAllocationByteLimit(size_t newLimit); 105 106 /** 107 * Dumps memory usage of caches using the SkTraceMemoryDump interface. See SkTraceMemoryDump 108 * for usage of this method. 109 */ 110 static void DumpMemoryStatistics(SkTraceMemoryDump* dump); 111 112 /** 113 * Free as much globally cached memory as possible. This will purge all private caches in Skia, 114 * including font and image caches. 115 * 116 * If there are caches associated with GPU context, those will not be affected by this call. 117 */ 118 static void PurgeAllCaches(); 119 120 /** 121 * Applications with command line options may pass optional state, such 122 * as cache sizes, here, for instance: 123 * font-cache-limit=12345678 124 * 125 * The flags format is name=value[;name=value...] with no spaces. 126 * This format is subject to change. 127 */ 128 static void SetFlags(const char* flags); 129 130 typedef std::unique_ptr<SkImageGenerator> 131 (*ImageGeneratorFromEncodedDataFactory)(sk_sp<SkData>); 132 133 /** 134 * To instantiate images from encoded data, first looks at this runtime function-ptr. If it 135 * exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr 136 * or there is, but it returns NULL, then skia will call its internal default implementation. 137 * 138 * Returns the previous factory (which could be NULL). 139 */ 140 static ImageGeneratorFromEncodedDataFactory 141 SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory); 142 143 /** 144 * To draw OpenType SVG data, Skia will look at this runtime function pointer. If this function 145 * pointer is set, the SkTypeface implementations which support OpenType SVG will call this 146 * function to create an SkOpenTypeSVGDecoder to decode the OpenType SVG and draw it as needed. 147 * If this function is not set, the SkTypeface implementations will generally not support 148 * OpenType SVG and attempt to use other glyph representations if available. 149 */ 150 using OpenTypeSVGDecoderFactory = 151 std::unique_ptr<SkOpenTypeSVGDecoder> (*)(const uint8_t* svg, size_t length); 152 static OpenTypeSVGDecoderFactory SetOpenTypeSVGDecoderFactory(OpenTypeSVGDecoderFactory); 153 static OpenTypeSVGDecoderFactory GetOpenTypeSVGDecoderFactory(); 154 155 /** 156 * Call early in main() to allow Skia to use a JIT to accelerate CPU-bound operations. 157 */ 158 static void AllowJIT(); 159 }; 160 161 class SkAutoGraphics { 162 public: SkAutoGraphics()163 SkAutoGraphics() { 164 SkGraphics::Init(); 165 } 166 }; 167 168 #endif 169