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/big_endian.h" | 9 #include "base/big_endian.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 : rb_(rb), resource_id_(resource_id) {} | 98 : rb_(rb), resource_id_(resource_id) {} |
99 virtual ~ResourceBundleImageSource() {} | 99 virtual ~ResourceBundleImageSource() {} |
100 | 100 |
101 // gfx::ImageSkiaSource overrides: | 101 // gfx::ImageSkiaSource overrides: |
102 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { | 102 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { |
103 SkBitmap image; | 103 SkBitmap image; |
104 bool fell_back_to_1x = false; | 104 bool fell_back_to_1x = false; |
105 ScaleFactor scale_factor = GetSupportedScaleFactor(scale); | 105 ScaleFactor scale_factor = GetSupportedScaleFactor(scale); |
106 bool found = rb_->LoadBitmap(resource_id_, &scale_factor, | 106 bool found = rb_->LoadBitmap(resource_id_, &scale_factor, |
107 &image, &fell_back_to_1x); | 107 &image, &fell_back_to_1x); |
| 108 if (!found) |
| 109 return gfx::ImageSkiaRep(); |
| 110 |
108 // If the resource is in the package with SCALE_FACTOR_NONE, it | 111 // If the resource is in the package with SCALE_FACTOR_NONE, it |
109 // can be used in any scale factor. The image is maked as "unscaled" | 112 // can be used in any scale factor. The image is maked as "unscaled" |
110 // so that the ImageSkia do not automatically scale. | 113 // so that the ImageSkia do not automatically scale. |
111 bool unscaled = scale_factor == ui::SCALE_FACTOR_NONE; | 114 if (scale_factor == ui::SCALE_FACTOR_NONE) |
112 if (unscaled) | 115 return gfx::ImageSkiaRep(image, 0.0f); |
113 scale_factor = ui::SCALE_FACTOR_100P; | |
114 | |
115 if (!found) | |
116 return gfx::ImageSkiaRep(); | |
117 | 116 |
118 if (fell_back_to_1x) { | 117 if (fell_back_to_1x) { |
119 // GRIT fell back to the 100% image, so rescale it to the correct size. | 118 // GRIT fell back to the 100% image, so rescale it to the correct size. |
120 image = skia::ImageOperations::Resize( | 119 image = skia::ImageOperations::Resize( |
121 image, | 120 image, |
122 skia::ImageOperations::RESIZE_LANCZOS3, | 121 skia::ImageOperations::RESIZE_LANCZOS3, |
123 gfx::ToCeiledInt(image.width() * scale), | 122 gfx::ToCeiledInt(image.width() * scale), |
124 gfx::ToCeiledInt(image.height() * scale)); | 123 gfx::ToCeiledInt(image.height() * scale)); |
125 } else if (!unscaled) { | |
126 SkBitmap scaled_image = | |
127 PlatformScaleImage(image, | |
128 ui::GetScaleForScaleFactor(scale_factor), | |
129 scale); | |
130 if (scaled_image.isNull()) | |
131 scale = GetScaleForScaleFactor(scale_factor); | |
132 else | |
133 image = scaled_image; | |
134 } else { | 124 } else { |
135 scale = GetScaleForScaleFactor(scale_factor); | 125 scale = GetScaleForScaleFactor(scale_factor); |
136 } | 126 } |
137 return gfx::ImageSkiaRep(image, unscaled ? 0.0f : scale); | 127 return gfx::ImageSkiaRep(image, scale); |
138 } | 128 } |
139 | 129 |
140 private: | 130 private: |
141 ResourceBundle* rb_; | 131 ResourceBundle* rb_; |
142 const int resource_id_; | 132 const int resource_id_; |
143 | 133 |
144 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); | 134 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); |
145 }; | 135 }; |
146 | 136 |
147 // static | 137 // static |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 if (image.IsEmpty()) { | 342 if (image.IsEmpty()) { |
353 DCHECK(!data_packs_.empty()) << | 343 DCHECK(!data_packs_.empty()) << |
354 "Missing call to SetResourcesDataDLL?"; | 344 "Missing call to SetResourcesDataDLL?"; |
355 | 345 |
356 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 346 #if defined(OS_CHROMEOS) || defined(OS_WIN) |
357 ui::ScaleFactor scale_factor_to_load = GetMaxScaleFactor(); | 347 ui::ScaleFactor scale_factor_to_load = GetMaxScaleFactor(); |
358 #else | 348 #else |
359 ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; | 349 ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P; |
360 #endif | 350 #endif |
361 | 351 |
362 float scale = GetImageScale(scale_factor_to_load); | |
363 | |
364 // TODO(oshima): Consider reading the image size from png IHDR chunk and | 352 // TODO(oshima): Consider reading the image size from png IHDR chunk and |
365 // skip decoding here and remove #ifdef below. | 353 // skip decoding here and remove #ifdef below. |
366 // ResourceBundle::GetSharedInstance() is destroyed after the | 354 // ResourceBundle::GetSharedInstance() is destroyed after the |
367 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be | 355 // BrowserMainLoop has finished running. |image_skia| is guaranteed to be |
368 // destroyed before the resource bundle is destroyed. | 356 // destroyed before the resource bundle is destroyed. |
369 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), | 357 gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id), |
370 scale); | 358 GetScaleForScaleFactor(scale_factor_to_load)); |
371 if (image_skia.isNull()) { | 359 if (image_skia.isNull()) { |
372 LOG(WARNING) << "Unable to load image with id " << resource_id; | 360 LOG(WARNING) << "Unable to load image with id " << resource_id; |
373 NOTREACHED(); // Want to assert in debug mode. | 361 NOTREACHED(); // Want to assert in debug mode. |
374 // The load failed to retrieve the image; show a debugging red square. | 362 // The load failed to retrieve the image; show a debugging red square. |
375 return GetEmptyImage(); | 363 return GetEmptyImage(); |
376 } | 364 } |
377 image_skia.SetReadOnly(); | 365 image_skia.SetReadOnly(); |
378 image = gfx::Image(image_skia); | 366 image = gfx::Image(image_skia); |
379 } | 367 } |
380 | 368 |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 | 794 |
807 // static | 795 // static |
808 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 796 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
809 size_t size, | 797 size_t size, |
810 SkBitmap* bitmap, | 798 SkBitmap* bitmap, |
811 bool* fell_back_to_1x) { | 799 bool* fell_back_to_1x) { |
812 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 800 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
813 return gfx::PNGCodec::Decode(buf, size, bitmap); | 801 return gfx::PNGCodec::Decode(buf, size, bitmap); |
814 } | 802 } |
815 | 803 |
816 #if !defined(OS_WIN) | |
817 // static | |
818 SkBitmap ResourceBundle::PlatformScaleImage(const SkBitmap& image, | |
819 float loaded_image_scale, | |
820 float desired_scale) { | |
821 return SkBitmap(); | |
822 } | |
823 #endif | |
824 | |
825 } // namespace ui | 804 } // namespace ui |
OLD | NEW |