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

#include "d3d9_private.h"

24
WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
25

26
static inline struct d3d9_surface *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
27
{
28
    return CONTAINING_RECORD(iface, struct d3d9_surface, IDirect3DSurface9_iface);
29 30
}

31
static HRESULT WINAPI d3d9_surface_QueryInterface(IDirect3DSurface9 *iface, REFIID riid, void **out)
32
{
33
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
Henri Verbeet's avatar
Henri Verbeet committed
34

35 36 37 38
    if (IsEqualGUID(riid, &IID_IDirect3DSurface9)
            || IsEqualGUID(riid, &IID_IDirect3DResource9)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
39
        IDirect3DSurface9_AddRef(iface);
40
        *out = iface;
H. Verbeet's avatar
H. Verbeet committed
41
        return S_OK;
42 43
    }

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

46
    *out = NULL;
47 48 49
    return E_NOINTERFACE;
}

50
static ULONG WINAPI d3d9_surface_AddRef(IDirect3DSurface9 *iface)
51
{
52 53
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
    ULONG refcount;
54

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

57
    if (surface->texture)
58
    {
59 60
        TRACE("Forwarding to %p.\n", surface->texture);
        return IDirect3DBaseTexture9_AddRef(&surface->texture->IDirect3DBaseTexture9_iface);
61
    }
Henri Verbeet's avatar
Henri Verbeet committed
62

63
    refcount = InterlockedIncrement(&surface->resource.refcount);
64
    TRACE("%p increasing refcount to %u.\n", iface, refcount);
65

66 67 68 69 70
    if (refcount == 1)
    {
        if (surface->parent_device)
            IDirect3DDevice9Ex_AddRef(surface->parent_device);
        wined3d_mutex_lock();
71 72
        if (surface->wined3d_rtv)
            wined3d_rendertarget_view_incref(surface->wined3d_rtv);
73
        wined3d_texture_incref(surface->wined3d_texture);
74
        wined3d_mutex_unlock();
75
    }
76

77
    return refcount;
78 79
}

80
static ULONG WINAPI d3d9_surface_Release(IDirect3DSurface9 *iface)
81
{
82 83
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
    ULONG refcount;
84

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

87
    if (surface->texture)
88
    {
89 90
        TRACE("Forwarding to %p.\n", surface->texture);
        return IDirect3DBaseTexture9_Release(&surface->texture->IDirect3DBaseTexture9_iface);
91
    }
92

93 94 95 96 97 98
    if (!surface->resource.refcount)
    {
        WARN("Surface does not have any references.\n");
        return 0;
    }

99
    refcount = InterlockedDecrement(&surface->resource.refcount);
100
    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
101

102 103 104
    if (!refcount)
    {
        IDirect3DDevice9Ex *parent_device = surface->parent_device;
105

106
        wined3d_mutex_lock();
107 108
        if (surface->wined3d_rtv)
            wined3d_rendertarget_view_decref(surface->wined3d_rtv);
109
        wined3d_texture_decref(surface->wined3d_texture);
110
        wined3d_mutex_unlock();
111

112 113 114
        /* Release the device last, as it may cause the device to be destroyed. */
        if (parent_device)
            IDirect3DDevice9Ex_Release(parent_device);
115
    }
116 117

    return refcount;
118 119
}

120
static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
121
{
122
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
123

124
    TRACE("iface %p, device %p.\n", iface, device);
125

126 127
    if (surface->texture)
        return IDirect3DBaseTexture9_GetDevice(&surface->texture->IDirect3DBaseTexture9_iface, device);
128

129
    *device = (IDirect3DDevice9 *)surface->parent_device;
130
    IDirect3DDevice9_AddRef(*device);
131

132 133 134
    TRACE("Returning device %p.\n", *device);

    return D3D_OK;
135 136
}

137
static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
138 139
        const void *data, DWORD data_size, DWORD flags)
{
140
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
141
    TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
142
            iface, debugstr_guid(guid), data, data_size, flags);
143

144
    return d3d9_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
145 146
}

147
static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
148 149
        void *data, DWORD *data_size)
{
150
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
151
    TRACE("iface %p, guid %s, data %p, data_size %p.\n",
152
            iface, debugstr_guid(guid), data, data_size);
153

154
    return d3d9_resource_get_private_data(&surface->resource, guid, data, data_size);
155 156
}

157
static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
158
{
159
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
160
    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
161

162
    return d3d9_resource_free_private_data(&surface->resource, guid);
163 164
}

