swapchain.c 10.8 KB
Newer Older
1 2 3 4 5
/*
 * IDirect3DSwapChain9 implementation
 *
 * Copyright 2002-2003 Jason Edmeades
 *                     Raphael Junqueira
Oliver Stieber's avatar
Oliver Stieber committed
6
 * Copyright 2005 Oliver Stieber
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 24 25
 */

#include "config.h"
#include "d3d9_private.h"

Oliver Stieber's avatar
Oliver Stieber committed
26
WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 28

/* IDirect3DSwapChain IUnknown parts follow: */
29
static HRESULT WINAPI IDirect3DSwapChain9Impl_QueryInterface(LPDIRECT3DSWAPCHAIN9 iface, REFIID riid, LPVOID* ppobj)
30
{
31
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
32 33 34

    if (IsEqualGUID(riid, &IID_IUnknown)
        || IsEqualGUID(riid, &IID_IDirect3DSwapChain9)) {
Oliver Stieber's avatar
Oliver Stieber committed
35 36
        IUnknown_AddRef(iface);
        *ppobj = This;
H. Verbeet's avatar
H. Verbeet committed
37
        return S_OK;
38 39 40
    }

    WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
H. Verbeet's avatar
H. Verbeet committed
41
    *ppobj = NULL;
42 43 44
    return E_NOINTERFACE;
}

45
static ULONG WINAPI IDirect3DSwapChain9Impl_AddRef(LPDIRECT3DSWAPCHAIN9 iface) {
46
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
47 48
    ULONG ref = InterlockedIncrement(&This->ref);

49
    TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50 51

    return ref;
52 53
}

54
static ULONG WINAPI IDirect3DSwapChain9Impl_Release(LPDIRECT3DSWAPCHAIN9 iface) {
55
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
56 57
    ULONG ref = InterlockedDecrement(&This->ref);

58
    TRACE("(%p) : ReleaseRef to %d\n", This, ref);
59

60
    if (ref == 0) {
61
        IWineD3DSwapChain_Release(This->wineD3DSwapChain);
62
        if (This->parentDevice) IUnknown_Release(This->parentDevice);
63 64 65 66 67 68
        HeapFree(GetProcessHeap(), 0, This);
    }
    return ref;
}

/* IDirect3DSwapChain9 parts follow: */
69
static HRESULT WINAPI IDirect3DSwapChain9Impl_Present(LPDIRECT3DSWAPCHAIN9 iface, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags) {
70
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
71 72
    TRACE("(%p) Relay\n", This);
    return IWineD3DSwapChain_Present(This->wineD3DSwapChain, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
73 74
}

75
static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DSurface9* pDestSurface) {
76
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
77 78
    TRACE("(%p) Relay\n", This);
    return IWineD3DSwapChain_GetFrontBufferData(This->wineD3DSwapChain,  ((IDirect3DSurface9Impl *)pDestSurface)->wineD3DSurface);
79 80
}

81
static HRESULT WINAPI IDirect3DSwapChain9Impl_GetBackBuffer(LPDIRECT3DSWAPCHAIN9 iface, UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9** ppBackBuffer) {
82
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
83 84 85 86 87
    HRESULT hrc = D3D_OK;
    IWineD3DSurface *mySurface = NULL;

    TRACE("(%p) Relay\n", This);

88
    hrc = IWineD3DSwapChain_GetBackBuffer(This->wineD3DSwapChain, iBackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &mySurface);
Oliver Stieber's avatar
Oliver Stieber committed
89 90
    if (hrc == D3D_OK && NULL != mySurface) {
       IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppBackBuffer);
91
       IWineD3DSurface_Release(mySurface);
Oliver Stieber's avatar
Oliver Stieber committed
92
    }
93
    /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
Oliver Stieber's avatar
Oliver Stieber committed
94
    return hrc;
95 96
}

97
static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
98
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
99
    TRACE("(%p) Relay\n", This);
100
    return IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
101 102
}

103
static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
104
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
105
    TRACE("(%p) Relay\n", This);
106
    return IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
107 108
}

109
static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DDevice9** ppDevice) {
Oliver Stieber's avatar
Oliver Stieber committed
110 111 112
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
    HRESULT hrc = D3D_OK;
    IWineD3DDevice *device = NULL;
113

Oliver Stieber's avatar
Oliver Stieber committed
114 115 116 117 118 119 120 121
    TRACE("(%p) Relay\n", This);

    hrc = IWineD3DSwapChain_GetDevice(This->wineD3DSwapChain, &device);
    if (hrc == D3D_OK && NULL != device) {
       IWineD3DDevice_GetParent(device, (IUnknown **)ppDevice);
       IWineD3DDevice_Release(device);
    }
    return hrc;
122 123
}

124
static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
125
    IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
126 127 128 129 130 131
    WINED3DPRESENT_PARAMETERS winePresentParameters;
    TRACE("(%p)->(%p): Relay\n", This, pPresentationParameters);
    winePresentParameters.BackBufferWidth = &pPresentationParameters->BackBufferWidth;
    winePresentParameters.BackBufferHeight = &pPresentationParameters->BackBufferHeight;
    winePresentParameters.BackBufferFormat = (WINED3DFORMAT *) &pPresentationParameters->BackBufferFormat;
    winePresentParameters.BackBufferCount = &pPresentationParameters->BackBufferCount;
