OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/font_cache_dispatcher_win.h" | 5 #include "content/common/font_cache_dispatcher_win.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "content/common/child_process_messages.h" | 12 #include "content/common/child_process_messages.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 namespace { | 15 namespace { |
16 typedef std::vector<string16> FontNameVector; | 16 typedef std::vector<base::string16> FontNameVector; |
17 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; | 17 typedef std::map<FontCacheDispatcher*, FontNameVector> DispatcherToFontNames; |
18 | 18 |
19 class FontCache { | 19 class FontCache { |
20 public: | 20 public: |
21 static FontCache* GetInstance() { | 21 static FontCache* GetInstance() { |
22 return Singleton<FontCache>::get(); | 22 return Singleton<FontCache>::get(); |
23 } | 23 } |
24 | 24 |
25 void PreCacheFont(const LOGFONT& font, FontCacheDispatcher* dispatcher) { | 25 void PreCacheFont(const LOGFONT& font, FontCacheDispatcher* dispatcher) { |
26 typedef std::map<string16, FontCache::CacheElement> FontNameToElement; | 26 typedef std::map<base::string16, FontCache::CacheElement> FontNameToElement; |
27 | 27 |
28 base::AutoLock lock(mutex_); | 28 base::AutoLock lock(mutex_); |
29 | 29 |
30 // Fetch the font into memory. | 30 // Fetch the font into memory. |
31 // No matter the font is cached or not, we load it to avoid GDI swapping out | 31 // No matter the font is cached or not, we load it to avoid GDI swapping out |
32 // that font file. | 32 // that font file. |
33 HDC hdc = GetDC(NULL); | 33 HDC hdc = GetDC(NULL); |
34 HFONT font_handle = CreateFontIndirect(&font); | 34 HFONT font_handle = CreateFontIndirect(&font); |
35 DCHECK(NULL != font_handle); | 35 DCHECK(NULL != font_handle); |
36 | 36 |
37 HGDIOBJ old_font = SelectObject(hdc, font_handle); | 37 HGDIOBJ old_font = SelectObject(hdc, font_handle); |
38 DCHECK(NULL != old_font); | 38 DCHECK(NULL != old_font); |
39 | 39 |
40 TEXTMETRIC tm; | 40 TEXTMETRIC tm; |
41 BOOL ret = GetTextMetrics(hdc, &tm); | 41 BOOL ret = GetTextMetrics(hdc, &tm); |
42 DCHECK(ret); | 42 DCHECK(ret); |
43 | 43 |
44 string16 font_name = font.lfFaceName; | 44 base::string16 font_name = font.lfFaceName; |
45 int ref_count_inc = 1; | 45 int ref_count_inc = 1; |
46 FontNameVector::iterator it = | 46 FontNameVector::iterator it = |
47 std::find(dispatcher_font_map_[dispatcher].begin(), | 47 std::find(dispatcher_font_map_[dispatcher].begin(), |
48 dispatcher_font_map_[dispatcher].end(), | 48 dispatcher_font_map_[dispatcher].end(), |
49 font_name); | 49 font_name); |
50 if (it == dispatcher_font_map_[dispatcher].end()) { | 50 if (it == dispatcher_font_map_[dispatcher].end()) { |
51 // Requested font is new to cache. | 51 // Requested font is new to cache. |
52 dispatcher_font_map_[dispatcher].push_back(font_name); | 52 dispatcher_font_map_[dispatcher].push_back(font_name); |
53 } else { | 53 } else { |
54 ref_count_inc = 0; | 54 ref_count_inc = 0; |
55 } | 55 } |
56 | 56 |
57 if (cache_[font_name].ref_count_ == 0) { // Requested font is new to cache. | 57 if (cache_[font_name].ref_count_ == 0) { // Requested font is new to cache. |
58 cache_[font_name].ref_count_ = 1; | 58 cache_[font_name].ref_count_ = 1; |
59 } else { // Requested font is already in cache, release old handles. | 59 } else { // Requested font is already in cache, release old handles. |
60 SelectObject(cache_[font_name].dc_, cache_[font_name].old_font_); | 60 SelectObject(cache_[font_name].dc_, cache_[font_name].old_font_); |
61 DeleteObject(cache_[font_name].font_); | 61 DeleteObject(cache_[font_name].font_); |
62 ReleaseDC(NULL, cache_[font_name].dc_); | 62 ReleaseDC(NULL, cache_[font_name].dc_); |
63 } | 63 } |
64 cache_[font_name].font_ = font_handle; | 64 cache_[font_name].font_ = font_handle; |
65 cache_[font_name].dc_ = hdc; | 65 cache_[font_name].dc_ = hdc; |
66 cache_[font_name].old_font_ = old_font; | 66 cache_[font_name].old_font_ = old_font; |
67 cache_[font_name].ref_count_ += ref_count_inc; | 67 cache_[font_name].ref_count_ += ref_count_inc; |
68 } | 68 } |
69 | 69 |
70 void ReleaseCachedFonts(FontCacheDispatcher* dispatcher) { | 70 void ReleaseCachedFonts(FontCacheDispatcher* dispatcher) { |
71 typedef std::map<string16, FontCache::CacheElement> FontNameToElement; | 71 typedef std::map<base::string16, FontCache::CacheElement> FontNameToElement; |
72 | 72 |
73 base::AutoLock lock(mutex_); | 73 base::AutoLock lock(mutex_); |
74 | 74 |
75 DispatcherToFontNames::iterator it; | 75 DispatcherToFontNames::iterator it; |
76 it = dispatcher_font_map_.find(dispatcher); | 76 it = dispatcher_font_map_.find(dispatcher); |
77 if (it == dispatcher_font_map_.end()) { | 77 if (it == dispatcher_font_map_.end()) { |
78 return; | 78 return; |
79 } | 79 } |
80 | 80 |
81 for (FontNameVector::iterator i = it->second.begin(), e = it->second.end(); | 81 for (FontNameVector::iterator i = it->second.begin(), e = it->second.end(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 HFONT font_; | 118 HFONT font_; |
119 HGDIOBJ old_font_; | 119 HGDIOBJ old_font_; |
120 HDC dc_; | 120 HDC dc_; |
121 int ref_count_; | 121 int ref_count_; |
122 }; | 122 }; |
123 friend struct DefaultSingletonTraits<FontCache>; | 123 friend struct DefaultSingletonTraits<FontCache>; |
124 | 124 |
125 FontCache() { | 125 FontCache() { |
126 } | 126 } |
127 | 127 |
128 std::map<string16, CacheElement> cache_; | 128 std::map<base::string16, CacheElement> cache_; |
129 DispatcherToFontNames dispatcher_font_map_; | 129 DispatcherToFontNames dispatcher_font_map_; |
130 base::Lock mutex_; | 130 base::Lock mutex_; |
131 | 131 |
132 DISALLOW_COPY_AND_ASSIGN(FontCache); | 132 DISALLOW_COPY_AND_ASSIGN(FontCache); |
133 }; | 133 }; |
134 | 134 |
135 } | 135 } |
136 | 136 |
137 FontCacheDispatcher::FontCacheDispatcher() | 137 FontCacheDispatcher::FontCacheDispatcher() |
138 : channel_(NULL) { | 138 : channel_(NULL) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 FontCache::GetInstance()->PreCacheFont(font, this); | 185 FontCache::GetInstance()->PreCacheFont(font, this); |
186 } | 186 } |
187 | 187 |
188 void FontCacheDispatcher::OnReleaseCachedFonts() { | 188 void FontCacheDispatcher::OnReleaseCachedFonts() { |
189 // Release cached fonts that requested from a pid by decrementing the ref | 189 // Release cached fonts that requested from a pid by decrementing the ref |
190 // count. When ref count is zero, the handles are released. | 190 // count. When ref count is zero, the handles are released. |
191 FontCache::GetInstance()->ReleaseCachedFonts(this); | 191 FontCache::GetInstance()->ReleaseCachedFonts(this); |
192 } | 192 } |
193 | 193 |
194 } // namespace content | 194 } // namespace content |
OLD | NEW |