Run my code in already existing and running thread - java

I'm using 3rd party which drawing to a view something.
I got the process threads list and i found the thread they are using to draw their stuff in.
I want to add my drawing code in this thread - meaning when they are finishing drawing their stuff in the thread then my code will run and then the thread will end (or whatever happens with it).
I tried to do my drawing code in different thread and UI Thread but I can see that my drawing is running after the 3rd party when we change the camera position.
Is it possible to inject some code into another thread? btw, I have only their Thread object in my hand which i got from running over all the threads list.
If not, other solution will be fine also (if there is)
-- EDIT --
Tried to suspend their thread, draw my stuff, and resume, but it is crashing - guess due to those are deprecated functions

Purely theoretical stuff ahead:
Check if they support drawing to backbuffer (not screen) -- OR -- try to redirect drawing to a backbuffer(framebuffer). Do your drawing in the backbuffer too. Manually switch backbuffer (actually back framebuffer) with front framebuffer.
They are most probably doing their drawing in a loop. The onDraw may be event triggered or it just tries to provide as much fps as it can. But if you synchronize the access to your backbuffer, you may force their draw thread to wait exactly for the time you do your drawing and the swap of buffers. You need to initially sync your procedure with theirs (probably by painfully testing that your code gets to run from time to time, and drawing is not blocked). And you must remember that if this work, the synchronization will mean that their thread will wait on their N loop for the end of your N-1 loop at exactly the first operation working with the backbuffer.

Related

Understanding the replica island rendering system. Why does it have a double-buffer of render commands?

