query.c 6.42 KB
Newer Older
1 2 3 4
/*
 * IDirect3DQuery9 implementation
 *
 * Copyright 2002-2003 Raphael Junqueira
5 6
 * Copyright 2002-2003 Jason Edmeades
 * Copyright 2005 Oliver Stieber
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23 24
 */

#include "d3d9_private.h"

25
WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26

27 28 29 30 31 32 33 34 35 36
static D3DQUERYTYPE d3dquerytype_from_wined3d(enum wined3d_query_type type)
{
    return (D3DQUERYTYPE)type;
}

static enum wined3d_query_type wined3d_query_type_from_d3d(D3DQUERYTYPE type)
{
    return (enum wined3d_query_type)type;
}

37
static inline struct d3d9_query *impl_from_IDirect3DQuery9(IDirect3DQuery9 *iface)
38
{
39
    return CONTAINING_RECORD(iface, struct d3d9_query, IDirect3DQuery9_iface);
40 41
}

42
static HRESULT WINAPI d3d9_query_QueryInterface(IDirect3DQuery9 *iface, REFIID riid, void **out)
43
{
Andrey Gusev's avatar
Andrey Gusev committed
44
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
45

46 47 48
    if (IsEqualGUID(riid, &IID_IDirect3DQuery9)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
49
        IDirect3DQuery9_AddRef(iface);
50
        *out = iface;
H. Verbeet's avatar
H. Verbeet committed
51
        return S_OK;
52 53
    }

54 55
    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

56
    *out = NULL;
57 58 59
    return E_NOINTERFACE;
}

60
static ULONG WINAPI d3d9_query_AddRef(IDirect3DQuery9 *iface)
61
{
62 63
    struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
    ULONG refcount = InterlockedIncrement(&query->refcount);
64

65
    TRACE("%p increasing refcount to %u.\n", iface, refcount);
Henri Verbeet's avatar
Henri Verbeet committed
66

67
    return refcount;
68 69
}

70
static ULONG WINAPI d3d9_query_Release(IDirect3DQuery9 *iface)
71
{
72 73
    struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
    ULONG refcount = InterlockedDecrement(&query->refcount);
74

75
    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
76

77 78
    if (!refcount)
    {
79
        wined3d_mutex_lock();
80
        wined3d_query_decref(query->wined3d_query);
81 82
        wined3d_mutex_unlock();

83
        IDirect3DDevice9Ex_Release(query->parent_device);
84
        heap_free(query);
85
    }
86
    return refcount;
87 88
}

89
static HRESULT WINAPI d3d9_query_GetDevice(IDirect3DQuery9 *iface, IDirect3DDevice9 **device)
90
{
91
    struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
92

93
    TRACE("iface %p, device %p.\n", iface, device);
94

95
    *device = (IDirect3DDevice9 *)query->parent_device;
96 97 98
    IDirect3DDevice9_AddRef(*device);

    TRACE("Returning device %p.\n", *device);
99

100
    return D3D_OK;
101 102
}

103
static D3DQUERYTYPE WINAPI d3d9_query_GetType(IDirect3DQuery9 *iface)
104
{
105
    struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
106
    D3DQUERYTYPE type;
Henri Verbeet's avatar
Henri Verbeet committed
107 108

    TRACE("iface %p.\n", iface);
109

110
    wined3d_mutex_lock();
111
    type = d3dquerytype_from_wined3d(wined3d_query_get_type(query->wined3d_query));
112 113
    wined3d_mutex_unlock();

114
    return type;
115 116
}

117
static DWORD WINAPI d3d9_query_GetDataSize(IDirect3DQuery9 *iface)
118
{
119
    struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
120 121

    TRACE("iface %p.\n", iface);
122

123
    return query->data_size;
124 125
}

126
static HRESULT WINAPI d3d9_query_Issue(IDirect3DQuery9 *iface, DWORD flags)
127
{
128
    struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
129
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
130

131
    TRACE("iface %p, flags %#x.\n", iface, flags);
132

133
    wined3d_mutex_lock();
134
    hr = wined3d_query_issue(query->wined3d_query, flags);
135 136
    wined3d_mutex_unlock();

137
    return hr;
138 139
}

140
static HRESULT WINAPI d3d9_query_GetData(IDirect3DQuery9 *iface, void *data, DWORD size, DWORD flags)
141
{
142
    struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
143
    enum wined3d_query_type type;
144
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
145 146

    TRACE("iface %p, data %p, size %u, flags %#x.\n",
147
            iface, data, size, flags);
148

149
    wined3d_mutex_lock();
150 151 152 153 154
    type = wined3d_query_get_type(query->wined3d_query);
    if (type == WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT && data)
    {
        struct wined3d_query_data_timestamp_disjoint data_disjoint;

155 156 157
        if (size > sizeof(data_disjoint.disjoint))
            size = sizeof(data_disjoint.disjoint);

158
        hr = wined3d_query_get_data(query->wined3d_query, &data_disjoint, sizeof(data_disjoint), flags);
159 160
        if (SUCCEEDED(hr))
            memcpy(data, &data_disjoint.disjoint, size);
161 162 163 164 165
    }
    else
    {
        hr = wined3d_query_get_data(query->wined3d_query, data, size, flags);
    }
166 167
    wined3d_mutex_unlock();

168
    if (hr == D3DERR_INVALIDCALL)
169 170 171 172
    {
        if (data)
        {
            memset(data, 0, size);
173
            memset(data, 0xdd, min(size, query->data_size));
174
        }
175
        return S_OK;
176
    }
177
    return hr;
178 179 180
}


181
static const struct IDirect3DQuery9Vtbl d3d9_query_vtbl =
182
{
183 184 185 186 187 188 189 190
    d3d9_query_QueryInterface,
    d3d9_query_AddRef,
    d3d9_query_Release,
    d3d9_query_GetDevice,
    d3d9_query_GetType,
    d3d9_query_GetDataSize,
    d3d9_query_Issue,
    d3d9_query_GetData,
191 192
};

193
HRESULT query_init(struct d3d9_query *query, struct d3d9_device *device, D3DQUERYTYPE type)
194 195
{
    HRESULT hr;
196

197 198
    if (type > D3DQUERYTYPE_MEMORYPRESSURE)
    {
199 200 201 202 203
        if (type == 0x16)
            FIXME("Undocumented query %#x created.\n", type);
        else
            WARN("Invalid query type %#x.\n", type);

204 205 206
        return D3DERR_NOTAVAILABLE;
    }

207 208
    query->IDirect3DQuery9_iface.lpVtbl = &d3d9_query_vtbl;
    query->refcount = 1;
209

210
    wined3d_mutex_lock();
211
    if (FAILED(hr = wined3d_query_create(device->wined3d_device, wined3d_query_type_from_d3d(type),
212
            query, &d3d9_null_wined3d_parent_ops, &query->wined3d_query)))
213
    {
214
        wined3d_mutex_unlock();
215
        WARN("Failed to create wined3d query, hr %#x.\n", hr);
216 217
        return hr;
    }
218

219 220 221 222 223 224 225 226
    if (type == D3DQUERYTYPE_OCCLUSION)
        query->data_size = sizeof(DWORD);
    else if (type == D3DQUERYTYPE_TIMESTAMPDISJOINT)
        query->data_size = sizeof(BOOL);
    else
        query->data_size = wined3d_query_get_data_size(query->wined3d_query);
    wined3d_mutex_unlock();

227 228
    query->parent_device = &device->IDirect3DDevice9Ex_iface;
    IDirect3DDevice9Ex_AddRef(query->parent_device);
229

230
    return D3D_OK;
231
}