The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 20 | #include <cutils/properties.h> |
| 21 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 22 | #include <surfaceflinger/SurfaceComposerClient.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | struct offsets_t { |
| 38 | jfieldID display; |
| 39 | jfieldID pixelFormat; |
| 40 | jfieldID fps; |
| 41 | jfieldID density; |
| 42 | jfieldID xdpi; |
| 43 | jfieldID ydpi; |
| 44 | }; |
| 45 | static offsets_t offsets; |
| 46 | |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 47 | static int gShortSize = -1; |
| 48 | static int gLongSize = -1; |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 49 | static int gOldSize = -1; |
| 50 | static int gNewSize = -1; |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | static 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 | |
| 60 | static 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 | |
| 76 | static jint android_view_Display_getWidth( |
| 77 | JNIEnv* env, jobject clazz) |
| 78 | { |
| 79 | DisplayID dpy = env->GetIntField(clazz, offsets.display); |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 80 | jint w = SurfaceComposerClient::getDisplayWidth(dpy); |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 81 | if (gShortSize > 0) { |
| 82 | jint h = SurfaceComposerClient::getDisplayHeight(dpy); |
| 83 | return w < h ? gShortSize : gLongSize; |
| 84 | } |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 85 | return w == gOldSize ? gNewSize : w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static jint android_view_Display_getHeight( |
| 89 | JNIEnv* env, jobject clazz) |
| 90 | { |
| 91 | DisplayID dpy = env->GetIntField(clazz, offsets.display); |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 92 | int h = SurfaceComposerClient::getDisplayHeight(dpy); |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 93 | if (gShortSize > 0) { |
| 94 | jint w = SurfaceComposerClient::getDisplayWidth(dpy); |
| 95 | return h < w ? gShortSize : gLongSize; |
| 96 | } |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 97 | return h == gOldSize ? gNewSize : h; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 100 | static 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 | |
| 107 | static 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | static 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 | |
| 121 | static jint android_view_Display_getDisplayCount( |
| 122 | JNIEnv* env, jclass clazz) |
| 123 | { |
| 124 | return SurfaceComposerClient::getNumberOfDisplays(); |
| 125 | } |
| 126 | |
| 127 | // ---------------------------------------------------------------------------- |
| 128 | |
| 129 | const char* const kClassPathName = "android/view/Display"; |
| 130 | |
| 131 | static void nativeClassInit(JNIEnv* env, jclass clazz); |
| 132 | |
| 133 | static 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 Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 144 | { "getRealWidth", "()I", |
| 145 | (void*)android_view_Display_getRealWidth }, |
| 146 | { "getRealHeight", "()I", |
| 147 | (void*)android_view_Display_getRealHeight }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | { "getOrientation", "()I", |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 149 | (void*)android_view_Display_getOrientation } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | void 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 | |
| 162 | int register_android_view_Display(JNIEnv* env) |
| 163 | { |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 164 | char buf[PROPERTY_VALUE_MAX]; |
| 165 | int len = property_get("persist.demo.screensizehack", buf, ""); |
| 166 | if (len > 0) { |
| 167 | int temp1, temp2; |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 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) { |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 177 | gOldSize = temp1; |
| 178 | gNewSize = temp2; |
| 179 | } |
| 180 | } |
| 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | return AndroidRuntime::registerNativeMethods(env, |
| 183 | kClassPathName, gMethods, NELEM(gMethods)); |
| 184 | } |
| 185 | |
| 186 | }; |