+static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource)
+{
+ wl_resource_destroy(resource);
+}
+
+static void
+_tdm_voutput_cb_set_available_modes(struct wl_client *client,
+ struct wl_resource *resource,
+ struct wl_array *modes)
+{
+ tdm_server_voutput_info *voutput_info;
+ tdm_output_mode *mode;
+ int size, count = 0, i = 0;
+
+ voutput_info = wl_resource_get_user_data(resource);
+
+ voutput_info->available_modes.count = 0;
+ if (voutput_info->available_modes.modes)
+ free(voutput_info->available_modes.modes);
+
+ wl_array_for_each(mode, modes)
+ count++;
+ size = sizeof(tdm_output_mode);
+
+ voutput_info->available_modes.modes = malloc(count * size);
+ voutput_info->available_modes.count = count;
+
+ wl_array_for_each(mode, modes)
+ memcpy(&voutput_info->available_modes.modes[i++], mode, size);
+}
+
+static void
+_tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource,
+ unsigned int mmwidth, unsigned int mmheight)
+{
+ tdm_server_voutput_info *voutput_info;
+
+ voutput_info = wl_resource_get_user_data(resource);
+
+ voutput_info->mmwidth = mmwidth;
+ voutput_info->mmheight = mmheight;
+}
+
+static void
+_tdm_voutput_cb_set_mode(struct wl_client *client, struct wl_resource *resource, unsigned int index)
+{
+ tdm_server_voutput_info *voutput_info = NULL;
+ tdm_output *output = NULL;
+ tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
+ const tdm_output_mode *modes, *mode, *current_mode;
+ tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
+ int count = 0;
+ tdm_error ret;
+
+ voutput_info = wl_resource_get_user_data(resource);
+ TDM_RETURN_IF_FAIL(voutput_info != NULL);
+ output = voutput_info->output;
+
+ ret = tdm_output_get_conn_status(output, &status);
+ TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
+ TDM_RETURN_IF_FAIL(status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED);
+
+ ret = tdm_output_get_available_modes(output, &modes, &count);
+ TDM_RETURN_IF_FAIL(index < count);
+
+ mode = &modes[index];
+ TDM_DBG("mode set request to index:%d (%dx%d, %d)", index, mode->hdisplay, mode->vdisplay, mode->vrefresh);
+
+ ret = tdm_output_get_mode(output, ¤t_mode);
+ TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
+ if (mode == current_mode) {
+ TDM_DBG("same mode");
+ return;
+ }
+
+ if (current_mode && ((mode->hdisplay != current_mode->hdisplay) || (mode->vdisplay != current_mode->vdisplay))) {
+ LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
+ if (vb->wl_buffer == voutput_info->attach_buffer->wl_buffer)
+ voutput_info->attach_buffer->need_reset = 1;
+ else
+ wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, vb->wl_buffer);
+ }
+ }
+
+ tdm_output_request_mode_set(voutput_info->output, index);
+}
+
+static void
+_tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
+{
+ tdm_server_voutput_info *voutput_info;
+
+ voutput_info = wl_resource_get_user_data(resource);
+ voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
+
+ voutput_info->request_commit = 1;
+
+ tdm_voutput_set_physical_size(voutput_info->voutput, voutput_info->mmwidth, voutput_info->mmheight);
+ tdm_voutput_set_available_mode(voutput_info->voutput, voutput_info->available_modes.modes, voutput_info->available_modes.count);
+ tdm_voutput_connect(voutput_info->voutput);
+ tdm_output_set_voutput_commit(voutput_info->voutput);
+}
+
+static void
+_tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource)
+{
+ tdm_server_voutput_info *voutput_info;
+
+ voutput_info = wl_resource_get_user_data(resource);
+ voutput_info->status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
+
+ /* Do free resources when it's being disconnected */
+ free(voutput_info->available_modes.modes);
+ voutput_info->available_modes.modes = NULL;
+ voutput_info->available_modes.count = 0;
+ voutput_info->mmwidth = 0;
+ voutput_info->mmheight = 0;
+
+ if (voutput_info->request_commit == 1) {
+ tdm_output_unset_voutput_commit(voutput_info->voutput);
+ voutput_info->request_commit = 0;
+ }
+
+ if (voutput_info->attach_buffer) {
+ tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
+ tbm_surface_internal_unref(buffer);
+ voutput_info->committing = 0;
+ if (voutput_info->attach_buffer->need_reset)
+ wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, voutput_info->attach_buffer->wl_buffer);
+ voutput_info->attach_buffer = NULL;
+ tdm_voutput_commit_done(voutput_info->voutput);
+ }
+
+ tdm_voutput_disconnect(voutput_info->voutput);
+}
+
+static void
+_tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource)
+{
+ tdm_server_voutput_info *voutput_info;
+ tbm_surface_h buffer;
+
+ voutput_info = wl_resource_get_user_data(resource);
+ if (voutput_info->status != TDM_OUTPUT_CONN_STATUS_CONNECTED) {
+ TDM_DBG("not connected.");
+ return;
+ }
+
+ buffer = voutput_info->attach_buffer->buffer;
+ tbm_surface_internal_unref(buffer);
+ voutput_info->committing = 0;
+ if (voutput_info->attach_buffer->need_reset)
+ wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, voutput_info->attach_buffer->wl_buffer);
+ voutput_info->attach_buffer = NULL;
+
+ if (voutput_info->request_commit == 1)
+ tdm_output_set_voutput_commit(voutput_info->voutput);
+
+ tdm_voutput_commit_done(voutput_info->voutput);
+}
+
+static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
+ _tdm_voutput_cb_destroy,
+ _tdm_voutput_cb_set_available_modes,
+ _tdm_voutput_cb_set_physical_size,
+ _tdm_voutput_cb_set_mode,
+ _tdm_voutput_cb_connect,
+ _tdm_voutput_cb_disconnect,
+ _tdm_voutput_cb_commit_done
+};
+
+static void
+_tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer)
+{
+ wl_resource_destroy(wl_buffer);
+}
+
+static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = {
+ _tdm_voutput_wl_buffer_destroy
+};
+
+static void
+_tdm_voutput_buffer_destory(struct wl_resource *wl_buffer)
+{
+ tdm_server_voutput_info *voutput_info = NULL;
+ tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
+
+ voutput_info = (tdm_server_voutput_info *)wl_resource_get_user_data(wl_buffer);
+ TDM_RETURN_IF_FAIL(voutput_info != NULL);
+
+ LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
+ if (vb->wl_buffer == wl_buffer) {
+ tbm_surface_internal_unref(vb->buffer);
+ wl_resource_set_user_data(wl_buffer, NULL);
+ LIST_DEL(&vb->link);
+ free(vb);
+ }
+ }
+}
+
+struct wl_resource *
+_tdm_voutput_create_wl_buffer(tdm_server_voutput_info *voutput_info)
+{
+ struct wl_client *wl_client;
+ struct wl_resource *wl_buffer = NULL;
+
+ wl_client = wl_resource_get_client(voutput_info->resource);
+
+ /* create a wl_buffer resource */
+ wl_buffer = wl_resource_create(wl_client, &wl_buffer_interface, 1, 0);
+ TDM_RETURN_VAL_IF_FAIL(wl_buffer != NULL, NULL);
+
+ wl_resource_set_implementation(wl_buffer,
+ (void (**)(void)) &_tdm_voutput_buffer_impementation,
+ voutput_info, _tdm_voutput_buffer_destory);
+
+ return wl_buffer;
+}
+
+struct wl_resource *
+_tdm_voutput_export_buffer(tdm_server_voutput_info *voutput_info,
+ tbm_surface_h buffer)
+{
+ int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
+ struct wl_resource *wl_buffer = NULL;
+ int num_buf, is_fd = -1, i;
+ tbm_surface_info_s info;
+ uint32_t flags = 0;
+ struct wl_array plane_buf_idx, plane_offset, plane_stride, plane_size;
+ int *p;
+
+ TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, NULL);
+ TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
+
+ if (tbm_surface_get_info(buffer, &info) != TBM_SURFACE_ERROR_NONE) {
+ TDM_ERR("Failed to create buffer from surface");
+ return NULL;
+ }
+
+ if (info.num_planes > 3) {
+ TDM_ERR("invalid num_planes(%d)", info.num_planes);
+ return NULL;
+ }
+
+ num_buf = tbm_surface_internal_get_num_bos(buffer);
+ if (num_buf == 0) {
+ TDM_ERR("surface doesn't have any bo.");
+ return NULL;
+ }
+
+ for (i = 0; i < num_buf; i++) {
+ tbm_bo bo = tbm_surface_internal_get_bo(buffer, i);
+ if (bo == NULL) {
+ TDM_ERR("Failed to get bo from surface");
+ goto err;
+ }
+
+ /* try to get fd first */
+ if (is_fd == -1 || is_fd == 1) {
+ bufs[i] = tbm_bo_export_fd(bo);
+ if (is_fd == -1 && bufs[i] >= 0)
+ is_fd = 1;
+ }
+
+ /* if fail to get fd, try to get name second */
+ if (is_fd == -1 || is_fd == 0) {
+ bufs[i] = tbm_bo_export(bo);
+ if (is_fd == -1 && bufs[i] > 0)
+ is_fd = 0;
+ }
+
+ if (is_fd == -1 ||
+ (is_fd == 1 && bufs[i] < 0) ||
+ (is_fd == 0 && bufs[i] <= 0)) {
+ TDM_ERR("Failed to export(is_fd:%d, bufs:%d)", is_fd, bufs[i]);
+ goto err;
+ }
+ }
+
+ wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info);
+ if (!wl_buffer) {
+ TDM_ERR("Failed to create wl_buffer");
+ goto err;
+ }
+
+ wl_array_init(&plane_buf_idx);
+ wl_array_init(&plane_offset);
+ wl_array_init(&plane_stride);
+ wl_array_init(&plane_size);
+
+ for (i = 0; i < 3; i++) {
+ p = wl_array_add(&plane_buf_idx, sizeof(int));
+ *p = tbm_surface_internal_get_plane_bo_idx(buffer, i);
+ p = wl_array_add(&plane_offset, sizeof(int));
+ *p = info.planes[i].offset;
+ p = wl_array_add(&plane_stride, sizeof(int));
+ *p = info.planes[i].stride;
+ p = wl_array_add(&plane_size, sizeof(int));
+ *p = info.planes[i].size;
+ }
+
+ if (is_fd == 1)
+ wl_tdm_voutput_send_buffer_set_with_fd(voutput_info->resource,
+ wl_buffer,
+ info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
+ &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
+ flags, num_buf, bufs[0],
+ (bufs[1] == -1) ? bufs[0] : bufs[1],
+ (bufs[2] == -1) ? bufs[0] : bufs[2]);
+ else
+ wl_tdm_voutput_send_buffer_set_with_id(voutput_info->resource,
+ wl_buffer,
+ info.width, info.height, info.format, info.bpp, info.size, info.num_planes,
+ &plane_buf_idx, &plane_offset, &plane_stride, &plane_size,
+ flags,
+ num_buf, bufs[0], bufs[1], bufs[2]);
+
+ wl_array_release(&plane_buf_idx);
+ wl_array_release(&plane_offset);
+ wl_array_release(&plane_stride);
+ wl_array_release(&plane_size);
+
+ for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
+ if (is_fd == 1 && (bufs[i] >= 0))
+ close(bufs[i]);
+ }
+
+ return wl_buffer;
+
+err:
+ for (i = 0; i < TBM_SURF_PLANE_MAX; i++) {
+ if (is_fd == 1 && (bufs[i] >= 0))
+ close(bufs[i]);
+ }
+
+ return NULL;
+}
+
+tdm_server_voutput_buffer *
+_tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surface_h buffer)
+{
+ tdm_server_voutput_buffer *voutput_buffer = NULL, *vb = NULL;
+
+ LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
+ if (vb && vb->buffer == buffer) {
+ return vb;
+ }
+ }
+
+ tbm_surface_internal_ref(buffer);
+ voutput_buffer = calloc(1, sizeof *voutput_buffer);
+ if (!voutput_buffer) {
+ /* LCOV_EXCL_START */
+
+ TDM_ERR("fail calloc");
+ tbm_surface_internal_unref(buffer);
+ return NULL;
+
+ /* LCOV_EXCL_STOP */
+ }
+
+ voutput_buffer->wl_buffer = _tdm_voutput_export_buffer(voutput_info, buffer);
+ if (!voutput_buffer->wl_buffer) {
+ /* LCOV_EXCL_START */
+
+ TDM_ERR("fail export buffer");
+ free(voutput_buffer);
+ tbm_surface_internal_unref(buffer);
+ return NULL;
+
+ /* LCOV_EXCL_STOP */
+ }
+
+ voutput_buffer->buffer = buffer;
+ LIST_ADDTAIL(&voutput_buffer->link, &voutput_info->buffer_list);
+
+ return voutput_buffer;
+}
+
+tdm_error
+tdm_voutput_attach_buffer(tdm_voutput *voutput, tbm_surface_h buffer)
+{
+ tdm_private_server *private_server = keep_private_server;
+ tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
+ tdm_server_voutput_buffer *voutput_buffer = NULL;
+
+ TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
+
+ LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
+ if (vo && vo->voutput == voutput) {
+ voutput_info = vo;
+ break;
+ }
+ }
+ TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
+ TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer == NULL, TDM_ERROR_OPERATION_FAILED);
+
+ voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, buffer);
+ TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY);
+
+ voutput_info->attach_buffer = voutput_buffer;
+
+ wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
+
+ return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_voutput_commit_buffer(tdm_voutput *voutput)
+{
+ tdm_private_server *private_server = keep_private_server;
+ tdm_server_voutput_info *voutput_info = NULL, *vo = NULL;
+
+ TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED);
+
+ LIST_FOR_EACH_ENTRY(vo, &private_server->voutput_list, link) {
+ if (vo && vo->voutput == voutput) {
+ voutput_info = vo;
+ break;
+ }
+ }
+ TDM_RETURN_VAL_IF_FAIL(voutput_info != NULL, TDM_ERROR_INVALID_PARAMETER);
+ TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
+ TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
+
+ voutput_info->committing = 1;
+
+ wl_tdm_voutput_send_commit(voutput_info->resource);
+
+ return TDM_ERROR_NONE;
+}
+
+void
+tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
+{
+ tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
+ tdm_server_voutput_buffer *vb = NULL;
+ tdm_voutput *voutput;
+ tdm_error ret = TDM_ERROR_NONE;
+
+ TDM_RETURN_IF_FAIL(voutput_info != NULL);
+
+ voutput = voutput_info->voutput;
+
+ if (voutput_info->request_commit)
+ tdm_output_unset_voutput_commit(voutput_info->voutput);
+
+ if (voutput) {
+ if (voutput_info->request_commit == 1) {
+ tdm_output_unset_voutput_commit(voutput_info->voutput);
+ voutput_info->request_commit = 0;
+ tdm_voutput_disconnect(voutput_info->voutput);
+ }
+
+ ret = tdm_voutput_destroy(voutput);
+ if (ret != TDM_ERROR_NONE)
+ TDM_ERR("_tdm_voutput_cb_destroy fail");
+ }
+
+ if (voutput_info->attach_buffer) {
+ tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
+ tbm_surface_internal_unref(buffer);
+ voutput_info->committing = 0;
+ voutput_info->attach_buffer = NULL;
+ }
+
+ LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
+ if (vb->wl_buffer)
+ wl_resource_destroy(vb->wl_buffer);
+ }
+
+ LIST_DEL(&voutput_info->link);
+
+ /* Do free your own resource */
+ free(voutput_info);
+}
+
+static void
+_tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resource *resource, const char *name, uint32_t id)
+{
+ struct wl_resource *voutput_resource = NULL;
+ tdm_private_server *private_server = wl_resource_get_user_data(resource);
+ tdm_server_voutput_info *voutput_info;
+ tdm_voutput *voutput;
+ tdm_output *output;
+ tdm_error ret;
+
+ output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
+ if (output) {
+ TDM_ERR("There is '%s' output, cannot create.", name);
+ wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
+ "There is '%s' output", name);
+ return;
+ }
+
+ voutput = tdm_display_voutput_create(private_server->private_loop->dpy, name, &ret);
+ if (!voutput) {
+ TDM_ERR("voutput creation fail(%s)(%d).", name, ret);
+ wl_resource_post_error(resource, WL_DISPLAY_ERROR_NO_MEMORY,
+ "%s output creation fail", name);
+ return;
+ }
+
+ output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL);
+ if (!output) {
+ TDM_ERR("There is no '%s' output.", name);
+ wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
+ "There is '%s' output", name);
+ return;
+ }
+
+ voutput_resource =
+ wl_resource_create(client, &wl_tdm_voutput_interface,
+ wl_resource_get_version(resource), id);
+ if (!voutput_resource) {
+ /* LCOV_EXCL_START */
+
+ wl_resource_post_no_memory(resource);
+ TDM_ERR("wl_resource_create failed");
+ return;
+
+ /* LCOV_EXCL_STOP */
+ }
+
+ voutput_info = calloc(1, sizeof * voutput_info);
+ if (!voutput_info) {
+ /* LCOV_EXCL_START */
+
+ wl_resource_post_no_memory(resource);
+ wl_resource_destroy(voutput_resource);
+ TDM_ERR("alloc failed");
+ return;
+
+ /* LCOV_EXCL_STOP */
+ }
+
+ LIST_ADDTAIL(&voutput_info->link, &private_server->voutput_list);
+ voutput_info->private_server = private_server;
+ voutput_info->resource = voutput_resource;
+ voutput_info->voutput = voutput;
+ voutput_info->output = output;
+ LIST_INITHEAD(&voutput_info->buffer_list);
+
+ wl_resource_set_implementation(voutput_resource,
+ &tdm_voutput_implementation,
+ voutput_info,
+ tdm_voutput_cb_resource_destroy);
+
+ wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
+}
+
+/* LCOV_EXCL_START */