Add support for more than 2 audio channels.
[wine] / dlls / ddraw / dsurface / hal.c
1 /*      DirectDrawSurface HAL driver
2  *
3  * Copyright 2001 TransGaming Technologies Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "config.h"
21
22 #include <assert.h>
23 #include <stdlib.h>
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #define CONST_VTABLE
29
30 #include "wine/debug.h"
31 #include "ddraw_private.h"
32 #include "ddraw/user.h"
33 #include "ddraw/hal.h"
34 #include "dsurface/main.h"
35 #include "dsurface/dib.h"
36 #include "dsurface/user.h"
37 #include "dsurface/hal.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
40
41 static const IDirectDrawSurface7Vtbl HAL_IDirectDrawSurface7_VTable;
42
43 static HRESULT HAL_DirectDrawSurface_create_surface(IDirectDrawSurfaceImpl* This,
44                                                     IDirectDrawImpl* pDD)
45 {
46     HAL_PRIV_VAR(priv, This);
47     HAL_DDRAW_PRIV_VAR(ddpriv, pDD);
48     LPDDRAWI_DIRECTDRAW_GBL dd_gbl = pDD->local.lpGbl;
49     LPDDRAWI_DDRAWSURFACE_LCL local = &This->local;
50     DDHAL_CREATESURFACEDATA data;
51     HRESULT hr;
52
53     data.lpDD = dd_gbl;
54     data.lpDDSurfaceDesc = (LPDDSURFACEDESC)&This->surface_desc;
55     data.lplpSList = &local;
56     data.dwSCnt = 1;
57     data.ddRVal = 0;
58     data.CreateSurface = dd_gbl->lpDDCBtmp->HALDD.CreateSurface;
59     hr = data.CreateSurface(&data);
60
61     if (hr == DDHAL_DRIVER_HANDLED) {
62         if (This->global.fpVidMem < 4) {
63             /* grab framebuffer data from current_mode */
64             priv->hal.fb_pitch = dd_gbl->vmiData.lDisplayPitch;
65             priv->hal.fb_vofs  = ddpriv->hal.next_vofs;
66             priv->hal.fb_addr  = ((LPBYTE)dd_gbl->vmiData.fpPrimary) +
67                                  dd_gbl->vmiData.lDisplayPitch * priv->hal.fb_vofs;
68             TRACE("vofs=%ld, addr=%p\n", priv->hal.fb_vofs, priv->hal.fb_addr);
69             ddpriv->hal.next_vofs += This->surface_desc.dwHeight;
70
71             This->global.fpVidMem = (FLATPTR)priv->hal.fb_addr;
72             This->global.u4.lPitch = priv->hal.fb_pitch;
73         }
74         This->surface_desc.lpSurface = (LPVOID)This->global.fpVidMem;
75         This->surface_desc.dwFlags |= DDSD_LPSURFACE;
76         if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_EXECUTEBUFFER) {
77             This->surface_desc.u1.dwLinearSize = This->global.u4.dwLinearSize;
78             This->surface_desc.dwFlags |= DDSD_LINEARSIZE;
79         } else {
80             This->surface_desc.u1.lPitch = This->global.u4.lPitch;
81             This->surface_desc.dwFlags |= DDSD_PITCH;
82         }
83     }
84     else priv->hal.need_late = TRUE;
85
86     return data.ddRVal;
87 }
88
89 static inline BOOL HAL_IsUser(IDirectDrawSurfaceImpl* This)
90 {
91     HAL_PRIV_VAR(priv, This);
92     if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_EXECUTEBUFFER))
93         return FALSE;
94     if (priv->hal.fb_addr)
95         return FALSE;
96     return TRUE;
97 }
98
99 HRESULT
100 HAL_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl* This,
101                                 IDirectDrawImpl* pDD,
102                                 const DDSURFACEDESC2* pDDSD)
103 {
104     HAL_PRIV_VAR(priv, This);
105     LPDDRAWI_DIRECTDRAW_GBL dd_gbl = pDD->local.lpGbl;
106     HRESULT hr;
107
108     TRACE("(%p,%p,%p)\n",This,pDD,pDDSD);
109
110     /* copy surface_desc, we may want to modify it before DIB construction */
111     This->surface_desc = *pDDSD;
112
113     /* the driver may want to dereference these pointers */
114     This->local.lpSurfMore = &This->more;
115     This->local.lpGbl = &This->global;
116     This->gmore = &This->global_more;
117
118     if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE) {
119         hr = HAL_DirectDrawSurface_create_surface(This, pDD);
120         if (FAILED(hr)) return hr;
121
122         hr = DIB_DirectDrawSurface_Construct(This, pDD, &This->surface_desc);
123         if (FAILED(hr)) return hr;
124     }
125     else if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_EXECUTEBUFFER) {
126         FIXME("create execute buffer\n");
127         return DDERR_GENERIC;
128     }
129     else {
130         if (!(dd_gbl->dwFlags & DDRAWI_MODECHANGED)) {
131             /* force a mode set (HALs like DGA may need it) */
132             hr = HAL_DirectDraw_SetDisplayMode(ICOM_INTERFACE(pDD, IDirectDraw7),
133                                                pDD->width, pDD->height,
134                                                pDD->pixelformat.u1.dwRGBBitCount,
135                                                0, 0);
136             if (FAILED(hr)) return hr;
137         }
138
139         if (dd_gbl->vmiData.fpPrimary) {
140             hr = HAL_DirectDrawSurface_create_surface(This, pDD);
141             if (FAILED(hr)) return hr;
142
143             if (priv->hal.need_late) {
144                 /* this doesn't make sense... driver error? */
145                 ERR("driver failed to create framebuffer surface\n");
146                 return DDERR_GENERIC;
147             }
148
149             hr = DIB_DirectDrawSurface_Construct(This, pDD, &This->surface_desc);
150             if (FAILED(hr)) return hr;
151         } else {
152             /* no framebuffer, construct User-based primary */
153             hr = User_DirectDrawSurface_Construct(This, pDD, pDDSD);
154             if (FAILED(hr)) return hr;
155
156             /* must notify HAL *after* creating User-based primary */
157             /* (or use CreateSurfaceEx, which we don't yet) */
158             hr = HAL_DirectDrawSurface_create_surface(This, pDD);
159             if (FAILED(hr)) return hr;
160
161             priv->hal.need_late = FALSE;
162         }
163     }
164
165     ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
166                         HAL_IDirectDrawSurface7_VTable);
167
168     This->final_release = HAL_DirectDrawSurface_final_release;
169     This->late_allocate = HAL_DirectDrawSurface_late_allocate;
170     This->duplicate_surface = HAL_DirectDrawSurface_duplicate_surface;
171
172     This->flip_data   = HAL_DirectDrawSurface_flip_data;
173     This->flip_update = HAL_DirectDrawSurface_flip_update;
174
175     This->set_palette    = HAL_DirectDrawSurface_set_palette;
176
177     This->get_display_window = HAL_DirectDrawSurface_get_display_window;
178
179     return DD_OK;
180 }
181
182 HRESULT
183 HAL_DirectDrawSurface_Create(IDirectDrawImpl *pDD,
184                              const DDSURFACEDESC2 *pDDSD,
185                              LPDIRECTDRAWSURFACE7 *ppSurf,
186                              IUnknown *pUnkOuter)
187 {
188     IDirectDrawSurfaceImpl* This;
189     HRESULT hr;
190     assert(pUnkOuter == NULL);
191
192     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
193                      sizeof(*This) + sizeof(HAL_DirectDrawSurfaceImpl));
194     if (This == NULL) return E_OUTOFMEMORY;
195
196     This->private = (HAL_DirectDrawSurfaceImpl*)(This+1);
197
198     hr = HAL_DirectDrawSurface_Construct(This, pDD, pDDSD);
199     if (FAILED(hr))
200         HeapFree(GetProcessHeap(), 0, This);
201     else
202         *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
203
204     return hr;
205 }
206
207 void HAL_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
208 {
209     LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->more.lpDD_lcl->lpGbl;
210     DDHAL_DESTROYSURFACEDATA data;
211
212     /* destroy HAL surface */
213     data.lpDD = dd_gbl;
214     data.lpDDSurface = &This->local;
215     data.ddRVal = 0;
216     data.DestroySurface = dd_gbl->lpDDCBtmp->HALDDSurface.DestroySurface;
217     data.DestroySurface(&data);
218
219     if (HAL_IsUser(This)) {
220         User_DirectDrawSurface_final_release(This);
221     } else {
222         DIB_DirectDrawSurface_final_release(This);
223     }
224 }
225
226 HRESULT HAL_DirectDrawSurface_late_allocate(IDirectDrawSurfaceImpl* This)
227 {
228     HAL_PRIV_VAR(priv, This);
229     if (priv->hal.need_late) {
230         priv->hal.need_late = FALSE;
231         return HAL_DirectDrawSurface_create_surface(This, This->ddraw_owner);
232     }
233     return DD_OK;
234 }
235
236 void HAL_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
237                                        IDirectDrawPaletteImpl* pal)
238 {
239     LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->more.lpDD_lcl->lpGbl;
240     DDHAL_SETPALETTEDATA data;
241
242     DIB_DirectDrawSurface_set_palette(This, pal);
243     data.lpDD = dd_gbl;
244     data.lpDDSurface = &This->local;
245     data.lpDDPalette = (pal != NULL ? &pal->global : NULL);
246     data.ddRVal = 0;
247     data.Attach = TRUE; /* what's this? */
248     data.SetPalette = dd_gbl->lpDDCBtmp->HALDDSurface.SetPalette;
249     if (data.SetPalette)
250         data.SetPalette(&data);
251 }
252
253 HRESULT HAL_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
254                                                 LPDIRECTDRAWSURFACE7* ppDup)
255 {
256     return HAL_DirectDrawSurface_Create(This->ddraw_owner,
257                                              &This->surface_desc, ppDup, NULL);
258 }
259
260 void HAL_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl* This,
261                                        LPCRECT pRect, DWORD dwFlags)
262 {
263     LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->more.lpDD_lcl->lpGbl;
264     DDHAL_LOCKDATA      data;
265
266     data.lpDD           = dd_gbl;
267     data.lpDDSurface    = &This->local;
268     data.ddRVal         = 0;
269     data.lpSurfData     = This->surface_desc.lpSurface; /* FIXME: correct? */
270     if (pRect) {
271         data.rArea.top  = pRect->top;
272         data.rArea.bottom       = pRect->bottom;
273         data.rArea.left = pRect->left;
274         data.rArea.right        = pRect->right;
275         data.bHasRect   = TRUE;
276     } else {
277         data.bHasRect   = FALSE;
278     }
279     data.dwFlags        = dwFlags;
280
281     data.Lock           = dd_gbl->lpDDCBtmp->HALDDSurface.Lock;
282     if (data.Lock && (data.Lock(&data) == DDHAL_DRIVER_HANDLED))
283         return;
284
285     if (HAL_IsUser(This)) {
286         User_DirectDrawSurface_lock_update(This, pRect, dwFlags);
287     } else {
288         Main_DirectDrawSurface_lock_update(This, pRect, dwFlags);
289     }
290 }
291
292 void HAL_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl* This,
293                                          LPCRECT pRect)
294 {
295     LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->more.lpDD_lcl->lpGbl;
296     DDHAL_UNLOCKDATA    data;
297
298     data.lpDD           = dd_gbl;
299     data.lpDDSurface    = &This->local;
300     data.ddRVal         = 0;
301     data.Unlock         = dd_gbl->lpDDCBtmp->HALDDSurface.Unlock;
302     if (data.Unlock && (data.Unlock(&data) == DDHAL_DRIVER_HANDLED))
303         return;
304
305     if (HAL_IsUser(This)) {
306         User_DirectDrawSurface_unlock_update(This, pRect);
307     } else {
308         Main_DirectDrawSurface_unlock_update(This, pRect);
309     }
310 }
311
312 BOOL HAL_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
313                                      IDirectDrawSurfaceImpl* back,
314                                      DWORD dwFlags)
315 {
316     HAL_PRIV_VAR(front_priv, front);
317     HAL_PRIV_VAR(back_priv, back);
318     LPDDRAWI_DIRECTDRAW_GBL dd_gbl = front->more.lpDD_lcl->lpGbl;
319     DDHAL_FLIPDATA data;
320     BOOL ret;
321
322     {
323         DWORD tmp;
324         tmp = front_priv->hal.fb_vofs;
325         front_priv->hal.fb_vofs = back_priv->hal.fb_vofs;
326         back_priv->hal.fb_vofs = tmp;
327     }
328     {
329         LPVOID tmp;
330         tmp = front_priv->hal.fb_addr;
331         front_priv->hal.fb_addr = back_priv->hal.fb_addr;
332         back_priv->hal.fb_addr = tmp;
333     }
334
335     if (HAL_IsUser(front)) {
336         ret = User_DirectDrawSurface_flip_data(front, back, dwFlags);
337     } else {
338         ret = DIB_DirectDrawSurface_flip_data(front, back, dwFlags);
339     }
340
341     TRACE("(%p,%p)\n",front,back);
342     data.lpDD = dd_gbl;
343     data.lpSurfCurr = &front->local;
344     data.lpSurfTarg = &back->local;
345     data.lpSurfCurrLeft = NULL;
346     data.lpSurfTargLeft = NULL;
347     data.dwFlags = dwFlags;
348     data.ddRVal = 0;
349     data.Flip = dd_gbl->lpDDCBtmp->HALDDSurface.Flip;
350     if (data.Flip)
351         if (data.Flip(&data) == DDHAL_DRIVER_HANDLED) ret = FALSE;
352
353     return ret;
354 }
355
356 void HAL_DirectDrawSurface_flip_update(IDirectDrawSurfaceImpl* This, DWORD dwFlags)
357 {
358     if (HAL_IsUser(This)) {
359         User_DirectDrawSurface_flip_update(This, dwFlags);
360     }
361 }
362
363 HWND HAL_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl* This)
364 {
365     return 0;
366 }
367
368 static const IDirectDrawSurface7Vtbl HAL_IDirectDrawSurface7_VTable =
369 {
370     Main_DirectDrawSurface_QueryInterface,
371     Main_DirectDrawSurface_AddRef,
372     Main_DirectDrawSurface_Release,
373     Main_DirectDrawSurface_AddAttachedSurface,
374     Main_DirectDrawSurface_AddOverlayDirtyRect,
375     DIB_DirectDrawSurface_Blt,
376     Main_DirectDrawSurface_BltBatch,
377     DIB_DirectDrawSurface_BltFast,
378     Main_DirectDrawSurface_DeleteAttachedSurface,
379     Main_DirectDrawSurface_EnumAttachedSurfaces,
380     Main_DirectDrawSurface_EnumOverlayZOrders,
381     Main_DirectDrawSurface_Flip,
382     Main_DirectDrawSurface_GetAttachedSurface,
383     Main_DirectDrawSurface_GetBltStatus,
384     Main_DirectDrawSurface_GetCaps,
385     Main_DirectDrawSurface_GetClipper,
386     Main_DirectDrawSurface_GetColorKey,
387     Main_DirectDrawSurface_GetDC,
388     Main_DirectDrawSurface_GetFlipStatus,
389     Main_DirectDrawSurface_GetOverlayPosition,
390     Main_DirectDrawSurface_GetPalette,
391     Main_DirectDrawSurface_GetPixelFormat,
392     Main_DirectDrawSurface_GetSurfaceDesc,
393     Main_DirectDrawSurface_Initialize,
394     Main_DirectDrawSurface_IsLost,
395     Main_DirectDrawSurface_Lock,
396     Main_DirectDrawSurface_ReleaseDC,
397     DIB_DirectDrawSurface_Restore,
398     Main_DirectDrawSurface_SetClipper,
399     Main_DirectDrawSurface_SetColorKey,
400     Main_DirectDrawSurface_SetOverlayPosition,
401     Main_DirectDrawSurface_SetPalette,
402     Main_DirectDrawSurface_Unlock,
403     Main_DirectDrawSurface_UpdateOverlay,
404     Main_DirectDrawSurface_UpdateOverlayDisplay,
405     Main_DirectDrawSurface_UpdateOverlayZOrder,
406     Main_DirectDrawSurface_GetDDInterface,
407     Main_DirectDrawSurface_PageLock,
408     Main_DirectDrawSurface_PageUnlock,
409     DIB_DirectDrawSurface_SetSurfaceDesc,
410     Main_DirectDrawSurface_SetPrivateData,
411     Main_DirectDrawSurface_GetPrivateData,
412     Main_DirectDrawSurface_FreePrivateData,
413     Main_DirectDrawSurface_GetUniquenessValue,
414     Main_DirectDrawSurface_ChangeUniquenessValue,
415     Main_DirectDrawSurface_SetPriority,
416     Main_DirectDrawSurface_GetPriority,
417     Main_DirectDrawSurface_SetLOD,
418     Main_DirectDrawSurface_GetLOD
419 };