Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » al_lock_bitmap/al_unlock_bitmap pair is very slow on iOS

This thread is locked; no one can reply to it. rss feed Print
al_lock_bitmap/al_unlock_bitmap pair is very slow on iOS
topfortune2015
Member #15,944
April 2015

Hi guys,

I am working on a simple video app on iOS. I use Allegro to display the decoded video frames. I found the al_lock_bitmap/al_unlock_bitmap pair is the bottleneck. Follow is the test code for the problem.

void main()
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_BITMAP *bitmap = NULL;

al_init();

display = al_create_display(1136,640);

al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
bitmap = al_create_bitmap(1136, 640);

double t1 = al_get_time();
int n = 128;

while( n > 0 )
{
ALLEGRO_LOCKED_REGION* lr = al_lock_bitmap(bitmap, al_get_bitmap_format(bitmap), ALLEGRO_LOCK_WRITEONLY);
/*
For demonstration purchase, I place nothing here.
*/
al_unlock_bitmap(bitmap);

al_set_target_backbuffer(display);
al_clear_to_color(al_map_rgb(0,0,0));

al_draw_bitmap(bitmap, 0, 0, 0);

al_flip_display();

printf("frame: %d\n", 128-n);
n--;
}


al_destroy_bitmap(bitmap);
al_destroy_display(display);

double t = al_get_time() - t1;
printf("FPS = %f\n", 128/t);

}

On my iPhone 5, I got FPS = 4.828641. What's the problem?

Elias
Member #358
May 2000

If you use LOCK WRITE ONLY it means the entire bitmap contents have to be transferred back to the GPU in al_unlock_bitmap which is very slow. Locking a smaller region and not the entire bitmap should be faster.

--
"Either help out or stop whining" - Evert

Chris Katko
Member #1,881
January 2002
avatar

And by that, he means use this function:

https://www.allegro.cc/manual/5/al_lock_bitmap_region

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

topfortune2015
Member #15,944
April 2015

To @Elias and @Chris Katko, thank you for your suggestions!

I need to update the entire bitmap with a new same size video frame in the loop, so I think al_lock_bitmap_region is useless for me currently.

Did you notice the FPS I got on my iPhone 5, the frame rate is only 4.8, which means one lock/unlock pair need more than 200ms. I don’t understand that.

I also made a similar test with SFML 2 on my iphone5. I created a sf::Texture object with 1136x640 size, and use update method in the loop to feed new video frame data to it, I got 55 frames per second on my iphone5.

beoran
Member #12,636
March 2011

You should really use Allegro 5.1 with the video streaming add-on. Then Allegro will provide you with the active frame though al_get_video_frame. This will be much faster than trying to do it by hand.

http://alleg.sourceforge.net/a5docs/refman/video.html
http://sourceforge.net/p/alleg/allegro/ci/5.1/tree/examples/ex_video.c

SiegeLord
Member #7,827
October 2006
avatar

The video addon just uses the locking API, so it's unlikely that it will be any faster. I can't really explain why it's slow compared to SFML though, since from what I can see it all gets routed to a call of glTexSubImage2D without any conversion... one suggestion I have is to try creating the bitmap with al_set_new_bitmap_flags(ALLEGRO_NO_PRESERVE_TEXTURE).

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

beoran
Member #12,636
March 2011

Oh, I didn't know that the video add-on also uses locking, but it does make sense for quick rendering. In that case we'll have the "fun task" of figuring out what SFML does differently... :p

topfortune2015
Member #15,944
April 2015

Wow, al_set_new_bitmap_flags(ALLEGRO_NO_PRESERVE_TEXTURE) is working! Thank you @SiegeLord

Polybios
Member #12,293
October 2010

I thought this flag only had a meaning for Direct3D? Should be documented then. ;)

beoran
Member #12,636
March 2011

Yes, does it work for all OpenGl backends or only IOS? Will you get an ALLEGRO_EVENT_DISPLAY_LOST?

Thomas Fjellstrom
Member #476
June 2000
avatar

I suspect that flag also has an effect on locking a texture, it wont download it when you don't ask it to explicitly.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SiegeLord
Member #7,827
October 2006
avatar

Video bitmap contents may be lost on Android, iOS and Windows, so that flag does something on those 3 platforms. It's a shame that we have ALLEGRO_DISPLAY_LOST and ALLEGRO_EVENT_DISPLAY_HALT_DRWAING which basically do the same thing... it's something I want to rectify before 5.2 (probably deprecate ALLEGRO_DISPLAY_LOST).

Anyway, I documented the situation a bit better: http://sourceforge.net/p/alleg/allegro/ci/1ccf8dd170b08e1ef4b9d78c6c0707ddf0087907/

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Elias
Member #358
May 2000

