| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 public: | 116 public: |
| 117 ResourceBundleImageSource(ResourceBundle* rb, int resource_id) | 117 ResourceBundleImageSource(ResourceBundle* rb, int resource_id) |
| 118 : rb_(rb), resource_id_(resource_id) {} | 118 : rb_(rb), resource_id_(resource_id) {} |
| 119 virtual ~ResourceBundleImageSource() {} | 119 virtual ~ResourceBundleImageSource() {} |
| 120 | 120 |
| 121 // gfx::ImageSkiaSource overrides: | 121 // gfx::ImageSkiaSource overrides: |
| 122 virtual gfx::ImageSkiaRep GetImageForScale( | 122 virtual gfx::ImageSkiaRep GetImageForScale( |
| 123 ui::ScaleFactor scale_factor) OVERRIDE { | 123 ui::ScaleFactor scale_factor) OVERRIDE { |
| 124 SkBitmap image; | 124 SkBitmap image; |
| 125 bool fell_back_to_1x = false; | 125 bool fell_back_to_1x = false; |
| 126 bool found = rb_->LoadBitmap(resource_id_, scale_factor, | 126 bool found = rb_->LoadBitmap(resource_id_, &scale_factor, |
| 127 &image, &fell_back_to_1x); | 127 &image, &fell_back_to_1x); |
| 128 if (!found) | 128 if (!found) |
| 129 return gfx::ImageSkiaRep(); | 129 return gfx::ImageSkiaRep(); |
| 130 | 130 |
| 131 if (fell_back_to_1x) { | 131 if (fell_back_to_1x) { |
| 132 // GRIT fell back to the 100% image, so rescale it to the correct size. | 132 // GRIT fell back to the 100% image, so rescale it to the correct size. |
| 133 float scale = GetScaleFactorScale(scale_factor); | 133 float scale = GetScaleFactorScale(scale_factor); |
| 134 image = skia::ImageOperations::Resize( | 134 image = skia::ImageOperations::Resize( |
| 135 image, | 135 image, |
| 136 skia::ImageOperations::RESIZE_LANCZOS3, | 136 skia::ImageOperations::RESIZE_LANCZOS3, |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 } | 368 } |
| 369 | 369 |
| 370 gfx::Image image; | 370 gfx::Image image; |
| 371 if (delegate_) | 371 if (delegate_) |
| 372 image = delegate_->GetImageNamed(resource_id); | 372 image = delegate_->GetImageNamed(resource_id); |
| 373 | 373 |
| 374 if (image.IsEmpty()) { | 374 if (image.IsEmpty()) { |
| 375 DCHECK(!delegate_ && !data_packs_.empty()) << | 375 DCHECK(!delegate_ && !data_packs_.empty()) << |
| 376 "Missing call to SetResourcesDataDLL?"; | 376 "Missing call to SetResourcesDataDLL?"; |
| 377 | 377 |
| 378 // TODO(oshima): This should be GetPrimaryDisplay().device_scale_factor(), | 378 // TODO(oshima): Consider reading the image size from png IHDR chunk and |
| 379 // but GetPrimaryDisplay() crashes at startup. | 379 // skip decoding here and remove #ifdef below. |
| 380 ScaleFactor primary_scale_factor = SCALE_FACTOR_100P; | |
| 381 // ResourceBundle::GetSharedInstance() is destroyed after the | 380 // ResourceBundle::GetSharedInstance() is destroyed after the |
| 382 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be | 381 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be |
| 383 // destroyed before the resource bundle is destroyed. | 382 // destroyed before the resource bundle is destroyed. |
| 383 #if defined(OS_CHROMEOS) |
| 384 ui::ScaleFactor scale_factor_to_load = ui::GetMaxScaleFactor(); |
| 385 #else |
| 386 ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; |
| 387 #endif |
| 384 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), | 388 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), |
| 385 primary_scale_factor); | 389 scale_factor_to_load); |
| 386 if (image_skia.isNull()) { | 390 if (image_skia.isNull()) { |
| 387 LOG(WARNING) << "Unable to load image with id " << resource_id; | 391 LOG(WARNING) << "Unable to load image with id " << resource_id; |
| 388 NOTREACHED(); // Want to assert in debug mode. | 392 NOTREACHED(); // Want to assert in debug mode. |
| 389 // The load failed to retrieve the image; show a debugging red square. | 393 // The load failed to retrieve the image; show a debugging red square. |
| 390 return GetEmptyImage(); | 394 return GetEmptyImage(); |
| 391 } | 395 } |
| 392 image_skia.SetReadOnly(); | 396 image_skia.SetReadOnly(); |
| 393 image = gfx::Image(image_skia); | 397 image = gfx::Image(image_skia); |
| 394 } | 398 } |
| 395 | 399 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 return data; | 449 return data; |
| 446 | 450 |
| 447 if (scale_factor != ui::SCALE_FACTOR_100P) { | 451 if (scale_factor != ui::SCALE_FACTOR_100P) { |
| 448 for (size_t i = 0; i < data_packs_.size(); i++) { | 452 for (size_t i = 0; i < data_packs_.size(); i++) { |
| 449 if (data_packs_[i]->GetScaleFactor() == scale_factor && | 453 if (data_packs_[i]->GetScaleFactor() == scale_factor && |
| 450 data_packs_[i]->GetStringPiece(resource_id, &data)) | 454 data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 451 return data; | 455 return data; |
| 452 } | 456 } |
| 453 } | 457 } |
| 454 for (size_t i = 0; i < data_packs_.size(); i++) { | 458 for (size_t i = 0; i < data_packs_.size(); i++) { |
| 455 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P && | 459 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P || |
| 460 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) && |
| 456 data_packs_[i]->GetStringPiece(resource_id, &data)) | 461 data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 457 return data; | 462 return data; |
| 458 } | 463 } |
| 459 | 464 |
| 460 return base::StringPiece(); | 465 return base::StringPiece(); |
| 461 } | 466 } |
| 462 | 467 |
| 463 string16 ResourceBundle::GetLocalizedString(int message_id) { | 468 string16 ResourceBundle::GetLocalizedString(int message_id) { |
| 464 string16 string; | 469 string16 string; |
| 465 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) | 470 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 AddDataPack(data_pack.release()); | 575 AddDataPack(data_pack.release()); |
| 571 } else if (!optional) { | 576 } else if (!optional) { |
| 572 LOG(ERROR) << "Failed to load " << pack_path.value() | 577 LOG(ERROR) << "Failed to load " << pack_path.value() |
| 573 << "\nSome features may not be available."; | 578 << "\nSome features may not be available."; |
| 574 } | 579 } |
| 575 } | 580 } |
| 576 | 581 |
| 577 void ResourceBundle::AddDataPack(DataPack* data_pack) { | 582 void ResourceBundle::AddDataPack(DataPack* data_pack) { |
| 578 data_packs_.push_back(data_pack); | 583 data_packs_.push_back(data_pack); |
| 579 | 584 |
| 580 #if defined(OS_CHROMEOS) | |
| 581 // When Chrome is running on desktop and force-device-scale-factor is not | |
| 582 // specified, use SCALE_FACTOR_100P as |max_scale_factor_|. | |
| 583 if (!base::chromeos::IsRunningOnChromeOS() && | |
| 584 !CommandLine::ForCurrentProcess()->HasSwitch( | |
| 585 switches::kForceDeviceScaleFactor)) | |
| 586 return; | |
| 587 #endif | |
| 588 | |
| 589 if (GetScaleFactorScale(data_pack->GetScaleFactor()) > | 585 if (GetScaleFactorScale(data_pack->GetScaleFactor()) > |
| 590 GetScaleFactorScale(max_scale_factor_)) | 586 GetScaleFactorScale(max_scale_factor_)) |
| 591 max_scale_factor_ = data_pack->GetScaleFactor(); | 587 max_scale_factor_ = data_pack->GetScaleFactor(); |
| 592 } | 588 } |
| 593 | 589 |
| 594 void ResourceBundle::LoadFontsIfNecessary() { | 590 void ResourceBundle::LoadFontsIfNecessary() { |
| 595 images_and_fonts_lock_->AssertAcquired(); | 591 images_and_fonts_lock_->AssertAcquired(); |
| 596 if (!base_font_.get()) { | 592 if (!base_font_.get()) { |
| 597 if (delegate_) { | 593 if (delegate_) { |
| 598 base_font_.reset(delegate_->GetFont(BaseFont).release()); | 594 base_font_.reset(delegate_->GetFont(BaseFont).release()); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 *fell_back_to_1x = false; | 663 *fell_back_to_1x = false; |
| 668 return true; | 664 return true; |
| 669 } | 665 } |
| 670 #endif | 666 #endif |
| 671 | 667 |
| 672 NOTREACHED() << "Unable to decode theme image resource " << resource_id; | 668 NOTREACHED() << "Unable to decode theme image resource " << resource_id; |
| 673 return false; | 669 return false; |
| 674 } | 670 } |
| 675 | 671 |
| 676 bool ResourceBundle::LoadBitmap(int resource_id, | 672 bool ResourceBundle::LoadBitmap(int resource_id, |
| 677 ScaleFactor scale_factor, | 673 ScaleFactor* scale_factor, |
| 678 SkBitmap* bitmap, | 674 SkBitmap* bitmap, |
| 679 bool* fell_back_to_1x) const { | 675 bool* fell_back_to_1x) const { |
| 680 DCHECK(fell_back_to_1x); | 676 DCHECK(fell_back_to_1x); |
| 681 for (size_t i = 0; i < data_packs_.size(); ++i) { | 677 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 682 if (data_packs_[i]->GetScaleFactor() == scale_factor) { | 678 // If the resource is in the package with SCALE_FACTOR_NONE, it |
| 683 if (LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) | 679 // can be used in any scale factor, but set 100P in ImageSkia so |
| 684 return true; | 680 // that it will be scaled property. |
| 681 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE && |
| 682 LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) { |
| 683 *scale_factor = ui::SCALE_FACTOR_100P; |
| 684 DCHECK(!*fell_back_to_1x); |
| 685 return true; |
| 686 } |
| 687 if (data_packs_[i]->GetScaleFactor() == *scale_factor && |
| 688 LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) { |
| 689 return true; |
| 685 } | 690 } |
| 686 } | 691 } |
| 687 return false; | 692 return false; |
| 688 } | 693 } |
| 689 | 694 |
| 690 gfx::Image& ResourceBundle::GetEmptyImage() { | 695 gfx::Image& ResourceBundle::GetEmptyImage() { |
| 691 base::AutoLock lock(*images_and_fonts_lock_); | 696 base::AutoLock lock(*images_and_fonts_lock_); |
| 692 | 697 |
| 693 if (empty_image_.IsEmpty()) { | 698 if (empty_image_.IsEmpty()) { |
| 694 // The placeholder bitmap is bright red so people notice the problem. | 699 // The placeholder bitmap is bright red so people notice the problem. |
| 695 SkBitmap bitmap; | 700 SkBitmap bitmap; |
| 696 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 701 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
| 697 bitmap.allocPixels(); | 702 bitmap.allocPixels(); |
| 698 bitmap.eraseARGB(255, 255, 0, 0); | 703 bitmap.eraseARGB(255, 255, 0, 0); |
| 699 empty_image_ = gfx::Image(bitmap); | 704 empty_image_ = gfx::Image(bitmap); |
| 700 } | 705 } |
| 701 return empty_image_; | 706 return empty_image_; |
| 702 } | 707 } |
| 703 | 708 |
| 704 } // namespace ui | 709 } // namespace ui |
| OLD | NEW |