| 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/location.h" | 10 #include "base/location.h" |
| 10 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 11 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 12 #include "cc/resources/ui_resource_bitmap.h" | 13 #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_client_android.h" |
| 14 #include "content/public/browser/android/ui_resource_provider.h" | 15 #include "content/public/browser/android/ui_resource_provider.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "third_party/skia/include/core/SkCanvas.h" |
| 18 #include "third_party/skia/include/core/SkPaint.h" |
| 19 #include "third_party/skia/include/effects/SkPorterDuff.h" |
| 16 #include "ui/gfx/android/java_bitmap.h" | 20 #include "ui/gfx/android/java_bitmap.h" |
| 21 #include "ui/gfx/screen.h" |
| 17 | 22 |
| 18 namespace content { | 23 namespace content { |
| 24 namespace { |
| 25 |
| 26 SkBitmap CreateOverscrollGlowLBitmap(const gfx::Size& screen_size) { |
| 27 const float kSin = 0.5f; // sin(PI / 6) |
| 28 const float kCos = 0.866f; // cos(PI / 6); |
| 29 |
| 30 SkPaint paint; |
| 31 paint.setAntiAlias(true); |
| 32 paint.setAlpha(0x33); |
| 33 paint.setStyle(SkPaint::kFill_Style); |
| 34 |
| 35 const float arc_width = |
| 36 std::min(screen_size.width(), screen_size.height()) * 0.5f / kSin; |
| 37 const float y = kCos * arc_width; |
| 38 const float height = arc_width - y; |
| 39 gfx::Size bounds(arc_width, height); |
| 40 SkRect arc_rect = SkRect::MakeXYWH( |
| 41 -arc_width / 2.f, -arc_width - y, arc_width * 2.f, arc_width * 2.f); |
| 42 SkBitmap glow_bitmap; |
| 43 if (!glow_bitmap.allocPixels( |
| 44 SkImageInfo::MakeA8(bounds.width(), bounds.height()))) { |
| 45 LOG(FATAL) << " Failed to allocate bitmap of size " << bounds.width() << "x" |
| 46 << bounds.height(); |
| 47 } |
| 48 |
| 49 SkCanvas canvas(glow_bitmap); |
| 50 canvas.clipRect(SkRect::MakeXYWH(0, 0, bounds.width(), bounds.height())); |
| 51 canvas.drawArc(arc_rect, 45, 90, true, paint); |
| 52 return glow_bitmap; |
| 53 } |
| 54 |
| 55 void LoadBitmap(ui::SystemUIResourceManager::ResourceType type, |
| 56 SkBitmap* bitmap_holder, |
| 57 const gfx::Size& screen_size) { |
| 58 TRACE_EVENT1( |
| 59 "browser", "SystemUIResourceManagerImpl::LoadBitmap", "type", type); |
| 60 SkBitmap bitmap; |
| 61 switch (type) { |
| 62 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: |
| 63 bitmap = gfx::CreateSkBitmapFromAndroidResource( |
| 64 "android:drawable/overscroll_edge", gfx::Size(128, 12)); |
| 65 break; |
| 66 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: |
| 67 bitmap = gfx::CreateSkBitmapFromAndroidResource( |
| 68 "android:drawable/overscroll_glow", gfx::Size(128, 64)); |
| 69 break; |
| 70 case ui::SystemUIResourceManager::OVERSCROLL_GLOW_L: |
| 71 bitmap = CreateOverscrollGlowLBitmap(screen_size); |
| 72 break; |
| 73 } |
| 74 bitmap.setImmutable(); |
| 75 *bitmap_holder = bitmap; |
| 76 } |
| 77 |
| 78 } // namespace |
| 19 | 79 |
| 20 class SystemUIResourceManagerImpl::Entry | 80 class SystemUIResourceManagerImpl::Entry |
| 21 : public content::UIResourceClientAndroid { | 81 : public content::UIResourceClientAndroid { |
| 22 public: | 82 public: |
| 23 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { | 83 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { |
| 24 DCHECK(provider); | 84 DCHECK(provider); |
| 25 } | 85 } |
| 26 | 86 |
| 27 virtual ~Entry() { | 87 virtual ~Entry() { |
| 28 if (id_) | 88 if (id_) |
| 29 provider_->DeleteUIResource(id_); | 89 provider_->DeleteUIResource(id_); |
| 30 id_ = 0; | 90 id_ = 0; |
| 31 } | 91 } |
| 32 | 92 |
| 33 void SetBitmap(const SkBitmap& bitmap) { | 93 void SetBitmap(const SkBitmap& bitmap) { |
| 34 // TODO(jdduke): Verify validity of |bitmap| after resolving resource | 94 DCHECK(!bitmap.empty()); |
| 35 // loading issues on Android L, crbug.com/389744. | |
| 36 DCHECK(bitmap_.empty()); | 95 DCHECK(bitmap_.empty()); |
| 37 DCHECK(!id_); | 96 DCHECK(!id_); |
| 38 bitmap_ = bitmap; | 97 bitmap_ = bitmap; |
| 39 } | 98 } |
| 40 | 99 |
| 41 cc::UIResourceId GetUIResourceId() { | 100 cc::UIResourceId GetUIResourceId() { |
| 42 if (bitmap_.empty()) | 101 if (bitmap_.empty()) |
| 43 return 0; | 102 return 0; |
| 44 if (!id_) | 103 if (!id_) |
| 45 id_ = provider_->CreateUIResource(this); | 104 id_ = provider_->CreateUIResource(this); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 BuildResource(type); | 152 BuildResource(type); |
| 94 } | 153 } |
| 95 return resource_map_[type].get(); | 154 return resource_map_[type].get(); |
| 96 } | 155 } |
| 97 | 156 |
| 98 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) { | 157 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) { |
| 99 DCHECK(GetEntry(type)->bitmap().empty()); | 158 DCHECK(GetEntry(type)->bitmap().empty()); |
| 100 | 159 |
| 101 // Instead of blocking the main thread, we post a task to load the bitmap. | 160 // Instead of blocking the main thread, we post a task to load the bitmap. |
| 102 SkBitmap* bitmap = new SkBitmap(); | 161 SkBitmap* bitmap = new SkBitmap(); |
| 162 gfx::Size screen_size = |
| 163 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel(); |
| 103 base::Closure load_bitmap = | 164 base::Closure load_bitmap = |
| 104 base::Bind(&SystemUIResourceManagerImpl::LoadBitmap, type, bitmap); | 165 base::Bind(&LoadBitmap, type, bitmap, screen_size); |
| 105 base::Closure finished_load = | 166 base::Closure finished_load = |
| 106 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, | 167 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, |
| 107 weak_factory_.GetWeakPtr(), | 168 weak_factory_.GetWeakPtr(), |
| 108 type, | 169 type, |
| 109 base::Owned(bitmap)); | 170 base::Owned(bitmap)); |
| 110 base::WorkerPool::PostTaskAndReply( | 171 base::WorkerPool::PostTaskAndReply( |
| 111 FROM_HERE, load_bitmap, finished_load, true); | 172 FROM_HERE, load_bitmap, finished_load, true); |
| 112 } | 173 } |
| 113 | 174 |
| 114 void SystemUIResourceManagerImpl::LoadBitmap(ResourceType type, | |
| 115 SkBitmap* bitmap_holder) { | |
| 116 SkBitmap bitmap; | |
| 117 switch (type) { | |
| 118 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: | |
| 119 bitmap = gfx::CreateSkBitmapFromAndroidResource( | |
| 120 "android:drawable/overscroll_edge", gfx::Size(128, 12)); | |
| 121 break; | |
| 122 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: | |
| 123 bitmap = gfx::CreateSkBitmapFromAndroidResource( | |
| 124 "android:drawable/overscroll_glow", gfx::Size(128, 64)); | |
| 125 break; | |
| 126 } | |
| 127 bitmap.setImmutable(); | |
| 128 *bitmap_holder = bitmap; | |
| 129 } | |
| 130 | |
| 131 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( | 175 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( |
| 132 ResourceType type, | 176 ResourceType type, |
| 133 SkBitmap* bitmap_holder) { | 177 SkBitmap* bitmap_holder) { |
| 134 DCHECK(bitmap_holder); | 178 DCHECK(bitmap_holder); |
| 135 GetEntry(type)->SetBitmap(*bitmap_holder); | 179 GetEntry(type)->SetBitmap(*bitmap_holder); |
| 136 } | 180 } |
| 137 | 181 |
| 138 } // namespace content | 182 } // namespace content |
| OLD | NEW |