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

Side by Side Diff: ui/base/resource/resource_bundle.cc

Issue 10928231: Fix ResourceBundleImageSource (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 8 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
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | 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 "ui/base/resource/resource_bundle.h" 5 #include "ui/base/resource/resource_bundle.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 27 matching lines...) Expand all
38 // Font sizes relative to base font. 38 // Font sizes relative to base font.
39 const int kSmallFontSizeDelta = -2; 39 const int kSmallFontSizeDelta = -2;
40 const int kMediumFontSizeDelta = 3; 40 const int kMediumFontSizeDelta = 3;
41 const int kLargeFontSizeDelta = 8; 41 const int kLargeFontSizeDelta = 8;
42 42
43 ResourceBundle* g_shared_instance_ = NULL; 43 ResourceBundle* g_shared_instance_ = NULL;
44 44
45 // Returns the actual scale factor of |bitmap| given the image representations 45 // Returns the actual scale factor of |bitmap| given the image representations
46 // which have already been added to |image|. 46 // which have already been added to |image|.
47 // TODO(pkotwicz): Remove this once we are no longer loading 1x resources 47 // TODO(pkotwicz): Remove this once we are no longer loading 1x resources
48 // as part of 2x data packs. 48 // as part of non 1x data packs.
49 ui::ScaleFactor GetActualScaleFactor(const gfx::ImageSkia& image, 49 ui::ScaleFactor GetActualScaleFactor(const gfx::ImageSkia& image,
50 const SkBitmap& bitmap, 50 const SkBitmap& bitmap,
51 ui::ScaleFactor data_pack_scale_factor) { 51 ui::ScaleFactor data_pack_scale_factor) {
52 if (image.isNull()) 52 if (image.isNull())
53 return data_pack_scale_factor; 53 return data_pack_scale_factor;
54 54
55 return ui::GetScaleFactorFromScale( 55 return ui::GetScaleFactorFromScale(
56 static_cast<float>(bitmap.width()) / image.width()); 56 static_cast<float>(bitmap.width()) / image.width());
57 } 57 }
58 58
59 bool ShouldHighlightMissing2xResources() { 59 bool ShouldHighlightMissingScaledResources() {
60 return CommandLine::ForCurrentProcess()->HasSwitch( 60 return CommandLine::ForCurrentProcess()->HasSwitch(
61 switches::kHighlightMissing2xResources); 61 switches::kHighlightMissingScaledResources);
62 } 62 }
63 63
64 } // namespace 64 } // namespace
65 65
66 // An ImageSkiaSource that loads bitmaps for given scale factor from 66 // An ImageSkiaSource that loads bitmaps for requested scale factor from
67 // ResourceBundle on demand for given resource_id. It falls back 67 // ResourceBundle on demand for given resource_id. It falls back
68 // to 100P image if corresponding 200P image doesn't exist. 68 // to the 1x bitmap if the bitmap for the requested scale factor does not
69 // If 200P image does not have 2x size of 100P images, it will end up 69 // exist. If the resource for the requested scale factor is not exactly
70 // with broken UI because it will be drawn as if it has 2x size. 70 // |scale_factor| * the size of the 1x resource, it will end up with
71 // When --highlight-missing-2x-resources flag is specified, it 71 // broken UI because it will be drawn as if the bitmap was the correct size.
72 // When --highlight-missing-scaled-resources flag is specified, it
72 // will show the scaled image blended with red instead. 73 // will show the scaled image blended with red instead.
73 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { 74 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource {
74 public: 75 public:
75 ResourceBundleImageSource(int resource_id, const gfx::Size& size_in_dip) 76 ResourceBundleImageSource(ResourceBundle* rb,
76 : resource_id_(resource_id), 77 int resource_id,
78 const gfx::Size& size_in_dip)
79 : rb_(rb),
80 resource_id_(resource_id),
77 size_in_dip_(size_in_dip) { 81 size_in_dip_(size_in_dip) {
78 } 82 }
79 virtual ~ResourceBundleImageSource() {} 83 virtual ~ResourceBundleImageSource() {}
80 84
81 // gfx::ImageSkiaSource overrides: 85 // gfx::ImageSkiaSource overrides:
82 virtual gfx::ImageSkiaRep GetImageForScale( 86 virtual gfx::ImageSkiaRep GetImageForScale(
83 ui::ScaleFactor scale_factor) OVERRIDE { 87 ui::ScaleFactor scale_factor) OVERRIDE {
84 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 88 scoped_ptr<SkBitmap> result(rb_->LoadBitmap(resource_id_, scale_factor));
89 float scale = ui::GetScaleFactorScale(scale_factor);
90 gfx::Size size_in_pixel = size_in_dip_.Scale(scale);
85 91
86 scoped_ptr<SkBitmap> result(rb.LoadBitmap(resource_id_, scale_factor)); 92 if (scale_factor != SCALE_FACTOR_100P &&
87 gfx::Size size_in_pixel =
88 size_in_dip_.Scale(ui::GetScaleFactorScale(scale_factor));
89
90 if (scale_factor == SCALE_FACTOR_200P &&
91 (!result.get() || 93 (!result.get() ||
92 result->width() != size_in_pixel.width() || 94 result->width() != size_in_pixel.width() ||
93 result->height() != size_in_pixel.height())) { 95 result->height() != size_in_pixel.height())) {
94 96
95 // If 2x resource is missing from |image| or is the incorrect 97 // If non 1x resource is missing from |image| or is the incorrect
96 // size and --highlight-missing-2x-resources is specified, logs 98 // size and --highlight-missing-scaled-resources is specified, logs
97 // the resource id and creates a 2x version of the resource. 99 // the resource id and creates a version of the resource at the correct
98 // Blends the created resource with red to make it 100 // size. Blends the created resource with red to make it
99 // distinguishable from bitmaps in the resource pak. 101 // distinguishable from bitmaps in the resource pak.
100 if (ShouldHighlightMissing2xResources()) { 102 if (ShouldHighlightMissingScaledResources()) {
101 if (!result.get()) 103 if (!result.get()) {
102 LOG(ERROR) << "Missing 2x resource. id=" << resource_id_; 104 LOG(ERROR) << "Missing " << scale << "x resource. id="
103 else 105 << resource_id_;
104 LOG(ERROR) << "Incorrectly sized 2x resource. id=" << resource_id_; 106 } else {
107 LOG(ERROR) << "Incorrectly sized " << scale << "x resource. id="
108 << resource_id_;
109 }
105 110
106 SkBitmap bitmap1x = *(rb.LoadBitmap(resource_id_, SCALE_FACTOR_100P)); 111 scoped_ptr<SkBitmap> bitmap1x(
107 SkBitmap bitmap2x = skia::ImageOperations::Resize( 112 rb_->LoadBitmap(resource_id_, SCALE_FACTOR_100P));
108 bitmap1x, 113 DCHECK(bitmap1x.get());
114 SkBitmap bitmap_scaled = skia::ImageOperations::Resize(
115 *bitmap1x,
109 skia::ImageOperations::RESIZE_LANCZOS3, 116 skia::ImageOperations::RESIZE_LANCZOS3,
110 bitmap1x.width() * 2, bitmap1x.height() * 2); 117 size_in_pixel.width(),
118 size_in_pixel.height());
111 119
112 SkBitmap mask; 120 SkBitmap mask;
113 mask.setConfig(SkBitmap::kARGB_8888_Config, 121 mask.setConfig(SkBitmap::kARGB_8888_Config,
114 bitmap2x.width(), 122 bitmap_scaled.width(),
115 bitmap2x.height()); 123 bitmap_scaled.height());
116 mask.allocPixels(); 124 mask.allocPixels();
117 mask.eraseColor(SK_ColorRED); 125 mask.eraseColor(SK_ColorRED);
118 result.reset(new SkBitmap()); 126 result.reset(new SkBitmap());
119 *result.get() = SkBitmapOperations::CreateBlendedBitmap(bitmap2x, mask, 127 *result.get() = SkBitmapOperations::CreateBlendedBitmap(
120 0.2); 128 bitmap_scaled, mask, 0.2);
121 } else if (!result.get() || 129 } else if (!result.get() || result->width() == size_in_dip_.width()) {
122 result->width() == size_in_dip_.width()) { 130 // The scaled resource pack may have the 1x image if its grd file
123 // The 2x resource pack may have the 1x image if its grd file
124 // points to 1x image. Fallback to 1x by returning empty image 131 // points to 1x image. Fallback to 1x by returning empty image
125 // in this case. This 1x image will be scaled when drawn. 132 // in this case. This 1x image will be scaled when drawn.
126 return gfx::ImageSkiaRep(); 133 return gfx::ImageSkiaRep();
127 } 134 }
128 // If the size of 2x image isn't exactly 2x of 1x version, 135 // If the size of scaled image isn't exactly |scale| * 1x version,
129 // create ImageSkia as usual. This will end up with 136 // create ImageSkia as usual. This will end up with
130 // corrupted visual representation as the size of image doesn't 137 // corrupted visual representation as the size of image doesn't
131 // match the expected size. 138 // match the expected size.
132 } 139 }
140 DCHECK(result.get());
133 return gfx::ImageSkiaRep(*result.get(), scale_factor); 141 return gfx::ImageSkiaRep(*result.get(), scale_factor);
134 } 142 }
135 143
136 private: 144 private:
145 ResourceBundle* rb_;
137 const int resource_id_; 146 const int resource_id_;
138 const gfx::Size size_in_dip_; 147 const gfx::Size size_in_dip_;
139 148
140 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); 149 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource);
141 }; 150 };
142 151
143 // static 152 // static
144 std::string ResourceBundle::InitSharedInstanceWithLocale( 153 std::string ResourceBundle::InitSharedInstanceWithLocale(
145 const std::string& pref_locale, Delegate* delegate) { 154 const std::string& pref_locale, Delegate* delegate) {
146 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; 155 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 360
352 // TODO(oshima): Pick the scale factor from currently used scale factors. 361 // TODO(oshima): Pick the scale factor from currently used scale factors.
353 scoped_ptr<SkBitmap> bitmap(LoadBitmap(resource_id, SCALE_FACTOR_100P)); 362 scoped_ptr<SkBitmap> bitmap(LoadBitmap(resource_id, SCALE_FACTOR_100P));
354 if (!bitmap.get()) { 363 if (!bitmap.get()) {
355 LOG(WARNING) << "Unable to load image with id " << resource_id; 364 LOG(WARNING) << "Unable to load image with id " << resource_id;
356 NOTREACHED(); // Want to assert in debug mode. 365 NOTREACHED(); // Want to assert in debug mode.
357 // The load failed to retrieve the image; show a debugging red square. 366 // The load failed to retrieve the image; show a debugging red square.
358 return GetEmptyImage(); 367 return GetEmptyImage();
359 } 368 }
360 369
370 // ResourceBundle::GetSharedInstance() is destroyed after the
371 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be
372 // destroyed before the resource bundle is destroyed.
361 gfx::Size size_in_dip(bitmap->width(), bitmap->height()); 373 gfx::Size size_in_dip(bitmap->width(), bitmap->height());
362 gfx::ImageSkia image_skia( 374 gfx::ImageSkia image_skia(
363 new ResourceBundleImageSource(resource_id, size_in_dip), 375 new ResourceBundleImageSource(this, resource_id, size_in_dip),
364 size_in_dip); 376 size_in_dip);
365 image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap.get(), 377 image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap.get(),
366 SCALE_FACTOR_100P)); 378 SCALE_FACTOR_100P));
367 image_skia.SetReadOnly(); 379 image_skia.SetReadOnly();
368 image = gfx::Image(image_skia); 380 image = gfx::Image(image_skia);
369 } 381 }
370 382
371 // The load was successful, so cache the image. 383 // The load was successful, so cache the image.
372 base::AutoLock lock_scope(*images_and_fonts_lock_); 384 base::AutoLock lock_scope(*images_and_fonts_lock_);
373 385
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 SkBitmap bitmap; 644 SkBitmap bitmap;
633 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); 645 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
634 bitmap.allocPixels(); 646 bitmap.allocPixels();
635 bitmap.eraseARGB(255, 255, 0, 0); 647 bitmap.eraseARGB(255, 255, 0, 0);
636 empty_image_ = gfx::Image(bitmap); 648 empty_image_ = gfx::Image(bitmap);
637 } 649 }
638 return empty_image_; 650 return empty_image_;
639 } 651 }
640 652
641 } // namespace ui 653 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698