1 2/* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#ifndef SkGraphics_DEFINED 11#define SkGraphics_DEFINED 12 13#include "SkTypes.h" 14 15class SK_API SkGraphics { 16public: 17 /** 18 * Call this at process initialization time if your environment does not 19 * permit static global initializers that execute code. Note that 20 * Init() is not thread-safe. 21 */ 22 static void Init(); 23 24 /** 25 * Call this to release any memory held privately, such as the font cache. 26 */ 27 static void Term(); 28 29 /** 30 * Return the version numbers for the library. If the parameter is not 31 * null, it is set to the version number. 32 */ 33 static void GetVersion(int32_t* major, int32_t* minor, int32_t* patch); 34 35 /** 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(); 41 42 /** 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 51 /** 52 * Return the number of bytes currently used by the font cache. 53 */ 54 static size_t GetFontCacheUsed(); 55 56 /** 57 * 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); 74 75 /** 76 * For debugging purposes, this will attempt to purge the font cache. It 77 * does not change the limit, but will cause subsequent font measures and 78 * draws to be recreated, since they will no longer be in the cache. 79 */ 80 static void PurgeFontCache(); 81 82 /** 83 * Scaling bitmaps with the SkPaint::kHigh_FilterLevel setting is 84 * expensive, so the result is saved in the global Scaled Image 85 * Cache. 86 * 87 * This function returns the memory usage of the Scaled Image Cache. 88 */ 89 static size_t GetImageCacheTotalBytesUsed(); 90 /** 91 * These functions get/set the memory usage limit for the Scaled 92 * Image Cache. Bitmaps are purged from the cache when the 93 * memory useage exceeds this limit. 94 */ 95 static size_t GetImageCacheTotalByteLimit(); 96 static size_t SetImageCacheTotalByteLimit(size_t newLimit); 97 98 // DEPRECATED 99 static size_t GetImageCacheBytesUsed() { 100 return GetImageCacheTotalBytesUsed(); 101 } 102 // DEPRECATED 103 static size_t GetImageCacheByteLimit() { 104 return GetImageCacheTotalByteLimit(); 105 } 106 // DEPRECATED 107 static size_t SetImageCacheByteLimit(size_t newLimit) { 108 return SetImageCacheTotalByteLimit(newLimit); 109 } 110 111 /** 112 * Scaling bitmaps with the SkPaint::kHigh_FilterLevel setting is 113 * expensive, so the result is saved in the global Scaled Image 114 * Cache. When the resulting bitmap is too large, this can 115 * overload the cache. If the ImageCacheSingleAllocationByteLimit 116 * is set to a non-zero number, and the resulting bitmap would be 117 * larger than that value, the bitmap scaling algorithm falls 118 * back onto a cheaper algorithm and does not cache the result. 119 * Zero is the default value. 120 */ 121 static size_t GetImageCacheSingleAllocationByteLimit(); 122 static size_t SetImageCacheSingleAllocationByteLimit(size_t newLimit); 123 124 /** 125 * Applications with command line options may pass optional state, such 126 * as cache sizes, here, for instance: 127 * font-cache-limit=12345678 128 * 129 * The flags format is name=value[;name=value...] with no spaces. 130 * This format is subject to change. 131 */ 132 static void SetFlags(const char* flags); 133 134 /** 135 * Return the max number of bytes that should be used by the thread-local 136 * font cache. 137 * If the cache needs to allocate more, it will purge previous entries. 138 * This max can be changed by calling SetFontCacheLimit(). 139 * 140 * If this thread has never called SetTLSFontCacheLimit, or has called it 141 * with 0, then this thread is using the shared font cache. In that case, 142 * this function will always return 0, and the caller may want to call 143 * GetFontCacheLimit. 144 */ 145 static size_t GetTLSFontCacheLimit(); 146 147 /** 148 * Specify the max number of bytes that should be used by the thread-local 149 * font cache. If this value is 0, then this thread will use the shared 150 * global font cache. 151 */ 152 static void SetTLSFontCacheLimit(size_t bytes); 153 154private: 155 /** This is automatically called by SkGraphics::Init(), and must be 156 implemented by the host OS. This allows the host OS to register a callback 157 with the C++ runtime to call SkGraphics::FreeCaches() 158 */ 159 static void InstallNewHandler(); 160}; 161 162class SkAutoGraphics { 163public: 164 SkAutoGraphics() { 165 SkGraphics::Init(); 166 } 167 ~SkAutoGraphics() { 168 SkGraphics::Term(); 169 } 170}; 171 172#endif 173