shared_device_display_info.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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,
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                          jint smallest_dip_width,
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                          jint rotation_degrees) {
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  SharedDeviceDisplayInfo::GetInstance()->InvokeUpdate(env, obj,
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      display_height, display_width,
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      physical_display_height, physical_display_width,
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bits_per_pixel, bits_per_component,
300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      dip_scale, smallest_dip_width, rotation_degrees);
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// static
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)SharedDeviceDisplayInfo* SharedDeviceDisplayInfo::GetInstance() {
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return Singleton<SharedDeviceDisplayInfo>::get();
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetDisplayHeight() {
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, display_height_);
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return display_height_;
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetDisplayWidth() {
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, display_width_);
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return display_width_;
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int SharedDeviceDisplayInfo::GetPhysicalDisplayHeight() {
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::AutoLock autolock(lock_);
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return physical_display_height_;
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int SharedDeviceDisplayInfo::GetPhysicalDisplayWidth() {
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::AutoLock autolock(lock_);
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return physical_display_width_;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetBitsPerPixel() {
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, bits_per_pixel_);
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return bits_per_pixel_;
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetBitsPerComponent() {
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, bits_per_component_);
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return bits_per_component_;
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)double SharedDeviceDisplayInfo::GetDIPScale() {
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, dip_scale_);
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return dip_scale_;
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)int SharedDeviceDisplayInfo::GetSmallestDIPWidth() {
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_NE(0, smallest_dip_width_);
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return smallest_dip_width_;
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochint SharedDeviceDisplayInfo::GetRotationDegrees() {
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::AutoLock autolock(lock_);
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return rotation_degrees_;
870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// static
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool SharedDeviceDisplayInfo::RegisterSharedDeviceDisplayInfo(JNIEnv* env) {
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return RegisterNativesImpl(env);
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void SharedDeviceDisplayInfo::InvokeUpdate(JNIEnv* env,
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jobject obj,
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint display_height,
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint display_width,
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           jint physical_display_height,
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           jint physical_display_width,
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint bits_per_pixel,
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jint bits_per_component,
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                           jdouble dip_scale,
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                           jint smallest_dip_width,
1040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                           jint rotation_degrees) {
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::AutoLock autolock(lock_);
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UpdateDisplayInfo(env, obj,
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      display_height, display_width,
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      physical_display_height, physical_display_width,
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bits_per_pixel, bits_per_component, dip_scale,
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      smallest_dip_width, rotation_degrees);
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)SharedDeviceDisplayInfo::SharedDeviceDisplayInfo()
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    : display_height_(0),
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      display_width_(0),
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      bits_per_pixel_(0),
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      bits_per_component_(0),
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dip_scale_(0),
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      smallest_dip_width_(0) {
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  j_device_info_.Reset(
123e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      Java_DeviceDisplayInfo_create(
124e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          env, base::android::GetApplicationContext()));
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  UpdateDisplayInfo(env, j_device_info_.obj(),
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getDisplayHeight(env, j_device_info_.obj()),
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getDisplayWidth(env, j_device_info_.obj()),
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Java_DeviceDisplayInfo_getPhysicalDisplayHeight(env,
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                      j_device_info_.obj()),
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Java_DeviceDisplayInfo_getPhysicalDisplayWidth(env, j_device_info_.obj()),
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getBitsPerPixel(env, j_device_info_.obj()),
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getBitsPerComponent(env, j_device_info_.obj()),
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Java_DeviceDisplayInfo_getDIPScale(env, j_device_info_.obj()),
1340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Java_DeviceDisplayInfo_getSmallestDIPWidth(env, j_device_info_.obj()),
1350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Java_DeviceDisplayInfo_getRotationDegrees(env, j_device_info_.obj()));
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)SharedDeviceDisplayInfo::~SharedDeviceDisplayInfo() {
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void SharedDeviceDisplayInfo::UpdateDisplayInfo(JNIEnv* env,
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jobject jobj,
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint display_height,
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint display_width,
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                jint physical_display_height,
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                jint physical_display_width,
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint bits_per_pixel,
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jint bits_per_component,
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                jdouble dip_scale,
1500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                jint smallest_dip_width,
1510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                jint rotation_degrees) {
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  display_height_ = static_cast<int>(display_height);
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  display_width_ = static_cast<int>(display_width);
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  physical_display_height_ = static_cast<int>(physical_display_height);
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  physical_display_width_ = static_cast<int>(physical_display_width);
1561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bits_per_pixel_ = static_cast<int>(bits_per_pixel);
1571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bits_per_component_ = static_cast<int>(bits_per_component);
1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dip_scale_ = static_cast<double>(dip_scale);
1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  smallest_dip_width_ = static_cast<int>(smallest_dip_width);
1600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rotation_degrees_ = static_cast<int>(rotation_degrees);
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace gfx
164