virtual: support virtual output create & destroy 58/191058/4
authorJunkyeong Kim <jk0430.kim@samsung.com>
Thu, 11 Oct 2018 05:47:03 +0000 (14:47 +0900)
committerJunkyeong Kim <jk0430.kim@samsung.com>
Thu, 1 Nov 2018 02:31:29 +0000 (02:31 +0000)
Change-Id: I863304cefa6358950ab354b824d7acf2e81dcea6
Signed-off-by: Junkyeong Kim <jk0430.kim@samsung.com>
src/tdm.c
src/tdm_display.c
src/tdm_event_loop.c
src/tdm_helper.c
src/tdm_output.c
src/tdm_private.h
src/tdm_server.c
src/tdm_thread.c

index 6886527..30f3c07 100644 (file)
--- a/src/tdm.c
+++ b/src/tdm.c
@@ -50,6 +50,8 @@ pthread_mutex_t tdm_debug_mutex_check_lock = PTHREAD_MUTEX_INITIALIZER;
 const char *tdm_debug_mutex_lock_func;
 int tdm_debug_mutex_lock_line;
 
+static tdm_error _tdm_display_load_module_with_file(tdm_private_display *private_display, const char *file);
+
 /* LCOV_EXCL_START */
 static tdm_private_layer *
 _tdm_display_find_private_layer(tdm_private_output *private_output,
@@ -81,6 +83,34 @@ tdm_display_find_private_output(tdm_private_display *private_display, tdm_output
        return NULL;
 }
 
+INTERN unsigned int
+tdm_display_find_empty_output_pipe(tdm_private_display *private_display)
+{
+       tdm_private_module *private_module = NULL;
+       tdm_private_output *private_output = NULL;
+       unsigned int pipe = 0;
+       while (1) {
+               int found = 0;
+               LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
+                       LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
+                               if (private_output->pipe == pipe) {
+                                       found = 1;
+                                       break;
+                               }
+                       }
+                       if (found)
+                               break;
+               }
+
+               if (!found)
+                       break;
+               else
+                       pipe++;
+       }
+
+       return pipe;
+}
+
 INTERN void *
 tdm_display_find_output_stamp(tdm_private_display *private_display, double stamp)
 {
@@ -197,8 +227,8 @@ _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
        free(private_layer);
 }
 
-static void
-_tdm_display_destroy_private_output(tdm_private_output *private_output)
+INTERN void
+tdm_display_destroy_private_output(tdm_private_output *private_output)
 {
        tdm_private_display *private_display = private_output->private_display;
        tdm_private_layer *l = NULL, *ll = NULL;
@@ -210,6 +240,11 @@ _tdm_display_destroy_private_output(tdm_private_output *private_output)
        tdm_private_hwc_commit_handler *hm = NULL, *hmm = NULL;
        tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
        tdm_private_output_change_handler *h = NULL, *hh = NULL;
+       tdm_private_output_destroy_handler *dh = NULL, *dhh = NULL;
+       tdm_error ret;
+
+       ret = tdm_output_call_thread_cb_destroy(private_output);
+       TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
 
        free(private_output->layers_ptr);
 
@@ -251,6 +286,12 @@ _tdm_display_destroy_private_output(tdm_private_output *private_output)
                free(h);
        }
 
+       LIST_FOR_EACH_ENTRY_SAFE(dh, dhh, &private_output->destroy_handler_list, link) {
+               LIST_DEL(&dh->link);
+               tdm_thread_cb_remove(dh->private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, dh);
+               free(dh);
+       }
+
        if (private_output->vblank) {
                /* tdm_vblank APIs is for server. it should be called in unlock status*/
                _pthread_mutex_unlock(&private_display->lock);
@@ -295,6 +336,7 @@ _tdm_display_destroy_private_display(tdm_private_display *private_display)
        tdm_private_module *private_module = NULL, *bb = NULL;
        tdm_private_output *o = NULL, *oo = NULL;
        tdm_private_pp *p = NULL, *pp = NULL;
+       tdm_private_output_create_handler *ch = NULL, *chh = NULL;
 
        LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
                LIST_FOR_EACH_ENTRY_SAFE(p, pp, &private_module->pp_list, link) {
@@ -302,7 +344,7 @@ _tdm_display_destroy_private_display(tdm_private_display *private_display)
                }
 
                LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_module->output_list, link) {
-                       _tdm_display_destroy_private_output(o);
+                       tdm_display_destroy_private_output(o);
                }
 
                _tdm_display_destroy_caps_pp(&private_module->caps_pp);
