SkGraphics.h revision 1c84634454aa78fb26f23875b86a243aa4596c59
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 "SkTypes.h" 12 13class SkData; 14class SkImageGenerator; 15 16class SK_API SkGraphics { 17public: 18 /** 19 * Call this at process initialization time if your environment does not 20 * permit static global initializers that execute code. Note that 21 * Init() is not thread-safe. 22 */ 23 static void Init(); 24 25 /** 26 * Call this to release any memory held privately, such as the font cache. 27 */ 28 static void Term(); 29 30 /** 31 * Return the version numbers for the library. If the parameter is not 32 * null, it is set to the version number. 33 */ 34 static void GetVersion(int32_t* major, int32_t* minor, int32_t* patch); 35 36 /** 37 * Return 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 * This max can be changed by calling SetFontCacheLimit(). 40 */ 41 static size_t GetFontCacheLimit(); 42 43 /** 44 * Specify the max number of bytes that should be used by the font cache. 45 * If the cache needs to allocate more, it will purge previous entries. 46 * 47 * This function returns the previous setting, as if GetFontCacheLimit() 48 * had be called before the new limit was set. 49 */ 50 static size_t SetFontCacheLimit(size_t bytes); 51 52 /** 53 * Return the number of bytes currently used by the font cache. 54 */ 55 static size_t GetFontCacheUsed(); 56 57 /** 58 * Return the number of entries in the font cache. 59 * A cache "entry" is associated with each typeface + pointSize + matrix. 60 */ 61 static int GetFontCacheCountUsed(); 62 63 /** 64 * Return the current limit to the number of entries in the font cache. 65 * A cache "entry" is associated with each typeface + pointSize + matrix. 66 */ 67 static int GetFontCacheCountLimit(); 68 69 /** 70 * Set the limit to the number of entries in the font cache, and return 71 * the previous value. If this new value is lower than the previous, 72 * it will automatically try to purge entries to meet the new limit. 73 */ 74 static int SetFontCacheCountLimit(int count); 75 76 /** 77 * For debugging purposes, this will attempt to purge the font cache. It 78 * does not change the limit, but will cause subsequent font measures and 79 * draws to be recreated, since they will no longer be in the cache. 80 */ 81 static void PurgeFontCache(); 82 83 /** 84 * Scaling bitmaps with the kHigh_SkFilterQuality setting is 85 * expensive, so the result is saved in the global Scaled Image 86 * Cache. 87 * 88 * This function returns the memory usage of the Scaled Image Cache. 89 */ 90 static size_t GetResourceCacheTotalBytesUsed(); 91 92 /** 93 * These functions get/set the memory usage limit for the resource cache, used for temporary 94 * bitmaps and other resources. Entries are purged from the cache when the memory useage 95 * exceeds this limit. 96 */ 97 static size_t GetResourceCacheTotalByteLimit(); 98 static size_t SetResourceCacheTotalByteLimit(size_t newLimit); 99 100 /** 101 * For debugging purposes, this will attempt to purge the resource cache. It 102 * does not change the limit. 103 */ 104 static void PurgeResourceCache(); 105 106 /** 107 * When the cachable entry is very lage (e.g. a large scaled bitmap), adding it to the cache 108 * can cause most/all of the existing entries to be purged. To avoid the, the client can set 109 * a limit for a single allocation. If a cacheable entry would have been cached, but its size 110 * exceeds this limit, then we do not attempt to cache it at all. 111 * 112 * Zero is the default value, meaning we always attempt to cache entries. 113 */ 114 static size_t GetResourceCacheSingleAllocationByteLimit(); 115 static size_t SetResourceCacheSingleAllocationByteLimit(size_t newLimit); 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 /** 128 * Return the max number of bytes that should be used by the thread-local 129 * font cache. 130 * If the cache needs to allocate more, it will purge previous entries. 131 * This max can be changed by calling SetFontCacheLimit(). 132 * 133 * If this thread has never called SetTLSFontCacheLimit, or has called it 134 * with 0, then this thread is using the shared font cache. In that case, 135 * this function will always return 0, and the caller may want to call 136 * GetFontCacheLimit. 137 */ 138 static size_t GetTLSFontCacheLimit(); 139 140 /** 141 * Specify the max number of bytes that should be used by the thread-local 142 * font cache. If this value is 0, then this thread will use the shared 143 * global font cache. 144 */ 145 static void SetTLSFontCacheLimit(size_t bytes); 146 147 typedef SkImageGenerator* (*ImageGeneratorFromEncodedFactory)(SkData*); 148 149 /** 150 * To instantiate images from encoded data, first looks at this runtime function-ptr. If it 151 * exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr 152 * or there is, but it returns NULL, then skia will call its internal default implementation. 153 */ 154 static ImageGeneratorFromEncodedFactory GetImageGeneratorFromEncodedFactory(); 155 static void SetImageGeneratorFromEncodedFactory(ImageGeneratorFromEncodedFactory); 156}; 157 158class SkAutoGraphics { 159public: 160 SkAutoGraphics() { 161 SkGraphics::Init(); 162 } 163 ~SkAutoGraphics() { 164 SkGraphics::Term(); 165 } 166}; 167 168#endif 169