surface.c 11.2 KB
Newer Older
1 2 3
/*
 * IDirect3DSurface8 implementation
 *
4
 * Copyright 2005 Oliver Stieber
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include "config.h"
22
#include <assert.h>
23 24
#include "d3d8_private.h"

25
WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26

27 28
static inline IDirect3DSurface8Impl *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
{
29
    return CONTAINING_RECORD(iface, IDirect3DSurface8Impl, IDirect3DSurface8_iface);
30 31
}

32
/* IDirect3DSurface8 IUnknown parts follow: */
33 34 35 36
static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(IDirect3DSurface8 *iface, REFIID riid,
        void **ppobj)
{
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
37

Henri Verbeet's avatar
Henri Verbeet committed
38 39
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);

40
    if (IsEqualGUID(riid, &IID_IUnknown)
41
        || IsEqualGUID(riid, &IID_IDirect3DResource8)
Raphael Junqueira's avatar
Raphael Junqueira committed
42
        || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
43
        IUnknown_AddRef(iface);
44
        *ppobj = This;
H. Verbeet's avatar
H. Verbeet committed
45
        return S_OK;
46 47
    }

48
    WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
H. Verbeet's avatar
H. Verbeet committed
49
    *ppobj = NULL;
50 51 52
    return E_NOINTERFACE;
}

53 54 55
static ULONG WINAPI IDirect3DSurface8Impl_AddRef(IDirect3DSurface8 *iface)
{
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
56

Henri Verbeet's avatar
Henri Verbeet committed
57
    TRACE("iface %p.\n", iface);
58

59 60 61 62
    if (This->forwardReference) {
        /* Forward refcounting */
        TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
        return IUnknown_AddRef(This->forwardReference);
63 64 65
    } else {
        /* No container, handle our own refcounting */
        ULONG ref = InterlockedIncrement(&This->ref);
Henri Verbeet's avatar
Henri Verbeet committed
66 67 68

        TRACE("%p increasing refcount to %u.\n", iface, ref);

69 70 71 72
        if (ref == 1)
        {
            if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
            wined3d_mutex_lock();
73
            wined3d_surface_incref(This->wined3d_surface);
74 75
            wined3d_mutex_unlock();
        }
Henri Verbeet's avatar
Henri Verbeet committed
76

77 78
        return ref;
    }
79 80
}

81 82 83
static ULONG WINAPI IDirect3DSurface8Impl_Release(IDirect3DSurface8 *iface)
{
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
84

Henri Verbeet's avatar
Henri Verbeet committed
85
    TRACE("iface %p.\n", iface);
86

87 88 89 90
    if (This->forwardReference) {
        /* Forward refcounting */
        TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
        return IUnknown_Release(This->forwardReference);
91 92 93
    } else {
        /* No container, handle our own refcounting */
        ULONG ref = InterlockedDecrement(&This->ref);
Henri Verbeet's avatar
Henri Verbeet committed
94 95

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

97
        if (ref == 0) {
98 99
            IDirect3DDevice8 *parentDevice = This->parentDevice;

100
            /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
101
            wined3d_mutex_lock();
102
            wined3d_surface_decref(This->wined3d_surface);
103
            wined3d_mutex_unlock();
104 105

            if (parentDevice) IDirect3DDevice8_Release(parentDevice);
106
        }
107

108
        return ref;
109 110 111
    }
}

112
/* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
113 114
static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(IDirect3DSurface8 *iface,
        IDirect3DDevice8 **device)
115
{
116
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
Henri Verbeet's avatar
Henri Verbeet committed
117

118
    TRACE("iface %p, device %p.\n", iface, device);
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    if (This->forwardReference)
    {
        IDirect3DResource8 *resource;
        HRESULT hr;

        hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
        if (SUCCEEDED(hr))
        {
            hr = IDirect3DResource8_GetDevice(resource, device);
            IDirect3DResource8_Release(resource);

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

        return hr;
    }

137 138 139 140
    *device = (IDirect3DDevice8 *)This->parentDevice;
    IDirect3DDevice8_AddRef(*device);

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

142
    return D3D_OK;
143
}
144

145 146 147 148 149
static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
        const void *data, DWORD data_size, DWORD flags)
{
    IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
    struct wined3d_resource *resource;
150
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
151 152

    TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
153
            iface, debugstr_guid(guid), data, data_size, flags);
154

155
    wined3d_mutex_lock();
156 157
    resource = wined3d_surface_get_resource(surface->wined3d_surface);
    hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
158 159
    wined3d_mutex_unlock();

160
    return hr;
161
}
162

163 164 165 166 167
static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
        void *data, DWORD *data_size)
{
    IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
    struct wined3d_resource *resource;
168
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
169 170

    TRACE("iface %p, guid %s, data %p, data_size %p.\n",
171
            iface, debugstr_guid(guid), data, data_size);
172

173
    wined3d_mutex_lock();
174 175
    resource = wined3d_surface_get_resource(surface->wined3d_surface);
    hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
176 177
    wined3d_mutex_unlock();

178
    return hr;
179
}
180

181 182 183 184
static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
{
    IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
    struct wined3d_resource *resource;
185
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
186

187
    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
188

189
    wined3d_mutex_lock();
190 191
    resource = wined3d_surface_get_resource(surface->wined3d_surface);
    hr = wined3d_resource_free_private_data(resource, guid);
192 193
    wined3d_mutex_unlock();

194
    return hr;
195
}
196

197
/* IDirect3DSurface8 Interface follow: */
198 199 200 201
static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(IDirect3DSurface8 *iface, REFIID riid,
        void **ppContainer)
{
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
202
    HRESULT res;
203

Henri Verbeet's avatar
Henri Verbeet committed
204
    TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
205

206 207 208
    if (!This->container) return E_NOINTERFACE;

    res = IUnknown_QueryInterface(This->container, riid, ppContainer);
209

210
    TRACE("(%p) : returning %p\n", This, *ppContainer);
211
    return res;
212
}
213

