Ghost Context Refactor
[blender-staging.git] / source / creator / creator.c
index dbb7bef972352e1efcb7381fb43b748b43c4bd40..12ae5c502e4f2344df3d0e696f7f2ad6f164ca9b 100644 (file)
 #  include <xmmintrin.h>
 #endif
 
-/* crash handler */
-#ifdef WIN32
-#  include <process.h> /* getpid */
-#else
-#  include <unistd.h> /* getpid */
-#endif
-
 #ifdef WIN32
+#  if defined(_MSC_VER) && _MSC_VER >= 1800 && defined(_M_X64)
+#    include <math.h> /* needed for _set_FMA3_enable */
+#  endif
 #  include <windows.h>
 #  include "utfconv.h"
 #endif
@@ -79,6 +75,8 @@
 #include "BLI_callbacks.h"
 #include "BLI_blenlib.h"
 #include "BLI_mempool.h"
+#include "BLI_system.h"
+#include BLI_SYSTEM_PID_H
 
 #include "DNA_ID.h"
 #include "DNA_scene_types.h"
 #include "BKE_main.h"
 #include "BKE_material.h"
 #include "BKE_modifier.h"
-#include "BKE_packedFile.h"
 #include "BKE_scene.h"
 #include "BKE_node.h"
 #include "BKE_report.h"
 #include "BKE_sound.h"
 #include "BKE_image.h"
+#include "BKE_particle.h"
 
 #include "IMB_imbuf.h"  /* for IMB_init */
 
 #include "RE_pipeline.h"
 
 #include "ED_datafiles.h"
+#include "ED_util.h"
 
 #include "WM_api.h"
 
 #  include "libmv-capi.h"
 #endif
 
+#ifdef WITH_CYCLES_LOGGING
+#  include "CCL_api.h"
+#endif
+
 /* from buildinfo.c */
 #ifdef BUILD_DATE
 extern char build_date[];
