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" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 public: | 102 public: |
103 ResourceBundleImageSource(ResourceBundle* rb, int resource_id) | 103 ResourceBundleImageSource(ResourceBundle* rb, int resource_id) |
104 : rb_(rb), resource_id_(resource_id) {} | 104 : rb_(rb), resource_id_(resource_id) {} |
105 virtual ~ResourceBundleImageSource() {} | 105 virtual ~ResourceBundleImageSource() {} |
106 | 106 |
107 // gfx::ImageSkiaSource overrides: | 107 // gfx::ImageSkiaSource overrides: |
108 virtual gfx::ImageSkiaRep GetImageForScale( | 108 virtual gfx::ImageSkiaRep GetImageForScale( |
109 ui::ScaleFactor scale_factor) OVERRIDE { | 109 ui::ScaleFactor scale_factor) OVERRIDE { |
110 SkBitmap image; | 110 SkBitmap image; |
111 bool fell_back_to_1x = false; | 111 bool fell_back_to_1x = false; |
112 bool found = rb_->LoadBitmap(resource_id_, scale_factor, | 112 bool found = rb_->LoadBitmap(resource_id_, &scale_factor, |
113 &image, &fell_back_to_1x); | 113 &image, &fell_back_to_1x); |
114 if (!found) | 114 if (!found) |
115 return gfx::ImageSkiaRep(); | 115 return gfx::ImageSkiaRep(); |
116 | 116 |
117 if (fell_back_to_1x) { | 117 if (fell_back_to_1x) { |
118 // GRIT fell back to the 100% image, so rescale it to the correct size. | 118 // GRIT fell back to the 100% image, so rescale it to the correct size. |
119 float scale = GetScaleFactorScale(scale_factor); | 119 float scale = GetScaleFactorScale(scale_factor); |
120 image = skia::ImageOperations::Resize( | 120 image = skia::ImageOperations::Resize( |
121 image, | 121 image, |
122 skia::ImageOperations::RESIZE_LANCZOS3, | 122 skia::ImageOperations::RESIZE_LANCZOS3, |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 } | 354 } |
355 | 355 |
356 gfx::Image image; | 356 gfx::Image image; |
357 if (delegate_) | 357 if (delegate_) |
358 image = delegate_->GetImageNamed(resource_id); | 358 image = delegate_->GetImageNamed(resource_id); |
359 | 359 |
360 if (image.IsEmpty()) { | 360 if (image.IsEmpty()) { |
361 DCHECK(!delegate_ && !data_packs_.empty()) << | 361 DCHECK(!delegate_ && !data_packs_.empty()) << |
362 "Missing call to SetResourcesDataDLL?"; | 362 "Missing call to SetResourcesDataDLL?"; |
363 | 363 |
364 // TODO(oshima): This should be GetPrimaryDisplay().device_scale_factor(), | |
365 // but GetPrimaryDisplay() crashes at startup. | |
366 ScaleFactor primary_scale_factor = SCALE_FACTOR_100P; | |
367 // ResourceBundle::GetSharedInstance() is destroyed after the | 364 // ResourceBundle::GetSharedInstance() is destroyed after the |
368 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be | 365 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be |
369 // destroyed before the resource bundle is destroyed. | 366 // destroyed before the resource bundle is destroyed. |
370 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), | 367 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), |
371 primary_scale_factor); | 368 max_scale_factor()); |
372 if (image_skia.isNull()) { | 369 if (image_skia.isNull()) { |
373 LOG(WARNING) << "Unable to load image with id " << resource_id; | 370 LOG(WARNING) << "Unable to load image with id " << resource_id; |
374 NOTREACHED(); // Want to assert in debug mode. | 371 NOTREACHED(); // Want to assert in debug mode. |
375 // The load failed to retrieve the image; show a debugging red square. | 372 // The load failed to retrieve the image; show a debugging red square. |
376 return GetEmptyImage(); | 373 return GetEmptyImage(); |
377 } | 374 } |
378 image_skia.SetReadOnly(); | 375 image_skia.SetReadOnly(); |
379 image = gfx::Image(image_skia); | 376 image = gfx::Image(image_skia); |
380 } | 377 } |
381 | 378 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 return data; | 428 return data; |
432 | 429 |
433 if (scale_factor != ui::SCALE_FACTOR_100P) { | 430 if (scale_factor != ui::SCALE_FACTOR_100P) { |
434 for (size_t i = 0; i < data_packs_.size(); i++) { | 431 for (size_t i = 0; i < data_packs_.size(); i++) { |
435 if (data_packs_[i]->GetScaleFactor() == scale_factor && | 432 if (data_packs_[i]->GetScaleFactor() == scale_factor && |
436 data_packs_[i]->GetStringPiece(resource_id, &data)) | 433 data_packs_[i]->GetStringPiece(resource_id, &data)) |
437 return data; | 434 return data; |
438 } | 435 } |
439 } | 436 } |
440 for (size_t i = 0; i < data_packs_.size(); i++) { | 437 for (size_t i = 0; i < data_packs_.size(); i++) { |
441 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P && | 438 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P || |
| 439 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) && |
442 data_packs_[i]->GetStringPiece(resource_id, &data)) | 440 data_packs_[i]->GetStringPiece(resource_id, &data)) |
443 return data; | 441 return data; |
444 } | 442 } |
445 | 443 |
446 return base::StringPiece(); | 444 return base::StringPiece(); |
447 } | 445 } |
448 | 446 |
449 string16 ResourceBundle::GetLocalizedString(int message_id) { | 447 string16 ResourceBundle::GetLocalizedString(int message_id) { |
450 string16 string; | 448 string16 string; |
451 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) | 449 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 *fell_back_to_1x = false; | 651 *fell_back_to_1x = false; |
654 return true; | 652 return true; |
655 } | 653 } |
656 #endif | 654 #endif |
657 | 655 |
658 NOTREACHED() << "Unable to decode theme image resource " << resource_id; | 656 NOTREACHED() << "Unable to decode theme image resource " << resource_id; |
659 return false; | 657 return false; |
660 } | 658 } |
661 | 659 |
662 bool ResourceBundle::LoadBitmap(int resource_id, | 660 bool ResourceBundle::LoadBitmap(int resource_id, |
663 ScaleFactor scale_factor, | 661 ScaleFactor* scale_factor, |
664 SkBitmap* bitmap, | 662 SkBitmap* bitmap, |
665 bool* fell_back_to_1x) const { | 663 bool* fell_back_to_1x) const { |
666 DCHECK(fell_back_to_1x); | 664 DCHECK(fell_back_to_1x); |
667 for (size_t i = 0; i < data_packs_.size(); ++i) { | 665 for (size_t i = 0; i < data_packs_.size(); ++i) { |
668 if (data_packs_[i]->GetScaleFactor() == scale_factor) { | 666 // If the resource is in the package with SCALE_FACTOR_NONE, it |
669 if (LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) | 667 // can be used in any scale factor, but set 100P in ImageSkia so |
670 return true; | 668 // that it will be scaled property. |
| 669 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE && |
| 670 LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) { |
| 671 *scale_factor = ui::SCALE_FACTOR_100P; |
| 672 DCHECK(!*fell_back_to_1x); |
| 673 return true; |
| 674 } |
| 675 if (data_packs_[i]->GetScaleFactor() == *scale_factor && |
| 676 LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) { |
| 677 return true; |
671 } | 678 } |
672 } | 679 } |
673 return false; | 680 return false; |
674 } | 681 } |
675 | 682 |
676 gfx::Image& ResourceBundle::GetEmptyImage() { | 683 gfx::Image& ResourceBundle::GetEmptyImage() { |
677 base::AutoLock lock(*images_and_fonts_lock_); | 684 base::AutoLock lock(*images_and_fonts_lock_); |
678 | 685 |
679 if (empty_image_.IsEmpty()) { | 686 if (empty_image_.IsEmpty()) { |
680 // The placeholder bitmap is bright red so people notice the problem. | 687 // The placeholder bitmap is bright red so people notice the problem. |
681 SkBitmap bitmap; | 688 SkBitmap bitmap; |
682 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 689 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
683 bitmap.allocPixels(); | 690 bitmap.allocPixels(); |
684 bitmap.eraseARGB(255, 255, 0, 0); | 691 bitmap.eraseARGB(255, 255, 0, 0); |
685 empty_image_ = gfx::Image(bitmap); | 692 empty_image_ = gfx::Image(bitmap); |
686 } | 693 } |
687 return empty_image_; | 694 return empty_image_; |
688 } | 695 } |
689 | 696 |
690 } // namespace ui | 697 } // namespace ui |
OLD | NEW |