Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / android / thumbnail / thumbnail.cc
blobf4c9186e7a5c14f68e5ecf76a038ec63d2bd5e0a
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "chrome/browser/android/thumbnail/thumbnail.h"
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "ui/android/resources/ui_resource_provider.h"
10 #include "ui/gfx/geometry/size_conversions.h"
12 namespace {
14 SkBitmap CreateSmallHolderBitmap() {
15 SkBitmap small_bitmap;
16 SkCanvas canvas(small_bitmap);
17 small_bitmap.allocPixels(
18 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
19 canvas.drawColor(SK_ColorWHITE);
20 small_bitmap.setImmutable();
21 return small_bitmap;
24 } // anonymous namespace
26 scoped_ptr<Thumbnail> Thumbnail::Create(
27 TabId tab_id,
28 const base::Time& time_stamp,
29 float scale,
30 ui::UIResourceProvider* ui_resource_provider,
31 ThumbnailDelegate* thumbnail_delegate) {
32 return make_scoped_ptr(new Thumbnail(
33 tab_id, time_stamp, scale, ui_resource_provider, thumbnail_delegate));
36 Thumbnail::Thumbnail(TabId tab_id,
37 const base::Time& time_stamp,
38 float scale,
39 ui::UIResourceProvider* ui_resource_provider,
40 ThumbnailDelegate* thumbnail_delegate)
41 : tab_id_(tab_id),
42 time_stamp_(time_stamp),
43 scale_(scale),
44 bitmap_(gfx::Size(1, 1), true),
45 ui_resource_id_(0),
46 retrieved_(false),
47 ui_resource_provider_(ui_resource_provider),
48 thumbnail_delegate_(thumbnail_delegate) {
51 Thumbnail::~Thumbnail() {
52 ClearUIResourceId();
55 void Thumbnail::SetBitmap(const SkBitmap& bitmap) {
56 DCHECK(!bitmap.empty());
57 retrieved_ = false;
58 ClearUIResourceId();
59 scaled_content_size_ =
60 gfx::ScaleSize(gfx::Size(bitmap.width(), bitmap.height()), 1.f / scale_);
61 scaled_data_size_ = scaled_content_size_;
62 bitmap_ = cc::UIResourceBitmap(bitmap);
65 void Thumbnail::SetCompressedBitmap(skia::RefPtr<SkPixelRef> compressed_bitmap,
66 const gfx::Size& content_size) {
67 DCHECK(compressed_bitmap);
68 DCHECK(!content_size.IsEmpty());
69 retrieved_ = false;
70 ClearUIResourceId();
71 gfx::Size data_size(compressed_bitmap->info().width(),
72 compressed_bitmap->info().height());
73 scaled_content_size_ = gfx::ScaleSize(content_size, 1.f / scale_);
74 scaled_data_size_ = gfx::ScaleSize(data_size, 1.f / scale_);
75 bitmap_ = cc::UIResourceBitmap(compressed_bitmap, data_size);
78 void Thumbnail::CreateUIResource() {
79 DCHECK(ui_resource_provider_);
80 if (!ui_resource_id_)
81 ui_resource_id_ = ui_resource_provider_->CreateUIResource(this);
84 cc::UIResourceBitmap Thumbnail::GetBitmap(cc::UIResourceId uid,
85 bool resource_lost) {
86 if (retrieved_)
87 return bitmap_;
89 retrieved_ = true;
91 cc::UIResourceBitmap old_bitmap(bitmap_);
92 // Return a place holder for all other calls to GetBitmap.
93 bitmap_ = cc::UIResourceBitmap(CreateSmallHolderBitmap());
94 return old_bitmap;
97 void Thumbnail::UIResourceIsInvalid() {
98 ui_resource_id_ = 0;
99 if (thumbnail_delegate_)
100 thumbnail_delegate_->InvalidateCachedThumbnail(this);
103 void Thumbnail::ClearUIResourceId() {
104 if (ui_resource_id_ && ui_resource_provider_)
105 ui_resource_provider_->DeleteUIResource(ui_resource_id_);
106 ui_resource_id_ = 0;