Skip to content

Commit 9f58340

Browse files
committed
Optimize and reduce memory footprint for ImFontAtlas#getTexDataAsAlpha8/ImFontAtlas#getTexDataAsRGBA32 methods
1 parent 11edf0c commit 9f58340

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

imgui-binding/src/main/java/imgui/ImFontAtlas.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
public final class ImFontAtlas implements ImGuiDestroyableStruct {
2828
final long ptr;
2929

30+
private ByteBuffer alpha8pixels = null;
31+
private ByteBuffer rgba32pixels = null;
32+
3033
/**
3134
* This class will create a native structure.
3235
* Call {@link #destroy()} method to manually free used memory.
@@ -52,11 +55,17 @@ public void destroy() {
5255
jfieldID imFontAtlasPtrID;
5356
5457
#define IM_FONT_ATLAS ((ImFontAtlas*)env->GetLongField(object, imFontAtlasPtrID))
58+
59+
jmethodID jImFontAtlasCreateAlpha8PixelsMID;
60+
jmethodID jImFontAtlasCreateRgba32PixelsMID;
5561
*/
5662

5763
static native void nInit(); /*
5864
jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas");
5965
imFontAtlasPtrID = env->GetFieldID(jImFontAtlasClass, "ptr", "J");
66+
67+
jImFontAtlasCreateAlpha8PixelsMID = env->GetMethodID(jImFontAtlasClass, "createAlpha8Pixels", "(I)Ljava/nio/ByteBuffer;");
68+
jImFontAtlasCreateRgba32PixelsMID = env->GetMethodID(jImFontAtlasClass, "createRgba32Pixels", "(I)Ljava/nio/ByteBuffer;");
6069
*/
6170

6271
private native long nCreate(); /*
@@ -313,20 +322,30 @@ public ByteBuffer getTexDataAsAlpha8(final ImInt outWidth, final ImInt outHeight
313322
* 1 byte-per-pixel
314323
*/
315324
public ByteBuffer getTexDataAsAlpha8(final ImInt outWidth, final ImInt outHeight, final ImInt outBytesPerPixel) {
316-
final byte[] tmpBuff = getTexDataAsAlpha8(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData());
317-
final ByteBuffer buffer = ByteBuffer.allocateDirect(tmpBuff.length).order(ByteOrder.nativeOrder());
318-
buffer.put(tmpBuff);
319-
buffer.flip();
320-
return buffer;
325+
getTexDataAsAlpha8(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData());
326+
return alpha8pixels;
327+
}
328+
329+
private ByteBuffer createAlpha8Pixels(final int size) {
330+
if (alpha8pixels == null || alpha8pixels.limit() != size) {
331+
alpha8pixels = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
332+
} else {
333+
alpha8pixels.clear();
334+
}
335+
336+
return alpha8pixels;
321337
}
322338

323-
private native byte[] getTexDataAsAlpha8(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /*
339+
private native void getTexDataAsAlpha8(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /*
324340
unsigned char* pixels;
325341
IM_FONT_ATLAS->GetTexDataAsAlpha8(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]);
342+
326343
int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0];
327-
jbyteArray jBytes = env->NewByteArray(size);
328-
env->SetByteArrayRegion(jBytes, 0, size, (jbyte*)pixels);
329-
return jBytes;
344+
345+
jobject jBuffer = env->CallObjectMethod(object, jImFontAtlasCreateAlpha8PixelsMID, size);
346+
char* buffer = (char*)env->GetDirectBufferAddress(jBuffer);
347+
348+
memcpy(buffer, pixels, size);
330349
*/
331350

332351
/**
@@ -340,20 +359,30 @@ public ByteBuffer getTexDataAsRGBA32(final ImInt outWidth, final ImInt outHeight
340359
* 4 bytes-per-pixel
341360
*/
342361
public ByteBuffer getTexDataAsRGBA32(final ImInt outWidth, final ImInt outHeight, final ImInt outBytesPerPixel) {
343-
final byte[] tmpBuff = nGetTexDataAsRGBA32(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData());
344-
final ByteBuffer buffer = ByteBuffer.allocateDirect(tmpBuff.length).order(ByteOrder.nativeOrder());
345-
buffer.put(tmpBuff);
346-
buffer.flip();
347-
return buffer;
362+
nGetTexDataAsRGBA32(outWidth.getData(), outHeight.getData(), outBytesPerPixel.getData());
363+
return rgba32pixels;
364+
}
365+
366+
private ByteBuffer createRgba32Pixels(final int size) {
367+
if (rgba32pixels == null || rgba32pixels.limit() != size) {
368+
rgba32pixels = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
369+
} else {
370+
rgba32pixels.clear();
371+
}
372+
373+
return rgba32pixels;
348374
}
349375

350-
private native byte[] nGetTexDataAsRGBA32(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /*
376+
private native void nGetTexDataAsRGBA32(int[] outWidth, int[] outHeight, int[] outBytesPerPixel); /*
351377
unsigned char* pixels;
352378
IM_FONT_ATLAS->GetTexDataAsRGBA32(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]);
379+
353380
int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0];
354-
jbyteArray jBytes = env->NewByteArray(size);
355-
env->SetByteArrayRegion(jBytes, 0, size, (jbyte*)pixels);
356-
return jBytes;
381+
382+
jobject jBuffer = env->CallObjectMethod(object, jImFontAtlasCreateRgba32PixelsMID, size);
383+
char* buffer = (char*)env->GetDirectBufferAddress(jBuffer);
384+
385+
memcpy(buffer, pixels, size);
357386
*/
358387

359388
public native boolean isBuilt(); /*

imgui-lwjgl3/src/main/java/imgui/gl3/ImGuiImplGl3.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,18 @@ public void dispose() {
201201
*/
202202
public void updateFontsTexture() {
203203
glDeleteTextures(gFontTexture);
204-
gFontTexture = glGenTextures();
205204

206205
final ImFontAtlas fontAtlas = ImGui.getIO().getFonts();
207206
final ImInt width = new ImInt();
208207
final ImInt height = new ImInt();
209208
final ByteBuffer buffer = fontAtlas.getTexDataAsRGBA32(width, height);
210209

210+
gFontTexture = glGenTextures();
211+
211212
glBindTexture(GL_TEXTURE_2D, gFontTexture);
212213
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
213214
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
214215

215-
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
216216
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width.get(), height.get(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
217217

218218
fontAtlas.setTexID(gFontTexture);

0 commit comments

Comments
 (0)