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::EnsureResource(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 SystemUIResourceMap::iterator iter = resource_map_.find(type); |
| 88 if (iter != resource_map_.end()) |
| 89 return iter->second; |
| 90 |
| 91 scoped_ptr<Entry> resource_entry = |
| 92 make_scoped_ptr(new Entry(ui_resource_provider_)); |
| 93 resource_map_.set(type, resource_entry.Pass()); |
| 94 |
| 95 // Lazily build the resource. |
| 96 BuildResource(type); |
| 97 return resource_map_.get(type); |
| 98 } |
| 99 |
| 100 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) { |
| 101 DCHECK(GetEntry(type)->bitmap().empty()); |
| 102 |
| 103 // Instead of blocking the main thread, we post a task to load the bitmap. |
| 104 SkBitmap* bitmap = new SkBitmap(); |
| 105 base::Closure load_bitmap = |
| 106 base::Bind(&SystemUIResourceManagerImpl::LoadBitmap, type, bitmap); |
| 107 base::Closure finished_load = |
| 108 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, |
| 109 weak_factory_.GetWeakPtr(), |
| 110 type, |
| 111 base::Owned(bitmap)); |
| 112 base::WorkerPool::PostTaskAndReply( |
| 113 FROM_HERE, load_bitmap, finished_load, true); |
| 114 } |
| 115 |
| 116 void SystemUIResourceManagerImpl::LoadBitmap(ResourceType type, |
| 117 SkBitmap* bitmap_holder) { |
| 118 SkBitmap bitmap; |
| 119 switch (type) { |
| 120 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: |
| 121 bitmap = gfx::CreateSkBitmapFromAndroidResource( |
| 122 "android:drawable/overscroll_edge", gfx::Size(128, 12)); |
| 123 break; |
| 124 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: |
| 125 bitmap = gfx::CreateSkBitmapFromAndroidResource( |
| 126 "android:drawable/overscroll_glow", gfx::Size(128, 64)); |
| 127 break; |
| 128 } |
| 129 bitmap.setImmutable(); |
| 130 *bitmap_holder = bitmap; |
| 131 } |
| 132 |
| 133 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( |
| 134 ResourceType type, |
| 135 SkBitmap* bitmap_holder) { |
| 136 DCHECK(bitmap_holder); |
| 137 GetEntry(type)->SetBitmap(*bitmap_holder); |
| 138 } |
| 139 |
| 140 } // namespace content |
OLD | NEW |