android_view_Display.cpp revision 99aac7beca18b6d73e40db5e8e49f52f94be638e
1/*
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
20#include <cutils/properties.h>
21
22#include <surfaceflinger/SurfaceComposerClient.h>
23#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>
29#include <utils/Log.h>
30
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
47static int gShortSize = -1;
48static int gLongSize = -1;
49static int gOldSize = -1;
50static int gNewSize = -1;
51
52static 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);
80    jint w = SurfaceComposerClient::getDisplayWidth(dpy);
81    if (gShortSize > 0) {
82        jint h = SurfaceComposerClient::getDisplayHeight(dpy);
83        return w < h ? gShortSize : gLongSize;
84    }
85    return w == gOldSize ? gNewSize : w;
86}
87
88static jint android_view_Display_getHeight(
89        JNIEnv* env, jobject clazz)
90{
91    DisplayID dpy = env->GetIntField(clazz, offsets.display);
92    int h = SurfaceComposerClient::getDisplayHeight(dpy);
93    if (gShortSize > 0) {
94        jint w = SurfaceComposerClient::getDisplayWidth(dpy);
95        return h < w ? gShortSize : gLongSize;
96    }
97    return h == gOldSize ? gNewSize : h;
98}
99
100static 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
114static 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 },
144    {   "getRealWidth", "()I",
145            (void*)android_view_Display_getRealWidth },
146    {   "getRealHeight", "()I",
147            (void*)android_view_Display_getRealHeight },
148    {   "getOrientation", "()I",
149            (void*)android_view_Display_getOrientation }
150};
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{
164    char buf[PROPERTY_VALUE_MAX];
165    int len = property_get("persist.demo.screensizehack", buf, "");
166    if (len > 0) {
167        int temp1, temp2;
168        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) {
177            gOldSize = temp1;
178            gNewSize = temp2;
179        }
180    }
181
182    return AndroidRuntime::registerNativeMethods(env,
183            kClassPathName, gMethods, NELEM(gMethods));
184}
185
186};
187