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