- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / android / shared_device_display_info.cc
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
12 namespace gfx {
13
14 // static JNI call
15 static void UpdateSharedDeviceDisplayInfo(JNIEnv* env,
16                                           jobject obj,
17                                           jint display_height,
18                                           jint display_width,
19                                           jint bits_per_pixel,
20                                           jint bits_per_component,
21                                           jdouble dip_scale,
22                                           jint smallest_dip_width) {
23   SharedDeviceDisplayInfo::GetInstance()->InvokeUpdate(env, obj,
24       display_height, display_width, bits_per_pixel, bits_per_component,
25       dip_scale, smallest_dip_width);
26 }
27
28 // static
29 SharedDeviceDisplayInfo* SharedDeviceDisplayInfo::GetInstance() {
30   return Singleton<SharedDeviceDisplayInfo>::get();
31 }
32
33 int SharedDeviceDisplayInfo::GetDisplayHeight() {
34   base::AutoLock autolock(lock_);
35   DCHECK_NE(0, display_height_);
36   return display_height_;
37 }
38
39 int SharedDeviceDisplayInfo::GetDisplayWidth() {
40   base::AutoLock autolock(lock_);
41   DCHECK_NE(0, display_width_);
42   return display_width_;
43 }
44
45 int SharedDeviceDisplayInfo::GetBitsPerPixel() {
46   base::AutoLock autolock(lock_);
47   DCHECK_NE(0, bits_per_pixel_);
48   return bits_per_pixel_;
49 }
50
51 int SharedDeviceDisplayInfo::GetBitsPerComponent() {
52   base::AutoLock autolock(lock_);
53   DCHECK_NE(0, bits_per_component_);
54   return bits_per_component_;
55 }
56
57 double SharedDeviceDisplayInfo::GetDIPScale() {
58   base::AutoLock autolock(lock_);
59   DCHECK_NE(0, dip_scale_);
60   return dip_scale_;
61 }
62
63 int SharedDeviceDisplayInfo::GetSmallestDIPWidth() {
64   base::AutoLock autolock(lock_);
65   DCHECK_NE(0, smallest_dip_width_);
66   return smallest_dip_width_;
67 }
68
69 // static
70 bool SharedDeviceDisplayInfo::RegisterSharedDeviceDisplayInfo(JNIEnv* env) {
71   return RegisterNativesImpl(env);
72 }
73
74 void SharedDeviceDisplayInfo::InvokeUpdate(JNIEnv* env,
75                                            jobject obj,
76                                            jint display_height,
77                                            jint display_width,
78                                            jint bits_per_pixel,
79                                            jint bits_per_component,
80                                            jdouble dip_scale,
81                                            jint smallest_dip_width) {
82   base::AutoLock autolock(lock_);
83
84   UpdateDisplayInfo(env, obj, display_height,
85       display_width, bits_per_pixel, bits_per_component, dip_scale,
86       smallest_dip_width);
87 }
88
89 SharedDeviceDisplayInfo::SharedDeviceDisplayInfo()
90     : display_height_(0),
91       display_width_(0),
92       bits_per_pixel_(0),
93       bits_per_component_(0),
94       dip_scale_(0),
95       smallest_dip_width_(0) {
96   JNIEnv* env = base::android::AttachCurrentThread();
97   j_device_info_.Reset(
98       Java_DeviceDisplayInfo_createWithListener(env,
99           base::android::GetApplicationContext()));
100   UpdateDisplayInfo(env, j_device_info_.obj(),
101       Java_DeviceDisplayInfo_getDisplayHeight(env, j_device_info_.obj()),
102       Java_DeviceDisplayInfo_getDisplayWidth(env, j_device_info_.obj()),
103       Java_DeviceDisplayInfo_getBitsPerPixel(env, j_device_info_.obj()),
104       Java_DeviceDisplayInfo_getBitsPerComponent(env, j_device_info_.obj()),
105       Java_DeviceDisplayInfo_getDIPScale(env, j_device_info_.obj()),
106       Java_DeviceDisplayInfo_getSmallestDIPWidth(env, j_device_info_.obj()));
107 }
108
109 SharedDeviceDisplayInfo::~SharedDeviceDisplayInfo() {
110 }
111
112 void SharedDeviceDisplayInfo::UpdateDisplayInfo(JNIEnv* env,
113                                                 jobject jobj,
114                                                 jint display_height,
115                                                 jint display_width,
116                                                 jint bits_per_pixel,
117                                                 jint bits_per_component,
118                                                 jdouble dip_scale,
119                                                 jint smallest_dip_width) {
120   display_height_ = static_cast<int>(display_height);
121   display_width_ = static_cast<int>(display_width);
122   bits_per_pixel_ = static_cast<int>(bits_per_pixel);
123   bits_per_component_ = static_cast<int>(bits_per_component);
124   dip_scale_ = static_cast<double>(dip_scale);
125   smallest_dip_width_ = static_cast<int>(smallest_dip_width);
126 }
127
128 }  // namespace gfx