I want to be able to draw consecutive bitmaps (of type BufferedImage.TYPE_INT_RGB) of a video as quickly as possible in java. I want to know the best method in doing so. Does anyone have any advice where I should start? From what I've read, 2 options are:
1) Use GDI/GDI+ routines in a JNI dll working with JAWT (Im on Windows)
2) Use Java3D and apply Textures to a Box's face and rotate it to the camera
Im interesting in any advice on these topics as well as any others.
I have done a decent amount of GDI/GDI+ programming in VB when i created an ActiveX control, so using GDI should be painless, but im guessing Java3D will utilize the GPU more (I could be wrong) and give better performance. What do you think? GDI and JAWT with my previous experience, or start and new API journey with Java3D.
Thanks in advance. :)
To obtain a fluid animation (if it what you want to get), you need to use double buffering. For doing this, you will need to create a new java.awt.Image (or a subclass like BufferedImage, or if you want OpenGL accelerated processing, VolatileImage) for each frame you want to display. If you haven't already done so, call Image.getGraphics() to get a java.awt.Graphics object (can also be useful to add your content to the Image). At the end, when you hidden Image is complete, call Graphics.draw() to replace the current display smoothly.
VolatileImage is OpenGL accelerated and much faster. When VolatileImage.getGraphics() is called, it actually returns a Graphics2D, which is also part of the accelerated graphic pipeline.
It works on Windows, Linux and Solaris, but you need to have OpenGL drivers installed for your graphic card.
Some additional refs:
Accelerated graphic pipeline:
http://download.oracle.com/javase/1.5.0/docs/guide/2d/new_features.html
http://www.javalobby.org/forums/thread.jspa?threadID=16840&tstart=0
Double buffering:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/Smoothmoveusingdoublebuffer.htm
http://www.heatonresearch.com/articles/23/page2.html
http://www.javacooperation.gmxhome.de/BildschirmflackernEng.html
Related
I'm developing an app that will connect to (my own) oscilloscope, read data via USB and display plots - so just normal oscilloscope.
USB is ok, now I have to draw the graphs.
As you know (or not) in oscilloscope there are really fast-changing signals, so I have to render graphs really fast.
So I'm looking for a fast way to do it.
I've read that GLSurfaceView uses OpenGL and GPU so it is used for games, but will I benefit from it in my project?
GL is really complicated (to draw a square or just lines, not to mention about text...) while SurfaceView has its Canvas so it's much easier.
I really care for performance, but have not much time and nerver used OpenGL so what do you think?
It is complicated, but you will benefit from GLES when using a SurfaceView.
Canvas on SurfaceView is not hardware-accelerated, so on some devices you'll have trouble hitting 60fps. (See for example the "multi-surface test" in Grafika. Tap the "bounce" button to cause it to animate. On some devices it'll do 60fps, on others it won't hit 30fps.)
One alternative would be to create a custom View. That way the rendering would be accelerated by hardware.
I'm currently working on a project where I need to plot the predicted footprint of a satellite on a mercator-projected world map with possible scaling/cropping/etc. done to the map.
I thought Cairo would be a good library to use for this purpose. There are Java-bindings available for it. However, I just can't find a way to make it render onto a SWING GUI (e. g. onto the surface of a JPanel). I thought about rendering into a byte buffer and plotting it out pixel by pixel using Java2D, but I can't find any API call to make Cairo render into a buffer (which is weird, as this is one of the most fundamental functionalities I'd expect to get supported by such a library).
Any way I can achieve this? I know there is Java2D, but it is fairly basic. I'd really appreciate a more powerful, widespread, well-tested, high-quality, free (LGPL) graphics library for this purpose. Cairo would be such a perfect fit, if I could get it to work with SWING somehow.
Thank you very much for your proposals.
One of the fundamentals in Cairo is that any non-abstract image context is bound to one of the supported back ends.
I've never tried the Java bindings, but it is likely they are a thin layer, not providing a a new surface type - you should use the "Image Surface" type.
On the C documentation for the Library, there is a "cairo_image_surface_get_data()" call
(here: http://cairographics.org/manual/cairo-Image-Surfaces.html ) which gives one acess to the buffer.
Maybe the bindings didn't expose this to Java due to the low level memory access required to actually use its contents. If that is true, then I propose you the following work-around:
(1)Render your results to a Cairo Image Surface, (2) Write a temporary file with the surface contents,(3) Read and display the temporary file with the Java 2D API.
Here is a example.
I found this examples on http://java-gnome.sourceforge.net
It create a gtk window, and actually a gtk DrawingArea widget, the onDraw() event uses cairo.
I compiled and run it on linux, it works good.
however java-gnome seems only have linux binary. Maybe somebody could make a windows binary, but need some work.
It is a gtk window, so have nothing to do with swing.
Maybe you don't need swing if gtk(java-gnome) can fit your needs.
If you must use swing, you can use cairo to render to a image in memory, then show it to JComponent by somthing like overriding paintComponent() method. I don't know the performance.
I'm looking for some method to draw half transparent figures within the draw2d framework.
Currently I'm using setAlpha() of org.eclipse.draw2d.Graphics. The problem is that it slows down the whole UI, if I draw more than one half transparent figure.
Here was another question regarding the perfomance of it [1]. It targets SWT on Linux, I target both Linux and Windows. The windows (Win7) system has a 3D capable graphic card and a i7 q720. The Linux machine is even better equipped and has also 3D, so I think it's not a performance issue of the platform.
My question is: Is there a performant way to achieve alpha blending in draw2d?
I've tried to use OpenGL respectivly LWJGL. Since draw2d and OpenGL can't be mixed up and I must rely on draw2d, I thought I could do something like this: draw2d -> OpenGL -> draw2d
here where canvas is the Canvas where the OpenGL drawing happens and c1 is the Canvas where I want to get the drawn OpenGL picture. But my attempt to copy the OpenGL drawings doesn't work it gets me just the underlying Canvas (canvas) background. Besides this I'm not sure if this would improve the "perormance" anyway...
Thanks,
atx
This issue is known but unsolved. On the mailing list of draw2d there is a thread regarding this issue: mailing list
TL;DR: A possible solution was dropped because of license issues between LWJGL and Eclipse.
Make sure all your src/dest images are of the same depth (eg. ARGB)
Otherwise, expensive conversions happen during copy (src->dest)
GCs don't allow access to their underlying buffers, afaik.
MemoryImageSource might be a way to go.
You can grab your OpenGL drawing images using something like:
BufferedImage capture = new Robot().createScreenCapture(new Rectangle( canvas.getX(), canvas.getY(), canvas.getWidth(), canvas.getHeight() ) );
Kind of fiddly, but very fast.
I use it to save jpegs of my live animation frames to a folder.
VirtualDub then creates a movie from the frames. Have to play with the frame rate ;)
Great for producing youtube teasers!
You can see one here!
I'm writing a game in Java, LJGWL (OpenGL). I'm using a library that handles a lot of messy details for me, but need to find a lot faster way to do this.
Basically I want to set every pixel on the screen to say a random color as fast a possible. The "random colors" is just an Array [][] that gets updated every 2-3 seconds. I've tried drawing rects and using images, both are pretty slow for what I want to do.
I think I want to learn how to write a GPU shader? That is the fastest way to do this? LJGWL exposes OpenGL api to java. Any basic tutorials on how to get started with OpenGL shaders? Or should I dynamically create a texture of some sort and then just throw up the entire texture, would that be faster?
If it were the case that you were statically displaying the same image, than using a texture or display list would suffice. But as you want to frequently update it, shaders really are the best option. Shader code executes on the GPU and modifies data in GRAM, so you have no bottle neck transferring from CPU to GPU. The next best thing would probably be a Pixel or Frame Buffer Object. Buffer Objects let you read/write to GRAM via DMA (without having to go through the CPU) so they can be pretty fast.
I haven't written any shaders yet, so I can't recommend any good resources. But SongHo's OpenGL pages are a good place to learn about Buffer Objects. (His examples are in C++ though)
Textures are the fastest way to draw something on screen, draw a texture mapped quad into the screen, it should be fast enough. When you need to reupload the texture data, use glTexSubimage2D to update it.
No need to use shaders.
I've yet to do any work with shaders in OpenGL, but given the same scenario in multiple occasions, I handled it with a texture I threw up across the screen on top, and it worked quite effectively.
I don't know how you are drawing your pixels exactly, but this limit you hit could be because of the amount of data you transfer (inefficiently?). Updating a screen full of pixels every 2-3 seconds shouldn't be hard at all. Although shaders bring you closer to the graphics card, they will never make inefficient methods fast, so...
Why is your code so slow?
What code? What code exactly did you try? What texture did you use, render to, ...?
Is it slow? How slow? How fast do you expect it to be?
How quickly can one get 1920x1080(?) pixels in video ram, what's your hardware, drivers, OS?
I think you need to edit/repost before we can help you solve your problem. Just because it is slow, is no guarantee at all that shaders will even be one bit faster.
I was programming a simple game and wanted to display it via the Java Graphics + Swing API. It felt a little slow, of course, so I measured how long it took to repaint, which was around 32ms. Then I read about accelerated Java graphics and used the method described here: Space Invaders
However, somehow this is even slower. It now takes around 98ms to redraw. Why is that?
Note that I am aware of libraries like LWGL and JOGL, but I don't want to use a full-blown OpenGL wrapper for such a graphically simple game.
A few hints to improve performance:
Clip and just draw the area that has changed
Don't scale anything when drawing
Use the correct buffer strategy
Use full screen exclusive mode if appropriate.
Swing isn't inherently slow. Most of the confusion about Swing comes from people who don't know or understand about the Event Dispatch Thread (and yes, it's a lot of effort to get it right).
Regarding your times, are you just calling g.drawImage between the time settings? If so, what size are you repainting, and are you doing any scaling on this call?
EDIT: Forgot to mention Pulp Core - a very nice gaming framework for Java.
Read this about timers
Use an always sleeping thread in the background to lower the system interrupt rate to get much more precise timers!
You can take a look at my code in here
A small game I've created can be found here
long renderStart = System.nanoTime()
// render stuff here
long renderTime = (System.nanoTime() - renderStart) / 1000000;
Swing will be slow for a game. Your best bet is probably an SDL wrapper, such as jsdl or sdljava. SDL is quite lightweight and supported on many platforms.
The nice thing is, well-implemented 2d graphics library wrappers implement the Graphics interface, so you should be able to try several and see which one performs best for your app without modifying your code.
I am not an expert, but Java2D supports different rendering pipelines. On my machine, switching to the OpenGL one by setting the system property
sun.java2d.opengl=true
yielded a substantial increase in rendering speed.
You may look at processing.
I agree with Nicolas, for graphics / interactivity processing is great
Try JGame. It's a simple engine for 2D games which uses GL when its available and can produce games that run on anything from a mobile phone to a desktop system.
First of all, you should check and see how much time is spent in your own code vs. how much time is spent actually drawing the frame. You may have some bug or glitch that makes it run more slowly.
You should be able to get much better performance then 98ms to draw your screen. Those game libraries will probably be using the same graphics calls as you would. But which containers you use can have a big impact on how fast things draw. I remember a couple months ago working on some double buffered graphics code, and how the back buffer was created made a huge difference in performance.
In particular, make sure you only create background image once or at least, only when your canvas resizes. In my draw code I do this:
//create a graphics backplane
if(backplane == null || !backplaneSize.equals(this.getSize())){
backplane = this.createImage((int)this.getSize().getWidth(), (int)this.getSize().getHeight());
backplaneSize = this.getSize();
}
So the back plane only gets created if it's new, or if the component is resized. This has a huge impact on speed.
I've been doing graphics programming in java since JDK 1.0. Actually I'd never heard of this BufferStrategy thing. Heh.
I've never had any trouble getting good frame rates with basic java graphics calls. Another thing to look out for is making sure that Swing isn't trying to clear the background of your component for you. That can be another source of slowdown.
Java2D performance is a bit of a dark art. It can vary between platforms, but it possible to get it to perform well.
Sometimes doing seemingly innocuous things can result in much slower performance. You want to make sure that you are using accelerated images as much as possible.
The type of the image can also be significant. I don't know the full details, but I do know that my programs were much faster using type 1 images (INT RGB) than other types such as type 5 (3BYTE BGR) because of conversion issues.
I can't tell exactly what you are doing. Your question is about Swing but you mention a Canvas and Bufferstrategy. Well in Swing you would use a JPanel which is automatically double buffered so you don't have to worry about that.
I tested a simple animation with 1000 moving balls. The Timer was set to fire every 100ms. When the Timer fired, the 1000 ball where randomly moved to a new position and a new BufferedImage was created and the panel displaying the image was repainted. The difference in time between the repainting was 110ms implying it only took 10ms to create an new BufferedImage and do the painting. The painting was done a a full screen so no clipping was done.
This posting show the example code I tested.