Author Topic: Update texture at run-time without calling GC?  (Read 1437 times)

Offline Timbertoes

  • byte
  • *
  • Posts: 3
    • View Profile
Update texture at run-time without calling GC?
« on: May 18, 2011, 12:28:27 am »
Greetings!

Here's what I'm trying to accomplish:
  I accept user input in the form of a String and would like to update a Texture during run-time to display the string whenever it's modified.

I've tried the ITextureEffect approach as well as just unloading the texture and replacing it with the new one. My current problem is that both methods seem to call the android garbage collector.

Here's my current implementation using the replaceTexture() function:
Code: [Select]
Bitmap texture_bmp = Bitmap.createBitmap(imageWidth, imageHeight, DEFAULT_BITMAP_CONFIG);

// ... Code to draw text onto texture_bmp ...

TextureManager.getInstance().unloadTexture(frameBuffer, old_texture);

old_texture = new Texture(texture_bmp, true);

TextureManager.getInstance().replaceTexture(texture_name, texture);

texture_bmp.recycle();
texture = null;

This makes sense why the GC is being called since I'm allocating a new Texture object. If I comment out the lines that allocate the new Texture, the GC is not called.

Because I want to avoid the GC, I thought I'd try and figure out how to use the ITextureEffect. I read about ways to implement it here, and here and it seemed easy enough.

However, once I had it implemented, the GC still seemed to be called each frame as long as the code that called setEffect and applyEffect were there. I did read that the ITextureEffect says: "The affected texture has to be converted and uploaded to the GPU every time the effect will be applied." My question: During this 'conversion' is a new texture being allocated and possibly triggering the GC?

I checked my memory usage and I'm only using about half of my 13MB heap while this GC is happening. The logcat output says 'GC_FOR_MALLOC' which I believe happens when a new allocation is made.

I'm aware of how delicate most operations must be, especially after reading the article on Performance tips for Android.

Allocating the new Bitmap does not seem to trigger the GC at all if I comment out the 'new Texture()' or ITextureEffect code. And as for my ITextureEffect code, it is just a blank class for now. If you need any additional code, let me know and I'll post what I can.

If anyone that is more familiar with jPCT-AE can help me out here, I would greatly appreciate it. There's always the chance that I am approaching the problem the wrong way and might need a new strategy.

Thanks!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11770
    • View Profile
    • http://www.jpct.net
Re: Update texture at run-time without calling GC?
« Reply #1 on: May 18, 2011, 10:40:40 am »
Uploading a texture creates garbage. Not as much as creating a new texture does, but it does create some. Diabling mip-mapping on the texture (if in use) might help a little, but don't expect too much.

For printing out text, it might be a better idea to simply blit the chars one by one instead of fiddling around with textures.

Offline Timbertoes

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Update texture at run-time without calling GC?
« Reply #2 on: May 18, 2011, 11:45:50 am »
I don't currently have any mip-mapping on but I'll remember that for later.

I thought about just blitting the chars one at a time but since my program will be localized into some other languages with large alphabets, it's a little more in-depth than I'd like to do for now. I suppose I could draw a sprite sheet for each language during bootstrap but it may be best to just deal with the GC. :)

For the mean time, I will just reduce the content onscreen during the texture update so the GC may not be as visible.

Thanks for the tips EgonOlsen. I'm having tons of fun with jPCT-AE.