fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / ddraw / ddraw_private.h
1 /*
2  * Copyright 2000-2001 TransGaming Technologies Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #ifndef __WINE_DLLS_DDRAW_DDRAW_PRIVATE_H
20 #define __WINE_DLLS_DDRAW_DDRAW_PRIVATE_H
21
22 /* MAY NOT CONTAIN X11 or DGA specific includes/defines/structs! */
23
24 #include "winbase.h"
25 #include "wtypes.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "ddraw.h"
29 #include "d3d.h"
30 #include "ddcomimpl.h"
31 #include "ddrawi.h"
32
33 /* XXX Put this somewhere proper. */
34 #define DD_STRUCT_INIT(x)                       \
35         do {                                    \
36                 memset((x), 0, sizeof(*(x)));   \
37                 (x)->dwSize = sizeof(*x);       \
38         } while (0)
39
40 #define DD_STRUCT_COPY_BYSIZE(to,from)                  \
41         do {                                            \
42                 DWORD __size = (to)->dwSize;            \
43                 DWORD __copysize = __size;              \
44                 memset(to,0,__size);                    \
45                 if ((from)->dwSize < __size)            \
46                     __copysize = (from)->dwSize;        \
47                 memcpy(to,from,__copysize);             \
48                 (to)->dwSize = __size;/*restore size*/  \
49         } while (0)
50
51 /*****************************************************************************
52  * IDirectDraw implementation structure
53  */
54
55 typedef struct IDirectDrawImpl IDirectDrawImpl;
56 typedef struct IDirectDrawPaletteImpl IDirectDrawPaletteImpl;
57 typedef struct IDirectDrawClipperImpl IDirectDrawClipperImpl;
58 typedef struct IDirectDrawSurfaceImpl IDirectDrawSurfaceImpl;
59
60 typedef void (*pixel_convert_func)(void *src, void *dst, DWORD width,
61                                    DWORD height, LONG pitch,
62                                    IDirectDrawPaletteImpl *palette);
63
64 typedef void (*palette_convert_func)(LPPALETTEENTRY palent,
65                                      void *screen_palette, DWORD start,
66                                      DWORD count);
67
68 struct IDirectDrawImpl
69 {
70     ICOM_VFIELD_MULTI(IDirectDraw7);
71     ICOM_VFIELD_MULTI(IDirectDraw4);
72     ICOM_VFIELD_MULTI(IDirectDraw2);
73     ICOM_VFIELD_MULTI(IDirectDraw);
74
75     DWORD ref;
76
77     /* TRUE if created via DirectDrawCreateEx or CoCreateInstance,
78      * FALSE if created via DirectDrawCreate. */
79     BOOL ex;
80
81     /* Linked list of surfaces, joined by next_ddraw in IDirectSurfaceImpl. */
82     IDirectDrawSurfaceImpl* surfaces;
83     /* Linked list of palettes, joined by next_ddraw. */
84     IDirectDrawPaletteImpl* palettes;
85     /* Linked list of clippers, joined by next_ddraw. */
86     IDirectDrawClipperImpl* clippers;
87
88     IDirectDrawSurfaceImpl* primary_surface;
89
90     DDRAWI_DIRECTDRAW_LCL local;
91     DDCAPS caps;
92
93     HWND window;
94     DWORD cooperative_level;
95     WNDPROC original_wndproc;
96
97     DWORD width, height;
98     LONG pitch;
99     DDPIXELFORMAT pixelformat;
100
101     /* Should each of these go into some structure? */
102     DWORD orig_width, orig_height;
103     LONG orig_pitch;
104     DDPIXELFORMAT orig_pixelformat;
105
106     /* Called when the refcount goes to 0. */
107     void (*final_release)(IDirectDrawImpl *This);
108
109     HRESULT (*set_exclusive_mode)(IDirectDrawImpl *This, DWORD dwExcl);
110
111     HRESULT (*create_palette)(IDirectDrawImpl* This, DWORD dwFlags,
112                               LPDIRECTDRAWPALETTE* ppPalette,
113                               LPUNKNOWN pUnkOuter);
114
115     /* Surface creation functions. For all of these, pOuter == NULL. */
116
117     /* Do not create any backbuffers or the flipping chain. */
118     HRESULT (*create_primary)(IDirectDrawImpl* This,
119                               const DDSURFACEDESC2* pDDSD,
120                               LPDIRECTDRAWSURFACE7* ppSurf, LPUNKNOWN pOuter);
121
122     /* Primary may be NULL if we are creating an unattached backbuffer. */
123     HRESULT (*create_backbuffer)(IDirectDrawImpl* This,
124                                  const DDSURFACEDESC2* pDDSD,
125                                  LPDIRECTDRAWSURFACE7* ppSurf,
126                                  LPUNKNOWN pOuter,
127                                  IDirectDrawSurfaceImpl* primary);
128
129     /* shiny happy offscreenplain surfaces */
130     HRESULT (*create_offscreen)(IDirectDrawImpl* This,
131                                 const DDSURFACEDESC2* pDDSD,
132                                 LPDIRECTDRAWSURFACE7* ppSurf,
133                                 LPUNKNOWN pOuter);
134
135     /* dwMipMapLevel is specified as per OpenGL. (i.e. 0 is base) */
136     HRESULT (*create_texture)(IDirectDrawImpl* This,
137                               const DDSURFACEDESC2* pDDSD,
138                               LPDIRECTDRAWSURFACE7* ppSurf, LPUNKNOWN pOuter,
139                               DWORD dwMipMapLevel);
140
141     HRESULT (*create_zbuffer)(IDirectDrawImpl* This,
142                               const DDSURFACEDESC2* pDDSD,
143                               LPDIRECTDRAWSURFACE7* ppSurf, LPUNKNOWN pOuter);
144
145     LPVOID      private;
146
147     /* Everything below here is still questionable. */
148
149     DDPIXELFORMAT screen_pixelformat;
150
151     int           pixmap_depth;
152     pixel_convert_func pixel_convert;
153     palette_convert_func palette_convert;
154
155     /* Use to fool some too strict games */
156     INT32 (*allocate_memory)(IDirectDrawImpl *This, DWORD mem);
157     void (*free_memory)(IDirectDrawImpl *This, DWORD mem);
158     DWORD total_vidmem, available_vidmem;
159     
160     /* This is to get the D3D object associated to this DDraw object */
161     struct IDirect3DImpl *d3d;
162     
163     /* This is for the fake mainWindow */
164     ATOM        winclass;
165     PAINTSTRUCT ps;
166     BOOL        paintable;
167 };
168
169 /*****************************************************************************
170  * IDirectDrawPalette implementation structure
171  */
172 struct IDirectDrawPaletteImpl
173 {
174     /* IUnknown fields */
175     ICOM_VFIELD_MULTI(IDirectDrawPalette);
176     DWORD ref;
177
178     DDRAWI_DDRAWPALETTE_LCL local;
179     DDRAWI_DDRAWPALETTE_GBL global;
180
181     /* IDirectDrawPalette fields */
182     HPALETTE            hpal;
183     WORD                palVersion, palNumEntries; /* LOGPALETTE */
184     PALETTEENTRY        palents[256];
185     /* This is to store the palette in 'screen format' */
186     int                 screen_palents[256];
187
188     VOID (*final_release)(IDirectDrawPaletteImpl* This);
189
190     IDirectDrawImpl* ddraw_owner;
191     IDirectDrawPaletteImpl* prev_ddraw;
192     IDirectDrawPaletteImpl* next_ddraw;
193
194     LPVOID              private;
195 };
196
197 /*****************************************************************************
198  * IDirectDrawClipper implementation structure
199  */
200 struct IDirectDrawClipperImpl
201 {
202     /* IUnknown fields */
203     ICOM_VFIELD_MULTI(IDirectDrawClipper);
204     DWORD ref;
205
206     /* IDirectDrawClipper fields */
207     HWND hWnd;
208
209     IDirectDrawImpl* ddraw_owner;
210     IDirectDrawClipperImpl* prev_ddraw;
211     IDirectDrawClipperImpl* next_ddraw;
212 };
213
214 /*****************************************************************************
215  * IDirectDrawSurface implementation structure
216  */
217
218 struct IDirectDrawSurfaceImpl
219 {
220     /* IUnknown fields */
221     ICOM_VFIELD_MULTI(IDirectDrawSurface7);
222     ICOM_VFIELD_MULTI(IDirectDrawSurface3);
223     ICOM_VFIELD_MULTI(IDirectDrawGammaControl);
224     ICOM_VFIELD_MULTI(IDirect3DTexture2);
225     ICOM_VFIELD_MULTI(IDirect3DTexture);
226     DWORD ref;
227
228     struct IDirectDrawSurfaceImpl* attached; /* attached surfaces */
229
230     struct IDirectDrawSurfaceImpl* next_ddraw; /* ddraw surface chain */
231     struct IDirectDrawSurfaceImpl* prev_ddraw;
232     struct IDirectDrawSurfaceImpl* next_attached; /* attached surface chain */
233     struct IDirectDrawSurfaceImpl* prev_attached;
234
235     IDirectDrawImpl* ddraw_owner;
236     IDirectDrawSurfaceImpl* surface_owner;
237
238     IDirectDrawPaletteImpl* palette; /* strong ref */
239     IDirectDrawClipperImpl* clipper; /* strong ref */
240
241     DDRAWI_DDRAWSURFACE_LCL local;
242     DDRAWI_DDRAWSURFACE_MORE more;
243     /* FIXME: since Flip should swap the GBL structures, they should
244      * probably not be embedded into the IDirectDrawSurfaceImpl structure... */
245     LPDDRAWI_DDRAWSURFACE_GBL_MORE gmore;
246     DDRAWI_DDRAWSURFACE_GBL global;
247     DDRAWI_DDRAWSURFACE_GBL_MORE global_more;
248
249     DDSURFACEDESC2 surface_desc;
250
251     HDC hDC;
252     RECT lastlockrect;
253     DWORD lastlocktype;
254     BOOL dc_in_use;
255
256     HRESULT (*duplicate_surface)(IDirectDrawSurfaceImpl* src,
257                                  LPDIRECTDRAWSURFACE7* dst);
258     void (*final_release)(IDirectDrawSurfaceImpl *This);
259     HRESULT (*late_allocate)(IDirectDrawSurfaceImpl *This);
260     BOOL (*attach)(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *to);
261     BOOL (*detach)(IDirectDrawSurfaceImpl *This);
262     void (*lock_update)(IDirectDrawSurfaceImpl* This, LPCRECT pRect, DWORD dwFlags);
263     void (*unlock_update)(IDirectDrawSurfaceImpl* This, LPCRECT pRect);
264     void (*lose_surface)(IDirectDrawSurfaceImpl* This);
265     BOOL (*flip_data)(IDirectDrawSurfaceImpl* front,
266                       IDirectDrawSurfaceImpl* back,
267                       DWORD dwFlags);
268     void (*flip_update)(IDirectDrawSurfaceImpl* front, DWORD dwFlags);
269     HRESULT (*get_dc)(IDirectDrawSurfaceImpl* This, HDC* phDC);
270     HRESULT (*release_dc)(IDirectDrawSurfaceImpl* This, HDC hDC);
271     void (*set_palette)(IDirectDrawSurfaceImpl* This, IDirectDrawPaletteImpl* pal);
272     void (*update_palette)(IDirectDrawSurfaceImpl* This, IDirectDrawPaletteImpl* pal,
273                            DWORD dwStart, DWORD dwCount, LPPALETTEENTRY palent);
274     HWND (*get_display_window)(IDirectDrawSurfaceImpl *This);
275     HRESULT (*get_gamma_ramp)(IDirectDrawSurfaceImpl *This, DWORD dwFlags, LPDDGAMMARAMP lpGammaRamp);
276     HRESULT (*set_gamma_ramp)(IDirectDrawSurfaceImpl *This, DWORD dwFlags, LPDDGAMMARAMP lpGammaRamp);
277
278     struct PrivateData* private_data;
279
280     DWORD max_lod;
281     DWORD priority;
282
283     BOOL lost;
284
285     DWORD uniqueness_value;
286
287     LPVOID private;
288
289     /* Everything below here is dodgy. */
290     /* For Direct3D use */
291     LPVOID aux_ctx, aux_data;
292     void (*aux_release)(LPVOID ctx, LPVOID data);
293     BOOL (*aux_flip)(LPVOID ctx, LPVOID data);
294     void (*aux_unlock)(LPVOID ctx, LPVOID data, LPRECT lpRect);
295     HRESULT (WINAPI *SetColorKey_cb)(struct IDirectDrawSurfaceImpl *texture, DWORD dwFlags, LPDDCOLORKEY ckey ) ;
296     /* This is to get the D3DDevice object associated to this surface */
297     struct IDirect3DDeviceImpl *d3ddevice;
298     LPVOID tex_private;
299 };
300
301 /*****************************************************************************
302  * Driver initialisation functions.
303  */
304 BOOL DDRAW_HAL_Init(HINSTANCE, DWORD, LPVOID);
305 BOOL DDRAW_User_Init(HINSTANCE, DWORD, LPVOID);
306
307 typedef struct {
308     const DDDEVICEIDENTIFIER2* info;
309     int preference;     /* how good we are. dga might get 100, xlib 50*/
310     HRESULT (*create)(const GUID*, LPDIRECTDRAW7*, LPUNKNOWN, BOOL ex);
311
312     /* For IDirectDraw7::Initialize. */
313     HRESULT (*init)(IDirectDrawImpl *, const GUID*);
314 } ddraw_driver;
315
316 void DDRAW_register_driver(const ddraw_driver*);
317
318 const ddraw_driver* DDRAW_FindDriver(const GUID* guid);
319
320 /******************************************************************************
321  * Random utilities
322  */
323
324 /* Get DDSCAPS of surface (shortcutmacro) */
325 #define SDDSCAPS(iface) ((iface)->s.surface_desc.ddsCaps.dwCaps)
326 /* Get the number of bytes per pixel for a given surface */
327 #define PFGET_BPP(pf) (pf.dwFlags&DDPF_PALETTEINDEXED8?1:((pf.u1.dwRGBBitCount+7)/8))
328 #define GET_BPP(desc) PFGET_BPP(desc.u4.ddpfPixelFormat)
329
330 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp);
331
332 typedef struct {
333     unsigned short      bpp,depth;
334     unsigned int        rmask,gmask,bmask;
335 } ConvertMode;
336
337 typedef struct {
338     void (*pixel_convert)(void *src, void *dst, DWORD width, DWORD height, LONG pitch, IDirectDrawPaletteImpl* palette);
339     void (*palette_convert)(LPPALETTEENTRY palent, void *screen_palette, DWORD start, DWORD count);
340 } ConvertFuncs;
341
342 typedef struct {
343     ConvertMode screen, dest;
344     ConvertFuncs funcs;
345 } Convert;
346
347 extern Convert ModeEmulations[8];
348 extern int _common_depth_to_pixelformat(DWORD depth,LPDIRECTDRAW ddraw);
349
350 /******************************************************************************
351  * Structure conversion (for thunks)
352  */
353 void DDRAW_Convert_DDSCAPS_1_To_2(const DDSCAPS* pIn, DDSCAPS2* pOut);
354 void DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(const DDDEVICEIDENTIFIER2* pIn,
355                                              DDDEVICEIDENTIFIER* pOut);
356
357 /******************************************************************************
358  * Debugging / Flags output functions
359  */
360 extern void DDRAW_dump_DDBLTFX(DWORD flagmask);
361 extern void DDRAW_dump_DDBLTFAST(DWORD flagmask);
362 extern void DDRAW_dump_DDBLT(DWORD flagmask);
363 extern void DDRAW_dump_DDSCAPS(const DDSCAPS *in);
364 extern void DDRAW_dump_DDSCAPS2(const DDSCAPS2 *in);
365 extern void DDRAW_dump_pixelformat_flag(DWORD flagmask);
366 extern void DDRAW_dump_paletteformat(DWORD dwFlags);
367 extern void DDRAW_dump_pixelformat(const DDPIXELFORMAT *in);
368 extern void DDRAW_dump_colorkeyflag(DWORD ck);
369 extern void DDRAW_dump_surface_desc(const DDSURFACEDESC2 *lpddsd);
370 extern void DDRAW_dump_cooperativelevel(DWORD cooplevel);
371 extern void DDRAW_dump_lockflag(DWORD lockflag);
372 extern void DDRAW_dump_DDCOLORKEY(const DDCOLORKEY *in);
373 extern void DDRAW_dump_DDCAPS(const DDCAPS *lpcaps);
374
375 /* Used for generic dumping */
376 typedef struct
377 {
378     DWORD val;
379     const char* name;
380 } flag_info;
381
382 #define FE(x) { x, #x }
383
384 typedef struct
385 {
386     DWORD val;
387     const char* name;
388     void (*func)(const void *);
389     ptrdiff_t offset;
390 } member_info;
391
392 #define DDRAW_dump_flags(flags,names,num_names) DDRAW_dump_flags_(flags, names, num_names, 1)
393 #define ME(x,f,e) { x, #x, (void (*)(const void *))(f), offsetof(STRUCT, e) }
394
395 extern void DDRAW_dump_flags_(DWORD flags, const flag_info* names, size_t num_names, int newline);
396 extern void DDRAW_dump_members(DWORD flags, const void* data, const member_info* mems, size_t num_mems);
397
398 #endif /* __WINE_DLLS_DDRAW_DDRAW_PRIVATE_H */