File: iunknown_wrapper.cpp

package info (click to toggle)
gfxreconstruct 0.9.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,636 kB
  • sloc: cpp: 328,961; ansic: 25,454; python: 18,156; xml: 255; sh: 128; makefile: 6
file content (176 lines) | stat: -rw-r--r-- 5,876 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
** Copyright (c) 2021 LunarG, Inc.
**
** 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.
*/

#include "encode/iunknown_wrapper.h"

#include "encode/d3d12_capture_manager.h"
#include "encode/dx12_object_wrapper_resources.h"
#include "generated/generated_dx12_api_call_encoders.h"
#include "generated/generated_dx12_wrapper_creators.h"

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(encode)

IUnknown_Wrapper::IUnknown_Wrapper(REFIID                                        riid,
                                   IUnknown*                                     wrapped_object,
                                   DxWrapperResources*                           resources,
                                   const std::function<void(IUnknown_Wrapper*)>& destructor) :
    riid_(riid),
    object_(wrapped_object, false), capture_id_(D3D12CaptureManager::GetUniqueId()), ref_count_(1)
{
    assert(wrapped_object != nullptr);

    // Register this wrapper with the list of related resources, which represents a list of pointers to the same object
    // with different IIDs, all of which share a reference count and will stay active until all of the resources in the
    // list are released by the application.
    if (resources != nullptr)
    {
        resources_ = resources;
        resources_->AddWrapper(this, destructor);
        resources_->IncrementSharedCount();
    }
    else
    {
        resources_ = new DxWrapperResources();
        resources_->AddWrapper(this, destructor);
    }
}

HRESULT IUnknown_Wrapper::QueryInterface(REFIID riid, void** object)
{
    // Check for a query from the capture framework to determine if an object with an IUknown* type is really a
    // wrapped object.  When the IID is a match, return a success code and a pointer to the current object
    // without incrementing the reference count.
    if (IsEqualIID(riid, IID_IUnknown_Wrapper))
    {
        (*object) = this;
        return S_OK;
    }

    HRESULT result     = E_FAIL;
    auto    manager    = D3D12CaptureManager::Get();
    auto    call_scope = manager->IncrementCallScope();

    if (call_scope == 1)
    {
        auto state_lock = manager->AcquireSharedStateLock();

        IID target_interface_id = riid;

        // If GFXRECON_CAPTURE_IUNKNOWN_WRAPPING is enabled:
        // Detect when applications call QueryInterface(IID_IUnknown)
        // Switch IID_IUnknown to original IID, so that GFXR returns original interface wrapper
        if (manager->GetIUnknownWrappingSetting() && IsEqualGUID(riid, IID_IUnknown))
        {
            GFXRECON_LOG_WARNING("Detect call QueryInterface(IID_IUnknown)");
            target_interface_id = riid_;
        }

        result = object_->QueryInterface(target_interface_id, object);

        if (SUCCEEDED(result))
        {
            WrapObject(target_interface_id, object, resources_);
        }

        Encode_IUnknown_QueryInterface(this, result, target_interface_id, object);
    }
    else
    {
        result = object_->QueryInterface(riid, object);
    }

    manager->DecrementCallScope();

    return result;
}

ULONG IUnknown_Wrapper::AddRef()
{
    unsigned long local_count = ++ref_count_;
    resources_->IncrementSharedCount();

    auto manager    = D3D12CaptureManager::Get();
    auto call_scope = manager->IncrementCallScope();

    if (call_scope == 1)
    {
        auto state_lock = manager->AcquireSharedStateLock();

        Encode_IUnknown_AddRef(this, local_count);
    }

    manager->DecrementCallScope();

    return local_count;
}

ULONG IUnknown_Wrapper::Release()
{
    auto local_count  = --ref_count_;
    auto shared_count = resources_->DecrementSharedCount();

    auto manager    = D3D12CaptureManager::Get();
    auto call_scope = manager->IncrementCallScope();

    if (call_scope == 1)
    {
        auto state_lock = manager->AcquireSharedStateLock();

        Encode_IUnknown_Release(this, local_count);
    }

    if (shared_count == 0)
    {
        // The resources_ destructor destroys this wrapper and all other wrappers linked to it, so no additional work
        // may be performed in this function, as the current wrapper will no longer be valid.
        delete resources_;
    }

    manager->DecrementCallScope();

    return local_count;
}

void IUnknown_Wrapper::MakeRefInternal()
{
    // Decrement the local reference count, while leaving the shared reference count unmodified.
    --ref_count_;
}

void IUnknown_Wrapper::AddRefInternal()
{
    resources_->IncrementSharedCount();
}

void IUnknown_Wrapper::ReleaseRefInternal()
{
    auto shared_count = resources_->DecrementSharedCount();
    if (shared_count == 0)
    {
        // The resources_ destructor destroys this wrapper and all other wrappers linked to it.
        delete resources_;
    }
}

GFXRECON_END_NAMESPACE(encode)
GFXRECON_END_NAMESPACE(gfxrecon)