Hi,
currently I am working on a Unity’s client that is receiving camera frames by network using sockets. I checked LoadImage at the beginning and I wasn’t satisfied with the results, as there are spikes every time client decodes frame and I cannot use LoadImage from other thread.
I’ve read that creating textures in native code should be more efficient but my native Android skills are not that high and I ended up with a solution that is twice slower than LoadImage. I tried also LoadRawTextureData to move texture loading to another thread, but it was total fail. I could not find proper TextureFormat I guess, everytime I had an error “not enough data provided”.
I am using opencv to prepare frames encoded in jpg format. Here is my Java code for creating native textures, I noticed that decodeByteArray method is responsible for low performance:
public class NativeImage {
private static Bitmap gBitmap;
private static BitmapFactory.Options gOpts = new BitmapFactory.Options();
public static int NativeImageTexturePtr(byte[] data) {
gOpts.inSampleSize = 1;
int textures[] = new int[1];
GLES20.glGenTextures(1, textures, 0);
if(textures[0] != 0) {
if(gBitmap != null) {
gBitmap.recycle();
}
gBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
gOpts.inBitmap = gBitmap;
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, gBitmap, 0);
}
return textures[0];
}
}
Could someone advice me how to speed up native texture creation? Does Unity use external libraries that gets better results than my method? I could live with same performance as LoadImage, but I would like to load textures asynchronous to avoid spikes that make my client a bit laggy.