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

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: Fixed unittest crash 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
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(const base::WeakPtr<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 // Return early if the resource bundle was destroyed.
89 if (!rb_.get())
90 return gfx::ImageSkiaRep();
85 91
86 scoped_ptr<SkBitmap> result(rb.LoadBitmap(resource_id_, scale_factor)); 92 scoped_ptr<SkBitmap> result(rb_->LoadBitmap(resource_id_, scale_factor));
87 gfx::Size size_in_pixel = 93 float scale = ui::GetScaleFactorScale(scale_factor);
88 size_in_dip_.Scale(ui::GetScaleFactorScale(scale_factor)); 94 gfx::Size size_in_pixel = size_in_dip_.Scale(scale);
89 95
90 if (scale_factor == SCALE_FACTOR_200P && 96 if (scale_factor != SCALE_FACTOR_100P &&
91 (!result.get() || 97 (!result.get() ||
92 result->width() != size_in_pixel.width() || 98 result->width() != size_in_pixel.width() ||
93 result->height() != size_in_pixel.height())) { 99 result->height() != size_in_pixel.height())) {
94 100
95 // If 2x resource is missing from |image| or is the incorrect 101 // If non 1x resource is missing from |image| or is the incorrect
96 // size and --highlight-missing-2x-resources is specified, logs 102 // size and --highlight-missing-scaled-resources is specified, logs
97 // the resource id and creates a 2x version of the resource. 103 // the resource id and creates a version of the resource at the correct
98 // Blends the created resource with red to make it 104 // size. Blends the created resource with red to make it
99 // distinguishable from bitmaps in the resource pak. 105 // distinguishable from bitmaps in the resource pak.
100 if (ShouldHighlightMissing2xResources()) { 106 if (ShouldHighlightMissingScaledResources()) {
101 if (!result.get()) 107 if (!result.get()) {
102 LOG(ERROR) << "Missing 2x resource. id=" << resource_id_; 108 LOG(ERROR) << "Missing " << scale << "x resource. id="
103 else 109 << resource_id_;
104 LOG(ERROR) << "Incorrectly sized 2x resource. id=" << resource_id_; 110 } else {
111 LOG(ERROR) << "Incorrectly sized " << scale << "x resource. id="
112 << resource_id_;
113 }
105 114
106 SkBitmap bitmap1x = *(rb.LoadBitmap(resource_id_, SCALE_FACTOR_100P)); 115 scoped_ptr<SkBitmap> bitmap1x(
107 SkBitmap bitmap2x = skia::ImageOperations::Resize( 116 rb_->LoadBitmap(resource_id_, SCALE_FACTOR_100P));
108 bitmap1x, 117 DCHECK(bitmap1x.get());
118 SkBitmap bitmap_scaled = skia::ImageOperations::Resize(
119 *bitmap1x,
109 skia::ImageOperations::RESIZE_LANCZOS3, 120 skia::ImageOperations::RESIZE_LANCZOS3,
110 bitmap1x.width() * 2, bitmap1x.height() * 2); 121 size_in_pixel.width(),
122 size_in_pixel.height());
111 123
112 SkBitmap mask; 124 SkBitmap mask;
113 mask.setConfig(SkBitmap::kARGB_8888_Config, 125 mask.setConfig(SkBitmap::kARGB_8888_Config,
114 bitmap2x.width(), 126 bitmap_scaled.width(),
115 bitmap2x.height()); 127 bitmap_scaled.height());
116 mask.allocPixels(); 128 mask.allocPixels();
117 mask.eraseColor(SK_ColorRED); 129 mask.eraseColor(SK_ColorRED);
118 result.reset(new SkBitmap()); 130 result.reset(new SkBitmap());
119 *result.get() = SkBitmapOperations::CreateBlendedBitmap(bitmap2x, mask, 131 *result.get() = SkBitmapOperations::CreateBlendedBitmap(
120 0.2); 132 bitmap_scaled, mask, 0.2);
121 } else if (!result.get() || 133 } else if (!result.get() || result->width() == size_in_dip_.width()) {
122 result->width() == size_in_dip_.width()) { 134 // 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 135 // points to 1x image. Fallback to 1x by returning empty image
125 // in this case. This 1x image will be scaled when drawn. 136 // in this case. This 1x image will be scaled when drawn.
126 return gfx::ImageSkiaRep(); 137 return gfx::ImageSkiaRep();
127 } 138 }
128 // If the size of 2x image isn't exactly 2x of 1x version, 139 // If the size of scaled image isn't exactly |scale| * 1x version,
129 // create ImageSkia as usual. This will end up with 140 // create ImageSkia as usual. This will end up with
130 // corrupted visual representation as the size of image doesn't 141 // corrupted visual representation as the size of image doesn't
131 // match the expected size. 142 // match the expected size.
132 } 143 }
144 DCHECK(result.get());
133 return gfx::ImageSkiaRep(*result.get(), scale_factor); 145 return gfx::ImageSkiaRep(*result.get(), scale_factor);
134 } 146 }
135 147
136 private: 148 private:
149 base::WeakPtr<ResourceBundle> rb_;
137 const int resource_id_; 150 const int resource_id_;
138 const gfx::Size size_in_dip_; 151 const gfx::Size size_in_dip_;
139 152
140 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); 153 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource);
141 }; 154 };
142 155
143 // static 156 // static
144 std::string ResourceBundle::InitSharedInstanceWithLocale( 157 std::string ResourceBundle::InitSharedInstanceWithLocale(
145 const std::string& pref_locale, Delegate* delegate) { 158 const std::string& pref_locale, Delegate* delegate) {
146 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; 159 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 scoped_ptr<SkBitmap> bitmap(LoadBitmap(resource_id, SCALE_FACTOR_100P)); 366 scoped_ptr<SkBitmap> bitmap(LoadBitmap(resource_id, SCALE_FACTOR_100P));
354 if (!bitmap.get()) { 367 if (!bitmap.get()) {
355 LOG(WARNING) << "Unable to load image with id " << resource_id; 368 LOG(WARNING) << "Unable to load image with id " << resource_id;
356 NOTREACHED(); // Want to assert in debug mode. 369 NOTREACHED(); // Want to assert in debug mode.
357 // The load failed to retrieve the image; show a debugging red square. 370 // The load failed to retrieve the image; show a debugging red square.
358 return GetEmptyImage(); 371 return GetEmptyImage();
359 } 372 }
360 373
361 gfx::Size size_in_dip(bitmap->width(), bitmap->height()); 374 gfx::Size size_in_dip(bitmap->width(), bitmap->height());
362 gfx::ImageSkia image_skia( 375 gfx::ImageSkia image_skia(
363 new ResourceBundleImageSource(resource_id, size_in_dip), 376 new ResourceBundleImageSource(AsWeakPtr(), resource_id, size_in_dip),
364 size_in_dip); 377 size_in_dip);
365 image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap.get(), 378 image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap.get(),
366 SCALE_FACTOR_100P)); 379 SCALE_FACTOR_100P));
367 image_skia.SetReadOnly(); 380 image_skia.SetReadOnly();
368 image = gfx::Image(image_skia); 381 image = gfx::Image(image_skia);
369 } 382 }
370 383
371 // The load was successful, so cache the image. 384 // The load was successful, so cache the image.
372 base::AutoLock lock_scope(*images_and_fonts_lock_); 385 base::AutoLock lock_scope(*images_and_fonts_lock_);
373 386
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 SkBitmap bitmap; 645 SkBitmap bitmap;
633 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); 646 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
634 bitmap.allocPixels(); 647 bitmap.allocPixels();
635 bitmap.eraseARGB(255, 255, 0, 0); 648 bitmap.eraseARGB(255, 255, 0, 0);
636 empty_image_ = gfx::Image(bitmap); 649 empty_image_ = gfx::Image(bitmap);
637 } 650 }
638 return empty_image_; 651 return empty_image_;
639 } 652 }
640 653
641 } // namespace ui 654 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698