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 "content/browser/android/system_ui_resource_manager_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/location.h" | |
10 #include "base/observer_list.h" | |
11 #include "base/threading/worker_pool.h" | |
12 #include "cc/resources/ui_resource_bitmap.h" | |
13 #include "content/public/browser/android/ui_resource_client_android.h" | |
14 #include "content/public/browser/android/ui_resource_provider.h" | |
15 #include "third_party/skia/include/core/SkBitmap.h" | |
16 #include "ui/gfx/android/java_bitmap.h" | |
17 | |
18 namespace content { | |
19 | |
20 class SystemUIResourceManagerImpl::Entry | |
21 : public content::UIResourceClientAndroid { | |
22 public: | |
23 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { | |
24 DCHECK(provider); | |
25 } | |
26 | |
27 virtual ~Entry() { | |
28 if (id_) | |
29 provider_->DeleteUIResource(id_); | |
30 id_ = 0; | |
31 } | |
32 | |
33 void SetBitmap(const SkBitmap& bitmap) { | |
34 DCHECK(bitmap_.empty()); | |
35 DCHECK(!bitmap.empty()); | |
36 DCHECK(!id_); | |
37 bitmap_ = bitmap; | |
38 } | |
39 | |
40 cc::UIResourceId GetUIResourceId() { | |
41 if (bitmap_.empty()) | |
42 return 0; | |
43 if (!id_) | |
44 id_ = provider_->CreateUIResource(this); | |
45 return id_; | |
46 } | |
47 | |
48 // content::UIResourceClient implementation. | |
49 virtual cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid, | |
50 bool resource_lost) OVERRIDE { | |
51 DCHECK(!bitmap_.empty()); | |
52 return cc::UIResourceBitmap(bitmap_); | |
53 } | |
54 | |
55 // content::UIResourceClientAndroid implementation. | |
56 virtual void UIResourceIsInvalid() OVERRIDE { id_ = 0; } | |
57 | |
58 const SkBitmap& bitmap() const { return bitmap_; } | |
59 | |
60 private: | |
61 SkBitmap bitmap_; | |
62 cc::UIResourceId id_; | |
63 UIResourceProvider* provider_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(Entry); | |
66 }; | |
67 | |
68 SystemUIResourceManagerImpl::SystemUIResourceManagerImpl( | |
69 UIResourceProvider* ui_resource_provider) | |
70 : ui_resource_provider_(ui_resource_provider), weak_factory_(this) { | |
71 } | |
72 | |
73 SystemUIResourceManagerImpl::~SystemUIResourceManagerImpl() { | |
74 } | |
75 | |
76 void SystemUIResourceManagerImpl::PreloadResource(ResourceType type) { | |
77 GetEntry(type); | |
78 } | |
79 | |
80 cc::UIResourceId SystemUIResourceManagerImpl::GetUIResourceId( | |
81 ResourceType type) { | |
82 return GetEntry(type)->GetUIResourceId(); | |
83 } | |
84 | |
85 SystemUIResourceManagerImpl::Entry* SystemUIResourceManagerImpl::GetEntry( | |
86 ResourceType type) { | |
87 DCHECK_GE(type, RESOURCE_TYPE_FIRST); | |
88 DCHECK_LE(type, RESOURCE_TYPE_LAST); | |
89 if (!resource_map_[type]) { | |
90 resource_map_[type].reset(new Entry(ui_resource_provider_)); | |
91 // Lazily build the resource. | |
92 BuildResource(type); | |
93 } | |
94 return resource_map_[type].get(); | |
95 } | |
96 | |
97 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) { | |
98 DCHECK(GetEntry(type)->bitmap().empty()); | |
99 | |
100 // Instead of blocking the main thread, we post a task to load the bitmap. | |
101 SkBitmap* bitmap = new SkBitmap(); | |
102 base::Closure load_bitmap = | |
103 base::Bind(&SystemUIResourceManagerImpl::LoadBitmap, type, bitmap); | |
104 base::Closure finished_load = | |
105 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, | |
106 weak_factory_.GetWeakPtr(), | |
107 type, | |
108 base::Owned(bitmap)); | |
109 base::WorkerPool::PostTaskAndReply( | |
110 FROM_HERE, load_bitmap, finished_load, true); | |
111 } | |
112 | |
113 void SystemUIResourceManagerImpl::LoadBitmap(ResourceType type, | |
114 SkBitmap* bitmap_holder) { | |
115 SkBitmap bitmap; | |
116 switch (type) { | |
117 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: | |
118 bitmap = gfx::CreateSkBitmapFromAndroidResource( | |
119 "android:drawable/overscroll_edge", gfx::Size(128, 12)); | |
120 break; | |
121 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: | |
122 bitmap = gfx::CreateSkBitmapFromAndroidResource( | |
123 "android:drawable/overscroll_glow", gfx::Size(128, 64)); | |
124 break; | |
125 } | |
126 bitmap.setImmutable(); | |
127 *bitmap_holder = bitmap; | |
128 } | |
129 | |
130 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( | |
131 ResourceType type, | |
132 SkBitmap* bitmap_holder) { | |
133 DCHECK(bitmap_holder); | |
134 GetEntry(type)->SetBitmap(*bitmap_holder); | |
135 } | |
136 | |
137 } // namespace content | |
OLD | NEW |