/* check whether the decoded surface size has changed */
static gboolean
is_surface_resolution_changed (GstVaapiDecode * decode,
    GstVaapiSurface * surface)
{
  GstVideoInfo *vinfo = &decode->decoded_info;
  GstVideoFormat surface_format;
  guint surface_width, surface_height;

  g_return_val_if_fail (surface != NULL, FALSE);

  gst_vaapi_surface_get_size (surface, &surface_width, &surface_height);

  if (GST_VIDEO_INFO_WIDTH (vinfo) == surface_width
      && GST_VIDEO_INFO_HEIGHT (vinfo) == surface_height)
    return FALSE;

  /* doing gst_vaapi_surface_get_format() only if necessary since it
   * execute vaDeriveImage in the background. This will usually get
   * executed only once */
  surface_format = GST_VIDEO_INFO_FORMAT (vinfo);
  if (surface_format == GST_VIDEO_FORMAT_UNKNOWN) {
    surface_format = gst_vaapi_surface_get_format (surface);

    /* if the VA context delivers a currently unrecognized format
     * (ICM3, e.g.), we can assume NV12 "safely" */
    if (surface_format == GST_VIDEO_FORMAT_UNKNOWN
        || surface_format == GST_VIDEO_FORMAT_ENCODED)
      surface_format = GST_VIDEO_FORMAT_NV12;
  }

  gst_video_info_set_format (vinfo, surface_format, surface_width,
      surface_height);

  return TRUE;
}
static void
ensure_window_size (App * app, GstVaapiSurface * surface)
{
  guint width, height;

  if (gst_vaapi_window_get_fullscreen (app->window))
    return;

  gst_vaapi_surface_get_size (surface, &width, &height);
  if (app->surface_width == width && app->surface_height == height)
    return;
  app->surface_width = width;
  app->surface_height = height;

  gst_vaapi_window_set_size (app->window, width, height);
  gst_vaapi_window_get_size (app->window,
      &app->window_width, &app->window_height);
}
static gboolean
is_surface_resolution_changed (GstVideoDecoder *vdec, GstVaapiSurface *surface)
{
  guint surface_width, surface_height;
  guint configured_width, configured_height;
  GstVideoCodecState *state;
  gboolean ret = FALSE;

  gst_vaapi_surface_get_size(surface, &surface_width, &surface_height);

  state = gst_video_decoder_get_output_state (vdec);
  configured_width = GST_VIDEO_INFO_WIDTH (&state->info);
  configured_height = GST_VIDEO_INFO_HEIGHT (&state->info);
  gst_video_codec_state_unref (state);

  if (surface_width != configured_width || surface_height != configured_height)
    ret = TRUE;

  return ret;
}
static gboolean
ensure_pixmaps (App * app, GstVaapiSurface * surface,
    const GstVaapiRectangle * crop_rect)
{
  GstVaapiPixmap *pixmaps[G_N_ELEMENTS (app->pixmaps)];
  guint num_pixmaps, i, width, height;
  gboolean success = FALSE;

  if (crop_rect) {
    width = crop_rect->width;
    height = crop_rect->height;
  } else
    gst_vaapi_surface_get_size (surface, &width, &height);
  if (app->pixmap_width == width && app->pixmap_height == height)
    return TRUE;

  for (i = 0, num_pixmaps = 0; i < G_N_ELEMENTS (pixmaps); i++) {
    GstVaapiPixmap *const pixmap =
        video_output_create_pixmap (app->display, GST_VIDEO_FORMAT_xRGB,
        width, height);
    if (!pixmap)
      goto end;
    pixmaps[num_pixmaps++] = pixmap;
  }

  for (i = 0; i < num_pixmaps; i++)
    gst_vaapi_pixmap_replace (&app->pixmaps[i], pixmaps[i]);
  app->pixmap_width = width;
  app->pixmap_height = height;
  success = TRUE;

end:
  for (i = 0; i < num_pixmaps; i++)
    gst_vaapi_pixmap_replace (&pixmaps[i], NULL);
  return success;
}
static gboolean
gst_vaapi_window_wayland_render (GstVaapiWindow * window,
                                 GstVaapiSurface * surface,
                                 const GstVaapiRectangle * src_rect,
                                 const GstVaapiRectangle * dst_rect, guint flags)
{
    GstVaapiWindowWaylandPrivate *const priv =
        GST_VAAPI_WINDOW_WAYLAND_GET_PRIVATE (window);
    GstVaapiDisplay *const display = GST_VAAPI_OBJECT_DISPLAY (window);
    struct wl_display *const wl_display =
        GST_VAAPI_OBJECT_NATIVE_DISPLAY (window);
    struct wl_buffer *buffer;
    FrameState *frame;
    guint width, height, va_flags;
    VAStatus status;
    gboolean need_vpp = FALSE;

    /* Check that we don't need to crop source VA surface */
    gst_vaapi_surface_get_size (surface, &width, &height);
    if (src_rect->x != 0 || src_rect->y != 0)
        need_vpp = TRUE;
    if (src_rect->width != width || src_rect->height != height)
        need_vpp = TRUE;

    /* Check that we don't render to a subregion of this window */
    if (dst_rect->x != 0 || dst_rect->y != 0)
        need_vpp = TRUE;
    if (dst_rect->width != window->width || dst_rect->height != window->height)
        need_vpp = TRUE;

    /* Try to construct a Wayland buffer from VA surface as is (without VPP) */
    if (!need_vpp) {
        GST_VAAPI_OBJECT_LOCK_DISPLAY (window);
        va_flags = from_GstVaapiSurfaceRenderFlags (flags);
        status = vaGetSurfaceBufferWl (GST_VAAPI_DISPLAY_VADISPLAY (display),
                                       GST_VAAPI_OBJECT_ID (surface),
                                       va_flags & (VA_TOP_FIELD | VA_BOTTOM_FIELD), &buffer);
        GST_VAAPI_OBJECT_UNLOCK_DISPLAY (window);
        if (status == VA_STATUS_ERROR_FLAG_NOT_SUPPORTED)
            need_vpp = TRUE;
        else if (!vaapi_check_status (status, "vaGetSurfaceBufferWl()"))
            return FALSE;
    }

    /* Try to construct a Wayland buffer with VPP */
    if (need_vpp) {
        if (priv->use_vpp) {
            GstVaapiSurface *const vpp_surface =
                vpp_convert (window, surface, src_rect, dst_rect, flags);
            if (G_UNLIKELY (!vpp_surface))
                need_vpp = FALSE;
            else {
                surface = vpp_surface;
                width = window->width;
                height = window->height;
            }
        }

        GST_VAAPI_OBJECT_LOCK_DISPLAY (window);
        status = vaGetSurfaceBufferWl (GST_VAAPI_DISPLAY_VADISPLAY (display),
                                       GST_VAAPI_OBJECT_ID (surface), VA_FRAME_PICTURE, &buffer);
        GST_VAAPI_OBJECT_UNLOCK_DISPLAY (window);
        if (!vaapi_check_status (status, "vaGetSurfaceBufferWl()"))
            return FALSE;
    }

    /* Wait for the previous frame to complete redraw */
    if (!gst_vaapi_window_wayland_sync (window)) {
        wl_buffer_destroy (buffer);
        return !priv->sync_failed;
    }

    frame = frame_state_new (window);
    if (!frame)
        return FALSE;
    g_atomic_pointer_set (&priv->last_frame, frame);
    g_atomic_int_inc (&priv->num_frames_pending);

    if (need_vpp && priv->use_vpp) {
        frame->surface = surface;
        frame->surface_pool = gst_vaapi_video_pool_ref (priv->surface_pool);
    }

    /* XXX: attach to the specified target rectangle */
    GST_VAAPI_OBJECT_LOCK_DISPLAY (window);
    wl_surface_attach (priv->surface, buffer, 0, 0);
    wl_surface_damage (priv->surface, 0, 0, width, height);

    if (priv->opaque_region) {
        wl_surface_set_opaque_region (priv->surface, priv->opaque_region);
        wl_region_destroy (priv->opaque_region);
        priv->opaque_region = NULL;
    }

    wl_proxy_set_queue ((struct wl_proxy *) buffer, priv->event_queue);
    wl_buffer_add_listener (buffer, &frame_buffer_listener, frame);

    frame->callback = wl_surface_frame (priv->surface);
    wl_callback_add_listener (frame->callback, &frame_callback_listener, frame);

    wl_surface_commit (priv->surface);
    wl_display_flush (wl_display);
    GST_VAAPI_OBJECT_UNLOCK_DISPLAY (window);
    return TRUE;
}
static gboolean
gst_vaapi_window_wayland_render(
    GstVaapiWindow          *window,
    GstVaapiSurface         *surface,
    const GstVaapiRectangle *src_rect,
    const GstVaapiRectangle *dst_rect,
    guint                    flags
)
{
    GstVaapiWindowWaylandPrivate * const priv =
        GST_VAAPI_WINDOW_WAYLAND(window)->priv;
    GstVaapiDisplay * const display = GST_VAAPI_OBJECT_DISPLAY(window);
    struct wl_display * const wl_display = GST_VAAPI_OBJECT_WL_DISPLAY(window);
    struct wl_buffer *buffer;
    struct wl_callback *callback;
    guint width, height, va_flags;
    VASurfaceID surface_id;
    VAStatus status;

    /* XXX: use VPP to support unusual source and destination rectangles */
    gst_vaapi_surface_get_size(surface, &width, &height);
    if (src_rect->x      != 0     ||
            src_rect->y      != 0     ||
            src_rect->width  != width ||
            src_rect->height != height) {
        GST_ERROR("unsupported source rectangle for rendering");
        return FALSE;
    }

    if (0 && (dst_rect->width != width || dst_rect->height != height)) {
        GST_ERROR("unsupported target rectangle for rendering");
        return FALSE;
    }

    surface_id = GST_VAAPI_OBJECT_ID(surface);
    if (surface_id == VA_INVALID_ID)
        return FALSE;

    GST_VAAPI_OBJECT_LOCK_DISPLAY(window);

    /* Wait for the previous frame to complete redraw */
    if (priv->redraw_pending)
        wl_display_iterate(wl_display, WL_DISPLAY_READABLE);

    /* XXX: use VA/VPP for other filters */
    va_flags = from_GstVaapiSurfaceRenderFlags(flags);
    status = vaGetSurfaceBufferWl(
                 GST_VAAPI_DISPLAY_VADISPLAY(display),
                 surface_id,
                 va_flags & (VA_TOP_FIELD|VA_BOTTOM_FIELD),
                 &buffer
             );
    if (status == VA_STATUS_ERROR_FLAG_NOT_SUPPORTED) {
        /* XXX: de-interlacing flags not supported, try with VPP? */
        status = vaGetSurfaceBufferWl(
                     GST_VAAPI_DISPLAY_VADISPLAY(display),
                     surface_id,
                     VA_FRAME_PICTURE,
                     &buffer
                 );
    }
    if (!vaapi_check_status(status, "vaGetSurfaceBufferWl()"))
        return FALSE;

    /* XXX: attach to the specified target rectangle */
    wl_surface_attach(priv->surface, buffer, 0, 0);
    wl_surface_damage(priv->surface, 0, 0, width, height);

    wl_display_iterate(wl_display, WL_DISPLAY_WRITABLE);
    priv->redraw_pending = TRUE;
    priv->buffer = buffer;

    callback = wl_surface_frame(priv->surface);
    wl_callback_add_listener(callback, &frame_callback_listener, priv);
    GST_VAAPI_OBJECT_UNLOCK_DISPLAY(window);
    return TRUE;
}