165
static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
166
{
167 168
    TRACE("iface %p, priority %u. Ignored on surfaces.\n", iface, priority);
    return 0;
169 170
}

171
static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
172
{
173 174
    TRACE("iface %p. Ignored on surfaces.\n", iface);
    return 0;
175 176
}

177
static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
178
{
179
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
180 181

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

183
    wined3d_mutex_lock();
184
    wined3d_resource_preload(wined3d_texture_get_resource(surface->wined3d_texture));
185
    wined3d_mutex_unlock();
186 187
}

188
static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
189
{
Henri Verbeet's avatar
Henri Verbeet committed
190
    TRACE("iface %p.\n", iface);
191

192
    return D3DRTYPE_SURFACE;
193 194
}

195
static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
196
{
197 198
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
    HRESULT hr;
199

200
    TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
201

202 203
    if (!surface->container)
        return E_NOINTERFACE;
204

205
    hr = IUnknown_QueryInterface(surface->container, riid, container);
206

207
    TRACE("Returning %p.\n", *container);
208

209
    return hr;
210 211
}

212
static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
213
{
214
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
215
    struct wined3d_sub_resource_desc wined3d_desc;
Henri Verbeet's avatar
Henri Verbeet committed
216

217
    TRACE("iface %p, desc %p.\n", iface, desc);
218

219
    wined3d_mutex_lock();
220
    wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
221
    wined3d_mutex_unlock();
222

223
    desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
224
    desc->Type = D3DRTYPE_SURFACE;
225
    desc->Usage = d3dusage_from_wined3dusage(wined3d_desc.usage, wined3d_desc.bind_flags);
226
    desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
227 228 229 230
    desc->MultiSampleType = wined3d_desc.multisample_type;
    desc->MultiSampleQuality = wined3d_desc.multisample_quality;
    desc->Width = wined3d_desc.width;
    desc->Height = wined3d_desc.height;
231

232
    return D3D_OK;
233 234
}

235
static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
236
        D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
237
{
238
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
239
    struct wined3d_box box;
240
    struct wined3d_map_desc map_desc;
241
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
242

243 244
    TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
            iface, locked_rect, wine_dbgstr_rect(rect), flags);
245

246
    if (rect)
247
        wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
248

249
    wined3d_mutex_lock();
250
    hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
251
            &map_desc, rect ? &box : NULL, wined3dmapflags_from_d3dmapflags(flags, 0));
252 253
    wined3d_mutex_unlock();

254 255 256 257 258
    if (SUCCEEDED(hr))
    {
        locked_rect->Pitch = map_desc.row_pitch;
        locked_rect->pBits = map_desc.data;
    }
259

260 261
    if (hr == E_INVALIDARG)
        return D3DERR_INVALIDCALL;
262
    return hr;
263 264
}

265
static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
266
{
267
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
268
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
269 270

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

272
    wined3d_mutex_lock();
273
    hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
274 275
    if (SUCCEEDED(hr) && surface->texture)
        d3d9_texture_flag_auto_gen_mipmap(surface->texture);
276 277
    wined3d_mutex_unlock();

278
    if (hr == WINEDDERR_NOTLOCKED)
279
    {
280 281 282 283 284 285
        D3DRESOURCETYPE type;
        if (surface->texture)
            type = IDirect3DBaseTexture9_GetType(&surface->texture->IDirect3DBaseTexture9_iface);
        else
            type = D3DRTYPE_SURFACE;
        hr = type == D3DRTYPE_TEXTURE ? D3D_OK : D3DERR_INVALIDCALL;
286
    }
287
    return hr;
288 289
}

290
static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
291
{
292
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
293
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
294

295
    TRACE("iface %p, dc %p.\n", iface, dc);
296

297
    wined3d_mutex_lock();
298
    hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
299 300
    wined3d_mutex_unlock();

301
    return hr;
302 303
}

304
static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
305
{
306
    struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
307
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
308

309
    TRACE("iface %p, dc %p.\n", iface, dc);
310

311
    wined3d_mutex_lock();
312
    hr = wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
313 314
    if (SUCCEEDED(hr) && surface->texture)
        d3d9_texture_flag_auto_gen_mipmap(surface->texture);
315 316
    wined3d_mutex_unlock();

317
    return hr;
318 319
}

