[quote]Premature optimization is the root of all evil.
[/quote]
Aha, you’re so right!
I did some similar approach by using arrays and it worked, but I prefer the command way. The classes are anonymous and short lived. From the performance side, there are no visible differences from the direct method invocation.
Anyway I didn’t use a producer/consumer pattern.
I created a Master class that creates a thread that waits for commands. The commands are created inside the Master itself through anonymous inner classes. The calls are synchronized through a lock, so I can wait for a reply from the call (useful for pbuffers/textures allocation inside the thread).
some snippets:
the SGLBitmap allocation
public SGLBitmap newSGLBitmap(final int width, final int height, final int samples)
{
return (SGLBitmap) invokeCommand(new SGLCommand()
{
public Object execute()
{
return new SGLBitmap(width, height, samples);
}
});
}
The drawImage
public void drawImage(final SGLContext ctx, final SGLImage img, final float x, final float y)
{
invokeCommand(new SGLCommand()
{
public Object execute()
{
ctx.drawImage(img, x, y);
return null;
}
});
}
the invokeCommand
// call a command in the thread
private Object invokeCommand(SGLCommand cmd)
{
synchronized (lock)
{
sendCommand(cmd);
return waitReply();
}
}
the server
// the server code
public void run()
{
synchronized (lock)
{
while (true)
{
sendReply(waitCommand().execute());
}
}
}
I would like to avoid to run in a synchronized block, but I didn’t find a reliable solution.