OLD | NEW |
---|---|
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 Loading... | |
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 given scale factor from |
huangs
2012/09/17 17:53:45
This block of comments should be updated.
(using "
| |
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 100P image if corresponding 200P image doesn't exist. |
69 // If 200P image does not have 2x size of 100P images, it will end up | 69 // If 200P image does not have 2x size of 100P images, it will end up |
70 // with broken UI because it will be drawn as if it has 2x size. | 70 // with broken UI because it will be drawn as if it has 2x size. |
71 // When --highlight-missing-2x-resources flag is specified, it | 71 // When --highlight-missing-2x-resources flag is specified, it |
72 // will show the scaled image blended with red instead. | 72 // will show the scaled image blended with red instead. |
73 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { | 73 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { |
74 public: | 74 public: |
75 ResourceBundleImageSource(int resource_id, const gfx::Size& size_in_dip) | 75 ResourceBundleImageSource(int resource_id, const gfx::Size& size_in_dip) |
76 : resource_id_(resource_id), | 76 : resource_id_(resource_id), |
77 size_in_dip_(size_in_dip) { | 77 size_in_dip_(size_in_dip) { |
78 } | 78 } |
79 virtual ~ResourceBundleImageSource() {} | 79 virtual ~ResourceBundleImageSource() {} |
80 | 80 |
81 // gfx::ImageSkiaSource overrides: | 81 // gfx::ImageSkiaSource overrides: |
82 virtual gfx::ImageSkiaRep GetImageForScale( | 82 virtual gfx::ImageSkiaRep GetImageForScale( |
83 ui::ScaleFactor scale_factor) OVERRIDE { | 83 ui::ScaleFactor scale_factor) OVERRIDE { |
84 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 84 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
85 | 85 |
86 scoped_ptr<SkBitmap> result(rb.LoadBitmap(resource_id_, scale_factor)); | 86 scoped_ptr<SkBitmap> result(rb.LoadBitmap(resource_id_, scale_factor)); |
87 gfx::Size size_in_pixel = | 87 float scale = ui::GetScaleFactorScale(scale_factor); |
88 size_in_dip_.Scale(ui::GetScaleFactorScale(scale_factor)); | 88 gfx::Size size_in_pixel = size_in_dip_.Scale(scale); |
89 | 89 |
90 if (scale_factor == SCALE_FACTOR_200P && | 90 if (scale_factor != SCALE_FACTOR_100P && |
91 (!result.get() || | 91 (!result.get() || |
92 result->width() != size_in_pixel.width() || | 92 result->width() != size_in_pixel.width() || |
93 result->height() != size_in_pixel.height())) { | 93 result->height() != size_in_pixel.height())) { |
94 | 94 |
95 // If 2x resource is missing from |image| or is the incorrect | 95 // If 2x resource is missing from |image| or is the incorrect |
96 // size and --highlight-missing-2x-resources is specified, logs | 96 // size and --highlight-missing-2x-resources is specified, logs |
huangs
2012/09/17 17:53:45
Comments need update.
| |
97 // the resource id and creates a 2x version of the resource. | 97 // the resource id and creates a 2x version of the resource. |
98 // Blends the created resource with red to make it | 98 // Blends the created resource with red to make it |
99 // distinguishable from bitmaps in the resource pak. | 99 // distinguishable from bitmaps in the resource pak. |
100 if (ShouldHighlightMissing2xResources()) { | 100 if (ShouldHighlightMissingScaledResources()) { |
101 if (!result.get()) | 101 if (!result.get()) { |
102 LOG(ERROR) << "Missing 2x resource. id=" << resource_id_; | 102 LOG(ERROR) << "Missing " << scale << "x resource. id=" |
103 else | 103 << resource_id_; |
104 LOG(ERROR) << "Incorrectly sized 2x resource. id=" << resource_id_; | 104 } else { |
105 LOG(ERROR) << "Incorrectly sized " << scale << "x resource. id=" | |
106 << resource_id_; | |
107 } | |
105 | 108 |
106 SkBitmap bitmap1x = *(rb.LoadBitmap(resource_id_, SCALE_FACTOR_100P)); | 109 scoped_ptr<SkBitmap> bitmap1x( |
107 SkBitmap bitmap2x = skia::ImageOperations::Resize( | 110 rb.LoadBitmap(resource_id_, SCALE_FACTOR_100P)); |
108 bitmap1x, | 111 DCHECK(bitmap1x.get()); |
112 SkBitmap bitmap_scaled = skia::ImageOperations::Resize( | |
113 *bitmap1x, | |
109 skia::ImageOperations::RESIZE_LANCZOS3, | 114 skia::ImageOperations::RESIZE_LANCZOS3, |
110 bitmap1x.width() * 2, bitmap1x.height() * 2); | 115 static_cast<int>(ceil(bitmap1x->width() * scale)), |
huangs
2012/09/17 17:53:45
Reuse size_in_pixel, for consistent rounding.
| |
116 static_cast<int>(ceil(bitmap1x->height() * scale))); | |
111 | 117 |
112 SkBitmap mask; | 118 SkBitmap mask; |
113 mask.setConfig(SkBitmap::kARGB_8888_Config, | 119 mask.setConfig(SkBitmap::kARGB_8888_Config, |
114 bitmap2x.width(), | 120 bitmap_scaled.width(), |
115 bitmap2x.height()); | 121 bitmap_scaled.height()); |
116 mask.allocPixels(); | 122 mask.allocPixels(); |
117 mask.eraseColor(SK_ColorRED); | 123 mask.eraseColor(SK_ColorRED); |
118 result.reset(new SkBitmap()); | 124 result.reset(new SkBitmap()); |
119 *result.get() = SkBitmapOperations::CreateBlendedBitmap(bitmap2x, mask, | 125 *result.get() = SkBitmapOperations::CreateBlendedBitmap( |
120 0.2); | 126 bitmap_scaled, mask, 0.2); |
121 } else if (!result.get() || | 127 } else if (!result.get() || result->width() == size_in_dip_.width()) { |
122 result->width() == size_in_dip_.width()) { | 128 // 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 | 129 // points to 1x image. Fallback to 1x by returning empty image |
125 // in this case. This 1x image will be scaled when drawn. | 130 // in this case. This 1x image will be scaled when drawn. |
126 return gfx::ImageSkiaRep(); | 131 return gfx::ImageSkiaRep(); |
127 } | 132 } |
128 // If the size of 2x image isn't exactly 2x of 1x version, | 133 // If the size of scaled image isn't exactly |scale| * 1x version, |
129 // create ImageSkia as usual. This will end up with | 134 // create ImageSkia as usual. This will end up with |
130 // corrupted visual representation as the size of image doesn't | 135 // corrupted visual representation as the size of image doesn't |
131 // match the expected size. | 136 // match the expected size. |
132 } | 137 } |
138 DCHECK(result.get()); | |
133 return gfx::ImageSkiaRep(*result.get(), scale_factor); | 139 return gfx::ImageSkiaRep(*result.get(), scale_factor); |
134 } | 140 } |
135 | 141 |
136 private: | 142 private: |
137 const int resource_id_; | 143 const int resource_id_; |
138 const gfx::Size size_in_dip_; | 144 const gfx::Size size_in_dip_; |
139 | 145 |
140 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); | 146 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); |
141 }; | 147 }; |
142 | 148 |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
632 SkBitmap bitmap; | 638 SkBitmap bitmap; |
633 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 639 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
634 bitmap.allocPixels(); | 640 bitmap.allocPixels(); |
635 bitmap.eraseARGB(255, 255, 0, 0); | 641 bitmap.eraseARGB(255, 255, 0, 0); |
636 empty_image_ = gfx::Image(bitmap); | 642 empty_image_ = gfx::Image(bitmap); |
637 } | 643 } |
638 return empty_image_; | 644 return empty_image_; |
639 } | 645 } |
640 | 646 |
641 } // namespace ui | 647 } // namespace ui |
OLD | NEW |