android_graphics_PixelFormat.cpp revision a696f5d667227365da732481770767dcb330dd23
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
28namespace android {
29
30// ----------------------------------------------------------------------------
31
32struct offsets_t {
33    jfieldID bytesPerPixel;
34    jfieldID bitsPerPixel;
35};
36
37static offsets_t offsets;
38
39static 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
47static void android_graphics_getPixelFormatInfo(
48        JNIEnv* env, jobject clazz, jint format, jobject pixelFormatObject)
49{
50    PixelFormatInfo info;
51    status_t err;
52
53    // we need this for backward compatibility with PixelFormat's
54    // deprecated constants
55    switch (format) {
56    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
57        // defined as the bytes per pixel of the Y plane
58        info.bytesPerPixel = 1;
59        info.bitsPerPixel = 16;
60        goto done;
61    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
62        // defined as the bytes per pixel of the Y plane
63        info.bytesPerPixel = 1;
64        info.bitsPerPixel = 12;
65        goto done;
66    case HAL_PIXEL_FORMAT_YCbCr_422_I:
67        // defined as the bytes per pixel of the Y plane
68        info.bytesPerPixel = 1;
69        info.bitsPerPixel = 16;
70        goto done;
71    }
72
73    err = getPixelFormatInfo(format, &info);
74    if (err < 0) {
75        doThrow(env, "java/lang/IllegalArgumentException");
76        return;
77    }
78
79done:
80    env->SetIntField(pixelFormatObject, offsets.bytesPerPixel, info.bytesPerPixel);
81    env->SetIntField(pixelFormatObject, offsets.bitsPerPixel,  info.bitsPerPixel);
82}
83// ----------------------------------------------------------------------------
84
85const char* const kClassPathName = "android/graphics/PixelFormat";
86
87static void nativeClassInit(JNIEnv* env, jclass clazz);
88
89static JNINativeMethod gMethods[] = {
90    {   "nativeClassInit", "()V",
91        (void*)nativeClassInit },
92	{   "getPixelFormatInfo", "(ILandroid/graphics/PixelFormat;)V",
93        (void*)android_graphics_getPixelFormatInfo
94    }
95};
96
97void nativeClassInit(JNIEnv* env, jclass clazz)
98{
99    offsets.bytesPerPixel = env->GetFieldID(clazz, "bytesPerPixel", "I");
100    offsets.bitsPerPixel  = env->GetFieldID(clazz, "bitsPerPixel", "I");
101}
102
103int register_android_graphics_PixelFormat(JNIEnv* env)
104{
105    return AndroidRuntime::registerNativeMethods(env,
106            kClassPathName, gMethods, NELEM(gMethods));
107}
108
109};
110