132
    winePresentParameters.MultiSampleType = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
133
    winePresentParameters.MultiSampleQuality = &pPresentationParameters->MultiSampleQuality;
134
    winePresentParameters.SwapEffect = (WINED3DSWAPEFFECT *)&pPresentationParameters->SwapEffect;
135 136 137 138 139 140 141
    winePresentParameters.hDeviceWindow = &pPresentationParameters->hDeviceWindow;
    winePresentParameters.Windowed = &pPresentationParameters->Windowed;
    winePresentParameters.EnableAutoDepthStencil = &pPresentationParameters->EnableAutoDepthStencil;
    winePresentParameters.Flags = &pPresentationParameters->Flags;
    winePresentParameters.FullScreen_RefreshRateInHz = &pPresentationParameters->FullScreen_RefreshRateInHz;
    winePresentParameters.PresentationInterval = &pPresentationParameters->PresentationInterval;
    return IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
142 143 144
}


145
static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
{
    IDirect3DSwapChain9Impl_QueryInterface,
    IDirect3DSwapChain9Impl_AddRef,
    IDirect3DSwapChain9Impl_Release,
    IDirect3DSwapChain9Impl_Present,
    IDirect3DSwapChain9Impl_GetFrontBufferData,
    IDirect3DSwapChain9Impl_GetBackBuffer,
    IDirect3DSwapChain9Impl_GetRasterStatus,
    IDirect3DSwapChain9Impl_GetDisplayMode,
    IDirect3DSwapChain9Impl_GetDevice,
    IDirect3DSwapChain9Impl_GetPresentParameters
};


/* IDirect3DDevice9 IDirect3DSwapChain9 Methods follow: */
HRESULT  WINAPI  IDirect3DDevice9Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain9** pSwapChain) {
162
    IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
163 164 165 166 167 168 169 170
    IDirect3DSwapChain9Impl* object;
    HRESULT hrc = D3D_OK;
    WINED3DPRESENT_PARAMETERS localParameters;

    TRACE("(%p) Relay\n", This);

    object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
    if (NULL == object) {
171
        FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
Oliver Stieber's avatar
Oliver Stieber committed
172 173 174 175 176
        return D3DERR_OUTOFVIDEOMEMORY;
    }
    object->ref = 1;
    object->lpVtbl = &Direct3DSwapChain9_Vtbl;

177 178 179 180 181
    /* The back buffer count is set to one if it's 0 */
    if(pPresentationParameters->BackBufferCount == 0) {
        pPresentationParameters->BackBufferCount = 1;
    }

Oliver Stieber's avatar
Oliver Stieber committed
182 183 184
    /* Allocate an associated WineD3DDevice object */
    localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
    localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
185
    localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
Oliver Stieber's avatar
Oliver Stieber committed
186
    localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
187
    localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
Oliver Stieber's avatar
Oliver Stieber committed
188
    localParameters.MultiSampleQuality             = &pPresentationParameters->MultiSampleQuality;
189
    localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *)&pPresentationParameters->SwapEffect;
Oliver Stieber's avatar
Oliver Stieber committed
190 191 192
    localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
    localParameters.Windowed                       = &pPresentationParameters->Windowed;
    localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
193
    localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
Oliver Stieber's avatar
Oliver Stieber committed
194 195 196 197 198 199 200 201 202
    localParameters.Flags                          = &pPresentationParameters->Flags;
    localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz;
    localParameters.PresentationInterval           = &pPresentationParameters->PresentationInterval;


    hrc = IWineD3DDevice_CreateAdditionalSwapChain(This->WineD3DDevice, &localParameters, &object->wineD3DSwapChain, (IUnknown*)object, D3D9CB_CreateRenderTarget, D3D9CB_CreateDepthStencilSurface);
    if (hrc != D3D_OK) {
        FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
        HeapFree(GetProcessHeap(), 0 , object);
203 204 205
    } else {
        IUnknown_AddRef(iface);
        object->parentDevice = iface;
Oliver Stieber's avatar
Oliver Stieber committed
206
        *pSwapChain = (IDirect3DSwapChain9 *)object;
207
        TRACE("(%p) : Created swapchain %p\n", This, *pSwapChain);
Oliver Stieber's avatar
Oliver Stieber committed
208 209
    }
    TRACE("(%p) returning %p\n", This, *pSwapChain);
210
    return hrc;
211 212 213
}

HRESULT  WINAPI  IDirect3DDevice9Impl_GetSwapChain(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, IDirect3DSwapChain9** pSwapChain) {
214
    IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
215 216 217 218 219 220 221 222
    HRESULT hrc = D3D_OK;
    IWineD3DSwapChain *swapchain = NULL;

    TRACE("(%p) Relay\n", This);

    hrc = IWineD3DDevice_GetSwapChain(This->WineD3DDevice, iSwapChain, &swapchain);
    if (hrc == D3D_OK && NULL != swapchain) {
       IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)pSwapChain);
223
       IWineD3DSwapChain_Release(swapchain);
224 225
    } else {
        *pSwapChain = NULL;
Oliver Stieber's avatar
Oliver Stieber committed
226 227
    }
    return hrc;
228 229 230
}

UINT     WINAPI  IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9 iface) {
231
    IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
Oliver Stieber's avatar
Oliver Stieber committed
232 233
    TRACE("(%p) Relay\n", This);
    return IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
234
}