@@ -208,7 +211,7 @@ static void blender_esc(int sig)
 {
        static int count = 0;
        
-       G.is_break = TRUE;  /* forces render loop to read queue, not sure if its needed */
+       G.is_break = true;  /* forces render loop to read queue, not sure if its needed */
        
        if (sig == 2) {
                if (count) {
@@ -309,6 +312,9 @@ static int print_help(int UNUSED(argc), const char **UNUSED(argv), void *data)
        BLI_argsPrintArgDoc(ba, "--debug-handlers");
 #ifdef WITH_LIBMV
        BLI_argsPrintArgDoc(ba, "--debug-libmv");
+#endif
+#ifdef WITH_CYCLES_LOGGING
+       BLI_argsPrintArgDoc(ba, "--debug-cycles");
 #endif
        BLI_argsPrintArgDoc(ba, "--debug-memory");
        BLI_argsPrintArgDoc(ba, "--debug-jobs");
@@ -451,6 +457,15 @@ static int debug_mode_libmv(int UNUSED(argc), const char **UNUSED(argv), void *U
 }
 #endif
 
+#ifdef WITH_CYCLES_LOGGING
+static int debug_mode_cycles(int UNUSED(argc), const char **UNUSED(argv),
+                             void *UNUSED(data))
+{
+       CCL_start_debug_logging();
+       return 0;
+}
+#endif
+
 static int debug_mode_memory(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
 {
        MEM_set_memory_debug();
@@ -536,10 +551,10 @@ static void blender_crash_handler_backtrace(FILE *fp)
 
        process = GetCurrentProcess();
 
-       SymInitialize(process, NULL, TRUE);
+       SymInitialize(process, NULL, true);
 
        nframes = CaptureStackBackTrace(0, SIZE, stack, NULL);
-       symbolinfo = MEM_callocN(sizeof(SYMBOL_INFO) + MAXSYMBOL * sizeof( char ), "crash Symbol table");
+       symbolinfo = MEM_callocN(sizeof(SYMBOL_INFO) + MAXSYMBOL * sizeof(char), "crash Symbol table");
        symbolinfo->MaxNameLen = MAXSYMBOL - 1;
        symbolinfo->SizeOfStruct = sizeof(SYMBOL_INFO);
 
@@ -570,7 +585,7 @@ static void blender_crash_handler(int signum)
                char fname[FILE_MAX];
 
                if (!G.main->name[0]) {
-                       BLI_make_file_string("/", fname, BLI_temporary_dir(), "crash.blend");
+                       BLI_make_file_string("/", fname, BLI_temp_dir_base(), "crash.blend");
                }
                else {
                        BLI_strncpy(fname, G.main->name, sizeof(fname));
@@ -591,10 +606,10 @@ static void blender_crash_handler(int signum)
        char fname[FILE_MAX];
 
        if (!G.main->name[0]) {
-               BLI_join_dirfile(fname, sizeof(fname), BLI_temporary_dir(), "blender.crash.txt");
+               BLI_join_dirfile(fname, sizeof(fname), BLI_temp_dir_base(), "blender.crash.txt");
        }
        else {
-               BLI_join_dirfile(fname, sizeof(fname), BLI_temporary_dir(), BLI_path_basename(G.main->name));
+               BLI_join_dirfile(fname, sizeof(fname), BLI_temp_dir_base(), BLI_path_basename(G.main->name));
                BLI_replace_extension(fname, sizeof(fname), ".crash.txt");
        }
 
@@ -625,6 +640,8 @@ static void blender_crash_handler(int signum)
                fclose(fp);
        }
 
+       /* Delete content of temp dir! */
+       BLI_temp_dir_session_purge();
 
        /* really crash */
        signal(signum, SIG_DFL);
@@ -816,6 +833,9 @@ static int set_engine(int argc, const char **argv, void *data)
                                if (BLI_findstring(&R_engines, argv[1], offsetof(RenderEngineType, idname))) {
                                        BLI_strncpy_utf8(rd->engine, argv[1], sizeof(rd->engine));
                                }
+                               else {
+                                       printf("\nError: engine not found '%s'\n", argv[1]);
+                               }
                        }
                        else {
                                printf("\nError: no blend loaded. order the arguments so '-E  / --engine ' is after a blend is loaded.\n");
@@ -883,6 +903,8 @@ static int set_verbosity(int argc, const char **argv, void *UNUSED(data))
 
 #ifdef WITH_LIBMV
                libmv_setLoggingVerbosity(level);
+#elif defined(WITH_CYCLES_LOGGING)
+               CCL_logging_verbosity_set(level);
 #else
                (void)level;
 #endif
@@ -1003,6 +1025,7 @@ static int render_frame(int argc, const char **argv, void *data)
                                        break;
                        }
 
+                       BLI_begin_threaded_malloc();
                        BKE_reports_init(&reports, RPT_PRINT);
 
                        frame = CLAMPIS(frame, MINAFRAME, MAXFRAME);
@@ -1010,6 +1033,7 @@ static int render_frame(int argc, const char **argv, void *data)
                        RE_SetReports(re, &reports);
                        RE_BlenderAnim(re, bmain, scene, NULL, scene->lay, frame, frame, scene->r.frame_step);
                        RE_SetReports(re, NULL);
+                       BLI_end_threaded_malloc();
                        return 1;
                }
                else {
@@ -1031,10 +1055,12 @@ static int render_animation(int UNUSED(argc), const char **UNUSED(argv), void *d
                Main *bmain = CTX_data_main(C);
                Render *re = RE_NewRender(scene->id.name);
                ReportList reports;
+               BLI_begin_threaded_malloc();
                BKE_reports_init(&reports, RPT_PRINT);
                RE_SetReports(re, &reports);
                RE_BlenderAnim(re, bmain, scene, NULL, scene->lay, scene->r.sfra, scene->r.efra, scene->r.frame_step);
                RE_SetReports(re, NULL);
+               BLI_end_threaded_malloc();
        }
        else {
                printf("\nError: no blend loaded. cannot use '-a'.\n");
@@ -1253,7 +1279,11 @@ static int load_file(int UNUSED(argc), const char **argv, void *data)
        BLI_path_cwd(filename);
 
        if (G.background) {
-               int retval = BKE_read_file(C, filename, NULL);
+               int retval;
+
+               BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_PRE);
+
+               retval = BKE_read_file(C, filename, NULL);
 
                /* we successfully loaded a blend file, get sure that
                 * pointcache works */
@@ -1266,7 +1296,7 @@ static int load_file(int UNUSED(argc), const char **argv, void *data)
                                extern void wm_add_default(bContext *C);
 
                                /* wm_add_default() needs the screen to be set. */
-                               CTX_wm_screen_set(C, CTX_data_main(C)->screen.first);
+                               CTX_wm_screen_set(C, bmain->screen.first);
                                wm_add_default(C);
                        }
 
@@ -1275,7 +1305,9 @@ static int load_file(int UNUSED(argc), const char **argv, void *data)
                        G.relbase_valid = 1;
                        if (CTX_wm_manager(C) == NULL) CTX_wm_manager_set(C, wm);  /* reset wm */
 
-                       DAG_on_visible_update(bmain, TRUE);
+                       /* WM_file_read would call normally */
+                       ED_editors_init(C);
+                       DAG_on_visible_update(bmain, true);
                        BKE_scene_update_tagged(bmain->eval_ctx, bmain, CTX_data_scene(C));
                }
                else {
@@ -1289,6 +1321,8 @@ static int load_file(int UNUSED(argc), const char **argv, void *data)
                BPY_python_reset(C);
 #endif
 
+               BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_POST);
+
                /* happens for the UI on file reading too (huh? (ton))*/
                // XXX          BKE_reset_undo();
                //                      BKE_write_undo("original");     /* save current state */
@@ -1402,6 +1436,9 @@ static void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle)
 
 #ifdef WITH_LIBMV
        BLI_argsAdd(ba, 1, NULL, "--debug-libmv", "\n\tEnable debug messages from libmv library", debug_mode_libmv, NULL);
+#endif
+#ifdef WITH_CYCLES_LOGGING
+       BLI_argsAdd(ba, 1, NULL, "--debug-cycles", "\n\tEnable debug messages from Cycles", debug_mode_cycles, NULL);
 #endif
        BLI_argsAdd(ba, 1, NULL, "--debug-memory", "\n\tEnable fully guarded memory allocation and debugging", debug_mode_memory, NULL);
 
@@ -1468,12 +1505,21 @@ char **environ = NULL;
 #  endif
 #endif
 
-
+/**
+ * Blender's main function responsabilities are:
+ * - setup subsystems.
+ * - handle arguments.
+ * - run WM_main() event loop,
+ *   or exit when running in background mode.
+ */
+int main(
+       int argc,
 #ifdef WIN32
-int main(int argc, const char **UNUSED(argv_c)) /* Do not mess with const */
+        const char **UNUSED(argv_c)
 #else
-int main(int argc, const char **argv)
+        const char **argv
 #endif
+         )
 {
        bContext *C;
        SYS_SystemHandle syshandle;
@@ -1482,7 +1528,13 @@ int main(int argc, const char **argv)
        bArgs *ba;
 #endif
 
-#ifdef WIN32 /* Win32 Unicode Args */
+#ifdef WIN32
+       /* FMA3 support in the 2013 CRT is broken on Vista and Windows 7 RTM (fixed in SP1). Just disable it. */
+#  if defined(_MSC_VER) && _MSC_VER >= 1800 && defined(_M_X64)
+       _set_FMA3_enable(0);
+#  endif
+
+       /* Win32 Unicode Args */
        /* NOTE: cannot use guardedalloc malloc here, as it's not yet initialized
         *       (it depends on the args passed in, which is what we're getting here!)
         */
@@ -1552,24 +1604,11 @@ int main(int argc, const char **argv)
 
 #ifdef WITH_LIBMV
        libmv_initLogging(argv[0]);
+#elif defined(WITH_CYCLES_LOGGING)
+       CCL_init_logging(argv[0]);
 #endif
 
        setCallbacks();
-#if defined(__APPLE__) && !defined(WITH_PYTHON_MODULE)
-       /* patch to ignore argument finder gives us (pid?) */
-       if (argc == 2 && strncmp(argv[1], "-psn_", 5) == 0) {
-               extern int GHOST_HACK_getFirstFile(char buf[]);
-               static char firstfilebuf[512];
-
-               argc = 1;
-
-               if (GHOST_HACK_getFirstFile(firstfilebuf)) {
-                       argc = 2;
-                       argv[1] = firstfilebuf;
-               }
-       }
-
-#endif
 
 #ifdef __FreeBSD__
        fpsetmask(0);
@@ -1622,6 +1661,7 @@ int main(int argc, const char **argv)
 
        RE_engines_init();
        init_nodesystem();
+       psys_init_rng();
        /* end second init */
 
 
@@ -1652,7 +1692,7 @@ int main(int argc, const char **argv)
 
                /* this is properly initialized with user defs, but this is default */
                /* call after loading the startup.blend so we can read U.tempdir */
-               BLI_init_temporary_dir(U.tempdir);
+               BLI_temp_dir_init(U.tempdir);
        }
        else {
 #ifndef WITH_PYTHON_MODULE
@@ -1662,7 +1702,7 @@ int main(int argc, const char **argv)
                WM_init(C, argc, (const char **)argv);
 
                /* don't use user preferences temp dir */
-               BLI_init_temporary_dir(NULL);
+               BLI_temp_dir_init(NULL);
        }
 #ifdef WITH_PYTHON
        /**