summaryrefslogtreecommitdiff
path: root/wrappers/d3d9trace.py
blob: 5b966cb056f02e1dedb6c34ad140022a8d305791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
##########################################################################
#
# Copyright 2008-2009 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/


from dlltrace import DllTracer
from specs.stdapi import API, Pointer, ObjPointer
from specs.d3d9 import d3d9, D3DSHADER9, IDirect3DSwapChain9Ex, d3dperf
from specs.dxva2 import dxva2


class D3D9Tracer(DllTracer):

    def serializeArgValue(self, function, arg):
        # Dump shaders as strings
        if arg.type is D3DSHADER9:
            print '    DumpShader(trace::localWriter, %s);' % (arg.name)
            return

        DllTracer.serializeArgValue(self, function, arg)

    def wrapArg(self, function, arg):
        # Correctly handle the wrapping of IDirect3DSwapChain9Ex objects
        if function.name in ('GetSwapChain', 'CreateAdditionalSwapChain') \
           and self.interface.name == 'IDirect3DDevice9Ex' \
           and arg.name == 'pSwapChain':
            self.wrapValue(Pointer(ObjPointer(IDirect3DSwapChain9Ex)), '((IDirect3DSwapChain9Ex**)pSwapChain)')
            return

        DllTracer.wrapArg(self, function, arg)

    def enumWrapperInterfaceVariables(self, interface):
        variables = DllTracer.enumWrapperInterfaceVariables(self, interface)
        
        # Add additional members to track locks
        if interface.getMethodByName('Lock') is not None or \
           interface.getMethodByName('LockRect') is not None or \
           interface.getMethodByName('LockBox') is not None:
            if interface.base.name == 'IDirect3DBaseTexture9':
                variables += [
                    ('std::map<UINT, std::pair<size_t, VOID *> >', '_MappedData', 'std::map<UINT, std::pair<size_t, VOID *> >()'),
                ]
            else:
                variables += [
                    ('size_t', '_MappedSize', '0'),
                    ('VOID *', 'm_pbData', '0'),
                ]

        if interface.name == 'IDirectXVideoDecoder':
            variables += [
                ('std::map<UINT, std::pair<void *, UINT> >', '_MappedData', None),
            ]

        return variables

    def implementWrapperInterfaceMethodBody(self, interface, base, method):
        if method.name in ('Unlock', 'UnlockRect', 'UnlockBox'):
            if interface.base.name == 'IDirect3DBaseTexture9':
                assert method.getArgByName('Level') is not None
                print '    std::map<UINT, std::pair<size_t, VOID *> >::iterator it = _MappedData.find(Level);'
                print '    if (it != _MappedData.end()) {'
                self.emit_memcpy('(LPBYTE)it->second.second', 'it->second.first')
                print '        _MappedData.erase(it);'
                print '    }'
            else:
                assert method.getArgByName('Level') is None
                print '    if (_MappedSize && m_pbData) {'
                self.emit_memcpy('(LPBYTE)m_pbData', '_MappedSize')
                print '    }'

        if interface.name == 'IDirectXVideoDecoder' and method.name == 'ReleaseBuffer':
            print '    std::map<UINT, std::pair<void *, UINT> >::iterator it = _MappedData.find(BufferType);'
            print '    if (it != _MappedData.end()) {'
            self.emit_memcpy('it->second.first', 'it->second.second')
            print '        _MappedData.erase(it);'
            print '    }'

        DllTracer.implementWrapperInterfaceMethodBody(self, interface, base, method)

        if method.name in ('Lock', 'LockRect', 'LockBox'):
            if interface.base.name == 'IDirect3DBaseTexture9':
                assert method.getArgByName('Level') is not None
                print '    if (SUCCEEDED(_result) && !(Flags & D3DLOCK_READONLY)) {'
                print '        size_t mappedSize;'
                print '        VOID * pbData;'
                print '        _getMapInfo(_this, %s, pbData, mappedSize);' % ', '.join(method.argNames()[:-1])
                print '        _MappedData[Level] = std::make_pair(mappedSize, pbData);'
                print '    } else {'
                print '        _MappedData.erase(Level);'
                print '    }'
            else:
                # FIXME: handle recursive locks
                assert method.getArgByName('Level') is None
                if method.name == 'Lock':
                    # Ignore D3DLOCK_READONLY for buffers.
                    # https://github.com/apitrace/apitrace/issues/435
                    print '    if (SUCCEEDED(_result)) {'
                else:
                    print '    if (SUCCEEDED(_result) && !(Flags & D3DLOCK_READONLY)) {'
                print '        _getMapInfo(_this, %s, m_pbData, _MappedSize);' % ', '.join(method.argNames()[:-1])
                print '    } else {'
                print '        m_pbData = NULL;'
                print '        _MappedSize = 0;'
                print '    }'

        if interface.name == 'IDirectXVideoDecoder' and method.name == 'GetBuffer':
            print '    if (SUCCEEDED(_result)) {'
            print '        _MappedData[BufferType] = std::make_pair(*ppBuffer, *pBufferSize);'
            print '    } else {'
            print '        _MappedData[BufferType] = std::make_pair(nullptr, 0);'
            print '    }'


if __name__ == '__main__':
    print '#define INITGUID'
    print
    print '#include "trace_writer_local.hpp"'
    print '#include "os.hpp"'
    print
    print '#include "d3d9imports.hpp"'
    print '#include "d3d9size.hpp"'
    print '#include "d3d9shader.hpp"'
    print '#include "dxva2imports.hpp"'
    print

    d3d9.mergeModule(d3dperf)

    api = API()
    api.addModule(d3d9)
    api.addModule(dxva2)
    tracer = D3D9Tracer()
    tracer.traceApi(api)