Author Topic: EGL_BAD_SURFACE after some orientation changes  (Read 11264 times)

Offline ruspa

  • byte
  • *
  • Posts: 6
    • View Profile
EGL_BAD_SURFACE after some orientation changes
« on: March 05, 2016, 03:02:22 pm »
hello, anyone had issues after you've changed screen orientation a few times? Especially maybe with wallpapers based on GLSurfaceView(http://www.rbgrn.net/content/354-glsurfaceview-adapted-3d-live-wallpapers)?
I'd have preferred to force my app not to change orientation in any case, but since it's a live wallpaper I really cannot do it. You can always change the orientation of LWP in the wallpaper gallery menu, for instance, or if you have a phone with a switchblade-like side keyboard.

After a couple of times I reorient the device (and my lwp as well), or locking and unlocking very quickly, I start getting wobbles and flickers in my rendering, and in the logcat i see errors like this.

Code: [Select]
E/Surface: queueBuffer: error queuing buffer to SurfaceTexture, -19
W/Adreno-EGLSUB: <SwapBuffers:1345>: failed to queueBuffer
W/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3888>: EGL_BAD_SURFACE
W/Adreno-EGLSUB: <DequeueBuffer:721>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
W/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3792>: EGL_BAD_SURFACE

over and over again. At the beginning just a few. The more you keep reorienting, the more the render engine gets recreated and the more same idendical errors flow on the log.

Is this something due to previus-orientation instances still trying to render on null surfaceviews? I tried my best to not recreate everything at any rotation change but this occurs nevertheless. See the formatted log  ???

