1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#include "nine_debug.h"
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include <ctype.h>
26bf215546Sopenharmony_ci#include "c11/threads.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_cistatic const struct debug_named_value nine_debug_flags[] = {
29bf215546Sopenharmony_ci    { "unknown", DBG_UNKNOWN,              "IUnknown implementation." },
30bf215546Sopenharmony_ci    { "adapter", DBG_ADAPTER,              "ID3D9Adapter implementation." },
31bf215546Sopenharmony_ci    { "overlay", DBG_OVERLAYEXTENSION,     "IDirect3D9ExOverlayExtension implementation." },
32bf215546Sopenharmony_ci    { "auth",    DBG_AUTHENTICATEDCHANNEL, "IDirect3DAuthenticatedChannel9 implementation." },
33bf215546Sopenharmony_ci    { "basetex", DBG_BASETEXTURE,          "IDirect3DBaseTexture9 implementation." },
34bf215546Sopenharmony_ci    { "crypto",  DBG_CRYPTOSESSION,        "IDirect3DCryptoSession9 implementation." },
35bf215546Sopenharmony_ci    { "cubetex", DBG_CUBETEXTURE,          "IDirect3DCubeTexture9 implementation." },
36bf215546Sopenharmony_ci    { "device",  DBG_DEVICE,               "IDirect3DDevice9(Ex) implementation." },
37bf215546Sopenharmony_ci    { "video",   DBG_DEVICEVIDEO,          "IDirect3DDeviceVideo9 implementation." },
38bf215546Sopenharmony_ci    { "ibuf",    DBG_INDEXBUFFER,          "IDirect3DIndexBuffer9 implementation." },
39bf215546Sopenharmony_ci    { "ps",      DBG_PIXELSHADER,          "IDirect3DPixelShader9 implementation." },
40bf215546Sopenharmony_ci    { "query",   DBG_QUERY,                "IDirect3DQuery9 implementation." },
41bf215546Sopenharmony_ci    { "res",     DBG_RESOURCE,             "IDirect3DResource9 implementation." },
42bf215546Sopenharmony_ci    { "state",   DBG_STATEBLOCK,           "IDirect3DStateBlock9 implementation." },
43bf215546Sopenharmony_ci    { "surf",    DBG_SURFACE,              "IDirect3DSurface9 implementation." },
44bf215546Sopenharmony_ci    { "swap",    DBG_SWAPCHAIN,            "IDirect3DSwapChain9(Ex) implementation." },
45bf215546Sopenharmony_ci    { "tex",     DBG_TEXTURE,              "IDirect3DTexture9 implementation." },
46bf215546Sopenharmony_ci    { "vbuf",    DBG_VERTEXBUFFER,         "IDirect3DVertexBuffer9 implementation." },
47bf215546Sopenharmony_ci    { "vdecl",   DBG_VERTEXDECLARATION,    "IDirect3DVertexDeclaration9 implementation." },
48bf215546Sopenharmony_ci    { "vs",      DBG_VERTEXSHADER,         "IDirect3DVertexShader9 implementation." },
49bf215546Sopenharmony_ci    { "3dsurf",  DBG_VOLUME,               "IDirect3DVolume9 implementation." },
50bf215546Sopenharmony_ci    { "3dtex",   DBG_VOLUMETEXTURE,        "IDirect3DVolumeTexture9 implementation." },
51bf215546Sopenharmony_ci    { "shader",  DBG_SHADER,               "Shader token stream translator." },
52bf215546Sopenharmony_ci    { "ff",      DBG_FF,                   "Fixed function emulation." },
53bf215546Sopenharmony_ci    { "user",    DBG_USER,                 "User errors, both fixable and unfixable." },
54bf215546Sopenharmony_ci    { "error",   DBG_ERROR,                "Driver errors, always visible." },
55bf215546Sopenharmony_ci    { "warn",    DBG_WARN,                 "Driver warnings, always visible in debug builds." },
56bf215546Sopenharmony_ci    { "tid",     DBG_TID,                  "Display thread-ids." },
57bf215546Sopenharmony_ci    DEBUG_NAMED_VALUE_END
58bf215546Sopenharmony_ci};
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_civoid
61bf215546Sopenharmony_ci_nine_debug_printf( unsigned long flag,
62bf215546Sopenharmony_ci                    const char *func,
63bf215546Sopenharmony_ci                    const char *fmt,
64bf215546Sopenharmony_ci                    ... )
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci    static boolean first = TRUE;
67bf215546Sopenharmony_ci    static unsigned long dbg_flags = DBG_ERROR | DBG_WARN;
68bf215546Sopenharmony_ci    unsigned long tid = 0;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci    if (first) {
71bf215546Sopenharmony_ci        first = FALSE;
72bf215546Sopenharmony_ci        dbg_flags |= debug_get_flags_option("NINE_DEBUG", nine_debug_flags, 0);
73bf215546Sopenharmony_ci    }
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci#if defined(HAVE_PTHREAD)
76bf215546Sopenharmony_ci    if (dbg_flags & DBG_TID)
77bf215546Sopenharmony_ci        tid = (unsigned long)pthread_self();
78bf215546Sopenharmony_ci#endif
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci    if (dbg_flags & flag) {
81bf215546Sopenharmony_ci        const char *f = func ? strrchr(func, '_') : NULL;
82bf215546Sopenharmony_ci        va_list ap;
83bf215546Sopenharmony_ci        /* inside a class this will print nine:tid:classinlowercase:func: while
84bf215546Sopenharmony_ci         * outside a class (rarely used) it will just print nine:tid:func
85bf215546Sopenharmony_ci         * the reason for lower case is simply to match the filenames, as it
86bf215546Sopenharmony_ci         * will also strip off the "Nine" */
87bf215546Sopenharmony_ci        if (f && strncmp(func, "Nine", 4) == 0) {
88bf215546Sopenharmony_ci            char klass[96]; /* no class name is this long */
89bf215546Sopenharmony_ci            char *ptr = klass;
90bf215546Sopenharmony_ci            for (func += 4; func != f; ++func) { *ptr++ = tolower(*func); }
91bf215546Sopenharmony_ci            *ptr = '\0';
92bf215546Sopenharmony_ci            if (tid)
93bf215546Sopenharmony_ci                _debug_printf("nine:0x%08lx:%s:%s: ", tid, klass, ++f);
94bf215546Sopenharmony_ci            else
95bf215546Sopenharmony_ci                _debug_printf("nine:%s:%s: ", klass, ++f);
96bf215546Sopenharmony_ci        } else if (func) {
97bf215546Sopenharmony_ci            if (tid)
98bf215546Sopenharmony_ci                _debug_printf("nine:0x%08lx:%s ", tid, func);
99bf215546Sopenharmony_ci            else
100bf215546Sopenharmony_ci                _debug_printf("nine:%s ", func);
101bf215546Sopenharmony_ci        }
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci        va_start(ap, fmt);
104bf215546Sopenharmony_ci        _debug_vprintf(fmt, ap);
105bf215546Sopenharmony_ci        va_end(ap);
106bf215546Sopenharmony_ci    }
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_civoid
110bf215546Sopenharmony_ci_nine_stub( const char *file,
111bf215546Sopenharmony_ci            const char *func,
112bf215546Sopenharmony_ci            unsigned line )
113bf215546Sopenharmony_ci{
114bf215546Sopenharmony_ci    const char *r = strrchr(file, '/');
115bf215546Sopenharmony_ci    if (r == NULL) { r = strrchr(file, '\\'); }
116bf215546Sopenharmony_ci    _debug_printf("nine:%s:%d: %s STUB!\n", r ? ++r : file, line, func);
117bf215546Sopenharmony_ci}
118