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

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

Powered by Google App Engine
This is Rietveld 408576698