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 14 matching lines...) Expand all Loading... |
25 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
26 #include "third_party/skia/include/core/SkCanvas.h" | 26 #include "third_party/skia/include/core/SkCanvas.h" |
27 #include "ui/base/resource/data_pack.h" | 27 #include "ui/base/resource/data_pack.h" |
28 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
29 #include "ui/gfx/canvas.h" | 29 #include "ui/gfx/canvas.h" |
30 #include "ui/gfx/codec/png_codec.h" | 30 #include "ui/gfx/codec/png_codec.h" |
31 #include "ui/gfx/image/canvas_image_source.h" | 31 #include "ui/gfx/image/canvas_image_source.h" |
32 #include "ui/gfx/image/image_skia.h" | 32 #include "ui/gfx/image/image_skia.h" |
33 #include "ui/gfx/image/image_skia_operations.h" | 33 #include "ui/gfx/image/image_skia_operations.h" |
34 #include "ui/gfx/screen.h" | 34 #include "ui/gfx/screen.h" |
| 35 #include "ui/gfx/size_conversions.h" |
35 #include "ui/gfx/skia_util.h" | 36 #include "ui/gfx/skia_util.h" |
36 | 37 |
37 using content::BrowserThread; | 38 using content::BrowserThread; |
38 using extensions::Extension; | 39 using extensions::Extension; |
39 | 40 |
40 namespace { | 41 namespace { |
41 | 42 |
42 // Version number of the current theme pack. We just throw out and rebuild | 43 // Version number of the current theme pack. We just throw out and rebuild |
43 // theme packs that aren't int-equal to this. Increment this number if you | 44 // theme packs that aren't int-equal to this. Increment this number if you |
44 // change default theme assets. | 45 // change default theme assets. |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 // ImageSkiaReps for all supported scale factors. | 576 // ImageSkiaReps for all supported scale factors. |
576 for (ImageCache::iterator it = pack->images_on_ui_thread_.begin(); | 577 for (ImageCache::iterator it = pack->images_on_ui_thread_.begin(); |
577 it != pack->images_on_ui_thread_.end(); ++it) { | 578 it != pack->images_on_ui_thread_.end(); ++it) { |
578 const gfx::ImageSkia source_image_skia = it->second.AsImageSkia(); | 579 const gfx::ImageSkia source_image_skia = it->second.AsImageSkia(); |
579 ThemeImageSource* source = new ThemeImageSource(source_image_skia); | 580 ThemeImageSource* source = new ThemeImageSource(source_image_skia); |
580 // image_skia takes ownership of source. | 581 // image_skia takes ownership of source. |
581 gfx::ImageSkia image_skia(source, source_image_skia.size()); | 582 gfx::ImageSkia image_skia(source, source_image_skia.size()); |
582 it->second = gfx::Image(image_skia); | 583 it->second = gfx::Image(image_skia); |
583 } | 584 } |
584 | 585 |
| 586 // Generate raw images (for new-tab-page attribution and background) for |
| 587 // any missing scale from an available scale image. |
| 588 for (size_t i = 0; i < arraysize(kPreloadIDs); ++i) { |
| 589 pack->GenerateRawImageForAllSupportedScales(kPreloadIDs[i]); |
| 590 } |
| 591 |
585 // The BrowserThemePack is now in a consistent state. | 592 // The BrowserThemePack is now in a consistent state. |
586 return pack; | 593 return pack; |
587 } | 594 } |
588 | 595 |
589 // static | 596 // static |
590 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( | 597 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( |
591 const base::FilePath& path, const std::string& expected_id) { | 598 const base::FilePath& path, const std::string& expected_id) { |
592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 599 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
593 // Allow IO on UI thread due to deep-seated theme design issues. | 600 // Allow IO on UI thread due to deep-seated theme design issues. |
594 // (see http://crbug.com/80206) | 601 // (see http://crbug.com/80206) |
(...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1436 float scale = static_cast<float>(percent) / 100.0f; | 1443 float scale = static_cast<float>(percent) / 100.0f; |
1437 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1444 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
1438 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) { | 1445 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) { |
1439 *scale_factor = scale_factors_[i]; | 1446 *scale_factor = scale_factors_[i]; |
1440 return true; | 1447 return true; |
1441 } | 1448 } |
1442 } | 1449 } |
1443 } | 1450 } |
1444 return false; | 1451 return false; |
1445 } | 1452 } |
| 1453 |
| 1454 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { |
| 1455 // Compute (by scaling) bitmaps for |prs_id| for any scale factors |
| 1456 // for which the theme author did not provide a bitmap. We compute |
| 1457 // the bitmaps using the highest scale factor that theme author |
| 1458 // provided. |
| 1459 // Note: We use only supported scale factors. For example, if scale |
| 1460 // factor 2x is supported by the current system, but 1.8x is not and |
| 1461 // if the theme author did not provide an image for 2x but one for |
| 1462 // 1.8x, we will not use the 1.8x image here. Here we will only use |
| 1463 // images provided for scale factors supported by the current system. |
| 1464 |
| 1465 // See if any image is missing. If not, we're done. |
| 1466 bool image_missing = false; |
| 1467 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| 1468 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| 1469 if (image_memory_.find(raw_id) == image_memory_.end()) { |
| 1470 image_missing = true; |
| 1471 break; |
| 1472 } |
| 1473 } |
| 1474 if (!image_missing) |
| 1475 return; |
| 1476 |
| 1477 // Find available scale factor with highest scale. |
| 1478 ui::ScaleFactor available = ui::SCALE_FACTOR_NONE; |
| 1479 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| 1480 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| 1481 if ((available == ui::SCALE_FACTOR_NONE || |
| 1482 (ui::GetScaleFactorScale(scale_factors_[i]) > |
| 1483 ui::GetScaleFactorScale(available))) && |
| 1484 image_memory_.find(raw_id) != image_memory_.end()) { |
| 1485 available = scale_factors_[i]; |
| 1486 } |
| 1487 } |
| 1488 // If no scale factor is available, we're done. |
| 1489 if (available == ui::SCALE_FACTOR_NONE) |
| 1490 return; |
| 1491 |
| 1492 // Get bitmap for the available scale factor. |
| 1493 int available_raw_id = GetRawIDByPersistentID(prs_id, available); |
| 1494 RawImages::const_iterator it = image_memory_.find(available_raw_id); |
| 1495 SkBitmap available_bitmap; |
| 1496 if (!gfx::PNGCodec::Decode(it->second->front(), |
| 1497 it->second->size(), |
| 1498 &available_bitmap)) { |
| 1499 NOTREACHED() << "Unable to decode theme image for prs_id=" |
| 1500 << prs_id << " for scale_factor=" << available; |
| 1501 return; |
| 1502 } |
| 1503 |
| 1504 // Fill in all missing scale factors by scaling the available bitmap. |
| 1505 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| 1506 int scaled_raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| 1507 if (image_memory_.find(scaled_raw_id) != image_memory_.end()) |
| 1508 continue; |
| 1509 gfx::Size scaled_size = gfx::ToCeiledSize( |
| 1510 gfx::ScaleSize(gfx::Size(available_bitmap.width(), |
| 1511 available_bitmap.height()), |
| 1512 ui::GetScaleFactorScale(scale_factors_[i]) / |
| 1513 ui::GetScaleFactorScale(available))); |
| 1514 SkBitmap scaled_bitmap; |
| 1515 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 1516 scaled_size.width(), |
| 1517 scaled_size.height()); |
| 1518 if (!scaled_bitmap.allocPixels()) |
| 1519 SK_CRASH(); |
| 1520 scaled_bitmap.eraseARGB(0, 0, 0, 0); |
| 1521 SkCanvas canvas(scaled_bitmap); |
| 1522 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); |
| 1523 canvas.drawBitmapRect(available_bitmap, NULL, scaled_bounds); |
| 1524 std::vector<unsigned char> bitmap_data; |
| 1525 if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap, |
| 1526 false, |
| 1527 &bitmap_data)) { |
| 1528 NOTREACHED() << "Unable to encode theme image for prs_id=" |
| 1529 << prs_id << " for scale_factor=" << scale_factors_[i]; |
| 1530 break; |
| 1531 } |
| 1532 image_memory_[scaled_raw_id] = |
| 1533 base::RefCountedBytes::TakeVector(&bitmap_data); |
| 1534 } |
| 1535 } |
OLD | NEW |