320
static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
321
{
322
    /* IUnknown */
323 324 325
    d3d9_surface_QueryInterface,
    d3d9_surface_AddRef,
    d3d9_surface_Release,
326
    /* IDirect3DResource9 */
327 328 329 330 331 332 333 334
    d3d9_surface_GetDevice,
    d3d9_surface_SetPrivateData,
    d3d9_surface_GetPrivateData,
    d3d9_surface_FreePrivateData,
    d3d9_surface_SetPriority,
    d3d9_surface_GetPriority,
    d3d9_surface_PreLoad,
    d3d9_surface_GetType,
335
    /* IDirect3DSurface9 */
336 337 338 339 340 341
    d3d9_surface_GetContainer,
    d3d9_surface_GetDesc,
    d3d9_surface_LockRect,
    d3d9_surface_UnlockRect,
    d3d9_surface_GetDC,
    d3d9_surface_ReleaseDC,
342
};
343

344 345
static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
{
346 347
    struct d3d9_surface *surface = parent;
    d3d9_resource_cleanup(&surface->resource);
348
    heap_free(surface);
349 350 351 352 353 354 355
}

static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
{
    surface_wined3d_object_destroyed,
};

356 357
void surface_init(struct d3d9_surface *surface, struct wined3d_texture *wined3d_texture,
        unsigned int sub_resource_idx, const struct wined3d_parent_ops **parent_ops)
358
{
359
    IDirect3DBaseTexture9 *texture;
360

361
    surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
362
    d3d9_resource_init(&surface->resource);
363
    surface->resource.refcount = 0;
364
    list_init(&surface->rtv_entry);
365 366 367
    surface->container = wined3d_texture_get_parent(wined3d_texture);
    surface->wined3d_texture = wined3d_texture;
    surface->sub_resource_idx = sub_resource_idx;
368

369
    if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
370 371 372 373 374
            &IID_IDirect3DBaseTexture9, (void **)&texture)))
    {
        surface->texture = unsafe_impl_from_IDirect3DBaseTexture9(texture);
        IDirect3DBaseTexture9_Release(texture);
    }
375

376
    *parent_ops = &d3d9_surface_wined3d_parent_ops;
377
}
378

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
{
    struct d3d9_surface *surface = parent;

    /* If the surface reference count drops to zero, we release our reference
     * to the view, but don't clear the pointer yet, in case e.g. a
     * GetRenderTarget() call brings the surface back before the view is
     * actually destroyed. When the view is destroyed, we need to clear the
     * pointer, or a subsequent surface AddRef() would reference it again.
     *
     * This is safe because as long as the view still has a reference to the
     * texture, the surface is also still alive, and we're called before the
     * view releases that reference. */
    surface->wined3d_rtv = NULL;
    list_remove(&surface->rtv_entry);
}

static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops =
{
    view_wined3d_object_destroyed,
};

401 402 403 404 405 406 407
struct d3d9_device *d3d9_surface_get_device(const struct d3d9_surface *surface)
{
    IDirect3DDevice9Ex *device;
    device = surface->texture ? surface->texture->parent_device : surface->parent_device;
    return impl_from_IDirect3DDevice9Ex(device);
}

408
struct wined3d_rendertarget_view *d3d9_surface_acquire_rendertarget_view(struct d3d9_surface *surface)
409 410 411
{
    HRESULT hr;

412 413 414 415 416
    /* The surface reference count can be equal to 0 when this function is
     * called. In order to properly manage the render target view reference
     * count, we temporarily increment the surface reference count. */
    d3d9_surface_AddRef(&surface->IDirect3DSurface9_iface);

417 418 419
    if (surface->wined3d_rtv)
        return surface->wined3d_rtv;

420 421
    if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
            surface->sub_resource_idx, surface, &d3d9_view_wined3d_parent_ops, &surface->wined3d_rtv)))
422 423
    {
        ERR("Failed to create rendertarget view, hr %#x.\n", hr);
424
        d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
425 426 427 428 429 430 431 432 433
        return NULL;
    }

    if (surface->texture)
        list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);

    return surface->wined3d_rtv;
}

434 435 436 437 438 439 440
void d3d9_surface_release_rendertarget_view(struct d3d9_surface *surface,
        struct wined3d_rendertarget_view *rtv)
{
    if (rtv)
        d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
}

441
struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
442 443 444
{
    if (!iface)
        return NULL;
445
    assert(iface->lpVtbl == &d3d9_surface_vtbl);
446 447 448

    return impl_from_IDirect3DSurface9(iface);
}