X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Ftdm_server.c;h=25ad51df576177890fb0b685f650f0c404960daa;hb=d077eef0042ab7dbc6857ce8e2ec367b3fe90b2e;hp=15d856e529a5795bff981590c4a8949f672fd7f7;hpb=2d3906b8341b46fc6cf852316c99e3c3c3d164ab;p=platform%2Fcore%2Fuifw%2Flibtdm.git diff --git a/src/tdm_server.c b/src/tdm_server.c index 15d856e..25ad51d 100644 --- a/src/tdm_server.c +++ b/src/tdm_server.c @@ -1,355 +1,1538 @@ /************************************************************************** + * + * libtdm + * + * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: Eunchul Kim , + * JinYoung Jeon , + * Taeheon Kim , + * YoungJun Cho , + * SooChan Lim , + * Boram Park + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * +**************************************************************************/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#define WL_HIDE_DEPRECATED + +#include + +#include "tdm_private.h" + +/* CAUTION: + * - tdm server doesn't care about thread things. + * - DO NOT use the TDM internal functions here. + * However, the internal function which does lock/unlock the mutex of + * private_display in itself can be called. + * - DO NOT use the tdm_private_display structure here. + * - The callback function things can be called in main thread. + */ + +struct _tdm_private_server { + tdm_private_loop *private_loop; + struct list_head output_list; + struct list_head voutput_list; + struct list_head wait_list; +}; + +typedef struct _tdm_server_output_info { + struct list_head link; + tdm_private_server *private_server; + struct wl_resource *resource; + tdm_output *output; + struct list_head vblank_list; + unsigned int watch_output_changes; +} tdm_server_output_info; + +typedef struct _tdm_server_voutput_buffer { + struct list_head link; + struct wl_resource *wl_buffer; + tbm_surface_h buffer; + int need_reset; +} tdm_server_voutput_buffer; + +typedef struct _tdm_server_voutput_info { + struct list_head link; + tdm_private_server *private_server; + struct wl_resource *resource; + tdm_voutput *voutput; + tdm_output *output; + + tdm_output_conn_status status; + struct { + int count; + tdm_output_mode *modes; + } available_modes; + + unsigned int mmwidth; + unsigned int mmheight; + + struct list_head buffer_list; + tdm_server_voutput_buffer *attach_buffer; + int committing; + unsigned int request_commit; +} tdm_server_voutput_info; + +typedef struct _tdm_server_vblank_info { + struct list_head link; + tdm_server_output_info *output_info; + struct wl_resource *resource; + + tdm_vblank *vblank; + unsigned int stamp; + + /* for timeout */ + tdm_event_loop_source *vblank_timeout_timer; +} tdm_server_vblank_info; + +typedef struct _tdm_server_wait_info { + struct list_head link; + tdm_server_vblank_info *vblank_info; + + unsigned int req_id; + double req_time; + + unsigned int timeout; +} tdm_server_wait_info; + +typedef struct _tdm_server_client_info { + struct list_head link; + pid_t pid; + char name[TDM_NAME_LEN]; + struct wl_resource *resource; +} tdm_server_client_info; + +static tdm_private_server *keep_private_server; +static struct list_head client_list; + +static void destroy_wait(tdm_server_wait_info *wait_info); +static void _tdm_server_vblank_timeout_update(tdm_server_vblank_info *vblank_info, int ms_delay); + +static void +_tdm_server_get_process_name(pid_t pid, char *name, unsigned int size) +{ + char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN]; + FILE *h; + size_t len; + + snprintf(name, size, "Unknown"); + + snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid); + h = fopen(proc, "r"); + if (!h) + return; + + len = fread(pname, sizeof(char), TDM_NAME_LEN, h); + if (len == 0) { + strncpy(pname, "NO NAME", 7); + len = 8; + } + pname[len - 1] = '\0'; + + strncpy(name, pname, size - 1); + name[size - 1] = '\0'; + + fclose(h); +} + +/* LCOV_EXCL_START */ +static void +_tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error, + unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec) +{ + tdm_server_wait_info *found; + tdm_server_vblank_info *vblank_info; + tdm_server_output_info *output_info; + tdm_private_server *private_server; + tdm_private_loop *private_loop; + + TDM_RETURN_IF_FAIL(keep_private_server != NULL); + + LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list, + tdm_server_wait_info, link, found); + if (!found) { + TDM_ERR("wait_info(%p) is destroyed", wait_info); + return; + } + + if (tdm_debug_module & TDM_DEBUG_VBLANK) + TDM_DBG("req_id(%d) done", wait_info->req_id); + + vblank_info = wait_info->vblank_info; + output_info = vblank_info->output_info; + private_server = output_info->private_server; + private_loop = private_server->private_loop; + + tdm_display_lock(private_loop->dpy); + _tdm_server_vblank_timeout_update(vblank_info, 0); + tdm_display_unlock(private_loop->dpy); + + if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK) + TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp); + + if (!wait_info->timeout) + wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id, + sequence, tv_sec, tv_usec, error); + + destroy_wait(wait_info); +} +/* LCOV_EXCL_STOP */ + +/* LCOV_EXCL_START */ +static tdm_error +_tdm_server_timeout_timer_cb(void *user_data) +{ + tdm_server_vblank_info *vblank_info = user_data; + tdm_server_wait_info *wait_info = NULL; + double curr; + unsigned int tv_sec; + unsigned int tv_usec; + + TDM_RETURN_VAL_IF_FAIL(vblank_info != NULL, TDM_ERROR_OPERATION_FAILED); + TDM_RETURN_VAL_IF_FAIL(keep_private_server != NULL, TDM_ERROR_OPERATION_FAILED); + + curr = tdm_helper_get_time(); + tv_sec = TDM_TIME_SEC(curr); + tv_usec = TDM_TIME_USEC(curr); + + LIST_FOR_EACH_ENTRY(wait_info, &keep_private_server->wait_list, link) { + if (wait_info->timeout) continue; + if (vblank_info != wait_info->vblank_info) continue; + + wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id, + 0, tv_sec, tv_usec, TDM_ERROR_TIMEOUT); + TDM_ERR("tdm_server_vblank(%p) req_id(%d) timeout force send vblank", vblank_info, wait_info->req_id); + wait_info->timeout = 1; + } + + return TDM_ERROR_NONE; +} + +static void +_tdm_server_vblank_timeout_update(tdm_server_vblank_info *vblank_info, int ms_delay) +{ + tdm_server_output_info *output_info = vblank_info->output_info; + tdm_private_server *private_server = output_info->private_server; + tdm_private_loop *private_loop = private_server->private_loop; + tdm_error ret; + + if (!vblank_info->vblank_timeout_timer) { + vblank_info->vblank_timeout_timer = + tdm_event_loop_add_timer_handler(private_loop->dpy, + _tdm_server_timeout_timer_cb, + vblank_info, + &ret); + if (!vblank_info->vblank_timeout_timer) { + TDM_ERR("tdm_server_vblank(%p) couldn't add timer", vblank_info); + return; + } + + if (tdm_debug_module & TDM_DEBUG_VBLANK) + TDM_INFO("tdm_server_vblank(%p) create vblank timeout timer", vblank_info); + } + + ret = tdm_event_loop_source_timer_update(vblank_info->vblank_timeout_timer, ms_delay); + if (ret != TDM_ERROR_NONE) { + TDM_ERR("tdm_server_vblank(%p) couldn't update timer", vblank_info); + return; + } +} + +static void +_tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence, + unsigned int tv_sec, unsigned int tv_usec, void *user_data) +{ + _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec); +} +/* LCOV_EXCL_STOP */ + +/* LCOV_EXCL_START */ +static void +_tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type, + tdm_value value, void *user_data) +{ + tdm_server_output_info *output_info = user_data; + struct wl_client *client; + pid_t pid = 0; + tdm_output_conn_status status; + tdm_error ret = TDM_ERROR_NONE; + + TDM_RETURN_IF_FAIL(output_info != NULL); + + client = wl_resource_get_client(output_info->resource); + TDM_RETURN_IF_FAIL(client != NULL); + + wl_client_get_credentials(client, &pid, NULL, NULL); + + if (!output_info->watch_output_changes) { + TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid); + return; + } + + TDM_DBG("send the output changes: %d, type:%d, value:%d", (unsigned int)pid, type, value.u32); + + switch (type) { + case TDM_OUTPUT_CHANGE_DPMS: + wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE); + break; + case TDM_OUTPUT_CHANGE_CONNECTION: + status = value.u32; + if (status == TDM_OUTPUT_CONN_STATUS_MODE_SETTED) { + const tdm_output_mode *mode = NULL; + unsigned int hdisplay, vdisplay, vrefresh; + + ret = tdm_output_get_mode(output_info->output, &mode); + if (ret == TDM_ERROR_NONE) { + hdisplay = (mode) ? mode->hdisplay : 0; + vdisplay = (mode) ? mode->vdisplay : 0; + vrefresh = (mode) ? mode->vrefresh : 0; + + wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret); + } else { + wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret); + } + } + wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE); + break; + default: + break; + } +} +/* LCOV_EXCL_STOP */ + +static void +destroy_wait(tdm_server_wait_info *wait_info) +{ + LIST_DEL(&wait_info->link); + free(wait_info); +} + +static void +destroy_vblank_callback(struct wl_resource *resource) +{ + tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); + tdm_server_output_info *output_info = vblank_info->output_info; + tdm_private_server *private_server = output_info->private_server; + tdm_private_loop *private_loop = private_server->private_loop; + tdm_server_wait_info *w = NULL, *ww = NULL; + + TDM_RETURN_IF_FAIL(vblank_info != NULL); + + if (vblank_info->vblank_timeout_timer) { + tdm_display_lock(private_loop->dpy); + tdm_event_loop_source_remove(vblank_info->vblank_timeout_timer); + tdm_display_unlock(private_loop->dpy); + } + + LIST_DEL(&vblank_info->link); + + if (vblank_info->vblank) + tdm_vblank_destroy(vblank_info->vblank); + + LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) { + if (w->vblank_info == vblank_info) + destroy_wait(w); + } + + free(vblank_info); +} + +/* LCOV_EXCL_START */ +static void +_tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource) +{ + wl_resource_destroy(resource); +} +/* LCOV_EXCL_STOP */ + +/* LCOV_EXCL_START */ +static void +_tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name) +{ + tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); + tdm_error ret; + + ret = tdm_vblank_set_name(vblank_info->vblank, name); + TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE); +} +/* LCOV_EXCL_STOP */ + +/* LCOV_EXCL_START */ +static void +_tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps) +{ + tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); + tdm_error ret; + + ret = tdm_vblank_set_fps(vblank_info->vblank, fps); + TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE); +} +/* LCOV_EXCL_STOP */ + +/* LCOV_EXCL_START */ +static void +_tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset) +{ + tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); + tdm_error ret; + + ret = tdm_vblank_set_offset(vblank_info->vblank, offset); + TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE); +} +/* LCOV_EXCL_STOP */ + +static void +_tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake) +{ + tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); + tdm_error ret; + + ret = tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake); + TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE); +} + +static void +_tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource, + uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec) +{ + tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); + tdm_server_output_info *output_info = vblank_info->output_info; + tdm_private_server *private_server = output_info->private_server; + tdm_private_loop *private_loop = private_server->private_loop; + tdm_server_wait_info *wait_info; + unsigned int enable_fake = 0; + tdm_error ret; + + wait_info = calloc(1, sizeof * wait_info); + if (!wait_info) { + /* LCOV_EXCL_START */ + + TDM_ERR("alloc failed"); + ret = TDM_ERROR_OUT_OF_MEMORY; + goto wait_failed; + + /* LCOV_EXCL_STOP */ + } + + LIST_ADDTAIL(&wait_info->link, &private_server->wait_list); + wait_info->vblank_info = vblank_info; + wait_info->req_id = req_id; + wait_info->req_time = TDM_TIME(req_sec, req_usec); + + if (tdm_debug_module & TDM_DEBUG_VBLANK) + TDM_DBG("req_id(%d) wait", req_id); + + if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK) + TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp); + + ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info); + + tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake); + if (!enable_fake && ret == TDM_ERROR_DPMS_OFF) + goto wait_failed; + + TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed); + + tdm_display_lock(private_loop->dpy); + _tdm_server_vblank_timeout_update(vblank_info, 1000); + tdm_display_unlock(private_loop->dpy); + + return; +wait_failed: + /* LCOV_EXCL_START */ + + wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret); + if (wait_info) + destroy_wait(wait_info); + + /* LCOV_EXCL_STOP */ +} + +static void +_tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource, + uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec) +{ + tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); + tdm_server_output_info *output_info = vblank_info->output_info; + tdm_private_server *private_server = output_info->private_server; + tdm_server_wait_info *wait_info; + unsigned int enable_fake = 0; + tdm_error ret; + + wait_info = calloc(1, sizeof * wait_info); + if (!wait_info) { + /* LCOV_EXCL_START */ + + TDM_ERR("alloc failed"); + ret = TDM_ERROR_OUT_OF_MEMORY; + goto wait_failed; + + /* LCOV_EXCL_STOP */ + } + + LIST_ADDTAIL(&wait_info->link, &private_server->wait_list); + wait_info->vblank_info = vblank_info; + wait_info->req_id = req_id; + wait_info->req_time = TDM_TIME(req_sec, req_usec); + + if (tdm_debug_module & TDM_DEBUG_VBLANK) + TDM_DBG("req_id(%d) wait", req_id); + + if (tdm_ttrace_module & TDM_TTRACE_SERVER_VBLANK) + TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Server_Vblank:%u", vblank_info->stamp); + + ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info); + + tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake); + if (!enable_fake && ret == TDM_ERROR_DPMS_OFF) + goto wait_failed; + + TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed); + + return; +wait_failed: + /* LCOV_EXCL_START */ + + wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret); + if (wait_info) + destroy_wait(wait_info); + + /* LCOV_EXCL_STOP */ +} + +static const struct wl_tdm_vblank_interface tdm_vblank_implementation = { + _tdm_server_vblank_cb_destroy, + _tdm_server_vblank_cb_set_name, + _tdm_server_vblank_cb_set_fps, + _tdm_server_vblank_cb_set_offset, + _tdm_server_vblank_cb_set_enable_fake, + _tdm_server_vblank_cb_wait_vblank, + _tdm_server_vblank_cb_wait_vblank_seq, +}; + +/* LCOV_EXCL_START */ +static void +_tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource) +{ + wl_resource_destroy(resource); +} +/* LCOV_EXCL_STOP */ + +static void +_tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id) +{ + tdm_server_output_info *output_info = wl_resource_get_user_data(resource); + tdm_private_server *private_server = output_info->private_server; + tdm_private_loop *private_loop = private_server->private_loop; + struct wl_resource *vblank_resource; + tdm_vblank *vblank; + tdm_server_vblank_info *vblank_info; + + vblank_resource = + wl_resource_create(client, &wl_tdm_vblank_interface, + wl_resource_get_version(resource), id); + if (!vblank_resource) { + /* LCOV_EXCL_START */ + + wl_resource_post_no_memory(resource); + TDM_ERR("wl_resource_create failed"); + return; + + /* LCOV_EXCL_STOP */ + } + + vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL); + if (!vblank) { + /* LCOV_EXCL_START */ + + wl_resource_post_no_memory(resource); + wl_resource_destroy(vblank_resource); + TDM_ERR("tdm_vblank_create failed"); + return; + + /* LCOV_EXCL_STOP */ + } + + vblank_info = calloc(1, sizeof * vblank_info); + if (!vblank_info) { + /* LCOV_EXCL_START */ + + wl_resource_post_no_memory(resource); + wl_resource_destroy(vblank_resource); + tdm_vblank_destroy(vblank); + TDM_ERR("alloc failed"); + return; + + /* LCOV_EXCL_STOP */ + } + + LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list); + vblank_info->output_info = output_info; + vblank_info->resource = vblank_resource; + vblank_info->vblank = vblank; + vblank_info->stamp = (unsigned int)tdm_vblank_get_stamp(vblank); + + tdm_vblank_set_resource(vblank, vblank_resource); + + wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation, + vblank_info, destroy_vblank_callback); + + wl_tdm_vblank_send_stamp(vblank_info->resource, vblank_info->stamp); + + if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK) { + tdm_output *output = tdm_display_get_output(private_loop->dpy, tdm_ttrace_output, NULL); + if (output == output_info->output) + wl_tdm_vblank_send_ttrace(vblank_info->resource, 1); + } + + return; +} + +/* LCOV_EXCL_START */ +static void +_tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable) +{ + tdm_server_output_info *output_info = wl_resource_get_user_data(resource); + + TDM_RETURN_IF_FAIL(output_info != NULL); + + output_info->watch_output_changes = enable; +} +/* LCOV_EXCL_STOP */ + +static void +_tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource) +{ + tdm_server_output_info *output_info = wl_resource_get_user_data(resource); + tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED; + tdm_error ret; + + TDM_RETURN_IF_FAIL(output_info != NULL); + + ret = tdm_output_get_conn_status(output_info->output, &status); + TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed); + + wl_tdm_output_send_connection(output_info->resource, status, ret); + + return; + +failed: + wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret); +} + +static void +_tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource) +{ + tdm_server_output_info *output_info = wl_resource_get_user_data(resource); + tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED; + tdm_error ret; + + TDM_RETURN_IF_FAIL(output_info != NULL); + + ret = tdm_output_get_conn_status(output_info->output, &status); + TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed); + + if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) { + const tdm_output_mode *mode = NULL; + unsigned int hdisplay, vdisplay, vrefresh; + + ret = tdm_output_get_mode(output_info->output, &mode); + TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed); + + hdisplay = (mode) ? mode->hdisplay : 0; + vdisplay = (mode) ? mode->vdisplay : 0; + vrefresh = (mode) ? mode->vrefresh : 0; + + wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret); + } else { + wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED); + } + + return; +failed: + wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret); +} + +static void +_tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource) +{ + tdm_server_output_info *output_info = wl_resource_get_user_data(resource); + tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED; + tdm_error ret; + + TDM_RETURN_IF_FAIL(output_info != NULL); + + ret = tdm_output_get_conn_status(output_info->output, &status); + TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed); + + if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) { + tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF; + + ret = tdm_output_get_dpms(output_info->output, &dpms_value); + TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed); + + wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret); + } else { + wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED); + } + + return; +failed: + wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret); +} + +static const struct wl_tdm_output_interface tdm_output_implementation = { + _tdm_server_output_cb_destroy, + _tdm_server_output_cb_create_vblank, + _tdm_server_output_cb_watch_output_changes, + _tdm_server_output_cb_get_connection, + _tdm_server_output_cb_get_mode, + _tdm_server_output_cb_get_dpms, +}; + +static void +destroy_output_callback(struct wl_resource *resource) +{ + tdm_server_output_info *output_info = wl_resource_get_user_data(resource); + tdm_server_vblank_info *v = NULL, *vv = NULL; + + TDM_RETURN_IF_FAIL(output_info != NULL); + + LIST_DEL(&output_info->link); + + tdm_output_remove_change_handler(output_info->output, + _tdm_server_cb_output_change, output_info); + + LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) { + wl_resource_destroy(v->resource); + } + + free(output_info); +} + +static void +_tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource, + const char *name, uint32_t id) +{ + tdm_private_server *private_server = wl_resource_get_user_data(resource); + tdm_server_output_info *output_info; + struct wl_resource *output_resource = NULL; + tdm_output *output; + tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED; + tdm_error ret; + + output = tdm_display_find_output(private_server->private_loop->dpy, name, NULL); + if (!output) { + /* LCOV_EXCL_START */ + + TDM_ERR("There is no '%s' output", name); + wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, + "There is no '%s' output", name); + return; + + /* LCOV_EXCL_STOP */ + } + + output_resource = + wl_resource_create(client, &wl_tdm_output_interface, + wl_resource_get_version(resource), id); + if (!output_resource) { + /* LCOV_EXCL_START */ + + wl_resource_post_no_memory(resource); + TDM_ERR("wl_resource_create failed"); + return; + + /* LCOV_EXCL_STOP */ + } + + output_info = calloc(1, sizeof * output_info); + if (!output_info) { + /* LCOV_EXCL_START */ + + wl_resource_post_no_memory(resource); + wl_resource_destroy(output_resource); + TDM_ERR("alloc failed"); + return; + + /* LCOV_EXCL_STOP */ + } + + ret = tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info); + if (ret != TDM_ERROR_NONE) { + wl_resource_post_no_memory(resource); + wl_resource_destroy(output_resource); + free(output_info); + TDM_ERR("tdm_output_add_change_handler failed"); + return; + } + + LIST_ADDTAIL(&output_info->link, &private_server->output_list); + output_info->private_server = private_server; + output_info->resource = output_resource; + output_info->output = output; + LIST_INITHEAD(&output_info->vblank_list); -libtdm - -Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved. - -Contact: Eunchul Kim , - JinYoung Jeon , - Taeheon Kim , - YoungJun Cho , - SooChan Lim , - Boram Park - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sub license, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice (including the -next paragraph) shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. -IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR -ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + wl_resource_set_implementation(output_resource, &tdm_output_implementation, + output_info, destroy_output_callback); -**************************************************************************/ + ret = tdm_output_get_conn_status(output, &status); + wl_tdm_output_send_connection(output_resource, status, ret); -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif + if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) { + tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF; + const tdm_output_mode *mode = NULL; + unsigned int hdisplay, vdisplay, vrefresh; -#include "tdm.h" -#include "tdm_private.h" -#include "tdm_list.h" -#include "tdm-server-protocol.h" + ret = tdm_output_get_mode(output, &mode); + hdisplay = (mode) ? mode->hdisplay : 0; + vdisplay = (mode) ? mode->vdisplay : 0; + vrefresh = (mode) ? mode->vrefresh : 0; + wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret); -/* CAUTION: - * - tdm server doesn't care about thread things. - * - DO NOT use the TDM internal functions here. - */ + ret = tdm_output_get_dpms(output, &dpms_value); + wl_tdm_output_send_dpms(output_resource, dpms_value, ret); + } else { + wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED); + wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED); + } +} -struct _tdm_private_server { - tdm_private_loop *private_loop; - struct list_head client_list; - struct list_head vblank_list; -}; +static void _tdm_voutput_cb_destroy(struct wl_client *client, struct wl_resource *resource) +{ + wl_resource_destroy(resource); +} -typedef struct _tdm_server_client_info { - struct list_head link; - tdm_private_server *private_server; - struct wl_resource *resource; -} tdm_server_client_info; +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; -typedef struct _tdm_server_vblank_info { - struct list_head link; - tdm_server_client_info *client_info; - struct wl_resource *resource; -} tdm_server_vblank_info; + voutput_info = wl_resource_get_user_data(resource); -static tdm_private_server *keep_private_server; -static int tdm_debug_server; + 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_server_cb_output_vblank(tdm_output *output, unsigned int sequence, - unsigned int tv_sec, unsigned int tv_usec, - void *user_data) +_tdm_voutput_cb_set_physical_size(struct wl_client *client, struct wl_resource *resource, + unsigned int mmwidth, unsigned int mmheight) { - tdm_server_vblank_info *vblank_info = (tdm_server_vblank_info*)user_data; - tdm_server_vblank_info *found; + tdm_server_voutput_info *voutput_info; - if (!keep_private_server) - return; + voutput_info = wl_resource_get_user_data(resource); - LIST_FIND_ITEM(vblank_info, &keep_private_server->vblank_list, - tdm_server_vblank_info, link, found); - if (!found) { - TDM_DBG("vblank_info(%p) is destroyed", vblank_info); + 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 (tdm_debug_server) { - unsigned long curr = tdm_helper_get_time_in_micros(); - unsigned long vtime = (tv_sec * 1000000) + tv_usec; - if (curr - vtime > 1000) /* 1ms */ - TDM_WRN("delay: %d us", (int)(curr - vtime)); + 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_DBG("wl_tdm_vblank@%d done", wl_resource_get_id(vblank_info->resource)); + 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; - wl_tdm_vblank_send_done(vblank_info->resource, sequence, tv_sec, tv_usec); - wl_resource_destroy(vblank_info->resource); + 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 -destroy_vblank_callback(struct wl_resource *resource) +_tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resource) { - tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource); - LIST_DEL(&vblank_info->link); - free(vblank_info); + 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_server_client_cb_destroy(struct wl_client *client, struct wl_resource *resource) +_tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(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_server_client_cb_wait_vblank(struct wl_client *client, - struct wl_resource *resource, - uint32_t id, const char *name, - int32_t sw_timer, int32_t interval, - uint32_t req_sec, uint32_t req_usec) +_tdm_voutput_wl_buffer_destroy(struct wl_client *client, struct wl_resource *wl_buffer) { - tdm_server_client_info *client_info = wl_resource_get_user_data(resource); - tdm_private_server *private_server = client_info->private_server; - tdm_private_loop *private_loop = private_server->private_loop; - tdm_server_vblank_info *vblank_info; - struct wl_resource *vblank_resource; - tdm_output *found = NULL; - tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_ON; - int count = 0, i; - tdm_error ret; + wl_resource_destroy(wl_buffer); +} - TDM_DBG("The tdm client requests vblank"); +static const struct wl_buffer_interface _tdm_voutput_buffer_impementation = { + _tdm_voutput_wl_buffer_destroy +}; - if (tdm_debug_server) { - unsigned long curr = tdm_helper_get_time_in_micros(); - unsigned long reqtime = (req_sec * 1000000) + req_usec; - if (curr - reqtime > 1000) /* 1ms */ - TDM_WRN("delay(req): %d us", (int)(curr - reqtime)); +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); + } } +} - tdm_display_get_output_count(private_loop->dpy, &count); +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; - for (i = 0; i < count; i++) { - tdm_output *output= tdm_display_get_output(private_loop->dpy, i, NULL); - tdm_output_conn_status status; - const char *model = NULL; + wl_client = wl_resource_get_client(voutput_info->resource); - ret = tdm_output_get_conn_status(output, &status); - if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) - continue; + /* 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); - ret = tdm_output_get_model_info(output, NULL, &model, NULL); - if (ret || !model) - continue; + wl_resource_set_implementation(wl_buffer, + (void (**)(void)) &_tdm_voutput_buffer_impementation, + voutput_info, _tdm_voutput_buffer_destory); - if (strncmp(model, name, TDM_NAME_LEN)) - continue; + return wl_buffer; +} - found = output; - break; +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 (!found) { - wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_INVALID_NAME, - "There is no '%s' output", name); - TDM_ERR("There is no '%s' output", name); - return; + 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; } - tdm_output_get_dpms(found, &dpms_value); + 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; + } - if (dpms_value != TDM_OUTPUT_DPMS_ON && !sw_timer) { - wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_DPMS_OFF, - "dpms '%s'", tdm_get_dpms_str(dpms_value)); - TDM_ERR("dpms '%s'", tdm_get_dpms_str(dpms_value)); - return; + /* 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; + } } - vblank_info = calloc(1, sizeof *vblank_info); - if (!vblank_info) { - wl_resource_post_no_memory(resource); - TDM_ERR("alloc failed"); - return; + wl_buffer = _tdm_voutput_create_wl_buffer(voutput_info); + if (!wl_buffer) { + TDM_ERR("Failed to create wl_buffer"); + goto err; } - TDM_DBG("wl_tdm_vblank@%d output(%s) interval(%d)", id, name, interval); + 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; + } - vblank_resource = - wl_resource_create(client, &wl_tdm_vblank_interface, - wl_resource_get_version(resource), id); - if (!vblank_resource) { - wl_resource_post_no_memory(resource); - TDM_ERR("wl_resource_create failed"); - goto free_info; + 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]); } - if (dpms_value == TDM_OUTPUT_DPMS_ON) { - ret = tdm_output_wait_vblank(found, interval, 0, - _tdm_server_cb_output_vblank, vblank_info); - if (ret != TDM_ERROR_NONE) { - wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED, - "couldn't wait vblank for %s", name); - TDM_ERR("couldn't wait vblank for %s", name); - goto destroy_resource; + 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; } - } else if (sw_timer) { - /* TODO: need to implement things related with sw_timer */ - if (ret != TDM_ERROR_NONE) { - wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED, - "not implemented yet"); - TDM_ERR("not implemented yet"); - goto destroy_resource; + } + + 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; } - } else { - wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED, - "bad implementation"); - TDM_NEVER_GET_HERE(); - goto destroy_resource; } + 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); - vblank_info->resource = vblank_resource; - vblank_info->client_info = client_info; + voutput_buffer = _tdm_output_get_voutput_buffer(voutput_info, buffer); + TDM_RETURN_VAL_IF_FAIL(voutput_buffer != NULL, TDM_ERROR_OUT_OF_MEMORY); - wl_resource_set_implementation(vblank_resource, NULL, vblank_info, - destroy_vblank_callback); + voutput_info->attach_buffer = voutput_buffer; - LIST_ADDTAIL(&vblank_info->link, &private_server->vblank_list); - return; -destroy_resource: - wl_resource_destroy(vblank_resource); -free_info: - free(vblank_info); + tbm_surface_internal_ref(buffer); + wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer); + + return TDM_ERROR_NONE; } -static const struct wl_tdm_client_interface tdm_client_implementation = { - _tdm_server_client_cb_destroy, - _tdm_server_client_cb_wait_vblank, -}; +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; -static void -destroy_client_callback(struct wl_resource *resource) + 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_client_info *client_info = wl_resource_get_user_data(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); + } - LIST_DEL(&client_info->link); - free(client_info); + 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_client(struct wl_client *client, - struct wl_resource *resource, uint32_t id) +_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_client_info *client_info; - struct wl_resource *client_resource; + tdm_server_voutput_info *voutput_info; + tdm_voutput *voutput; + tdm_output *output; + tdm_error ret; - client_info = calloc(1, sizeof *client_info); - if (!client_info) { - wl_resource_post_no_memory(resource); - TDM_ERR("alloc failed"); + 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; } - client_resource = - wl_resource_create(client, &wl_tdm_client_interface, - wl_resource_get_version(resource), id); - if (!client_resource) { + 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); - free(client_info); 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 */ +static void +_tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options) +{ + tdm_private_server *private_server = wl_resource_get_user_data(resource); + tdm_private_loop *private_loop = private_server->private_loop; + char message[TDM_SERVER_REPLY_MSG_LEN]; + char *m; + int len = sizeof(message), size; + uid_t uid; + + wl_client_get_credentials(client, NULL, &uid, NULL); + + if (uid != 0) { + snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n"); + TDM_ERR("%s", message); + } else { + tdm_monitor_server_command(private_loop->dpy, options, message, &len); } - client_info->private_server = private_server; - client_info->resource = client_resource; + size = sizeof(message) - len; + m = message; + + wl_client_flush(client); + + while (size > 0) { + char buffer[TDM_DEBUG_REPLY_MSG_LEN]; + int copylen = TDM_MIN(size, sizeof(buffer) - 1); - wl_resource_set_implementation(client_resource, &tdm_client_implementation, - client_info, destroy_client_callback); + strncpy(buffer, m, copylen); + m += copylen; + size -= copylen; - LIST_ADDTAIL(&client_info->link, &private_server->client_list); + buffer[copylen] = '\0'; + + wl_tdm_send_debug_message(resource, buffer); + } + + wl_tdm_send_debug_done(resource); } +/* LCOV_EXCL_STOP */ static const struct wl_tdm_interface tdm_implementation = { - _tdm_server_cb_create_client, + _tdm_server_cb_debug, + _tdm_server_cb_create_output, + _tdm_server_cb_create_virtual_output }; static void +destroy_client(struct wl_resource *resource) +{ + tdm_server_client_info *c = NULL, *cc = NULL; + struct wl_client *client; + + client = wl_resource_get_client(resource); + TDM_RETURN_IF_FAIL(client != NULL); + + LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) { + if (c->resource == resource) { + LIST_DEL(&c->link); + free(c); + return; + } + } +} + +static void _tdm_server_bind(struct wl_client *client, void *data, - uint32_t version, uint32_t id) + uint32_t version, uint32_t id) { struct wl_resource *resource; + tdm_server_client_info *cinfo; resource = wl_resource_create(client, &wl_tdm_interface, version, id); if (!resource) { + /* LCOV_EXCL_START */ + + wl_client_post_no_memory(client); + return; + + /* LCOV_EXCL_STOP */ + } + + cinfo = calloc(1, sizeof(tdm_server_client_info)); + if (!cinfo) { + /* LCOV_EXCL_START */ + wl_client_post_no_memory(client); + wl_resource_destroy(resource); return; + + /* LCOV_EXCL_STOP */ } - TDM_DBG("tdm server binding"); + cinfo->resource = resource; - wl_resource_set_implementation(resource, &tdm_implementation, data, NULL); + LIST_ADDTAIL(&cinfo->link, &client_list); + wl_client_get_credentials(client, &cinfo->pid, NULL, NULL); + _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN); + + wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client); } INTERN tdm_error tdm_server_init(tdm_private_loop *private_loop) { tdm_private_server *private_server; - const char *debug; - debug = getenv("TDM_DEBUG_SERVER"); - if (debug && (strstr(debug, "1"))) - tdm_debug_server = 1; + TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED); + TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED); if (private_loop->private_server) return TDM_ERROR_NONE; - TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED); - TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED); + if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) { + /* LCOV_EXCL_START */ - if(wl_display_add_socket(private_loop->wl_display, "tdm-socket")) { TDM_ERR("createing a tdm-socket failed"); return TDM_ERROR_OPERATION_FAILED; + + /* LCOV_EXCL_STOP */ } - private_server = calloc(1, sizeof *private_server); + private_server = calloc(1, sizeof * private_server); if (!private_server) { TDM_ERR("alloc failed"); return TDM_ERROR_OUT_OF_MEMORY; } - LIST_INITHEAD(&private_server->client_list); - LIST_INITHEAD(&private_server->vblank_list); + LIST_INITHEAD(&private_server->output_list); + LIST_INITHEAD(&private_server->voutput_list); + LIST_INITHEAD(&private_server->wait_list); if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1, - private_server, _tdm_server_bind)) { + private_server, _tdm_server_bind)) { + /* LCOV_EXCL_START */ + TDM_ERR("creating a global resource failed"); free(private_server); return TDM_ERROR_OUT_OF_MEMORY; + + /* LCOV_EXCL_STOP */ } private_server->private_loop = private_loop; private_loop->private_server = private_server; keep_private_server = private_server; + LIST_INITHEAD(&client_list); + return TDM_ERROR_NONE; } INTERN void tdm_server_deinit(tdm_private_loop *private_loop) { - tdm_server_vblank_info *v = NULL, *vv = NULL; + tdm_server_output_info *o = NULL, *oo = NULL; + tdm_server_voutput_info *vo = NULL, *voo = NULL; + tdm_server_wait_info *w = NULL, *ww = NULL; tdm_server_client_info *c = NULL, *cc = NULL; tdm_private_server *private_server; @@ -358,11 +1541,19 @@ tdm_server_deinit(tdm_private_loop *private_loop) private_server = private_loop->private_server; - LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_server->vblank_list, link) { - wl_resource_destroy(v->resource); + LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) { + destroy_wait(w); + } + + LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) { + wl_resource_destroy(o->resource); + } + + LIST_FOR_EACH_ENTRY_SAFE(vo, voo, &private_server->voutput_list, link) { + wl_resource_destroy(vo->resource); } - LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_server->client_list, link) { + LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) { wl_resource_destroy(c->resource); } @@ -370,3 +1561,43 @@ tdm_server_deinit(tdm_private_loop *private_loop) private_loop->private_server = NULL; keep_private_server = NULL; } + +/* LCOV_EXCL_START */ +INTERN const char* +tdm_server_get_client_name(pid_t pid) +{ + tdm_server_client_info *c = NULL; + + LIST_FOR_EACH_ENTRY(c, &client_list, link) { + if (c->pid == pid) + return (const char*)c->name; + } + + return NULL; +} +/* LCOV_EXCL_STOP */ + +/* LCOV_EXCL_START */ +INTERN tdm_error +tdm_server_enable_ttrace_client_vblank(tdm_display *dpy, tdm_output *output, int enable) +{ + tdm_private_server *private_server = keep_private_server; + tdm_server_output_info *output_info = NULL; + + if (!keep_private_server) + return TDM_ERROR_NONE; + + LIST_FOR_EACH_ENTRY(output_info, &private_server->output_list, link) { + tdm_server_vblank_info *vblank_info = NULL; + + if (output && output_info->output != output) + continue; + + LIST_FOR_EACH_ENTRY(vblank_info, &output_info->vblank_list, link) { + wl_tdm_vblank_send_ttrace(vblank_info->resource, enable); + } + } + + return TDM_ERROR_NONE; +} +/* LCOV_EXCL_STOP */