• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ui/PixelFormat.h>
21 
22 #include "jni.h"
23 #include <android_runtime/AndroidRuntime.h>
24 #include <utils/misc.h>
25 
26 // ----------------------------------------------------------------------------
27 
28 namespace android {
29 
30 // ----------------------------------------------------------------------------
31 
32 struct offsets_t {
33     jfieldID bytesPerPixel;
34     jfieldID bitsPerPixel;
35 };
36 
37 static offsets_t offsets;
38 
doThrow(JNIEnv * env,const char * exc,const char * msg=NULL)39 static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
40 {
41     jclass npeClazz = env->FindClass(exc);
42     env->ThrowNew(npeClazz, msg);
43 }
44 
45 // ----------------------------------------------------------------------------
46 
android_graphics_getPixelFormatInfo(JNIEnv * env,jobject clazz,jint format,jobject pixelFormatObject)47 static void android_graphics_getPixelFormatInfo(
48         JNIEnv* env, jobject clazz, jint format, jobject pixelFormatObject)
49 {
50     PixelFormatInfo info;
51     status_t err = getPixelFormatInfo(format, &info);
52     if (err < 0) {
53         doThrow(env, "java/lang/IllegalArgumentException");
54         return;
55     }
56     env->SetIntField(pixelFormatObject, offsets.bytesPerPixel, info.bytesPerPixel);
57     env->SetIntField(pixelFormatObject, offsets.bitsPerPixel,  info.bitsPerPixel);
58 }
59 // ----------------------------------------------------------------------------
60 
61 const char* const kClassPathName = "android/graphics/PixelFormat";
62 
63 static void nativeClassInit(JNIEnv* env, jclass clazz);
64 
65 static JNINativeMethod gMethods[] = {
66     {   "nativeClassInit", "()V",
67         (void*)nativeClassInit },
68 	{   "getPixelFormatInfo", "(ILandroid/graphics/PixelFormat;)V",
69         (void*)android_graphics_getPixelFormatInfo
70     }
71 };
72 
nativeClassInit(JNIEnv * env,jclass clazz)73 void nativeClassInit(JNIEnv* env, jclass clazz)
74 {
75     offsets.bytesPerPixel = env->GetFieldID(clazz, "bytesPerPixel", "I");
76     offsets.bitsPerPixel  = env->GetFieldID(clazz, "bitsPerPixel", "I");
77 }
78 
register_android_graphics_PixelFormat(JNIEnv * env)79 int register_android_graphics_PixelFormat(JNIEnv* env)
80 {
81     return AndroidRuntime::registerNativeMethods(env,
82             kClassPathName, gMethods, NELEM(gMethods));
83 }
84 
85 };
86