I'm not sure if I wouldn't rather deprecate ALLEGRO_NO_PRESERVE_TEXTURE. In addition to the slowness if you ever modify the contents (like the 5 FPS instead of 55 FPS in this thread), it also means keeping a memory copy of every bitmap around.

But of course the possibility of losing all of your bitmap contents is quite bad as well - I almost can't believe there's no better way around it. Should check SDL2 and SFML and see how they solve the problem.

--
"Either help out or stop whining" - Evert

beoran
Member #12,636
March 2011

I rather think it is insane that the video system may independently decide to throw away your uploaded bitmaps. Imagine if the OS did that with your file handles, etc. >:(

IIRC SDL distinguishes between a surface (a memory bitmap) and a texture (uploaded to video). That approach brings a lot of unneeded duplication with it, though.

Chris Katko
Member #1,881
January 2002
avatar

SiegeLord said:

Video bitmap contents may be lost on Android, iOS and Windows, so that flag does something on those 3 platforms.

I thought that was some old, antiquated problem during the early days of videocard acceleration--back in the day, if you used ALT-TAB at any point your computer would freeze for ~15-30 seconds. It was actually common to see people play their games in "windowed mode" or even "windowless" (a window, full screen, without window borders/title/etc) just because it made ALT-TAB'ing faster, and less prone to crashes.

http://stackoverflow.com/questions/972299/best-practices-for-alt-tab-support-in-a-directx-app

Then again, it's apparently still a userspace problem:

http://www.rockpapershotgun.com/2008/05/12/a-lamention-for-alt-tab/

Yet supposedly, Direct9 and later is (almost) never supposed to dump your resources:

Devices are now only lost under two circumstances; when the hardware is reset because it is hanging, and when the device driver is stopped. When hardware hangs, the device can be reset by calling ResetEx. If hardware hangs, texture memory is lost.

Yet we're talking OpenGL here, right? I always thought OpenGL handled the situation better.

The only information I see related to OpenGL losing anything is this:

Changing the window visibility status may require to reset the OpenGL viewport, so it's a good idea to either call glViewport apropriately in the display/rendering function, or at least set it in the resize handler, followed by a complete redraw.

???

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Thomas Fjellstrom
Member #476
June 2000
avatar

OpenGL should never forget texture contents, but to do that the implementation usually keeps a memory copy around itself (which is why we dont...).

Yet supposedly, Direct9 and later is (almost) never supposed to dump your resources:

That's the first I've heard about that. Someone should test various conditions of when the context could be lost by d3d (alt-tab, minimize, another app pops up, etc).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SiegeLord
Member #7,827
October 2006
avatar

Our Direct3D bitmaps are not managed by the driver (since we allow drawing to them), so that's why we need to do this manually.

For OpenGL, it's not an issue except the two GLES platforms I described above which apparently evict the bitmaps from memory when you minimize the application. This makes sense, to a certain extent, given how apps are not meant to be closed on mobile devices...

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Polybios
Member #12,293
October 2010

SiegeLord said:

It's a shame that we have ALLEGRO_DISPLAY_LOST and ALLEGRO_EVENT_DISPLAY_HALT_DRAWING which basically do the same thing... it's something I want to rectify before 5.2 (probably deprecate ALLEGRO_DISPLAY_LOST).

There's this warning that apps will crash when you draw after ALLEGRO_EVENT_DISPLAY_HALT_DRAWING but it says that drawing calls are ignored after
ALLEGRO_EVENT_DISPLAY_LOST. I know it mainly depends on the underlying platform, but I'd prefer the non-crash behavior (?). I didn't try if it really crashes though. At least it would be nice to have one clearly defined behavior if these are merged into one.

Quote:

Anyway, I documented the situation a bit better

Great!

beoran
Member #12,636
March 2011

As an aside, does the the video add on already use ALLEGRO_NO_PRESERVE_TEXTURE? If not, it probably should.

SiegeLord
Member #7,827
October 2006
avatar

Polybios said:

There's this warning that apps will crash when you draw after ALLEGRO_EVENT_DISPLAY_HALT_DRAWING but it says that drawing calls are ignored after ALLEGRO_EVENT_DISPLAY_LOST. I know it mainly depends on the underlying platform, but I'd prefer the non-crash behavior (?). I didn't try if it really crashes though. At least it would be nice to have one clearly defined behavior if these are merged into one.

It's just an implementational detail (the D3D backend has tons of early returns in drawing functions when the display is lost). We don't trust D3D to properly ignore drawing calls.

For iOS/Android it's a bit different, since it's very bad form to keep using CPU after you have minimized the app, so not stopping drawing is bad even we don't let it crash.

beoran said:

As an aside, does the the video add on already use ALLEGRO_NO_PRESERVE_TEXTURE? If not, it probably should.

It doesn't, but the addon is getting a rewrite in the coming months so I wouldn't worry about what it does right now too much.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Go to: