13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Tester Core
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Android JNI interface.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuDefs.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuApp.hpp"
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuAndroidExecService.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuTestLog.hpp"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuCommandLine.hpp"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string>
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <vector>
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <cstring>
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <jni.h>
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdlib.h>
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <android/log.h>
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// ExecService entry points.
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic jfieldID getExecServiceField (JNIEnv* env, jobject obj)
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jclass cls = env->GetObjectClass(obj);
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TCU_CHECK_INTERNAL(cls);
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jfieldID fid = env->GetFieldID(cls, "m_server", "J");
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TCU_CHECK_INTERNAL(fid);
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return fid;
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic tcu::Android::ExecService* getExecService (JNIEnv* env, jobject obj)
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jfieldID field = getExecServiceField(env, obj);
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return (tcu::Android::ExecService*)(deIntptr)env->GetLongField(obj, field);
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void setExecService (JNIEnv* env, jobject obj, tcu::Android::ExecService* service)
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	jfieldID field = getExecServiceField(env, obj);
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	env->SetLongField(obj, field, (jlong)(deIntptr)service);
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void logException (const std::exception& e)
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	__android_log_print(ANDROID_LOG_ERROR, "dEQP", "%s", e.what());
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
683c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_BEGIN_EXTERN_C
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
70330f6e62b24a80eb92f08a60eb6b769480572be9Pyry HaulosJNIEXPORT void JNICALL Java_com_drawelements_deqp_execserver_ExecService_startServer (JNIEnv* env, jobject obj, jint port)
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::Android::ExecService*	service		= DE_NULL;
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	JavaVM*						vm			= DE_NULL;
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_ASSERT(!getExecService(env, obj));
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		env->GetJavaVM(&vm);
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK_INTERNAL(vm);
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
82330f6e62b24a80eb92f08a60eb6b769480572be9Pyry Haulos		service = new tcu::Android::ExecService(vm, obj, port);
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		service->start();
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		setExecService(env, obj, service);
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (const std::exception& e)
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		logException(e);
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete service;
91330f6e62b24a80eb92f08a60eb6b769480572be9Pyry Haulos		tcu::die("ExecService.startServer() failed");
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
95330f6e62b24a80eb92f08a60eb6b769480572be9Pyry HaulosJNIEXPORT void JNICALL Java_com_drawelements_deqp_execserver_ExecService_stopServer (JNIEnv* env, jobject obj)
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		tcu::Android::ExecService* service = getExecService(env, obj);
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK_INTERNAL(service);
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		service->stop();
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete service;
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		setExecService(env, obj, DE_NULL);
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (const std::exception& e)
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		logException(e);
110330f6e62b24a80eb92f08a60eb6b769480572be9Pyry Haulos		tcu::die("ExecService.stopServer() failed");
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1143c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_END_EXTERN_C
115