/*****************************************************************************
 * Copyright © 2011, M2X BV
 * Author: Jean-Paul Saman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ****************************************************************************/

#ifdef HAVE_AVCODEC_VAAPI

/* VA API connection shared structure
 * The decoding thread and the display thread
 * need to share the connection to libva and
 * the created surfaces for use with decoding
 * context.
 */
typedef struct vlc_va_conn_t vlc_va_conn_t;
struct vlc_va_conn_t
{
    VADisplay     p_display;
    int           i_version_major;
    int           i_version_minor;
    int           i_ref_count;

    /* locking functions */
    void (*lock)(void);
    void (*unlock)(void);

    /* NOTE: must be called with lock held */
    int           i_surface_count;
    VASurfaceID  *p_surface_ids;

    VASurfaceID *(*create_surfaces)(vlc_va_conn_t *conn, const int width, const int height,
                                   const int fourcc, const int num);
    void (*destroy_surfaces)(vlc_va_conn_t *conn);
};


/* Retrieve shared connection struct */
vlc_va_conn_t *vlc_va_get_conn( void );

/* Initialize shared connection to libva */
vlc_va_conn_t *vlc_va_Initialize(Display *display);

/* Deinitialize shared connection to libva */
void vlc_va_Terminate(vlc_va_conn_t *conn);

static inline VASurfaceID *vlc_va_conn_CreateSurface(vlc_va_conn_t *conn,
    const int width, const int height, const int fourcc, const int num)
{
    return conn->create_surfaces(conn, width, height, fourcc, num);
}

static inline void vlc_va_conn_DestorySurfaces(vlc_va_conn_t *conn)
{
    conn->destroy_surfaces(conn);
}

static inline VASurfaceID *vlc_va_conn_GetSurfaces(vlc_va_conn_t *conn)
{
    return conn->p_surface_ids;
}

static inline int vlc_va_conn_SurfacesCount(vlc_va_conn_t *conn)
{
    return conn->i_surface_count;
}

/* vlc_va_surface_t */
typedef struct
{
    VASurfaceID  i_id;
    int          i_refcount;
    unsigned int i_order;
} vlc_va_surface_t;

/* picture_sys_t */
struct picture_sys_t
{
    /* Link surface with picture_sys_t permanently */
    VASurfaceID  i_id;
    int          i_refcount;

    /* subpicture */
    unsigned int i_cache;
};

#endif