I made a relatively simple structured Android game that could quite easily get away with updating the game logic and doing the rendering all inside the onDrawFrame function in the render thread of the GLSurfaceView. However, thinking of future projects I have begun to explore multi-threaded options, including running the main game update logic outside of the rendering thread, which is what replica island did. There is a blogpost about it here: http://replicaisland.blogspot.co.nz/2009/10/rendering-with-two-threads.html
Resources such as the replica island source code (https://code.google.com/archive/p/replicaisland/source) have been helpful in understanding this double-buffered render-command-registering multi-threaded renderer idea. The game loop updates objects and those that want to be drawn register themselves in the not-currently-being-used buffer. That buffer gets swapped at some point and the renderer works on those render-commands to draw them using opengl.
There are some things I am not really understanding yet.
The replica island game thread loop (GameThread.java) starts with a block waiting for the renderer to stop rendering (mRenderer.waitDrawingComplete) . It then updates the game logic, and then swaps the buffers. Swapping the buffers notifies the renderer to start on the next batch. When the renderer is done the game loop will update again, and so on.
This approach doesn't seem to run the game logic and the code that loops over the render objects in parallel? They seem to operate in lock-step, and derive their performance boost from the game logic not having to block while opengl actually finishes the rendering after the onDrawFrame function is finished.
I tested similar code and this is definitely faster than just single-threading it all when there is a large amount of stuff to be drawn. However, because the render loop does not loop over objects while the game thread is also working with them, there doesn't seem to be any inherent need to have the multiple render command buffers. From what I can read of it (and I am guessing I am mistaken somewhere) the game update is not actually updating the 2nd buffer while the renderer is using the first. If that is the case then why have two buffers at all?
Why is the game thread waiting for the renderer to stop drawing before it does its update?
Doom 3 does the same thing: process all of app input and events, then kick off a separate thread to run and draw the next frame while submitting the previous frame to the render system. Then, it waits for the Game Thread to return before swapping the buffers.
Both Threads are designed to use a specific buffer frame, the render or the draw frame. Because they are confined to a specific buffer you don't need to use mutexes or synchronization primitives to control access. Waiting on the thread is the synchronization.
It also keeps rendering and drawing in sync. You are already rendering a frame behind, and want to keep the lag to a minimum. Only drawing a single frame ahead means that your interpolation code is closer to elapsed time and only needs to emit render commands once per render frame. (As opposed to constantly blasting out frames and letting the render thread drop/skip).
Why is the game thread waiting for the renderer to stop drawing before it does its update?
The render system is a state machine, one that you can't concurrently execute commands against. Draw emits render commands that can be executed asynchronously on the other thread. Draw sends them and forgets. It doesn't care about the state or what happens to them. This is async.
Switching the window mode from fullscreen to windowed, changing surface resolutions or generally any other render state update needs to fully execute before continuing (synchronous or blocking) and must be performed with a know API state. These and other updates might affect Draw states, window states, etc...
So the wisdom is to perform event processing and update() on a single synchronized thread so we can block when we need to and change anything that affects states (any state). Then split off and issue render commands on the draw thread and execute the previous set on the render thread.

Threads in jMonkey and Nifty?

I have recently learnt that jMonkey operates on only a single thread which is its openGL render thread. However I'm not able to understand it completely. I can understand that it executes all update and initialize() calls on a single update loop but input should be independent of this update loop, otherwise it will become a polling mechanism.
What exactly happens when the jMonkey application starts. Three tasks that it needs to do is run a update loop, run initialize methods of app states once, do rendering and constantly cater to events? How does it manage all of this via a single thread?
What happens when I add a new app state in the initialize method of another app state?
In input handling input manager notifies the various listeners about events. How are nifty callback events say onClick() are handled on the same render loop?
Lastly in which order this listen- update-render loop runs and where we can find code related to it?
jME uses an approach that's very common in a lot of game engines (and also used in some other user interface libraries such as Swing).
Everything that the game engine does is done in one thread. This can be called the LWJGL thread but as jME can work with alternatives to LWJGL it's more generic to call it the Render Thread or just the jME Thread.
Everything is indeed done on the Render thread, and it does indeed use a polling mechanism. For example if you are holding down the "left" key then each frame the relevant control or app state will be called on the render thread and will move you left by an amount modified by tpf. Tpf is time-per-frame and is very important for keeping smooth movement and letting game systems run at the same speed independently of frame rate.
The only thing within jME3 that commonly uses a separate thread is the Physics Engine. That has a thread that it uses for doing the physics updates and then changes are pushed through to the Render thread using appropriate mechanisms. The system handles this for you though so it is not something you need to worry about.
The thread runs around a game loop. Each time around the loop it checks for things it needs to do (like enqueued tasks, initialize app states, render, etc. It calls update in each active Controller and control, etc). Once it has finished updating everything it then goes on to perform the render. All of this happens every single frame, but computers are so fast that it can still handle all of that and render the game at good frame rates.
That's an implementation detail that you shouldn't need to worry about except to know that it will definitely work. The adding actually gets queued up and handled in the next frame I think.
Everything is handled from the same loop. jME calls through to Nifty to allow Nifty to do its processing. As part of that processing Nifty detects the events and fires off the callback. This means that the callbacks are already coming in on the render thread so you can safely modify the scene graph. jME uses some specially written collections such as SafeArrayList to allow you to modify the scene graph at the same time as iterating over it.
Update, Render, Update, Render, etc. Events firing generally happens as part of the update process when an update is detected. To find the code start by looking in the Application class, you should be able to find the main game loop in there by tracing through from the start() call.
The jME3 Threading Tutorial covers a fair amount of this:
http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:multithreading

Java: How to interrupt a program partway through execution with a mouseclick

How would one go about implementing a mouselistener (or some other way, doesn't matter) that will handle a mouse click event at ANY part of the program? Preferably returning to the line it left off at when the click event handler method completes.
I am using swing. The 'context' is a GUI that constantly updates, but must respond to a mouse click from the user at any time with no delay. Indeed I do have experience with events, using and overwriting their handlers etc., not too in-depth I suppose but what i do know has been sufficient in anything until now.
I could not understand your first para, so my answer goes for your second para, if I understood that correctly. ;)
Swing follows single thread model. So, you should be updating the UI from Event Dispatch Thread (EDT). This thread is responsible for delivering the events to your code too, hence the name. If you are continuously updating an UI in a loop then that is going to keep the EDT busy and blocked. The end effect will be an UI which does not respond to user events. This because the events are getting queued and EDT can pick them and deliver them to your code when it becomes free.
Games typically encounter this kind of scenario. You might have noticed that games typically have one fixed rate of refresh which they call FPS (Frames Per Second). Typically maintaining 60 FPS is good enough. That is, you need to draw your UI 50 times per second, but right now it seems that your render loop (which updates the UI) is running continuously.
You need to have separate thread continuously running which is responsible for drawing the UI. This should draw into a buffer (Image). And then invoke repaint() on the UI element to be updated. That UI element's paintComponent() needs to overridden, so that it can copy the image in Image buffer and paint that on the graphics context.
Now comes the real trick. The loop which calls repaint() must do some arithmetic to make sure it does not go beyond drawing 60 times, i.e. looping 60 times, per second. If and when it does then it must call Thread.sleep(sleepTime), where sleepTime is the number of milliseconds left in a second after looping 60 times. It might happen sometime that your loop may take more than a second to complete 60 iterations, then don't just go ahead for next iteration, but call Thread.yield(). This will give other threads a chance to use the CPU, e.g. maybe your EDT. To make the matter more complicated, do not keep yielding always, so might want to put some logic to make sure that yield for only x consecutive times. This last scenario should be very rare, if at all. This scenario means the system is under heavy load.
Remember, repaint() is thread safe and allowed to be called from any thread. It schedules a paint() call on EDT. So, calling repaint() does not guarantee a paint. So, you may want to experiment with different values of FPS to find the one which suites you.
By the way, the trick of rendering to an in-memory Image is technically called Double buffer. This gives us the ability to render nice smooth animations.
Further reading:-
LANSim - Wrote this code a long time back. You can use this code as an example.
http://java.sun.com/docs/books/performance/1st_edition/html/JPSwingThreads.fm.html
Killer Game Programming in Java - This book is on this subject.
Have you looked at SwingWorker? It's a simple framework that lets you run computations in the background and periodically publish updates to the GUI thread.

Is there a more ideal way to send draw commands?

I'm still working on moving my android app over to OpenGL and i'm once again having an issue.
I have gotten to the point where everything FUNCTIONS, the only problem is that the animation seems a little juddery.
I'm monitoring the frame rates and seeing that the drawing thread isn't slowing down, but every once in a while, the main loop slows a bit and I suspect I'm having a problem I was worried about.
The way the app works is that as new objects (say for instance, enemies) are created, 2 objects are actually created. The first one is created and mapped in the main thread, and then in it's constructor, it creates a mesh object which is then added to a group to be drawn continuously by the renderer.
Every time an attribute for the object is changed, (such as its coordinates) The object relays the necessary command to its mesh counterpart (in this example to translate the mesh.)
It was suggested that this was thread safe communication, but i'm having my doubts. I also notice a greater amount of frame skip when new objects are created, I can fix this somewhat by reusing the same mesh object for identical Game objects, but I don't believe this will fix everything by itself.
Can you think of a way to make this more efficient and thread-safe?
Another possible solution: The game logic does not HAVE to go at full speed (realtime) I have it actually set up so that no updates are made untill 33 millis pass. So obviously, I should have plenty of time between frames to draw, can I set it up so that draw is only called on command in the thread (after the game logic has been updated)?
It looks like you need something like a ScheduledThreadPoolExecutor.
With one of these you could put your renderer on a schedule so it executes at 30fps leaving your main/control thread to do whatever it needs to do to the object map between frames.
I don't think you need any wait/notify interlocking as all you really need is to block access to the map while the renderer is walking it. To do this you just need to make the map synchronized. As this will only happen once every 1/30th of a second you are certainly not introducing a significant overhead.
Your main aim should be to put as little unnecessary load on the CPU as possible, this is the key to smooth multithread work. Try to spend as much time as possible either sleeping or blocked.
Added
I subtract the time it took to loop from 33ms, and use the result to specify the length of sleep().
I wonder if that may be part of your issue. On a Windows machine you often get a 15ms resolution on the currentTimeMillis so your sleeps may end up hardly sleeping at all. It may be worth experimenting with a ScheduledThreadPoolExecutor just to see if it improves your timing. ... oops ... this is Android isn't it. Still ... it may be worth a try.

Lag in Java Game

I'm writing a game in Java. And, oh wonder, i have performance issues. I benchmarked the paint itself - 1000 cycles in 3 ms tops. The game logic is even below that. So far, so good. But i still encounter an annoying lag: When scrolling, when zooming, when clicking. The problems get worse when i zoom in and more objects are placed. But still - even when I loop the painting a 1000 times the lags stays more or less the same, so that cant be it.
I tried putting the loop in a task - still the same. I tried pausing the task in between paints - still the same.
Animations run as smooth as silk (since the framerate is stable and high, that makes sense). So how on earth do i organize the inputs in an orderly fashion? Put them all in a seperate thread?
Any input would and will be greatly appreciated!
It sounds like you're using listener callbacks directly on the Swing Event Dispatch Thread, where the UI updates are being done. You should use a command queue, and put events on the queue when a callback is invoked, with the nature of the command, then you use this in the main game update loop that has nothing to do with the EDT.

Categories

Resources