Home | History | Annotate | Download | only in jni
      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 "JNIHelp.h"
     28 #include <android_runtime/AndroidRuntime.h>
     29 #include <utils/misc.h>
     30 #include <utils/Log.h>
     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 
     48 // ----------------------------------------------------------------------------
     49 
     50 static void android_view_Display_init(
     51         JNIEnv* env, jobject clazz, jint dpy)
     52 {
     53     DisplayInfo info;
     54     status_t err = SurfaceComposerClient::getDisplayInfo(DisplayID(dpy), &info);
     55     if (err < 0) {
     56         jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
     57         return;
     58     }
     59     env->SetIntField(clazz, offsets.pixelFormat,info.pixelFormatInfo.format);
     60     env->SetFloatField(clazz, offsets.fps,      info.fps);
     61     env->SetFloatField(clazz, offsets.density,  info.density);
     62     env->SetFloatField(clazz, offsets.xdpi,     info.xdpi);
     63     env->SetFloatField(clazz, offsets.ydpi,     info.ydpi);
     64 }
     65 
     66 static jint android_view_Display_getRawWidthNative(
     67         JNIEnv* env, jobject clazz)
     68 {
     69     DisplayID dpy = env->GetIntField(clazz, offsets.display);
     70     return SurfaceComposerClient::getDisplayWidth(dpy);
     71 }
     72 
     73 static jint android_view_Display_getRawHeightNative(
     74         JNIEnv* env, jobject clazz)
     75 {
     76     DisplayID dpy = env->GetIntField(clazz, offsets.display);
     77     return SurfaceComposerClient::getDisplayHeight(dpy);
     78 }
     79 
     80 static jint android_view_Display_getOrientation(
     81         JNIEnv* env, jobject clazz)
     82 {
     83     DisplayID dpy = env->GetIntField(clazz, offsets.display);
     84     return SurfaceComposerClient::getDisplayOrientation(dpy);
     85 }
     86 
     87 static jint android_view_Display_getDisplayCount(
     88         JNIEnv* env, jclass clazz)
     89 {
     90     return SurfaceComposerClient::getNumberOfDisplays();
     91 }
     92 
     93 // ----------------------------------------------------------------------------
     94 
     95 const char* const kClassPathName = "android/view/Display";
     96 
     97 static void nativeClassInit(JNIEnv* env, jclass clazz);
     98 
     99 static JNINativeMethod gMethods[] = {
    100     {   "nativeClassInit", "()V",
    101             (void*)nativeClassInit },
    102     {   "getDisplayCount", "()I",
    103             (void*)android_view_Display_getDisplayCount },
    104 	{   "init", "(I)V",
    105             (void*)android_view_Display_init },
    106     {   "getRawWidthNative", "()I",
    107             (void*)android_view_Display_getRawWidthNative },
    108     {   "getRawHeightNative", "()I",
    109             (void*)android_view_Display_getRawHeightNative },
    110     {   "getOrientation", "()I",
    111             (void*)android_view_Display_getOrientation }
    112 };
    113 
    114 void nativeClassInit(JNIEnv* env, jclass clazz)
    115 {
    116     offsets.display     = env->GetFieldID(clazz, "mDisplay", "I");
    117     offsets.pixelFormat = env->GetFieldID(clazz, "mPixelFormat", "I");
    118     offsets.fps         = env->GetFieldID(clazz, "mRefreshRate", "F");
    119     offsets.density     = env->GetFieldID(clazz, "mDensity", "F");
    120     offsets.xdpi        = env->GetFieldID(clazz, "mDpiX", "F");
    121     offsets.ydpi        = env->GetFieldID(clazz, "mDpiY", "F");
    122 }
    123 
    124 int register_android_view_Display(JNIEnv* env)
    125 {
    126     return AndroidRuntime::registerNativeMethods(env,
    127             kClassPathName, gMethods, NELEM(gMethods));
    128 }
    129 
    130 };
    131