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 pack->GenerateRawImageForAllScales(PRS_THEME_NTP_BACKGROUND); | |
589 pack->GenerateRawImageForAllScales(PRS_THEME_NTP_ATTRIBUTION); | |
590 | |
585 // The BrowserThemePack is now in a consistent state. | 591 // The BrowserThemePack is now in a consistent state. |
586 return pack; | 592 return pack; |
587 } | 593 } |
588 | 594 |
589 // static | 595 // static |
590 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( | 596 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( |
591 const base::FilePath& path, const std::string& expected_id) { | 597 const base::FilePath& path, const std::string& expected_id) { |
592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 598 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
593 // Allow IO on UI thread due to deep-seated theme design issues. | 599 // Allow IO on UI thread due to deep-seated theme design issues. |
594 // (see http://crbug.com/80206) | 600 // (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; | 1442 float scale = static_cast<float>(percent) / 100.0f; |
1437 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1443 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
1438 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) { | 1444 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) { |
1439 *scale_factor = scale_factors_[i]; | 1445 *scale_factor = scale_factors_[i]; |
1440 return true; | 1446 return true; |
1441 } | 1447 } |
1442 } | 1448 } |
1443 } | 1449 } |
1444 return false; | 1450 return false; |
1445 } | 1451 } |
1452 | |
1453 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.
| |
1454 // For any raw theme image for a scale that is not in the theme | |
1455 // extension we compute (by scaling) an image based on an available | |
1456 // image. For expediency we choose only one image to be used for | |
1457 // all missing scales. (On Chrome OS there are only two scales). | |
1458 // First we attempt to find an available image with a scale that is | |
1459 // the smallest scale larger than the largest missing scale. | |
1460 // If no such image is available, we attempt to find an available | |
1461 // image with a scale that is the largest scale smaller than the | |
1462 // highest missing scale. | |
1463 | |
1464 // Find highest missing scale. | |
1465 ui::ScaleFactor highest_missing = ui::SCALE_FACTOR_NONE; | |
1466 for (size_t i = 0; i < scale_factors_.size(); ++i) { | |
1467 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); | |
1468 if ((highest_missing == ui::SCALE_FACTOR_NONE || | |
1469 (ui::GetScaleFactorScale(scale_factors_[i]) > | |
1470 ui::GetScaleFactorScale(highest_missing))) && | |
1471 image_memory_.find(raw_id) == image_memory_.end()) { | |
1472 highest_missing = scale_factors_[i]; | |
1473 } | |
1474 } | |
1475 // If no image is missing, we're done. | |
1476 if (highest_missing == ui::SCALE_FACTOR_NONE) | |
1477 return; | |
1478 | |
1479 // Find lowest available scale that is larger than highest missing. | |
1480 ui::ScaleFactor available = ui::SCALE_FACTOR_NONE; | |
1481 for (size_t i = 0; i < scale_factors_.size(); ++i) { | |
1482 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.
| |
1483 if ((ui::GetScaleFactorScale(scale_factors_[i]) > | |
1484 ui::GetScaleFactorScale(highest_missing)) && | |
1485 (available == ui::SCALE_FACTOR_NONE || | |
1486 (ui::GetScaleFactorScale(scale_factors_[i]) < | |
1487 ui::GetScaleFactorScale(available))) && | |
1488 image_memory_.find(raw_id) != image_memory_.end()) { | |
1489 available = scale_factors_[i]; | |
1490 } | |
1491 } | |
1492 | |
1493 if (available == ui::SCALE_FACTOR_NONE) { | |
1494 // find the highest available scale that is smaller than highest missing | |
1495 for (size_t i = 0; i < scale_factors_.size(); ++i) { | |
1496 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); | |
1497 if ((ui::GetScaleFactorScale(scale_factors_[i]) < | |
1498 ui::GetScaleFactorScale(highest_missing)) && | |
1499 (available == ui::SCALE_FACTOR_NONE || | |
1500 (ui::GetScaleFactorScale(scale_factors_[i]) > | |
1501 ui::GetScaleFactorScale(available))) && | |
1502 image_memory_.find(raw_id) != image_memory_.end()) { | |
1503 available = scale_factors_[i]; | |
1504 } | |
1505 } | |
1506 } | |
1507 | |
1508 // If no image is available, we're done. | |
1509 if (available == ui::SCALE_FACTOR_NONE) | |
1510 return; | |
1511 | |
1512 // Now fill in all missing scales. | |
1513 int available_raw_id = GetRawIDByPersistentID(prs_id, available); | |
1514 RawImages::const_iterator it = image_memory_.find(available_raw_id); | |
1515 DCHECK(it != image_memory_.end()); | |
1516 DCHECK(it->second->size() > 0); | |
1517 SkBitmap available_bitmap; | |
1518 for (size_t i = 0; i < scale_factors_.size(); ++i) { | |
1519 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.
| |
1520 continue; | |
1521 int scaled_raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); | |
1522 if (image_memory_.find(scaled_raw_id) != image_memory_.end()) | |
1523 continue; | |
1524 if (available_bitmap.isNull()) { | |
1525 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.
| |
1526 it->second->size(), | |
1527 &available_bitmap)) { | |
1528 NOTREACHED() << "Unable to decode theme image for prs_id=" | |
1529 << prs_id << " for scale_factor=" << available; | |
1530 break; | |
1531 } | |
1532 } | |
1533 gfx::Size scaled_size = gfx::ToCeiledSize( | |
1534 gfx::ScaleSize(gfx::Size(available_bitmap.width(), | |
1535 available_bitmap.height()), | |
1536 ui::GetScaleFactorScale(scale_factors_[i]) / | |
1537 ui::GetScaleFactorScale(available))); | |
1538 SkBitmap scaled_bitmap; | |
1539 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
1540 scaled_size.width(), | |
1541 scaled_size.height()); | |
1542 if (!scaled_bitmap.allocPixels()) | |
1543 SK_CRASH(); | |
1544 scaled_bitmap.eraseARGB(0, 0, 0, 0); | |
1545 SkCanvas canvas(scaled_bitmap); | |
1546 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); | |
1547 canvas.drawBitmapRect(available_bitmap, NULL, scaled_bounds); | |
1548 std::vector<unsigned char> bitmap_data; | |
1549 if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap, | |
1550 false, | |
1551 &bitmap_data)) { | |
1552 NOTREACHED() << "Unable to encode theme image for prs_id=" | |
1553 << prs_id << " for scale_factor=" << scale_factors_[i]; | |
1554 break; | |
1555 } | |
1556 image_memory_[scaled_raw_id] = | |
1557 base::RefCountedBytes::TakeVector(&bitmap_data); | |
1558 } | |
1559 } | |
OLD | NEW |