Chromium Code Reviews| Index: chrome/browser/themes/browser_theme_pack.cc |
| diff --git a/chrome/browser/themes/browser_theme_pack.cc b/chrome/browser/themes/browser_theme_pack.cc |
| index e2a71228b328f9049ffcea10887346c88a8ca267..b2cb84a08d6ae94241b1ae5fcf805c985c9b53d0 100644 |
| --- a/chrome/browser/themes/browser_theme_pack.cc |
| +++ b/chrome/browser/themes/browser_theme_pack.cc |
| @@ -32,6 +32,7 @@ |
| #include "ui/gfx/image/image_skia.h" |
| #include "ui/gfx/image/image_skia_operations.h" |
| #include "ui/gfx/screen.h" |
| +#include "ui/gfx/size_conversions.h" |
| #include "ui/gfx/skia_util.h" |
| using content::BrowserThread; |
| @@ -582,6 +583,11 @@ scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromExtension( |
| it->second = gfx::Image(image_skia); |
| } |
| + // Generate raw images (for new-tab-page attribution and background) for |
| + // any missing scale from an available scale image. |
| + pack->GenerateRawImageForAllScales(PRS_THEME_NTP_BACKGROUND); |
| + pack->GenerateRawImageForAllScales(PRS_THEME_NTP_ATTRIBUTION); |
| + |
| // The BrowserThemePack is now in a consistent state. |
| return pack; |
| } |
| @@ -1443,3 +1449,111 @@ bool BrowserThemePack::GetScaleFactorFromManifestKey( |
| } |
| return false; |
| } |
| + |
| +void BrowserThemePack::GenerateRawImageForAllScales(int prs_id) { |
|
pkotwicz
2013/06/07 16:15:26
Nit: Rename to GenerateRawImagesForAllSupportedSca
sschmitz
2013/06/07 21:13:54
Done.
|
| + // For any raw theme image for a scale that is not in the theme |
| + // extension we compute (by scaling) an image based on an available |
| + // image. For expediency we choose only one image to be used for |
| + // all missing scales. (On Chrome OS there are only two scales). |
| + // First we attempt to find an available image with a scale that is |
| + // the smallest scale larger than the largest missing scale. |
| + // If no such image is available, we attempt to find an available |
| + // image with a scale that is the largest scale smaller than the |
| + // highest missing scale. |
| + |
| + // Find highest missing scale. |
| + ui::ScaleFactor highest_missing = ui::SCALE_FACTOR_NONE; |
| + for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| + int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| + if ((highest_missing == ui::SCALE_FACTOR_NONE || |
| + (ui::GetScaleFactorScale(scale_factors_[i]) > |
| + ui::GetScaleFactorScale(highest_missing))) && |
| + image_memory_.find(raw_id) == image_memory_.end()) { |
| + highest_missing = scale_factors_[i]; |
| + } |
| + } |
| + // If no image is missing, we're done. |
| + if (highest_missing == ui::SCALE_FACTOR_NONE) |
| + return; |
| + |
| + // Find lowest available scale that is larger than highest missing. |
| + ui::ScaleFactor available = ui::SCALE_FACTOR_NONE; |
| + for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| + int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
|
pkotwicz
2013/06/07 16:15:26
I think you can just go and find the image for |pr
sschmitz
2013/06/07 21:13:54
Done.
|
| + if ((ui::GetScaleFactorScale(scale_factors_[i]) > |
| + ui::GetScaleFactorScale(highest_missing)) && |
| + (available == ui::SCALE_FACTOR_NONE || |
| + (ui::GetScaleFactorScale(scale_factors_[i]) < |
| + ui::GetScaleFactorScale(available))) && |
| + image_memory_.find(raw_id) != image_memory_.end()) { |
| + available = scale_factors_[i]; |
| + } |
| + } |
| + |
| + if (available == ui::SCALE_FACTOR_NONE) { |
| + // find the highest available scale that is smaller than highest missing |
| + for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| + int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| + if ((ui::GetScaleFactorScale(scale_factors_[i]) < |
| + ui::GetScaleFactorScale(highest_missing)) && |
| + (available == ui::SCALE_FACTOR_NONE || |
| + (ui::GetScaleFactorScale(scale_factors_[i]) > |
| + ui::GetScaleFactorScale(available))) && |
| + image_memory_.find(raw_id) != image_memory_.end()) { |
| + available = scale_factors_[i]; |
| + } |
| + } |
| + } |
| + |
| + // If no image is available, we're done. |
| + if (available == ui::SCALE_FACTOR_NONE) |
| + return; |
| + |
| + // Now fill in all missing scales. |
| + int available_raw_id = GetRawIDByPersistentID(prs_id, available); |
| + RawImages::const_iterator it = image_memory_.find(available_raw_id); |
| + DCHECK(it != image_memory_.end()); |
| + DCHECK(it->second->size() > 0); |
| + SkBitmap available_bitmap; |
| + for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| + if (scale_factors_[i] == available) |
|
pkotwicz
2013/06/07 16:15:26
You don't need the if on line 1519 because the if
sschmitz
2013/06/07 21:13:54
Done.
|
| + continue; |
| + int scaled_raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| + if (image_memory_.find(scaled_raw_id) != image_memory_.end()) |
| + continue; |
| + if (available_bitmap.isNull()) { |
| + if (!gfx::PNGCodec::Decode(it->second->front(), |
|
pkotwicz
2013/06/07 16:15:26
You know that you need |available_bitmap| because
sschmitz
2013/06/07 21:13:54
Done.
|
| + it->second->size(), |
| + &available_bitmap)) { |
| + NOTREACHED() << "Unable to decode theme image for prs_id=" |
| + << prs_id << " for scale_factor=" << available; |
| + break; |
| + } |
| + } |
| + gfx::Size scaled_size = gfx::ToCeiledSize( |
| + gfx::ScaleSize(gfx::Size(available_bitmap.width(), |
| + available_bitmap.height()), |
| + ui::GetScaleFactorScale(scale_factors_[i]) / |
| + ui::GetScaleFactorScale(available))); |
| + SkBitmap scaled_bitmap; |
| + scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| + scaled_size.width(), |
| + scaled_size.height()); |
| + if (!scaled_bitmap.allocPixels()) |
| + SK_CRASH(); |
| + scaled_bitmap.eraseARGB(0, 0, 0, 0); |
| + SkCanvas canvas(scaled_bitmap); |
| + SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); |
| + canvas.drawBitmapRect(available_bitmap, NULL, scaled_bounds); |
| + std::vector<unsigned char> bitmap_data; |
| + if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap, |
| + false, |
| + &bitmap_data)) { |
| + NOTREACHED() << "Unable to encode theme image for prs_id=" |
| + << prs_id << " for scale_factor=" << scale_factors_[i]; |
| + break; |
| + } |
| + image_memory_[scaled_raw_id] = |
| + base::RefCountedBytes::TakeVector(&bitmap_data); |
| + } |
| +} |