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