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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With