OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/common/resource_bundle.h" | 5 #include "chrome/common/resource_bundle.h" |
6 | 6 |
7 #include <atlbase.h> | 7 #include <atlbase.h> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/gfx/png_decoder.h" | 10 #include "base/gfx/png_decoder.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 NOTREACHED() << "Unable to decode theme resource " << resource_id; | 139 NOTREACHED() << "Unable to decode theme resource " << resource_id; |
140 return NULL; | 140 return NULL; |
141 } | 141 } |
142 | 142 |
143 return PNGDecoder::CreateSkBitmapFromBGRAFormat(png_data, | 143 return PNGDecoder::CreateSkBitmapFromBGRAFormat(png_data, |
144 image_width, | 144 image_width, |
145 image_height); | 145 image_height); |
146 } | 146 } |
147 | 147 |
148 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { | 148 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { |
149 AutoLock lock_scope(lock_); | 149 // Check to see if we already have the Skia image in the cache. |
150 | 150 { |
151 SkImageMap::const_iterator found = skia_images_.find(resource_id); | 151 AutoLock lock_scope(lock_); |
152 SkBitmap* bitmap = NULL; | 152 SkImageMap::const_iterator found = skia_images_.find(resource_id); |
153 | 153 if (found != skia_images_.end()) |
154 // If not found load and store the image | 154 return found->second; |
155 if (found == skia_images_.end()) { | |
156 // Try to load the bitmap from the theme dll. | |
157 if (theme_dll_) | |
158 bitmap = LoadBitmap(theme_dll_, resource_id); | |
159 // We did not find the bitmap in the theme DLL, try the current one. | |
160 if (!bitmap) | |
161 bitmap = LoadBitmap(_AtlBaseModule.GetModuleInstance(), resource_id); | |
162 skia_images_[resource_id] = bitmap; | |
163 } else { | |
164 bitmap = found->second; | |
165 } | 155 } |
166 | 156 |
167 // This bitmap will be returned when a bitmap is requested that can not be | 157 scoped_ptr<SkBitmap> bitmap; |
168 // found. The data inside is lazily initialized, so users must lock and | |
169 static SkBitmap* empty_bitmap = NULL; | |
170 | 158 |
171 // Handle the case where loading the bitmap failed. | 159 // Try to load the bitmap from the theme dll. |
172 if (!bitmap) { | 160 if (theme_dll_) |
| 161 bitmap.reset(LoadBitmap(theme_dll_, resource_id)); |
| 162 |
| 163 // If we did not find the bitmap in the theme DLL, try the current one. |
| 164 if (!bitmap.get()) |
| 165 bitmap.reset(LoadBitmap(_AtlBaseModule.GetModuleInstance(), resource_id)); |
| 166 |
| 167 // We loaded successfully. Cache the Skia version of the bitmap. |
| 168 if (bitmap.get()) { |
| 169 AutoLock lock_scope(lock_); |
| 170 |
| 171 // Another thread raced us, and has already cached the skia image. |
| 172 if (skia_images_.count(resource_id)) |
| 173 return skia_images_[resource_id]; |
| 174 |
| 175 skia_images_[resource_id] = bitmap.get(); |
| 176 return bitmap.release(); |
| 177 } |
| 178 |
| 179 // We failed to retrieve the bitmap, show a debugging red square. |
| 180 { |
173 LOG(WARNING) << "Unable to load bitmap with id " << resource_id; | 181 LOG(WARNING) << "Unable to load bitmap with id " << resource_id; |
174 NOTREACHED(); // Want to assert in debug mode. | 182 NOTREACHED(); // Want to assert in debug mode. |
| 183 |
| 184 AutoLock lock_scope(lock_); // Guard empty_bitmap initialization. |
| 185 |
| 186 static SkBitmap* empty_bitmap = NULL; |
175 if (!empty_bitmap) { | 187 if (!empty_bitmap) { |
176 // The placeholder bitmap is bright red so people notice the problem.» | 188 // The placeholder bitmap is bright red so people notice the problem. |
177 empty_bitmap = new SkBitmap();» | 189 // This bitmap will be leaked, but this code should never be hit. |
| 190 empty_bitmap = new SkBitmap(); |
178 empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 191 empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
179 empty_bitmap->allocPixels(); | 192 empty_bitmap->allocPixels(); |
180 empty_bitmap->eraseARGB(255, 255, 0, 0); | 193 empty_bitmap->eraseARGB(255, 255, 0, 0); |
181 } | 194 } |
182 return empty_bitmap; | 195 return empty_bitmap; |
183 } | 196 } |
184 return bitmap; | |
185 } | 197 } |
186 | 198 |
187 bool ResourceBundle::LoadImageResourceBytes(int resource_id, | 199 bool ResourceBundle::LoadImageResourceBytes(int resource_id, |
188 vector<unsigned char>* bytes) { | 200 vector<unsigned char>* bytes) { |
189 return LoadModuleResourceBytes(theme_dll_, resource_id, bytes); | 201 return LoadModuleResourceBytes(theme_dll_, resource_id, bytes); |
190 } | 202 } |
191 | 203 |
192 bool ResourceBundle::LoadDataResourceBytes(int resource_id, | 204 bool ResourceBundle::LoadDataResourceBytes(int resource_id, |
193 vector<unsigned char>* bytes) { | 205 vector<unsigned char>* bytes) { |
194 return LoadModuleResourceBytes(_AtlBaseModule.GetModuleInstance(), | 206 return LoadModuleResourceBytes(_AtlBaseModule.GetModuleInstance(), |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 return *medium_bold_font_; | 321 return *medium_bold_font_; |
310 case LargeFont: | 322 case LargeFont: |
311 return *large_font_; | 323 return *large_font_; |
312 case WebFont: | 324 case WebFont: |
313 return *web_font_; | 325 return *web_font_; |
314 default: | 326 default: |
315 return *base_font_; | 327 return *base_font_; |
316 } | 328 } |
317 } | 329 } |
318 | 330 |
OLD | NEW |