| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 } | 383 } |
| 384 | 384 |
| 385 gfx::Image image; | 385 gfx::Image image; |
| 386 if (delegate_) | 386 if (delegate_) |
| 387 image = delegate_->GetImageNamed(resource_id); | 387 image = delegate_->GetImageNamed(resource_id); |
| 388 | 388 |
| 389 if (image.IsEmpty()) { | 389 if (image.IsEmpty()) { |
| 390 DCHECK(!data_packs_.empty()) << | 390 DCHECK(!data_packs_.empty()) << |
| 391 "Missing call to SetResourcesDataDLL?"; | 391 "Missing call to SetResourcesDataDLL?"; |
| 392 | 392 |
| 393 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 393 #if defined(OS_CHROMEOS) |
| 394 ui::ScaleFactor scale_factor_to_load = GetMaxScaleFactor(); | 394 ui::ScaleFactor scale_factor_to_load = GetMaxScaleFactor(); |
| 395 #elif defined(OS_WIN) |
| 396 ui::ScaleFactor scale_factor_to_load = |
| 397 gfx::GetDPIScale() > 1.25 ? GetMaxScaleFactor() : ui::SCALE_FACTOR_100P; |
| 395 #else | 398 #else |
| 396 ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; | 399 ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; |
| 397 #endif | 400 #endif |
| 398 | |
| 399 // TODO(oshima): Consider reading the image size from png IHDR chunk and | 401 // TODO(oshima): Consider reading the image size from png IHDR chunk and |
| 400 // skip decoding here and remove #ifdef below. | 402 // skip decoding here and remove #ifdef below. |
| 401 // ResourceBundle::GetSharedInstance() is destroyed after the | 403 // ResourceBundle::GetSharedInstance() is destroyed after the |
| 402 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be | 404 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be |
| 403 // destroyed before the resource bundle is destroyed. | 405 // destroyed before the resource bundle is destroyed. |
| 404 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), | 406 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), |
| 405 GetScaleForScaleFactor(scale_factor_to_load)); | 407 GetScaleForScaleFactor(scale_factor_to_load)); |
| 406 if (image_skia.isNull()) { | 408 if (image_skia.isNull()) { |
| 407 LOG(WARNING) << "Unable to load image with id " << resource_id; | 409 LOG(WARNING) << "Unable to load image with id " << resource_id; |
| 408 NOTREACHED(); // Want to assert in debug mode. | 410 NOTREACHED(); // Want to assert in debug mode. |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 return GetFontList(style).GetPrimaryFont(); | 605 return GetFontList(style).GetPrimaryFont(); |
| 604 } | 606 } |
| 605 | 607 |
| 606 void ResourceBundle::ReloadFonts() { | 608 void ResourceBundle::ReloadFonts() { |
| 607 base::AutoLock lock_scope(*images_and_fonts_lock_); | 609 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 608 InitDefaultFontList(); | 610 InitDefaultFontList(); |
| 609 font_cache_.clear(); | 611 font_cache_.clear(); |
| 610 } | 612 } |
| 611 | 613 |
| 612 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { | 614 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { |
| 613 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 615 #if defined(OS_CHROMEOS) || defined(OS_WIN) || defined(OS_LINUX) |
| 614 return max_scale_factor_; | 616 return max_scale_factor_; |
| 615 #else | 617 #else |
| 616 return GetSupportedScaleFactors().back(); | 618 return GetSupportedScaleFactors().back(); |
| 617 #endif | 619 #endif |
| 618 } | 620 } |
| 619 | 621 |
| 620 bool ResourceBundle::IsScaleFactorSupported(ScaleFactor scale_factor) { | 622 bool ResourceBundle::IsScaleFactorSupported(ScaleFactor scale_factor) { |
| 621 const std::vector<ScaleFactor>& supported_scale_factors = | 623 const std::vector<ScaleFactor>& supported_scale_factors = |
| 622 ui::GetSupportedScaleFactors(); | 624 ui::GetSupportedScaleFactors(); |
| 623 return std::find(supported_scale_factors.begin(), | 625 return std::find(supported_scale_factors.begin(), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 635 ResourceBundle::~ResourceBundle() { | 637 ResourceBundle::~ResourceBundle() { |
| 636 FreeImages(); | 638 FreeImages(); |
| 637 UnloadLocaleResources(); | 639 UnloadLocaleResources(); |
| 638 } | 640 } |
| 639 | 641 |
| 640 // static | 642 // static |
| 641 void ResourceBundle::InitSharedInstance(Delegate* delegate) { | 643 void ResourceBundle::InitSharedInstance(Delegate* delegate) { |
| 642 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 644 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
| 643 g_shared_instance_ = new ResourceBundle(delegate); | 645 g_shared_instance_ = new ResourceBundle(delegate); |
| 644 static std::vector<ScaleFactor> supported_scale_factors; | 646 static std::vector<ScaleFactor> supported_scale_factors; |
| 645 #if !defined(OS_IOS) && !defined(OS_WIN) | 647 #if !defined(OS_IOS) |
| 646 // On platforms other than iOS, 100P is always a supported scale factor. | 648 // On platforms other than iOS, 100P is always a supported scale factor. |
| 647 // For Windows we have a separate case in this function. | 649 // For Windows we have a separate case in this function. |
| 648 supported_scale_factors.push_back(SCALE_FACTOR_100P); | 650 supported_scale_factors.push_back(SCALE_FACTOR_100P); |
| 649 #endif | 651 #endif |
| 650 #if defined(OS_ANDROID) | 652 #if defined(OS_ANDROID) |
| 651 float display_density; | 653 float display_density; |
| 652 if (gfx::Display::HasForceDeviceScaleFactor()) { | 654 if (gfx::Display::HasForceDeviceScaleFactor()) { |
| 653 display_density = gfx::Display::GetForcedDeviceScaleFactor(); | 655 display_density = gfx::Display::GetForcedDeviceScaleFactor(); |
| 654 } else { | 656 } else { |
| 655 gfx::DeviceDisplayInfo device_info; | 657 gfx::DeviceDisplayInfo device_info; |
| 656 display_density = device_info.GetDIPScale(); | 658 display_density = device_info.GetDIPScale(); |
| 657 } | 659 } |
| 658 const ScaleFactor closest = FindClosestScaleFactorUnsafe(display_density); | 660 const ScaleFactor closest = FindClosestScaleFactorUnsafe(display_density); |
| 659 if (closest != SCALE_FACTOR_100P) | 661 if (closest != SCALE_FACTOR_100P) |
| 660 supported_scale_factors.push_back(closest); | 662 supported_scale_factors.push_back(closest); |
| 661 #elif defined(OS_IOS) | 663 #elif defined(OS_IOS) |
| 662 gfx::Display display = gfx::Screen::GetScreen()->GetPrimaryDisplay(); | 664 gfx::Display display = gfx::Screen::GetScreen()->GetPrimaryDisplay(); |
| 663 if (display.device_scale_factor() > 2.0) { | 665 if (display.device_scale_factor() > 2.0) { |
| 664 DCHECK_EQ(3.0, display.device_scale_factor()); | 666 DCHECK_EQ(3.0, display.device_scale_factor()); |
| 665 supported_scale_factors.push_back(SCALE_FACTOR_300P); | 667 supported_scale_factors.push_back(SCALE_FACTOR_300P); |
| 666 } else if (display.device_scale_factor() > 1.0) { | 668 } else if (display.device_scale_factor() > 1.0) { |
| 667 DCHECK_EQ(2.0, display.device_scale_factor()); | 669 DCHECK_EQ(2.0, display.device_scale_factor()); |
| 668 supported_scale_factors.push_back(SCALE_FACTOR_200P); | 670 supported_scale_factors.push_back(SCALE_FACTOR_200P); |
| 669 } else { | 671 } else { |
| 670 supported_scale_factors.push_back(SCALE_FACTOR_100P); | 672 supported_scale_factors.push_back(SCALE_FACTOR_100P); |
| 671 } | 673 } |
| 672 #elif defined(OS_MACOSX) | 674 #elif defined(OS_MACOSX) |
| 673 if (base::mac::IsOSLionOrLater()) | 675 if (base::mac::IsOSLionOrLater()) |
| 674 supported_scale_factors.push_back(SCALE_FACTOR_200P); | 676 supported_scale_factors.push_back(SCALE_FACTOR_200P); |
| 675 #elif defined(OS_CHROMEOS) | 677 #elif defined(OS_CHROMEOS) || defined(OS_LINUX) || defined(OS_WIN) |
| 676 // TODO(oshima): Include 200P only if the device support 200P | |
| 677 supported_scale_factors.push_back(SCALE_FACTOR_200P); | 678 supported_scale_factors.push_back(SCALE_FACTOR_200P); |
| 678 #elif defined(OS_LINUX) && BUILDFLAG(ENABLE_HIDPI) | |
| 679 supported_scale_factors.push_back(SCALE_FACTOR_200P); | |
| 680 #elif defined(OS_WIN) | |
| 681 bool default_to_100P = true; | |
| 682 // On Windows if the dpi scale is greater than 1.25 on high dpi machines | |
| 683 // downscaling from 200 percent looks better than scaling up from 100 | |
| 684 // percent. | |
| 685 if (gfx::GetDPIScale() > 1.25) { | |
| 686 supported_scale_factors.push_back(SCALE_FACTOR_200P); | |
| 687 default_to_100P = false; | |
| 688 } | |
| 689 if (default_to_100P) | |
| 690 supported_scale_factors.push_back(SCALE_FACTOR_100P); | |
| 691 #endif | 679 #endif |
| 692 ui::SetSupportedScaleFactors(supported_scale_factors); | 680 ui::SetSupportedScaleFactors(supported_scale_factors); |
| 693 } | 681 } |
| 694 | 682 |
| 695 void ResourceBundle::FreeImages() { | 683 void ResourceBundle::FreeImages() { |
| 696 images_.clear(); | 684 images_.clear(); |
| 697 } | 685 } |
| 698 | 686 |
| 699 void ResourceBundle::LoadChromeResources() { | 687 void ResourceBundle::LoadChromeResources() { |
| 700 // The material design data packs contain some of the same asset IDs as in | 688 // The material design data packs contain some of the same asset IDs as in |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 883 // static | 871 // static |
| 884 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 872 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
| 885 size_t size, | 873 size_t size, |
| 886 SkBitmap* bitmap, | 874 SkBitmap* bitmap, |
| 887 bool* fell_back_to_1x) { | 875 bool* fell_back_to_1x) { |
| 888 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 876 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
| 889 return gfx::PNGCodec::Decode(buf, size, bitmap); | 877 return gfx::PNGCodec::Decode(buf, size, bitmap); |
| 890 } | 878 } |
| 891 | 879 |
| 892 } // namespace ui | 880 } // namespace ui |
| OLD | NEW |