Chromium Code Reviews| 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 "chrome/browser/themes/browser_theme_pack.h" | 5 #include "chrome/browser/themes/browser_theme_pack.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 473 canvas.drawBitmapRect(rep_100p.sk_bitmap(), NULL, resized_bounds); | 473 canvas.drawBitmapRect(rep_100p.sk_bitmap(), NULL, resized_bounds); |
| 474 return gfx::ImageSkiaRep(resized_bitmap, scale_factor); | 474 return gfx::ImageSkiaRep(resized_bitmap, scale_factor); |
| 475 } | 475 } |
| 476 | 476 |
| 477 private: | 477 private: |
| 478 const gfx::ImageSkia source_; | 478 const gfx::ImageSkia source_; |
| 479 | 479 |
| 480 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource); | 480 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource); |
| 481 }; | 481 }; |
| 482 | 482 |
| 483 // An ImageSkiaSouce that delays decoding PNG data into bitmaps until | |
|
pkotwicz
2013/06/18 15:23:43
Nit: ImageSkiaSource
sschmitz
2013/06/18 18:20:29
Done.
| |
| 484 // needed and, if necessary, generates bitmaps for scale factors for | |
| 485 // which no PNG was provided. Once a PNG is decoded the resulting | |
| 486 // bitmap is stored for later use. | |
| 487 class ThemeImagePngSource : public gfx::ImageSkiaSource { | |
| 488 public: | |
| 489 ThemeImagePngSource(const base::WeakPtr<BrowserThemePack>& btp, int id) : | |
| 490 browser_theme_pack_(btp), | |
| 491 idr_id_(id) { | |
| 492 } | |
| 493 | |
| 494 virtual ~ThemeImagePngSource() {} | |
| 495 | |
| 496 private: | |
| 497 virtual gfx::ImageSkiaRep GetImageForScale( | |
| 498 ui::ScaleFactor scale_factor) OVERRIDE { | |
| 499 // Look up the scale factor in the bitmap map. If found return it. | |
| 500 BitmapMap::const_iterator exact_bitmap = bitmap_map_.find(scale_factor); | |
|
pkotwicz
2013/06/18 15:23:43
I personally like suffixing iterator variable name
sschmitz
2013/06/18 18:20:29
Done.
| |
| 501 if (exact_bitmap != bitmap_map_.end()) | |
| 502 return gfx::ImageSkiaRep(exact_bitmap->second, scale_factor); | |
| 503 | |
| 504 if (!browser_theme_pack_.get()) | |
| 505 return gfx::ImageSkiaRep(SkBitmap(), scale_factor); | |
|
pkotwicz
2013/06/18 15:23:43
Nit: just use the empty constructor for ImageSkiaR
sschmitz
2013/06/18 18:20:29
Done.
| |
| 506 | |
| 507 // Look up the scale factor in the browser theme pack. If found, | |
|
pkotwicz
2013/06/18 15:23:43
How about: "Look up the raw PNG data for |idr_id_|
sschmitz
2013/06/18 18:20:29
Done.
| |
| 508 // decode it, store the result in the bitmap map and return it. | |
| 509 scoped_refptr<base::RefCountedMemory> exact_memory = | |
| 510 browser_theme_pack_->GetRawData(idr_id_, scale_factor); | |
| 511 if (exact_memory.get()) { | |
| 512 SkBitmap bitmap; | |
| 513 if (!gfx::PNGCodec::Decode(exact_memory->front(), | |
| 514 exact_memory->size(), | |
| 515 &bitmap)) { | |
| 516 NOTREACHED(); | |
| 517 return gfx::ImageSkiaRep(SkBitmap(), scale_factor); | |
|
pkotwicz
2013/06/18 15:23:43
Nit: use the empty constructor
sschmitz
2013/06/18 18:20:29
Done.
| |
| 518 } | |
| 519 bitmap_map_[scale_factor] = bitmap; | |
| 520 return gfx::ImageSkiaRep(bitmap, scale_factor); | |
| 521 } | |
| 522 | |
| 523 // No exact match was found, find an available png with highest scale. | |
|
pkotwicz
2013/06/18 15:23:43
Nit: the highest scale factor
sschmitz
2013/06/18 18:20:29
I changed to: "for the scale factor that correspon
| |
| 524 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; | |
| 525 scoped_refptr<base::RefCountedMemory> available_png = | |
| 526 browser_theme_pack_->GetRawDataWithHighestScale( | |
| 527 idr_id_, | |
| 528 &available_scale_factor); | |
| 529 // No png found for this id, return null rep. | |
| 530 if (!available_png.get()) | |
| 531 return gfx::ImageSkiaRep(SkBitmap(), scale_factor); | |
| 532 | |
| 533 // Look up the found scale factor in the bitmap map. If not found, decode | |
| 534 // the corresponding png and store the result in bitmap map. | |
|
pkotwicz
2013/06/18 15:23:43
Nit: "the bitmap map" or "|bitmap_map_|".
sschmitz
2013/06/18 18:20:29
Done.
| |
| 535 BitmapMap::const_iterator available_bitmap = | |
| 536 bitmap_map_.find(available_scale_factor); | |
| 537 if (available_bitmap == bitmap_map_.end()) { | |
| 538 SkBitmap bitmap; | |
| 539 if (!gfx::PNGCodec::Decode(available_png->front(), | |
| 540 available_png->size(), | |
| 541 &bitmap)) { | |
| 542 NOTREACHED(); | |
| 543 return gfx::ImageSkiaRep(SkBitmap(), scale_factor); | |
| 544 } | |
| 545 bitmap_map_[available_scale_factor] = bitmap; | |
| 546 available_bitmap = bitmap_map_.find(available_scale_factor); | |
| 547 } | |
| 548 | |
| 549 // Use available bitmap to create bitmap for desired scale factor by | |
| 550 // scaling, store the result in the bitmap map and return it. | |
| 551 gfx::Size scaled_size = gfx::ToCeiledSize( | |
|
pkotwicz
2013/06/18 15:23:43
Nit: Pull out the resizing code into a function in
sschmitz
2013/06/18 18:20:29
Done.
| |
| 552 gfx::ScaleSize(gfx::Size(available_bitmap->second.width(), | |
| 553 available_bitmap->second.height()), | |
| 554 ui::GetScaleFactorScale(scale_factor) / | |
| 555 ui::GetScaleFactorScale(available_scale_factor))); | |
| 556 SkBitmap scaled_bitmap; | |
| 557 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
| 558 scaled_size.width(), | |
| 559 scaled_size.height()); | |
| 560 if (!scaled_bitmap.allocPixels()) | |
| 561 SK_CRASH(); | |
| 562 scaled_bitmap.eraseARGB(0, 0, 0, 0); | |
| 563 SkCanvas canvas(scaled_bitmap); | |
| 564 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); | |
| 565 canvas.drawBitmapRect(available_bitmap->second, NULL, scaled_bounds); | |
| 566 bitmap_map_[scale_factor] = scaled_bitmap; | |
| 567 return gfx::ImageSkiaRep(scaled_bitmap, scale_factor); | |
| 568 } | |
| 569 | |
| 570 private: | |
| 571 typedef std::map<ui::ScaleFactor, SkBitmap> BitmapMap; | |
| 572 base::WeakPtr<BrowserThemePack> browser_theme_pack_; | |
| 573 const int idr_id_; | |
| 574 BitmapMap bitmap_map_; | |
|
pkotwicz
2013/06/18 15:23:43
Nit: Move the typedef to the line above |bitmap_ma
sschmitz
2013/06/18 18:20:29
Done.
| |
| 575 | |
| 576 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource); | |
| 577 }; | |
| 578 | |
| 483 class TabBackgroundImageSource: public gfx::CanvasImageSource { | 579 class TabBackgroundImageSource: public gfx::CanvasImageSource { |
| 484 public: | 580 public: |
| 485 TabBackgroundImageSource(const gfx::ImageSkia& image_to_tint, | 581 TabBackgroundImageSource(const gfx::ImageSkia& image_to_tint, |
| 486 const gfx::ImageSkia& overlay, | 582 const gfx::ImageSkia& overlay, |
| 487 const color_utils::HSL& hsl_shift, | 583 const color_utils::HSL& hsl_shift, |
| 488 int vertical_offset) | 584 int vertical_offset) |
| 489 : gfx::CanvasImageSource(image_to_tint.size(), false), | 585 : gfx::CanvasImageSource(image_to_tint.size(), false), |
| 490 image_to_tint_(image_to_tint), | 586 image_to_tint_(image_to_tint), |
| 491 overlay_(overlay), | 587 overlay_(overlay), |
| 492 hsl_shift_(hsl_shift), | 588 hsl_shift_(hsl_shift), |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 513 | 609 |
| 514 private: | 610 private: |
| 515 const gfx::ImageSkia image_to_tint_; | 611 const gfx::ImageSkia image_to_tint_; |
| 516 const gfx::ImageSkia overlay_; | 612 const gfx::ImageSkia overlay_; |
| 517 const color_utils::HSL hsl_shift_; | 613 const color_utils::HSL hsl_shift_; |
| 518 const int vertical_offset_; | 614 const int vertical_offset_; |
| 519 | 615 |
| 520 DISALLOW_COPY_AND_ASSIGN(TabBackgroundImageSource); | 616 DISALLOW_COPY_AND_ASSIGN(TabBackgroundImageSource); |
| 521 }; | 617 }; |
| 522 | 618 |
| 619 class OrderByScale { | |
| 620 public: | |
| 621 bool operator()(const ui::ScaleFactor& a, | |
| 622 const ui::ScaleFactor& b) const { | |
| 623 return ui::GetScaleFactorScale(a) > ui::GetScaleFactorScale(b); | |
| 624 } | |
| 625 }; | |
| 626 | |
| 523 } // namespace | 627 } // namespace |
| 524 | 628 |
| 525 BrowserThemePack::~BrowserThemePack() { | 629 BrowserThemePack::~BrowserThemePack() { |
| 526 if (!data_pack_.get()) { | 630 if (!data_pack_.get()) { |
| 527 delete header_; | 631 delete header_; |
| 528 delete [] tints_; | 632 delete [] tints_; |
| 529 delete [] colors_; | 633 delete [] colors_; |
| 530 delete [] display_properties_; | 634 delete [] display_properties_; |
| 531 delete [] source_images_; | 635 delete [] source_images_; |
| 532 } | 636 } |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 742 gfx::Image BrowserThemePack::GetImageNamed(int idr_id) { | 846 gfx::Image BrowserThemePack::GetImageNamed(int idr_id) { |
| 743 int prs_id = GetPersistentIDByIDR(idr_id); | 847 int prs_id = GetPersistentIDByIDR(idr_id); |
| 744 if (prs_id == -1) | 848 if (prs_id == -1) |
| 745 return gfx::Image(); | 849 return gfx::Image(); |
| 746 | 850 |
| 747 // Check if the image is cached. | 851 // Check if the image is cached. |
| 748 ImageCache::const_iterator image_iter = images_on_ui_thread_.find(prs_id); | 852 ImageCache::const_iterator image_iter = images_on_ui_thread_.find(prs_id); |
| 749 if (image_iter != images_on_ui_thread_.end()) | 853 if (image_iter != images_on_ui_thread_.end()) |
| 750 return image_iter->second; | 854 return image_iter->second; |
| 751 | 855 |
| 752 // TODO(pkotwicz): Do something better than loading the bitmaps | 856 gfx::ImageSkia image_skia( |
|
pkotwicz
2013/06/18 15:23:43
Nit: Comment that the gfx::ImageSkia machinery wil
sschmitz
2013/06/18 18:20:29
Done.
| |
| 753 // for all the scale factors associated with |idr_id|. | 857 new ThemeImagePngSource(weak_ptr_factory_.GetWeakPtr(), idr_id), |
| 754 // See crbug.com/243831. | 858 ui::SCALE_FACTOR_100P); |
| 755 gfx::ImageSkia source_image_skia; | 859 gfx::Image ret = gfx::Image(image_skia); |
| 756 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 860 images_on_ui_thread_[prs_id] = ret; |
| 757 scoped_refptr<base::RefCountedMemory> memory = | 861 return ret; |
| 758 GetRawData(idr_id, scale_factors_[i]); | |
| 759 if (memory.get()) { | |
| 760 // Decode the PNG. | |
| 761 SkBitmap bitmap; | |
| 762 if (!gfx::PNGCodec::Decode(memory->front(), memory->size(), | |
| 763 &bitmap)) { | |
| 764 NOTREACHED() << "Unable to decode theme image resource " << idr_id | |
| 765 << " from saved DataPack."; | |
| 766 continue; | |
| 767 } | |
| 768 source_image_skia.AddRepresentation( | |
| 769 gfx::ImageSkiaRep(bitmap, scale_factors_[i])); | |
| 770 } | |
| 771 } | |
| 772 | |
| 773 if (!source_image_skia.isNull()) { | |
| 774 ThemeImageSource* source = new ThemeImageSource(source_image_skia); | |
| 775 gfx::ImageSkia image_skia(source, source_image_skia.size()); | |
| 776 gfx::Image ret = gfx::Image(image_skia); | |
| 777 images_on_ui_thread_[prs_id] = ret; | |
| 778 return ret; | |
| 779 } | |
| 780 return gfx::Image(); | |
| 781 } | 862 } |
| 782 | 863 |
| 783 base::RefCountedMemory* BrowserThemePack::GetRawData( | 864 base::RefCountedMemory* BrowserThemePack::GetRawData( |
| 784 int idr_id, | 865 int idr_id, |
| 785 ui::ScaleFactor scale_factor) const { | 866 ui::ScaleFactor scale_factor) const { |
| 786 base::RefCountedMemory* memory = NULL; | 867 base::RefCountedMemory* memory = NULL; |
| 787 int prs_id = GetPersistentIDByIDR(idr_id); | 868 int prs_id = GetPersistentIDByIDR(idr_id); |
| 788 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); | 869 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); |
| 789 | 870 |
| 790 if (raw_id != -1) { | 871 if (raw_id != -1) { |
| 791 if (data_pack_.get()) { | 872 if (data_pack_.get()) { |
| 792 memory = data_pack_->GetStaticMemory(raw_id); | 873 memory = data_pack_->GetStaticMemory(raw_id); |
| 793 } else { | 874 } else { |
| 794 RawImages::const_iterator it = image_memory_.find(raw_id); | 875 RawImages::const_iterator it = image_memory_.find(raw_id); |
| 795 if (it != image_memory_.end()) { | 876 if (it != image_memory_.end()) { |
| 796 memory = it->second.get(); | 877 memory = it->second.get(); |
| 797 } | 878 } |
| 798 } | 879 } |
| 799 } | 880 } |
| 800 | 881 |
| 801 return memory; | 882 return memory; |
| 802 } | 883 } |
| 803 | 884 |
| 885 base::RefCountedMemory* BrowserThemePack::GetRawDataWithHighestScale( | |
|
pkotwicz
2013/06/18 15:23:43
This is only used by ThemeImagePngSource. Can this
sschmitz
2013/06/18 18:20:29
Done.
| |
| 886 int idr_id, | |
| 887 ui::ScaleFactor* scale_factor) const { | |
| 888 for (std::vector<ui::ScaleFactor>::const_reverse_iterator it = | |
| 889 scale_factors_ordered_by_scale_.rbegin(); | |
| 890 it != scale_factors_ordered_by_scale_.rend(); ++it) { | |
| 891 if (base::RefCountedMemory* memory = GetRawData(idr_id, *it)) { | |
| 892 if (scale_factor) | |
| 893 *scale_factor = *it; | |
| 894 return memory; | |
| 895 } | |
| 896 } | |
| 897 return NULL; | |
| 898 } | |
| 899 | |
| 804 // static | 900 // static |
| 805 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { | 901 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { |
| 806 if (!result) | 902 if (!result) |
| 807 return; | 903 return; |
| 808 | 904 |
| 809 result->clear(); | 905 result->clear(); |
| 810 for (size_t i = 0; i < kPersistingImagesLength; ++i) | 906 for (size_t i = 0; i < kPersistingImagesLength; ++i) |
| 811 result->insert(kPersistingImages[i].idr_id); | 907 result->insert(kPersistingImages[i].idr_id); |
| 812 | 908 |
| 813 #if defined(OS_WIN) && defined(USE_AURA) | 909 #if defined(OS_WIN) && defined(USE_AURA) |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 830 return false; | 926 return false; |
| 831 } | 927 } |
| 832 | 928 |
| 833 // private: | 929 // private: |
| 834 | 930 |
| 835 BrowserThemePack::BrowserThemePack() | 931 BrowserThemePack::BrowserThemePack() |
| 836 : header_(NULL), | 932 : header_(NULL), |
| 837 tints_(NULL), | 933 tints_(NULL), |
| 838 colors_(NULL), | 934 colors_(NULL), |
| 839 display_properties_(NULL), | 935 display_properties_(NULL), |
| 840 source_images_(NULL) { | 936 source_images_(NULL), |
| 937 weak_ptr_factory_(this) { | |
| 841 scale_factors_ = ui::GetSupportedScaleFactors(); | 938 scale_factors_ = ui::GetSupportedScaleFactors(); |
|
pkotwicz
2013/06/18 15:23:43
Nit: The result of ui::GetSupportedScaleFactors()
sschmitz
2013/06/18 18:20:29
Done.
| |
| 939 scale_factors_ordered_by_scale_ = scale_factors_; | |
| 940 std::sort(scale_factors_ordered_by_scale_.begin(), | |
| 941 scale_factors_ordered_by_scale_.end(), | |
| 942 OrderByScale()); | |
| 842 } | 943 } |
| 843 | 944 |
| 844 void BrowserThemePack::BuildHeader(const Extension* extension) { | 945 void BrowserThemePack::BuildHeader(const Extension* extension) { |
| 845 header_ = new BrowserThemePackHeader; | 946 header_ = new BrowserThemePackHeader; |
| 846 header_->version = kThemePackVersion; | 947 header_->version = kThemePackVersion; |
| 847 | 948 |
| 848 // TODO(erg): Need to make this endian safe on other computers. Prerequisite | 949 // TODO(erg): Need to make this endian safe on other computers. Prerequisite |
| 849 // is that ui::DataPack removes this same check. | 950 // is that ui::DataPack removes this same check. |
| 850 #if defined(__BYTE_ORDER) | 951 #if defined(__BYTE_ORDER) |
| 851 // Linux check | 952 // Linux check |
| (...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1526 false, | 1627 false, |
| 1527 &bitmap_data)) { | 1628 &bitmap_data)) { |
| 1528 NOTREACHED() << "Unable to encode theme image for prs_id=" | 1629 NOTREACHED() << "Unable to encode theme image for prs_id=" |
| 1529 << prs_id << " for scale_factor=" << scale_factors_[i]; | 1630 << prs_id << " for scale_factor=" << scale_factors_[i]; |
| 1530 break; | 1631 break; |
| 1531 } | 1632 } |
| 1532 image_memory_[scaled_raw_id] = | 1633 image_memory_[scaled_raw_id] = |
| 1533 base::RefCountedBytes::TakeVector(&bitmap_data); | 1634 base::RefCountedBytes::TakeVector(&bitmap_data); |
| 1534 } | 1635 } |
| 1535 } | 1636 } |
| OLD | NEW |