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                                          jint rotation_degrees) {
26  SharedDeviceDisplayInfo::GetInstance()->InvokeUpdate(env, obj,
27      display_height, display_width,
28      physical_display_height, physical_display_width,
29      bits_per_pixel, bits_per_component,
30      dip_scale, smallest_dip_width, rotation_degrees);
31}
32
33// static
34SharedDeviceDisplayInfo* SharedDeviceDisplayInfo::GetInstance() {
35  return Singleton<SharedDeviceDisplayInfo>::get();
36}
37
38int SharedDeviceDisplayInfo::GetDisplayHeight() {
39  base::AutoLock autolock(lock_);
40  DCHECK_NE(0, display_height_);
41  return display_height_;
42}
43
44int SharedDeviceDisplayInfo::GetDisplayWidth() {
45  base::AutoLock autolock(lock_);
46  DCHECK_NE(0, display_width_);
47  return display_width_;
48}
49
50int SharedDeviceDisplayInfo::GetPhysicalDisplayHeight() {
51  base::AutoLock autolock(lock_);
52  return physical_display_height_;
53}
54
55int SharedDeviceDisplayInfo::GetPhysicalDisplayWidth() {
56  base::AutoLock autolock(lock_);
57  return physical_display_width_;
58}
59
60int SharedDeviceDisplayInfo::GetBitsPerPixel() {
61  base::AutoLock autolock(lock_);
62  DCHECK_NE(0, bits_per_pixel_);
63  return bits_per_pixel_;
64}
65
66int SharedDeviceDisplayInfo::GetBitsPerComponent() {
67  base::AutoLock autolock(lock_);
68  DCHECK_NE(0, bits_per_component_);
69  return bits_per_component_;
70}
71
72double SharedDeviceDisplayInfo::GetDIPScale() {
73  base::AutoLock autolock(lock_);
74  DCHECK_NE(0, dip_scale_);
75  return dip_scale_;
76}
77
78int SharedDeviceDisplayInfo::GetSmallestDIPWidth() {
79  base::AutoLock autolock(lock_);
80  DCHECK_NE(0, smallest_dip_width_);
81  return smallest_dip_width_;
82}
83
84int SharedDeviceDisplayInfo::GetRotationDegrees() {
85  base::AutoLock autolock(lock_);
86  return rotation_degrees_;
87}
88
89// static
90bool SharedDeviceDisplayInfo::RegisterSharedDeviceDisplayInfo(JNIEnv* env) {
91  return RegisterNativesImpl(env);
92}
93
94void SharedDeviceDisplayInfo::InvokeUpdate(JNIEnv* env,
95                                           jobject obj,
96                                           jint display_height,
97                                           jint display_width,
98                                           jint physical_display_height,
99                                           jint physical_display_width,
100                                           jint bits_per_pixel,
101                                           jint bits_per_component,
102                                           jdouble dip_scale,
103                                           jint smallest_dip_width,
104                                           jint rotation_degrees) {
105  base::AutoLock autolock(lock_);
106
107  UpdateDisplayInfo(env, obj,
108      display_height, display_width,
109      physical_display_height, physical_display_width,
110      bits_per_pixel, bits_per_component, dip_scale,
111      smallest_dip_width, rotation_degrees);
112}
113
114SharedDeviceDisplayInfo::SharedDeviceDisplayInfo()
115    : display_height_(0),
116      display_width_(0),
117      bits_per_pixel_(0),
118      bits_per_component_(0),
119      dip_scale_(0),
120      smallest_dip_width_(0) {
121  JNIEnv* env = base::android::AttachCurrentThread();
122  j_device_info_.Reset(
123      Java_DeviceDisplayInfo_create(
124          env, base::android::GetApplicationContext()));
125  UpdateDisplayInfo(env, j_device_info_.obj(),
126      Java_DeviceDisplayInfo_getDisplayHeight(env, j_device_info_.obj()),
127      Java_DeviceDisplayInfo_getDisplayWidth(env, j_device_info_.obj()),
128      Java_DeviceDisplayInfo_getPhysicalDisplayHeight(env,
129                                                      j_device_info_.obj()),
130      Java_DeviceDisplayInfo_getPhysicalDisplayWidth(env, j_device_info_.obj()),
131      Java_DeviceDisplayInfo_getBitsPerPixel(env, j_device_info_.obj()),
132      Java_DeviceDisplayInfo_getBitsPerComponent(env, j_device_info_.obj()),
133      Java_DeviceDisplayInfo_getDIPScale(env, j_device_info_.obj()),
134      Java_DeviceDisplayInfo_getSmallestDIPWidth(env, j_device_info_.obj()),
135      Java_DeviceDisplayInfo_getRotationDegrees(env, j_device_info_.obj()));
136}
137
138SharedDeviceDisplayInfo::~SharedDeviceDisplayInfo() {
139}
140
141void SharedDeviceDisplayInfo::UpdateDisplayInfo(JNIEnv* env,
142                                                jobject jobj,
143                                                jint display_height,
144                                                jint display_width,
145                                                jint physical_display_height,
146                                                jint physical_display_width,
147                                                jint bits_per_pixel,
148                                                jint bits_per_component,
149                                                jdouble dip_scale,
150                                                jint smallest_dip_width,
151                                                jint rotation_degrees) {
152  display_height_ = static_cast<int>(display_height);
153  display_width_ = static_cast<int>(display_width);
154  physical_display_height_ = static_cast<int>(physical_display_height);
155  physical_display_width_ = static_cast<int>(physical_display_width);
156  bits_per_pixel_ = static_cast<int>(bits_per_pixel);
157  bits_per_component_ = static_cast<int>(bits_per_component);
158  dip_scale_ = static_cast<double>(dip_scale);
159  smallest_dip_width_ = static_cast<int>(smallest_dip_width);
160  rotation_degrees_ = static_cast<int>(rotation_degrees);
161}
162
163}  // namespace gfx
164