Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(463)

Side by Side Diff: content/browser/android/system_ui_resource_manager_impl.cc

Issue 367173003: [Android] Implementation of overscroll effect for Android L (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Write event location through DidOverscrollParams Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // TODO(jdduke): Fetch the theme color from the platform using
28 // the com.android.internal.R.styleable.EdgeEffect attribute and the
29 // com.android.internal.R.styleable.EdgeEffect_colorPrimary color key.
30 const int kThemeColor = 0xff666666;
31 const float kSin = 0.5f; // sin(PI / 6)
32 const float kCos = 0.866f; // cos(PI / 6);
33
34 SkPaint paint;
35 paint.setAntiAlias(true);
36 paint.setColor((kThemeColor & 0xffffff) | 0x33000000);
aelias_OOO_until_Jul13 2014/08/14 22:54:20 Let's just do paint.setAlpha(0x33). We shouldn't
jdduke (slow) 2014/08/15 19:15:06 Done.
37 paint.setStyle(SkPaint::kFill_Style);
38
39 const float screen_width =
aelias_OOO_until_Jul13 2014/08/14 22:54:20 "screen_width" sometimes being height seems confus
jdduke (slow) 2014/08/15 19:15:06 Done.
40 std::min(screen_size.width(), screen_size.height());
41 const float arc_width = screen_width * 0.5f / kSin;
42 const float y = kCos * arc_width;
43 const float height = arc_width - y;
44 gfx::Size bounds(arc_width, height);
aelias_OOO_until_Jul13 2014/08/14 22:54:20 If I'm reading this correctly, this still ends up
jdduke (slow) 2014/08/15 19:15:06 Where are you getting 500KB? On a 720 phone, we en
45 SkRect arc_rect = SkRect::MakeXYWH(
46 -arc_width / 2.f, -arc_width - y, arc_width * 2.f, arc_width * 2.f);
47 SkBitmap glow_bitmap;
48 if (!glow_bitmap.allocPixels(
49 SkImageInfo::MakeA8(bounds.width(), bounds.height()))) {
50 LOG(FATAL) << " Failed to allocate bitmap of size " << bounds.width() << "x"
51 << bounds.height();
52 }
53
54 SkCanvas canvas(glow_bitmap);
55 canvas.clipRect(SkRect::MakeXYWH(0, 0, bounds.width(), bounds.height()));
56 canvas.drawArc(arc_rect, 45, 90, true, paint);
57 return glow_bitmap;
58 }
59
60 void LoadBitmap(ui::SystemUIResourceManager::ResourceType type,
61 SkBitmap* bitmap_holder,
62 const gfx::Size& screen_size) {
63 TRACE_EVENT1(
64 "browser", "SystemUIResourceManagerImpl::LoadBitmap", "type", type);
65 SkBitmap bitmap;
66 switch (type) {
67 case ui::SystemUIResourceManager::OVERSCROLL_EDGE:
68 bitmap = gfx::CreateSkBitmapFromAndroidResource(
69 "android:drawable/overscroll_edge", gfx::Size(128, 12));
70 break;
71 case ui::SystemUIResourceManager::OVERSCROLL_GLOW:
72 bitmap = gfx::CreateSkBitmapFromAndroidResource(
73 "android:drawable/overscroll_glow", gfx::Size(128, 64));
74 break;
75 case ui::SystemUIResourceManager::OVERSCROLL_GLOW_L:
76 bitmap = CreateOverscrollGlowLBitmap(screen_size);
77 break;
78 }
79 bitmap.setImmutable();
80 *bitmap_holder = bitmap;
81 }
82
83 } // namespace
19 84
20 class SystemUIResourceManagerImpl::Entry 85 class SystemUIResourceManagerImpl::Entry
21 : public content::UIResourceClientAndroid { 86 : public content::UIResourceClientAndroid {
22 public: 87 public:
23 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) { 88 explicit Entry(UIResourceProvider* provider) : id_(0), provider_(provider) {
24 DCHECK(provider); 89 DCHECK(provider);
25 } 90 }
26 91
27 virtual ~Entry() { 92 virtual ~Entry() {
28 if (id_) 93 if (id_)
29 provider_->DeleteUIResource(id_); 94 provider_->DeleteUIResource(id_);
30 id_ = 0; 95 id_ = 0;
31 } 96 }
32 97
33 void SetBitmap(const SkBitmap& bitmap) { 98 void SetBitmap(const SkBitmap& bitmap) {
34 // TODO(jdduke): Verify validity of |bitmap| after resolving resource 99 DCHECK(!bitmap.empty());
35 // loading issues on Android L, crbug.com/389744.
36 DCHECK(bitmap_.empty()); 100 DCHECK(bitmap_.empty());
37 DCHECK(!id_); 101 DCHECK(!id_);
38 bitmap_ = bitmap; 102 bitmap_ = bitmap;
39 } 103 }
40 104
41 cc::UIResourceId GetUIResourceId() { 105 cc::UIResourceId GetUIResourceId() {
42 if (bitmap_.empty()) 106 if (bitmap_.empty())
43 return 0; 107 return 0;
44 if (!id_) 108 if (!id_)
45 id_ = provider_->CreateUIResource(this); 109 id_ = provider_->CreateUIResource(this);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 BuildResource(type); 157 BuildResource(type);
94 } 158 }
95 return resource_map_[type].get(); 159 return resource_map_[type].get();
96 } 160 }
97 161
98 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) { 162 void SystemUIResourceManagerImpl::BuildResource(ResourceType type) {
99 DCHECK(GetEntry(type)->bitmap().empty()); 163 DCHECK(GetEntry(type)->bitmap().empty());
100 164
101 // Instead of blocking the main thread, we post a task to load the bitmap. 165 // Instead of blocking the main thread, we post a task to load the bitmap.
102 SkBitmap* bitmap = new SkBitmap(); 166 SkBitmap* bitmap = new SkBitmap();
167 gfx::Size screen_size =
168 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel();
103 base::Closure load_bitmap = 169 base::Closure load_bitmap =
104 base::Bind(&SystemUIResourceManagerImpl::LoadBitmap, type, bitmap); 170 base::Bind(&LoadBitmap, type, bitmap, screen_size);
105 base::Closure finished_load = 171 base::Closure finished_load =
106 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, 172 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap,
107 weak_factory_.GetWeakPtr(), 173 weak_factory_.GetWeakPtr(),
108 type, 174 type,
109 base::Owned(bitmap)); 175 base::Owned(bitmap));
110 base::WorkerPool::PostTaskAndReply( 176 base::WorkerPool::PostTaskAndReply(
111 FROM_HERE, load_bitmap, finished_load, true); 177 FROM_HERE, load_bitmap, finished_load, true);
112 } 178 }
113 179
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( 180 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap(
132 ResourceType type, 181 ResourceType type,
133 SkBitmap* bitmap_holder) { 182 SkBitmap* bitmap_holder) {
134 DCHECK(bitmap_holder); 183 DCHECK(bitmap_holder);
135 GetEntry(type)->SetBitmap(*bitmap_holder); 184 GetEntry(type)->SetBitmap(*bitmap_holder);
136 } 185 }
137 186
138 } // namespace content 187 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698