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(), |
| 57 type, |
| 58 size.width(), |
| 59 size.height()); |
| 60 if (jobj.is_null()) |
| 61 return SkBitmap(); |
| 62 |
| 63 SkBitmap bitmap = CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jobj.obj())); |
| 64 if (bitmap.isNull()) |
| 65 return bitmap; |
| 66 |
| 67 if (size.IsEmpty()) |
| 68 return bitmap; |
| 69 |
| 70 return skia::ImageOperations::Resize( |
| 71 bitmap, skia::ImageOperations::RESIZE_BOX, size.width(), size.height()); |
| 72 } |
| 73 |
| 74 void LoadBitmap(ui::SystemUIResourceType type, |
52 SkBitmap* bitmap_holder, | 75 SkBitmap* bitmap_holder, |
53 const gfx::Size& screen_size) { | 76 const gfx::Size& screen_size) { |
54 TRACE_EVENT1( | 77 TRACE_EVENT1( |
55 "browser", "SystemUIResourceManagerImpl::LoadBitmap", "type", type); | 78 "browser", "SystemUIResourceManagerImpl::LoadBitmap", "type", type); |
56 SkBitmap bitmap; | 79 SkBitmap bitmap; |
57 switch (type) { | 80 switch (type) { |
58 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: | 81 case ui::OVERSCROLL_EDGE: |
59 bitmap = gfx::CreateSkBitmapFromAndroidResource( | 82 bitmap = LoadJavaBitmap(type, gfx::Size(128, 12)); |
60 "android:drawable/overscroll_edge", gfx::Size(128, 12)); | |
61 break; | 83 break; |
62 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: | 84 case ui::OVERSCROLL_GLOW: |
63 bitmap = gfx::CreateSkBitmapFromAndroidResource( | 85 bitmap = LoadJavaBitmap(type, gfx::Size(128, 64)); |
64 "android:drawable/overscroll_glow", gfx::Size(128, 64)); | |
65 break; | 86 break; |
66 case ui::SystemUIResourceManager::OVERSCROLL_GLOW_L: | 87 case ui::OVERSCROLL_GLOW_L: |
67 bitmap = CreateOverscrollGlowLBitmap(screen_size); | 88 bitmap = CreateOverscrollGlowLBitmap(screen_size); |
68 break; | 89 break; |
| 90 case ui::OVERSCROLL_REFRESH_IDLE: |
| 91 case ui::OVERSCROLL_REFRESH_ACTIVE: |
| 92 bitmap = LoadJavaBitmap(type, gfx::Size()); |
| 93 break; |
69 } | 94 } |
70 bitmap.setImmutable(); | 95 bitmap.setImmutable(); |
71 *bitmap_holder = bitmap; | 96 *bitmap_holder = bitmap; |
72 } | 97 } |
73 | 98 |
74 } // namespace | 99 } // namespace |
75 | 100 |
76 class SystemUIResourceManagerImpl::Entry | 101 class SystemUIResourceManagerImpl::Entry |
77 : public content::UIResourceClientAndroid { | 102 : public content::UIResourceClientAndroid { |
78 public: | 103 public: |
79 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { | 104 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { |
80 DCHECK(provider); | 105 DCHECK(provider); |
81 } | 106 } |
82 | 107 |
83 virtual ~Entry() { | 108 ~Entry() override { |
84 if (id_) | 109 if (id_) |
85 provider_->DeleteUIResource(id_); | 110 provider_->DeleteUIResource(id_); |
86 id_ = 0; | 111 id_ = 0; |
87 } | 112 } |
88 | 113 |
89 void SetBitmap(const SkBitmap& bitmap) { | 114 void SetBitmap(const SkBitmap& bitmap) { |
90 DCHECK(!bitmap.empty()); | 115 DCHECK(!bitmap.empty()); |
91 DCHECK(bitmap_.empty()); | 116 DCHECK(bitmap_.empty()); |
92 DCHECK(!id_); | 117 DCHECK(!id_); |
93 bitmap_ = bitmap; | 118 bitmap_ = bitmap; |
94 } | 119 } |
95 | 120 |
96 cc::UIResourceId GetUIResourceId() { | 121 cc::UIResourceId GetUIResourceId() { |
97 if (bitmap_.empty()) | 122 if (bitmap_.empty()) |
98 return 0; | 123 return 0; |
99 if (!id_) | 124 if (!id_) |
100 id_ = provider_->CreateUIResource(this); | 125 id_ = provider_->CreateUIResource(this); |
101 return id_; | 126 return id_; |
102 } | 127 } |
103 | 128 |
104 // content::UIResourceClient implementation. | 129 // content::UIResourceClient implementation. |
105 virtual cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid, | 130 cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid, |
106 bool resource_lost) override { | 131 bool resource_lost) override { |
107 DCHECK(!bitmap_.empty()); | 132 DCHECK(!bitmap_.empty()); |
108 return cc::UIResourceBitmap(bitmap_); | 133 return cc::UIResourceBitmap(bitmap_); |
109 } | 134 } |
110 | 135 |
111 // content::UIResourceClientAndroid implementation. | 136 // content::UIResourceClientAndroid implementation. |
112 virtual void UIResourceIsInvalid() override { id_ = 0; } | 137 void UIResourceIsInvalid() override { id_ = 0; } |
113 | 138 |
114 const SkBitmap& bitmap() const { return bitmap_; } | 139 const SkBitmap& bitmap() const { return bitmap_; } |
115 | 140 |
116 private: | 141 private: |
117 SkBitmap bitmap_; | 142 SkBitmap bitmap_; |
118 cc::UIResourceId id_; | 143 cc::UIResourceId id_; |
119 UIResourceProvider* provider_; | 144 UIResourceProvider* provider_; |
120 | 145 |
121 DISALLOW_COPY_AND_ASSIGN(Entry); | 146 DISALLOW_COPY_AND_ASSIGN(Entry); |
122 }; | 147 }; |
123 | 148 |
124 SystemUIResourceManagerImpl::SystemUIResourceManagerImpl( | 149 SystemUIResourceManagerImpl::SystemUIResourceManagerImpl( |
125 UIResourceProvider* ui_resource_provider) | 150 UIResourceProvider* ui_resource_provider) |
126 : ui_resource_provider_(ui_resource_provider), weak_factory_(this) { | 151 : ui_resource_provider_(ui_resource_provider), weak_factory_(this) { |
127 } | 152 } |
128 | 153 |
129 SystemUIResourceManagerImpl::~SystemUIResourceManagerImpl() { | 154 SystemUIResourceManagerImpl::~SystemUIResourceManagerImpl() { |
130 } | 155 } |
131 | 156 |
132 void SystemUIResourceManagerImpl::PreloadResource(ResourceType type) { | 157 void SystemUIResourceManagerImpl::PreloadResource( |
| 158 ui::SystemUIResourceType type) { |
133 GetEntry(type); | 159 GetEntry(type); |
134 } | 160 } |
135 | 161 |
136 cc::UIResourceId SystemUIResourceManagerImpl::GetUIResourceId( | 162 cc::UIResourceId SystemUIResourceManagerImpl::GetUIResourceId( |
137 ResourceType type) { | 163 ui::SystemUIResourceType type) { |
138 return GetEntry(type)->GetUIResourceId(); | 164 return GetEntry(type)->GetUIResourceId(); |
139 } | 165 } |
140 | 166 |
141 SystemUIResourceManagerImpl::Entry* SystemUIResourceManagerImpl::GetEntry( | 167 SystemUIResourceManagerImpl::Entry* SystemUIResourceManagerImpl::GetEntry( |
142 ResourceType type) { | 168 ui::SystemUIResourceType type) { |
143 DCHECK_GE(type, RESOURCE_TYPE_FIRST); | 169 DCHECK_GE(type, ui::SYSTEM_UI_RESOURCE_TYPE_FIRST); |
144 DCHECK_LE(type, RESOURCE_TYPE_LAST); | 170 DCHECK_LE(type, ui::SYSTEM_UI_RESOURCE_TYPE_LAST); |
145 if (!resource_map_[type]) { | 171 if (!resource_map_[type]) { |
146 resource_map_[type].reset(new Entry(ui_resource_provider_)); | 172 resource_map_[type].reset(new Entry(ui_resource_provider_)); |
147 // Lazily build the resource. | 173 // Lazily build the resource. |
148 BuildResource(type); | 174 BuildResource(type); |
149 } | 175 } |
150 return resource_map_[type].get(); | 176 return resource_map_[type].get(); |
151 } | 177 } |
152 | 178 |
153 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) { | 179 void SystemUIResourceManagerImpl::BuildResource(ui::SystemUIResourceType type) { |
154 DCHECK(GetEntry(type)->bitmap().empty()); | 180 DCHECK(GetEntry(type)->bitmap().empty()); |
155 | 181 |
156 // Instead of blocking the main thread, we post a task to load the bitmap. | 182 // Instead of blocking the main thread, we post a task to load the bitmap. |
157 SkBitmap* bitmap = new SkBitmap(); | 183 SkBitmap* bitmap = new SkBitmap(); |
158 gfx::Size screen_size = | 184 gfx::Size screen_size = |
159 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel(); | 185 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel(); |
160 base::Closure load_bitmap = | 186 base::Closure load_bitmap = |
161 base::Bind(&LoadBitmap, type, bitmap, screen_size); | 187 base::Bind(&LoadBitmap, type, bitmap, screen_size); |
162 base::Closure finished_load = | 188 base::Closure finished_load = |
163 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, | 189 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, |
164 weak_factory_.GetWeakPtr(), | 190 weak_factory_.GetWeakPtr(), |
165 type, | 191 type, |
166 base::Owned(bitmap)); | 192 base::Owned(bitmap)); |
167 base::WorkerPool::PostTaskAndReply( | 193 base::WorkerPool::PostTaskAndReply( |
168 FROM_HERE, load_bitmap, finished_load, true); | 194 FROM_HERE, load_bitmap, finished_load, true); |
169 } | 195 } |
170 | 196 |
171 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( | 197 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( |
172 ResourceType type, | 198 ui::SystemUIResourceType type, |
173 SkBitmap* bitmap_holder) { | 199 SkBitmap* bitmap_holder) { |
174 DCHECK(bitmap_holder); | 200 DCHECK(bitmap_holder); |
175 GetEntry(type)->SetBitmap(*bitmap_holder); | 201 GetEntry(type)->SetBitmap(*bitmap_holder); |
176 } | 202 } |
177 | 203 |
| 204 // static |
| 205 bool SystemUIResourceManagerImpl::RegisterUIResources(JNIEnv* env) { |
| 206 return RegisterNativesImpl(env); |
| 207 } |
| 208 |
178 } // namespace content | 209 } // namespace content |
OLD | NEW |