Merge branch 'master' into blender2.8
[blender.git] / source / blender / windowmanager / intern / wm_init_exit.c
index 088327fa611a5c44a063b50594d14ef235a1131e..6c374f3d8d45dc8dc7f52f7ea3c7fed321bc5676 100644 (file)
@@ -61,7 +61,6 @@
 #include "BKE_blender_undo.h"
 #include "BKE_context.h"
 #include "BKE_screen.h"
-#include "BKE_DerivedMesh.h"
 #include "BKE_global.h"
 #include "BKE_icons.h"
 #include "BKE_library.h"
@@ -75,6 +74,7 @@
 #include "BKE_addon.h"
 #include "BKE_appdir.h"
 #include "BKE_sequencer.h" /* free seq clipboard */
+#include "BKE_studiolight.h"
 #include "BKE_material.h" /* clear_matcopybuf */
 #include "BKE_tracking.h" /* free tracking clipboard */
 #include "BKE_mask.h" /* free mask clipboard */
@@ -86,9 +86,6 @@
 #include "BPY_extern.h"
 #endif
 
-#ifdef WITH_GAMEENGINE
-#  include "BL_System.h"
-#endif
 #include "GHOST_Path-api.h"
 #include "GHOST_C-api.h"
 
@@ -96,6 +93,7 @@
 
 #include "WM_api.h"
 #include "WM_types.h"
+#include "WM_message.h"
 
 #include "wm_cursors.h"
 #include "wm_event_system.h"
 #include "BLF_api.h"
 #include "BLT_lang.h"
 
-#include "GPU_buffers.h"
+#include "GPU_material.h"
 #include "GPU_draw.h"
 #include "GPU_init_exit.h"
 
-#include "BKE_depsgraph.h"
 #include "BKE_sound.h"
 #include "COM_compositor.h"
 
+#include "DEG_depsgraph.h"
+
+#include "DRW_engine.h"
+
 #ifdef WITH_OPENSUBDIV
 #  include "BKE_subsurf.h"
 #endif
@@ -135,6 +136,9 @@ CLG_LOGREF_DECLARE_GLOBAL(WM_LOG_OPERATORS, "wm.operator");
 CLG_LOGREF_DECLARE_GLOBAL(WM_LOG_HANDLERS, "wm.handler");
 CLG_LOGREF_DECLARE_GLOBAL(WM_LOG_EVENTS, "wm.event");
 CLG_LOGREF_DECLARE_GLOBAL(WM_LOG_KEYMAPS, "wm.keymap");
