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

Side by Side Diff: ui/gfx/android/device_display_info.cc

Issue 26129009: Cache DeviceDisplayInfo data in shared structure on native side to avoid frequent JNI calls. (Closed) Base URL: https://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 2 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/gfx/android/device_display_info.h" 5 #include "ui/gfx/android/device_display_info.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "jni/DeviceDisplayInfo_jni.h" 10 #include "jni/DeviceDisplayInfo_jni.h"
11 11
12 using base::android::AttachCurrentThread; 12 using base::android::AttachCurrentThread;
Yaron 2013/10/10 11:52:46 Inline this
13 using base::android::ScopedJavaLocalRef; 13 using base::android::ScopedJavaLocalRef;
14 14
15 namespace gfx { 15 namespace gfx {
16 16
17
Yaron 2013/10/10 11:52:46 nit: remove
17 DeviceDisplayInfo::DeviceDisplayInfo() { 18 DeviceDisplayInfo::DeviceDisplayInfo() {
18 JNIEnv* env = AttachCurrentThread();
19 j_device_info_.Reset(Java_DeviceDisplayInfo_create(env,
20 base::android::GetApplicationContext()));
21 } 19 }
22 20
23 DeviceDisplayInfo::~DeviceDisplayInfo() { 21 DeviceDisplayInfo::~DeviceDisplayInfo() {
24 } 22 }
25 23
26 int DeviceDisplayInfo::GetDisplayHeight() { 24 int DeviceDisplayInfo::GetDisplayHeight() {
27 JNIEnv* env = AttachCurrentThread(); 25 return SharedDisplayInfo::GetDisplayHeight();
28 jint result =
29 Java_DeviceDisplayInfo_getDisplayHeight(env, j_device_info_.obj());
30 return static_cast<int>(result);
31 } 26 }
32 27
33 int DeviceDisplayInfo::GetDisplayWidth() { 28 int DeviceDisplayInfo::GetDisplayWidth() {
34 JNIEnv* env = AttachCurrentThread(); 29 return SharedDisplayInfo::GetDisplayWidth();
35 jint result =
36 Java_DeviceDisplayInfo_getDisplayWidth(env, j_device_info_.obj());
37 return static_cast<int>(result);
38 } 30 }
39 31
40 int DeviceDisplayInfo::GetBitsPerPixel() { 32 int DeviceDisplayInfo::GetBitsPerPixel() {
41 JNIEnv* env = AttachCurrentThread(); 33 return SharedDisplayInfo::GetBitsPerPixel();
42 jint result =
43 Java_DeviceDisplayInfo_getBitsPerPixel(env, j_device_info_.obj());
44 return static_cast<int>(result);
45 } 34 }
46 35
47 int DeviceDisplayInfo::GetBitsPerComponent() { 36 int DeviceDisplayInfo::GetBitsPerComponent() {
48 JNIEnv* env = AttachCurrentThread(); 37 return SharedDisplayInfo::GetBitsPerComponent();
49 jint result =
50 Java_DeviceDisplayInfo_getBitsPerComponent(env, j_device_info_.obj());
51 return static_cast<int>(result);
52 } 38 }
53 39
54 double DeviceDisplayInfo::GetDIPScale() { 40 double DeviceDisplayInfo::GetDIPScale() {
55 JNIEnv* env = AttachCurrentThread(); 41 return SharedDisplayInfo::GetDIPScale();
56 jdouble result =
57 Java_DeviceDisplayInfo_getDIPScale(env, j_device_info_.obj());
58 return static_cast<double>(result);
59 } 42 }
60 43
61 // static 44 static pthread_rwlock_t rwlock_ = PTHREAD_RWLOCK_INITIALIZER;
62 bool DeviceDisplayInfo::RegisterDeviceDisplayInfo(JNIEnv* env) { 45
46 struct ScopedReadLock {
Yaron 2013/10/10 11:52:46 Use the locking primitives from base if you still
ostap 2013/10/10 16:29:50 I'm sorry, I didn't find read/write lock implement
47 ScopedReadLock() {
48 pthread_rwlock_rdlock(&rwlock_);
49 }
50
51 ~ScopedReadLock() {
52 pthread_rwlock_unlock(&rwlock_);
53 }
54 };
55
56 struct ScopedWriteLock {
57 ScopedWriteLock() {
58 pthread_rwlock_wrlock(&rwlock_);
59 }
60
61 ~ScopedWriteLock() {
62 pthread_rwlock_unlock(&rwlock_);
63 }
64 };
65
66 scoped_ptr<SharedDisplayInfo> SharedDisplayInfo::instance_;
Yaron 2013/10/10 11:52:46 Use base::LazyInstance
67
68 int SharedDisplayInfo::GetDisplayHeight() {
69 SharedDisplayInfo* instance = Instance();
70 ScopedReadLock read_lock;
71 return instance->display_height_;
72 }
73
74 int SharedDisplayInfo::GetDisplayWidth() {
75 SharedDisplayInfo* instance = Instance();
76 ScopedReadLock read_lock;
77 return instance->display_width_;
78 }
79
80 int SharedDisplayInfo::GetBitsPerPixel() {
81 SharedDisplayInfo* instance = Instance();
82 ScopedReadLock read_lock;
83 return instance->bits_per_pixel_;
84 }
85
86 int SharedDisplayInfo::GetBitsPerComponent() {
87 SharedDisplayInfo* instance = Instance();
88 ScopedReadLock read_lock;
89 return instance->bits_per_component_;
90 }
91
92 double SharedDisplayInfo::GetDIPScale() {
93 SharedDisplayInfo* instance = Instance();
94 ScopedReadLock read_lock;
95 return instance->dip_scale_;
96 }
97
98 void UpdateSharedDisplayInfo(
99 JNIEnv* env, jobject obj, jint display_height,
Yaron 2013/10/10 11:52:46 In C++, these should be one per line per style gui
100 jint display_width, jint bits_per_pixel,
101 jint bits_per_component, jdouble dip_scale) {
102 SharedDisplayInfo::UpdateDisplayInfo(env, obj, display_height,
103 display_width, bits_per_pixel, bits_per_component, dip_scale);
104 }
105
106 void SharedDisplayInfo::UpdateDisplayInfo(JNIEnv*, jobject, jint display_height,
Yaron 2013/10/10 11:52:46 Same as above
107 jint display_width, jint bits_per_pixel, jint bits_per_component,
108 jdouble dip_scale) {
109 SharedDisplayInfo* instance = Instance();
110
111 ScopedWriteLock write_lock;
112
113 instance->display_height_ = static_cast<int>(display_height);
114 instance->display_width_ = static_cast<int>(display_width);
115 instance->bits_per_pixel_ = static_cast<int>(bits_per_pixel);
116 instance->bits_per_component_ = static_cast<int>(bits_per_component);
117 instance->dip_scale_ = static_cast<int>(dip_scale);
Yaron 2013/10/10 11:52:46 dip_scale_ is a double
118 }
119
120 SharedDisplayInfo* SharedDisplayInfo::Instance() {
121 if (instance_) return instance_.get();
122
123 {
124 // Have to scope this block to avoid lock conflict
125 // with UpdateDisplayInfo method.
126 ScopedWriteLock write_lock;
127 if (!instance_) {
128 instance_.reset(new SharedDisplayInfo());
129 }
130 }
131
132 instance_->RegisterListener();
133
134 return instance_.get();
135 }
136
137 SharedDisplayInfo::SharedDisplayInfo() {
138 JNIEnv* env = AttachCurrentThread();
139 j_device_info_.Reset(Java_DeviceDisplayInfo_create(env,
Yaron 2013/10/10 11:52:46 It's not clear to me why you need this anymore. L
ostap 2013/10/10 15:34:40 On 2013/10/10 11:52:46, Yaron wrote: The problem
140 base::android::GetApplicationContext()));
141 }
142
143 void SharedDisplayInfo::RegisterListener() {
144 JNIEnv* env = AttachCurrentThread();
145
146 Java_DeviceDisplayInfo_registerListener(env, j_device_info_.obj());
147 }
148
149 bool SharedDisplayInfo::RegisterSharedDeviceDisplayInfo(JNIEnv* env) {
63 return RegisterNativesImpl(env); 150 return RegisterNativesImpl(env);
64 } 151 }
65 152
66 } // namespace gfx 153 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698