resource.c 9.89 KB
Newer Older
1 2 3 4
/*
 * Copyright 2002-2004 Jason Edmeades
 * Copyright 2003-2004 Raphael Junqueira
 * Copyright 2004 Christian Costa
5
 * Copyright 2005 Oliver Stieber
6
 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
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
 */

#include "config.h"
24
#include "wine/port.h"
25 26 27 28
#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d);

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
struct private_data
{
    struct list entry;

    GUID tag;
    DWORD flags; /* DDSPD_* */

    union
    {
        void *data;
        IUnknown *object;
    } ptr;

    DWORD size;
};

45
static DWORD resource_access_from_pool(enum wined3d_pool pool)
46 47 48
{
    switch (pool)
    {
49
        case WINED3D_POOL_DEFAULT:
50 51
            return WINED3D_RESOURCE_ACCESS_GPU;

52
        case WINED3D_POOL_MANAGED:
53 54
            return WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU;

55
        case WINED3D_POOL_SYSTEM_MEM:
56 57
            return WINED3D_RESOURCE_ACCESS_CPU;

58
        case WINED3D_POOL_SCRATCH:
59 60 61 62 63 64 65 66
            return WINED3D_RESOURCE_ACCESS_SCRATCH;

        default:
            FIXME("Unhandled pool %#x.\n", pool);
            return 0;
    }
}

67 68 69 70 71 72 73 74 75 76 77 78 79
static void resource_check_usage(DWORD usage)
{
    static const DWORD handled = WINED3DUSAGE_RENDERTARGET
            | WINED3DUSAGE_DEPTHSTENCIL
            | WINED3DUSAGE_DYNAMIC
            | WINED3DUSAGE_AUTOGENMIPMAP
            | WINED3DUSAGE_STATICDECL
            | WINED3DUSAGE_OVERLAY;

    if (usage & ~handled)
        FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
}

80
HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
81
        enum wined3d_resource_type type, const struct wined3d_format *format,
82
        enum wined3d_multisample_type multisample_type, UINT multisample_quality,
83
        DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
84
        void *parent, const struct wined3d_parent_ops *parent_ops,
85
        const struct wined3d_resource_ops *resource_ops)
86
{
87
    resource->ref = 1;
88
    resource->device = device;
89
    resource->type = type;
90
    resource->format = format;
91 92
    resource->multisample_type = multisample_type;
    resource->multisample_quality = multisample_quality;
93
    resource->usage = usage;
94
    resource->pool = pool;
95 96 97
    resource->access_flags = resource_access_from_pool(pool);
    if (usage & WINED3DUSAGE_DYNAMIC)
        resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
98 99 100
    resource->width = width;
    resource->height = height;
    resource->depth = depth;
101 102 103 104 105 106
    resource->size = size;
    resource->priority = 0;
    resource->parent = parent;
    resource->parent_ops = parent_ops;
    resource->resource_ops = resource_ops;
    list_init(&resource->privateData);
107

108 109
    resource_check_usage(usage);

110 111
    if (size)
    {
112 113
        resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
        if (!resource->heapMemory)
114 115 116 117 118 119 120
        {
            ERR("Out of memory!\n");
            return WINED3DERR_OUTOFVIDEOMEMORY;
        }
    }
    else
    {
121
        resource->heapMemory = NULL;
122
    }
123 124
    resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory
            + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
125 126

    /* Check that we have enough video ram left */
127
    if (pool == WINED3D_POOL_DEFAULT)
128
    {
129
        if (size > wined3d_device_get_available_texture_mem(device))
130 131
        {
            ERR("Out of adapter memory\n");
132
            HeapFree(GetProcessHeap(), 0, resource->heapMemory);
133 134
            return WINED3DERR_OUTOFVIDEOMEMORY;
        }
135
        adapter_adjust_memory(device->adapter, size);
136 137
    }

138
    device_resource_add(device, resource);
139

140 141 142
    return WINED3D_OK;
}

143
void resource_cleanup(struct wined3d_resource *resource)
144
{
145
    struct private_data *data;
146 147 148
    struct list *e1, *e2;
    HRESULT hr;

149 150
    TRACE("Cleaning up resource %p.\n", resource);

151
    if (resource->pool == WINED3D_POOL_DEFAULT)
152
    {
153
        TRACE("Decrementing device memory pool by %u.\n", resource->size);
154
        adapter_adjust_memory(resource->device->adapter, 0 - resource->size);
155
    }
156

157
    LIST_FOR_EACH_SAFE(e1, e2, &resource->privateData)
158 159
    {
        data = LIST_ENTRY(e1, struct private_data, entry);
160
        hr = wined3d_resource_free_private_data(resource, &data->tag);
161 162
        if (FAILED(hr))
            ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
163 164
    }

165 166 167
    HeapFree(GetProcessHeap(), 0, resource->heapMemory);
    resource->allocatedMemory = 0;
    resource->heapMemory = 0;
168

169 170
    if (resource->device)
        device_resource_released(resource->device, resource);
171 172
}

