core.c 4.12 KB
Newer Older
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
/*
 *
 * Copyright 2002 Raphael Junqueira
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

#define COBJMACROS
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "wine/debug.h"
#include "wine/unicode.h"

#include "d3dx9_36_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3dx);

33
struct ID3DXBufferImpl
34
{
35 36 37
    ID3DXBuffer ID3DXBuffer_iface;
    LONG ref;

38 39
    void *buffer;
    DWORD size;
40
};
41

42
static inline struct ID3DXBufferImpl *impl_from_ID3DXBuffer(ID3DXBuffer *iface)
43
{
44
    return CONTAINING_RECORD(iface, struct ID3DXBufferImpl, ID3DXBuffer_iface);
45 46 47 48
}

static HRESULT WINAPI ID3DXBufferImpl_QueryInterface(ID3DXBuffer *iface, REFIID riid, void **ppobj)
{
49
    TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), ppobj);
50 51 52 53 54

    if (IsEqualGUID(riid, &IID_IUnknown)
      || IsEqualGUID(riid, &IID_ID3DXBuffer))
    {
        IUnknown_AddRef(iface);
55
        *ppobj = iface;
56 57 58
        return D3D_OK;
    }

59 60
    WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));

61 62 63
    return E_NOINTERFACE;
}

64
static ULONG WINAPI ID3DXBufferImpl_AddRef(ID3DXBuffer *iface)
65
{
66
    struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
67 68
    ULONG ref = InterlockedIncrement(&This->ref);

69
    TRACE("%p increasing refcount to %u\n", This, ref);
70 71 72 73

    return ref;
}

74
static ULONG WINAPI ID3DXBufferImpl_Release(ID3DXBuffer *iface)
75
{
76
    struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
77 78
    ULONG ref = InterlockedDecrement(&This->ref);

79
    TRACE("%p decreasing refcount to %u\n", This, ref);
80 81 82 83 84 85

    if (ref == 0)
    {
        HeapFree(GetProcessHeap(), 0, This->buffer);
        HeapFree(GetProcessHeap(), 0, This);
    }
86

87 88 89
    return ref;
}

90
static LPVOID WINAPI ID3DXBufferImpl_GetBufferPointer(ID3DXBuffer *iface)
91
{
92
    struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
93 94 95

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

96 97 98
    return This->buffer;
}

99
static DWORD WINAPI ID3DXBufferImpl_GetBufferSize(ID3DXBuffer *iface)
100
{
101
    struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
102 103 104

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

105
    return This->size;
106 107
}

108
static const struct ID3DXBufferVtbl ID3DXBufferImpl_Vtbl =
109
{
110
    /* IUnknown methods */
111 112 113
    ID3DXBufferImpl_QueryInterface,
    ID3DXBufferImpl_AddRef,
    ID3DXBufferImpl_Release,
114
    /* ID3DXBuffer methods */
115 116 117 118
    ID3DXBufferImpl_GetBufferPointer,
    ID3DXBufferImpl_GetBufferSize
};

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
static HRESULT d3dx9_buffer_init(struct ID3DXBufferImpl *buffer, DWORD size)
{
    buffer->ID3DXBuffer_iface.lpVtbl = &ID3DXBufferImpl_Vtbl;
    buffer->ref = 1;
    buffer->size = size;

    buffer->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
    if (!buffer->buffer)
    {
        ERR("Failed to allocate buffer memory\n");
        return E_OUTOFMEMORY;
    }

    return D3D_OK;
}

135
HRESULT WINAPI D3DXCreateBuffer(DWORD size, LPD3DXBUFFER *buffer)
136
{
137
    struct ID3DXBufferImpl *object;
138
    HRESULT hr;
139

140 141 142 143 144 145
    if (!buffer)
    {
        WARN("Invalid buffer specified.\n");
        return D3DERR_INVALIDCALL;
    }

146
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
147
    if (!object)
148
    {
149
        ERR("Failed to allocate buffer memory\n");
150 151
        return E_OUTOFMEMORY;
    }
152 153 154

    hr = d3dx9_buffer_init(object, size);
    if (FAILED(hr))
155
    {
156
        WARN("Failed to initialize buffer, hr %#x.\n", hr);
157
        HeapFree(GetProcessHeap(), 0, object);
158
        return hr;
159 160
    }

161
    *buffer = &object->ID3DXBuffer_iface;
162 163 164

    TRACE("Created ID3DBuffer %p\n", *buffer);

165 166
    return D3D_OK;
}