extern "C" void waylandws_Terminate(_EGLDisplay *dpy)
{
	WaylandDisplay *wdpy = (WaylandDisplay *)dpy;
	int ret = 0;
	// We still have the sync callback on flight, wait for it to arrive
	while (ret == 0 && !wdpy->wlegl) {
		ret = wl_display_dispatch_queue(wdpy->wl_dpy, wdpy->queue);
	}
	assert(ret >= 0);
	android_wlegl_destroy(wdpy->wlegl);
	wl_registry_destroy(wdpy->registry);
	wl_event_queue_destroy(wdpy->queue);
	delete wdpy;
}
WaylandNativeWindow::~WaylandNativeWindow()
{
    std::list<WaylandNativeWindowBuffer *>::iterator it = m_bufList.begin();
    destroyBuffers();
    if (frame_callback)
        wl_callback_destroy(frame_callback);
    wl_registry_destroy(registry);
    wl_event_queue_destroy(wl_queue);
    android_wlegl_destroy(m_android_wlegl);
    if (m_window) {
	    m_window->nativewindow = NULL;
	    m_window->resize_callback = NULL;
	    m_window->free_callback = NULL;
    }
}
WaylandNativeWindow::~WaylandNativeWindow()
{
    std::list<WaylandNativeWindowBuffer *>::iterator it = m_bufList.begin();
    for (; it != m_bufList.end(); it++)
    {
        WaylandNativeWindowBuffer* buf=*it;
        if (buf->wlbuffer)
            wl_buffer_destroy(buf->wlbuffer);
        buf->wlbuffer = NULL;
        buf->common.decRef(&buf->common);
    }
    if (frame_callback)
        wl_callback_destroy(frame_callback);
    wl_registry_destroy(registry);
    wl_event_queue_destroy(wl_queue);
    android_wlegl_destroy(m_android_wlegl);
}