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

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

Issue 16610002: High DPI support for themes: Raw images for NTP attribution and background (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 7 years, 6 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
« no previous file with comments | « chrome/browser/themes/browser_theme_pack.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 14 matching lines...) Expand all
25 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
26 #include "third_party/skia/include/core/SkCanvas.h" 26 #include "third_party/skia/include/core/SkCanvas.h"
27 #include "ui/base/resource/data_pack.h" 27 #include "ui/base/resource/data_pack.h"
28 #include "ui/base/resource/resource_bundle.h" 28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/canvas.h" 29 #include "ui/gfx/canvas.h"
30 #include "ui/gfx/codec/png_codec.h" 30 #include "ui/gfx/codec/png_codec.h"
31 #include "ui/gfx/image/canvas_image_source.h" 31 #include "ui/gfx/image/canvas_image_source.h"
32 #include "ui/gfx/image/image_skia.h" 32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/image/image_skia_operations.h" 33 #include "ui/gfx/image/image_skia_operations.h"
34 #include "ui/gfx/screen.h" 34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/size_conversions.h"
35 #include "ui/gfx/skia_util.h" 36 #include "ui/gfx/skia_util.h"
36 37
37 using content::BrowserThread; 38 using content::BrowserThread;
38 using extensions::Extension; 39 using extensions::Extension;
39 40
40 namespace { 41 namespace {
41 42
42 // Version number of the current theme pack. We just throw out and rebuild 43 // Version number of the current theme pack. We just throw out and rebuild
43 // theme packs that aren't int-equal to this. Increment this number if you 44 // theme packs that aren't int-equal to this. Increment this number if you
44 // change default theme assets. 45 // change default theme assets.
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 // ImageSkiaReps for all supported scale factors. 576 // ImageSkiaReps for all supported scale factors.
576 for (ImageCache::iterator it = pack->images_on_ui_thread_.begin(); 577 for (ImageCache::iterator it = pack->images_on_ui_thread_.begin();
577 it != pack->images_on_ui_thread_.end(); ++it) { 578 it != pack->images_on_ui_thread_.end(); ++it) {
578 const gfx::ImageSkia source_image_skia = it->second.AsImageSkia(); 579 const gfx::ImageSkia source_image_skia = it->second.AsImageSkia();
579 ThemeImageSource* source = new ThemeImageSource(source_image_skia); 580 ThemeImageSource* source = new ThemeImageSource(source_image_skia);
580 // image_skia takes ownership of source. 581 // image_skia takes ownership of source.
581 gfx::ImageSkia image_skia(source, source_image_skia.size()); 582 gfx::ImageSkia image_skia(source, source_image_skia.size());
582 it->second = gfx::Image(image_skia); 583 it->second = gfx::Image(image_skia);
583 } 584 }
584 585
586 // Generate raw images (for new-tab-page attribution and background) for
587 // any missing scale from an available scale image.
pkotwicz 2013/06/10 15:20:11 We should iterate through kPreloadIDs here.
sschmitz 2013/06/10 17:24:14 Done.
588 pack->GenerateRawImageForAllSupportedScales(PRS_THEME_NTP_BACKGROUND);
589 pack->GenerateRawImageForAllSupportedScales(PRS_THEME_NTP_ATTRIBUTION);
590
585 // The BrowserThemePack is now in a consistent state. 591 // The BrowserThemePack is now in a consistent state.
586 return pack; 592 return pack;
587 } 593 }
588 594
589 // static 595 // static
590 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( 596 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack(
591 const base::FilePath& path, const std::string& expected_id) { 597 const base::FilePath& path, const std::string& expected_id) {
592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 598 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
593 // Allow IO on UI thread due to deep-seated theme design issues. 599 // Allow IO on UI thread due to deep-seated theme design issues.
594 // (see http://crbug.com/80206) 600 // (see http://crbug.com/80206)
(...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after
1436 float scale = static_cast<float>(percent) / 100.0f; 1442 float scale = static_cast<float>(percent) / 100.0f;
1437 for (size_t i = 0; i < scale_factors_.size(); ++i) { 1443 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1438 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) { 1444 if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) {
1439 *scale_factor = scale_factors_[i]; 1445 *scale_factor = scale_factors_[i];
1440 return true; 1446 return true;
1441 } 1447 }
1442 } 1448 }
1443 } 1449 }
1444 return false; 1450 return false;
1445 } 1451 }
1452
1453 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) {
1454 // For any raw theme image for a supported scale that is not in the
pkotwicz 2013/06/10 15:20:11 How about: Compute (by scaling) bitmaps for |prs_i
sschmitz 2013/06/10 17:24:14 Done.
1455 // theme extension we compute (by scaling) an image based on an
1456 // available image. We use the available image with the highest
1457 // scale to compute the missing images.
1458
1459 // See if any image is missing. If not, we're done.
1460 bool image_missing = false;
1461 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1462 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1463 if (image_memory_.find(raw_id) == image_memory_.end()) {
1464 image_missing = true;
1465 break;
1466 }
1467 }
1468 if (!image_missing)
1469 return;
1470
1471 // Find available scale factor with highest scale.
1472 ui::ScaleFactor available = ui::SCALE_FACTOR_NONE;
1473 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1474 int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1475 if ((available == ui::SCALE_FACTOR_NONE ||
1476 (ui::GetScaleFactorScale(scale_factors_[i]) >
1477 ui::GetScaleFactorScale(available))) &&
1478 image_memory_.find(raw_id) != image_memory_.end()) {
1479 available = scale_factors_[i];
1480 }
1481 }
1482 // If no image is available, we're done.
1483 if (available == ui::SCALE_FACTOR_NONE)
1484 return;
1485
1486 // Get bitmap of the available image.
pkotwicz 2013/06/10 15:20:11 Nit: How about: Get the bitmap for the available s
sschmitz 2013/06/10 17:24:14 Done.
1487 int available_raw_id = GetRawIDByPersistentID(prs_id, available);
1488 RawImages::const_iterator it = image_memory_.find(available_raw_id);
1489 DCHECK(it != image_memory_.end());
1490 DCHECK(it->second->size() > 0);
pkotwicz 2013/06/10 15:20:11 The above DCHECK is not necessary. gfx::PNGCodec::
sschmitz 2013/06/10 17:24:14 Actually, we don't need either one. Done.
1491 SkBitmap available_bitmap;
1492 if (!gfx::PNGCodec::Decode(it->second->front(),
1493 it->second->size(),
1494 &available_bitmap)) {
1495 NOTREACHED() << "Unable to decode theme image for prs_id="
1496 << prs_id << " for scale_factor=" << available;
1497 return;
1498 }
1499
1500 // Fill in all missing scales by scaling the available image.
pkotwicz 2013/06/10 15:20:11 Nit scales -> scale factors available image ->
sschmitz 2013/06/10 17:24:14 Done.
1501 for (size_t i = 0; i < scale_factors_.size(); ++i) {
1502 int scaled_raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1503 if (image_memory_.find(scaled_raw_id) != image_memory_.end())
1504 continue;
1505 gfx::Size scaled_size = gfx::ToCeiledSize(
1506 gfx::ScaleSize(gfx::Size(available_bitmap.width(),
1507 available_bitmap.height()),
1508 ui::GetScaleFactorScale(scale_factors_[i]) /
1509 ui::GetScaleFactorScale(available)));
1510 SkBitmap scaled_bitmap;
1511 scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config,
1512 scaled_size.width(),
1513 scaled_size.height());
1514 if (!scaled_bitmap.allocPixels())
1515 SK_CRASH();
1516 scaled_bitmap.eraseARGB(0, 0, 0, 0);
1517 SkCanvas canvas(scaled_bitmap);
1518 SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size));
1519 canvas.drawBitmapRect(available_bitmap, NULL, scaled_bounds);
1520 std::vector<unsigned char> bitmap_data;
1521 if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap,
1522 false,
1523 &bitmap_data)) {
1524 NOTREACHED() << "Unable to encode theme image for prs_id="
1525 << prs_id << " for scale_factor=" << scale_factors_[i];
1526 break;
1527 }
1528 image_memory_[scaled_raw_id] =
1529 base::RefCountedBytes::TakeVector(&bitmap_data);
1530 }
1531 }
OLDNEW
« no previous file with comments | « chrome/browser/themes/browser_theme_pack.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698