| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/android/system_ui_resource_manager_impl.h" | 5 #include "content/browser/android/system_ui_resource_manager_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 13 #include "cc/resources/ui_resource_bitmap.h" | 13 #include "cc/resources/ui_resource_bitmap.h" |
| 14 #include "content/public/browser/android/ui_resource_client_android.h" | 14 #include "content/public/browser/android/ui_resource_client_android.h" |
| 15 #include "content/public/browser/android/ui_resource_provider.h" | 15 #include "content/public/browser/android/ui_resource_provider.h" |
| 16 #include "jni/UIResources_jni.h" |
| 17 #include "skia/ext/image_operations.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "third_party/skia/include/core/SkCanvas.h" | 19 #include "third_party/skia/include/core/SkCanvas.h" |
| 18 #include "third_party/skia/include/core/SkPaint.h" | 20 #include "third_party/skia/include/core/SkPaint.h" |
| 19 #include "ui/gfx/android/java_bitmap.h" | 21 #include "ui/gfx/android/java_bitmap.h" |
| 20 #include "ui/gfx/screen.h" | 22 #include "ui/gfx/screen.h" |
| 21 | 23 |
| 22 namespace content { | 24 namespace content { |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 SkBitmap CreateOverscrollGlowLBitmap(const gfx::Size& screen_size) { | 27 SkBitmap CreateOverscrollGlowLBitmap(const gfx::Size& screen_size) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 41 SkBitmap glow_bitmap; | 43 SkBitmap glow_bitmap; |
| 42 glow_bitmap.allocPixels(SkImageInfo::MakeA8(bounds.width(), bounds.height())); | 44 glow_bitmap.allocPixels(SkImageInfo::MakeA8(bounds.width(), bounds.height())); |
| 43 glow_bitmap.eraseColor(SK_ColorTRANSPARENT); | 45 glow_bitmap.eraseColor(SK_ColorTRANSPARENT); |
| 44 | 46 |
| 45 SkCanvas canvas(glow_bitmap); | 47 SkCanvas canvas(glow_bitmap); |
| 46 canvas.clipRect(SkRect::MakeXYWH(0, 0, bounds.width(), bounds.height())); | 48 canvas.clipRect(SkRect::MakeXYWH(0, 0, bounds.width(), bounds.height())); |
| 47 canvas.drawArc(arc_rect, 45, 90, true, paint); | 49 canvas.drawArc(arc_rect, 45, 90, true, paint); |
| 48 return glow_bitmap; | 50 return glow_bitmap; |
| 49 } | 51 } |
| 50 | 52 |
| 51 void LoadBitmap(ui::SystemUIResourceManager::ResourceType type, | 53 SkBitmap LoadJavaBitmap(ui::SystemUIResourceType type, const gfx::Size& size) { |
| 54 ScopedJavaLocalRef<jobject> jobj = |
| 55 Java_UIResources_getBitmap(base::android::AttachCurrentThread(), |
| 56 base::android::GetApplicationContext(), type, |
| 57 size.width(), size.height()); |
| 58 if (jobj.is_null()) |
| 59 return SkBitmap(); |
| 60 |
| 61 SkBitmap bitmap = CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jobj.obj())); |
| 62 if (bitmap.isNull()) |
| 63 return bitmap; |
| 64 |
| 65 if (size.IsEmpty()) |
| 66 return bitmap; |
| 67 |
| 68 return skia::ImageOperations::Resize( |
| 69 bitmap, skia::ImageOperations::RESIZE_BOX, size.width(), size.height()); |
| 70 } |
| 71 |
| 72 void LoadBitmap(ui::SystemUIResourceType type, |
| 52 SkBitmap* bitmap_holder, | 73 SkBitmap* bitmap_holder, |
| 53 const gfx::Size& screen_size) { | 74 const gfx::Size& screen_size) { |
| 54 TRACE_EVENT1( | 75 TRACE_EVENT1( |
| 55 "browser", "SystemUIResourceManagerImpl::LoadBitmap", "type", type); | 76 "browser", "SystemUIResourceManagerImpl::LoadBitmap", "type", type); |
| 56 SkBitmap bitmap; | 77 SkBitmap bitmap; |
| 57 switch (type) { | 78 switch (type) { |
| 58 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: | 79 case ui::OVERSCROLL_EDGE: |
| 59 bitmap = gfx::CreateSkBitmapFromAndroidResource( | 80 bitmap = LoadJavaBitmap(type, gfx::Size(128, 12)); |
| 60 "android:drawable/overscroll_edge", gfx::Size(128, 12)); | |
| 61 break; | 81 break; |
| 62 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: | 82 case ui::OVERSCROLL_GLOW: |
| 63 bitmap = gfx::CreateSkBitmapFromAndroidResource( | 83 bitmap = LoadJavaBitmap(type, gfx::Size(128, 64)); |
| 64 "android:drawable/overscroll_glow", gfx::Size(128, 64)); | |
| 65 break; | 84 break; |
| 66 case ui::SystemUIResourceManager::OVERSCROLL_GLOW_L: | 85 case ui::OVERSCROLL_GLOW_L: |
| 67 bitmap = CreateOverscrollGlowLBitmap(screen_size); | 86 bitmap = CreateOverscrollGlowLBitmap(screen_size); |
| 68 break; | 87 break; |
| 88 case ui::OVERSCROLL_REFRESH_IDLE: |
| 89 case ui::OVERSCROLL_REFRESH_ACTIVE: |
| 90 bitmap = LoadJavaBitmap(type, gfx::Size()); |
| 91 break; |
| 69 } | 92 } |
| 70 bitmap.setImmutable(); | 93 bitmap.setImmutable(); |
| 71 *bitmap_holder = bitmap; | 94 *bitmap_holder = bitmap; |
| 72 } | 95 } |
| 73 | 96 |
| 74 } // namespace | 97 } // namespace |
| 75 | 98 |
| 76 class SystemUIResourceManagerImpl::Entry | 99 class SystemUIResourceManagerImpl::Entry |
| 77 : public content::UIResourceClientAndroid { | 100 : public content::UIResourceClientAndroid { |
| 78 public: | 101 public: |
| 79 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { | 102 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { |
| 80 DCHECK(provider); | 103 DCHECK(provider); |
| 81 } | 104 } |
| 82 | 105 |
| 83 virtual ~Entry() { | 106 ~Entry() override { |
| 84 if (id_) | 107 if (id_) |
| 85 provider_->DeleteUIResource(id_); | 108 provider_->DeleteUIResource(id_); |
| 86 id_ = 0; | 109 id_ = 0; |
| 87 } | 110 } |
| 88 | 111 |
| 89 void SetBitmap(const SkBitmap& bitmap) { | 112 void SetBitmap(const SkBitmap& bitmap) { |
| 90 DCHECK(!bitmap.empty()); | 113 DCHECK(!bitmap.empty()); |
| 91 DCHECK(bitmap_.empty()); | 114 DCHECK(bitmap_.empty()); |
| 92 DCHECK(!id_); | 115 DCHECK(!id_); |
| 93 bitmap_ = bitmap; | 116 bitmap_ = bitmap; |
| 94 } | 117 } |
| 95 | 118 |
| 96 cc::UIResourceId GetUIResourceId() { | 119 cc::UIResourceId GetUIResourceId() { |
| 97 if (bitmap_.empty()) | 120 if (bitmap_.empty()) |
| 98 return 0; | 121 return 0; |
| 99 if (!id_) | 122 if (!id_) |
| 100 id_ = provider_->CreateUIResource(this); | 123 id_ = provider_->CreateUIResource(this); |
| 101 return id_; | 124 return id_; |
| 102 } | 125 } |
| 103 | 126 |
| 104 // content::UIResourceClient implementation. | 127 // content::UIResourceClient implementation. |
| 105 virtual cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid, | 128 cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid, |
| 106 bool resource_lost) override { | 129 bool resource_lost) override { |
| 107 DCHECK(!bitmap_.empty()); | 130 DCHECK(!bitmap_.empty()); |
| 108 return cc::UIResourceBitmap(bitmap_); | 131 return cc::UIResourceBitmap(bitmap_); |
| 109 } | 132 } |
| 110 | 133 |
| 111 // content::UIResourceClientAndroid implementation. | 134 // content::UIResourceClientAndroid implementation. |
| 112 virtual void UIResourceIsInvalid() override { id_ = 0; } | 135 void UIResourceIsInvalid() override { id_ = 0; } |
| 113 | 136 |
| 114 const SkBitmap& bitmap() const { return bitmap_; } | 137 const SkBitmap& bitmap() const { return bitmap_; } |
| 115 | 138 |
| 116 private: | 139 private: |
| 117 SkBitmap bitmap_; | 140 SkBitmap bitmap_; |
| 118 cc::UIResourceId id_; | 141 cc::UIResourceId id_; |
| 119 UIResourceProvider* provider_; | 142 UIResourceProvider* provider_; |
| 120 | 143 |
| 121 DISALLOW_COPY_AND_ASSIGN(Entry); | 144 DISALLOW_COPY_AND_ASSIGN(Entry); |
| 122 }; | 145 }; |
| 123 | 146 |
| 124 SystemUIResourceManagerImpl::SystemUIResourceManagerImpl( | 147 SystemUIResourceManagerImpl::SystemUIResourceManagerImpl( |
| 125 UIResourceProvider* ui_resource_provider) | 148 UIResourceProvider* ui_resource_provider) |
| 126 : ui_resource_provider_(ui_resource_provider), weak_factory_(this) { | 149 : ui_resource_provider_(ui_resource_provider), weak_factory_(this) { |
| 127 } | 150 } |
| 128 | 151 |
| 129 SystemUIResourceManagerImpl::~SystemUIResourceManagerImpl() { | 152 SystemUIResourceManagerImpl::~SystemUIResourceManagerImpl() { |
| 130 } | 153 } |
| 131 | 154 |
| 132 void SystemUIResourceManagerImpl::PreloadResource(ResourceType type) { | 155 void SystemUIResourceManagerImpl::PreloadResource( |
| 156 ui::SystemUIResourceType type) { |
| 133 GetEntry(type); | 157 GetEntry(type); |
| 134 } | 158 } |
| 135 | 159 |
| 136 cc::UIResourceId SystemUIResourceManagerImpl::GetUIResourceId( | 160 cc::UIResourceId SystemUIResourceManagerImpl::GetUIResourceId( |
| 137 ResourceType type) { | 161 ui::SystemUIResourceType type) { |
| 138 return GetEntry(type)->GetUIResourceId(); | 162 return GetEntry(type)->GetUIResourceId(); |
| 139 } | 163 } |
| 140 | 164 |
| 141 SystemUIResourceManagerImpl::Entry* SystemUIResourceManagerImpl::GetEntry( | 165 SystemUIResourceManagerImpl::Entry* SystemUIResourceManagerImpl::GetEntry( |
| 142 ResourceType type) { | 166 ui::SystemUIResourceType type) { |
| 143 DCHECK_GE(type, RESOURCE_TYPE_FIRST); | 167 DCHECK_GE(type, ui::SYSTEM_UI_RESOURCE_TYPE_FIRST); |
| 144 DCHECK_LE(type, RESOURCE_TYPE_LAST); | 168 DCHECK_LE(type, ui::SYSTEM_UI_RESOURCE_TYPE_LAST); |
| 145 if (!resource_map_[type]) { | 169 if (!resource_map_[type]) { |
| 146 resource_map_[type].reset(new Entry(ui_resource_provider_)); | 170 resource_map_[type].reset(new Entry(ui_resource_provider_)); |
| 147 // Lazily build the resource. | 171 // Lazily build the resource. |
| 148 BuildResource(type); | 172 BuildResource(type); |
| 149 } | 173 } |
| 150 return resource_map_[type].get(); | 174 return resource_map_[type].get(); |
| 151 } | 175 } |
| 152 | 176 |
| 153 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) { | 177 void SystemUIResourceManagerImpl::BuildResource(ui::SystemUIResourceType type) { |
| 154 DCHECK(GetEntry(type)->bitmap().empty()); | 178 DCHECK(GetEntry(type)->bitmap().empty()); |
| 155 | 179 |
| 156 // Instead of blocking the main thread, we post a task to load the bitmap. | 180 // Instead of blocking the main thread, we post a task to load the bitmap. |
| 157 SkBitmap* bitmap = new SkBitmap(); | 181 SkBitmap* bitmap = new SkBitmap(); |
| 158 gfx::Size screen_size = | 182 gfx::Size screen_size = |
| 159 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel(); | 183 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel(); |
| 160 base::Closure load_bitmap = | 184 base::Closure load_bitmap = |
| 161 base::Bind(&LoadBitmap, type, bitmap, screen_size); | 185 base::Bind(&LoadBitmap, type, bitmap, screen_size); |
| 162 base::Closure finished_load = | 186 base::Closure finished_load = |
| 163 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, | 187 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, |
| 164 weak_factory_.GetWeakPtr(), | 188 weak_factory_.GetWeakPtr(), |
| 165 type, | 189 type, |
| 166 base::Owned(bitmap)); | 190 base::Owned(bitmap)); |
| 167 base::WorkerPool::PostTaskAndReply( | 191 base::WorkerPool::PostTaskAndReply( |
| 168 FROM_HERE, load_bitmap, finished_load, true); | 192 FROM_HERE, load_bitmap, finished_load, true); |
| 169 } | 193 } |
| 170 | 194 |
| 171 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( | 195 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( |
| 172 ResourceType type, | 196 ui::SystemUIResourceType type, |
| 173 SkBitmap* bitmap_holder) { | 197 SkBitmap* bitmap_holder) { |
| 174 DCHECK(bitmap_holder); | 198 DCHECK(bitmap_holder); |
| 175 GetEntry(type)->SetBitmap(*bitmap_holder); | 199 GetEntry(type)->SetBitmap(*bitmap_holder); |
| 176 } | 200 } |
| 177 | 201 |
| 202 // static |
| 203 bool SystemUIResourceManagerImpl::RegisterUIResources(JNIEnv* env) { |
| 204 return RegisterNativesImpl(env); |
| 205 } |
| 206 |
| 178 } // namespace content | 207 } // namespace content |
| OLD | NEW |