+CLG_LOGREF_DECLARE_GLOBAL(WM_LOG_TOOLS, "wm.tool");
+CLG_LOGREF_DECLARE_GLOBAL(WM_LOG_MSGBUS_PUB, "wm.msgbus.pub");
+CLG_LOGREF_DECLARE_GLOBAL(WM_LOG_MSGBUS_SUB, "wm.msgbus.sub");
 
 static void wm_init_reports(bContext *C)
 {
@@ -153,6 +157,42 @@ static void wm_free_reports(bContext *C)
 
 bool wm_start_with_console = false; /* used in creator.c */
 
+/**
+ * Since we cannot know in advance if we will require the draw manager
+ * context when starting blender in background mode (specially true with
+ * scripts) we deferre the ghost initialization the most as possible
+ * so that it does not break anything that can run in headless mode (as in
+ * without display server attached).
+ **/
+static bool opengl_is_init = false;
+
+void WM_init_opengl(Main *bmain)
+{
+       /* must be called only once */
+       BLI_assert(opengl_is_init == false);
+
+       if (G.background) {
+               /* Ghost is still not init elsewhere in background mode. */
+               wm_ghost_init(NULL);
+       }
+
+       /* Needs to be first to have an ogl context bound. */
+       DRW_opengl_context_create();
+
+       GPU_init();
+       GPU_set_mipmap(bmain, true);
+       GPU_set_linear_mipmap(true);
+       GPU_set_anisotropic(bmain, U.anisotropic_filter);
+       GPU_set_gpu_mipmapping(bmain, U.use_gpu_mipmap);
+
+       GPU_pass_cache_init();
+
+#ifdef WITH_OPENSUBDIV
+       BKE_subsurf_osd_init();
+#endif
+       opengl_is_init = true;
+}
+
 /* only called once, for startup */
 void WM_init(bContext *C, int argc, const char **argv)
 {
@@ -161,6 +201,7 @@ void WM_init(bContext *C, int argc, const char **argv)
                wm_ghost_init(C);   /* note: it assigns C to ghost! */
                wm_init_cursor_data();
        }
+
        GHOST_CreateSystemPaths();
 
        BKE_addon_pref_type_init();
@@ -170,34 +211,38 @@ void WM_init(bContext *C, int argc, const char **argv)
 
        WM_menutype_init();
        WM_uilisttype_init();
+       wm_manipulatortype_init();
+       wm_manipulatorgrouptype_init();
 
        ED_undosys_type_init();
 
        BKE_library_callback_free_window_manager_set(wm_close_and_free);   /* library.c */
        BKE_library_callback_free_notifier_reference_set(WM_main_remove_notifier_reference);   /* library.c */
+       BKE_region_callback_free_manipulatormap_set(wm_manipulatormap_remove); /* screen.c */
+       BKE_region_callback_refresh_tag_manipulatormap_set(WM_manipulatormap_tag_refresh);
        BKE_library_callback_remap_editor_id_reference_set(WM_main_remap_editor_id_reference);   /* library.c */
        BKE_blender_callback_test_break_set(wm_window_testbreak); /* blender.c */
        BKE_spacedata_callback_id_remap_set(ED_spacedata_id_remap); /* screen.c */
-       DAG_editors_update_cb(ED_render_id_flush_update,
-                             ED_render_scene_update,
-                             ED_render_scene_update_pre); /* depsgraph.c */
+       DEG_editors_set_update_cb(ED_render_id_flush_update,
+                                 ED_render_scene_update);
 
        ED_spacetypes_init();   /* editors/space_api/spacetype.c */
 
        ED_file_init();         /* for fsmenu */
        ED_node_init_butfuncs();
 
-       BLF_init(); /* Please update source/gamengine/GamePlayer/GPG_ghost.cpp if you change this */
+       BLF_init();
        BLT_lang_init();
 
        /* reports cant be initialized before the wm,
         * but keep before file reading, since that may report errors */
        wm_init_reports(C);
 
+       WM_msgbus_types_init();
+
        /* get the default database, plus a wm */
        wm_homefile_read(C, NULL, G.factory_startup, false, true, NULL, NULL);
 
-
        BLT_lang_set(NULL);
 
        if (!G.background) {
@@ -206,19 +251,10 @@ void WM_init(bContext *C, int argc, const char **argv)
                /* sets 3D mouse deadzone */
                WM_ndof_deadzone_set(U.ndof_deadzone);
 #endif
-
-               GPU_init();
-
-               GPU_set_mipmap(G_MAIN, !(U.gameflags & USER_DISABLE_MIPMAP));
-               GPU_set_linear_mipmap(true);
-               GPU_set_anisotropic(G_MAIN, U.anisotropic_filter);
-               GPU_set_gpu_mipmapping(G_MAIN, U.use_gpu_mipmap);
-
-#ifdef WITH_OPENSUBDIV
-               BKE_subsurf_osd_init();
-#endif
+               WM_init_opengl(G_MAIN);
 
                UI_init();
+               BKE_studiolight_init();
        }
        else {
                /* Note: Currently only inits icons, which we now want in background mode too
@@ -228,7 +264,6 @@ void WM_init(bContext *C, int argc, const char **argv)
                BKE_icons_init(1);
        }
 
-
        ED_spacemacros_init();
 
        /* note: there is a bug where python needs initializing before loading the
@@ -255,7 +290,7 @@ void WM_init(bContext *C, int argc, const char **argv)
        clear_matcopybuf();
        ED_render_clear_mtex_copybuf();
 
-       // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       // glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 
        wm_history_file_read();
 
@@ -316,96 +351,6 @@ void WM_init_splash(bContext *C)
        }
 }
 
-bool WM_init_game(bContext *C)
-{
-       wmWindowManager *wm = CTX_wm_manager(C);
-       wmWindow *win;
-
-       ScrArea *sa;
-       ARegion *ar = NULL;
-
-       Scene *scene = CTX_data_scene(C);
-
-       if (!scene) {
-               /* XXX, this should not be needed. */
-               Main *bmain = CTX_data_main(C);
-               scene = bmain->scene.first;
-       }
-
-       win = wm->windows.first;
-
-       /* first to get a valid window */
-       if (win)
-               CTX_wm_window_set(C, win);
-
-       sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_VIEW3D, 0);
-       ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
-
-       /* if we have a valid 3D view */
-       if (sa && ar) {
-               ARegion *arhide;
-
-               CTX_wm_area_set(C, sa);
-               CTX_wm_region_set(C, ar);
-
-               /* disable quad view */
-               if (ar->alignment == RGN_ALIGN_QSPLIT)
-                       WM_operator_name_call(C, "SCREEN_OT_region_quadview", WM_OP_EXEC_DEFAULT, NULL);
-
-               /* toolbox, properties panel and header are hidden */
-               for (arhide = sa->regionbase.first; arhide; arhide = arhide->next) {
-                       if (arhide->regiontype != RGN_TYPE_WINDOW) {
-                               if (!(arhide->flag & RGN_FLAG_HIDDEN)) {
-                                       ED_region_toggle_hidden(C, arhide);
-                               }
-                       }
-               }
-
-               /* full screen the area */
-               if (!sa->full) {
-                       ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
-               }
-
-               /* Fullscreen */
-               if ((scene->gm.playerflag & GAME_PLAYER_FULLSCREEN)) {
-                       WM_operator_name_call(C, "WM_OT_window_fullscreen_toggle", WM_OP_EXEC_DEFAULT, NULL);
-                       wm_get_screensize(&ar->winrct.xmax, &ar->winrct.ymax);
-                       ar->winx = ar->winrct.xmax + 1;
-                       ar->winy = ar->winrct.ymax + 1;
-               }
-               else {
-                       GHOST_RectangleHandle rect = GHOST_GetClientBounds(win->ghostwin);
-                       ar->winrct.ymax = GHOST_GetHeightRectangle(rect);
-                       ar->winrct.xmax = GHOST_GetWidthRectangle(rect);
-                       ar->winx = ar->winrct.xmax + 1;
-                       ar->winy = ar->winrct.ymax + 1;
-                       GHOST_DisposeRectangle(rect);
-               }
-
-               WM_operator_name_call(C, "VIEW3D_OT_game_start", WM_OP_EXEC_DEFAULT, NULL);
-
-               BKE_sound_exit();
-
-               return true;
-       }
-       else {
-               ReportTimerInfo *rti;
-
-               BKE_report(&wm->reports, RPT_ERROR, "No valid 3D View found, game auto start is not possible");
-
-               /* After adding the report to the global list, reset the report timer. */
-               WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
-
-               /* Records time since last report was added */
-               wm->reports.reporttimer = WM_event_add_timer(wm, CTX_wm_window(C), TIMER, 0.02);
-
-               rti = MEM_callocN(sizeof(ReportTimerInfo), "ReportTimerInfo");
-               wm->reports.reporttimer->customdata = rti;
-
-               return false;
-       }
-}
-
 /* free strings of open recent files */
 static void free_openrecent(void)
 {
@@ -483,7 +428,7 @@ void WM_exit_ext(bContext *C, const bool do_python)
                                /* save the undo state as quit.blend */
                                char filename[FILE_MAX];
                                bool has_edited;
-                               int fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_HISTORY);
+                               int fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_HISTORY);
 
                                BLI_make_file_string("/", filename, BKE_tempdir_base(), BLENDER_QUIT_FILE);
 