214 215
static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
{
216
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
217
    struct wined3d_resource_desc wined3d_desc;
218
    struct wined3d_resource *wined3d_resource;
Henri Verbeet's avatar
Henri Verbeet committed
219

220
    TRACE("iface %p, desc %p.\n", iface, desc);
221

222
    wined3d_mutex_lock();
223
    wined3d_resource = wined3d_surface_get_resource(This->wined3d_surface);
224
    wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
225
    wined3d_mutex_unlock();
226

227 228 229 230 231 232 233 234
    desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
    desc->Type = wined3d_desc.resource_type;
    desc->Usage = wined3d_desc.usage;
    desc->Pool = wined3d_desc.pool;
    desc->Size = wined3d_desc.size;
    desc->MultiSampleType = wined3d_desc.multisample_type;
    desc->Width = wined3d_desc.width;
    desc->Height = wined3d_desc.height;
235

236
    return D3D_OK;
237
}
238

239 240 241 242
static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(IDirect3DSurface8 *iface,
        D3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD Flags)
{
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
243
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
244 245

    TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
246

247
    wined3d_mutex_lock();
248 249 250 251 252 253 254 255 256 257 258
    if (pRect) {
        D3DSURFACE_DESC desc;
        IDirect3DSurface8_GetDesc(iface, &desc);

        if ((pRect->left < 0)
                || (pRect->top < 0)
                || (pRect->left >= pRect->right)
                || (pRect->top >= pRect->bottom)
                || (pRect->right > desc.Width)
                || (pRect->bottom > desc.Height)) {
            WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
259 260
            wined3d_mutex_unlock();

261 262 263 264
            return D3DERR_INVALIDCALL;
        }
    }

265
    hr = wined3d_surface_map(This->wined3d_surface, (WINED3DLOCKED_RECT *)pLockedRect, pRect, Flags);
266 267
    wined3d_mutex_unlock();

268
    return hr;
269
}
270

271 272 273
static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(IDirect3DSurface8 *iface)
{
    IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
274
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
275 276

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

278
    wined3d_mutex_lock();
279
    hr = wined3d_surface_unmap(This->wined3d_surface);
280 281
    wined3d_mutex_unlock();

282 283 284 285 286
    switch(hr)
    {
        case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
        default:                        return hr;
    }
287 288
}

289
static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
290
{
291
    /* IUnknown */
292 293 294
    IDirect3DSurface8Impl_QueryInterface,
    IDirect3DSurface8Impl_AddRef,
    IDirect3DSurface8Impl_Release,
295
    /* IDirect3DResource8 */
296 297 298 299
    IDirect3DSurface8Impl_GetDevice,
    IDirect3DSurface8Impl_SetPrivateData,
    IDirect3DSurface8Impl_GetPrivateData,
    IDirect3DSurface8Impl_FreePrivateData,
300
    /* IDirect3DSurface8 */
301 302 303
    IDirect3DSurface8Impl_GetContainer,
    IDirect3DSurface8Impl_GetDesc,
    IDirect3DSurface8Impl_LockRect,
304
    IDirect3DSurface8Impl_UnlockRect
305
};
306

307 308 309 310 311 312 313 314 315 316
static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
{
    HeapFree(GetProcessHeap(), 0, parent);
}

static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
{
    surface_wined3d_object_destroyed,
};

317 318 319 320 321 322
HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
        UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
        DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
{
    HRESULT hr;

323
    surface->IDirect3DSurface8_iface.lpVtbl = &Direct3DSurface8_Vtbl;
324 325 326 327 328 329 330 331 332 333
    surface->ref = 1;

    /* FIXME: Check MAX bounds of MultisampleQuality. */
    if (multisample_quality > 0)
    {
        FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
        multisample_quality = 0;
    }

    wined3d_mutex_lock();
334
    hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
335
            lockable, discard, level, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, multisample_type,
336
            multisample_quality, SURFACE_OPENGL, surface, &d3d8_surface_wined3d_parent_ops, &surface->wined3d_surface);
337 338 339 340 341 342 343
    wined3d_mutex_unlock();
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d surface, hr %#x.\n", hr);
        return hr;
    }

344
    surface->parentDevice = &device->IDirect3DDevice8_iface;
345 346 347 348
    IUnknown_AddRef(surface->parentDevice);

    return D3D_OK;
}
349 350 351 352 353 354 355 356 357

IDirect3DSurface8Impl *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &Direct3DSurface8_Vtbl);

    return impl_from_IDirect3DSurface8(iface);
}