blob: 160d654b658f6fe8fb8c8b61f7a91fbf160ed861 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <assert.h>
19
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080020#include <cutils/properties.h>
21
Mathias Agopian000479f2010-02-09 17:46:37 -080022#include <surfaceflinger/SurfaceComposerClient.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <ui/PixelFormat.h>
24#include <ui/DisplayInfo.h>
25
26#include "jni.h"
27#include <android_runtime/AndroidRuntime.h>
28#include <utils/misc.h>
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080029#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31// ----------------------------------------------------------------------------
32
33namespace android {
34
35// ----------------------------------------------------------------------------
36
37struct offsets_t {
38 jfieldID display;
39 jfieldID pixelFormat;
40 jfieldID fps;
41 jfieldID density;
42 jfieldID xdpi;
43 jfieldID ydpi;
44};
45static offsets_t offsets;
46
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080047static int gShortSize = -1;
48static int gLongSize = -1;
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080049static int gOldSize = -1;
50static int gNewSize = -1;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
53{
54 jclass npeClazz = env->FindClass(exc);
55 env->ThrowNew(npeClazz, msg);
56}
57
58// ----------------------------------------------------------------------------
59
60static void android_view_Display_init(
61 JNIEnv* env, jobject clazz, jint dpy)
62{
63 DisplayInfo info;
64 status_t err = SurfaceComposerClient::getDisplayInfo(DisplayID(dpy), &info);
65 if (err < 0) {
66 doThrow(env, "java/lang/IllegalArgumentException");
67 return;
68 }
69 env->SetIntField(clazz, offsets.pixelFormat,info.pixelFormatInfo.format);
70 env->SetFloatField(clazz, offsets.fps, info.fps);
71 env->SetFloatField(clazz, offsets.density, info.density);
72 env->SetFloatField(clazz, offsets.xdpi, info.xdpi);
73 env->SetFloatField(clazz, offsets.ydpi, info.ydpi);
74}
75
76static jint android_view_Display_getWidth(
77 JNIEnv* env, jobject clazz)
78{
79 DisplayID dpy = env->GetIntField(clazz, offsets.display);
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080080 jint w = SurfaceComposerClient::getDisplayWidth(dpy);
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080081 if (gShortSize > 0) {
82 jint h = SurfaceComposerClient::getDisplayHeight(dpy);
83 return w < h ? gShortSize : gLongSize;
84 }
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080085 return w == gOldSize ? gNewSize : w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086}
87
88static jint android_view_Display_getHeight(
89 JNIEnv* env, jobject clazz)
90{
91 DisplayID dpy = env->GetIntField(clazz, offsets.display);
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080092 int h = SurfaceComposerClient::getDisplayHeight(dpy);
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080093 if (gShortSize > 0) {
94 jint w = SurfaceComposerClient::getDisplayWidth(dpy);
95 return h < w ? gShortSize : gLongSize;
96 }
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080097 return h == gOldSize ? gNewSize : h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098}
99
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800100static jint android_view_Display_getRealWidth(
101 JNIEnv* env, jobject clazz)
102{
103 DisplayID dpy = env->GetIntField(clazz, offsets.display);
104 return SurfaceComposerClient::getDisplayWidth(dpy);
105}
106
107static jint android_view_Display_getRealHeight(
108 JNIEnv* env, jobject clazz)
109{
110 DisplayID dpy = env->GetIntField(clazz, offsets.display);
111 return SurfaceComposerClient::getDisplayHeight(dpy);
112}
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114static jint android_view_Display_getOrientation(
115 JNIEnv* env, jobject clazz)
116{
117 DisplayID dpy = env->GetIntField(clazz, offsets.display);
118 return SurfaceComposerClient::getDisplayOrientation(dpy);
119}
120
121static jint android_view_Display_getDisplayCount(
122 JNIEnv* env, jclass clazz)
123{
124 return SurfaceComposerClient::getNumberOfDisplays();
125}
126
127// ----------------------------------------------------------------------------
128
129const char* const kClassPathName = "android/view/Display";
130
131static void nativeClassInit(JNIEnv* env, jclass clazz);
132
133static JNINativeMethod gMethods[] = {
134 { "nativeClassInit", "()V",
135 (void*)nativeClassInit },
136 { "getDisplayCount", "()I",
137 (void*)android_view_Display_getDisplayCount },
138 { "init", "(I)V",
139 (void*)android_view_Display_init },
140 { "getWidth", "()I",
141 (void*)android_view_Display_getWidth },
142 { "getHeight", "()I",
143 (void*)android_view_Display_getHeight },
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800144 { "getRealWidth", "()I",
145 (void*)android_view_Display_getRealWidth },
146 { "getRealHeight", "()I",
147 (void*)android_view_Display_getRealHeight },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 { "getOrientation", "()I",
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800149 (void*)android_view_Display_getOrientation }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150};
151
152void nativeClassInit(JNIEnv* env, jclass clazz)
153{
154 offsets.display = env->GetFieldID(clazz, "mDisplay", "I");
155 offsets.pixelFormat = env->GetFieldID(clazz, "mPixelFormat", "I");
156 offsets.fps = env->GetFieldID(clazz, "mRefreshRate", "F");
157 offsets.density = env->GetFieldID(clazz, "mDensity", "F");
158 offsets.xdpi = env->GetFieldID(clazz, "mDpiX", "F");
159 offsets.ydpi = env->GetFieldID(clazz, "mDpiY", "F");
160}
161
162int register_android_view_Display(JNIEnv* env)
163{
Dianne Hackborn4c7cc342010-12-16 16:37:39 -0800164 char buf[PROPERTY_VALUE_MAX];
165 int len = property_get("persist.demo.screensizehack", buf, "");
166 if (len > 0) {
167 int temp1, temp2;
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800168 if (sscanf(buf, "%dx%d", &temp1, &temp2) == 2) {
169 if (temp1 < temp2) {
170 gShortSize = temp1;
171 gLongSize = temp2;
172 } else {
173 gShortSize = temp2;
174 gLongSize = temp1;
175 }
176 } else if (sscanf(buf, "%d=%d", &temp1, &temp2) == 2) {
Dianne Hackborn4c7cc342010-12-16 16:37:39 -0800177 gOldSize = temp1;
178 gNewSize = temp2;
179 }
180 }
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 return AndroidRuntime::registerNativeMethods(env,
183 kClassPathName, gMethods, NELEM(gMethods));
184}
185
186};