@@ -504,7 +449,7 @@ void WM_exit_ext(bContext *C, const bool do_python)
                        CTX_wm_window_set(C, win);  /* needed by operator close callbacks */
                        WM_event_remove_handlers(C, &win->handlers);
                        WM_event_remove_handlers(C, &win->modalhandlers);
-                       ED_screen_exit(C, win, win->screen);
+                       ED_screen_exit(C, win, WM_window_get_active_screen(win));
                }
        }
 
@@ -520,11 +465,6 @@ void WM_exit_ext(bContext *C, const bool do_python)
 
        ED_undosys_type_free();
 
-//     XXX
-//     BIF_GlobalReebFree();
-//     BIF_freeRetarget();
-       BIF_freeTemplates(C);
-
        free_openrecent();
 
        BKE_mball_cubeTable_free();
@@ -547,6 +487,14 @@ void WM_exit_ext(bContext *C, const bool do_python)
        COM_deinitialize();
 #endif
 
+       if (opengl_is_init) {
+#ifdef WITH_OPENSUBDIV
+               BKE_subsurf_osd_cleanup();
+#endif
+
+               GPU_free_unused_buffers(G_MAIN);
+       }
+
        BKE_blender_free();  /* blender.c, does entire library and spacetypes */
 //     free_matcopybuf();
        ANIM_fcurves_copybuf_free();
@@ -557,8 +505,19 @@ void WM_exit_ext(bContext *C, const bool do_python)
        ED_gpencil_strokes_copybuf_free();
        BKE_node_clipboard_clear();
 
+       /* free manipulator-maps after freeing blender, so no deleted data get accessed during cleaning up of areas */
+       wm_manipulatormaptypes_free();
+       wm_manipulatorgrouptype_free();
+       wm_manipulatortype_free();
+
        BLF_exit();
 
+       if (opengl_is_init) {
+               GPU_pass_cache_free();
+               DRW_opengl_context_destroy();
+               GPU_exit();
+       }
+
 #ifdef WITH_INTERNATIONAL
        BLF_free_unifont();
        BLF_free_unifont_mono();
@@ -586,17 +545,6 @@ void WM_exit_ext(bContext *C, const bool do_python)
        (void)do_python;
 #endif
 
-       if (!G.background) {
-#ifdef WITH_OPENSUBDIV
-               BKE_subsurf_osd_cleanup();
-#endif
-
-               GPU_global_buffer_pool_free();
-               GPU_free_unused_buffers(G_MAIN);
-
-               GPU_exit();
-       }
-
        ED_file_exit(); /* for fsmenu */
 
        UI_exit();
@@ -607,9 +555,6 @@ void WM_exit_ext(bContext *C, const bool do_python)
        wm_ghost_exit();
 
        CTX_free(C);
-#ifdef WITH_GAMEENGINE
-       SYS_DeleteSystem(SYS_GetSystem());
-#endif
 
        GHOST_DisposeSystemPaths();