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..81caaf771a480ed2ff41ccccada30bec1761386e 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. |
|
pkotwicz
2013/06/10 15:20:11
We should iterate through kPreloadIDs here.
sschmitz
2013/06/10 17:24:14
Done.
|
| + pack->GenerateRawImageForAllSupportedScales(PRS_THEME_NTP_BACKGROUND); |
| + pack->GenerateRawImageForAllSupportedScales(PRS_THEME_NTP_ATTRIBUTION); |
| + |
| // The BrowserThemePack is now in a consistent state. |
| return pack; |
| } |
| @@ -1443,3 +1449,83 @@ bool BrowserThemePack::GetScaleFactorFromManifestKey( |
| } |
| return false; |
| } |
| + |
| +void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { |
| + // For any raw theme image for a supported scale that is not in the |
|
pkotwicz
2013/06/10 15:20:11
How about:
Compute (by scaling) bitmaps for |prs_i
sschmitz
2013/06/10 17:24:14
Done.
|
| + // theme extension we compute (by scaling) an image based on an |
| + // available image. We use the available image with the highest |
| + // scale to compute the missing images. |
| + |
| + // See if any image is missing. If not, we're done. |
| + bool image_missing = false; |
| + for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| + int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| + if (image_memory_.find(raw_id) == image_memory_.end()) { |
| + image_missing = true; |
| + break; |
| + } |
| + } |
| + if (!image_missing) |
| + return; |
| + |
| + // Find available scale factor with highest scale. |
| + 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]); |
| + if ((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; |
| + |
| + // Get bitmap of the available image. |
|
pkotwicz
2013/06/10 15:20:11
Nit: How about:
Get the bitmap for the available s
sschmitz
2013/06/10 17:24:14
Done.
|
| + 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); |
|
pkotwicz
2013/06/10 15:20:11
The above DCHECK is not necessary. gfx::PNGCodec::
sschmitz
2013/06/10 17:24:14
Actually, we don't need either one.
Done.
|
| + SkBitmap available_bitmap; |
| + if (!gfx::PNGCodec::Decode(it->second->front(), |
| + it->second->size(), |
| + &available_bitmap)) { |
| + NOTREACHED() << "Unable to decode theme image for prs_id=" |
| + << prs_id << " for scale_factor=" << available; |
| + return; |
| + } |
| + |
| + // Fill in all missing scales by scaling the available image. |
|
pkotwicz
2013/06/10 15:20:11
Nit scales -> scale factors
available image ->
sschmitz
2013/06/10 17:24:14
Done.
|
| + for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| + int scaled_raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| + if (image_memory_.find(scaled_raw_id) != image_memory_.end()) |
| + continue; |
| + 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); |
| + } |
| +} |