shared_device_display_info.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/gfx/android/shared_device_display_info.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/android/jni_android.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/android/jni_string.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/logging.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "jni/DeviceDisplayInfo_jni.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace gfx {
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// static JNI call
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)static void UpdateSharedDeviceDisplayInfo(JNIEnv* env,
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                          jobject obj,
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                          jint display_height,
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                          jint display_width,
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                          jint physical_display_height,
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                          jint physical_display_width,
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                          jint bits_per_pixel,
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                          jint bits_per_component,
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                          jdouble dip_scale,
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                          jint smallest_dip_width) {
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  SharedDeviceDisplayInfo::GetInstance()->InvokeUpdate(env, obj,
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      display_height, display_width,
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      physical_display_height, physical_display_width,
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bits_per_pixel, bits_per_component,
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dip_scale, smallest_dip_width);
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// static
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)SharedDeviceDisplayInfo* SharedDeviceDisplayInfo::GetInstance() {
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return Singleton<SharedDeviceDisplayInfo>::get();
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetDisplayHeight() {
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, display_height_);
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return display_height_;
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetDisplayWidth() {
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, display_width_);
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return display_width_;
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int SharedDeviceDisplayInfo::GetPhysicalDisplayHeight() {
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::AutoLock autolock(lock_);
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return physical_display_height_;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int SharedDeviceDisplayInfo::GetPhysicalDisplayWidth() {
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::AutoLock autolock(lock_);
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return physical_display_width_;
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetBitsPerPixel() {
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, bits_per_pixel_);
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return bits_per_pixel_;
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetBitsPerComponent() {
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, bits_per_component_);
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return bits_per_component_;
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)double SharedDeviceDisplayInfo::GetDIPScale() {
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, dip_scale_);
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return dip_scale_;
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetSmallestDIPWidth() {
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, smallest_dip_width_);
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return smallest_dip_width_;
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// static
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool SharedDeviceDisplayInfo::RegisterSharedDeviceDisplayInfo(JNIEnv* env) {
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return RegisterNativesImpl(env);
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void SharedDeviceDisplayInfo::InvokeUpdate(JNIEnv* env,
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jobject obj,
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint display_height,
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint display_width,
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           jint physical_display_height,
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           jint physical_display_width,
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint bits_per_pixel,
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint bits_per_component,
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jdouble dip_scale,
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint smallest_dip_width) {
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UpdateDisplayInfo(env, obj,
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      display_height, display_width,
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      physical_display_height, physical_display_width,
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bits_per_pixel, bits_per_component, dip_scale,
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      smallest_dip_width);
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)SharedDeviceDisplayInfo::SharedDeviceDisplayInfo()
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    : display_height_(0),
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      display_width_(0),
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      bits_per_pixel_(0),
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      bits_per_component_(0),
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dip_scale_(0),
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      smallest_dip_width_(0) {
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  j_device_info_.Reset(
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_createWithListener(env,
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          base::android::GetApplicationContext()));
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  UpdateDisplayInfo(env, j_device_info_.obj(),
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getDisplayHeight(env, j_device_info_.obj()),
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getDisplayWidth(env, j_device_info_.obj()),
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Java_DeviceDisplayInfo_getPhysicalDisplayHeight(env,
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                      j_device_info_.obj()),
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Java_DeviceDisplayInfo_getPhysicalDisplayWidth(env, j_device_info_.obj()),
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getBitsPerPixel(env, j_device_info_.obj()),
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getBitsPerComponent(env, j_device_info_.obj()),
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getDIPScale(env, j_device_info_.obj()),
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getSmallestDIPWidth(env, j_device_info_.obj()));
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)SharedDeviceDisplayInfo::~SharedDeviceDisplayInfo() {
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void SharedDeviceDisplayInfo::UpdateDisplayInfo(JNIEnv* env,
1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jobject jobj,
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint display_height,
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint display_width,
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                jint physical_display_height,
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                jint physical_display_width,
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint bits_per_pixel,
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint bits_per_component,
1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jdouble dip_scale,
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint smallest_dip_width) {
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  display_height_ = static_cast<int>(display_height);
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  display_width_ = static_cast<int>(display_width);
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  physical_display_height_ = static_cast<int>(physical_display_height);
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  physical_display_width_ = static_cast<int>(physical_display_width);
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bits_per_pixel_ = static_cast<int>(bits_per_pixel);
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bits_per_component_ = static_cast<int>(bits_per_component);
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dip_scale_ = static_cast<double>(dip_scale);
1501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  smallest_dip_width_ = static_cast<int>(smallest_dip_width);
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace gfx
154