| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ui/app_list/arc/arc_app_icon.h" | 5 #include "chrome/browser/ui/app_list/arc/arc_app_icon.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <utility> |
| 11 #include <vector> |
| 8 | 12 |
| 9 #include "base/bind.h" | 13 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/lazy_instance.h" |
| 12 #include "base/macros.h" | 17 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 14 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 15 #include "chrome/browser/image_decoder.h" | 20 #include "chrome/browser/image_decoder.h" |
| 16 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" | 21 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| 17 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" | 22 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" |
| 18 #include "chrome/browser/ui/ash/launcher/arc_app_shelf_id.h" | 23 #include "chrome/browser/ui/ash/launcher/arc_app_shelf_id.h" |
| 19 #include "chrome/grit/component_extension_resources.h" | 24 #include "chrome/grit/component_extension_resources.h" |
| 20 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 21 #include "extensions/grit/extensions_browser_resources.h" | 26 #include "extensions/grit/extensions_browser_resources.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 private: | 98 private: |
| 94 // gfx::ImageSkiaSource overrides: | 99 // gfx::ImageSkiaSource overrides: |
| 95 gfx::ImageSkiaRep GetImageForScale(float scale) override; | 100 gfx::ImageSkiaRep GetImageForScale(float scale) override; |
| 96 | 101 |
| 97 // Used to load images asynchronously. NULLed out when the ArcAppIcon is | 102 // Used to load images asynchronously. NULLed out when the ArcAppIcon is |
| 98 // destroyed. | 103 // destroyed. |
| 99 base::WeakPtr<ArcAppIcon> host_; | 104 base::WeakPtr<ArcAppIcon> host_; |
| 100 | 105 |
| 101 const int resource_size_in_dip_; | 106 const int resource_size_in_dip_; |
| 102 | 107 |
| 108 // A map from a pair of a resource ID and size in DIP to an image. This |
| 109 // is a cache to avoid resizing IDR icons in GetImageForScale every time. |
| 110 static base::LazyInstance<std::map<std::pair<int, int>, gfx::ImageSkia>> |
| 111 default_icons_cache_; |
| 112 |
| 103 DISALLOW_COPY_AND_ASSIGN(Source); | 113 DISALLOW_COPY_AND_ASSIGN(Source); |
| 104 }; | 114 }; |
| 105 | 115 |
| 116 base::LazyInstance<std::map<std::pair<int, int>, gfx::ImageSkia>> |
| 117 ArcAppIcon::Source::default_icons_cache_ = LAZY_INSTANCE_INITIALIZER; |
| 118 |
| 106 ArcAppIcon::Source::Source(const base::WeakPtr<ArcAppIcon>& host, | 119 ArcAppIcon::Source::Source(const base::WeakPtr<ArcAppIcon>& host, |
| 107 int resource_size_in_dip) | 120 int resource_size_in_dip) |
| 108 : host_(host), | 121 : host_(host), |
| 109 resource_size_in_dip_(resource_size_in_dip) { | 122 resource_size_in_dip_(resource_size_in_dip) { |
| 110 } | 123 } |
| 111 | 124 |
| 112 ArcAppIcon::Source::~Source() { | 125 ArcAppIcon::Source::~Source() { |
| 113 } | 126 } |
| 114 | 127 |
| 115 gfx::ImageSkiaRep ArcAppIcon::Source::GetImageForScale(float scale) { | 128 gfx::ImageSkiaRep ArcAppIcon::Source::GetImageForScale(float scale) { |
| 129 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 116 if (host_) | 130 if (host_) |
| 117 host_->LoadForScaleFactor(ui::GetSupportedScaleFactor(scale)); | 131 host_->LoadForScaleFactor(ui::GetSupportedScaleFactor(scale)); |
| 118 | 132 |
| 119 // Host loads icon asynchronously, so use default icon so far. | 133 // Host loads icon asynchronously, so use default icon so far. |
| 120 int resource_id; | 134 int resource_id; |
| 121 if (host_ && host_->app_id() == arc::kPlayStoreAppId) { | 135 if (host_ && host_->app_id() == arc::kPlayStoreAppId) { |
| 122 resource_id = scale >= 1.5f ? | 136 resource_id = scale >= 1.5f ? |
| 123 IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48; | 137 IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48; |
| 124 } else { | 138 } else { |
| 125 resource_id = IDR_APP_DEFAULT_ICON; | 139 resource_id = IDR_APP_DEFAULT_ICON; |
| 126 } | 140 } |
| 141 |
| 142 // Check |default_icons_cache_| and returns the existing one if possible. |
| 143 const auto key = std::make_pair(resource_id, resource_size_in_dip_); |
| 144 const auto it = default_icons_cache_.Get().find(key); |
| 145 if (it != default_icons_cache_.Get().end()) |
| 146 return it->second.GetRepresentation(scale); |
| 147 |
| 127 const gfx::ImageSkia* default_image = ResourceBundle::GetSharedInstance(). | 148 const gfx::ImageSkia* default_image = ResourceBundle::GetSharedInstance(). |
| 128 GetImageSkiaNamed(resource_id); | 149 GetImageSkiaNamed(resource_id); |
| 129 CHECK(default_image); | 150 CHECK(default_image); |
| 130 return gfx::ImageSkiaOperations::CreateResizedImage( | 151 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( |
| 131 *default_image, | 152 *default_image, skia::ImageOperations::RESIZE_BEST, |
| 132 skia::ImageOperations::RESIZE_BEST, | 153 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)); |
| 133 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)). | 154 |
| 134 GetRepresentation(scale); | 155 // Add the resized image to the cache to avoid executing the expensive resize |
| 156 // operation many times. Caching the result is safe because unlike ARC icons |
| 157 // that can be updated dynamically, IDR icons are static. |
| 158 default_icons_cache_.Get().insert(std::make_pair(key, resized_image)); |
| 159 return resized_image.GetRepresentation(scale); |
| 135 } | 160 } |
| 136 | 161 |
| 137 class ArcAppIcon::DecodeRequest : public ImageDecoder::ImageRequest { | 162 class ArcAppIcon::DecodeRequest : public ImageDecoder::ImageRequest { |
| 138 public: | 163 public: |
| 139 DecodeRequest(const base::WeakPtr<ArcAppIcon>& host, | 164 DecodeRequest(const base::WeakPtr<ArcAppIcon>& host, |
| 140 int dimension, | 165 int dimension, |
| 141 ui::ScaleFactor scale_factor); | 166 ui::ScaleFactor scale_factor); |
| 142 ~DecodeRequest() override; | 167 ~DecodeRequest() override; |
| 143 | 168 |
| 144 // ImageDecoder::ImageRequest | 169 // ImageDecoder::ImageRequest |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 363 |
| 339 void ArcAppIcon::DiscardDecodeRequest(DecodeRequest* request) { | 364 void ArcAppIcon::DiscardDecodeRequest(DecodeRequest* request) { |
| 340 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 365 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 341 | 366 |
| 342 ScopedVector<DecodeRequest>::iterator it = std::find(decode_requests_.begin(), | 367 ScopedVector<DecodeRequest>::iterator it = std::find(decode_requests_.begin(), |
| 343 decode_requests_.end(), | 368 decode_requests_.end(), |
| 344 request); | 369 request); |
| 345 CHECK(it != decode_requests_.end()); | 370 CHECK(it != decode_requests_.end()); |
| 346 decode_requests_.erase(it); | 371 decode_requests_.erase(it); |
| 347 } | 372 } |
| OLD | NEW |