resource.c 8.83 KB
Newer Older
1 2 3 4 5 6
/*
 * IWineD3DResource Implementation
 *
 * Copyright 2002-2004 Jason Edmeades
 * Copyright 2003-2004 Raphael Junqueira
 * Copyright 2004 Christian Costa
7
 * Copyright 2005 Oliver Stieber
8
 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24 25 26 27 28 29
 */

#include "config.h"
#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d);

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

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

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

    DWORD size;
};

46 47 48 49 50
HRESULT resource_init(struct wined3d_resource *resource, IWineD3DDeviceImpl *device,
        WINED3DRESOURCETYPE resource_type, const struct wined3d_format *format,
        WINED3DMULTISAMPLE_TYPE multisample_type, UINT multisample_quality,
        DWORD usage, WINED3DPOOL pool, UINT width, UINT height, UINT depth, UINT size,
        void *parent, const struct wined3d_parent_ops *parent_ops,
51
        const struct wined3d_resource_ops *resource_ops)
52
{
53
    resource->ref = 1;
54 55 56
    resource->device = device;
    resource->resourceType = resource_type;
    resource->format = format;
57 58
    resource->multisample_type = multisample_type;
    resource->multisample_quality = multisample_quality;
59
    resource->usage = usage;
60 61 62 63
    resource->pool = pool;
    resource->width = width;
    resource->height = height;
    resource->depth = depth;
64 65 66 67 68 69
    resource->size = size;
    resource->priority = 0;
    resource->parent = parent;
    resource->parent_ops = parent_ops;
    resource->resource_ops = resource_ops;
    list_init(&resource->privateData);
70 71 72

    if (size)
    {
73 74
        resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
        if (!resource->heapMemory)
75 76 77 78 79 80 81
        {
            ERR("Out of memory!\n");
            return WINED3DERR_OUTOFVIDEOMEMORY;
        }
    }
    else
    {
82
        resource->heapMemory = NULL;
83
    }
84 85
    resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory
            + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
86 87 88 89 90 91 92

    /* Check that we have enough video ram left */
    if (pool == WINED3DPOOL_DEFAULT)
    {
        if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
        {
            ERR("Out of adapter memory\n");
93
            HeapFree(GetProcessHeap(), 0, resource->heapMemory);
94 95 96 97 98
            return WINED3DERR_OUTOFVIDEOMEMORY;
        }
        WineD3DAdapterChangeGLRam(device, size);
    }

99
    device_resource_add(device, resource);
100

101 102 103
    return WINED3D_OK;
}

104
void resource_cleanup(struct wined3d_resource *resource)
105
{
106
    struct private_data *data;
107 108 109
    struct list *e1, *e2;
    HRESULT hr;

110 111
    TRACE("Cleaning up resource %p.\n", resource);

112
    if (resource->pool == WINED3DPOOL_DEFAULT)
113
    {
114 115
        TRACE("Decrementing device memory pool by %u.\n", resource->size);
        WineD3DAdapterChangeGLRam(resource->device, -resource->size);
116
    }
117

118
    LIST_FOR_EACH_SAFE(e1, e2, &resource->privateData)
119 120
    {
        data = LIST_ENTRY(e1, struct private_data, entry);
121
        hr = resource_free_private_data(resource, &data->tag);
122 123
        if (FAILED(hr))
            ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
124 125
    }

126 127 128
    HeapFree(GetProcessHeap(), 0, resource->heapMemory);
    resource->allocatedMemory = 0;
    resource->heapMemory = 0;
129

130 131
    if (resource->device)
        device_resource_released(resource->device, resource);
132 133
}

134
void resource_unload(struct wined3d_resource *resource)
135
{
136 137
    context_resource_unloaded(resource->device,
            resource, resource->resourceType);
138 139
}

