• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android/native_window_aidl.h>
25 #include <android/binder_parcel.h>
26 #include <android/binder_parcel_jni.h>
27 #include <android/binder_auto_utils.h>
28 #include <jni.h>
29 
30 #include "NativeTestHelpers.h"
31 
32 namespace {
33 
pushBufferWithTransform(JNIEnv * env,jclass,jobject jSurface,jint transform)34 void pushBufferWithTransform(JNIEnv* env, jclass, jobject jSurface, jint transform) {
35     auto window = ANativeWindow_fromSurface(env, jSurface);
36     ANativeWindow_setBuffersTransform(window, transform);
37     ANativeWindow_Buffer mappedBuffer;
38     ANativeWindow_lock(window, &mappedBuffer, nullptr);
39     ANativeWindow_unlockAndPost(window);
40     ANativeWindow_release(window);
41 }
42 
setBuffersDataSpace(JNIEnv * env,jclass,jobject jSurface,jint dataSpace)43 jint setBuffersDataSpace(JNIEnv* env, jclass, jobject jSurface, jint dataSpace) {
44     ANativeWindow* window = nullptr;
45     if (jSurface) {
46         window = ANativeWindow_fromSurface(env, jSurface);
47     }
48     int error = ANativeWindow_setBuffersDataSpace(window, dataSpace);
49     if (error != 0) {
50         return error;
51     }
52     ANativeWindow_Buffer mappedBuffer;
53     ANativeWindow_lock(window, &mappedBuffer, nullptr);
54     ANativeWindow_unlockAndPost(window);
55     ANativeWindow_release(window);
56     return error;
57 }
58 
getBuffersDataSpace(JNIEnv * env,jclass,jobject jSurface)59 jint getBuffersDataSpace(JNIEnv* env, jclass, jobject jSurface) {
60     ANativeWindow* window = nullptr;
61     if (jSurface) {
62         window = ANativeWindow_fromSurface(env, jSurface);
63     }
64     return ANativeWindow_getBuffersDataSpace(window);
65 }
66 
getBuffersDefaultDataspace(JNIEnv * env,jclass,jobject jSurface)67 jint getBuffersDefaultDataspace(JNIEnv* env, jclass, jobject jSurface) {
68     ANativeWindow* window = nullptr;
69     if (jSurface) {
70         window = ANativeWindow_fromSurface(env, jSurface);
71     }
72     return ANativeWindow_getBuffersDefaultDataSpace(window);
73 }
74 
tryAllocateBuffers(JNIEnv * env,jclass,jobject jSurface)75 void tryAllocateBuffers(JNIEnv* env, jclass, jobject jSurface) {
76     ANativeWindow* window = nullptr;
77     if (jSurface) {
78       window = ANativeWindow_fromSurface(env, jSurface);
79     }
80 
81     ANativeWindow_tryAllocateBuffers(window);
82 
83     if (window) {
84       ANativeWindow_release(window);
85     }
86 }
87 
readFromParcel(JNIEnv * env,jclass,jobject jParcel)88 jobject readFromParcel(JNIEnv* env, jclass, jobject jParcel) {
89     ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel));
90     ANativeWindow* window = nullptr;
91     auto result = ANativeWindow_readFromParcel(parcel.get(), &window);
92     jobject jSurface = nullptr;
93     if (result == STATUS_OK) {
94         jSurface = ANativeWindow_toSurface(env, window);
95         ANativeWindow_release(window);
96     }
97     return jSurface;
98 }
99 
writeToParcel(JNIEnv * env,jclass,jobject jSurface,jobject jParcel)100 void writeToParcel(JNIEnv* env, jclass, jobject jSurface, jobject jParcel) {
101     ANativeWindow* window = ANativeWindow_fromSurface(env, jSurface);
102     ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel));
103     auto result = ANativeWindow_writeToParcel(window, parcel.get());
104     ANativeWindow_release(window);
105     ASSERT_EQ(STATUS_OK, result);
106 }
107 
108 const std::array<JNINativeMethod, 7> JNI_METHODS = {{
109     {"nPushBufferWithTransform", "(Landroid/view/Surface;I)V",
110      (void*)pushBufferWithTransform},
111     {"nSetBuffersDataSpace", "(Landroid/view/Surface;I)I",
112      (void*)setBuffersDataSpace},
113     {"nGetBuffersDataSpace", "(Landroid/view/Surface;)I",
114      (void*)getBuffersDataSpace},
115     {"nGetBuffersDefaultDataSpace", "(Landroid/view/Surface;)I",
116      (void*)getBuffersDefaultDataspace},
117     {"nTryAllocateBuffers", "(Landroid/view/Surface;)V",
118      (void*)tryAllocateBuffers},
119     {"nReadFromParcel", "(Landroid/os/Parcel;)Landroid/view/Surface;", (void*)readFromParcel},
120     {"nWriteToParcel", "(Landroid/view/Surface;Landroid/os/Parcel;)V", (void*)writeToParcel},
121 }};
122 }
123 
register_android_graphics_cts_ANativeWindowTest(JNIEnv * env)124 int register_android_graphics_cts_ANativeWindowTest(JNIEnv* env) {
125     jclass clazz = env->FindClass("android/graphics/cts/ANativeWindowTest");
126     return env->RegisterNatives(clazz, JNI_METHODS.data(), JNI_METHODS.size());
127 }
128