1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Android JNI interface.
22 *//*--------------------------------------------------------------------*/
23
24 #include "tcuDefs.hpp"
25 #include "tcuApp.hpp"
26 #include "tcuAndroidExecService.hpp"
27 #include "tcuTestLog.hpp"
28 #include "tcuCommandLine.hpp"
29
30 #include <string>
31 #include <vector>
32 #include <cstring>
33
34 #include <jni.h>
35 #include <stdlib.h>
36 #include <android/log.h>
37
38 // ExecService entry points.
39
getExecServiceField(JNIEnv * env,jobject obj)40 static jfieldID getExecServiceField(JNIEnv *env, jobject obj)
41 {
42 jclass cls = env->GetObjectClass(obj);
43 TCU_CHECK_INTERNAL(cls);
44
45 jfieldID fid = env->GetFieldID(cls, "m_server", "J");
46 TCU_CHECK_INTERNAL(fid);
47
48 return fid;
49 }
50
getExecService(JNIEnv * env,jobject obj)51 static tcu::Android::ExecService *getExecService(JNIEnv *env, jobject obj)
52 {
53 jfieldID field = getExecServiceField(env, obj);
54 return (tcu::Android::ExecService *)(intptr_t)env->GetLongField(obj, field);
55 }
56
setExecService(JNIEnv * env,jobject obj,tcu::Android::ExecService * service)57 static void setExecService(JNIEnv *env, jobject obj, tcu::Android::ExecService *service)
58 {
59 jfieldID field = getExecServiceField(env, obj);
60 env->SetLongField(obj, field, (jlong)(intptr_t)service);
61 }
62
logException(const std::exception & e)63 static void logException(const std::exception &e)
64 {
65 __android_log_print(ANDROID_LOG_ERROR, "dEQP", "%s", e.what());
66 }
67
68 DE_BEGIN_EXTERN_C
69
Java_com_drawelements_deqp_execserver_ExecService_startServer(JNIEnv * env,jobject obj,jint port)70 JNIEXPORT void JNICALL Java_com_drawelements_deqp_execserver_ExecService_startServer(JNIEnv *env, jobject obj,
71 jint port)
72 {
73 tcu::Android::ExecService *service = DE_NULL;
74 JavaVM *vm = DE_NULL;
75
76 try
77 {
78 DE_ASSERT(!getExecService(env, obj));
79
80 env->GetJavaVM(&vm);
81 TCU_CHECK_INTERNAL(vm);
82
83 service = new tcu::Android::ExecService(vm, obj, port);
84 service->start();
85
86 setExecService(env, obj, service);
87 }
88 catch (const std::exception &e)
89 {
90 logException(e);
91 delete service;
92 tcu::die("ExecService.startServer() failed");
93 }
94 }
95
Java_com_drawelements_deqp_execserver_ExecService_stopServer(JNIEnv * env,jobject obj)96 JNIEXPORT void JNICALL Java_com_drawelements_deqp_execserver_ExecService_stopServer(JNIEnv *env, jobject obj)
97 {
98 try
99 {
100 tcu::Android::ExecService *service = getExecService(env, obj);
101 TCU_CHECK_INTERNAL(service);
102
103 service->stop();
104 delete service;
105
106 setExecService(env, obj, DE_NULL);
107 }
108 catch (const std::exception &e)
109 {
110 logException(e);
111 tcu::die("ExecService.stopServer() failed");
112 }
113 }
114
115 DE_END_EXTERN_C
116