@@ -316,6 +358,13 @@ _tdm_display_destroy_private_display(tdm_private_display *private_display)
                        private_module->outputs = NULL;
                }
        }
+
+       LIST_FOR_EACH_ENTRY_SAFE(ch, chh, &private_display->output_create_handler_list, link) {
+               LIST_DEL(&ch->link);
+               tdm_thread_cb_remove(ch->private_display, TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, NULL,
+                                                        tdm_display_thread_cb_output_create, ch);
+               free(ch);
+       }
 }
 
 static tdm_error
@@ -392,11 +441,6 @@ _tdm_display_update_caps_output(tdm_private_module *private_module, int pipe,
        tdm_error ret;
        double stamp;
 
-       if (!func_output->output_get_capability) {
-               TDM_ERR("backend(%s) no output_get_capability()", private_module->module_data->name);
-               return TDM_ERROR_BAD_MODULE;
-       }
-
        stamp = tdm_helper_get_time();
        ret = func_output->output_get_capability(output_backend, caps);
        TDM_DBG("backend(%s) backend output_get_capability() time: %.3f ms",
@@ -446,30 +490,35 @@ _tdm_display_update_layer(tdm_private_output *private_output,
 }
 
 INTERN tdm_error
-tdm_display_update_output(tdm_private_module *private_module,
-                                                 tdm_output *output_backend, int pipe, unsigned int need_new_caps)
+tdm_display_update_output(tdm_private_module *private_module, tdm_output *output_backend)
 {
        tdm_func_output *func_output = &private_module->func_output;
+       tdm_private_display *private_display = NULL;
        tdm_private_output *private_output = NULL;
        tdm_layer **layers = NULL;
        tdm_private_hwc *private_hwc = NULL;
        tdm_hwc *hwc;
        int layer_count = 0, i;
        tdm_error ret;
+       unsigned int output_created = 0;
+
+       private_display = private_module->private_display;
 
-       private_output = tdm_display_find_private_output(private_module->private_display, output_backend);
+       private_output = tdm_display_find_private_output(private_display, output_backend);
        if (!private_output) {
+               unsigned int pipe = tdm_display_find_empty_output_pipe(private_display);
+
                private_output = calloc(1, sizeof(tdm_private_output));
                TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
 
                private_output->stamp = tdm_helper_get_time();
-               while (tdm_display_find_output_stamp(private_module->private_display, private_output->stamp))
+               while (tdm_display_find_output_stamp(private_display, private_output->stamp))
                        private_output->stamp++;
 
                LIST_ADDTAIL(&private_output->link, &private_module->output_list);
 
                private_output->private_module = private_module;
-               private_output->private_display = private_module->private_display;
+               private_output->private_display = private_display;
                private_output->current_dpms_value = TDM_OUTPUT_DPMS_OFF;
                private_output->output_backend = output_backend;
                private_output->pipe = pipe;
@@ -479,6 +528,7 @@ tdm_display_update_output(tdm_private_module *private_module,
                LIST_INITHEAD(&private_output->vblank_handler_list);
                LIST_INITHEAD(&private_output->output_commit_handler_list);
                LIST_INITHEAD(&private_output->pending_commit_handler_list);
+               LIST_INITHEAD(&private_output->destroy_handler_list);
                LIST_INITHEAD(&private_output->change_handler_list);
 
                if (func_output->output_set_status_handler) {
@@ -488,7 +538,11 @@ tdm_display_update_output(tdm_private_module *private_module,
                        private_output->regist_change_cb = 1;
                }
 
-               ret = _tdm_display_update_caps_output(private_module, pipe, output_backend, &private_output->caps);
+               output_created = 1;
+
+               /* NOTE that output modes will be allocated newly after here */
+               _tdm_display_destroy_caps_output(&private_output->caps);
+               ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
                TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
 
                if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
@@ -519,18 +573,12 @@ tdm_display_update_output(tdm_private_module *private_module,
                        LIST_INITHEAD(&private_output->layer_commit_handler_list);
                }
        } else {
-               /* need_new_caps will be true only in case of "disconnected -> connected" and "connected -> disconnected"
-                * because we have to get new modes.
-                */
-               if (need_new_caps) {
-                       _tdm_display_destroy_caps_output(&private_output->caps);
-
-                       ret = _tdm_display_update_caps_output(private_module, pipe, output_backend, &private_output->caps);
-                       TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
+               _tdm_display_destroy_caps_output(&private_output->caps);
+               ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
+               TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
 
-                       if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
-                               private_output->current_mode = NULL;
-               }
+               if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
+                       private_output->current_mode = NULL;
        }
 
        /* do not use the layer object when the tdm_output has the hwc capability */
@@ -552,6 +600,11 @@ tdm_display_update_output(tdm_private_module *private_module,
                free(layers);
        }
 
+       if (output_created) {
+               ret = tdm_display_call_thread_cb_output_create(private_display, private_output);
+               TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
+       }
+
        return TDM_ERROR_NONE;
 }
 
@@ -589,12 +642,12 @@ _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
 
        outputs = func_display->display_get_outputs(private_module->bdata, &output_count, &ret);
        if (ret != TDM_ERROR_NONE)
-               goto failed_get_outputs;
+               goto no_output;
 
        *count = output_count;
 
        if (output_count == 0)
-               goto failed_get_outputs;
+               goto no_output;
        else if (output_count == 1) {
                private_module->outputs = outputs;
                return outputs;
@@ -609,7 +662,7 @@ _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
                ret = func_output->output_get_capability(outputs[i], &caps);
                if (ret != TDM_ERROR_NONE) {
                        TDM_ERR("output_get_capability() failed");
-                       goto failed_get_outputs;
+                       goto no_output;
                }
 
                if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
@@ -673,7 +726,7 @@ _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
 
        return outputs;
 
-failed_get_outputs:
+no_output:
        free(outputs);
        *count = 0;
        return NULL;
@@ -684,7 +737,7 @@ _tdm_display_setup(tdm_private_display *private_display)
 {
        tdm_private_module *private_module = NULL;
        tdm_error ret = TDM_ERROR_NONE;
-       int index = 0;
+       int output_count = 0;
 
        if (private_display->pp_module) {
                ret = _tdm_display_update_caps_pp(private_display->pp_module,
@@ -702,14 +755,43 @@ _tdm_display_setup(tdm_private_display *private_display)
 
        LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
                tdm_output **outputs;
-               int output_count = 0, i;
+               int i, count = 0;
 
-               outputs = _tdm_display_get_ordered_outputs(private_module, &output_count);
-               if (!outputs)
-                       goto failed_update;
+               outputs = _tdm_display_get_ordered_outputs(private_module, &count);
+
+               if (count > 0)
+                       TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
+
+               for (i = 0; i < count; i++) {
+                       ret = tdm_display_update_output(private_module, outputs[i]);
+                       if (ret != TDM_ERROR_NONE)
+                               goto failed_update;
+                       output_count++;
+               }
+       }
+
+       /* At least, the output count should be greater than 0 to ensure tdm_vblank works.
+        * So we will create a dummy output when backends don't have any output.
+        * Without destroying a tdm_output object, this dummy output will be replaced with
+        * a virtual output which is created in runtime.
+        */
+       if (output_count == 0) {
+               tdm_output **outputs;
+               int i, count = 0;
+
+               TDM_INFO("loading a %s backend", TDM_DUMMY_MODULE);
+               ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
+               TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed_update);
+               TDM_GOTO_IF_FAIL(private_display->dummy_module != NULL, failed_update);
+
+               private_module = private_display->dummy_module;
 
-               for (i = 0; i < output_count; i++) {
-                       ret = tdm_display_update_output(private_module, outputs[i], index++, 1);
+               outputs = _tdm_display_get_ordered_outputs(private_module, &count);
+               TDM_GOTO_IF_FAIL(count > 0, failed_update);
+               TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
+
+               for (i = 0; i < count; i++) {
+                       ret = tdm_display_update_output(private_module, outputs[i]);
                        if (ret != TDM_ERROR_NONE)
                                goto failed_update;
                }
@@ -925,6 +1007,9 @@ _tdm_display_load_module_with_file(tdm_private_display *private_display,
 
        private_display->current_module = NULL;
 
+       if (!strncmp(file, TDM_DUMMY_MODULE, TDM_NAME_LEN))
+               private_display->dummy_module = private_module;
+
        private_module->bdata = bdata;
 
        if (ret != TDM_ERROR_NONE) {
@@ -983,16 +1068,6 @@ _tdm_display_load_modules(tdm_private_display *private_display)
                arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
        }
 
-       /* load bufmgr priv from dummy lib */
-       if (LIST_IS_EMPTY(&private_display->module_list)) {
-               TDM_WRN("No backend. loading a %s backend", TDM_DUMMY_MODULE);
-               ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
-               if (ret == TDM_ERROR_NONE)
-                       TDM_INFO("%s backend loading success", TDM_DUMMY_MODULE);
-               else
-                       TDM_INFO("%s backend loading failed", TDM_DUMMY_MODULE);
-       }
-
        return ret;
 }
 
@@ -1014,6 +1089,12 @@ _tdm_display_unload_modules(tdm_private_display *private_display)
 }
 /* LCOV_EXCL_STOP */
 
+INTERN void *
+tdm_display_find_stamp(tdm_private_display *private_display, double stamp)
+{
+       return (void*)g_private_display;
+}
+
 EXTERN tdm_display *
 tdm_display_init(tdm_error *error)
 {
@@ -1046,6 +1127,8 @@ tdm_display_init(tdm_error *error)
 
        private_display->stamp = tdm_helper_get_time();
 
+       LIST_INITHEAD(&private_display->output_create_handler_list);
+
        str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
        if (str)
                tdm_display_enable_debug_module(str);
@@ -1158,6 +1241,8 @@ tdm_display_init(tdm_error *error)
        if (error)
                *error = TDM_ERROR_NONE;
 
+       tdm_thread_cb_set_find_func(TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, tdm_display_find_stamp);
+
        _pthread_mutex_unlock(&private_display->lock);
        pthread_mutex_unlock(&gLock);
 
index 297dd70..4ee693e 100644 (file)
@@ -349,6 +349,124 @@ tdm_display_enable_fps(tdm_private_display *private_display, int enable)
 }
 /* LCOV_EXCL_STOP */
 
+INTERN tdm_error
+tdm_display_call_thread_cb_output_create(tdm_private_display *private_display, tdm_output *output)
+{
+       tdm_thread_cb_display_output_create output_create;
+       tdm_error ret;
+
+       if (LIST_IS_EMPTY(&private_display->output_create_handler_list)) return TDM_ERROR_NONE;
+
+       memset(&output_create, 0, sizeof output_create);
+       output_create.base.type = TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE;
+       output_create.base.length = sizeof output_create;
+       output_create.base.object_stamp = tdm_helper_get_time();
+       output_create.base.data = NULL;
+       output_create.base.sync = 1;
+       output_create.output = output;
+
+       ret = tdm_thread_cb_call(private_display, &output_create.base, 1);
+       TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
+
+       return TDM_ERROR_NONE;
+}
+
+INTERN void
+tdm_display_thread_cb_output_create(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
+{
+       tdm_thread_cb_display_output_create *output_create = (tdm_thread_cb_display_output_create*)cb_base;
+       tdm_private_output_create_handler *create_handler = user_data;
+       TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
+
+       assert(create_handler->owner_tid == syscall(SYS_gettid));
+
+       _pthread_mutex_unlock(&private_display->lock);
+       create_handler->func(private_display, output_create->output, create_handler->user_data);
+       _pthread_mutex_lock(&private_display->lock);
+}
+
+EXTERN tdm_error
+tdm_display_add_output_create_handler(tdm_display *dpy,
+                                                                         tdm_output_create_handler func,
+                                                                         void *user_data)
+{
+       tdm_private_output_create_handler *create_handler = NULL;
+
+       DISPLAY_FUNC_ENTRY();
+
+       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       _pthread_mutex_lock(&private_display->lock);
+
+       LIST_FOR_EACH_ENTRY(create_handler, &private_display->output_create_handler_list, link) {
+               if (create_handler->func == func && create_handler->user_data == user_data) {
+                       TDM_ERR("can't add twice");
+                       _pthread_mutex_unlock(&private_display->lock);
+                       return TDM_ERROR_BAD_REQUEST;
+               }
+       }
+
+       create_handler = calloc(1, sizeof(tdm_private_output_create_handler));
+       if (!create_handler) {
+               TDM_ERR("failed: alloc memory");
+               _pthread_mutex_unlock(&private_display->lock);
+               return TDM_ERROR_OUT_OF_MEMORY;
+       }
+
+       ret = tdm_thread_cb_add(private_display, TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, NULL,
+                                                       tdm_display_thread_cb_output_create, create_handler);
+       if (ret != TDM_ERROR_NONE) {
+               TDM_ERR("tdm_thread_cb_add failed");
+               free(create_handler);
+               _pthread_mutex_unlock(&private_display->lock);
+               return TDM_ERROR_OPERATION_FAILED;
+       }
+
+       create_handler->private_display = private_display;
+       create_handler->func = func;
+       create_handler->user_data = user_data;
+       create_handler->owner_tid = syscall(SYS_gettid);
+
+       LIST_ADDTAIL(&create_handler->link, &private_display->output_create_handler_list);
+
+       _pthread_mutex_unlock(&private_display->lock);
+
+       return ret;
+}
+
+EXTERN void
+tdm_display_remove_output_create_handler(tdm_display *dpy,
+                                                                                tdm_output_create_handler func,
+                                                                                void *user_data)
+{
+       tdm_private_display *private_display;
+       tdm_private_output_create_handler *create_handler = NULL, *hh = NULL;
+
+       TDM_RETURN_IF_FAIL(dpy != NULL);
+       TDM_RETURN_IF_FAIL(func != NULL);
+
+       private_display = (tdm_private_display*)dpy;
+
+       _pthread_mutex_lock(&private_display->lock);
+
+       LIST_FOR_EACH_ENTRY_SAFE(create_handler, hh, &private_display->output_create_handler_list, link) {
+               if (create_handler->func != func || create_handler->user_data != user_data)
+                       continue;
+
+               tdm_thread_cb_remove(private_display, TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, NULL,
+                                                        tdm_display_thread_cb_output_create, create_handler);
+
+               LIST_DEL(&create_handler->link);
+               free(create_handler);
+
+               _pthread_mutex_unlock(&private_display->lock);
+
+               return;
+       }
+
+       _pthread_mutex_unlock(&private_display->lock);
+}
+
 EXTERN tdm_error
 tdm_display_get_capabilities(tdm_display *dpy,
                                                         tdm_display_capability *capabilities)
index b7de4ab..5117c27 100644 (file)
@@ -63,6 +63,7 @@ static tdm_error
 _tdm_event_loop_main_fd_handler(int fd, tdm_event_loop_mask mask, void *user_data)
 {
        tdm_private_module *private_module = (tdm_private_module*)user_data;
+       tdm_private_display *private_display;
        tdm_func_display *func_display;
        tdm_error ret;
 
@@ -76,7 +77,10 @@ _tdm_event_loop_main_fd_handler(int fd, tdm_event_loop_mask mask, void *user_dat
        if (!func_display->display_handle_events)
                return TDM_ERROR_NONE;
 
+       private_display = private_module->private_display;
+       private_display->current_module = private_module;
        ret = func_display->display_handle_events(private_module->bdata);
+       private_display->current_module = NULL;
 
        return ret;
 }
index 3607f13..afedccd 100644 (file)
@@ -846,6 +846,8 @@ _tdm_helper_get_backend_information(tdm_private_module *private_module, char *re
                        }
                }
        }
+       if (LIST_IS_EMPTY(&private_module->output_list))
+               TDM_SNPRINTF(reply, len, "(no output)\n");
        TDM_SNPRINTF(reply, len, "\n");
 
        /* layer information */
index ee760c1..4a96e16 100644 (file)
@@ -130,15 +130,130 @@ _tdm_output_vblank_timeout_update(tdm_private_output *private_output, int ms_del
 INTERN tdm_error
 tdm_output_init(tdm_private_display *private_display)
 {
+       tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_DESTROY, tdm_display_find_output_stamp);
        tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_COMMIT, tdm_display_find_output_stamp);
        tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_VBLANK, tdm_display_find_output_stamp);
        tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_STATUS, tdm_display_find_output_stamp);
        tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_DPMS, tdm_display_find_output_stamp);
 
+       return TDM_ERROR_NONE;
+}
+
+INTERN tdm_error
+tdm_output_call_thread_cb_destroy(tdm_private_output *private_output)
+{
+       tdm_thread_cb_output_destroy output_destroy;
+       tdm_error ret;
+
+       memset(&output_destroy, 0, sizeof output_destroy);
+       output_destroy.base.type = TDM_THREAD_CB_OUTPUT_DESTROY;
+       output_destroy.base.length = sizeof output_destroy;
+       output_destroy.base.object_stamp = private_output->stamp;
+       output_destroy.base.data = NULL;
+       output_destroy.base.sync = 1;
+
+       ret = tdm_thread_cb_call(private_output, &output_destroy.base, 1);
+       TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
 
        return TDM_ERROR_NONE;
 }
 
+INTERN void
+tdm_output_thread_cb_destroy(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
+{
+       tdm_private_output *private_output = object;
+       tdm_private_output_destroy_handler *destroy_handler = user_data;
+
+       TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
+
+       assert(destroy_handler->owner_tid == syscall(SYS_gettid));
+
+       _pthread_mutex_unlock(&private_display->lock);
+       destroy_handler->func(private_output, destroy_handler->user_data);
+       _pthread_mutex_lock(&private_display->lock);
+}
+
+EXTERN tdm_error
+tdm_output_add_destroy_handler(tdm_output *output,
+                                                          tdm_output_destroy_handler func,
+                                                          void *user_data)
+{
+       tdm_private_output_destroy_handler *destroy_handler = NULL;
+       OUTPUT_FUNC_ENTRY();
+
+       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       _pthread_mutex_lock(&private_display->lock);
+
+       LIST_FOR_EACH_ENTRY(destroy_handler, &private_output->destroy_handler_list, link) {
+               if (destroy_handler->func == func && destroy_handler->user_data == user_data) {
+                       TDM_ERR("can't add twice");
+                       _pthread_mutex_unlock(&private_display->lock);
+                       return TDM_ERROR_BAD_REQUEST;
+               }
+       }
+
+       destroy_handler = calloc(1, sizeof(tdm_private_output_destroy_handler));
+       if (!destroy_handler) {
+               TDM_ERR("failed: alloc memory");
+               _pthread_mutex_unlock(&private_display->lock);
+               return TDM_ERROR_OUT_OF_MEMORY;
+       }
+
+       ret = tdm_thread_cb_add(private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, destroy_handler);
+       if (ret != TDM_ERROR_NONE) {
+               TDM_ERR("tdm_thread_cb_add failed");
+               free(destroy_handler);
+               _pthread_mutex_unlock(&private_display->lock);
+               return TDM_ERROR_OPERATION_FAILED;
+       }
+
+       destroy_handler->private_output = private_output;
+       destroy_handler->func = func;
+       destroy_handler->user_data = user_data;
+       destroy_handler->owner_tid = syscall(SYS_gettid);
+
+       LIST_ADDTAIL(&destroy_handler->link, &private_output->destroy_handler_list);
+
+       _pthread_mutex_unlock(&private_display->lock);
+
+       return ret;
+}
+
+EXTERN void
+tdm_output_remove_destroy_handler(tdm_output *output,
+                                                                 tdm_output_destroy_handler func,
+                                                                 void *user_data)
+{
+       tdm_private_display *private_display;
+       tdm_private_output *private_output;
+       tdm_private_output_destroy_handler *destroy_handler = NULL, *hh = NULL;
+
+       TDM_RETURN_IF_FAIL(output != NULL);
+       TDM_RETURN_IF_FAIL(func != NULL);
+
+       private_output = (tdm_private_output*)output;
+       private_display = private_output->private_display;
+
+       _pthread_mutex_lock(&private_display->lock);
+
+       LIST_FOR_EACH_ENTRY_SAFE(destroy_handler, hh, &private_output->destroy_handler_list, link) {
+               if (destroy_handler->func != func || destroy_handler->user_data != user_data)
+                       continue;
+
+               tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, destroy_handler);
+
+               LIST_DEL(&destroy_handler->link);
+               free(destroy_handler);
+
+               _pthread_mutex_unlock(&private_display->lock);
+
+               return;
+       }
+
+       _pthread_mutex_unlock(&private_display->lock);
+}
+
 EXTERN tdm_module *
 tdm_output_get_backend_module(tdm_output *output, tdm_error *error)
 {
@@ -297,7 +412,7 @@ tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
 
        if ((private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED && status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) ||
                (private_output->caps.status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED && status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)) {
-               ret = tdm_display_update_output(private_output->private_module, output_backend, private_output->pipe, 1);
+               ret = tdm_display_update_output(private_output->private_module, output_backend);
                TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
        } else {
                private_output->caps.status = status;
@@ -456,7 +571,7 @@ tdm_output_get_layer_count(tdm_output *output, int *count)
 
        *count = 0;
        LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link)
-       (*count)++;
+               (*count)++;
        if (*count == 0) {
                _pthread_mutex_unlock(&private_display->lock);
                return TDM_ERROR_NONE;
@@ -1594,4 +1709,150 @@ tdm_output_choose_commit_per_vblank_mode(tdm_private_output *private_output, int
 
        return TDM_ERROR_NONE;
 }
-/* LCOV_EXCL_STOP */
+
+INTERN tdm_voutput *
+tdm_voutput_create(tdm_display *dpy, const char *name, tdm_error *error)
+{
+       tdm_private_module *private_module = NULL;
+       tdm_private_module *current_module = NULL;
+       tdm_private_voutput *private_voutput = NULL;
+       tdm_private_output *private_output = NULL;
+       tdm_func_display *func_display = NULL;
+       tdm_func_voutput *func_voutput = NULL;
+       tdm_voutput *voutput_backend = NULL;
+       tdm_output *output_backend = NULL;
+       int output_find = 0;
+       tdm_private_display *private_display;
+       tdm_error ret = TDM_ERROR_NONE;
+
+       TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
+       private_display = (tdm_private_display*)dpy;
+       private_module = private_display->virtual_module;
+       TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(private_module != NULL, TDM_ERROR_BAD_MODULE, NULL);
+
+       _pthread_mutex_lock(&private_display->lock);
+
+       if (error)
+               *error = TDM_ERROR_NONE;
+
+       private_voutput = calloc(1, sizeof(tdm_private_voutput));
+       if (!private_voutput) {
+               *error = TDM_ERROR_OUT_OF_MEMORY;
+               _pthread_mutex_unlock(&private_display->lock);
+               return NULL;
+       }
+
+       func_display = &private_module->func_display;
+       func_voutput = &private_module->func_voutput;
+       current_module = private_display->current_module;
+       private_display->current_module = private_module;
+
+       voutput_backend = func_display->voutput_create(private_module->bdata, name, &ret);
+
+       if (voutput_backend == NULL || ret != TDM_ERROR_NONE) {
+               TDM_ERR("voutput_create fail");
+               free(private_voutput);
+               *error = ret;
+               private_display->current_module = current_module;
+               _pthread_mutex_unlock(&private_display->lock);
+               return NULL;
+       }
+       private_voutput->voutput_backend = voutput_backend;
+       private_voutput->private_display = private_display;
+       private_voutput->private_module = private_module;
+       LIST_INITHEAD(&private_voutput->voutput_commit_handler_list);
+
+       output_backend = func_voutput->voutput_get_output(voutput_backend, &ret);
+       if (output_backend == NULL || ret != TDM_ERROR_NONE) {
+               TDM_ERR("voutput_get_output fail");
+               free(private_voutput);
+               *error = ret;
+               if (func_voutput->voutput_destroy)
+                       func_voutput->voutput_destroy(voutput_backend);
+               else
+                       TDM_ERR("no destroy function");
+               private_display->current_module = current_module;
+               _pthread_mutex_unlock(&private_display->lock);
+               return NULL;
+       }
+
+       ret = tdm_display_update_output(private_display->current_module, output_backend);
+       if (ret != TDM_ERROR_NONE) {
+               TDM_ERR("tdm_display_update_output fail");
+               free(private_voutput);
+               if (func_voutput->voutput_destroy)
+                       func_voutput->voutput_destroy(voutput_backend);
+               else
+                       TDM_ERR("no destroy function");
+               *error = ret;
+               private_display->current_module = current_module;
+               _pthread_mutex_unlock(&private_display->lock);
+               return NULL;
+       }
+
+       LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
+               if (private_output->output_backend == output_backend) {
+                       output_find = 1;
+                       break;
+               }
+       }
+
+       if (output_find != 1) {
+               private_output = NULL;
+               free(private_voutput);
+               if (func_voutput->voutput_destroy)
+                       func_voutput->voutput_destroy(voutput_backend);
+               else
+                       TDM_ERR("no destroy function");
+       } else {
+               strncpy(private_voutput->name, name, TDM_NAME_LEN);
+               strncpy(private_output->name, name, TDM_NAME_LEN);
+
+               private_voutput->private_output = private_output;
+               private_output->private_voutput = private_voutput;
+
+               /* do not use vblank */
+               tdm_output_choose_commit_per_vblank_mode(private_output, 0);
+
+               LIST_ADDTAIL(&private_voutput->link, &private_module->voutput_list);
+       }
+
+       private_display->current_module = current_module;
+
+       _pthread_mutex_unlock(&private_display->lock);
+
+       return private_voutput;
+}
+
+INTERN tdm_error
+tdm_voutput_destroy(tdm_voutput *voutput)
+{
+       tdm_private_module *private_module = NULL;
+       tdm_voutput *voutput_backend = NULL;
+       tdm_func_voutput *func_voutput = NULL;
+
+       tdm_private_display *private_display;
+       tdm_private_voutput *private_voutput;
+       tdm_private_output *private_output;
+       tdm_error ret = TDM_ERROR_NONE;
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+       private_voutput = (tdm_private_voutput*)voutput;
+       private_display = private_voutput->private_display;
+       private_module = private_voutput->private_module;
+       TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
+
+       _pthread_mutex_lock(&private_display->lock);
+
+       func_voutput = &private_module->func_voutput;
+       voutput_backend = private_voutput->voutput_backend;
+       private_output = private_voutput->private_output;
+       tdm_display_destroy_private_output(private_output);
+       LIST_DEL(&private_voutput->link);
+       free(private_voutput);
+       if (func_voutput->voutput_destroy)
+               ret = func_voutput->voutput_destroy(voutput_backend);
+
+       _pthread_mutex_unlock(&private_display->lock);
+
+       return ret;
+}
index fd1fdae..9e10713 100644 (file)
@@ -240,8 +240,10 @@ extern int tdm_dump_enable;
 extern char *tdm_debug_dump_dir;
 
 tdm_error
-tdm_display_update_output(tdm_private_module *private_module,
-                                                 tdm_output *output_backend, int pipe, unsigned int need_new_caps);
+tdm_display_update_output(tdm_private_module *private_module, tdm_output *output_backend);
+void
+tdm_display_destroy_private_output(tdm_private_output *private_output);
+
 tdm_error
 tdm_display_enable_debug_module(const char*modules);
 tdm_error
index 93d6be9..12896d5 100644 (file)
@@ -37,6 +37,8 @@
 #include "config.h"
 #endif
 
+#define WL_HIDE_DEPRECATED
+
 #include <tdm-server-protocol.h>
 
 #include "tdm_private.h"
@@ -53,6 +55,7 @@
 struct _tdm_private_server {
        tdm_private_loop *private_loop;
        struct list_head output_list;
+       struct list_head voutput_list;
        struct list_head wait_list;
 };
 
@@ -65,6 +68,29 @@ typedef struct _tdm_server_output_info {
        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;
+} 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;
+} tdm_server_voutput_info;
+
 typedef struct _tdm_server_vblank_info {
        struct list_head link;
        tdm_server_output_info *output_info;
@@ -739,13 +765,58 @@ static const struct wl_tdm_voutput_interface tdm_voutput_implementation = {
 void
 tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
 {
-       /* TODO */
+       tdm_server_voutput_info *voutput_info = wl_resource_get_user_data(resource);
+       tdm_voutput *voutput;
+       tdm_error ret = TDM_ERROR_NONE;
+
+       TDM_RETURN_IF_FAIL(voutput_info != NULL);
+
+       voutput = voutput_info->voutput;
+
+       if (voutput)
+               ret = tdm_voutput_destroy(voutput);
+       if (ret != TDM_ERROR_NONE)
+               TDM_ERR("_tdm_voutput_cb_destroy fail");
+
+       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_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,
@@ -759,9 +830,28 @@ _tdm_server_cb_create_virtual_output(struct wl_client *client, struct wl_resourc
 
                /* 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;
+
        wl_resource_set_implementation(voutput_resource,
                                                                   &tdm_voutput_implementation,
-                                                                  NULL,
+                                                                  voutput_info,
                                                                   tdm_voutput_cb_resource_destroy);
 
        wl_tdm_voutput_send_ack_message(voutput_resource, WL_TDM_VOUTPUT_MESSAGE_ADDED);
@@ -897,6 +987,7 @@ tdm_server_init(tdm_private_loop *private_loop)
        }
 
        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,
@@ -923,6 +1014,7 @@ INTERN void
 tdm_server_deinit(tdm_private_loop *private_loop)
 {
        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;
@@ -940,6 +1032,10 @@ tdm_server_deinit(tdm_private_loop *private_loop)
                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, &client_list, link) {
                wl_resource_destroy(c->resource);
        }
index 2cc2f13..d367d4e 100644 (file)
@@ -451,6 +451,8 @@ tdm_thread_handle_cb(tdm_private_loop *private_loop)
                if (tdm_debug_module & TDM_DEBUG_THREAD)
                        TDM_INFO("type(%s), length(%d)", tdm_cb_type_str(base->type), base->length);
                switch (base->type) {
+               case TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE:
+               case TDM_THREAD_CB_OUTPUT_DESTROY:
                case TDM_THREAD_CB_OUTPUT_COMMIT:
                case TDM_THREAD_CB_OUTPUT_VBLANK:
                case TDM_THREAD_CB_OUTPUT_STATUS: