OpenSubdiv: Commit of OpenSubdiv integration into Blender
[blender.git] / source / blender / windowmanager / intern / wm_init_exit.c
index 675eb5e2e3a992bb22f42667bb70f7c34069cfc9..ad7044c62187c9d4b3a1612c90f1192727a9901a 100644 (file)
@@ -40,9 +40,6 @@
 
 #include "MEM_guardedalloc.h"
 
-#include "IMB_imbuf_types.h"
-#include "IMB_imbuf.h"
-
 #include "DNA_scene_types.h"
 #include "DNA_userdef_types.h"
 #include "DNA_windowmanager_types.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 
+#include "BLO_writefile.h"
+
 #include "BKE_blender.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"
 #include "BKE_main.h"
-#include "BKE_mball.h"
+#include "BKE_mball_tessellate.h"
 #include "BKE_node.h"
 #include "BKE_report.h"
 
 #include "BKE_addon.h"
+#include "BKE_appdir.h"
 #include "BKE_sequencer.h" /* free seq clipboard */
 #include "BKE_material.h" /* clear_matcopybuf */
 #include "BKE_tracking.h" /* free tracking clipboard */
@@ -96,6 +97,7 @@
 #include "wm_window.h"
 
 #include "ED_armature.h"
+#include "ED_gpencil.h"
 #include "ED_keyframing.h"
 #include "ED_node.h"
 #include "ED_render.h"
 #include "BLF_translation.h"
 
 #include "GPU_buffers.h"
-#include "GPU_extensions.h"
 #include "GPU_draw.h"
+#include "GPU_init_exit.h"
 
 #include "BKE_depsgraph.h"
 #include "BKE_sound.h"
 #include "COM_compositor.h"
 
+#ifdef WITH_OPENSUBDIV
+#  include "opensubdiv_capi.h"
+#endif
+
 static void wm_init_reports(bContext *C)
 {
-       BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
+       ReportList *reports = CTX_wm_reports(C);
+
+       BLI_assert(!reports || BLI_listbase_is_empty(&reports->list));
+
+       BKE_reports_init(reports, RPT_STORE);
 }
 static void wm_free_reports(bContext *C)
 {
-       BKE_reports_clear(CTX_wm_reports(C));
+       ReportList *reports = CTX_wm_reports(C);
+
+       BKE_reports_clear(reports);
 }
 
 bool wm_start_with_console = false; /* used in creator.c */
@@ -142,9 +154,11 @@ void WM_init(bContext *C, int argc, const char **argv)
        WM_menutype_init();
        WM_uilisttype_init();
 
-       set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
-       set_free_notifier_reference_cb(WM_main_remove_notifier_reference);   /* library.c */
-       set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
+       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_library_callback_free_editor_id_reference_set(WM_main_remove_editor_id_reference);   /* library.c */
+       BKE_blender_callback_test_break_set(wm_window_testbreak); /* blender.c */
+       BKE_spacedata_callback_id_unref_set(ED_spacedata_id_unref); /* screen.c */
        DAG_editors_update_cb(ED_render_id_flush_update, ED_render_scene_update); /* depsgraph.c */
        
        ED_spacetypes_init();   /* editors/space_api/spacetype.c */
@@ -161,8 +175,33 @@ void WM_init(bContext *C, int argc, const char **argv)
        /* get the default database, plus a wm */
        wm_homefile_read(C, NULL, G.factory_startup, NULL);
        
+
        BLF_lang_set(NULL);
 
+       if (!G.background) {
+               /* sets 3D mouse deadzone */
+               WM_ndof_deadzone_set(U.ndof_deadzone);
+
+               GPU_init();
+
+               GPU_set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
+               GPU_set_linear_mipmap(true);
+               GPU_set_anisotropic(U.anisotropic_filter);
+               GPU_set_gpu_mipmapping(U.use_gpu_mipmap);
+
+               UI_init();
+       }
+       else {
+               /* Note: Currently only inits icons, which we now want in background mode too
+                * (scripts could use those in background processing...).
+                * In case we do more later, we may need to pass a 'background' flag.
+                * Called from 'UI_init' above */
+               BKE_icons_init(1);
+       }
+
+
+       ED_spacemacros_init();
+
        /* note: there is a bug where python needs initializing before loading the
         * startup.blend because it may contain PyDrivers. It also needs to be after
         * initializing space types and other internal data.
@@ -186,15 +225,6 @@ void WM_init(bContext *C, int argc, const char **argv)
 
        wm_init_reports(C); /* reports cant be initialized before the wm */
 
-       if (!G.background) {
-               GPU_extensions_init();
-               GPU_set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
-               GPU_set_anisotropic(U.anisotropic_filter);
-               GPU_set_gpu_mipmapping(U.use_gpu_mipmap);
-
-               UI_init();
-       }
-       
        clear_matcopybuf();
        ED_render_clear_mtex_copybuf();
 