Code: [Select]
03-05 14:58:34.468 24653-24653/jPCT-AE: [ 1457186314468 ] - WARNING: MyRenderer() NEW <-----
03-05 14:58:34.485 24653-24653/jPCT-AE: [ 1457186314485 ] - WARNING: WPService.MyEngine() NEW <---
03-05 14:58:34.737 24653-24653/jPCT-AE: [ 1457186314737 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:34.738 24653-24653/jPCT-AE: [ 1457186314738 ] - WARNING: pause
03-05 14:58:34.740 24653-24653/jPCT-AE: [ 1457186314740 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:34.808 24653-24676/jPCT-AE: [ 1457186314808 ] - WARNING: creating textures ****************************************************************************
03-05 14:58:35.228 24653-24676/jPCT-AE: [ 1457186315228 ] - WARNING: creating  object
03-05 14:58:35.355 24653-24676/jPCT-AE: [ 1457186315355 ] - WARNING: creating world
03-05 14:58:35.358 24653-24676/jPCT-AE: [ 1457186315358 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:35.358 24653-24676/jPCT-AE: [ 1457186315358 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:35.358 24653-24676/jPCT-AE: [ 1457186315358 ] - WARNING: -420 0 45.0
03-05 14:58:35.358 24653-24676/jPCT-AE: [ 1457186315358 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:38.596 24653-24653/jPCT-AE: [ 1457186318596 ] - WARNING: WPService.MyEngine.onSensorChanged > portrait: true
03-05 14:58:38.663 24653-24653/jPCT-AE: [ 1457186318663 ] - WARNING: pause
03-05 14:58:38.882 24653-24653/jPCT-AE: [ 1457186318882 ] - WARNING: MyRenderer() NEW <-----
03-05 14:58:38.882 24653-24653/jPCT-AE: [ 1457186318882 ] - WARNING: WPService.MyEngine() NEW <---
03-05 14:58:38.899 24653-24653/jPCT-AE: [ 1457186318899 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:38.899 24653-24653/jPCT-AE: [ 1457186318899 ] - WARNING: pause
03-05 14:58:38.905 24653-24653/jPCT-AE: [ 1457186318905 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:38.944 24653-24846/jPCT-AE: [ 1457186318944 ] - WARNING: creating textures ****************************************************************************
03-05 14:58:38.944 24653-24846/jPCT-AE: [ 1457186318944 ] - WARNING: creating  object
03-05 14:58:39.093 24653-24846/jPCT-AE: [ 1457186319093 ] - WARNING: creating world
03-05 14:58:39.094 24653-24846/jPCT-AE: [ 1457186319094 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:39.094 24653-24846/jPCT-AE: [ 1457186319094 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:39.094 24653-24846/jPCT-AE: [ 1457186319094 ] - WARNING: -420 0 45.0
03-05 14:58:39.094 24653-24846/jPCT-AE: [ 1457186319094 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:43.830 24653-24653/jPCT-AE: [ 1457186323830 ] - WARNING: WPService.MyEngine.onSensorChanged > portrait: false
03-05 14:58:43.961 24653-24653/jPCT-AE: [ 1457186323961 ] - WARNING: pause
03-05 14:58:44.067 24653-24653/jPCT-AE: [ 1457186324067 ] - WARNING: MyRenderer() NEW <-----
03-05 14:58:44.068 24653-24653/jPCT-AE: [ 1457186324068 ] - WARNING: WPService.MyEngine() NEW <---
03-05 14:58:44.089 24653-24653/jPCT-AE: [ 1457186324089 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:44.089 24653-24653/jPCT-AE: [ 1457186324089 ] - WARNING: pause
03-05 14:58:44.101 24653-24653/jPCT-AE: [ 1457186324101 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:44.141 24653-24976/jPCT-AE: [ 1457186324141 ] - WARNING: creating textures ****************************************************************************
03-05 14:58:44.141 24653-24976/jPCT-AE: [ 1457186324141 ] - WARNING: creating  object
03-05 14:58:44.283 24653-24976/jPCT-AE: [ 1457186324283 ] - WARNING: creating world
03-05 14:58:44.284 24653-24976/jPCT-AE: [ 1457186324284 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:44.284 24653-24976/jPCT-AE: [ 1457186324284 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:44.285 24653-24976/jPCT-AE: [ 1457186324284 ] - WARNING: -420 0 45.0
03-05 14:58:44.285 24653-24976/jPCT-AE: [ 1457186324285 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:53.901 24653-24653/jPCT-AE: [ 1457186333901 ] - WARNING: WPService.MyEngine.onSensorChanged > portrait: true
03-05 14:58:54.058 24653-24976/Adreno-EGLSUB: <DequeueBuffer:721>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
03-05 14:58:54.058 24653-24976/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3792>: EGL_BAD_SURFACE
03-05 14:58:54.108 24653-24976/Adreno-EGLSUB: <DequeueBuffer:721>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
03-05 14:58:54.108 24653-24976/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3792>: EGL_BAD_SURFACE
03-05 14:58:54.152 24653-24653/jPCT-AE: [ 1457186334152 ] - WARNING: pause
03-05 14:58:54.192 24653-24653/jPCT-AE: [ 1457186334192 ] - WARNING: MyRenderer() NEW <-----
03-05 14:58:54.193 24653-24653/jPCT-AE: [ 1457186334193 ] - WARNING: WPService.MyEngine() NEW <---
03-05 14:58:54.221 24653-24653/jPCT-AE: [ 1457186334221 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:54.221 24653-24653/jPCT-AE: [ 1457186334221 ] - WARNING: pause
03-05 14:58:54.238 24653-24653/jPCT-AE: [ 1457186334238 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:54.263 24653-25237/jPCT-AE: [ 1457186334263 ] - WARNING: creating textures ****************************************************************************
03-05 14:58:54.263 24653-25237/jPCT-AE: [ 1457186334263 ] - WARNING: creating  object
03-05 14:58:54.409 24653-25237/jPCT-AE: [ 1457186334409 ] - WARNING: creating world
03-05 14:58:54.410 24653-25237/jPCT-AE: [ 1457186334410 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:54.410 24653-25237/jPCT-AE: [ 1457186334410 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:54.410 24653-25237/jPCT-AE: [ 1457186334410 ] - WARNING: -420 0 45.0
03-05 14:58:54.410 24653-25237/jPCT-AE: [ 1457186334410 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:56.132 24653-24653/jPCT-AE: [ 1457186336132 ] - WARNING: WPService.MyEngine.onSensorChanged > portrait: false
03-05 14:58:56.275 24653-25237/Adreno-EGLSUB: <DequeueBuffer:721>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
03-05 14:58:56.275 24653-25237/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3792>: EGL_BAD_SURFACE
03-05 14:58:56.336 24653-25237/Adreno-EGLSUB: <DequeueBuffer:721>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
03-05 14:58:56.336 24653-25237/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3792>: EGL_BAD_SURFACE
03-05 14:58:56.348 24653-25237/Adreno-EGLSUB: <DequeueBuffer:721>: dequeue native buffer fail: No such device, buffer=0x0, handle=0x0
03-05 14:58:56.348 24653-25237/Adreno-EGL: <qeglDrvAPI_eglSwapBuffers:3792>: EGL_BAD_SURFACE
03-05 14:58:56.350 24653-24653/jPCT-AE: [ 1457186336350 ] - WARNING: pause
03-05 14:58:56.456 24653-24653/jPCT-AE: [ 1457186336456 ] - WARNING: MyRenderer() NEW <-----
03-05 14:58:56.457 24653-24653/jPCT-AE: [ 1457186336457 ] - WARNING: WPService.MyEngine() NEW <---
03-05 14:58:56.482 24653-24653/jPCT-AE: [ 1457186336482 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:56.482 24653-24653/jPCT-AE: [ 1457186336482 ] - WARNING: pause
03-05 14:58:56.508 24653-24653/jPCT-AE: [ 1457186336508 ] - WARNING:  WPService.MyEngine.onResume()
03-05 14:58:56.559 24653-25296/jPCT-AE: [ 1457186336559 ] - WARNING: creating textures ****************************************************************************
03-05 14:58:56.559 24653-25296/jPCT-AE: [ 1457186336559 ] - WARNING: creating  object
03-05 14:58:56.749 24653-25296/jPCT-AE: [ 1457186336749 ] - WARNING: creating world
03-05 14:58:56.749 24653-25296/jPCT-AE: [ 1457186336749 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:56.749 24653-25296/jPCT-AE: [ 1457186336749 ] - WARNING: No normals have been calculated for this mesh yet!
03-05 14:58:56.750 24653-25296/jPCT-AE: [ 1457186336750 ] - WARNING: -420 0 45.0
03-05 14:58:56.750 24653-25296/jPCT-AE: [ 1457186336750 ] - WARNING: No normals have been calculated for this mesh yet!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: EGL_BAD_SURFACE after some orientation changes
« Reply #1 on: March 05, 2016, 03:18:44 pm »
It feels like a threading issue to me. Your guess about the old instances still trying to render something is what I would have thought, too. What do these 5 digit numbers in the log represent? Thread IDS?

Offline ruspa

  • byte
  • *
  • Posts: 6
    • View Profile
Re: EGL_BAD_SURFACE after some orientation changes
« Reply #2 on: March 05, 2016, 04:11:47 pm »
Probably they are, seems reasonable. But I cannot easily debug on my device (with breakpoints and all the frills) to prove it, sometime works but most of the time it doesn't connect to the debugger. Since gradle/android studio at least, it could do it easier in eclipse.

I provided a stripped code of my 2 classes. I removed every chunk of code that looked totally unrelated to the issue (sensors reading, rotation matrix calculations and stuff) I think that android system recreates the wallpaper "almost completely" every time you rotate the device, leaving you with bits of the older activities still running or interfering with the newer one.
Or it can be the 2/3 second lag that the wallpaper picker has when switching orientation? I wonder why these errors (and the resulting bad rotation readings, basically the app is a sphere doing the compass, just in 3d and without gymbal lock) are incremental.
Perhaps there's a clever way to store and reuse previously created engines and to pass it again once the OS recreates the activity... but the openGL world and its statemachine is still black magic to me. By googling I found out this error (or pretty similar) is not new to the developers, but most of the times the solution they give is to lock the app in a fixed orientation. But I can't! :(

Main class:
Code: [Select]



public class myBallObjectWPService extends GLWallpaperService{

   

   

    public myBallObjectWPService(){
        super();
        android.os.Debug.waitForDebugger();

    }


    public Engine onCreateEngine() {


       if(engine!=null){
            engine.onDestroy();
            engine=null;
       }
        engine = new MyEngine(this);
        return engine;

    }





    class MyEngine extends GLEngine implements SensorEventListener{

        MyRenderer renderer;
        Context _context;



        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            initializeSensors();
        }

        @Override
        public void onDestroy() {


            if (renderer != null) {
                renderer.release();
            }
            renderer = null;

            super.onDestroy();


        }
        @Override
        public void onResume() {
            super.onResume();
        }

        @Override
        public void onPause() {
            super.onPause();
        }


     




        @Override
        public void onSensorChanged(SensorEvent event) {


     

            if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
               
synchronized (mRotationMatrix) {

//[do axes remap and sensor readings]

                }


        }



        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }



        public MyEngine(Context context) {

            _context = context;


            if(renderer!=null){
                renderer.release();
                renderer=null;
            }
            renderer = new MyRenderer(_context);
            setRenderer(renderer);
            setRenderMode(RENDERMODE_CONTINUOUSLY);



        }



}


Rendering class:
Code: [Select]

public class MyRenderer implements GLWallpaperService.Renderer {



    public MyRenderer(Context context) {


        if(myBallObjectWPService.renderer!=null){
            return;
        }

        GLSurfaceView mGLView = new GLSurfaceView(context);
        mGLView.setEGLContextClientVersion(2);
        _context = context;



    }



    public void onSurfaceChanged(GL10 gl, int w, int h) {



     
createObjects();

        if (fb != null) {
            fb.dispose();
        }
        fb = new FrameBuffer(gl, w, h);


Logger.log("creating world", Logger.WARNING);

world = new World();
world.setAmbientLight(100, 100, 100);

world.addObject(myBallObject);
cam = world.getCamera();

cam.moveCamera(Camera.CAMERA_MOVEOUT, myZoom);



SimpleVector sv = new SimpleVector();
sv.y -= 1600;
sv.z = 1600;
sv.x = -1600;
sun = new Light(world);
sun.setIntensity(250, 250, 250);
sun.setPosition(sv);


            MemoryHelper.compact();


        }
    }



    public void onSurfaceCreated(GL10 gl, EGLConfig config) {



        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.create((String) null, Typeface.BOLD));

        paint.setTextSize(50);
        glFont = new AGLFont(paint);

        paint.setTextSize(300);


    }





    public void onDrawFrame(GL10 gl) {





        synchronized (myBallObjectWPService.mRotationMatrix) {
            myBallObject.clearRotation();
            m = new Matrix();
            m.setDump(myBallObjectWPService.mRotationMatrix);
            myBallObject.setRotationMatrix(m);
        }


        fb.clear(back);
        world.renderScene(fb);
        world.draw(fb);
        fb.display();

    }


    public void release() {
    }

   

private void loadTextures(){


}
   
   

    private createObjectsAndTextures() {

        if ( !TextureManager.getInstance().containsTexture( "myBallObjecttex_1" ) ) {
            myBallObjectTexture_1 = new Texture(BitmapHelper.rescale(BitmapHelper.convert(ContextCompat.getDrawable(_context, R.raw.myBallObjecttex2)), 1024, 512));
            TextureManager.getInstance().addTexture("myBallObjecttex_1", myBallObjectTexture_1);
        }

myBallObject = ExtendedPrimitives.createEllipsoid(new SimpleVector(305, 305, 305), 64, 1f, 1f);


        myBallObject.setTexture("myBallObjecttex_1");
       

        myBallObject.rotateX((float) Math.toRadians(-90));
        myBallObject.rotateZ((float) Math.toRadians(90));
        myBallObject.rotateMesh();
        myBallObject.compile(true);

        myBallObject.calcNormals();
        myBallObject.strip();
       

        myBallObject.build();
    }


}
« Last Edit: March 05, 2016, 04:24:31 pm by ruspa »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: EGL_BAD_SURFACE after some orientation changes
« Reply #3 on: March 05, 2016, 04:23:42 pm »
I haven't any experience with wallpaper stuff myself, but from earlier posts I dimly remember that this wallpaper service does some threading stuff that can cause trouble.
Personally, I'm not a big fan of debuggers anyway. I prefer simple log output...so I suggest to add some more logging to the whole thing to see what's going on. If these numbers are thread or process IDs, it really looks like some threading issue to me.

Offline ruspa

  • byte
  • *
  • Posts: 6
    • View Profile
Re: EGL_BAD_SURFACE after some orientation changes
« Reply #4 on: March 05, 2016, 04:37:09 pm »
I probably have to deal with "fragments" [insert WAT meme here] as mentioned here https://stackoverflow.com/questions/7128670/best-practice-asynctask-during-orientation-change. Or find a cool way to pin my wallpaper in portrait like forever.

"Caution: While you can store any object, you should never pass an object that is tied to the Activity, such as a Drawable, an Adapter, a View or any other object that's associated with a Context. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)"

Fragments. Fragments of the weekend that ... pfff... evaporate :D :D :D