blob: 97f9fc34dae6c99ad101fa57fc56603ce835305f [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"
Elliott Hughes8451b252011-04-07 19:17:57 -070027#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <android_runtime/AndroidRuntime.h>
29#include <utils/misc.h>
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080030#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32// ----------------------------------------------------------------------------
33
34namespace android {
35
36// ----------------------------------------------------------------------------
37
38struct offsets_t {
39 jfieldID display;
40 jfieldID pixelFormat;
41 jfieldID fps;
42 jfieldID density;
43 jfieldID xdpi;
44 jfieldID ydpi;
45};
46static offsets_t offsets;
47
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080048static int gShortSize = -1;
49static int gLongSize = -1;
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080050static int gOldSize = -1;
51static int gNewSize = -1;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053// ----------------------------------------------------------------------------
54
55static void android_view_Display_init(
56 JNIEnv* env, jobject clazz, jint dpy)
57{
58 DisplayInfo info;
59 status_t err = SurfaceComposerClient::getDisplayInfo(DisplayID(dpy), &info);
60 if (err < 0) {
Elliott Hughes8451b252011-04-07 19:17:57 -070061 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 return;
63 }
64 env->SetIntField(clazz, offsets.pixelFormat,info.pixelFormatInfo.format);
65 env->SetFloatField(clazz, offsets.fps, info.fps);
66 env->SetFloatField(clazz, offsets.density, info.density);
67 env->SetFloatField(clazz, offsets.xdpi, info.xdpi);
68 env->SetFloatField(clazz, offsets.ydpi, info.ydpi);
69}
70
71static jint android_view_Display_getWidth(
72 JNIEnv* env, jobject clazz)
73{
74 DisplayID dpy = env->GetIntField(clazz, offsets.display);
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080075 jint w = SurfaceComposerClient::getDisplayWidth(dpy);
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080076 if (gShortSize > 0) {
77 jint h = SurfaceComposerClient::getDisplayHeight(dpy);
78 return w < h ? gShortSize : gLongSize;
79 }
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080080 return w == gOldSize ? gNewSize : w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081}
82
83static jint android_view_Display_getHeight(
84 JNIEnv* env, jobject clazz)
85{
86 DisplayID dpy = env->GetIntField(clazz, offsets.display);
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080087 int h = SurfaceComposerClient::getDisplayHeight(dpy);
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080088 if (gShortSize > 0) {
89 jint w = SurfaceComposerClient::getDisplayWidth(dpy);
90 return h < w ? gShortSize : gLongSize;
91 }
Dianne Hackborn4c7cc342010-12-16 16:37:39 -080092 return h == gOldSize ? gNewSize : h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093}
94
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070095static jint android_view_Display_getRawWidth(
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080096 JNIEnv* env, jobject clazz)
97{
98 DisplayID dpy = env->GetIntField(clazz, offsets.display);
99 return SurfaceComposerClient::getDisplayWidth(dpy);
100}
101
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700102static jint android_view_Display_getRawHeight(
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800103 JNIEnv* env, jobject clazz)
104{
105 DisplayID dpy = env->GetIntField(clazz, offsets.display);
106 return SurfaceComposerClient::getDisplayHeight(dpy);
107}
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109static jint android_view_Display_getOrientation(
110 JNIEnv* env, jobject clazz)
111{
112 DisplayID dpy = env->GetIntField(clazz, offsets.display);
113 return SurfaceComposerClient::getDisplayOrientation(dpy);
114}
115
116static jint android_view_Display_getDisplayCount(
117 JNIEnv* env, jclass clazz)
118{
119 return SurfaceComposerClient::getNumberOfDisplays();
120}
121
122// ----------------------------------------------------------------------------
123
124const char* const kClassPathName = "android/view/Display";
125
126static void nativeClassInit(JNIEnv* env, jclass clazz);
127
128static JNINativeMethod gMethods[] = {
129 { "nativeClassInit", "()V",
130 (void*)nativeClassInit },
131 { "getDisplayCount", "()I",
132 (void*)android_view_Display_getDisplayCount },
133 { "init", "(I)V",
134 (void*)android_view_Display_init },
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800135 { "getRealWidth", "()I",
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700136 (void*)android_view_Display_getWidth },
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800137 { "getRealHeight", "()I",
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700138 (void*)android_view_Display_getHeight },
139 { "getRawWidth", "()I",
140 (void*)android_view_Display_getRawWidth },
141 { "getRawHeight", "()I",
142 (void*)android_view_Display_getRawHeight },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 { "getOrientation", "()I",
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800144 (void*)android_view_Display_getOrientation }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145};
146
147void nativeClassInit(JNIEnv* env, jclass clazz)
148{
149 offsets.display = env->GetFieldID(clazz, "mDisplay", "I");
150 offsets.pixelFormat = env->GetFieldID(clazz, "mPixelFormat", "I");
151 offsets.fps = env->GetFieldID(clazz, "mRefreshRate", "F");
152 offsets.density = env->GetFieldID(clazz, "mDensity", "F");
153 offsets.xdpi = env->GetFieldID(clazz, "mDpiX", "F");
154 offsets.ydpi = env->GetFieldID(clazz, "mDpiY", "F");
155}
156
157int register_android_view_Display(JNIEnv* env)
158{
Dianne Hackborn4c7cc342010-12-16 16:37:39 -0800159 char buf[PROPERTY_VALUE_MAX];
160 int len = property_get("persist.demo.screensizehack", buf, "");
161 if (len > 0) {
162 int temp1, temp2;
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800163 if (sscanf(buf, "%dx%d", &temp1, &temp2) == 2) {
164 if (temp1 < temp2) {
165 gShortSize = temp1;
166 gLongSize = temp2;
167 } else {
168 gShortSize = temp2;
169 gLongSize = temp1;
170 }
171 } else if (sscanf(buf, "%d=%d", &temp1, &temp2) == 2) {
Dianne Hackborn4c7cc342010-12-16 16:37:39 -0800172 gOldSize = temp1;
173 gNewSize = temp2;
174 }
175 }
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 return AndroidRuntime::registerNativeMethods(env,
178 kClassPathName, gMethods, NELEM(gMethods));
179}
180
181};