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