| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 7 #include <vector> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 if (it == dispatcher_font_map_[dispatcher].end()) { | 49 if (it == dispatcher_font_map_[dispatcher].end()) { |
| 50 // Requested font is new to cache. | 50 // Requested font is new to cache. |
| 51 dispatcher_font_map_[dispatcher].push_back(font_name); | 51 dispatcher_font_map_[dispatcher].push_back(font_name); |
| 52 } else { | 52 } else { |
| 53 ref_count_inc = 0; | 53 ref_count_inc = 0; |
| 54 } | 54 } |
| 55 | 55 |
| 56 if (cache_[font_name].ref_count_ == 0) { // Requested font is new to cache. | 56 if (cache_[font_name].ref_count_ == 0) { // Requested font is new to cache. |
| 57 cache_[font_name].ref_count_ = 1; | 57 cache_[font_name].ref_count_ = 1; |
| 58 } else { // Requested font is already in cache, release old handles. | 58 } else { // Requested font is already in cache, release old handles. |
| 59 SelectObject(cache_[font_name].dc_, cache_[font_name].old_font_); |
| 59 DeleteObject(cache_[font_name].font_); | 60 DeleteObject(cache_[font_name].font_); |
| 60 DeleteDC(cache_[font_name].dc_); | 61 ReleaseDC(NULL, cache_[font_name].dc_); |
| 61 } | 62 } |
| 62 cache_[font_name].font_ = font_handle; | 63 cache_[font_name].font_ = font_handle; |
| 63 cache_[font_name].dc_ = hdc; | 64 cache_[font_name].dc_ = hdc; |
| 65 cache_[font_name].old_font_ = old_font; |
| 64 cache_[font_name].ref_count_ += ref_count_inc; | 66 cache_[font_name].ref_count_ += ref_count_inc; |
| 65 } | 67 } |
| 66 | 68 |
| 67 void ReleaseCachedFonts(FontCacheDispatcher* dispatcher) { | 69 void ReleaseCachedFonts(FontCacheDispatcher* dispatcher) { |
| 68 typedef std::map<string16, FontCache::CacheElement> FontNameToElement; | 70 typedef std::map<string16, FontCache::CacheElement> FontNameToElement; |
| 69 | 71 |
| 70 base::AutoLock lock(mutex_); | 72 base::AutoLock lock(mutex_); |
| 71 | 73 |
| 72 DispatcherToFontNames::iterator it; | 74 DispatcherToFontNames::iterator it; |
| 73 it = dispatcher_font_map_.find(dispatcher); | 75 it = dispatcher_font_map_.find(dispatcher); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 90 cache_.erase(i++); | 92 cache_.erase(i++); |
| 91 } else { | 93 } else { |
| 92 ++i; | 94 ++i; |
| 93 } | 95 } |
| 94 } | 96 } |
| 95 } | 97 } |
| 96 | 98 |
| 97 private: | 99 private: |
| 98 struct CacheElement { | 100 struct CacheElement { |
| 99 CacheElement() | 101 CacheElement() |
| 100 : font_(NULL), dc_(NULL), ref_count_(0) { | 102 : font_(NULL), old_font_(NULL), dc_(NULL), ref_count_(0) { |
| 101 } | 103 } |
| 102 | 104 |
| 103 ~CacheElement() { | 105 ~CacheElement() { |
| 104 if (font_) { | 106 if (font_) { |
| 107 if (dc_ && old_font_) { |
| 108 SelectObject(dc_, old_font_); |
| 109 } |
| 105 DeleteObject(font_); | 110 DeleteObject(font_); |
| 106 } | 111 } |
| 107 if (dc_) { | 112 if (dc_) { |
| 108 DeleteDC(dc_); | 113 ReleaseDC(NULL, dc_); |
| 109 } | 114 } |
| 110 } | 115 } |
| 111 | 116 |
| 112 HFONT font_; | 117 HFONT font_; |
| 118 HGDIOBJ old_font_; |
| 113 HDC dc_; | 119 HDC dc_; |
| 114 int ref_count_; | 120 int ref_count_; |
| 115 }; | 121 }; |
| 116 friend struct DefaultSingletonTraits<FontCache>; | 122 friend struct DefaultSingletonTraits<FontCache>; |
| 117 | 123 |
| 118 FontCache() { | 124 FontCache() { |
| 119 } | 125 } |
| 120 | 126 |
| 121 std::map<string16, CacheElement> cache_; | 127 std::map<string16, CacheElement> cache_; |
| 122 DispatcherToFontNames dispatcher_font_map_; | 128 DispatcherToFontNames dispatcher_font_map_; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // when a font is asked to be cached, we always recreates the font object | 182 // when a font is asked to be cached, we always recreates the font object |
| 177 // to avoid the case that an in-cache font is swapped out by GDI. | 183 // to avoid the case that an in-cache font is swapped out by GDI. |
| 178 FontCache::GetInstance()->PreCacheFont(font, this); | 184 FontCache::GetInstance()->PreCacheFont(font, this); |
| 179 } | 185 } |
| 180 | 186 |
| 181 void FontCacheDispatcher::OnReleaseCachedFonts() { | 187 void FontCacheDispatcher::OnReleaseCachedFonts() { |
| 182 // Release cached fonts that requested from a pid by decrementing the ref | 188 // Release cached fonts that requested from a pid by decrementing the ref |
| 183 // count. When ref count is zero, the handles are released. | 189 // count. When ref count is zero, the handles are released. |
| 184 FontCache::GetInstance()->ReleaseCachedFonts(this); | 190 FontCache::GetInstance()->ReleaseCachedFonts(this); |
| 185 } | 191 } |
| OLD | NEW |