static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size)

{

    VirtIOGPU *g = opaque;

    struct virtio_gpu_simple_resource *res;

    struct virtio_gpu_scanout *scanout;

    uint32_t resource_id, pformat;

    int i;



    g->hostmem = 0;



    resource_id = qemu_get_be32(f);

    while (resource_id != 0) {

        res = g_new0(struct virtio_gpu_simple_resource, 1);

        res->resource_id = resource_id;

        res->width = qemu_get_be32(f);

        res->height = qemu_get_be32(f);

        res->format = qemu_get_be32(f);

        res->iov_cnt = qemu_get_be32(f);



        /* allocate */

        pformat = get_pixman_format(res->format);

        if (!pformat) {


            return -EINVAL;


        res->image = pixman_image_create_bits(pformat,

                                              res->width, res->height,

                                              NULL, 0);

        if (!res->image) {


            return -EINVAL;




        res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width * res->height;



        res->addrs = g_new(uint64_t, res->iov_cnt);

        res->iov = g_new(struct iovec, res->iov_cnt);



        /* read data */

        for (i = 0; i < res->iov_cnt; i++) {

            res->addrs[i] = qemu_get_be64(f);

            res->iov[i].iov_len = qemu_get_be32(f);


        qemu_get_buffer(f, (void *)pixman_image_get_data(res->image),

                        pixman_image_get_stride(res->image) * res->height);



        /* restore mapping */

        for (i = 0; i < res->iov_cnt; i++) {

            hwaddr len = res->iov[i].iov_len;

            res->iov[i].iov_base =

                cpu_physical_memory_map(res->addrs[i], &len, 1);

            if (!res->iov[i].iov_base || len != res->iov[i].iov_len) {











                return -EINVAL;





        QTAILQ_INSERT_HEAD(&g->reslist, res, next);

        g->hostmem += res->hostmem;



        resource_id = qemu_get_be32(f);




    /* load & apply scanout state */

    vmstate_load_state(f, &vmstate_virtio_gpu_scanouts, g, 1);

    for (i = 0; i < g->conf.max_outputs; i++) {

        scanout = &g->scanout[i];

        if (!scanout->resource_id) {

            continue;


        res = virtio_gpu_find_resource(g, scanout->resource_id);

        if (!res) {

            return -EINVAL;


        scanout->ds = qemu_create_displaysurface_pixman(res->image);

        if (!scanout->ds) {

            return -EINVAL;




        dpy_gfx_replace_surface(scanout->con, scanout->ds);

        dpy_gfx_update(scanout->con, 0, 0, scanout->width, scanout->height);

        update_cursor(g, &scanout->cursor);

        res->scanout_bitmask |= (1 << i);




    return 0;

static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size)

{

    VirtIOGPU *g = opaque;

    struct virtio_gpu_simple_resource *res;

    struct virtio_gpu_scanout *scanout;

    uint32_t resource_id, pformat;

    int i;



    g->hostmem = 0;



    resource_id = qemu_get_be32(f);

    while (resource_id != 0) {

        res = g_new0(struct virtio_gpu_simple_resource, 1);

        res->resource_id = resource_id;

        res->width = qemu_get_be32(f);

        res->height = qemu_get_be32(f);

        res->format = qemu_get_be32(f);

        res->iov_cnt = qemu_get_be32(f);



        

        pformat = get_pixman_format(res->format);

        if (!pformat) {


            return -EINVAL;


        res->image = pixman_image_create_bits(pformat,

                                              res->width, res->height,

                                              NULL, 0);

        if (!res->image) {


            return -EINVAL;




        res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width * res->height;



        res->addrs = g_new(uint64_t, res->iov_cnt);

        res->iov = g_new(struct iovec, res->iov_cnt);



        

        for (i = 0; i < res->iov_cnt; i++) {

            res->addrs[i] = qemu_get_be64(f);

            res->iov[i].iov_len = qemu_get_be32(f);


        qemu_get_buffer(f, (void *)pixman_image_get_data(res->image),

                        pixman_image_get_stride(res->image) * res->height);



        

        for (i = 0; i < res->iov_cnt; i++) {

            hwaddr len = res->iov[i].iov_len;

            res->iov[i].iov_base =

                cpu_physical_memory_map(res->addrs[i], &len, 1);

            if (!res->iov[i].iov_base || len != res->iov[i].iov_len) {











                return -EINVAL;





        QTAILQ_INSERT_HEAD(&g->reslist, res, next);

        g->hostmem += res->hostmem;



        resource_id = qemu_get_be32(f);




    

    vmstate_load_state(f, &vmstate_virtio_gpu_scanouts, g, 1);

    for (i = 0; i < g->conf.max_outputs; i++) {

        scanout = &g->scanout[i];

        if (!scanout->resource_id) {

            continue;


        res = virtio_gpu_find_resource(g, scanout->resource_id);

        if (!res) {

            return -EINVAL;


        scanout->ds = qemu_create_displaysurface_pixman(res->image);

        if (!scanout->ds) {

            return -EINVAL;




        dpy_gfx_replace_surface(scanout->con, scanout->ds);

        dpy_gfx_update(scanout->con, 0, 0, scanout->width, scanout->height);

        update_cursor(g, &scanout->cursor);

        res->scanout_bitmask |= (1 << i);




    return 0;

static int FUNC_0(QEMUFile *VAR_0, void *VAR_1, size_t VAR_2)

{

    VirtIOGPU *g = VAR_1;

    struct virtio_gpu_simple_resource *VAR_3;

    struct virtio_gpu_scanout *VAR_4;

    uint32_t resource_id, pformat;

    int VAR_5;



    g->hostmem = 0;



    resource_id = qemu_get_be32(VAR_0);

    while (resource_id != 0) {

        VAR_3 = g_new0(struct virtio_gpu_simple_resource, 1);

        VAR_3->resource_id = resource_id;

        VAR_3->width = qemu_get_be32(VAR_0);

        VAR_3->height = qemu_get_be32(VAR_0);

        VAR_3->format = qemu_get_be32(VAR_0);

        VAR_3->iov_cnt = qemu_get_be32(VAR_0);



        

        pformat = get_pixman_format(VAR_3->format);

        if (!pformat) {


            return -EINVAL;


        VAR_3->image = pixman_image_create_bits(pformat,

                                              VAR_3->width, VAR_3->height,

                                              NULL, 0);

        if (!VAR_3->image) {


            return -EINVAL;




        VAR_3->hostmem = PIXMAN_FORMAT_BPP(pformat) * VAR_3->width * VAR_3->height;



        VAR_3->addrs = g_new(uint64_t, VAR_3->iov_cnt);

        VAR_3->iov = g_new(struct iovec, VAR_3->iov_cnt);



        

        for (VAR_5 = 0; VAR_5 < VAR_3->iov_cnt; VAR_5++) {

            VAR_3->addrs[VAR_5] = qemu_get_be64(VAR_0);

            VAR_3->iov[VAR_5].iov_len = qemu_get_be32(VAR_0);


        qemu_get_buffer(VAR_0, (void *)pixman_image_get_data(VAR_3->image),

                        pixman_image_get_stride(VAR_3->image) * VAR_3->height);



        

        for (VAR_5 = 0; VAR_5 < VAR_3->iov_cnt; VAR_5++) {

            hwaddr len = VAR_3->iov[VAR_5].iov_len;

            VAR_3->iov[VAR_5].iov_base =

                cpu_physical_memory_map(VAR_3->addrs[VAR_5], &len, 1);

            if (!VAR_3->iov[VAR_5].iov_base || len != VAR_3->iov[VAR_5].iov_len) {











                return -EINVAL;





        QTAILQ_INSERT_HEAD(&g->reslist, VAR_3, next);

        g->hostmem += VAR_3->hostmem;



        resource_id = qemu_get_be32(VAR_0);




    

    vmstate_load_state(VAR_0, &vmstate_virtio_gpu_scanouts, g, 1);

    for (VAR_5 = 0; VAR_5 < g->conf.max_outputs; VAR_5++) {

        VAR_4 = &g->VAR_4[VAR_5];

        if (!VAR_4->resource_id) {

            continue;


        VAR_3 = virtio_gpu_find_resource(g, VAR_4->resource_id);

        if (!VAR_3) {

            return -EINVAL;


        VAR_4->ds = qemu_create_displaysurface_pixman(VAR_3->image);

        if (!VAR_4->ds) {

            return -EINVAL;




        dpy_gfx_replace_surface(VAR_4->con, VAR_4->ds);

        dpy_gfx_update(VAR_4->con, 0, 0, VAR_4->width, VAR_4->height);

        update_cursor(g, &VAR_4->cursor);

        VAR_3->scanout_bitmask |= (1 << VAR_5);




    return 0;

static int FUNC_0(QEMUFile *VAR_0, void *VAR_1, size_t VAR_2)
{ VirtIOGPU *g = VAR_1; struct virtio_gpu_simple_resource *VAR_3; struct virtio_gpu_scanout *VAR_4; uint32_t resource_id, pformat; int VAR_5; g->hostmem = 0; resource_id = qemu_get_be32(VAR_0); while (resource_id != 0) { VAR_3 = g_new0(struct virtio_gpu_simple_resource, 1); VAR_3->resource_id = resource_id; VAR_3->width = qemu_get_be32(VAR_0); VAR_3->height = qemu_get_be32(VAR_0); VAR_3->format = qemu_get_be32(VAR_0); VAR_3->iov_cnt = qemu_get_be32(VAR_0); pformat = get_pixman_format(VAR_3->format); if (!pformat) { return -EINVAL; VAR_3->image = pixman_image_create_bits(pformat,
VAR_3->width, VAR_3->height,
NULL, 0); if (!VAR_3->image) { return -EINVAL; VAR_3->hostmem = PIXMAN_FORMAT_BPP(pformat) * VAR_3->width * VAR_3->height; VAR_3->addrs = g_new(uint64_t, VAR_3->iov_cnt); VAR_3->iov = g_new(struct iovec, VAR_3->iov_cnt); for (VAR_5 = 0; VAR_5 < VAR_3->iov_cnt; VAR_5++) { VAR_3->addrs[VAR_5] = qemu_get_be64(VAR_0); VAR_3->iov[VAR_5].iov_len = qemu_get_be32(VAR_0); qemu_get_buffer(VAR_0, (void *)pixman_image_get_data(VAR_3->image),
pixman_image_get_stride(VAR_3->image) * VAR_3->height); for (VAR_5 = 0; VAR_5 < VAR_3->iov_cnt; VAR_5++) { hwaddr len = VAR_3->iov[VAR_5].iov_len; VAR_3->iov[VAR_5].iov_base =
cpu_physical_memory_map(VAR_3->addrs[VAR_5], &len, 1); if (!VAR_3->iov[VAR_5].iov_base || len != VAR_3->iov[VAR_5].iov_len) { return -EINVAL; QTAILQ_INSERT_HEAD(&g->reslist, VAR_3, next); g->hostmem += VAR_3->hostmem; resource_id = qemu_get_be32(VAR_0); vmstate_load_state(VAR_0, &vmstate_virtio_gpu_scanouts, g, 1); for (VAR_5 = 0; VAR_5 < g->conf.max_outputs; VAR_5++) { VAR_4 = &g->VAR_4[VAR_5]; if (!VAR_4->resource_id) { continue; VAR_3 = virtio_gpu_find_resource(g, VAR_4->resource_id); if (!VAR_3) { return -EINVAL; VAR_4->ds = qemu_create_displaysurface_pixman(VAR_3->image); if (!VAR_4->ds) { return -EINVAL; dpy_gfx_replace_surface(VAR_4->con, VAR_4->ds); dpy_gfx_update(VAR_4->con, 0, 0, VAR_4->width, VAR_4->height); update_cursor(g, &VAR_4->cursor); VAR_3->scanout_bitmask |= (1 << VAR_5); return 0;