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

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

Issue 24175004: Remove dependency on ui::ScaleFactor from ui/gfx (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix new usage of scale in FastShowPickler 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::GetScaleFactorScale(expected[index])) 248 if (scales[index] != ui::GetImageScale(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::GetScaleFactorScale(scale_factors[i]); 259 scales[i] = ui::GetImageScale(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::GetScaleFactorScale(desired_scale_factor) / 455 ui::GetImageScale(desired_scale_factor) /
456 ui::GetScaleFactorScale(source_scale_factor))); 456 ui::GetImageScale(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( 480 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
481 ui::ScaleFactor scale_factor) OVERRIDE { 481 if (source_.HasRepresentation(scale))
482 if (source_.HasRepresentation(scale_factor)) 482 return source_.GetRepresentation(scale);
483 return source_.GetRepresentation(scale_factor); 483 const gfx::ImageSkiaRep& rep_100p = source_.GetRepresentation(1.0f);
484 const gfx::ImageSkiaRep& rep_100p =
485 source_.GetRepresentation(ui::SCALE_FACTOR_100P);
486 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap( 484 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
487 rep_100p.sk_bitmap(), 485 rep_100p.sk_bitmap(),
488 ui::SCALE_FACTOR_100P, 486 ui::SCALE_FACTOR_100P,
489 scale_factor); 487 ui::GetSupportedScaleFactor(scale));
490 return gfx::ImageSkiaRep(scaled_bitmap, scale_factor); 488 return gfx::ImageSkiaRep(scaled_bitmap, scale);
491 } 489 }
492 490
493 private: 491 private:
494 const gfx::ImageSkia source_; 492 const gfx::ImageSkia source_;
495 493
496 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource); 494 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource);
497 }; 495 };
498 496
499 // An ImageSkiaSource that delays decoding PNG data into bitmaps until 497 // An ImageSkiaSource that delays decoding PNG data into bitmaps until
500 // needed. Missing data for a scale factor is computed by scaling data for an 498 // needed. Missing data for a scale factor is computed by scaling data for an
501 // available scale factor. Computed bitmaps are stored for future look up. 499 // available scale factor. Computed bitmaps are stored for future look up.
502 class ThemeImagePngSource : public gfx::ImageSkiaSource { 500 class ThemeImagePngSource : public gfx::ImageSkiaSource {
503 public: 501 public:
504 typedef std::map<ui::ScaleFactor, 502 typedef std::map<ui::ScaleFactor,
505 scoped_refptr<base::RefCountedMemory> > PngMap; 503 scoped_refptr<base::RefCountedMemory> > PngMap;
506 504
507 explicit ThemeImagePngSource(const PngMap& png_map) : png_map_(png_map) {} 505 explicit ThemeImagePngSource(const PngMap& png_map) : png_map_(png_map) {}
508 506
509 virtual ~ThemeImagePngSource() {} 507 virtual ~ThemeImagePngSource() {}
510 508
511 private: 509 private:
512 virtual gfx::ImageSkiaRep GetImageForScale( 510 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
513 ui::ScaleFactor scale_factor) OVERRIDE { 511 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale);
514 // Look up the bitmap for |scale factor| in the bitmap map. If found 512 // Look up the bitmap for |scale factor| in the bitmap map. If found
515 // return it. 513 // return it.
516 BitmapMap::const_iterator exact_bitmap_it = bitmap_map_.find(scale_factor); 514 BitmapMap::const_iterator exact_bitmap_it = bitmap_map_.find(scale_factor);
517 if (exact_bitmap_it != bitmap_map_.end()) 515 if (exact_bitmap_it != bitmap_map_.end())
518 return gfx::ImageSkiaRep(exact_bitmap_it->second, scale_factor); 516 return gfx::ImageSkiaRep(exact_bitmap_it->second, scale);
519 517
520 // Look up the raw PNG data for |scale_factor| in the png map. If found, 518 // Look up the raw PNG data for |scale_factor| in the png map. If found,
521 // decode it, store the result in the bitmap map and return it. 519 // decode it, store the result in the bitmap map and return it.
522 PngMap::const_iterator exact_png_it = png_map_.find(scale_factor); 520 PngMap::const_iterator exact_png_it = png_map_.find(scale_factor);
523 if (exact_png_it != png_map_.end()) { 521 if (exact_png_it != png_map_.end()) {
524 SkBitmap bitmap; 522 SkBitmap bitmap;
525 if (!gfx::PNGCodec::Decode(exact_png_it->second->front(), 523 if (!gfx::PNGCodec::Decode(exact_png_it->second->front(),
526 exact_png_it->second->size(), 524 exact_png_it->second->size(),
527 &bitmap)) { 525 &bitmap)) {
528 NOTREACHED(); 526 NOTREACHED();
529 return gfx::ImageSkiaRep(); 527 return gfx::ImageSkiaRep();
530 } 528 }
531 bitmap_map_[scale_factor] = bitmap; 529 bitmap_map_[scale_factor] = bitmap;
532 return gfx::ImageSkiaRep(bitmap, scale_factor); 530 return gfx::ImageSkiaRep(bitmap, scale);
533 } 531 }
534 532
535 // Find an available PNG for another scale factor. We want to use the 533 // Find an available PNG for another scale factor. We want to use the
536 // highest available scale factor. 534 // highest available scale factor.
537 PngMap::const_iterator available_png_it = png_map_.end(); 535 PngMap::const_iterator available_png_it = png_map_.end();
538 for (PngMap::const_iterator png_it = png_map_.begin(); 536 for (PngMap::const_iterator png_it = png_map_.begin();
539 png_it != png_map_.end(); ++png_it) { 537 png_it != png_map_.end(); ++png_it) {
540 if (available_png_it == png_map_.end() || 538 if (available_png_it == png_map_.end() ||
541 ui::GetScaleFactorScale(png_it->first) > 539 ui::GetImageScale(png_it->first) >
542 ui::GetScaleFactorScale(available_png_it->first)) { 540 ui::GetImageScale(available_png_it->first)) {
543 available_png_it = png_it; 541 available_png_it = png_it;
544 } 542 }
545 } 543 }
546 if (available_png_it == png_map_.end()) 544 if (available_png_it == png_map_.end())
547 return gfx::ImageSkiaRep(); 545 return gfx::ImageSkiaRep();
548 ui::ScaleFactor available_scale_factor = available_png_it->first; 546 ui::ScaleFactor available_scale_factor = available_png_it->first;
549 547
550 // Look up the bitmap for |available_scale_factor| in the bitmap map. 548 // Look up the bitmap for |available_scale_factor| in the bitmap map.
551 // If not found, decode the corresponging png data, store the result 549 // If not found, decode the corresponging png data, store the result
552 // in the bitmap map. 550 // in the bitmap map.
(...skipping 11 matching lines...) Expand all
564 available_bitmap_it = bitmap_map_.find(available_scale_factor); 562 available_bitmap_it = bitmap_map_.find(available_scale_factor);
565 } 563 }
566 564
567 // Scale the available bitmap to the desired scale factor, store the result 565 // Scale the available bitmap to the desired scale factor, store the result
568 // in the bitmap map and return it. 566 // in the bitmap map and return it.
569 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap( 567 SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
570 available_bitmap_it->second, 568 available_bitmap_it->second,
571 available_scale_factor, 569 available_scale_factor,
572 scale_factor); 570 scale_factor);
573 bitmap_map_[scale_factor] = scaled_bitmap; 571 bitmap_map_[scale_factor] = scaled_bitmap;
574 return gfx::ImageSkiaRep(scaled_bitmap, scale_factor); 572 return gfx::ImageSkiaRep(scaled_bitmap, scale);
575 } 573 }
576 574
577 PngMap png_map_; 575 PngMap png_map_;
578 576
579 typedef std::map<ui::ScaleFactor, SkBitmap> BitmapMap; 577 typedef std::map<ui::ScaleFactor, SkBitmap> BitmapMap;
580 BitmapMap bitmap_map_; 578 BitmapMap bitmap_map_;
581 579
582 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource); 580 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource);
583 }; 581 };
584 582
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
1278 return false; 1276 return false;
1279 } 1277 }
1280 if (is_copyable) { 1278 if (is_copyable) {
1281 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor); 1279 int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
1282 image_memory_[raw_id] = raw_data; 1280 image_memory_[raw_id] = raw_data;
1283 } else { 1281 } else {
1284 SkBitmap bitmap; 1282 SkBitmap bitmap;
1285 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), 1283 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(),
1286 &bitmap)) { 1284 &bitmap)) {
1287 image_skia.AddRepresentation( 1285 image_skia.AddRepresentation(
1288 gfx::ImageSkiaRep(bitmap, scale_factor)); 1286 gfx::ImageSkiaRep(bitmap,
1287 ui::GetImageScale(scale_factor)));
1289 } else { 1288 } else {
1290 NOTREACHED() << "Unable to decode theme image resource " 1289 NOTREACHED() << "Unable to decode theme image resource "
1291 << it->first; 1290 << it->first;
1292 } 1291 }
1293 } 1292 }
1294 } 1293 }
1295 } 1294 }
1296 if (!is_copyable && !image_skia.isNull()) 1295 if (!is_copyable && !image_skia.isNull())
1297 (*image_cache)[prs_id] = gfx::Image(image_skia); 1296 (*image_cache)[prs_id] = gfx::Image(image_skia);
1298 } 1297 }
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 NOTREACHED() << "No image reps for resource " << it->first << "."; 1465 NOTREACHED() << "No image reps for resource " << it->first << ".";
1467 } 1466 }
1468 for (ImageSkiaReps::iterator rep_it = image_reps.begin(); 1467 for (ImageSkiaReps::iterator rep_it = image_reps.begin();
1469 rep_it != image_reps.end(); ++rep_it) { 1468 rep_it != image_reps.end(); ++rep_it) {
1470 std::vector<unsigned char> bitmap_data; 1469 std::vector<unsigned char> bitmap_data;
1471 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it->sk_bitmap(), false, 1470 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it->sk_bitmap(), false,
1472 &bitmap_data)) { 1471 &bitmap_data)) {
1473 NOTREACHED() << "Image file for resource " << it->first 1472 NOTREACHED() << "Image file for resource " << it->first
1474 << " could not be encoded."; 1473 << " could not be encoded.";
1475 } 1474 }
1476 int raw_id = GetRawIDByPersistentID(it->first, rep_it->scale_factor()); 1475 int raw_id = GetRawIDByPersistentID(
1476 it->first,
1477 ui::GetSupportedScaleFactor(rep_it->scale()));
1477 (*reencoded_images)[raw_id] = 1478 (*reencoded_images)[raw_id] =
1478 base::RefCountedBytes::TakeVector(&bitmap_data); 1479 base::RefCountedBytes::TakeVector(&bitmap_data);
1479 } 1480 }
1480 } 1481 }
1481 } 1482 }
1482 1483
1483 void BrowserThemePack::MergeImageCaches( 1484 void BrowserThemePack::MergeImageCaches(
1484 const ImageCache& source, ImageCache* destination) const { 1485 const ImageCache& source, ImageCache* destination) const {
1485 for (ImageCache::const_iterator it = source.begin(); it != source.end(); 1486 for (ImageCache::const_iterator it = source.begin(); it != source.end();
1486 ++it) { 1487 ++it) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1526 return -1; 1527 return -1;
1527 } 1528 }
1528 1529
1529 bool BrowserThemePack::GetScaleFactorFromManifestKey( 1530 bool BrowserThemePack::GetScaleFactorFromManifestKey(
1530 const std::string& key, 1531 const std::string& key,
1531 ui::ScaleFactor* scale_factor) const { 1532 ui::ScaleFactor* scale_factor) const {
1532 int percent = 0; 1533 int percent = 0;
1533 if (base::StringToInt(key, &percent)) { 1534 if (base::StringToInt(key, &percent)) {
1534 float scale = static_cast<float>(percent) / 100.0f; 1535 float scale = static_cast<float>(percent) / 100.0f;
1535 for (size_t i = 0; i < scale_factors_.size(); ++i) { 1536 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1536 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) { 1537 if (fabs(ui::GetImageScale(scale_factors_[i]) - scale) < 0.001) {
1537 *scale_factor = scale_factors_[i]; 1538 *scale_factor = scale_factors_[i];
1538 return true; 1539 return true;
1539 } 1540 }
1540 } 1541 }
1541 } 1542 }
1542 return false; 1543 return false;
1543 } 1544 }
1544 1545
1545 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) { 1546 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) {
1546 // Compute (by scaling) bitmaps for |prs_id| for any scale factors 1547 // Compute (by scaling) bitmaps for |prs_id| for any scale factors
(...skipping 16 matching lines...) Expand all
1563 } 1564 }
1564 } 1565 }
1565 if (!image_missing) 1566 if (!image_missing)
1566 return; 1567 return;
1567 1568
1568 // Find available scale factor with highest scale. 1569 // Find available scale factor with highest scale.
1569 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE; 1570 ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE;
1570 for (size_t i = 0; i < scale_factors_.size(); ++i) { 1571 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1571 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]); 1572 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1572 if ((available_scale_factor == ui::SCALE_FACTOR_NONE || 1573 if ((available_scale_factor == ui::SCALE_FACTOR_NONE ||
1573 (ui::GetScaleFactorScale(scale_factors_[i]) > 1574 (ui::GetImageScale(scale_factors_[i]) >
1574 ui::GetScaleFactorScale(available_scale_factor))) && 1575 ui::GetImageScale(available_scale_factor))) &&
1575 image_memory_.find(raw_id) != image_memory_.end()) { 1576 image_memory_.find(raw_id) != image_memory_.end()) {
1576 available_scale_factor = scale_factors_[i]; 1577 available_scale_factor = scale_factors_[i];
1577 } 1578 }
1578 } 1579 }
1579 // If no scale factor is available, we're done. 1580 // If no scale factor is available, we're done.
1580 if (available_scale_factor == ui::SCALE_FACTOR_NONE) 1581 if (available_scale_factor == ui::SCALE_FACTOR_NONE)
1581 return; 1582 return;
1582 1583
1583 // Get bitmap for the available scale factor. 1584 // Get bitmap for the available scale factor.
1584 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor); 1585 int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor);
(...skipping 21 matching lines...) Expand all
1606 false, 1607 false,
1607 &bitmap_data)) { 1608 &bitmap_data)) {
1608 NOTREACHED() << "Unable to encode theme image for prs_id=" 1609 NOTREACHED() << "Unable to encode theme image for prs_id="
1609 << prs_id << " for scale_factor=" << scale_factors_[i]; 1610 << prs_id << " for scale_factor=" << scale_factors_[i];
1610 break; 1611 break;
1611 } 1612 }
1612 image_memory_[scaled_raw_id] = 1613 image_memory_[scaled_raw_id] =
1613 base::RefCountedBytes::TakeVector(&bitmap_data); 1614 base::RefCountedBytes::TakeVector(&bitmap_data);
1614 } 1615 }
1615 } 1616 }
OLDNEW
« no previous file with comments | « chrome/browser/task_manager/browser_process_resource_provider.cc ('k') | chrome/browser/themes/browser_theme_pack_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698