I have a question regarding creation of a SurfaceView and subsequently getting a ANativeWindow from it.
mSurfaceView = new SurfaceView(this);
in:onCreate()
onStart()
Reason for asking: as I understand it the SurfaceView will get destroyed when we lose focus (something else covers up the entire screen) so we will need to re-create it every time we gain focus (onStart() is executed). Or does the SurfaceView remain dormant and reusable?
Moving on, now I would like to create a native window from the above-mentioned surface (in native code). Is it proper to do ANativeWindow* newwindow = ANativeWindow_fromSurface(jniEnv, joSurface)
in:
onSurfaceCreated_native(..., jobject surface)
onSurfaceChanged_native(..., jobject surface)
Reason for asking: onSurfaceChanged seems to be always called after onSurfaceCreated
so we have a choice as to when to create the native window. On one hand, it appears logical to do this in onSurfaceCreated
, but the two jobject surface
appear to be referencing different objects! (As checked by creating a weak global ref to surface in onSurfaceCreated and checking it against both NULL and surface in onSurfaceChanged, see code below)
onSurfaceCreated_native(JNIEnv env, ... ,jobject surface) {
myWeakObjectGlobal = env->NewWeakGlobalRef(surface);
}
onSurfaceChanged_native(JNIEnv env, ... ,jobject surface) {
if (env->IsSameObject(surface, myWeakObjectGlobal)) {
LOGW("onSurfaceChanged_native: new surface is SAME as old surface");
} else {
LOGW("onSurfaceChanged_native: new surface is DIFFERENT as old surface");
}
if (env->IsSameObject(NULL, myWeakObjectGlobal)) {
LOGW(" furthermore, old surface is NULL");
} else {
LOGW(" furthermore, old surface is NOT null");
}
}
Therefore, if there are indeed two distinct surface objects being sent into onSurfaceCreated and onSurfaceChanged, then we want to use the freshest one and not hang on to a stale surface reference, and consequently do ANativeWindow_from_Surface in onSurfaceChanged.
I would really appreciate it if someone could shine some light on this issue for me.
Have you tried using android.view.Surface
instead of android.view.SurfaceView
?