]> git.cworth.org Git - apitrace/blobdiff - common/trace_tools_trace.cpp
image: Make PNG writing an Image method.
[apitrace] / common / trace_tools_trace.cpp
index ad355abcbf92b1c327d6e9671b6206410a030768..4c0082d7e51771e1d540c9ce53e5372ca27ef713 100644 (file)
@@ -33,6 +33,7 @@
 #include "os_string.hpp"
 #include "os_process.hpp"
 #include "trace_tools.hpp"
+#include "trace_resource.hpp"
 
 
 
@@ -51,57 +52,83 @@ namespace trace {
 #endif
 
 
-static os::String
-findWrapper(const char *wrapperFilename)
+static inline bool
+copyWrapper(const os::String & wrapperPath,
+            const char *programPath,
+            bool verbose)
 {
-    os::String wrapperPath;
-
-    os::String processDir = os::getProcessName();
-    processDir.trimFilename();
-
-    // Try relative build directory
-    // XXX: Just make build and install directory layout match
-    wrapperPath = processDir;
-    wrapperPath.join("wrappers");
-    wrapperPath.join(wrapperFilename);
-    if (wrapperPath.exists()) {
-        return wrapperPath;
+    os::String wrapperFilename(wrapperPath);
+    wrapperFilename.trimDirectory();
+
+    os::String tmpWrapper(programPath);
+    tmpWrapper.trimFilename();
+    tmpWrapper.join(wrapperFilename);
+
+    if (verbose) {
+        std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
     }
 
-    // Try relative install directory
-    wrapperPath = processDir;
-#if defined(_WIN32)
-    wrapperPath.join("..\\lib\\wrappers");
-#elif defined(__APPLE__)
-    wrapperPath.join("../lib/wrappers");
-#else
-    wrapperPath.join("../lib/apitrace/wrappers");
-#endif
-    wrapperPath.join(wrapperFilename);
-    if (wrapperPath.exists()) {
-        return wrapperPath;
+    if (tmpWrapper.exists()) {
+        std::cerr << "error: not overwriting " << tmpWrapper << "\n";
+        return false;
     }
 
-#ifndef _WIN32
-    // Try absolute install directory
-    wrapperPath = APITRACE_WRAPPER_INSTALL_DIR;
-    wrapperPath.join(wrapperFilename);
-    if (wrapperPath.exists()) {
-        return wrapperPath;
+    if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
+        std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
+        return false;
     }
-#endif
 
-    return "";
+    return true;
 }
 
 
+static const char *glWrappers[] = {
+    GL_TRACE_WRAPPER,
+    NULL
+};
+
+#ifdef EGL_TRACE_WRAPPER
+static const char *eglWrappers[] = {
+    EGL_TRACE_WRAPPER,
+    NULL
+};
+#endif
+
+#ifdef _WIN32
+static const char *d3d7Wrappers[] = {
+    "ddraw.dll",
+    NULL
+};
+
+static const char *d3d8Wrappers[] = {
+    "d3d8.dll",
+    NULL
+};
+
+static const char *d3d9Wrappers[] = {
+    "d3d9.dll",
+    NULL
+};
+
+static const char *dxgiWrappers[] = {
+    "dxgitrace.dll",
+    //"dxgi.dll",
+    "d3d10.dll",
+    "d3d10_1.dll",
+    "d3d11.dll",
+    NULL
+};
+#endif
+
 int
 traceProgram(API api,
              char * const *argv,
              const char *output,
              bool verbose)
 {
-    const char *wrapperFilename;
+    const char **wrapperFilenames;
+    unsigned numWrappers;
+    int status = 1;
 
     /*
      * TODO: simplify code
@@ -109,31 +136,27 @@ traceProgram(API api,
 
     switch (api) {
     case API_GL:
-        wrapperFilename = GL_TRACE_WRAPPER;
+        wrapperFilenames = glWrappers;
         break;
 #ifdef EGL_TRACE_WRAPPER
     case API_EGL:
-        wrapperFilename = EGL_TRACE_WRAPPER;
+        wrapperFilenames = eglWrappers;
         break;
 #endif
 #ifdef _WIN32
     case API_D3D7:
-        wrapperFilename = "ddraw.dll";
+        wrapperFilenames = d3d7Wrappers;
         break;
     case API_D3D8:
-        wrapperFilename = "d3d8.dll";
+        wrapperFilenames = d3d8Wrappers;
         break;
     case API_D3D9:
-        wrapperFilename = "d3d9.dll";
+        wrapperFilenames = d3d9Wrappers;
         break;
     case API_D3D10:
-        wrapperFilename = "d3d10.dll";
-        break;
     case API_D3D10_1:
-        wrapperFilename = "d3d10_1.dll";
-        break;
     case API_D3D11:
-        wrapperFilename = "d3d11.dll";
+        wrapperFilenames = dxgiWrappers;
         break;
 #endif
     default:
@@ -141,48 +164,45 @@ traceProgram(API api,
         return 1;
     }
 
-    os::String wrapperPath = findWrapper(wrapperFilename);
-
-    if (!wrapperPath.length()) {
-        std::cerr << "error: failed to find " << wrapperFilename << "\n";
-        return 1;
+    numWrappers = 0;
+    while (wrapperFilenames[numWrappers]) {
+        ++numWrappers;
     }
 
-#if defined(_WIN32)
-    /* On Windows copy the wrapper to the program directory.
-     */
-    os::String tmpWrapper(argv[0]);
-    tmpWrapper.trimFilename();
-    tmpWrapper.join(wrapperFilename);
+    unsigned i;
+    for (i = 0; i < numWrappers; ++i) {
+        const char *wrapperFilename = wrapperFilenames[i];
 
-    if (verbose) {
-        std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
-    }
+        os::String wrapperPath = findWrapper(wrapperFilename);
 
-    if (tmpWrapper.exists()) {
-        std::cerr << "error: not overwriting " << tmpWrapper << "\n";
-        return 1;
-    }
+        if (!wrapperPath.length()) {
+            std::cerr << "error: failed to find " << wrapperFilename << "\n";
+            goto exit;
+        }
 
-    if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
-        std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
-        return 1;
-    }
+#if defined(_WIN32)
+        /* On Windows copy the wrapper to the program directory.
+         */
+        if (!copyWrapper(wrapperPath, argv[0], verbose)) {
+            goto exit;
+        }
 #endif /* _WIN32 */
 
 #if defined(__APPLE__)
-    /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
-     * directory, not the file. */
-    wrapperPath.trimFilename();
+        /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
+         * directory, not the file. */
+        wrapperPath.trimFilename();
 #endif
 
 #if defined(TRACE_VARIABLE)
-    if (verbose) {
-        std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
-    }
-    /* FIXME: Don't modify the current environment */
-    os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
+        assert(numWrappers == 1);
+        if (verbose) {
+            std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
+        }
+        /* FIXME: Don't modify the current environment */
+        os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
 #endif /* TRACE_VARIABLE */
+    }
 
     if (output) {
         os::setEnvironment("TRACE_FILE", output);
@@ -197,13 +217,20 @@ traceProgram(API api,
         std::cerr << "\n";
     }
 
-    int status = os::execute(argv);
+    status = os::execute(argv);
 
+exit:
 #if defined(TRACE_VARIABLE)
     os::unsetEnvironment(TRACE_VARIABLE);
 #endif
 #if defined(_WIN32)
-    os::removeFile(tmpWrapper);
+    for (unsigned j = 0; j < i; ++j) {
+        const char *wrapperFilename = wrapperFilenames[j];
+        os::String tmpWrapper(argv[0]);
+        tmpWrapper.trimFilename();
+        tmpWrapper.join(wrapperFilename);
+        os::removeFile(tmpWrapper);
+    }
 #endif
 
     if (output) {