Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: ui/base/resource/resource_bundle.cc

Issue 11301007: Load the resources for max scale factor first. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: do this only for chromeos for now Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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(), 364 // TODO(oshima): Consider reading the image size from png IHDR chunk and
365 // but GetPrimaryDisplay() crashes at startup. 365 // skip decoding here and remove #ifdef below.
366 ScaleFactor primary_scale_factor = SCALE_FACTOR_100P;
367 // ResourceBundle::GetSharedInstance() is destroyed after the 366 // ResourceBundle::GetSharedInstance() is destroyed after the
368 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be 367 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be
369 // destroyed before the resource bundle is destroyed. 368 // destroyed before the resource bundle is destroyed.
369 #if defined(OS_CHROMEOS)
370 // Use |GetMaxScaleFactor()| instead of |max_scale_factor()|, so that
371 // a test can override it.
372 ui::ScaleFactor scale_factor_to_load = GetMaxScaleFactor();
373 #else
374 ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P;
375 #endif
370 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), 376 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id),
371 primary_scale_factor); 377 scale_factor_to_load);
372 if (image_skia.isNull()) { 378 if (image_skia.isNull()) {
373 LOG(WARNING) << "Unable to load image with id " << resource_id; 379 LOG(WARNING) << "Unable to load image with id " << resource_id;
374 NOTREACHED(); // Want to assert in debug mode. 380 NOTREACHED(); // Want to assert in debug mode.
375 // The load failed to retrieve the image; show a debugging red square. 381 // The load failed to retrieve the image; show a debugging red square.
376 return GetEmptyImage(); 382 return GetEmptyImage();
377 } 383 }
378 image_skia.SetReadOnly(); 384 image_skia.SetReadOnly();
379 image = gfx::Image(image_skia); 385 image = gfx::Image(image_skia);
380 } 386 }
381 387
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 return data; 437 return data;
432 438
433 if (scale_factor != ui::SCALE_FACTOR_100P) { 439 if (scale_factor != ui::SCALE_FACTOR_100P) {
434 for (size_t i = 0; i < data_packs_.size(); i++) { 440 for (size_t i = 0; i < data_packs_.size(); i++) {
435 if (data_packs_[i]->GetScaleFactor() == scale_factor && 441 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
436 data_packs_[i]->GetStringPiece(resource_id, &data)) 442 data_packs_[i]->GetStringPiece(resource_id, &data))
437 return data; 443 return data;
438 } 444 }
439 } 445 }
440 for (size_t i = 0; i < data_packs_.size(); i++) { 446 for (size_t i = 0; i < data_packs_.size(); i++) {
441 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P && 447 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
448 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
442 data_packs_[i]->GetStringPiece(resource_id, &data)) 449 data_packs_[i]->GetStringPiece(resource_id, &data))
443 return data; 450 return data;
444 } 451 }
445 452
446 return base::StringPiece(); 453 return base::StringPiece();
447 } 454 }
448 455
449 string16 ResourceBundle::GetLocalizedString(int message_id) { 456 string16 ResourceBundle::GetLocalizedString(int message_id) {
450 string16 string; 457 string16 string;
451 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) 458 if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 *fell_back_to_1x = false; 660 *fell_back_to_1x = false;
654 return true; 661 return true;
655 } 662 }
656 #endif 663 #endif
657 664
658 NOTREACHED() << "Unable to decode theme image resource " << resource_id; 665 NOTREACHED() << "Unable to decode theme image resource " << resource_id;
659 return false; 666 return false;
660 } 667 }
661 668
662 bool ResourceBundle::LoadBitmap(int resource_id, 669 bool ResourceBundle::LoadBitmap(int resource_id,
663 ScaleFactor scale_factor, 670 ScaleFactor* scale_factor,
664 SkBitmap* bitmap, 671 SkBitmap* bitmap,
665 bool* fell_back_to_1x) const { 672 bool* fell_back_to_1x) const {
666 DCHECK(fell_back_to_1x); 673 DCHECK(fell_back_to_1x);
667 for (size_t i = 0; i < data_packs_.size(); ++i) { 674 for (size_t i = 0; i < data_packs_.size(); ++i) {
668 if (data_packs_[i]->GetScaleFactor() == scale_factor) { 675 // 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)) 676 // can be used in any scale factor, but set 100P in ImageSkia so
670 return true; 677 // that it will be scaled property.
678 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE &&
679 LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) {
680 *scale_factor = ui::SCALE_FACTOR_100P;
681 DCHECK(!*fell_back_to_1x);
682 return true;
683 }
684 if (data_packs_[i]->GetScaleFactor() == *scale_factor &&
685 LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) {
686 return true;
671 } 687 }
672 } 688 }
673 return false; 689 return false;
674 } 690 }
675 691
676 gfx::Image& ResourceBundle::GetEmptyImage() { 692 gfx::Image& ResourceBundle::GetEmptyImage() {
677 base::AutoLock lock(*images_and_fonts_lock_); 693 base::AutoLock lock(*images_and_fonts_lock_);
678 694
679 if (empty_image_.IsEmpty()) { 695 if (empty_image_.IsEmpty()) {
680 // The placeholder bitmap is bright red so people notice the problem. 696 // The placeholder bitmap is bright red so people notice the problem.
681 SkBitmap bitmap; 697 SkBitmap bitmap;
682 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); 698 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
683 bitmap.allocPixels(); 699 bitmap.allocPixels();
684 bitmap.eraseARGB(255, 255, 0, 0); 700 bitmap.eraseARGB(255, 255, 0, 0);
685 empty_image_ = gfx::Image(bitmap); 701 empty_image_ = gfx::Image(bitmap);
686 } 702 }
687 return empty_image_; 703 return empty_image_;
688 } 704 }
689 705
690 } // namespace ui 706 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698