| 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::GetScaleForScaleFactor(expected[index])) | 247 if (scales[index] != ui::GetImageScale(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::GetScaleForScaleFactor(scale_factors[i]); | 258 scales[i] = ui::GetImageScale(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 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 *src_image, hsl_shift)); | 444 *src_image, hsl_shift)); |
| 445 } | 445 } |
| 446 | 446 |
| 447 // Computes a bitmap at one scale from a bitmap at a different scale. | 447 // Computes a bitmap at one scale from a bitmap at a different scale. |
| 448 SkBitmap CreateLowQualityResizedBitmap(const SkBitmap& source_bitmap, | 448 SkBitmap CreateLowQualityResizedBitmap(const SkBitmap& source_bitmap, |
| 449 ui::ScaleFactor source_scale_factor, | 449 ui::ScaleFactor source_scale_factor, |
| 450 ui::ScaleFactor desired_scale_factor) { | 450 ui::ScaleFactor desired_scale_factor) { |
| 451 gfx::Size scaled_size = gfx::ToCeiledSize( | 451 gfx::Size scaled_size = gfx::ToCeiledSize( |
| 452 gfx::ScaleSize(gfx::Size(source_bitmap.width(), | 452 gfx::ScaleSize(gfx::Size(source_bitmap.width(), |
| 453 source_bitmap.height()), | 453 source_bitmap.height()), |
| 454 ui::GetScaleForScaleFactor(desired_scale_factor) / | 454 ui::GetImageScale(desired_scale_factor) / |
| 455 ui::GetScaleForScaleFactor(source_scale_factor))); | 455 ui::GetImageScale(source_scale_factor))); |
| 456 SkBitmap scaled_bitmap; | 456 SkBitmap scaled_bitmap; |
| 457 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 457 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 458 scaled_size.width(), | 458 scaled_size.width(), |
| 459 scaled_size.height()); | 459 scaled_size.height()); |
| 460 if (!scaled_bitmap.allocPixels()) | 460 if (!scaled_bitmap.allocPixels()) |
| 461 SK_CRASH(); | 461 SK_CRASH(); |
| 462 scaled_bitmap.eraseARGB(0, 0, 0, 0); | 462 scaled_bitmap.eraseARGB(0, 0, 0, 0); |
| 463 SkCanvas canvas(scaled_bitmap); | 463 SkCanvas canvas(scaled_bitmap); |
| 464 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); | 464 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); |
| 465 // Note(oshima): The following scaling code doesn't work with | 465 // Note(oshima): The following scaling code doesn't work with |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 bitmap_map_[scale_factor] = bitmap; | 528 bitmap_map_[scale_factor] = bitmap; |
| 529 return gfx::ImageSkiaRep(bitmap, scale); | 529 return gfx::ImageSkiaRep(bitmap, scale); |
| 530 } | 530 } |
| 531 | 531 |
| 532 // Find an available PNG for another scale factor. We want to use the | 532 // Find an available PNG for another scale factor. We want to use the |
| 533 // highest available scale factor. | 533 // highest available scale factor. |
| 534 PngMap::const_iterator available_png_it = png_map_.end(); | 534 PngMap::const_iterator available_png_it = png_map_.end(); |
| 535 for (PngMap::const_iterator png_it = png_map_.begin(); | 535 for (PngMap::const_iterator png_it = png_map_.begin(); |
| 536 png_it != png_map_.end(); ++png_it) { | 536 png_it != png_map_.end(); ++png_it) { |
| 537 if (available_png_it == png_map_.end() || | 537 if (available_png_it == png_map_.end() || |
| 538 ui::GetScaleForScaleFactor(png_it->first) > | 538 ui::GetImageScale(png_it->first) > |
| 539 ui::GetScaleForScaleFactor(available_png_it->first)) { | 539 ui::GetImageScale(available_png_it->first)) { |
| 540 available_png_it = png_it; | 540 available_png_it = png_it; |
| 541 } | 541 } |
| 542 } | 542 } |
| 543 if (available_png_it == png_map_.end()) | 543 if (available_png_it == png_map_.end()) |
| 544 return gfx::ImageSkiaRep(); | 544 return gfx::ImageSkiaRep(); |
| 545 ui::ScaleFactor available_scale_factor = available_png_it->first; | 545 ui::ScaleFactor available_scale_factor = available_png_it->first; |
| 546 | 546 |
| 547 // Look up the bitmap for |available_scale_factor| in the bitmap map. | 547 // Look up the bitmap for |available_scale_factor| in the bitmap map. |
| 548 // If not found, decode the corresponging png data, store the result | 548 // If not found, decode the corresponging png data, store the result |
| 549 // in the bitmap map. | 549 // in the bitmap map. |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 return NULL; | 749 return NULL; |
| 750 pack->source_images_ = reinterpret_cast<int*>( | 750 pack->source_images_ = reinterpret_cast<int*>( |
| 751 const_cast<char*>(pointer.data())); | 751 const_cast<char*>(pointer.data())); |
| 752 | 752 |
| 753 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) | 753 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) |
| 754 return NULL; | 754 return NULL; |
| 755 | 755 |
| 756 if (!InputScalesValid(pointer, pack->scale_factors_)) { | 756 if (!InputScalesValid(pointer, pack->scale_factors_)) { |
| 757 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " | 757 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " |
| 758 << "from those supported by platform."; | 758 << "from those supported by platform."; |
| 759 return NULL; | |
| 760 } | 759 } |
| 761 return pack; | 760 return pack; |
| 762 } | 761 } |
| 763 | 762 |
| 764 // static | 763 // static |
| 765 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { | 764 void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) { |
| 766 if (!result) | 765 if (!result) |
| 767 return; | 766 return; |
| 768 | 767 |
| 769 result->clear(); | 768 result->clear(); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 920 // private: | 919 // private: |
| 921 | 920 |
| 922 BrowserThemePack::BrowserThemePack() | 921 BrowserThemePack::BrowserThemePack() |
| 923 : CustomThemeSupplier(EXTENSION), | 922 : CustomThemeSupplier(EXTENSION), |
| 924 header_(NULL), | 923 header_(NULL), |
| 925 tints_(NULL), | 924 tints_(NULL), |
| 926 colors_(NULL), | 925 colors_(NULL), |
| 927 display_properties_(NULL), | 926 display_properties_(NULL), |
| 928 source_images_(NULL) { | 927 source_images_(NULL) { |
| 929 scale_factors_ = ui::GetSupportedScaleFactors(); | 928 scale_factors_ = ui::GetSupportedScaleFactors(); |
| 930 // On Windows with high DPI SCALE_FACTOR_100P may not be supported by | |
| 931 // default. | |
| 932 // Add it to the supported scale factors list as themes needs it. | |
| 933 if (!ui::IsScaleFactorSupported(ui::SCALE_FACTOR_100P)) | |
| 934 scale_factors_.push_back(ui::SCALE_FACTOR_100P); | |
| 935 } | 929 } |
| 936 | 930 |
| 937 void BrowserThemePack::BuildHeader(const Extension* extension) { | 931 void BrowserThemePack::BuildHeader(const Extension* extension) { |
| 938 header_ = new BrowserThemePackHeader; | 932 header_ = new BrowserThemePackHeader; |
| 939 header_->version = kThemePackVersion; | 933 header_->version = kThemePackVersion; |
| 940 | 934 |
| 941 // TODO(erg): Need to make this endian safe on other computers. Prerequisite | 935 // TODO(erg): Need to make this endian safe on other computers. Prerequisite |
| 942 // is that ui::DataPack removes this same check. | 936 // is that ui::DataPack removes this same check. |
| 943 #if defined(__BYTE_ORDER) | 937 #if defined(__BYTE_ORDER) |
| 944 // Linux check | 938 // Linux check |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1281 } | 1275 } |
| 1282 if (is_copyable) { | 1276 if (is_copyable) { |
| 1283 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); | 1277 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); |
| 1284 image_memory_[raw_id] = raw_data; | 1278 image_memory_[raw_id] = raw_data; |
| 1285 } else { | 1279 } else { |
| 1286 SkBitmap bitmap; | 1280 SkBitmap bitmap; |
| 1287 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), | 1281 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), |
| 1288 &bitmap)) { | 1282 &bitmap)) { |
| 1289 image_skia.AddRepresentation( | 1283 image_skia.AddRepresentation( |
| 1290 gfx::ImageSkiaRep(bitmap, | 1284 gfx::ImageSkiaRep(bitmap, |
| 1291 ui::GetScaleForScaleFactor(scale_factor))); | 1285 ui::GetImageScale(scale_factor))); |
| 1292 } else { | 1286 } else { |
| 1293 NOTREACHED() << "Unable to decode theme image resource " | 1287 NOTREACHED() << "Unable to decode theme image resource " |
| 1294 << it->first; | 1288 << it->first; |
| 1295 } | 1289 } |
| 1296 } | 1290 } |
| 1297 } | 1291 } |
| 1298 } | 1292 } |
| 1299 if (!is_copyable && !image_skia.isNull()) | 1293 if (!is_copyable && !image_skia.isNull()) |
| 1300 (*image_cache)[prs_id] = gfx::Image(image_skia); | 1294 (*image_cache)[prs_id] = gfx::Image(image_skia); |
| 1301 } | 1295 } |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1530 return -1; | 1524 return -1; |
| 1531 } | 1525 } |
| 1532 | 1526 |
| 1533 bool BrowserThemePack::GetScaleFactorFromManifestKey( | 1527 bool BrowserThemePack::GetScaleFactorFromManifestKey( |
| 1534 const std::string& key, | 1528 const std::string& key, |
| 1535 ui::ScaleFactor* scale_factor) const { | 1529 ui::ScaleFactor* scale_factor) const { |
| 1536 int percent = 0; | 1530 int percent = 0; |
| 1537 if (base::StringToInt(key, &percent)) { | 1531 if (base::StringToInt(key, &percent)) { |
| 1538 float scale = static_cast<float>(percent) / 100.0f; | 1532 float scale = static_cast<float>(percent) / 100.0f; |
| 1539 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1533 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| 1540 if (fabs(ui::GetScaleForScaleFactor(scale_factors_[i]) - scale) | 1534 if (fabs(ui::GetImageScale(scale_factors_[i]) - scale) < 0.001) { |
| 1541 < 0.001) { | |
| 1542 *scale_factor = scale_factors_[i]; | 1535 *scale_factor = scale_factors_[i]; |
| 1543 return true; | 1536 return true; |
| 1544 } | 1537 } |
| 1545 } | 1538 } |
| 1546 } | 1539 } |
| 1547 return false; | 1540 return false; |
| 1548 } | 1541 } |
| 1549 | 1542 |
| 1550 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { | 1543 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { |
| 1551 // Compute (by scaling) bitmaps for |prs_id| for any scale factors | 1544 // Compute (by scaling) bitmaps for |prs_id| for any scale factors |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1568 } | 1561 } |
| 1569 } | 1562 } |
| 1570 if (!image_missing) | 1563 if (!image_missing) |
| 1571 return; | 1564 return; |
| 1572 | 1565 |
| 1573 // Find available scale factor with highest scale. | 1566 // Find available scale factor with highest scale. |
| 1574 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; | 1567 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; |
| 1575 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1568 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
| 1576 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); | 1569 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); |
| 1577 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || | 1570 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || |
| 1578 (ui::GetScaleForScaleFactor(scale_factors_[i]) > | 1571 (ui::GetImageScale(scale_factors_[i]) > |
| 1579 ui::GetScaleForScaleFactor(available_scale_factor))) && | 1572 ui::GetImageScale(available_scale_factor))) && |
| 1580 image_memory_.find(raw_id) != image_memory_.end()) { | 1573 image_memory_.find(raw_id) != image_memory_.end()) { |
| 1581 available_scale_factor = scale_factors_[i]; | 1574 available_scale_factor = scale_factors_[i]; |
| 1582 } | 1575 } |
| 1583 } | 1576 } |
| 1584 // If no scale factor is available, we're done. | 1577 // If no scale factor is available, we're done. |
| 1585 if (available_scale_factor == ui::SCALE_FACTOR_NONE) | 1578 if (available_scale_factor == ui::SCALE_FACTOR_NONE) |
| 1586 return; | 1579 return; |
| 1587 | 1580 |
| 1588 // Get bitmap for the available scale factor. | 1581 // Get bitmap for the available scale factor. |
| 1589 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); | 1582 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1611 false, | 1604 false, |
| 1612 &bitmap_data)) { | 1605 &bitmap_data)) { |
| 1613 NOTREACHED() << "Unable to encode theme image for prs_id=" | 1606 NOTREACHED() << "Unable to encode theme image for prs_id=" |
| 1614 << prs_id << " for scale_factor=" << scale_factors_[i]; | 1607 << prs_id << " for scale_factor=" << scale_factors_[i]; |
| 1615 break; | 1608 break; |
| 1616 } | 1609 } |
| 1617 image_memory_[scaled_raw_id] = | 1610 image_memory_[scaled_raw_id] = |
| 1618 base::RefCountedBytes::TakeVector(&bitmap_data); | 1611 base::RefCountedBytes::TakeVector(&bitmap_data); |
| 1619 } | 1612 } |
| 1620 } | 1613 } |
| OLD | NEW |