Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright 2017 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 
     18 #define LOG_TAG "ANativeWindowTest"
     19 
     20 #include <array>
     21 
     22 #include <android/native_window.h>
     23 #include <android/native_window_jni.h>
     24 #include <jni.h>
     25 
     26 namespace {
     27 
     28 void pushBufferWithTransform(JNIEnv* env, jclass, jobject jSurface, jint transform) {
     29     auto window = ANativeWindow_fromSurface(env, jSurface);
     30     ANativeWindow_setBuffersTransform(window, transform);
     31     ANativeWindow_Buffer mappedBuffer;
     32     ANativeWindow_lock(window, &mappedBuffer, nullptr);
     33     ANativeWindow_unlockAndPost(window);
     34     ANativeWindow_release(window);
     35 }
     36 
     37 jint setBuffersDataSpace(JNIEnv* env, jclass, jobject jSurface, jint dataSpace) {
     38     ANativeWindow* window = nullptr;
     39     if (jSurface) {
     40         window = ANativeWindow_fromSurface(env, jSurface);
     41     }
     42     int error = ANativeWindow_setBuffersDataSpace(window, dataSpace);
     43     if (error != 0) {
     44         return error;
     45     }
     46     ANativeWindow_Buffer mappedBuffer;
     47     ANativeWindow_lock(window, &mappedBuffer, nullptr);
     48     ANativeWindow_unlockAndPost(window);
     49     ANativeWindow_release(window);
     50     return error;
     51 }
     52 
     53 jint getBuffersDataSpace(JNIEnv* env, jclass, jobject jSurface) {
     54     ANativeWindow* window = nullptr;
     55     if (jSurface) {
     56         window = ANativeWindow_fromSurface(env, jSurface);
     57     }
     58     return ANativeWindow_getBuffersDataSpace(window);
     59 }
     60 
     61 const std::array<JNINativeMethod, 3> JNI_METHODS = {{
     62     { "nPushBufferWithTransform", "(Landroid/view/Surface;I)V", (void*)pushBufferWithTransform },
     63     { "nSetBuffersDataSpace", "(Landroid/view/Surface;I)I", (void*)setBuffersDataSpace },
     64     { "nGetBuffersDataSpace", "(Landroid/view/Surface;)I", (void*)getBuffersDataSpace },
     65 }};
     66 
     67 }
     68 
     69 int register_android_graphics_cts_ANativeWindowTest(JNIEnv* env) {
     70     jclass clazz = env->FindClass("android/graphics/cts/ANativeWindowTest");
     71     return env->RegisterNatives(clazz, JNI_METHODS.data(), JNI_METHODS.size());
     72 }
     73