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 "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "skia/ext/image_operations.h" |
20 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
22 #include "ui/base/layout.h" | 23 #include "ui/base/layout.h" |
23 #include "ui/base/resource/data_pack.h" | 24 #include "ui/base/resource/data_pack.h" |
24 #include "ui/base/ui_base_paths.h" | 25 #include "ui/base/ui_base_paths.h" |
25 #include "ui/base/ui_base_switches.h" | 26 #include "ui/base/ui_base_switches.h" |
26 #include "ui/gfx/codec/jpeg_codec.h" | 27 #include "ui/gfx/codec/jpeg_codec.h" |
27 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
28 #include "ui/gfx/image/image_skia.h" | 29 #include "ui/gfx/image/image_skia.h" |
29 #include "ui/gfx/screen.h" | 30 #include "ui/gfx/screen.h" |
| 31 #include "ui/gfx/skbitmap_operations.h" |
30 | 32 |
31 namespace ui { | 33 namespace ui { |
32 | 34 |
33 namespace { | 35 namespace { |
34 | 36 |
35 // Font sizes relative to base font. | 37 // Font sizes relative to base font. |
36 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) | 38 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) |
37 const int kSmallFontSizeDelta = -3; | 39 const int kSmallFontSizeDelta = -3; |
38 const int kMediumFontSizeDelta = 2; | 40 const int kMediumFontSizeDelta = 2; |
39 const int kLargeFontSizeDelta = 7; | 41 const int kLargeFontSizeDelta = 7; |
40 #else | 42 #else |
41 const int kSmallFontSizeDelta = -2; | 43 const int kSmallFontSizeDelta = -2; |
42 const int kMediumFontSizeDelta = 3; | 44 const int kMediumFontSizeDelta = 3; |
43 const int kLargeFontSizeDelta = 8; | 45 const int kLargeFontSizeDelta = 8; |
44 #endif | 46 #endif |
45 | 47 |
| 48 // If 2x resource is missing from |image| or is the incorrect size, |
| 49 // logs the resource id and creates a 2x version of the resource. |
| 50 // Blends the created resource with red to make it distinguishable from |
| 51 // bitmaps in the resource pak. |
| 52 void Create2xResourceIfMissing(gfx::ImageSkia image, int idr) { |
| 53 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 54 if (command_line->HasSwitch( |
| 55 switches::kHighlightMissing2xResources) && |
| 56 command_line->HasSwitch(switches::kLoad2xResources) && |
| 57 !image.HasBitmapForScale(2.0f)) { |
| 58 float bitmap_scale; |
| 59 SkBitmap bitmap = image.GetBitmapForScale(2.0f, &bitmap_scale); |
| 60 |
| 61 if (bitmap_scale == 1.0f) |
| 62 LOG(INFO) << "Missing 2x resource with id " << idr; |
| 63 else |
| 64 LOG(INFO) << "Incorrectly sized 2x resource with id " << idr; |
| 65 |
| 66 SkBitmap bitmap2x = skia::ImageOperations::Resize(bitmap, |
| 67 skia::ImageOperations::RESIZE_LANCZOS3, |
| 68 image.width() * 2, image.height() * 2); |
| 69 |
| 70 SkBitmap mask; |
| 71 mask.setConfig(SkBitmap::kARGB_8888_Config, |
| 72 bitmap2x.width(), |
| 73 bitmap2x.height()); |
| 74 mask.allocPixels(); |
| 75 mask.eraseColor(SK_ColorRED); |
| 76 SkBitmap result = SkBitmapOperations::CreateBlendedBitmap(bitmap2x, mask, |
| 77 0.2); |
| 78 image.AddBitmapForScale(result, 2.0f); |
| 79 } |
| 80 } |
| 81 |
46 } // namespace | 82 } // namespace |
47 | 83 |
48 ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; | 84 ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; |
49 | 85 |
50 // static | 86 // static |
51 std::string ResourceBundle::InitSharedInstanceWithLocale( | 87 std::string ResourceBundle::InitSharedInstanceWithLocale( |
52 const std::string& pref_locale, Delegate* delegate) { | 88 const std::string& pref_locale, Delegate* delegate) { |
53 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 89 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
54 g_shared_instance_ = new ResourceBundle(delegate); | 90 g_shared_instance_ = new ResourceBundle(delegate); |
55 | 91 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 } | 288 } |
253 } | 289 } |
254 | 290 |
255 if (image_skia.empty()) { | 291 if (image_skia.empty()) { |
256 LOG(WARNING) << "Unable to load image with id " << resource_id; | 292 LOG(WARNING) << "Unable to load image with id " << resource_id; |
257 NOTREACHED(); // Want to assert in debug mode. | 293 NOTREACHED(); // Want to assert in debug mode. |
258 // The load failed to retrieve the image; show a debugging red square. | 294 // The load failed to retrieve the image; show a debugging red square. |
259 return GetEmptyImage(); | 295 return GetEmptyImage(); |
260 } | 296 } |
261 | 297 |
| 298 Create2xResourceIfMissing(image_skia, resource_id); |
| 299 |
262 image = gfx::Image(image_skia); | 300 image = gfx::Image(image_skia); |
263 } | 301 } |
264 | 302 |
265 // The load was successful, so cache the image. | 303 // The load was successful, so cache the image. |
266 base::AutoLock lock_scope(*images_and_fonts_lock_); | 304 base::AutoLock lock_scope(*images_and_fonts_lock_); |
267 | 305 |
268 // Another thread raced the load and has already cached the image. | 306 // Another thread raced the load and has already cached the image. |
269 if (images_.count(resource_id)) | 307 if (images_.count(resource_id)) |
270 return images_[resource_id]; | 308 return images_[resource_id]; |
271 | 309 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 SkBitmap bitmap; | 531 SkBitmap bitmap; |
494 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 532 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
495 bitmap.allocPixels(); | 533 bitmap.allocPixels(); |
496 bitmap.eraseARGB(255, 255, 0, 0); | 534 bitmap.eraseARGB(255, 255, 0, 0); |
497 empty_image_ = gfx::Image(bitmap); | 535 empty_image_ = gfx::Image(bitmap); |
498 } | 536 } |
499 return empty_image_; | 537 return empty_image_; |
500 } | 538 } |
501 | 539 |
502 } // namespace ui | 540 } // namespace ui |
OLD | NEW |