Optimize and reduce memory footprint for ImFontAtlas#getTexDataAsAlpha8/ImFontAtlas#getTexDataAsRGBA32 methods

This commit is contained in:
SpaiR committed 2020-06-21 17:42:37 +03:00
1 parent 11edf0cad4
commit 9f58340d6e
2 files changed
+49 -20

No files matched your search

@@ -27,6 +27,9 @@ import java.nio.ByteOrder;
public final class ImFontAtlas implements ImGuiDestroyableStruct { public final class ImFontAtlas implements ImGuiDestroyableStruct {
final long ptr; final long ptr;
private ByteBuffer alpha8pixels = null;
private ByteBuffer rgba32pixels = null;
/** /**
* This class will create a native structure. * This class will create a native structure.
* Call {@link #destroy()} method to manually free used memory. * Call {@link #destroy()} method to manually free used memory.
@@ -52,11 +55,17 @@ public final class ImFontAtlas implements ImGuiDestroyableStruct {
jfieldID imFontAtlasPtrID; jfieldID imFontAtlasPtrID;
#define IM_FONT_ATLAS ((ImFontAtlas*)env->GetLongField(object, imFontAtlasPtrID)) #define IM_FONT_ATLAS ((ImFontAtlas*)env->GetLongField(object, imFontAtlasPtrID))
jmethodID jImFontAtlasCreateAlpha8PixelsMID;
jmethodID jImFontAtlasCreateRgba32PixelsMID;
*/ */
static native void nInit(); /* static native void nInit(); /*
jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas"); jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas");
imFontAtlasPtrID = env->GetFieldID(jImFontAtlasClass, "ptr", "J"); imFontAtlasPtrID = env->GetFieldID(jImFontAtlasClass, "ptr", "J");
jImFontAtlasCreateAlpha8PixelsMID = env->GetMethodID(jImFontAtlasClass, "createAlpha8Pixels", "(I)Ljava/nio/ByteBuffer;");
jImFontAtlasCreateRgba32PixelsMID = env->GetMethodID(jImFontAtlasClass, "createRgba32Pixels", "(I)Ljava/nio/ByteBuffer;");
*/ */
private native long nCreate(); /* private native long nCreate(); /*
@@ -313,20 +322,30 @@ public final class ImFontAtlas implements ImGuiDestroyableStruct {
* 1 byte-per-pixel * 1 byte-per-pixel
*/ */
public ByteBuffer getTexDataAsAlpha8(final ImInt outWidth, final ImInt outHeight, final ImInt outBytesPerPixel) { public ByteBuffer getTexDataAsAlpha8(final ImInt outWidth, final ImInt outHeight, final ImInt outBytesPerPixel) {
final byte[] tmpBuff = getTexDataAsAlpha8(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData()); getTexDataAsAlpha8(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData());
final ByteBuffer buffer = ByteBuffer.allocateDirect(tmpBuff.length).order(ByteOrder.nativeOrder()); return alpha8pixels;
buffer.put(tmpBuff);
buffer.flip();
return buffer;
} }
private native byte[] getTexDataAsAlpha8(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /* private ByteBuffer createAlpha8Pixels(final int size) {
if (alpha8pixels == null || alpha8pixels.limit() != size) {
alpha8pixels = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
} else {
alpha8pixels.clear();
}
return alpha8pixels;
}
private native void getTexDataAsAlpha8(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /*
unsigned char* pixels; unsigned char* pixels;
IM_FONT_ATLAS->GetTexDataAsAlpha8(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]); IM_FONT_ATLAS->GetTexDataAsAlpha8(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]);
int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0]; int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0];
jbyteArray jBytes = env->NewByteArray(size);
env->SetByteArrayRegion(jBytes, 0, size, (jbyte*)pixels); jobject jBuffer = env->CallObjectMethod(object, jImFontAtlasCreateAlpha8PixelsMID, size);
return jBytes; char* buffer = (char*)env->GetDirectBufferAddress(jBuffer);
memcpy(buffer, pixels, size);
*/ */
/** /**
@@ -340,20 +359,30 @@ public final class ImFontAtlas implements ImGuiDestroyableStruct {
* 4 bytes-per-pixel * 4 bytes-per-pixel
*/ */
public ByteBuffer getTexDataAsRGBA32(final ImInt outWidth, final ImInt outHeight, final ImInt outBytesPerPixel) { public ByteBuffer getTexDataAsRGBA32(final ImInt outWidth, final ImInt outHeight, final ImInt outBytesPerPixel) {
final byte[] tmpBuff = nGetTexDataAsRGBA32(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData()); nGetTexDataAsRGBA32(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData());
final ByteBuffer buffer = ByteBuffer.allocateDirect(tmpBuff.length).order(ByteOrder.nativeOrder()); return rgba32pixels;
buffer.put(tmpBuff);
buffer.flip();
return buffer;
} }
private native byte[] nGetTexDataAsRGBA32(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /* private ByteBuffer createRgba32Pixels(final int size) {
if (rgba32pixels == null || rgba32pixels.limit() != size) {
rgba32pixels = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
} else {
rgba32pixels.clear();
}
return rgba32pixels;
}
private native void nGetTexDataAsRGBA32(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /*
unsigned char* pixels; unsigned char* pixels;
IM_FONT_ATLAS->GetTexDataAsRGBA32(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]); IM_FONT_ATLAS->GetTexDataAsRGBA32(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]);
int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0]; int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0];
jbyteArray jBytes = env->NewByteArray(size);
env->SetByteArrayRegion(jBytes, 0, size, (jbyte*)pixels); jobject jBuffer = env->CallObjectMethod(object, jImFontAtlasCreateRgba32PixelsMID, size);
return jBytes; char* buffer = (char*)env->GetDirectBufferAddress(jBuffer);
memcpy(buffer, pixels, size);
*/ */
public native boolean isBuilt(); /* public native boolean isBuilt(); /*
@@ -201,18 +201,18 @@ public final class ImGuiImplGl3 {
*/ */
public void updateFontsTexture() { public void updateFontsTexture() {
glDeleteTextures(gFontTexture); glDeleteTextures(gFontTexture);
gFontTexture = glGenTextures();
final ImFontAtlas fontAtlas = ImGui.getIO().getFonts(); final ImFontAtlas fontAtlas = ImGui.getIO().getFonts();
final ImInt width = new ImInt(); final ImInt width = new ImInt();
final ImInt height = new ImInt(); final ImInt height = new ImInt();
final ByteBuffer buffer = fontAtlas.getTexDataAsRGBA32(width, height); final ByteBuffer buffer = fontAtlas.getTexDataAsRGBA32(width, height);
gFontTexture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, gFontTexture); glBindTexture(GL_TEXTURE_2D, gFontTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width.get(), height.get(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width.get(), height.get(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
fontAtlas.setTexID(gFontTexture); fontAtlas.setTexID(gFontTexture);