5

When I rotate my device and my app recieves APP_CMD_CONFIG_CHANGED, I attempt to update my OpenGL viewport with the following:

EGLint width;
EGLint height;
eglQuerySurface(display,surface,EGL_WIDTH,&width);
eglQuerySurface(display,surface,EGL_HEIGHT,&height);

glViewport(0,0,width,height);

MY_LOG_MACRO2("New window size: %d x %d",width,height);

This works about 20% of the time. The other 80% of the time the width and height are the previous values. In other words, most of the time when I turn my device to landscape mode, the portrait size is logged. And when I go back to portrait, the landscape size is logged.

I haven't had much luck with getting the size from the ANativeWindow either.

What should I be doing to get correct window size after rotation?

Update:

By waiting a few frames after APP_CMD_CONFIG_CHANGED, the size is always correct. Querying the size every frame, without regard for APP_CMD_CONFIG_CHANGED, and checking if it has changed seems to work as well but feels wrong.

IronMensan
  • 6,761
  • 1
  • 26
  • 35
  • Where are you calling that code? Opengl context are thread specific. If this code is ran in random thread, it may only work when thread with context set is used – crazyjul Oct 07 '11 at 00:44
  • @crazyjul My application only has one thread. This function is being called when ALooper_pollAll gets a `APP_CMD_CONFIG_CHANGED` event and then I run `pollSource->process(appState,pollSource)`. My event loop is nearly identical to the sample code. – IronMensan Oct 07 '11 at 14:44
  • A post from Android forums with the same issue http://groups.google.com/group/android-ndk/browse_thread/thread/8fcfd0177eb78c26/9bfe07b81c36fdea?pli=1 – crazyjul Oct 07 '11 at 16:01

2 Answers2

3

For detecting screen orientation changing I've decided do not catch APP_CMD_CONFIG_CHANGED, but call eglQuerySurface() every frame. If screen size values mismatch saved values, screen orientation has been changed.

Two calls of eglQuerySurface() for getting width and height take about 10 microseconds (100 milliseconds when called 10000 times) so it doesn't reduce performance and it give 100% guarantee that screen rotation will be handled.

I think this solution is better than waiting several frames after APP_CMD_CONFIG_CHANGED. By the way, rotating device to 180 degree (upside down) cause getting APP_CMD_CONFIG_CHANGED event but actually no screen orientation has been changed and no resize-depended actions should be performed.

Roman Shuvalov
  • 355
  • 3
  • 12
0

It might not work because for example surface size might be set when creating context, and you querying for 'previous' size (when context was created). Edit: I've tested it on SGS2(2.3.5) and I get correct results immediatelly, so it might be a device 'vendor' problem (or Android version problem).

Edit2: It looks that I've made some mistake while testing, I'm getting weird results too as @IronMensan. Sometimes it works correctly, but mostly not. Sorry for my mistake. So I would stay at recreation of EGL context as in my initial answer (below).

When I get APP_CMD_CONFIG_CHANGED I just simply destroy and recreate EGL context (with shaders, textures, etc.), similar when I get APP_CMD_TERM_WINDOW -> APP_CMD_INIT_WINDOW.

Krystian Bigaj
  • 1,295
  • 8
  • 14
  • Take another look at the question and the update I just added - the surface is resized, just usually not immediately. It takes about five frames on average. – IronMensan Oct 02 '11 at 12:52
  • @IronMensan: I've just tested it and it works correctly, after I get APP_CMD_CONFIG_CHANGED then eglQuerySurface(EGL_WIDTH/EGL_HEIGHT) gives me correct results immediately. Tested on SGSII(2.3.5-KI3). So it might be a some vendor implementation problem. What is your device and android version? – Krystian Bigaj Oct 03 '11 at 17:05
  • HTC Evo 4G Android 2.3.3. It looks like compatibility testing is going to be more painful than I anticipated. – IronMensan Oct 03 '11 at 17:15
  • For reference, I have a Hero running Gingerbread 2.3.4 (via a modded ROM, I'm using Elelinux) and I'm seeing this too. – David Given Oct 06 '11 at 11:12
  • @IronMensan: I've edited my answer, I'm seeing same weird behaviour. – Krystian Bigaj Oct 06 '11 at 19:17