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