shared_device_display_info.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gfx/android/shared_device_display_info.h"
6
7#include "base/android/jni_android.h"
8#include "base/android/jni_string.h"
9#include "base/logging.h"
10#include "jni/DeviceDisplayInfo_jni.h"
11
12namespace gfx {
13
14// static JNI call
15static void UpdateSharedDeviceDisplayInfo(JNIEnv* env,
16                                          jobject obj,
17                                          jint display_height,
18                                          jint display_width,
19                                          jint physical_display_height,
20                                          jint physical_display_width,
21                                          jint bits_per_pixel,
22                                          jint bits_per_component,
23                                          jdouble dip_scale,
24                                          jint smallest_dip_width) {
25  SharedDeviceDisplayInfo::GetInstance()->InvokeUpdate(env, obj,
26      display_height, display_width,
27      physical_display_height, physical_display_width,
28      bits_per_pixel, bits_per_component,
29      dip_scale, smallest_dip_width);
30}
31
32// static
33SharedDeviceDisplayInfo* SharedDeviceDisplayInfo::GetInstance() {
34  return Singleton<SharedDeviceDisplayInfo>::get();
35}
36
37int SharedDeviceDisplayInfo::GetDisplayHeight() {
38  base::AutoLock autolock(lock_);
39  DCHECK_NE(0, display_height_);
40  return display_height_;
41}
42
43int SharedDeviceDisplayInfo::GetDisplayWidth() {
44  base::AutoLock autolock(lock_);
45  DCHECK_NE(0, display_width_);
46  return display_width_;
47}
48
49int SharedDeviceDisplayInfo::GetPhysicalDisplayHeight() {
50  base::AutoLock autolock(lock_);
51  return physical_display_height_;
52}
53
54int SharedDeviceDisplayInfo::GetPhysicalDisplayWidth() {
55  base::AutoLock autolock(lock_);
56  return physical_display_width_;
57}
58
59int SharedDeviceDisplayInfo::GetBitsPerPixel() {
60  base::AutoLock autolock(lock_);
61  DCHECK_NE(0, bits_per_pixel_);
62  return bits_per_pixel_;
63}
64
65int SharedDeviceDisplayInfo::GetBitsPerComponent() {
66  base::AutoLock autolock(lock_);
67  DCHECK_NE(0, bits_per_component_);
68  return bits_per_component_;
69}
70
71double SharedDeviceDisplayInfo::GetDIPScale() {
72  base::AutoLock autolock(lock_);
73  DCHECK_NE(0, dip_scale_);
74  return dip_scale_;
75}
76
77int SharedDeviceDisplayInfo::GetSmallestDIPWidth() {
78  base::AutoLock autolock(lock_);
79  DCHECK_NE(0, smallest_dip_width_);
80  return smallest_dip_width_;
81}
82
83// static
84bool SharedDeviceDisplayInfo::RegisterSharedDeviceDisplayInfo(JNIEnv* env) {
85  return RegisterNativesImpl(env);
86}
87
88void SharedDeviceDisplayInfo::InvokeUpdate(JNIEnv* env,
89                                           jobject obj,
90                                           jint display_height,
91                                           jint display_width,
92                                           jint physical_display_height,
93                                           jint physical_display_width,
94                                           jint bits_per_pixel,
95                                           jint bits_per_component,
96                                           jdouble dip_scale,
97                                           jint smallest_dip_width) {
98  base::AutoLock autolock(lock_);
99
100  UpdateDisplayInfo(env, obj,
101      display_height, display_width,
102      physical_display_height, physical_display_width,
103      bits_per_pixel, bits_per_component, dip_scale,
104      smallest_dip_width);
105}
106
107SharedDeviceDisplayInfo::SharedDeviceDisplayInfo()
108    : display_height_(0),
109      display_width_(0),
110      bits_per_pixel_(0),
111      bits_per_component_(0),
112      dip_scale_(0),
113      smallest_dip_width_(0) {
114  JNIEnv* env = base::android::AttachCurrentThread();
115  j_device_info_.Reset(
116      Java_DeviceDisplayInfo_createWithListener(env,
117          base::android::GetApplicationContext()));
118  UpdateDisplayInfo(env, j_device_info_.obj(),
119      Java_DeviceDisplayInfo_getDisplayHeight(env, j_device_info_.obj()),
120      Java_DeviceDisplayInfo_getDisplayWidth(env, j_device_info_.obj()),
121      Java_DeviceDisplayInfo_getPhysicalDisplayHeight(env,
122                                                      j_device_info_.obj()),
123      Java_DeviceDisplayInfo_getPhysicalDisplayWidth(env, j_device_info_.obj()),
124      Java_DeviceDisplayInfo_getBitsPerPixel(env, j_device_info_.obj()),
125      Java_DeviceDisplayInfo_getBitsPerComponent(env, j_device_info_.obj()),
126      Java_DeviceDisplayInfo_getDIPScale(env, j_device_info_.obj()),
127      Java_DeviceDisplayInfo_getSmallestDIPWidth(env, j_device_info_.obj()));
128}
129
130SharedDeviceDisplayInfo::~SharedDeviceDisplayInfo() {
131}
132
133void SharedDeviceDisplayInfo::UpdateDisplayInfo(JNIEnv* env,
134                                                jobject jobj,
135                                                jint display_height,
136                                                jint display_width,
137                                                jint physical_display_height,
138                                                jint physical_display_width,
139                                                jint bits_per_pixel,
140                                                jint bits_per_component,
141                                                jdouble dip_scale,
142                                                jint smallest_dip_width) {
143  display_height_ = static_cast<int>(display_height);
144  display_width_ = static_cast<int>(display_width);
145  physical_display_height_ = static_cast<int>(physical_display_height);
146  physical_display_width_ = static_cast<int>(physical_display_width);
147  bits_per_pixel_ = static_cast<int>(bits_per_pixel);
148  bits_per_component_ = static_cast<int>(bits_per_component);
149  dip_scale_ = static_cast<double>(dip_scale);
150  smallest_dip_width_ = static_cast<int>(smallest_dip_width);
151}
152
153}  // namespace gfx
154