| 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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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."; |
| 758 return NULL; |
| 758 } | 759 } |
| 759 return pack; | 760 return pack; |
| 760 } | 761 } |
| 761 | 762 |
| 762 // static | 763 // static |
| 763 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { | 764 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { |
| 764 if (!result) | 765 if (!result) |
| 765 return; | 766 return; |
| 766 | 767 |
| 767 result->clear(); | 768 result->clear(); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 918 // private: | 919 // private: |
| 919 | 920 |
| 920 BrowserThemePack::BrowserThemePack() | 921 BrowserThemePack::BrowserThemePack() |
| 921 : CustomThemeSupplier(EXTENSION), | 922 : CustomThemeSupplier(EXTENSION), |
| 922 header_(NULL), | 923 header_(NULL), |
| 923 tints_(NULL), | 924 tints_(NULL), |
| 924 colors_(NULL), | 925 colors_(NULL), |
| 925 display_properties_(NULL), | 926 display_properties_(NULL), |
| 926 source_images_(NULL) { | 927 source_images_(NULL) { |
| 927 scale_factors_ = ui::GetSupportedScaleFactors(); | 928 scale_factors_ = ui::GetSupportedScaleFactors(); |
| 929 // On Windows with high DPI SCALE_FACTOR_100P may not be supported by |
| 930 // default. |
| 931 // Add it to the supported scale factors list as themes needs it. |
| 932 if (!ui::IsScaleFactorSupported(ui::SCALE_FACTOR_100P)) |
| 933 scale_factors_.push_back(ui::SCALE_FACTOR_100P); |
| 928 } | 934 } |
| 929 | 935 |
| 930 void BrowserThemePack::BuildHeader(const Extension* extension) { | 936 void BrowserThemePack::BuildHeader(const Extension* extension) { |
| 931 header_ = new BrowserThemePackHeader; | 937 header_ = new BrowserThemePackHeader; |
| 932 header_->version = kThemePackVersion; | 938 header_->version = kThemePackVersion; |
| 933 | 939 |
| 934 // TODO(erg): Need to make this endian safe on other computers. Prerequisite | 940 // TODO(erg): Need to make this endian safe on other computers. Prerequisite |
| 935 // is that ui::DataPack removes this same check. | 941 // is that ui::DataPack removes this same check. |
| 936 #if defined(__BYTE_ORDER) | 942 #if defined(__BYTE_ORDER) |
| 937 // Linux check | 943 // Linux check |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1274 } | 1280 } |
| 1275 if (is_copyable) { | 1281 if (is_copyable) { |
| 1276 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); | 1282 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); |
| 1277 image_memory_[raw_id] = raw_data; | 1283 image_memory_[raw_id] = raw_data; |
| 1278 } else { | 1284 } else { |
| 1279 SkBitmap bitmap; | 1285 SkBitmap bitmap; |
| 1280 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), | 1286 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), |
| 1281 &bitmap)) { | 1287 &bitmap)) { |
| 1282 image_skia.AddRepresentation( | 1288 image_skia.AddRepresentation( |
| 1283 gfx::ImageSkiaRep(bitmap, | 1289 gfx::ImageSkiaRep(bitmap, |
| 1284 ui::GetImageScale(scale_factor))); | 1290 ui::GetScaleForScaleFactor(scale_factor))); |
| 1285 } else { | 1291 } else { |
| 1286 NOTREACHED() << "Unable to decode theme image resource " | 1292 NOTREACHED() << "Unable to decode theme image resource " |
| 1287 << it->first; | 1293 << it->first; |
| 1288 } | 1294 } |
| 1289 } | 1295 } |
| 1290 } | 1296 } |
| 1291 } | 1297 } |
| 1292 if (!is_copyable && !image_skia.isNull()) | 1298 if (!is_copyable && !image_skia.isNull()) |
| 1293 (*image_cache)[prs_id] = gfx::Image(image_skia); | 1299 (*image_cache)[prs_id] = gfx::Image(image_skia); |
| 1294 } | 1300 } |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1523 return -1; | 1529 return -1; |
| 1524 } | 1530 } |
| 1525 | 1531 |
| 1526 bool BrowserThemePack::GetScaleFactorFromManifestKey( | 1532 bool BrowserThemePack::GetScaleFactorFromManifestKey( |
| 1527 const std::string& key, | 1533 const std::string& key, |
| 1528 ui::ScaleFactor* scale_factor) const { | 1534 ui::ScaleFactor* scale_factor) const { |
| 1529 int percent = 0; | 1535 int percent = 0; |
| 1530 if (base::StringToInt(key, &percent)) { | 1536 if (base::StringToInt(key, &percent)) { |
| 1531 float scale = static_cast<float>(percent) / 100.0f; | 1537 float scale = static_cast<float>(percent) / 100.0f; |
| 1532 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1538 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| 1533 if (fabs(ui::GetImageScale(scale_factors_[i]) - scale) < 0.001) { | 1539 if (fabs(ui::GetScaleForScaleFactor(scale_factors_[i]) - scale) |
| 1540 < 0.001) { |
| 1534 *scale_factor = scale_factors_[i]; | 1541 *scale_factor = scale_factors_[i]; |
| 1535 return true; | 1542 return true; |
| 1536 } | 1543 } |
| 1537 } | 1544 } |
| 1538 } | 1545 } |
| 1539 return false; | 1546 return false; |
| 1540 } | 1547 } |
| 1541 | 1548 |
| 1542 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { | 1549 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { |
| 1543 // Compute (by scaling) bitmaps for |prs_id| for any scale factors | 1550 // Compute (by scaling) bitmaps for |prs_id| for any scale factors |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1560 } | 1567 } |
| 1561 } | 1568 } |
| 1562 if (!image_missing) | 1569 if (!image_missing) |
| 1563 return; | 1570 return; |
| 1564 | 1571 |
| 1565 // Find available scale factor with highest scale. | 1572 // Find available scale factor with highest scale. |
| 1566 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; | 1573 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; |
| 1567 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1574 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| 1568 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); | 1575 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| 1569 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || | 1576 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || |
| 1570 (ui::GetImageScale(scale_factors_[i]) > | 1577 (ui::GetScaleForScaleFactor(scale_factors_[i]) > |
| 1571 ui::GetImageScale(available_scale_factor))) && | 1578 ui::GetScaleForScaleFactor(available_scale_factor))) && |
| 1572 image_memory_.find(raw_id) != image_memory_.end()) { | 1579 image_memory_.find(raw_id) != image_memory_.end()) { |
| 1573 available_scale_factor = scale_factors_[i]; | 1580 available_scale_factor = scale_factors_[i]; |
| 1574 } | 1581 } |
| 1575 } | 1582 } |
| 1576 // If no scale factor is available, we're done. | 1583 // If no scale factor is available, we're done. |
| 1577 if (available_scale_factor == ui::SCALE_FACTOR_NONE) | 1584 if (available_scale_factor == ui::SCALE_FACTOR_NONE) |
| 1578 return; | 1585 return; |
| 1579 | 1586 |
| 1580 // Get bitmap for the available scale factor. | 1587 // Get bitmap for the available scale factor. |
| 1581 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); | 1588 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1603 false, | 1610 false, |
| 1604 &bitmap_data)) { | 1611 &bitmap_data)) { |
| 1605 NOTREACHED() << "Unable to encode theme image for prs_id=" | 1612 NOTREACHED() << "Unable to encode theme image for prs_id=" |
| 1606 << prs_id << " for scale_factor=" << scale_factors_[i]; | 1613 << prs_id << " for scale_factor=" << scale_factors_[i]; |
| 1607 break; | 1614 break; |
| 1608 } | 1615 } |
| 1609 image_memory_[scaled_raw_id] = | 1616 image_memory_[scaled_raw_id] = |
| 1610 base::RefCountedBytes::TakeVector(&bitmap_data); | 1617 base::RefCountedBytes::TakeVector(&bitmap_data); |
| 1611 } | 1618 } |
| 1612 } | 1619 } |
| OLD | NEW |