[AMSTREAM] Sync with Wine Staging 3.9. CORE-14656
[reactos.git] / dll / directx / ddraw / Surface / createsurface.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS DirectX
4 * FILE: dll/directx/ddraw/Surface/createsurface.c
5 * PURPOSE: IDirectDrawSurface Creation
6 * PROGRAMMER: Magnus Olsen
7 *
8 */
9 #include "rosdraw.h"
10
11 /*
12 * All parameters must have been checked if they are valid before they are passed to Internal_CreateSurface.
13 * If not please fix the code in the functions which call Internal_CreateSurface.
14 * ppSurf,pDDSD,pDDraw are being validated in Internal_CreateSurface.
15 */
16
17 HRESULT
18 Internal_CreateSurface( LPDDRAWI_DIRECTDRAW_INT pDDraw, LPDDSURFACEDESC2 pDDSD,
19 LPDDRAWI_DDRAWSURFACE_INT *ppSurf, IUnknown *pUnkOuter)
20 {
21 DDHAL_CANCREATESURFACEDATA mDdCanCreateSurface = { 0 };
22 DDHAL_CREATESURFACEDATA mDdCreateSurface = { 0 };
23
24 LPDDRAWI_DDRAWSURFACE_INT ThisSurfInt;
25 LPDDRAWI_DDRAWSURFACE_LCL ThisSurfLcl;
26 LPDDRAWI_DDRAWSURFACE_GBL ThisSurfGbl;
27 LPDDRAWI_DDRAWSURFACE_MORE ThisSurfMore;
28
29 LPDDRAWI_DDRAWSURFACE_INT * slist_int = NULL;
30 LPDDRAWI_DDRAWSURFACE_LCL * slist_lcl = NULL;
31 LPDDRAWI_DDRAWSURFACE_GBL * slist_gbl = NULL;
32 LPDDRAWI_DDRAWSURFACE_MORE * slist_more = NULL;
33 DWORD num_of_surf=1;
34 DWORD count;
35 HRESULT ret;
36
37 if((pDDraw->lpLcl->dwLocalFlags & DDRAWILCL_SETCOOPCALLED) != DDRAWILCL_SETCOOPCALLED)
38 {
39 return DDERR_NOCOOPERATIVELEVELSET;
40 }
41
42 if(pUnkOuter)
43 {
44 return CLASS_E_NOAGGREGATION;
45 }
46
47 if(!(pDDSD->dwFlags & DDSD_CAPS))
48 {
49 return DDERR_INVALIDPARAMS;
50 }
51 if (pDDraw->lpLcl->dwProcessId != GetCurrentProcessId() )
52 {
53 return DDERR_INVALIDOBJECT;
54 }
55
56 if ( ((pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY) == DDSCAPS_SYSTEMMEMORY) &&
57 ((pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY) == DDSCAPS_VIDEOMEMORY) )
58 {
59 return DDERR_INVALIDCAPS;
60 }
61
62 if((!(pDDSD->dwFlags & DDSD_HEIGHT) || !(pDDSD->dwFlags & DDSD_WIDTH))
63 && !(pDDSD->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
64 {
65 return DDERR_INVALIDPARAMS;
66 }
67
68 else if(((pDDSD->dwFlags & DDSD_HEIGHT) || (pDDSD->dwFlags & DDSD_WIDTH))
69 && (pDDSD->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
70 {
71 return DDERR_INVALIDPARAMS;
72 }
73
74 /*
75 * program does not need set the DDSD_LPSURFACE,
76 * if they forget set it, the ddraw will automatic
77 * set it for system memory.
78 */
79 if ( ((pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY) == DDSCAPS_SYSTEMMEMORY) &&
80 ((pDDSD->dwFlags & DDSD_LPSURFACE) != DDSD_LPSURFACE) )
81 {
82 pDDSD->dwFlags = pDDSD->dwFlags | DDSD_LPSURFACE;
83 }
84
85 /* FIXME count how many surfaces we need */
86
87 DxHeapMemAlloc(slist_int, num_of_surf * sizeof( LPDDRAWI_DDRAWSURFACE_INT ) );
88 if( slist_int == NULL)
89 {
90 ret = DDERR_OUTOFMEMORY;
91 goto cleanup;
92 }
93
94 DxHeapMemAlloc(slist_lcl, num_of_surf * sizeof( LPDDRAWI_DDRAWSURFACE_LCL ) );
95 if( slist_lcl == NULL )
96 {
97 ret = DDERR_OUTOFMEMORY;
98 goto cleanup;
99 }
100
101 /* keep pointers to all gbl surfs to be able to free them on error */
102 DxHeapMemAlloc(slist_gbl, num_of_surf * sizeof( LPDDRAWI_DDRAWSURFACE_GBL ) );
103 if( slist_gbl == NULL )
104 {
105 DxHeapMemFree(slist_int);
106 return DDERR_OUTOFMEMORY;
107 }
108
109 /* keep pointers to all more surfs to be able to free them on error */
110 DxHeapMemAlloc(slist_more, num_of_surf * sizeof( LPDDRAWI_DDRAWSURFACE_MORE ) );
111 if( slist_more == NULL )
112 {
113 DxHeapMemFree(slist_int);
114 return DDERR_OUTOFMEMORY;
115 }
116
117 for( count=0; count < num_of_surf; count++ )
118 {
119 /* Allocate the surface interface and needed members */
120 DxHeapMemAlloc(ThisSurfInt, sizeof( DDRAWI_DDRAWSURFACE_INT ) );
121 if( ThisSurfInt == NULL )
122 {
123 ret = DDERR_OUTOFMEMORY;
124 goto cleanup;
125 }
126
127 DxHeapMemAlloc(ThisSurfLcl, sizeof( DDRAWI_DDRAWSURFACE_LCL ) );
128 if( ThisSurfLcl == NULL )
129 {
130 ret = DDERR_OUTOFMEMORY;
131 goto cleanup;
132 }
133
134 DxHeapMemAlloc(ThisSurfGbl, sizeof( DDRAWI_DDRAWSURFACE_GBL ) );
135 if( ThisSurfGbl == NULL )
136 {
137 ret = DDERR_OUTOFMEMORY;
138 goto cleanup;
139 }
140
141 DxHeapMemAlloc(ThisSurfMore, sizeof( DDRAWI_DDRAWSURFACE_MORE ) );
142 if( ThisSurfMore == NULL )
143 {
144 ret = DDERR_OUTOFMEMORY;
145 goto cleanup;
146 }
147
148 /* setup lists, really needed are slist_lcl, slist_int
149 other slists should be released on return */
150
151 slist_int[count] = ThisSurfInt;
152 slist_lcl[count] = ThisSurfLcl;
153 slist_gbl[count] = ThisSurfGbl;
154 slist_more[count] = ThisSurfMore;
155
156 /* Start now fill in the member as they shall look like before call to createsurface */
157
158 ThisSurfInt->lpLcl = ThisSurfLcl;
159 ThisSurfLcl->lpGbl = ThisSurfGbl;
160
161 ThisSurfLcl->ddsCaps.dwCaps = pDDSD->ddsCaps.dwCaps;
162
163 ThisSurfGbl->lpDD = pDDraw->lpLcl->lpGbl;
164 ThisSurfGbl->lpDDHandle = pDDraw->lpLcl->lpGbl;
165
166 /* FIXME ? */
167 ThisSurfGbl->dwGlobalFlags = DDRAWISURFGBL_ISGDISURFACE;
168
169 if (pDDSD->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
170 {
171 ThisSurfGbl->wWidth = pDDraw->lpLcl->lpGbl->vmiData.dwDisplayWidth;
172 ThisSurfGbl->wHeight = pDDraw->lpLcl->lpGbl->vmiData.dwDisplayHeight;
173 ThisSurfGbl->lPitch = pDDraw->lpLcl->lpGbl->vmiData.lDisplayPitch;
174 ThisSurfGbl->dwLinearSize = pDDraw->lpLcl->lpGbl->vmiData.lDisplayPitch;
175
176
177 ThisSurfMore->dmiDDrawReserved7.wWidth = pDDraw->lpLcl->lpGbl->vmiData.dwDisplayWidth;
178 ThisSurfMore->dmiDDrawReserved7.wHeight = pDDraw->lpLcl->lpGbl->vmiData.dwDisplayHeight;
179 ThisSurfMore->dmiDDrawReserved7.wBPP = pDDraw->lpLcl->lpGbl->dwMonitorFrequency;
180
181 /* FIXME ThisSurfaceMore->dmiDDrawReserved7.wMonitorsAttachedToDesktop */
182 ThisSurfMore->dmiDDrawReserved7.wMonitorsAttachedToDesktop = 1;
183 pDDraw->lpLcl->lpPrimary = ThisSurfInt;
184 }
185 else
186 {
187 ThisSurfGbl->wWidth = (WORD)pDDSD->dwWidth;
188 ThisSurfGbl->wHeight = (WORD)pDDSD->dwHeight;
189 ThisSurfGbl->lPitch = pDDSD->lPitch;
190 ThisSurfGbl->dwLinearSize = pDDSD->lPitch;
191 }
192
193 if(pDDraw->lpVtbl == &DirectDraw7_Vtable)
194 {
195 ThisSurfInt->lpVtbl = &DirectDrawSurface7_Vtable;
196 }
197 else if(pDDraw->lpVtbl == &DirectDraw4_Vtable)
198 {
199 ThisSurfInt->lpVtbl = &DirectDrawSurface4_Vtable;
200 }
201 else if(pDDraw->lpVtbl == &DirectDraw2_Vtable)
202 {
203 ThisSurfInt->lpVtbl = &DirectDrawSurface2_Vtable;
204 }
205 else if(pDDraw->lpVtbl == &DirectDraw_Vtable)
206 {
207 ThisSurfInt->lpVtbl = &DirectDrawSurface_Vtable;
208 }
209 else
210 {
211 ret = DDERR_NOTINITIALIZED;
212 goto cleanup;
213 }
214
215 ThisSurfLcl->lpSurfMore = ThisSurfMore;
216 ThisSurfMore->dwSize = sizeof(DDRAWI_DDRAWSURFACE_MORE);
217 ThisSurfMore->lpDD_int = pDDraw;
218 ThisSurfMore->lpDD_lcl = pDDraw->lpLcl;
219 ThisSurfMore->slist = slist_lcl;
220
221 ThisSurfLcl->dwProcessId = GetCurrentProcessId();
222
223 /* FIXME the lpLnk */
224
225 Main_DDrawSurface_AddRef(ThisSurfInt);
226 }
227
228 pDDraw->lpLcl->lpGbl->dsList = (LPDDRAWI_DDRAWSURFACE_INT) slist_int;
229
230 /* Fixme call on DdCanCreate then on DdCreateSurface createsurface data here */
231
232 /* FIXME bIsDifferentPixelFormat being set to true or false with automatic detections */
233 mDdCanCreateSurface.bIsDifferentPixelFormat = FALSE;
234
235 mDdCanCreateSurface.lpDD = pDDraw->lpLcl->lpGbl;
236 mDdCanCreateSurface.CanCreateSurface = pDDraw->lpLcl->lpDDCB->HALDD.CanCreateSurface;
237 mDdCanCreateSurface.lpDDSurfaceDesc = (LPDDSURFACEDESC) pDDSD;
238 mDdCanCreateSurface.ddRVal = DDERR_GENERIC;
239
240 if (mDdCanCreateSurface.CanCreateSurface(&mDdCanCreateSurface) == DDHAL_DRIVER_NOTHANDLED)
241 {
242 DX_STUB_str("mDdCanCreateSurface failed with DDHAL_DRIVER_NOTHANDLED.");
243 ret = DDERR_NOTINITIALIZED;
244 goto cleanup;
245 }
246
247 if (mDdCanCreateSurface.ddRVal != DD_OK)
248 {
249 DX_STUB_str("mDdCanCreateSurface failed.");
250 ret = mDdCanCreateSurface.ddRVal;
251 goto cleanup;
252 }
253
254 mDdCreateSurface.lpDD = pDDraw->lpLcl->lpGbl;
255 mDdCreateSurface.CreateSurface = pDDraw->lpLcl->lpGbl->lpDDCBtmp->HALDD.CreateSurface;
256 mDdCreateSurface.ddRVal = DDERR_GENERIC;
257 mDdCreateSurface.dwSCnt = num_of_surf;
258 mDdCreateSurface.lpDDSurfaceDesc = (LPDDSURFACEDESC) pDDSD;
259 mDdCreateSurface.lplpSList = slist_lcl;
260
261 if (mDdCreateSurface.CreateSurface(&mDdCreateSurface) == DDHAL_DRIVER_NOTHANDLED)
262 {
263 DX_STUB_str("mDdCreateSurface failed with DDHAL_DRIVER_NOTHANDLED.");
264 ret = DDERR_NOTINITIALIZED;
265 goto cleanup;
266 }
267
268 if (mDdCreateSurface.ddRVal != DD_OK)
269 {
270 DX_STUB_str("mDdCreateSurface failed.");
271 ret = mDdCreateSurface.ddRVal;
272 goto cleanup;
273 }
274
275 /* free unneeded slists */
276 if (slist_more != NULL)
277 DxHeapMemFree(slist_more);
278 if (slist_gbl != NULL)
279 DxHeapMemFree(slist_gbl);
280
281 *ppSurf = (LPDDRAWI_DDRAWSURFACE_INT) &slist_int[0]->lpVtbl;
282
283 return DD_OK;
284
285 cleanup:
286 for(count = 0; count < num_of_surf; count++)
287 {
288 if (slist_more[count] != NULL)
289 DxHeapMemFree(slist_more[count]);
290 if (slist_gbl[count] != NULL)
291 DxHeapMemFree(slist_gbl[count]);
292 if (slist_lcl[count] != NULL)
293 DxHeapMemFree(slist_lcl[count]);
294 if (slist_int[count] != NULL)
295 DxHeapMemFree(slist_int[count]);
296 }
297 if (slist_more != NULL)
298 DxHeapMemFree(slist_more);
299 if (slist_gbl != NULL)
300 DxHeapMemFree(slist_gbl);
301 if (slist_lcl != NULL)
302 DxHeapMemFree(slist_lcl);
303 if (slist_int != NULL)
304 DxHeapMemFree(slist_int);
305
306 return ret;
307 }
308
309
310
311 void CopyDDSurfDescToDDSurfDesc2(LPDDSURFACEDESC2 dst_pDesc, LPDDSURFACEDESC src_pDesc)
312 {
313 RtlZeroMemory(dst_pDesc,sizeof(DDSURFACEDESC2));
314 RtlCopyMemory(dst_pDesc,src_pDesc,sizeof(DDSURFACEDESC));
315 dst_pDesc->dwSize = sizeof(DDSURFACEDESC2);
316 }
317
318
319
320
321