Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: trunk/src/chrome/browser/themes/browser_theme_pack.cc

Issue 24262008: Revert 224473 "Remove dependency on ui::ScaleFactor from ui/gfx" (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 // The order must match as the index is used in determining the raw id. 238 // The order must match as the index is used in determining the raw id.
239 bool InputScalesValid(const base::StringPiece& input, 239 bool InputScalesValid(const base::StringPiece& input,
240 const std::vector<ui::ScaleFactor>& expected) { 240 const std::vector<ui::ScaleFactor>& expected) {
241 size_t scales_size = static_cast<size_t>(input.size() / sizeof(float)); 241 size_t scales_size = static_cast<size_t>(input.size() / sizeof(float));
242 if (scales_size != expected.size()) 242 if (scales_size != expected.size())
243 return false; 243 return false;
244 scoped_ptr<float[]> scales(new float[scales_size]); 244 scoped_ptr<float[]> scales(new float[scales_size]);
245 // Do a memcpy to avoid misaligned memory access. 245 // Do a memcpy to avoid misaligned memory access.
246 memcpy(scales.get(), input.data(), input.size()); 246 memcpy(scales.get(), input.data(), input.size());
247 for (size_t index = 0; index < scales_size; ++index) { 247 for (size_t index = 0; index < scales_size; ++index) {
248 if (scales[index] != ui::GetImageScale(expected[index])) 248 if (scales[index] != ui::GetScaleFactorScale(expected[index]))
249 return false; 249 return false;
250 } 250 }
251 return true; 251 return true;
252 } 252 }
253 253
254 // Returns |scale_factors| as a string to be written to disk. 254 // Returns |scale_factors| as a string to be written to disk.
255 std::string GetScaleFactorsAsString( 255 std::string GetScaleFactorsAsString(
256 const std::vector<ui::ScaleFactor>& scale_factors) { 256 const std::vector<ui::ScaleFactor>& scale_factors) {
257 scoped_ptr<float[]> scales(new float[scale_factors.size()]); 257 scoped_ptr<float[]> scales(new float[scale_factors.size()]);
258 for (size_t i = 0; i < scale_factors.size(); ++i) 258 for (size_t i = 0; i < scale_factors.size(); ++i)
259 scales[i] = ui::GetImageScale(scale_factors[i]); 259 scales[i] = ui::GetScaleFactorScale(scale_factors[i]);
260 std::string out_string = std::string( 260 std::string out_string = std::string(
261 reinterpret_cast<const char*>(scales.get()), 261 reinterpret_cast<const char*>(scales.get()),
262 scale_factors.size() * sizeof(float)); 262 scale_factors.size() * sizeof(float));
263 return out_string; 263 return out_string;
264 } 264 }
265 265
266 struct StringToIntTable { 266 struct StringToIntTable {
267 const char* key; 267 const char* key;
268 ThemeProperties::OverwritableByUserThemeProperty id; 268 ThemeProperties::OverwritableByUserThemeProperty id;
269 }; 269 };
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 *src_image, hsl_shift)); 445 *src_image, hsl_shift));
446 } 446 }
447 447
448 // Computes a bitmap at one scale from a bitmap at a different scale. 448 // Computes a bitmap at one scale from a bitmap at a different scale.
449 SkBitmap CreateLowQualityResizedBitmap(const SkBitmap& source_bitmap, 449 SkBitmap CreateLowQualityResizedBitmap(const SkBitmap& source_bitmap,
450 ui::ScaleFactor source_scale_factor, 450 ui::ScaleFactor source_scale_factor,
451 ui::ScaleFactor desired_scale_factor) { 451 ui::ScaleFactor desired_scale_factor) {
452 gfx::Size scaled_size = gfx::ToCeiledSize( 452 gfx::Size scaled_size = gfx::ToCeiledSize(
453 gfx::ScaleSize(gfx::Size(source_bitmap.width(), 453 gfx::ScaleSize(gfx::Size(source_bitmap.width(),
454 source_bitmap.height()), 454 source_bitmap.height()),
455 ui::GetImageScale(desired_scale_factor) / 455 ui::GetScaleFactorScale(desired_scale_factor) /
456 ui::GetImageScale(source_scale_factor))); 456 ui::GetScaleFactorScale(source_scale_factor)));
457 SkBitmap scaled_bitmap; 457 SkBitmap scaled_bitmap;
458 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 458 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config,
459 scaled_size.width(), 459 scaled_size.width(),
460 scaled_size.height()); 460 scaled_size.height());
461 if (!scaled_bitmap.allocPixels()) 461 if (!scaled_bitmap.allocPixels())
462 SK_CRASH(); 462 SK_CRASH();
463 scaled_bitmap.eraseARGB(0, 0, 0, 0); 463 scaled_bitmap.eraseARGB(0, 0, 0, 0);
464 SkCanvas canvas(scaled_bitmap); 464 SkCanvas canvas(scaled_bitmap);
465 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size)); 465 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size));
466 // Note(oshima): The following scaling code doesn't work with 466 // Note(oshima): The following scaling code doesn't work with
467 // a mask image. 467 // a mask image.
468 canvas.drawBitmapRect(source_bitmap, NULL, scaled_bounds); 468 canvas.drawBitmapRect(source_bitmap, NULL, scaled_bounds);
469 return scaled_bitmap; 469 return scaled_bitmap;
470 } 470 }
471 471
472 // A ImageSkiaSource that scales 100P image to the target scale factor 472 // A ImageSkiaSource that scales 100P image to the target scale factor
473 // if the ImageSkiaRep for the target scale factor isn't available. 473 // if the ImageSkiaRep for the target scale factor isn't available.
474 class ThemeImageSource: public gfx::ImageSkiaSource { 474 class ThemeImageSource: public gfx::ImageSkiaSource {
475 public: 475 public:
476 explicit ThemeImageSource(const gfx::ImageSkia& source) : source_(source) { 476 explicit ThemeImageSource(const gfx::ImageSkia& source) : source_(source) {
477 } 477 }
478 virtual ~ThemeImageSource() {} 478 virtual ~ThemeImageSource() {}
479 479
480 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { 480 virtual gfx::ImageSkiaRep GetImageForScale(
481 if (source_.HasRepresentation(scale)) 481 ui::ScaleFactor scale_factor) OVERRIDE {
482 return source_.GetRepresentation(scale); 482 if (source_.HasRepresentation(scale_factor))
483 const gfx::ImageSkiaRep& rep_100p = source_.GetRepresentation(1.0f); 483 return source_.GetRepresentation(scale_factor);
484 const gfx::ImageSkiaRep& rep_100p =
485 source_.GetRepresentation(ui::SCALE_FACTOR_100P);
484 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap( 486 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
485 rep_100p.sk_bitmap(), 487 rep_100p.sk_bitmap(),
486 ui::SCALE_FACTOR_100P, 488 ui::SCALE_FACTOR_100P,
487 ui::GetSupportedScaleFactor(scale)); 489 scale_factor);
488 return gfx::ImageSkiaRep(scaled_bitmap, scale); 490 return gfx::ImageSkiaRep(scaled_bitmap, scale_factor);
489 } 491 }
490 492
491 private: 493 private:
492 const gfx::ImageSkia source_; 494 const gfx::ImageSkia source_;
493 495
494 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource); 496 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource);
495 }; 497 };
496 498
497 // An ImageSkiaSource that delays decoding PNG data into bitmaps until 499 // An ImageSkiaSource that delays decoding PNG data into bitmaps until
498 // needed. Missing data for a scale factor is computed by scaling data for an 500 // needed. Missing data for a scale factor is computed by scaling data for an
499 // available scale factor. Computed bitmaps are stored for future look up. 501 // available scale factor. Computed bitmaps are stored for future look up.
500 class ThemeImagePngSource : public gfx::ImageSkiaSource { 502 class ThemeImagePngSource : public gfx::ImageSkiaSource {
501 public: 503 public:
502 typedef std::map<ui::ScaleFactor, 504 typedef std::map<ui::ScaleFactor,
503 scoped_refptr<base::RefCountedMemory> > PngMap; 505 scoped_refptr<base::RefCountedMemory> > PngMap;
504 506
505 explicit ThemeImagePngSource(const PngMap& png_map) : png_map_(png_map) {} 507 explicit ThemeImagePngSource(const PngMap& png_map) : png_map_(png_map) {}
506 508
507 virtual ~ThemeImagePngSource() {} 509 virtual ~ThemeImagePngSource() {}
508 510
509 private: 511 private:
510 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { 512 virtual gfx::ImageSkiaRep GetImageForScale(
511 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); 513 ui::ScaleFactor scale_factor) OVERRIDE {
512 // Look up the bitmap for |scale factor| in the bitmap map. If found 514 // Look up the bitmap for |scale factor| in the bitmap map. If found
513 // return it. 515 // return it.
514 BitmapMap::const_iterator exact_bitmap_it = bitmap_map_.find(scale_factor); 516 BitmapMap::const_iterator exact_bitmap_it = bitmap_map_.find(scale_factor);
515 if (exact_bitmap_it != bitmap_map_.end()) 517 if (exact_bitmap_it != bitmap_map_.end())
516 return gfx::ImageSkiaRep(exact_bitmap_it->second, scale); 518 return gfx::ImageSkiaRep(exact_bitmap_it->second, scale_factor);
517 519
518 // Look up the raw PNG data for |scale_factor| in the png map. If found, 520 // Look up the raw PNG data for |scale_factor| in the png map. If found,
519 // decode it, store the result in the bitmap map and return it. 521 // decode it, store the result in the bitmap map and return it.
520 PngMap::const_iterator exact_png_it = png_map_.find(scale_factor); 522 PngMap::const_iterator exact_png_it = png_map_.find(scale_factor);
521 if (exact_png_it != png_map_.end()) { 523 if (exact_png_it != png_map_.end()) {
522 SkBitmap bitmap; 524 SkBitmap bitmap;
523 if (!gfx::PNGCodec::Decode(exact_png_it->second->front(), 525 if (!gfx::PNGCodec::Decode(exact_png_it->second->front(),
524 exact_png_it->second->size(), 526 exact_png_it->second->size(),
525 &bitmap)) { 527 &bitmap)) {
526 NOTREACHED(); 528 NOTREACHED();
527 return gfx::ImageSkiaRep(); 529 return gfx::ImageSkiaRep();
528 } 530 }
529 bitmap_map_[scale_factor] = bitmap; 531 bitmap_map_[scale_factor] = bitmap;
530 return gfx::ImageSkiaRep(bitmap, scale); 532 return gfx::ImageSkiaRep(bitmap, scale_factor);
531 } 533 }
532 534
533 // Find an available PNG for another scale factor. We want to use the 535 // Find an available PNG for another scale factor. We want to use the
534 // highest available scale factor. 536 // highest available scale factor.
535 PngMap::const_iterator available_png_it = png_map_.end(); 537 PngMap::const_iterator available_png_it = png_map_.end();
536 for (PngMap::const_iterator png_it = png_map_.begin(); 538 for (PngMap::const_iterator png_it = png_map_.begin();
537 png_it != png_map_.end(); ++png_it) { 539 png_it != png_map_.end(); ++png_it) {
538 if (available_png_it == png_map_.end() || 540 if (available_png_it == png_map_.end() ||
539 ui::GetImageScale(png_it->first) > 541 ui::GetScaleFactorScale(png_it->first) >
540 ui::GetImageScale(available_png_it->first)) { 542 ui::GetScaleFactorScale(available_png_it->first)) {
541 available_png_it = png_it; 543 available_png_it = png_it;
542 } 544 }
543 } 545 }
544 if (available_png_it == png_map_.end()) 546 if (available_png_it == png_map_.end())
545 return gfx::ImageSkiaRep(); 547 return gfx::ImageSkiaRep();
546 ui::ScaleFactor available_scale_factor = available_png_it->first; 548 ui::ScaleFactor available_scale_factor = available_png_it->first;
547 549
548 // Look up the bitmap for |available_scale_factor| in the bitmap map. 550 // Look up the bitmap for |available_scale_factor| in the bitmap map.
549 // If not found, decode the corresponging png data, store the result 551 // If not found, decode the corresponging png data, store the result
550 // in the bitmap map. 552 // in the bitmap map.
(...skipping 11 matching lines...) Expand all
562 available_bitmap_it = bitmap_map_.find(available_scale_factor); 564 available_bitmap_it = bitmap_map_.find(available_scale_factor);
563 } 565 }
564 566
565 // Scale the available bitmap to the desired scale factor, store the result 567 // Scale the available bitmap to the desired scale factor, store the result
566 // in the bitmap map and return it. 568 // in the bitmap map and return it.
567 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap( 569 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
568 available_bitmap_it->second, 570 available_bitmap_it->second,
569 available_scale_factor, 571 available_scale_factor,
570 scale_factor); 572 scale_factor);
571 bitmap_map_[scale_factor] = scaled_bitmap; 573 bitmap_map_[scale_factor] = scaled_bitmap;
572 return gfx::ImageSkiaRep(scaled_bitmap, scale); 574 return gfx::ImageSkiaRep(scaled_bitmap, scale_factor);
573 } 575 }
574 576
575 PngMap png_map_; 577 PngMap png_map_;
576 578
577 typedef std::map<ui::ScaleFactor, SkBitmap> BitmapMap; 579 typedef std::map<ui::ScaleFactor, SkBitmap> BitmapMap;
578 BitmapMap bitmap_map_; 580 BitmapMap bitmap_map_;
579 581
580 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource); 582 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource);
581 }; 583 };
582 584
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
1276 return false; 1278 return false;
1277 } 1279 }
1278 if (is_copyable) { 1280 if (is_copyable) {
1279 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); 1281 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
1280 image_memory_[raw_id] = raw_data; 1282 image_memory_[raw_id] = raw_data;
1281 } else { 1283 } else {
1282 SkBitmap bitmap; 1284 SkBitmap bitmap;
1283 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), 1285 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(),
1284 &bitmap)) { 1286 &bitmap)) {
1285 image_skia.AddRepresentation( 1287 image_skia.AddRepresentation(
1286 gfx::ImageSkiaRep(bitmap, 1288 gfx::ImageSkiaRep(bitmap, scale_factor));
1287 ui::GetImageScale(scale_factor)));
1288 } else { 1289 } else {
1289 NOTREACHED() << "Unable to decode theme image resource " 1290 NOTREACHED() << "Unable to decode theme image resource "
1290 << it->first; 1291 << it->first;
1291 } 1292 }
1292 } 1293 }
1293 } 1294 }
1294 } 1295 }
1295 if (!is_copyable && !image_skia.isNull()) 1296 if (!is_copyable && !image_skia.isNull())
1296 (*image_cache)[prs_id] = gfx::Image(image_skia); 1297 (*image_cache)[prs_id] = gfx::Image(image_skia);
1297 } 1298 }
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1465 NOTREACHED() << "No image reps for resource " << it->first << "."; 1466 NOTREACHED() << "No image reps for resource " << it->first << ".";
1466 } 1467 }
1467 for (ImageSkiaReps::iterator rep_it = image_reps.begin(); 1468 for (ImageSkiaReps::iterator rep_it = image_reps.begin();
1468 rep_it != image_reps.end(); ++rep_it) { 1469 rep_it != image_reps.end(); ++rep_it) {
1469 std::vector<unsigned char> bitmap_data; 1470 std::vector<unsigned char> bitmap_data;
1470 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it->sk_bitmap(), false, 1471 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it->sk_bitmap(), false,
1471 &bitmap_data)) { 1472 &bitmap_data)) {
1472 NOTREACHED() << "Image file for resource " << it->first 1473 NOTREACHED() << "Image file for resource " << it->first
1473 << " could not be encoded."; 1474 << " could not be encoded.";
1474 } 1475 }
1475 int raw_id = GetRawIDByPersistentID( 1476 int raw_id = GetRawIDByPersistentID(it->first, rep_it->scale_factor());
1476 it->first,
1477 ui::GetSupportedScaleFactor(rep_it->scale()));
1478 (*reencoded_images)[raw_id] = 1477 (*reencoded_images)[raw_id] =
1479 base::RefCountedBytes::TakeVector(&bitmap_data); 1478 base::RefCountedBytes::TakeVector(&bitmap_data);
1480 } 1479 }
1481 } 1480 }
1482 } 1481 }
1483 1482
1484 void BrowserThemePack::MergeImageCaches( 1483 void BrowserThemePack::MergeImageCaches(
1485 const ImageCache& source, ImageCache* destination) const { 1484 const ImageCache& source, ImageCache* destination) const {
1486 for (ImageCache::const_iterator it = source.begin(); it != source.end(); 1485 for (ImageCache::const_iterator it = source.begin(); it != source.end();
1487 ++it) { 1486 ++it) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1527 return -1; 1526 return -1;
1528 } 1527 }
1529 1528
1530 bool BrowserThemePack::GetScaleFactorFromManifestKey( 1529 bool BrowserThemePack::GetScaleFactorFromManifestKey(
1531 const std::string& key, 1530 const std::string& key,
1532 ui::ScaleFactor* scale_factor) const { 1531 ui::ScaleFactor* scale_factor) const {
1533 int percent = 0; 1532 int percent = 0;
1534 if (base::StringToInt(key, &percent)) { 1533 if (base::StringToInt(key, &percent)) {
1535 float scale = static_cast<float>(percent) / 100.0f; 1534 float scale = static_cast<float>(percent) / 100.0f;
1536 for (size_t i = 0; i < scale_factors_.size(); ++i) { 1535 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1537 if (fabs(ui::GetImageScale(scale_factors_[i]) - scale) < 0.001) { 1536 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) {
1538 *scale_factor = scale_factors_[i]; 1537 *scale_factor = scale_factors_[i];
1539 return true; 1538 return true;
1540 } 1539 }
1541 } 1540 }
1542 } 1541 }
1543 return false; 1542 return false;
1544 } 1543 }
1545 1544
1546 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { 1545 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) {
1547 // Compute (by scaling) bitmaps for |prs_id| for any scale factors 1546 // Compute (by scaling) bitmaps for |prs_id| for any scale factors
(...skipping 16 matching lines...) Expand all
1564 } 1563 }
1565 } 1564 }
1566 if (!image_missing) 1565 if (!image_missing)
1567 return; 1566 return;
1568 1567
1569 // Find available scale factor with highest scale. 1568 // Find available scale factor with highest scale.
1570 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; 1569 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE;
1571 for (size_t i = 0; i < scale_factors_.size(); ++i) { 1570 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1572 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); 1571 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1573 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || 1572 if ((available_scale_factor == ui::SCALE_FACTOR_NONE ||
1574 (ui::GetImageScale(scale_factors_[i]) > 1573 (ui::GetScaleFactorScale(scale_factors_[i]) >
1575 ui::GetImageScale(available_scale_factor))) && 1574 ui::GetScaleFactorScale(available_scale_factor))) &&
1576 image_memory_.find(raw_id) != image_memory_.end()) { 1575 image_memory_.find(raw_id) != image_memory_.end()) {
1577 available_scale_factor = scale_factors_[i]; 1576 available_scale_factor = scale_factors_[i];
1578 } 1577 }
1579 } 1578 }
1580 // If no scale factor is available, we're done. 1579 // If no scale factor is available, we're done.
1581 if (available_scale_factor == ui::SCALE_FACTOR_NONE) 1580 if (available_scale_factor == ui::SCALE_FACTOR_NONE)
1582 return; 1581 return;
1583 1582
1584 // Get bitmap for the available scale factor. 1583 // Get bitmap for the available scale factor.
1585 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); 1584 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor);
(...skipping 21 matching lines...) Expand all
1607 false, 1606 false,
1608 &bitmap_data)) { 1607 &bitmap_data)) {
1609 NOTREACHED() << "Unable to encode theme image for prs_id=" 1608 NOTREACHED() << "Unable to encode theme image for prs_id="
1610 << prs_id << " for scale_factor=" << scale_factors_[i]; 1609 << prs_id << " for scale_factor=" << scale_factors_[i];
1611 break; 1610 break;
1612 } 1611 }
1613 image_memory_[scaled_raw_id] = 1612 image_memory_[scaled_raw_id] =
1614 base::RefCountedBytes::TakeVector(&bitmap_data); 1613 base::RefCountedBytes::TakeVector(&bitmap_data);
1615 } 1614 }
1616 } 1615 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698