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"
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();
24 } // anonymous namespace
26 scoped_ptr
<Thumbnail
> Thumbnail::Create(
28 const base::Time
& time_stamp
,
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
,
39 ui::UIResourceProvider
* ui_resource_provider
,
40 ThumbnailDelegate
* thumbnail_delegate
)
42 time_stamp_(time_stamp
),
44 bitmap_(gfx::Size(1, 1), true),
47 ui_resource_provider_(ui_resource_provider
),
48 thumbnail_delegate_(thumbnail_delegate
) {
51 Thumbnail::~Thumbnail() {
55 void Thumbnail::SetBitmap(const SkBitmap
& bitmap
) {
56 DCHECK(!bitmap
.empty());
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());
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_
);
81 ui_resource_id_
= ui_resource_provider_
->CreateUIResource(this);
84 cc::UIResourceBitmap
Thumbnail::GetBitmap(cc::UIResourceId uid
,
91 cc::UIResourceBitmap
old_bitmap(bitmap_
);
92 // Return a place holder for all other calls to GetBitmap.
93 bitmap_
= cc::UIResourceBitmap(CreateSmallHolderBitmap());
97 void Thumbnail::UIResourceIsInvalid() {
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_
);