Beatmup
Beatmup::CallbackTask Class Reference

Task calling method of a Java object. More...

#include <callback_task.h>

Inheritance diagram for Beatmup::CallbackTask:
Beatmup::AbstractTask Beatmup::Object

Public Member Functions

 CallbackTask (JNIEnv *jenv)
 
 ~CallbackTask ()
 
void setCallbackObject (JNIEnv *jenv, jobject obj)
 Sets the object to be called. More...
 
- Public Member Functions inherited from Beatmup::AbstractTask
virtual void beforeProcessing (ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
 Instruction called before the task is executed. More...
 
virtual void afterProcessing (ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
 Instruction called after the task is executed. More...
 
virtual bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Attributes

static const char * JAVA_CLASS_NAME = "Beatmup/Utils/Callback"
 callback object java class name More...
 
static const char * JAVA_METHOD_NAME = "run"
 called method name More...
 

Private Member Functions

bool process (TaskThread &thread)
 Executes the task on CPU within a given thread. More...
 
TaskDeviceRequirement getUsedDevices () const
 Communicates devices (CPU and/or GPU) the task is run on. More...
 
ThreadIndex getMaxThreads () const
 Gives the upper limint on the number of threads the task may be performed by. More...
 

Private Attributes

JavaVM * jvm
 
jmethodID callbackMethodId
 
jobject callbackObject
 

Additional Inherited Members

- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 

Detailed Description

Task calling method of a Java object.

Definition at line 28 of file callback_task.h.

Constructor & Destructor Documentation

◆ CallbackTask()

CallbackTask::CallbackTask ( JNIEnv *  jenv)

Definition at line 27 of file callback_task.cpp.

27  : callbackObject(nullptr) {
28  jenv->GetJavaVM(&jvm);
29  jclass cls = jenv->FindClass(JAVA_CLASS_NAME);
30  callbackMethodId = jenv->GetMethodID(cls, JAVA_METHOD_NAME, "()V");
31 }
static const char * JAVA_METHOD_NAME
called method name
Definition: callback_task.h:46
jmethodID callbackMethodId
Definition: callback_task.h:31
static const char * JAVA_CLASS_NAME
callback object java class name
Definition: callback_task.h:45
JNIEnv jclass
JNIEnv * jenv

◆ ~CallbackTask()

CallbackTask::~CallbackTask ( )

Definition at line 34 of file callback_task.cpp.

34  {
35  if (callbackObject) {
36  JNIEnv *jenv;
37  jvm->AttachCurrentThread(&jenv, nullptr);
38  jenv->DeleteGlobalRef(callbackObject);
39  }
40 }

Member Function Documentation

◆ process()

bool CallbackTask::process ( TaskThread thread)
privatevirtual

Executes the task on CPU within a given thread.

Generally called by multiple threads.

Parameters
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Implements Beatmup::AbstractTask.

Definition at line 43 of file callback_task.cpp.

43  {
44  JNIEnv *jenv;
45  jvm->AttachCurrentThread(&jenv, nullptr);
46  jenv->CallVoidMethod(callbackObject, callbackMethodId);
47  return true;
48 }

◆ getUsedDevices()

TaskDeviceRequirement Beatmup::CallbackTask::getUsedDevices ( ) const
inlineprivatevirtual

Communicates devices (CPU and/or GPU) the task is run on.

Reimplemented from Beatmup::AbstractTask.

Definition at line 36 of file callback_task.h.

36  {
38  }
@ CPU_ONLY
this task does not use GPU

◆ getMaxThreads()

ThreadIndex Beatmup::CallbackTask::getMaxThreads ( ) const
inlineprivatevirtual

Gives the upper limint on the number of threads the task may be performed by.

The actual number of threads running a specific task may be less or equal to the returned value, depending on the number of workers in ThreadPool running the task.

Reimplemented from Beatmup::AbstractTask.

Definition at line 40 of file callback_task.h.

40  {
41  return 1;
42  }

◆ setCallbackObject()

void CallbackTask::setCallbackObject ( JNIEnv *  jenv,
jobject  obj 
)

Sets the object to be called.

The task calls a void method without parameters named JAVA_METHOD_NAME of a JAVA_CLASS_NAME class instance. The object is protected from accidental garbage collection by a global reference until the task instance is destroyed.

Parameters
jenvJava environment instance
objThe instance to be called

Definition at line 51 of file callback_task.cpp.

51  {
52  if (callbackObject)
53  jenv->DeleteGlobalRef(callbackObject);
54  callbackObject = jenv->NewGlobalRef(obj);
55 }

Member Data Documentation

◆ jvm

JavaVM* Beatmup::CallbackTask::jvm
private

Definition at line 30 of file callback_task.h.

◆ callbackMethodId

jmethodID Beatmup::CallbackTask::callbackMethodId
private

Definition at line 31 of file callback_task.h.

◆ callbackObject

jobject Beatmup::CallbackTask::callbackObject
private

Definition at line 32 of file callback_task.h.

◆ JAVA_CLASS_NAME

const char * CallbackTask::JAVA_CLASS_NAME = "Beatmup/Utils/Callback"
static

callback object java class name

Definition at line 45 of file callback_task.h.

◆ JAVA_METHOD_NAME

const char * CallbackTask::JAVA_METHOD_NAME = "run"
static

called method name

Definition at line 46 of file callback_task.h.


The documentation for this class was generated from the following files: