blob: f076cc85e3ba00b7ab995cb7133a928a931f4504 [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>
Mike Lockwood3a74bd32011-08-12 13:55:22 -070031#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33// ----------------------------------------------------------------------------
34
35namespace android {
36
37// ----------------------------------------------------------------------------
38
39struct offsets_t {
40 jfieldID display;
41 jfieldID pixelFormat;
42 jfieldID fps;
43 jfieldID density;
44 jfieldID xdpi;
45 jfieldID ydpi;
46};
47static offsets_t offsets;
Mike Lockwood3a74bd32011-08-12 13:55:22 -070048static bool headless = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050// ----------------------------------------------------------------------------
51
52static void android_view_Display_init(
53 JNIEnv* env, jobject clazz, jint dpy)
54{
55 DisplayInfo info;
Mike Lockwood3a74bd32011-08-12 13:55:22 -070056 if (headless) {
57 // initialize dummy display with reasonable values
58 info.pixelFormatInfo.format = 1; // RGB_8888
59 info.fps = 60;
60 info.density = 160;
61 info.xdpi = 160;
62 info.ydpi = 160;
63 } else {
64 status_t err = SurfaceComposerClient::getDisplayInfo(DisplayID(dpy), &info);
65 if (err < 0) {
66 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
67 return;
68 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 }
70 env->SetIntField(clazz, offsets.pixelFormat,info.pixelFormatInfo.format);
71 env->SetFloatField(clazz, offsets.fps, info.fps);
72 env->SetFloatField(clazz, offsets.density, info.density);
73 env->SetFloatField(clazz, offsets.xdpi, info.xdpi);
74 env->SetFloatField(clazz, offsets.ydpi, info.ydpi);
75}
76
Dianne Hackbornec537452011-09-14 19:19:55 -070077static jint android_view_Display_getRawWidthNative(
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080078 JNIEnv* env, jobject clazz)
79{
Mike Lockwood3a74bd32011-08-12 13:55:22 -070080 if (headless) return 640;
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080081 DisplayID dpy = env->GetIntField(clazz, offsets.display);
82 return SurfaceComposerClient::getDisplayWidth(dpy);
83}
84
Dianne Hackbornec537452011-09-14 19:19:55 -070085static jint android_view_Display_getRawHeightNative(
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080086 JNIEnv* env, jobject clazz)
87{
Mike Lockwood3a74bd32011-08-12 13:55:22 -070088 if (headless) return 480;
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080089 DisplayID dpy = env->GetIntField(clazz, offsets.display);
90 return SurfaceComposerClient::getDisplayHeight(dpy);
91}
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093static jint android_view_Display_getOrientation(
94 JNIEnv* env, jobject clazz)
95{
Mike Lockwood3a74bd32011-08-12 13:55:22 -070096 if (headless) return 0; // Surface.ROTATION_0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 DisplayID dpy = env->GetIntField(clazz, offsets.display);
98 return SurfaceComposerClient::getDisplayOrientation(dpy);
99}
100
101static jint android_view_Display_getDisplayCount(
102 JNIEnv* env, jclass clazz)
103{
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700104 if (headless) return 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 return SurfaceComposerClient::getNumberOfDisplays();
106}
107
108// ----------------------------------------------------------------------------
109
110const char* const kClassPathName = "android/view/Display";
111
112static void nativeClassInit(JNIEnv* env, jclass clazz);
113
114static JNINativeMethod gMethods[] = {
115 { "nativeClassInit", "()V",
116 (void*)nativeClassInit },
117 { "getDisplayCount", "()I",
118 (void*)android_view_Display_getDisplayCount },
119 { "init", "(I)V",
120 (void*)android_view_Display_init },
Dianne Hackbornec537452011-09-14 19:19:55 -0700121 { "getRawWidthNative", "()I",
122 (void*)android_view_Display_getRawWidthNative },
123 { "getRawHeightNative", "()I",
124 (void*)android_view_Display_getRawHeightNative },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 { "getOrientation", "()I",
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800126 (void*)android_view_Display_getOrientation }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127};
128
129void nativeClassInit(JNIEnv* env, jclass clazz)
130{
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700131 char value[PROPERTY_VALUE_MAX];
132
133 property_get("ro.config.headless", value, "0");
134 if (strcmp(value, "1") == 0)
135 headless = true;
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 offsets.display = env->GetFieldID(clazz, "mDisplay", "I");
138 offsets.pixelFormat = env->GetFieldID(clazz, "mPixelFormat", "I");
139 offsets.fps = env->GetFieldID(clazz, "mRefreshRate", "F");
140 offsets.density = env->GetFieldID(clazz, "mDensity", "F");
141 offsets.xdpi = env->GetFieldID(clazz, "mDpiX", "F");
142 offsets.ydpi = env->GetFieldID(clazz, "mDpiY", "F");
143}
144
145int register_android_view_Display(JNIEnv* env)
146{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 return AndroidRuntime::registerNativeMethods(env,
148 kClassPathName, gMethods, NELEM(gMethods));
149}
150
151};