OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/android/thumbnail/thumbnail.h" | |
6 #include "content/public/browser/android/ui_resource_provider.h" | |
7 #include "third_party/skia/include/core/SkBitmap.h" | |
8 #include "third_party/skia/include/core/SkCanvas.h" | |
9 #include "ui/gfx/geometry/size_conversions.h" | |
10 | |
11 namespace { | |
12 | |
13 SkBitmap CreateSmallHolderBitmap() { | |
14 SkBitmap small_bitmap; | |
15 SkCanvas canvas(small_bitmap); | |
16 small_bitmap.setConfig( | |
17 SkBitmap::kARGB_8888_Config, 1, 1, 0, kOpaque_SkAlphaType); | |
18 small_bitmap.allocPixels(); | |
19 canvas.drawColor(SK_ColorWHITE); | |
20 small_bitmap.setImmutable(); | |
21 return small_bitmap; | |
22 } | |
23 | |
24 const SkBitmap kSmallHolderBitmap = CreateSmallHolderBitmap(); | |
25 | |
26 } // anonymous namespace | |
27 | |
28 Thumbnail::Thumbnail() | |
29 : tab_id_(0), | |
30 scale_(0), | |
31 bitmap_(kSmallHolderBitmap), | |
32 ui_resource_id_(0), | |
33 retrieved_(false), | |
34 ui_resource_provider_(NULL), | |
35 thumbnail_manager_(NULL) { | |
36 } | |
37 | |
38 Thumbnail::Thumbnail(TabId tab_id, | |
39 const base::Time& time_stamp, | |
40 float scale, | |
41 content::UIResourceProvider* ui_resource_provider, | |
42 ThumbnailManager* thumbnail_manager) | |
43 : tab_id_(tab_id), | |
44 time_stamp_(time_stamp), | |
45 scale_(scale), | |
46 bitmap_(gfx::Size(1, 1), true), | |
47 ui_resource_id_(0), | |
48 retrieved_(false), | |
49 ui_resource_provider_(ui_resource_provider), | |
50 thumbnail_manager_(thumbnail_manager) { | |
51 } | |
52 | |
53 Thumbnail::~Thumbnail() { | |
54 ClearUIResourceId(); | |
55 } | |
56 | |
57 void Thumbnail::SetBitmap(const SkBitmap& bitmap) { | |
58 DCHECK(!bitmap.empty()); | |
59 ClearUIResourceId(); | |
60 scaled_content_size_ = | |
61 gfx::ScaleSize(gfx::Size(bitmap.width(), bitmap.height()), 1.f / scale_); | |
62 scaled_data_size_ = scaled_content_size_; | |
63 bitmap_ = cc::UIResourceBitmap(bitmap); | |
64 } | |
65 | |
66 void Thumbnail::SetCompressedBitmap(skia::RefPtr<SkPixelRef> compressed_bitmap, | |
67 gfx::Size content_size) { | |
68 DCHECK(compressed_bitmap); | |
69 DCHECK(!content_size.IsEmpty()); | |
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); | |
76 } | |
77 | |
78 void Thumbnail::CreateUIResource() { | |
79 DCHECK(ui_resource_provider_); | |
80 if (!ui_resource_id_) | |
81 ui_resource_id_ = ui_resource_provider_->CreateUIResource(this); | |
82 } | |
83 | |
84 cc::UIResourceBitmap Thumbnail::GetBitmap(cc::UIResourceId uid, | |
85 bool resource_lost) { | |
86 if (retrieved_) | |
87 return bitmap_; | |
88 | |
89 retrieved_ = true; | |
90 | |
91 cc::UIResourceBitmap old_bitmap(bitmap_); | |
92 // Return a place holder for all other calls to GetBitmap. | |
93 bitmap_ = cc::UIResourceBitmap(kSmallHolderBitmap); | |
David Trainor- moved to gerrit
2014/07/14 20:30:45
Not sure if this is a problem (haven't seen other
powei
2014/07/14 23:56:25
The write task is a ref-holder of the same pixels,
David Trainor- moved to gerrit
2014/07/15 20:08:23
Yeah that's fine with me :). It looks like you ar
powei
2014/07/18 00:51:49
Acknowledged.
| |
94 return old_bitmap; | |
95 } | |
96 | |
97 void Thumbnail::UIResourceIsInvalid() { | |
98 ui_resource_id_ = 0; | |
99 if (thumbnail_manager_) | |
100 thumbnail_manager_->InvalidateCachedThumbnail(*this); | |
101 } | |
102 | |
103 Thumbnail& Thumbnail::operator=(const Thumbnail& rhs) { | |
104 DCHECK(!rhs.ui_resource_id_); | |
105 ClearUIResourceId(); | |
106 | |
107 tab_id_ = rhs.tab_id_; | |
108 time_stamp_ = rhs.time_stamp_; | |
109 scale_ = rhs.scale_; | |
110 scaled_content_size_ = rhs.scaled_content_size_; | |
111 scaled_data_size_ = rhs.scaled_data_size_; | |
112 bitmap_ = rhs.bitmap_; | |
113 ui_resource_id_ = rhs.ui_resource_id_; | |
114 retrieved_ = rhs.retrieved_; | |
115 ui_resource_provider_ = rhs.ui_resource_provider_; | |
116 thumbnail_manager_ = rhs.thumbnail_manager_; | |
117 | |
118 return (*this); | |
119 } | |
120 | |
121 void Thumbnail::ClearUIResourceId() { | |
122 if (ui_resource_id_ && ui_resource_provider_) | |
123 ui_resource_provider_->DeleteUIResource(ui_resource_id_); | |
124 ui_resource_id_ = 0; | |
125 } | |
OLD | NEW |