www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: raft on March 30, 2010, 04:02:41 pm

Title: blitting text and images
Post by: raft on March 30, 2010, 04:02:41 pm
here are Android versions of two small and (hopefully) handy classes for blitting text and images. more information can be found on original thread (http://www.jpct.net/forum2/index.php/topic,1074.0.html)

TexturePack (http://www.aptalkarga.com/download/android/TexturePack.java) packs several arbitrary sized images into a jPCT texture. it automatically layouts images and adjusts Texture size.

AGLFont (http://www.aptalkarga.com/download/android/AGLFont.java) creates GL renderable (blittable) fonts out of Android fonts.

Rectangle (http://www.aptalkarga.com/download/android/Rectangle.java) is used by AGLFont.

Bones Android demo app (http://www.aptalkarga.com/bones/bones_android_demo.zip) demonstrates how to use it

and an addition to GLFont (http://www.jpct.net/forum2/index.php/topic,3528.0.html) by nmare which allows multi-line and multi-color text blits

cheers ;D
r a f t

edit: updated the links and fixed getStringBounds(..) issue
edit2: added link to nmare's addition
Title: Re: blitting text and images
Post by: EgonOlsen on March 30, 2010, 08:18:12 pm
An apk would be cool in addition...
Title: Re: blitting text and images
Post by: raft on March 30, 2010, 08:26:22 pm
An apk would be cool in addition...

already there is an apk (http://www.aptalkarga.com/bones/Bones-Android-Ninja.apk) indeed. it's listed in Bones home page
Title: Re: blitting text and images
Post by: EgonOlsen on March 30, 2010, 08:41:27 pm
I see...works fine (30fps without/8fps with animation) and looks great on my phone. I would be very interested to know how this runs on the Nexus One... dl.zerocool to the rescue?
Title: Re: blitting text and images
Post by: raft on March 30, 2010, 08:53:24 pm
i've already asked him for a test ;) hoping he wont mind quoting from pm:

Quote
Here is some basic results. Apparently everything runs pretty fine.
while not animating fps are arround 55 to 59

When the animation starts you drop down to 15fps to 20fps~ depending on which animation you choose, then after a second it goes back to 30fps even "spin" animation who look like a little bit slower that the other (sometimes it fall back from 30 to 26 fps)

What is strange is that all animation get stuck to 30fps after a second, like there's something synchronizing behind.
I didn't had time to look at your code, so at moment I'm just running the apk demo.

slow down while animating is expected. but i cant explain it gain some speed again. it behaves like it has JIT but it hasnt AFAIK. maybe Android detects continuous data flow and make some optimization ???
Title: Re: blitting text and images
Post by: EgonOlsen on March 30, 2010, 09:01:49 pm
I think so. I've noticed that too on my applications just to a smaller amount. Dalvik might do some caching or similar. However, 30 fps is a pretty good result. If they enable this new JIT in addition, maybe 50-60 fps will be possible... ;D
Title: Re: blitting text and images
Post by: raft on March 30, 2010, 10:22:40 pm
capture of some speech bubbles on emulator.
texts are blitted with GLFont, bubble images are blitted with TexturePack:

(http://img641.imageshack.us/img641/4462/blittingandroid.png)

blitting really hurts performace at least on emulator, dont know why ???
Title: Re: blitting text and images
Post by: EgonOlsen on March 30, 2010, 10:42:00 pm
Which version of AE is this? The latest?
Title: Re: blitting text and images
Post by: raft on March 30, 2010, 11:05:38 pm
latest i suppose (1.21). i downloaded yesterday
Title: Re: blitting text and images
Post by: EgonOlsen on March 30, 2010, 11:24:09 pm
Yes, that should be the latest then. I was asking, because i already improved blitting performance. I assume that each character is a single, transparent and maybe scaled blit? Is there an apk to try this on actual hardware? The emulator isn't a fillrate wonder.
However, i can see one or two additional optimizations...i'll give them a try either tomorrow or next week.
Title: Re: blitting text and images
Post by: raft on March 30, 2010, 11:46:24 pm
indeed it was sunday i suppose, does it make a difference ?
right, each character is single, transparent but unscaled blit. each balloon image are two (border and inside) transparent and scaled blits.

here is an apk: http://www.aptalkarga.com/tmp/A1.apk
center key adds a random bubble. they disappear after some seconds
you should probably need to remove old S1 or A1 before installing this.

thanks :)

Title: Re: blitting text and images
Post by: EgonOlsen on March 31, 2010, 08:03:11 am
It starts with 8fps and slows down to almost 0 if i keep adding bubbles. Looks like as if you are not discarding the bubbles once they moved out of the screen. However, 8 fps isn't really fast either. I'll write myself a test case to see how much this can be improved...then again, i don't expect any miracles unless i'm doing something really stupid that i haven't noticed yet. But basically, blitting is like playing an animation, i.e. it's all about pumping new vertices to the GPU each frame. That part can be optimized a little bit by using indexed geometry (which might run 10-20% faster than now), but apart from that, i don't see much room for improvement. We'll see...
Title: Re: blitting text and images
Post by: EgonOlsen on March 31, 2010, 08:37:11 am
You might want to try to order your blits by texture if possible. The less texture changes there are, the better. However, i don't think that this will help very much, but maybe it's worth a try anyway.
Title: Re: blitting text and images
Post by: raft on March 31, 2010, 10:58:03 am
yes, out of screen condition is not checked. it wasnt necessary for desktop version. i can do it. in real life there aren't many bubbles on the fly at the same time, but it wont hurt but help for sure..

i've tried blitting all text at last. which is kind of sorting. did slightly help: ~5-10% increase

i definitely need a boost here. 10-20% even 30-40% range wont help much
Title: Re: blitting text and images
Post by: EgonOlsen on March 31, 2010, 12:23:01 pm
You might use a modified approach of your second bullet point, by filling some pre-generated textures with the bubble texts via an ITextureEffect. That will reduce at least some of the overhead of creating new textures.
Title: Re: blitting text and images
Post by: raft on March 31, 2010, 12:29:59 pm
maybe. the outer part is not fixed in size, it's scaled according to inner text part.

btw, seems as texure's arent needed to be added to TextureManager for blitting. they are uploaded at first use i suppose ? how can i dispose them ?
Title: Re: blitting text and images
Post by: EgonOlsen on March 31, 2010, 12:42:00 pm
In desktop jPCT, you can do this via the TextureManager. In AE, you can't. I guess, i somehow missed that part in the porting...i'll add it.
Title: Re: blitting text and images
Post by: raft on March 31, 2010, 12:48:33 pm
great :) i wasn't even aware of TextureManager.unload.. method. you must have added it while i wasnt on the scene i guess..

i would expect uploading a new texture and modifying it with a texture effect perform almost the same, right ?
Title: Re: blitting text and images
Post by: EgonOlsen on March 31, 2010, 12:58:02 pm
Not quite. Modifying will always be cheaper, because it removes some gl overhead that the creation of a new texture will cause. It might not be that much though.
Title: Re: blitting text and images
Post by: raft on March 31, 2010, 01:26:55 pm
Quote from: raft
instead of character blitting, i can render each bubble's text to a texture and blit it. this is what i do in desktop karga except it's java2d not blitting. this will dramatically reduce number of blits in cost of texture creation

yeap, this doubled the performance on emulator ;D
http://www.aptalkarga.com/tmp/B1.apk
Title: Re: blitting text and images
Post by: EgonOlsen on March 31, 2010, 02:05:54 pm
Up to 26 fps now on the phone. I'll try to improve blitting speed somewhat anyway.
Title: Re: blitting text and images
Post by: dl.zerocool on March 31, 2010, 03:33:53 pm
Nice to see you guys working on better solutions, as always, you can ask for tests (Since that's the only help I can provide atm.)
I'm working on the project so feel free to ask.

Title: Re: blitting text and images
Post by: raft on March 31, 2010, 04:25:13 pm
@dl.zerocool
feel free to try the app above and paste the results ;D
Title: Re: blitting text and images
Post by: paulscode on March 31, 2010, 05:05:52 pm
i would expect uploading a new texture and modifying it with a texture effect perform almost the same, right ?

Not quite. Modifying will always be cheaper, because it removes some gl overhead that the creation of a new texture will cause. It might not be that much though.

When I was working on my "video texture" project, I found that the overhead of adding new textures was significantly more than modifying existing textures (to the point that the first option was not really an option if you want smooth video playback).

-- I haven't tested if this is still true for JPCT-AE though.
Title: Re: blitting text and images
Post by: raft on March 31, 2010, 05:12:10 pm
Quote from: paulscode
When I was working on my "video texture" project, I found that the overhead of adding new textures was significantly more than modifying existing textures (to the point that the first option was not really an option if you want smooth video playback).

but i suppose you were doing this every frame ? in my case this is done once per bubble. indeed every time font size changes for each bubble but on Android font size wont change
Title: Re: blitting text and images
Post by: dl.zerocool on March 31, 2010, 06:03:23 pm
@dl.zerocool
feel free to try the app above and paste the results ;D

First since I don't know what your application is doing, I only see few bubbles with text.

Next, when loading it start at 40fps and then go to 60fps and stay at 60fps.
Some time after the bubbles start disappearing gently to let the black screen alone, always at 60fps of course.

And stop animation from menu does nothing.

Title: Re: blitting text and images
Post by: raft on March 31, 2010, 06:16:34 pm
Quote from: dl.zerocool
First since I don't know what your application is doing, I only see few bubbles with text.

ehm ;D it's an image and text blitting test application, and hence the bubbles. you can hit center key to add more bubbles. and as you noticed they disappear after some time.

Quote
Next, when loading it start at 40fps and then go to 60fps and stay at 60fps.

same thing with animation, dalvik somehow warms up, good for us :D

Quote
Some time after the bubbles start disappearing gently to let the black screen alone, always at 60fps of course.

60fps with and without bubbles, i suppose there is vsync or something which limits fps.

and nevermind the menu, "copy paste is the mother of all evil" ;D
Title: Re: blitting text and images
Post by: dl.zerocool on March 31, 2010, 06:39:55 pm

ehm ;D it's an image and text blitting test application, and hence the bubbles. you can hit center key to add more bubbles. and as you noticed they disappear after some time.
Sorry I don't understand what "blitting" means, I'm living in Switzerland and my mothers tongue is French, even I also speak Portuguese, English, German(school level) :) and learning Japanese.

Quote
and nevermind the menu, "copy paste is the mother of all evil" ;D
Ahah I do know this very well...
---
"Last week we had to "play" program few small trains with train station in real. (It was to prove our understanding about threads using the pthread lib).
And I spent 45minutes why my TrainA was not doing is stops and the TrainB suddenly stop itself when it was not needed.
Each train has is own thread and I did a copy paste at start to not having to rewrite the all thing... Well it was a bad idea since i copied TrainStop(B); everywhere... on the A thread."
---
Now about A1.
If I add bubbles the fps drop under 60 fps
with 14 bubbles ~43fps
with 18 bubbles ~38fps
and as soon as they start disappearing it goes back to 60fps.

anther thing is that when you leave the application (home for exemple) then go back in, there's no more bubbles, only text appear. (same fps as with bubbles)

moving arround with the pad don't make any changes on fps. Or so few that's not visible.
Title: Re: blitting text and images
Post by: raft on March 31, 2010, 06:59:25 pm
Quote from: dl.zerocool
Sorry I don't understand what "blitting" means..
blitting is something like pasting 2d image or text on top of rendered scene

Quote
And I spent 45minutes why my TrainA was not doing is stops and the TrainB suddenly stop itself when it was not needed...
hehe, yeap such things happens all the time..

Quote
Now about A1.
thanks for the tests. did you download A1.apk B1.apk ?

Quote
anther thing is that when you leave the application (home for exemple) then go back in, there's no more bubbles, only text appear. (same fps as with bubbles)
i suppose that happens because some textures are lost when GL surface is recreated. that should be handled in a regular application
Title: Re: blitting text and images
Post by: dl.zerocool on March 31, 2010, 07:18:09 pm
Quote
Now about A1.
thanks for the tests. did you download A1.apk B1.apk ?
Thanks for the explanation.
I downloaded B1.apk, but the software name is A1, sorry for the confusion.


Title: Re: blitting text and images
Post by: paulscode on March 31, 2010, 07:51:56 pm
Quote from: paulscode
the overhead of adding new textures was significantly more than modifying existing textures (to the point that the first option was not really an option if you want smooth video playback).

but i suppose you were doing this every frame ? in my case this is done once per bubble. indeed every time font size changes for each bubble but on Android font size wont change

Even still, the texture effect is a better option, because you will experience a 50-200 millisecond pause each time you add a texture, which players will notice as "hiccups" in the rendering, especially if several bubbles pop up around the same time.
Title: Re: blitting text and images
Post by: raft on March 31, 2010, 09:13:40 pm
i guess you are right as a rule of thumb but somehow texture creation is not noticable on this thing. not on emulator and not an actual device. possibly because the created text textures are very small in size. closest 2^n x 2^m to what you see on screen.
Title: Re: blitting text and images
Post by: Darkflame on May 15, 2010, 04:50:19 pm
The class's look really handy.

I'm working on a AR Browser and need on-the-fly generated text bubbles, so this seems appropriate.

One thing; What license do you consider this under? Though we are developing a free, open source, browser ourself (http://arwave.org/), we want people to be able to re-use and adapt our code for their own projects, even if their commercial. So we need to know any code we use if allowed for that :P

Title: Re: blitting text and images
Post by: raft on May 15, 2010, 07:43:57 pm
you can do anything with it ;)
Title: Re: blitting text and images
Post by: Darkflame on May 15, 2010, 08:45:15 pm
Thats fantastic, cheers :)
Title: Re: blitting text and images
Post by: BLadeLaRus on January 20, 2011, 02:56:17 pm
Nice classes, but I wonder. Did somebody use GLFont.getStringBounds(String s)? May be I downloaded older version of this class. But there is simple error. This method GLFont.getStringBounds(String s) doesn't work, it always returns width = 0.  This is happening because of charWidths[] array isn't filled on setting Paint.
Just pointing on this simple copy/paste error  ;)
Title: Re: blitting text and images
Post by: EgonOlsen on January 20, 2011, 03:01:32 pm
I think i used this for Alien Runner and had the same problem. IIRC, raft fixed it back then. I just can't remember if i got that version from him or if he updated the download...
Title: Re: blitting text and images
Post by: raft on January 20, 2011, 03:04:10 pm
which version is it? android or desktop?
Title: Re: blitting text and images
Post by: BLadeLaRus on January 21, 2011, 11:55:36 am
It is android version. I got it from first post in this topic.
Title: Re: blitting text and images
Post by: raft on January 21, 2011, 12:54:07 pm
i've updated the links. please re-download
Title: Re: blitting text and images
Post by: jt123 on March 23, 2013, 01:50:16 pm
Is there any example source for texture pack.
Title: Re: blitting text and images
Post by: raft on March 23, 2013, 04:11:05 pm
Is there any example source for texture pack.
you can have a look at bones demo to see sample usage
http://aptalkarga.com/bones/ (http://aptalkarga.com/bones/)
Title: Re: blitting text and images
Post by: jt123 on March 24, 2013, 11:27:16 am
Thanks i forget to use pack(). Now it works.
Title: Re: blitting text and images
Post by: zerofox on July 09, 2013, 04:41:55 am
hi all,

want to ask about blitting the text. is there anyway to blit text vertically? thanks
Title: Re: blitting text and images
Post by: raft on July 09, 2013, 08:27:09 am
not directly. you need the modify the code.