173
void resource_unload(struct wined3d_resource *resource)
174
{
175 176 177
    if (resource->map_count)
        ERR("Resource %p is being unloaded while mapped.\n", resource);

178
    context_resource_unloaded(resource->device,
179
            resource, resource->type);
180 181
}

182
static struct private_data *resource_find_private_data(const struct wined3d_resource *resource, REFGUID tag)
183
{
184
    struct private_data *data;
185 186 187
    struct list *entry;

    TRACE("Searching for private data %s\n", debugstr_guid(tag));
188
    LIST_FOR_EACH(entry, &resource->privateData)
189
    {
190
        data = LIST_ENTRY(entry, struct private_data, entry);
191 192 193 194
        if (IsEqualGUID(&data->tag, tag)) {
            TRACE("Found %p\n", data);
            return data;
        }
195
    }
196 197
    TRACE("Not found\n");
    return NULL;
198 199
}

200
HRESULT CDECL wined3d_resource_set_private_data(struct wined3d_resource *resource, REFGUID guid,
201
        const void *data, DWORD data_size, DWORD flags)
202
{
203
    struct private_data *d;
204

205 206
    TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
            resource, debugstr_guid(guid), data, data_size, flags);
207

208
    wined3d_resource_free_private_data(resource, guid);
209

210 211
    d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
    if (!d) return E_OUTOFMEMORY;
212

213 214
    d->tag = *guid;
    d->flags = flags;
215

216 217
    if (flags & WINED3DSPD_IUNKNOWN)
    {
218
        if (data_size != sizeof(IUnknown *))
219
        {
220 221
            WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
            HeapFree(GetProcessHeap(), 0, d);
222
            return WINED3DERR_INVALIDCALL;
223
        }
224 225 226
        d->ptr.object = (IUnknown *)data;
        d->size = sizeof(IUnknown *);
        IUnknown_AddRef(d->ptr.object);
227 228 229
    }
    else
    {
230 231
        d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
        if (!d->ptr.data)
232
        {
233
            HeapFree(GetProcessHeap(), 0, d);
234
            return E_OUTOFMEMORY;
235
        }
236 237
        d->size = data_size;
        memcpy(d->ptr.data, data, data_size);
238
    }
239
    list_add_tail(&resource->privateData, &d->entry);
240

241
    return WINED3D_OK;
242
}
243

244 245
HRESULT CDECL wined3d_resource_get_private_data(const struct wined3d_resource *resource, REFGUID guid,
        void *data, DWORD *data_size)
246
{
247
    const struct private_data *d;
248

249 250 251 252 253
    TRACE("resource %p, guid %s, data %p, data_size %p.\n",
            resource, debugstr_guid(guid), data, data_size);

    d = resource_find_private_data(resource, guid);
    if (!d) return WINED3DERR_NOTFOUND;
254

255 256 257
    if (*data_size < d->size)
    {
        *data_size = d->size;
258
        return WINED3DERR_MOREDATA;
259 260
    }

261 262 263
    if (d->flags & WINED3DSPD_IUNKNOWN)
    {
        *(IUnknown **)data = d->ptr.object;
264
        if (resource->device->wined3d->dxVersion != 7)
265
        {
266 267 268 269
            /* D3D8 and D3D9 addref the private data, DDraw does not. This
             * can't be handled in ddraw because it doesn't know if the
             * pointer returned is an IUnknown * or just a blob. */
            IUnknown_AddRef(d->ptr.object);
270
        }
271
    }
272 273 274
    else
    {
        memcpy(data, d->ptr.data, d->size);
275 276
    }

277
    return WINED3D_OK;
278
}
279
HRESULT CDECL wined3d_resource_free_private_data(struct wined3d_resource *resource, REFGUID guid)
280
{
281
    struct private_data *data;
282

283 284 285
    TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));

    data = resource_find_private_data(resource, guid);
286
    if (!data) return WINED3DERR_NOTFOUND;
287

288
    if (data->flags & WINED3DSPD_IUNKNOWN)
289
    {
290
        if (data->ptr.object)
291
            IUnknown_Release(data->ptr.object);
292 293 294
    }
    else
    {
295
        HeapFree(GetProcessHeap(), 0, data->ptr.data);
296
    }
297
    list_remove(&data->entry);
298

299
    HeapFree(GetProcessHeap(), 0, data);
300

301
    return WINED3D_OK;
302 303
}

304
DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
305
{
306 307
    DWORD prev = resource->priority;
    resource->priority = priority;
308 309
    TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
    return prev;
310
}
311

312
DWORD resource_get_priority(const struct wined3d_resource *resource)
313
{
314 315
    TRACE("resource %p, returning %u.\n", resource, resource->priority);
    return resource->priority;
316 317
}

318 319 320
void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
{
    return resource->parent;
321
}
322

323
void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
324
{
325
    desc->resource_type = resource->type;
326 327 328 329 330 331 332 333 334 335
    desc->format = resource->format->id;
    desc->multisample_type = resource->multisample_type;
    desc->multisample_quality = resource->multisample_quality;
    desc->usage = resource->usage;
    desc->pool = resource->pool;
    desc->width = resource->width;
    desc->height = resource->height;
    desc->depth = resource->depth;
    desc->size = resource->size;
}