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 #include "SkGraphics.h"
9 
10 #include "SkBlitter.h"
11 #include "SkCanvas.h"
12 #include "SkCpu.h"
13 #include "SkGeometry.h"
14 #include "SkImageFilter.h"
15 #include "SkMath.h"
16 #include "SkMatrix.h"
17 #include "SkOpts.h"
18 #include "SkPath.h"
19 #include "SkPathEffect.h"
20 #include "SkRefCnt.h"
21 #include "SkResourceCache.h"
22 #include "SkScalerContext.h"
23 #include "SkShader.h"
24 #include "SkStream.h"
25 #include "SkStrikeCache.h"
26 #include "SkTSearch.h"
27 #include "SkTime.h"
28 #include "SkTypefaceCache.h"
29 #include "SkUTF.h"
30 
31 #include <stdlib.h>
32 
GetVersion(int32_t * major,int32_t * minor,int32_t * patch)33 void SkGraphics::GetVersion(int32_t* major, int32_t* minor, int32_t* patch) {
34     if (major) {
35         *major = SKIA_VERSION_MAJOR;
36     }
37     if (minor) {
38         *minor = SKIA_VERSION_MINOR;
39     }
40     if (patch) {
41         *patch = SKIA_VERSION_PATCH;
42     }
43 }
44 
Init()45 void SkGraphics::Init() {
46     // SkGraphics::Init() must be thread-safe and idempotent.
47     SkCpu::CacheRuntimeFeatures();
48     SkOpts::Init();
49 }
50 
51 ///////////////////////////////////////////////////////////////////////////////
52 
DumpMemoryStatistics(SkTraceMemoryDump * dump)53 void SkGraphics::DumpMemoryStatistics(SkTraceMemoryDump* dump) {
54   SkResourceCache::DumpMemoryStatistics(dump);
55   SkStrikeCache::DumpMemoryStatistics(dump);
56 }
57 
PurgeAllCaches()58 void SkGraphics::PurgeAllCaches() {
59     SkGraphics::PurgeFontCache();
60     SkGraphics::PurgeResourceCache();
61     SkImageFilter::PurgeCache();
62 }
63 
64 ///////////////////////////////////////////////////////////////////////////////
65 
66 static const char kFontCacheLimitStr[] = "font-cache-limit";
67 static const size_t kFontCacheLimitLen = sizeof(kFontCacheLimitStr) - 1;
68 
69 static const struct {
70     const char* fStr;
71     size_t fLen;
72     size_t (*fFunc)(size_t);
73 } gFlags[] = {
74     { kFontCacheLimitStr, kFontCacheLimitLen, SkGraphics::SetFontCacheLimit }
75 };
76 
77 /* flags are of the form param; or param=value; */
SetFlags(const char * flags)78 void SkGraphics::SetFlags(const char* flags) {
79     if (!flags) {
80         return;
81     }
82     const char* nextSemi;
83     do {
84         size_t len = strlen(flags);
85         const char* paramEnd = flags + len;
86         const char* nextEqual = strchr(flags, '=');
87         if (nextEqual && paramEnd > nextEqual) {
88             paramEnd = nextEqual;
89         }
90         nextSemi = strchr(flags, ';');
91         if (nextSemi && paramEnd > nextSemi) {
92             paramEnd = nextSemi;
93         }
94         size_t paramLen = paramEnd - flags;
95         for (int i = 0; i < (int)SK_ARRAY_COUNT(gFlags); ++i) {
96             if (paramLen != gFlags[i].fLen) {
97                 continue;
98             }
99             if (strncmp(flags, gFlags[i].fStr, paramLen) == 0) {
100                 size_t val = 0;
101                 if (nextEqual) {
102                     val = (size_t) atoi(nextEqual + 1);
103                 }
104                 (gFlags[i].fFunc)(val);
105                 break;
106             }
107         }
108         flags = nextSemi + 1;
109     } while (nextSemi);
110 }
111 
GetFontCacheLimit()112 size_t SkGraphics::GetFontCacheLimit() {
113     return SkStrikeCache::GlobalStrikeCache()->getCacheSizeLimit();
114 }
115 
SetFontCacheLimit(size_t bytes)116 size_t SkGraphics::SetFontCacheLimit(size_t bytes) {
117     return SkStrikeCache::GlobalStrikeCache()->setCacheSizeLimit(bytes);
118 }
119 
GetFontCacheUsed()120 size_t SkGraphics::GetFontCacheUsed() {
121     return SkStrikeCache::GlobalStrikeCache()->getTotalMemoryUsed();
122 }
123 
GetFontCacheCountLimit()124 int SkGraphics::GetFontCacheCountLimit() {
125     return SkStrikeCache::GlobalStrikeCache()->getCacheCountLimit();
126 }
127 
SetFontCacheCountLimit(int count)128 int SkGraphics::SetFontCacheCountLimit(int count) {
129     return SkStrikeCache::GlobalStrikeCache()->setCacheCountLimit(count);
130 }
131 
GetFontCacheCountUsed()132 int SkGraphics::GetFontCacheCountUsed() {
133     return SkStrikeCache::GlobalStrikeCache()->getCacheCountUsed();
134 }
135 
GetFontCachePointSizeLimit()136 int SkGraphics::GetFontCachePointSizeLimit() {
137     return SkStrikeCache::GlobalStrikeCache()->getCachePointSizeLimit();
138 }
139 
SetFontCachePointSizeLimit(int limit)140 int SkGraphics::SetFontCachePointSizeLimit(int limit) {
141     return SkStrikeCache::GlobalStrikeCache()->setCachePointSizeLimit(limit);
142 }
143 
PurgeFontCache()144 void SkGraphics::PurgeFontCache() {
145     SkStrikeCache::GlobalStrikeCache()->purgeAll();
146     SkTypefaceCache::PurgeAll();
147 }
148