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
|
// Copyright 2019 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/thumbnail/cc/thumbnail.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/task/single_thread_task_runner.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/android/resources/ui_resource_provider.h"
#include "ui/gfx/geometry/size_conversions.h"
namespace thumbnail {
namespace {
SkBitmap CreateSmallHolderBitmap() {
SkBitmap small_bitmap;
small_bitmap.allocPixels(
SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
SkCanvas canvas(small_bitmap);
canvas.drawColor(SK_ColorWHITE);
small_bitmap.setImmutable();
return small_bitmap;
}
} // anonymous namespace
std::unique_ptr<Thumbnail> Thumbnail::Create(
TabId tab_id,
const base::Time& time_stamp,
float scale,
base::WeakPtr<ui::UIResourceProvider> ui_resource_provider,
ThumbnailDelegate* thumbnail_delegate) {
return base::WrapUnique(new Thumbnail(
tab_id, time_stamp, scale, ui_resource_provider, thumbnail_delegate));
}
Thumbnail::Thumbnail(TabId tab_id,
const base::Time& time_stamp,
float scale,
base::WeakPtr<ui::UIResourceProvider> ui_resource_provider,
ThumbnailDelegate* thumbnail_delegate)
: tab_id_(tab_id),
time_stamp_(time_stamp),
scale_(scale),
bitmap_(gfx::Size(1, 1), true),
ui_resource_id_(0),
retrieved_(false),
ui_resource_provider_(ui_resource_provider),
thumbnail_delegate_(thumbnail_delegate) {}
Thumbnail::~Thumbnail() {
ClearUIResourceId();
}
void Thumbnail::SetBitmap(const SkBitmap& bitmap) {
DCHECK(!bitmap.empty());
retrieved_ = false;
ClearUIResourceId();
scaled_content_size_ =
gfx::ScaleSize(gfx::SizeF(bitmap.width(), bitmap.height()), 1.f / scale_);
scaled_data_size_ = scaled_content_size_;
size_in_bytes_ = bitmap.height() * bitmap.rowBytes();
bitmap_ = cc::UIResourceBitmap(bitmap);
}
void Thumbnail::SetCompressedBitmap(sk_sp<SkPixelRef> compressed_bitmap,
const gfx::Size& content_size) {
DCHECK(compressed_bitmap);
DCHECK(!content_size.IsEmpty());
retrieved_ = false;
ClearUIResourceId();
gfx::Size data_size(compressed_bitmap->width(), compressed_bitmap->height());
scaled_content_size_ = gfx::ScaleSize(gfx::SizeF(content_size), 1.f / scale_);
scaled_data_size_ = gfx::ScaleSize(gfx::SizeF(data_size), 1.f / scale_);
size_in_bytes_ = compressed_bitmap->height() * compressed_bitmap->rowBytes();
bitmap_ = cc::UIResourceBitmap(std::move(compressed_bitmap), data_size);
}
void Thumbnail::CreateUIResource() {
DCHECK(ui_resource_provider_);
if (!ui_resource_id_) {
ui_resource_id_ = ui_resource_provider_->CreateUIResource(this);
}
}
cc::UIResourceBitmap Thumbnail::GetBitmap(cc::UIResourceId uid,
bool resource_lost) {
if (retrieved_) {
// InvalidateCachedThumbnail() causes |this| to be deleted, so
// don't delete the resource while LayerTeeHost calls into |this|
// to avoid reentry there.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&Thumbnail::DoInvalidate, weak_factory_.GetWeakPtr()));
return bitmap_;
}
retrieved_ = true;
cc::UIResourceBitmap old_bitmap(bitmap_);
// Return a place holder for all other calls to GetBitmap.
bitmap_ = cc::UIResourceBitmap(CreateSmallHolderBitmap());
return old_bitmap;
}
void Thumbnail::DoInvalidate() {
if (thumbnail_delegate_) {
thumbnail_delegate_->InvalidateCachedThumbnail(this);
}
}
void Thumbnail::ClearUIResourceId() {
if (ui_resource_id_ && ui_resource_provider_) {
ui_resource_provider_->DeleteUIResource(ui_resource_id_);
}
ui_resource_id_ = 0;
}
} // namespace thumbnail
|