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