140
static struct private_data *resource_find_private_data(const struct wined3d_resource *resource, REFGUID tag)
141
{
142
    struct private_data *data;
143 144 145
    struct list *entry;

    TRACE("Searching for private data %s\n", debugstr_guid(tag));
146
    LIST_FOR_EACH(entry, &resource->privateData)
147
    {
148
        data = LIST_ENTRY(entry, struct private_data, entry);
149 150 151 152
        if (IsEqualGUID(&data->tag, tag)) {
            TRACE("Found %p\n", data);
            return data;
        }
153
    }
154 155
    TRACE("Not found\n");
    return NULL;
156 157
}

158
HRESULT resource_set_private_data(struct wined3d_resource *resource, REFGUID guid,
159
        const void *data, DWORD data_size, DWORD flags)
160
{
161
    struct private_data *d;
162

163 164
    TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
            resource, debugstr_guid(guid), data, data_size, flags);
165

166
    resource_free_private_data(resource, guid);
167

168 169
    d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
    if (!d) return E_OUTOFMEMORY;
170

171 172
    d->tag = *guid;
    d->flags = flags;
173

174 175
    if (flags & WINED3DSPD_IUNKNOWN)
    {
176
        if (data_size != sizeof(IUnknown *))
177
        {
178 179
            WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
            HeapFree(GetProcessHeap(), 0, d);
180
            return WINED3DERR_INVALIDCALL;
181
        }
182 183 184
        d->ptr.object = (IUnknown *)data;
        d->size = sizeof(IUnknown *);
        IUnknown_AddRef(d->ptr.object);
185 186 187
    }
    else
    {
188 189
        d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
        if (!d->ptr.data)
190
        {
191
            HeapFree(GetProcessHeap(), 0, d);
192
            return E_OUTOFMEMORY;
193
        }
194 195
        d->size = data_size;
        memcpy(d->ptr.data, data, data_size);
196
    }
197
    list_add_tail(&resource->privateData, &d->entry);
198

199
    return WINED3D_OK;
200
}
201

202
HRESULT resource_get_private_data(const struct wined3d_resource *resource, REFGUID guid, void *data, DWORD *data_size)
203
{
204
    const struct private_data *d;
205

206 207 208 209 210
    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;
211

212 213 214
    if (*data_size < d->size)
    {
        *data_size = d->size;
215
        return WINED3DERR_MOREDATA;
216 217
    }

218 219 220
    if (d->flags & WINED3DSPD_IUNKNOWN)
    {
        *(IUnknown **)data = d->ptr.object;
221
        if (resource->device->wined3d->dxVersion != 7)
222
        {
223 224 225 226
            /* 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);
227
        }
228
    }
229 230 231
    else
    {
        memcpy(data, d->ptr.data, d->size);
232 233
    }

234
    return WINED3D_OK;
235
}
236
HRESULT resource_free_private_data(struct wined3d_resource *resource, REFGUID guid)
237
{
238
    struct private_data *data;
239

240 241 242
    TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));

    data = resource_find_private_data(resource, guid);
243
    if (!data) return WINED3DERR_NOTFOUND;
244

245
    if (data->flags & WINED3DSPD_IUNKNOWN)
246
    {
247
        if (data->ptr.object)
248
            IUnknown_Release(data->ptr.object);
249 250 251
    }
    else
    {
252
        HeapFree(GetProcessHeap(), 0, data->ptr.data);
253
    }
254
    list_remove(&data->entry);
255

256
    HeapFree(GetProcessHeap(), 0, data);
257

258
    return WINED3D_OK;
259 260
}

261
DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
262
{
263 264
    DWORD prev = resource->priority;
    resource->priority = priority;
265 266
    TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
    return prev;
267
}
268

269
DWORD resource_get_priority(const struct wined3d_resource *resource)
270
{
271 272
    TRACE("resource %p, returning %u.\n", resource, resource->priority);
    return resource->priority;
273 274
}

275
WINED3DRESOURCETYPE resource_get_type(const struct wined3d_resource *resource)
276
{
277 278 279 280 281 282 283
    TRACE("resource %p, returning %#x.\n", resource, resource->resourceType);
    return resource->resourceType;
}

void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
{
    return resource->parent;
284
}
285

286
void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
287 288 289 290 291 292 293 294 295 296 297 298
{
    desc->resource_type = resource->resourceType;
    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;
}