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/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 // The order must match as the index is used in determining the raw id. | 237 // The order must match as the index is used in determining the raw id. |
238 bool InputScalesValid(const base::StringPiece& input, | 238 bool InputScalesValid(const base::StringPiece& input, |
239 const std::vector<ui::ScaleFactor>& expected) { | 239 const std::vector<ui::ScaleFactor>& expected) { |
240 size_t scales_size = static_cast<size_t>(input.size() / sizeof(float)); | 240 size_t scales_size = static_cast<size_t>(input.size() / sizeof(float)); |
241 if (scales_size != expected.size()) | 241 if (scales_size != expected.size()) |
242 return false; | 242 return false; |
243 scoped_ptr<float[]> scales(new float[scales_size]); | 243 scoped_ptr<float[]> scales(new float[scales_size]); |
244 // Do a memcpy to avoid misaligned memory access. | 244 // Do a memcpy to avoid misaligned memory access. |
245 memcpy(scales.get(), input.data(), input.size()); | 245 memcpy(scales.get(), input.data(), input.size()); |
246 for (size_t index = 0; index < scales_size; ++index) { | 246 for (size_t index = 0; index < scales_size; ++index) { |
247 if (scales[index] != ui::GetImageScale(expected[index])) | 247 if (scales[index] != ui::GetScaleForScaleFactor(expected[index])) |
248 return false; | 248 return false; |
249 } | 249 } |
250 return true; | 250 return true; |
251 } | 251 } |
252 | 252 |
253 // Returns |scale_factors| as a string to be written to disk. | 253 // Returns |scale_factors| as a string to be written to disk. |
254 std::string GetScaleFactorsAsString( | 254 std::string GetScaleFactorsAsString( |
255 const std::vector<ui::ScaleFactor>& scale_factors) { | 255 const std::vector<ui::ScaleFactor>& scale_factors) { |
256 scoped_ptr<float[]> scales(new float[scale_factors.size()]); | 256 scoped_ptr<float[]> scales(new float[scale_factors.size()]); |
257 for (size_t i = 0; i < scale_factors.size(); ++i) | 257 for (size_t i = 0; i < scale_factors.size(); ++i) |
258 scales[i] = ui::GetImageScale(scale_factors[i]); | 258 scales[i] = ui::GetScaleForScaleFactor(scale_factors[i]); |
259 std::string out_string = std::string( | 259 std::string out_string = std::string( |
260 reinterpret_cast<const char*>(scales.get()), | 260 reinterpret_cast<const char*>(scales.get()), |
261 scale_factors.size() * sizeof(float)); | 261 scale_factors.size() * sizeof(float)); |
262 return out_string; | 262 return out_string; |
263 } | 263 } |
264 | 264 |
265 struct StringToIntTable { | 265 struct StringToIntTable { |
266 const char* key; | 266 const char* key; |
267 ThemeProperties::OverwritableByUserThemeProperty id; | 267 ThemeProperties::OverwritableByUserThemeProperty id; |
268 }; | 268 }; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 *src_image, hsl_shift)); | 443 *src_image, hsl_shift)); |
444 } | 444 } |
445 | 445 |
446 // Computes a bitmap at one scale from a bitmap at a different scale. | 446 // Computes a bitmap at one scale from a bitmap at a different scale. |
447 SkBitmap CreateLowQualityResizedBitmap(const SkBitmap& source_bitmap, | 447 SkBitmap CreateLowQualityResizedBitmap(const SkBitmap& source_bitmap, |
448 ui::ScaleFactor source_scale_factor, | 448 ui::ScaleFactor source_scale_factor, |
449 ui::ScaleFactor desired_scale_factor) { | 449 ui::ScaleFactor desired_scale_factor) { |
450 gfx::Size scaled_size = gfx::ToCeiledSize( | 450 gfx::Size scaled_size = gfx::ToCeiledSize( |
451 gfx::ScaleSize(gfx::Size(source_bitmap.width(), | 451 gfx::ScaleSize(gfx::Size(source_bitmap.width(), |
452 source_bitmap.height()), | 452 source_bitmap.height()), |
453 ui::GetImageScale(desired_scale_factor) / | 453 ui::GetScaleForScaleFactor(desired_scale_factor) / |
454 ui::GetImageScale(source_scale_factor))); | 454 ui::GetScaleForScaleFactor(source_scale_factor))); |
455 SkBitmap scaled_bitmap; | 455 SkBitmap scaled_bitmap; |
456 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 456 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
457 scaled_size.width(), | 457 scaled_size.width(), |
458 scaled_size.height()); | 458 scaled_size.height()); |
459 if (!scaled_bitmap.allocPixels()) | 459 if (!scaled_bitmap.allocPixels()) |
460 SK_CRASH(); | 460 SK_CRASH(); |
461 scaled_bitmap.eraseARGB(0, 0, 0, 0); | 461 scaled_bitmap.eraseARGB(0, 0, 0, 0); |
462 SkCanvas canvas(scaled_bitmap); | 462 SkCanvas canvas(scaled_bitmap); |
463 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); | 463 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); |
464 // Note(oshima): The following scaling code doesn't work with | 464 // Note(oshima): The following scaling code doesn't work with |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
527 bitmap_map_[scale_factor] = bitmap; | 527 bitmap_map_[scale_factor] = bitmap; |
528 return gfx::ImageSkiaRep(bitmap, scale); | 528 return gfx::ImageSkiaRep(bitmap, scale); |
529 } | 529 } |
530 | 530 |
531 // Find an available PNG for another scale factor. We want to use the | 531 // Find an available PNG for another scale factor. We want to use the |
532 // highest available scale factor. | 532 // highest available scale factor. |
533 PngMap::const_iterator available_png_it = png_map_.end(); | 533 PngMap::const_iterator available_png_it = png_map_.end(); |
534 for (PngMap::const_iterator png_it = png_map_.begin(); | 534 for (PngMap::const_iterator png_it = png_map_.begin(); |
535 png_it != png_map_.end(); ++png_it) { | 535 png_it != png_map_.end(); ++png_it) { |
536 if (available_png_it == png_map_.end() || | 536 if (available_png_it == png_map_.end() || |
537 ui::GetImageScale(png_it->first) > | 537 ui::GetScaleForScaleFactor(png_it->first) > |
538 ui::GetImageScale(available_png_it->first)) { | 538 ui::GetScaleForScaleFactor(available_png_it->first)) { |
539 available_png_it = png_it; | 539 available_png_it = png_it; |
540 } | 540 } |
541 } | 541 } |
542 if (available_png_it == png_map_.end()) | 542 if (available_png_it == png_map_.end()) |
543 return gfx::ImageSkiaRep(); | 543 return gfx::ImageSkiaRep(); |
544 ui::ScaleFactor available_scale_factor = available_png_it->first; | 544 ui::ScaleFactor available_scale_factor = available_png_it->first; |
545 | 545 |
546 // Look up the bitmap for |available_scale_factor| in the bitmap map. | 546 // Look up the bitmap for |available_scale_factor| in the bitmap map. |
547 // If not found, decode the corresponging png data, store the result | 547 // If not found, decode the corresponging png data, store the result |
548 // in the bitmap map. | 548 // in the bitmap map. |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
747 if (!pack->data_pack_->GetStringPiece(kSourceImagesID, &pointer)) | 747 if (!pack->data_pack_->GetStringPiece(kSourceImagesID, &pointer)) |
748 return NULL; | 748 return NULL; |
749 pack->source_images_ = reinterpret_cast<int*>( | 749 pack->source_images_ = reinterpret_cast<int*>( |
750 const_cast<char*>(pointer.data())); | 750 const_cast<char*>(pointer.data())); |
751 | 751 |
752 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) | 752 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) |
753 return NULL; | 753 return NULL; |
754 | 754 |
755 if (!InputScalesValid(pointer, pack->scale_factors_)) { | 755 if (!InputScalesValid(pointer, pack->scale_factors_)) { |
756 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " | 756 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " |
757 << "from those supported by platform."; | 757 << "from those supported by platform."; |
pkotwicz
2014/05/20 19:33:01
We should return NULL in this case. Otherwise we w
ananta
2014/05/20 21:49:34
Done.
| |
758 } | 758 } |
759 return pack; | 759 return pack; |
760 } | 760 } |
761 | 761 |
762 // static | 762 // static |
763 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { | 763 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { |
764 if (!result) | 764 if (!result) |
765 return; | 765 return; |
766 | 766 |
767 result->clear(); | 767 result->clear(); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
918 // private: | 918 // private: |
919 | 919 |
920 BrowserThemePack::BrowserThemePack() | 920 BrowserThemePack::BrowserThemePack() |
921 : CustomThemeSupplier(EXTENSION), | 921 : CustomThemeSupplier(EXTENSION), |
922 header_(NULL), | 922 header_(NULL), |
923 tints_(NULL), | 923 tints_(NULL), |
924 colors_(NULL), | 924 colors_(NULL), |
925 display_properties_(NULL), | 925 display_properties_(NULL), |
926 source_images_(NULL) { | 926 source_images_(NULL) { |
927 scale_factors_ = ui::GetSupportedScaleFactors(); | 927 scale_factors_ = ui::GetSupportedScaleFactors(); |
928 // On Windows with high DPI SCALE_FACTOR_100P may not be supported by default | |
pkotwicz
2014/05/20 19:33:01
Nit: missing period.
ananta
2014/05/20 21:49:34
Done.
| |
929 // Add it to the supported scale factors list as themes needs it. | |
930 if (!ui::IsScaleFactorSupported(ui::SCALE_FACTOR_100P)) | |
931 scale_factors_.push_back(ui::SCALE_FACTOR_100P); | |
928 } | 932 } |
929 | 933 |
930 void BrowserThemePack::BuildHeader(const Extension* extension) { | 934 void BrowserThemePack::BuildHeader(const Extension* extension) { |
931 header_ = new BrowserThemePackHeader; | 935 header_ = new BrowserThemePackHeader; |
932 header_->version = kThemePackVersion; | 936 header_->version = kThemePackVersion; |
933 | 937 |
934 // TODO(erg): Need to make this endian safe on other computers. Prerequisite | 938 // TODO(erg): Need to make this endian safe on other computers. Prerequisite |
935 // is that ui::DataPack removes this same check. | 939 // is that ui::DataPack removes this same check. |
936 #if defined(__BYTE_ORDER) | 940 #if defined(__BYTE_ORDER) |
937 // Linux check | 941 // Linux check |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1274 } | 1278 } |
1275 if (is_copyable) { | 1279 if (is_copyable) { |
1276 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); | 1280 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); |
1277 image_memory_[raw_id] = raw_data; | 1281 image_memory_[raw_id] = raw_data; |
1278 } else { | 1282 } else { |
1279 SkBitmap bitmap; | 1283 SkBitmap bitmap; |
1280 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), | 1284 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), |
1281 &bitmap)) { | 1285 &bitmap)) { |
1282 image_skia.AddRepresentation( | 1286 image_skia.AddRepresentation( |
1283 gfx::ImageSkiaRep(bitmap, | 1287 gfx::ImageSkiaRep(bitmap, |
1284 ui::GetImageScale(scale_factor))); | 1288 ui::GetScaleForScaleFactor(scale_factor))); |
1285 } else { | 1289 } else { |
1286 NOTREACHED() << "Unable to decode theme image resource " | 1290 NOTREACHED() << "Unable to decode theme image resource " |
1287 << it->first; | 1291 << it->first; |
1288 } | 1292 } |
1289 } | 1293 } |
1290 } | 1294 } |
1291 } | 1295 } |
1292 if (!is_copyable && !image_skia.isNull()) | 1296 if (!is_copyable && !image_skia.isNull()) |
1293 (*image_cache)[prs_id] = gfx::Image(image_skia); | 1297 (*image_cache)[prs_id] = gfx::Image(image_skia); |
1294 } | 1298 } |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1523 return -1; | 1527 return -1; |
1524 } | 1528 } |
1525 | 1529 |
1526 bool BrowserThemePack::GetScaleFactorFromManifestKey( | 1530 bool BrowserThemePack::GetScaleFactorFromManifestKey( |
1527 const std::string& key, | 1531 const std::string& key, |
1528 ui::ScaleFactor* scale_factor) const { | 1532 ui::ScaleFactor* scale_factor) const { |
1529 int percent = 0; | 1533 int percent = 0; |
1530 if (base::StringToInt(key, &percent)) { | 1534 if (base::StringToInt(key, &percent)) { |
1531 float scale = static_cast<float>(percent) / 100.0f; | 1535 float scale = static_cast<float>(percent) / 100.0f; |
1532 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1536 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
1533 if (fabs(ui::GetImageScale(scale_factors_[i]) - scale) < 0.001) { | 1537 if (fabs(ui::GetScaleForScaleFactor(scale_factors_[i]) - scale) |
1538 < 0.001) { | |
1534 *scale_factor = scale_factors_[i]; | 1539 *scale_factor = scale_factors_[i]; |
1535 return true; | 1540 return true; |
1536 } | 1541 } |
1537 } | 1542 } |
1538 } | 1543 } |
1539 return false; | 1544 return false; |
1540 } | 1545 } |
1541 | 1546 |
1542 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { | 1547 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { |
1543 // Compute (by scaling) bitmaps for |prs_id| for any scale factors | 1548 // Compute (by scaling) bitmaps for |prs_id| for any scale factors |
(...skipping 16 matching lines...) Expand all Loading... | |
1560 } | 1565 } |
1561 } | 1566 } |
1562 if (!image_missing) | 1567 if (!image_missing) |
1563 return; | 1568 return; |
1564 | 1569 |
1565 // Find available scale factor with highest scale. | 1570 // Find available scale factor with highest scale. |
1566 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; | 1571 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; |
1567 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1572 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
1568 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); | 1573 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
1569 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || | 1574 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || |
1570 (ui::GetImageScale(scale_factors_[i]) > | 1575 (ui::GetScaleForScaleFactor(scale_factors_[i]) > |
1571 ui::GetImageScale(available_scale_factor))) && | 1576 ui::GetScaleForScaleFactor(available_scale_factor))) && |
1572 image_memory_.find(raw_id) != image_memory_.end()) { | 1577 image_memory_.find(raw_id) != image_memory_.end()) { |
1573 available_scale_factor = scale_factors_[i]; | 1578 available_scale_factor = scale_factors_[i]; |
1574 } | 1579 } |
1575 } | 1580 } |
1576 // If no scale factor is available, we're done. | 1581 // If no scale factor is available, we're done. |
1577 if (available_scale_factor == ui::SCALE_FACTOR_NONE) | 1582 if (available_scale_factor == ui::SCALE_FACTOR_NONE) |
1578 return; | 1583 return; |
1579 | 1584 |
1580 // Get bitmap for the available scale factor. | 1585 // Get bitmap for the available scale factor. |
1581 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); | 1586 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); |
(...skipping 21 matching lines...) Expand all Loading... | |
1603 false, | 1608 false, |
1604 &bitmap_data)) { | 1609 &bitmap_data)) { |
1605 NOTREACHED() << "Unable to encode theme image for prs_id=" | 1610 NOTREACHED() << "Unable to encode theme image for prs_id=" |
1606 << prs_id << " for scale_factor=" << scale_factors_[i]; | 1611 << prs_id << " for scale_factor=" << scale_factors_[i]; |
1607 break; | 1612 break; |
1608 } | 1613 } |
1609 image_memory_[scaled_raw_id] = | 1614 image_memory_[scaled_raw_id] = |
1610 base::RefCountedBytes::TakeVector(&bitmap_data); | 1615 base::RefCountedBytes::TakeVector(&bitmap_data); |
1611 } | 1616 } |
1612 } | 1617 } |
OLD | NEW |