@@ -207,7 +237,7 @@ void WM_init(bContext *C, int argc, const char **argv)
        /* allow a path of "", this is what happens when making a new file */
 #if 0
        if (G.main->name[0] == 0)
-               BLI_make_file_string("/", G.main->name, BLI_getDefaultDocumentFolder(), "untitled.blend");
+               BLI_make_file_string("/", G.main->name, BKE_appdir_folder_default(), "untitled.blend");
 #endif
 
        BLI_strncpy(G.lib, G.main->name, FILE_MAX);
@@ -230,6 +260,7 @@ void WM_init(bContext *C, int argc, const char **argv)
                 *
                 * unlikely any handlers are set but its possible,
                 * note that recovering the last session does its own callbacks. */
+               BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_VERSION_UPDATE);
                BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_POST);
        }
 }
@@ -295,7 +326,7 @@ bool WM_init_game(bContext *C)
 
                /* full screen the area */
                if (!sa->full) {
-                       ED_screen_full_toggle(C, win, sa);
+                       ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
                }
 
                /* Fullscreen */
@@ -316,7 +347,7 @@ bool WM_init_game(bContext *C)
 
                WM_operator_name_call(C, "VIEW3D_OT_game_start", WM_OP_EXEC_DEFAULT, NULL);
 
-               sound_exit();
+               BKE_sound_exit();
 
                return true;
        }
@@ -386,7 +417,7 @@ void WM_exit_ext(bContext *C, const bool do_python)
 {
        wmWindowManager *wm = C ? CTX_wm_manager(C) : NULL;
 
-       sound_exit();
+       BKE_sound_exit();
 
        /* first wrap up running stuff, we assume only the active WM is running */
        /* modal handlers are on window level freed, others too? */
@@ -395,14 +426,21 @@ void WM_exit_ext(bContext *C, const bool do_python)
                wmWindow *win;
 
                if (!G.background) {
-                       if ((U.uiflag2 & USER_KEEP_SESSION) || BKE_undo_valid(NULL)) {
+                       if ((U.uiflag2 & USER_KEEP_SESSION) || BKE_undo_is_valid(NULL)) {
                                /* save the undo state as quit.blend */
                                char filename[FILE_MAX];
-                               
-                               BLI_make_file_string("/", filename, BLI_temporary_dir(), BLENDER_QUIT_FILE);
+                               bool has_edited;
+                               int fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN | G_FILE_HISTORY);
+
+                               BLI_make_file_string("/", filename, BKE_tempdir_base(), BLENDER_QUIT_FILE);
 
-                               if (BKE_undo_save_file(filename))
+                               has_edited = ED_editors_flush_edits(C, false);
+
+                               if ((has_edited && BLO_write_file(CTX_data_main(C), filename, fileflags, NULL, NULL)) ||
+                                   BKE_undo_save_file(filename))
+                               {
                                        printf("Saved session recovery to '%s'\n", filename);
+                               }
                        }
                }
                
@@ -458,6 +496,7 @@ void WM_exit_ext(bContext *C, const bool do_python)
        free_anim_copybuf();
        free_anim_drivers_copybuf();
        free_fmodifiers_copybuf();
+       ED_gpencil_strokes_copybuf_free();
        ED_clipboard_posebuf_free();
        BKE_node_clipboard_clear();
 
@@ -490,13 +529,18 @@ void WM_exit_ext(bContext *C, const bool do_python)
        (void)do_python;
 #endif
 
+#ifdef WITH_OPENSUBDIV
+       openSubdiv_cleanup();
+#endif
+
        if (!G.background) {
                GPU_global_buffer_pool_free();
                GPU_free_unused_buffers();
-               GPU_extensions_exit();
+
+               GPU_exit();
        }
 
-       BKE_reset_undo(); 
+       BKE_undo_reset();
        
        ED_file_exit(); /* for fsmenu */
 
@@ -517,10 +561,15 @@ void WM_exit_ext(bContext *C, const bool do_python)
        BLI_threadapi_exit();
 
        if (MEM_get_memory_blocks_in_use() != 0) {
-               printf("Error: Not freed memory blocks: %d\n", MEM_get_memory_blocks_in_use());
+               size_t mem_in_use = MEM_get_memory_in_use() + MEM_get_memory_in_use();
+               printf("Error: Not freed memory blocks: %u, total unfreed memory %f MB\n",
+                      MEM_get_memory_blocks_in_use(),
+                      (double)mem_in_use / 1024 / 1024);
                MEM_printmemlist();
        }
        wm_autosave_delete();
+
+       BKE_tempdir_session_purge();
 }
 
 void WM_exit(bContext *C)