1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/apps/app_service/app_icon/app_icon_decoder.h"
#include <functional>
#include "base/functional/bind.h"
#include "base/task/thread_pool.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_factory.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_util.h"
#include "chrome/browser/image_decoder/image_decoder.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/data_decoder/public/cpp/decode_image.h"
#include "services/data_decoder/public/mojom/image_decoder.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/resource/resource_scale_factor.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_rep.h"
namespace apps {
AppIconDecoder::ImageSource::ImageSource(int32_t size_in_dip)
: size_in_dip_(size_in_dip) {}
AppIconDecoder::ImageSource::~ImageSource() = default;
gfx::ImageSkiaRep AppIconDecoder::ImageSource::GetImageForScale(float scale) {
TRACE_EVENT0("ui", "AppIconDecoder::ImageSource::GetImageForScale");
// Host loads icon asynchronously, so use default icon so far.
// Get the ImageSkia for the resource IDR_APP_DEFAULT_ICON and the size
// `size_in_dip_`.
return CreateResizedResourceImage(IDR_APP_DEFAULT_ICON, size_in_dip_)
.GetRepresentation(scale);
}
AppIconDecoder::AppIconDecoder(
const base::FilePath& base_path,
const std::string& app_id,
int32_t size_in_dip,
base::OnceCallback<void(AppIconDecoder* decoder, IconValuePtr iv)> callback)
: base_path_(base_path),
app_id_(app_id),
size_in_dip_(size_in_dip),
callback_(std::move(callback)) {}
AppIconDecoder::~AppIconDecoder() = default;
void AppIconDecoder::Start() {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(&ReadIconFilesOnBackgroundThread, base_path_, app_id_,
size_in_dip_),
base::BindOnce(&AppIconDecoder::OnIconRead,
weak_ptr_factory_.GetWeakPtr()));
}
bool AppIconDecoder::SetScaleFactors(
const std::map<ui::ResourceScaleFactor, IconValuePtr>& icon_datas) {
TRACE_EVENT0("ui", "AppIconDecoder::SetScaleFactors");
for (const auto& [scale_factor, iv] : icon_datas) {
if (!iv || iv->icon_type != IconType::kCompressed) {
return false;
}
if (HasAdaptiveIconData(iv)) {
is_adaptive_icon_ = true;
foreground_incomplete_scale_factors_.insert(scale_factor);
background_incomplete_scale_factors_.insert(scale_factor);
} else if (iv->compressed.empty()) {
return false;
} else {
incomplete_scale_factors_.insert(scale_factor);
}
}
if (is_adaptive_icon_ && !incomplete_scale_factors_.empty()) {
// Some scales have non-adaptive icons. Then we can't generate the adaptive
// icon for all scales. Set `is_adaptive_icon_` as false, and decode the
// foreground images only for scales with adaptive icon data.
is_adaptive_icon_ = false;
for (const auto& [scale_factor, iv] : icon_datas) {
incomplete_scale_factors_.insert(scale_factor);
}
}
// Initialize the ImageSkia with placeholder bitmaps, and the correct icon
// size to generate the adaptive icon using CompositeImagesAndApplyMask, which
// checks the ImageSkia's size to chop for paddings and resize the image_reps.
gfx::Size image_size(size_in_dip_, size_in_dip_);
if (is_adaptive_icon_) {
foreground_image_skia_ =
gfx::ImageSkia(std::make_unique<ImageSource>(size_in_dip_), image_size);
background_image_skia_ =
gfx::ImageSkia(std::make_unique<ImageSource>(size_in_dip_), image_size);
} else {
image_skia_ =
gfx::ImageSkia(std::make_unique<ImageSource>(size_in_dip_), image_size);
}
return true;
}
void AppIconDecoder::OnIconRead(
std::map<ui::ResourceScaleFactor, IconValuePtr> icon_datas) {
TRACE_EVENT0("ui", "AppIconDecoder::OnIconRead");
// Check `icon_datas` to set scale factors.
if (!SetScaleFactors(icon_datas)) {
DiscardDecodeRequest();
return;
}
// Create DecodeRequest to decode images safely in a sandboxed service per
// security requests.
for (auto& [scale_factor, iv] : icon_datas) {
if (HasAdaptiveIconData(iv)) {
if (!is_adaptive_icon_) {
// If we can't generate the adaptive icon for all scales, decode the
// foreground images only to fill in `image_skia_`.
DecodeImage(scale_factor, std::move(iv->foreground_icon_png_data),
image_skia_, incomplete_scale_factors_);
continue;
}
// Decode for the foreground and background image.
DecodeImage(scale_factor, std::move(iv->foreground_icon_png_data),
foreground_image_skia_, foreground_incomplete_scale_factors_);
DecodeImage(scale_factor, std::move(iv->background_icon_png_data),
background_image_skia_, background_incomplete_scale_factors_);
continue;
}
is_maskable_icon_ = iv->is_maskable_icon;
DecodeImage(scale_factor, std::move(iv->compressed), image_skia_,
incomplete_scale_factors_);
}
}
void AppIconDecoder::DecodeImage(
ui::ResourceScaleFactor scale_factor,
std::vector<uint8_t> icon_data,
gfx::ImageSkia& image_skia,
std::set<ui::ResourceScaleFactor>& incomplete_scale_factors) {
data_decoder::DecodeImage(
&GetIconDataDecoder(), std::move(icon_data),
data_decoder::mojom::ImageCodec::kDefault,
/*shrink_to_fit=*/false, data_decoder::kDefaultMaxSizeInBytes,
/*desired_image_frame_size=*/gfx::Size(),
base::BindOnce(&AppIconDecoder::UpdateImageSkia,
weak_ptr_factory_.GetWeakPtr(), scale_factor,
std::ref(image_skia), std::ref(incomplete_scale_factors)));
}
void AppIconDecoder::UpdateImageSkia(
ui::ResourceScaleFactor scale_factor,
gfx::ImageSkia& image_skia,
std::set<ui::ResourceScaleFactor>& incomplete_scale_factors,
const SkBitmap& bitmap) {
TRACE_EVENT0("ui", "AppIconDecoder::UpdateImageSkia");
// If decoding any scale factor fails, discard the entire decode request.
if (bitmap.drawsNothing()) {
DiscardDecodeRequest();
return;
}
CHECK(ui::IsScaleFactorSupported(scale_factor));
gfx::ImageSkiaRep image_rep(bitmap,
ui::GetScaleForResourceScaleFactor(scale_factor));
image_skia.RemoveRepresentation(image_rep.scale());
image_skia.AddRepresentation(image_rep);
image_skia.RemoveUnsupportedRepresentationsForScale(image_rep.scale());
incomplete_scale_factors.erase(scale_factor);
// For the adaptive icon, generate the adaptive icon with the foreground and
// background icon images.
if (is_adaptive_icon_) {
if (foreground_incomplete_scale_factors_.empty() &&
background_incomplete_scale_factors_.empty()) {
auto image = apps::CompositeImagesAndApplyMask(foreground_image_skia_,
background_image_skia_);
image.MakeThreadSafe();
CompleteWithImageSkia(image);
}
return;
}
if (incomplete_scale_factors_.empty()) {
CompleteWithImageSkia(image_skia_);
}
}
void AppIconDecoder::DiscardDecodeRequest() {
// `callback_` is responsible for removing this AppIconDecoder object, which
// will cause all pending decode results to be dropped.
//
// Return an empty icon value, because the callers assume the icon value
// should never be nullptr.
std::move(callback_).Run(this, std::make_unique<apps::IconValue>());
}
void AppIconDecoder::CompleteWithImageSkia(const gfx::ImageSkia& image_skia) {
TRACE_EVENT0("ui", "AppIconDecoder::CompleteWithImageSkia");
// `callback_` is responsible for removing this AppIconDecoder object.
auto iv = std::make_unique<apps::IconValue>();
iv->icon_type = IconType::kUncompressed;
iv->uncompressed = image_skia;
iv->is_maskable_icon = is_maskable_icon_;
std::move(callback_).Run(this, std::move(iv));
}
} // namespace apps
|