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" |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 27 | #include "JNIHelp.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <android_runtime/AndroidRuntime.h> |
| 29 | #include <utils/misc.h> |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 30 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | struct offsets_t { |
| 39 | jfieldID display; |
| 40 | jfieldID pixelFormat; |
| 41 | jfieldID fps; |
| 42 | jfieldID density; |
| 43 | jfieldID xdpi; |
| 44 | jfieldID ydpi; |
| 45 | }; |
| 46 | static offsets_t offsets; |
| 47 | |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 48 | static int gShortSize = -1; |
| 49 | static int gLongSize = -1; |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 50 | static int gOldSize = -1; |
| 51 | static int gNewSize = -1; |
| 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | // ---------------------------------------------------------------------------- |
| 54 | |
| 55 | static 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 Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 61 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | 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 | |
| 71 | static jint android_view_Display_getWidth( |
| 72 | JNIEnv* env, jobject clazz) |
| 73 | { |
| 74 | DisplayID dpy = env->GetIntField(clazz, offsets.display); |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 75 | jint w = SurfaceComposerClient::getDisplayWidth(dpy); |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 76 | if (gShortSize > 0) { |
| 77 | jint h = SurfaceComposerClient::getDisplayHeight(dpy); |
| 78 | return w < h ? gShortSize : gLongSize; |
| 79 | } |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 80 | return w == gOldSize ? gNewSize : w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static jint android_view_Display_getHeight( |
| 84 | JNIEnv* env, jobject clazz) |
| 85 | { |
| 86 | DisplayID dpy = env->GetIntField(clazz, offsets.display); |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 87 | int h = SurfaceComposerClient::getDisplayHeight(dpy); |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 88 | if (gShortSize > 0) { |
| 89 | jint w = SurfaceComposerClient::getDisplayWidth(dpy); |
| 90 | return h < w ? gShortSize : gLongSize; |
| 91 | } |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 92 | return h == gOldSize ? gNewSize : h; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 95 | static jint android_view_Display_getRawWidth( |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 96 | JNIEnv* env, jobject clazz) |
| 97 | { |
| 98 | DisplayID dpy = env->GetIntField(clazz, offsets.display); |
| 99 | return SurfaceComposerClient::getDisplayWidth(dpy); |
| 100 | } |
| 101 | |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 102 | static jint android_view_Display_getRawHeight( |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 103 | JNIEnv* env, jobject clazz) |
| 104 | { |
| 105 | DisplayID dpy = env->GetIntField(clazz, offsets.display); |
| 106 | return SurfaceComposerClient::getDisplayHeight(dpy); |
| 107 | } |
| 108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | static 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 | |
| 116 | static jint android_view_Display_getDisplayCount( |
| 117 | JNIEnv* env, jclass clazz) |
| 118 | { |
| 119 | return SurfaceComposerClient::getNumberOfDisplays(); |
| 120 | } |
| 121 | |
| 122 | // ---------------------------------------------------------------------------- |
| 123 | |
| 124 | const char* const kClassPathName = "android/view/Display"; |
| 125 | |
| 126 | static void nativeClassInit(JNIEnv* env, jclass clazz); |
| 127 | |
| 128 | static 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 Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 135 | { "getRealWidth", "()I", |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 136 | (void*)android_view_Display_getWidth }, |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 137 | { "getRealHeight", "()I", |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 138 | (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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | { "getOrientation", "()I", |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 144 | (void*)android_view_Display_getOrientation } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | void 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 | |
| 157 | int register_android_view_Display(JNIEnv* env) |
| 158 | { |
Dianne Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 159 | char buf[PROPERTY_VALUE_MAX]; |
| 160 | int len = property_get("persist.demo.screensizehack", buf, ""); |
| 161 | if (len > 0) { |
| 162 | int temp1, temp2; |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 163 | 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 Hackborn | 4c7cc34 | 2010-12-16 16:37:39 -0800 | [diff] [blame] | 172 | gOldSize = temp1; |
| 173 | gNewSize = temp2; |
| 174 | } |
| 175 | } |
| 176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | return AndroidRuntime::registerNativeMethods(env, |
| 178 | kClassPathName, gMethods, NELEM(gMethods)); |
| 179 | } |
| 180 | |
| 181 | }; |