OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/resource_bundle.h" | 5 #include "app/resource_bundle.h" |
6 | 6 |
7 #include "app/data_pack.h" | 7 #include "app/data_pack.h" |
8 #include "base/lock.h" | |
9 #include "base/logging.h" | 8 #include "base/logging.h" |
10 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "base/synchronization/lock.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "gfx/codec/png_codec.h" | 12 #include "gfx/codec/png_codec.h" |
13 #include "gfx/font.h" | 13 #include "gfx/font.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Font sizes relative to base font. | 18 // Font sizes relative to base font. |
19 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) | 19 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) |
20 const int kSmallFontSizeDelta = -3; | 20 const int kSmallFontSizeDelta = -3; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 } | 84 } |
85 | 85 |
86 /* static */ | 86 /* static */ |
87 ResourceBundle& ResourceBundle::GetSharedInstance() { | 87 ResourceBundle& ResourceBundle::GetSharedInstance() { |
88 // Must call InitSharedInstance before this function. | 88 // Must call InitSharedInstance before this function. |
89 CHECK(g_shared_instance_ != NULL); | 89 CHECK(g_shared_instance_ != NULL); |
90 return *g_shared_instance_; | 90 return *g_shared_instance_; |
91 } | 91 } |
92 | 92 |
93 ResourceBundle::ResourceBundle() | 93 ResourceBundle::ResourceBundle() |
94 : lock_(new Lock), | 94 : lock_(new base::Lock), |
95 resources_data_(NULL), | 95 resources_data_(NULL), |
96 locale_resources_data_(NULL) { | 96 locale_resources_data_(NULL) { |
97 } | 97 } |
98 | 98 |
99 void ResourceBundle::FreeImages() { | 99 void ResourceBundle::FreeImages() { |
100 for (SkImageMap::iterator i = skia_images_.begin(); | 100 for (SkImageMap::iterator i = skia_images_.begin(); |
101 i != skia_images_.end(); i++) { | 101 i != skia_images_.end(); i++) { |
102 delete i->second; | 102 delete i->second; |
103 } | 103 } |
104 skia_images_.clear(); | 104 skia_images_.clear(); |
(...skipping 26 matching lines...) Expand all Loading... |
131 !bytes && it != data_packs_.end(); ++it) { | 131 !bytes && it != data_packs_.end(); ++it) { |
132 bytes = (*it)->GetStaticMemory(resource_id); | 132 bytes = (*it)->GetStaticMemory(resource_id); |
133 } | 133 } |
134 | 134 |
135 return bytes; | 135 return bytes; |
136 } | 136 } |
137 | 137 |
138 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { | 138 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { |
139 // Check to see if we already have the Skia image in the cache. | 139 // Check to see if we already have the Skia image in the cache. |
140 { | 140 { |
141 AutoLock lock_scope(*lock_); | 141 base::AutoLock lock_scope(*lock_); |
142 SkImageMap::const_iterator found = skia_images_.find(resource_id); | 142 SkImageMap::const_iterator found = skia_images_.find(resource_id); |
143 if (found != skia_images_.end()) | 143 if (found != skia_images_.end()) |
144 return found->second; | 144 return found->second; |
145 } | 145 } |
146 | 146 |
147 scoped_ptr<SkBitmap> bitmap; | 147 scoped_ptr<SkBitmap> bitmap; |
148 | 148 |
149 bitmap.reset(LoadBitmap(resources_data_, resource_id)); | 149 bitmap.reset(LoadBitmap(resources_data_, resource_id)); |
150 | 150 |
151 if (bitmap.get()) { | 151 if (bitmap.get()) { |
152 // We loaded successfully. Cache the Skia version of the bitmap. | 152 // We loaded successfully. Cache the Skia version of the bitmap. |
153 AutoLock lock_scope(*lock_); | 153 base::AutoLock lock_scope(*lock_); |
154 | 154 |
155 // Another thread raced us, and has already cached the skia image. | 155 // Another thread raced us, and has already cached the skia image. |
156 if (skia_images_.count(resource_id)) | 156 if (skia_images_.count(resource_id)) |
157 return skia_images_[resource_id]; | 157 return skia_images_[resource_id]; |
158 | 158 |
159 skia_images_[resource_id] = bitmap.get(); | 159 skia_images_[resource_id] = bitmap.get(); |
160 return bitmap.release(); | 160 return bitmap.release(); |
161 } | 161 } |
162 | 162 |
163 // We failed to retrieve the bitmap, show a debugging red square. | 163 // We failed to retrieve the bitmap, show a debugging red square. |
164 { | 164 { |
165 LOG(WARNING) << "Unable to load bitmap with id " << resource_id; | 165 LOG(WARNING) << "Unable to load bitmap with id " << resource_id; |
166 NOTREACHED(); // Want to assert in debug mode. | 166 NOTREACHED(); // Want to assert in debug mode. |
167 | 167 |
168 AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization. | 168 base::AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization. |
169 | 169 |
170 static SkBitmap* empty_bitmap = NULL; | 170 static SkBitmap* empty_bitmap = NULL; |
171 if (!empty_bitmap) { | 171 if (!empty_bitmap) { |
172 // The placeholder bitmap is bright red so people notice the problem. | 172 // The placeholder bitmap is bright red so people notice the problem. |
173 // This bitmap will be leaked, but this code should never be hit. | 173 // This bitmap will be leaked, but this code should never be hit. |
174 empty_bitmap = new SkBitmap(); | 174 empty_bitmap = new SkBitmap(); |
175 empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 175 empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
176 empty_bitmap->allocPixels(); | 176 empty_bitmap->allocPixels(); |
177 empty_bitmap->eraseARGB(255, 255, 0, 0); | 177 empty_bitmap->eraseARGB(255, 255, 0, 0); |
178 } | 178 } |
179 return empty_bitmap; | 179 return empty_bitmap; |
180 } | 180 } |
181 } | 181 } |
182 | 182 |
183 void ResourceBundle::LoadFontsIfNecessary() { | 183 void ResourceBundle::LoadFontsIfNecessary() { |
184 AutoLock lock_scope(*lock_); | 184 base::AutoLock lock_scope(*lock_); |
185 if (!base_font_.get()) { | 185 if (!base_font_.get()) { |
186 base_font_.reset(new gfx::Font()); | 186 base_font_.reset(new gfx::Font()); |
187 | 187 |
188 bold_font_.reset(new gfx::Font()); | 188 bold_font_.reset(new gfx::Font()); |
189 *bold_font_ = | 189 *bold_font_ = |
190 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); | 190 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); |
191 | 191 |
192 small_font_.reset(new gfx::Font()); | 192 small_font_.reset(new gfx::Font()); |
193 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); | 193 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); |
194 | 194 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 253 |
254 bool ResourceBundle::LoadedDataPack::GetStringPiece( | 254 bool ResourceBundle::LoadedDataPack::GetStringPiece( |
255 int resource_id, base::StringPiece* data) const { | 255 int resource_id, base::StringPiece* data) const { |
256 return data_pack_->GetStringPiece(static_cast<uint32>(resource_id), data); | 256 return data_pack_->GetStringPiece(static_cast<uint32>(resource_id), data); |
257 } | 257 } |
258 | 258 |
259 RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( | 259 RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( |
260 int resource_id) const { | 260 int resource_id) const { |
261 return data_pack_->GetStaticMemory(resource_id); | 261 return data_pack_->GetStaticMemory(resource_id); |
262 } | 262 } |
OLD | NEW |