mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / gdi32 / enhmfdrv / init.c
blobcf9fb9236335addd711d78001035cd7710ed6273
1 /*
2 * Enhanced MetaFile driver initialisation functions
4 * Copyright 1999 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winnls.h"
29 #include "gdi_private.h"
30 #include "enhmfdrv/enhmetafiledrv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
35 static BOOL CDECL EMFDRV_DeleteDC( PHYSDEV dev );
37 static const struct gdi_dc_funcs emfdrv_driver =
39 NULL, /* pAbortDoc */
40 EMFDRV_AbortPath, /* pAbortPath */
41 NULL, /* pAlphaBlend */
42 EMFDRV_AngleArc, /* pAngleArc */
43 EMFDRV_Arc, /* pArc */
44 EMFDRV_ArcTo, /* pArcTo */
45 EMFDRV_BeginPath, /* pBeginPath */
46 NULL, /* pBlendImage */
47 EMFDRV_Chord, /* pChord */
48 EMFDRV_CloseFigure, /* pCloseFigure */
49 NULL, /* pCreateCompatibleDC */
50 NULL, /* pCreateDC */
51 EMFDRV_DeleteDC, /* pDeleteDC */
52 EMFDRV_DeleteObject, /* pDeleteObject */
53 NULL, /* pDeviceCapabilities */
54 EMFDRV_Ellipse, /* pEllipse */
55 NULL, /* pEndDoc */
56 NULL, /* pEndPage */
57 EMFDRV_EndPath, /* pEndPath */
58 NULL, /* pEnumFonts */
59 NULL, /* pEnumICMProfiles */
60 EMFDRV_ExcludeClipRect, /* pExcludeClipRect */
61 NULL, /* pExtDeviceMode */
62 NULL, /* pExtEscape */
63 EMFDRV_ExtFloodFill, /* pExtFloodFill */
64 EMFDRV_ExtSelectClipRgn, /* pExtSelectClipRgn */
65 EMFDRV_ExtTextOut, /* pExtTextOut */
66 EMFDRV_FillPath, /* pFillPath */
67 EMFDRV_FillRgn, /* pFillRgn */
68 EMFDRV_FlattenPath, /* pFlattenPath */
69 NULL, /* pFontIsLinked */
70 EMFDRV_FrameRgn, /* pFrameRgn */
71 EMFDRV_GdiComment, /* pGdiComment */
72 NULL, /* pGetBoundsRect */
73 NULL, /* pGetCharABCWidths */
74 NULL, /* pGetCharABCWidthsI */
75 NULL, /* pGetCharWidth */
76 NULL, /* pGetCharWidthInfo */
77 EMFDRV_GetDeviceCaps, /* pGetDeviceCaps */
78 NULL, /* pGetDeviceGammaRamp */
79 NULL, /* pGetFontData */
80 NULL, /* pGetFontRealizationInfo */
81 NULL, /* pGetFontUnicodeRanges */
82 NULL, /* pGetGlyphIndices */
83 NULL, /* pGetGlyphOutline */
84 NULL, /* pGetICMProfile */
85 NULL, /* pGetImage */
86 NULL, /* pGetKerningPairs */
87 NULL, /* pGetNearestColor */
88 NULL, /* pGetOutlineTextMetrics */
89 NULL, /* pGetPixel */
90 NULL, /* pGetSystemPaletteEntries */
91 NULL, /* pGetTextCharsetInfo */
92 NULL, /* pGetTextExtentExPoint */
93 NULL, /* pGetTextExtentExPointI */
94 NULL, /* pGetTextFace */
95 NULL, /* pGetTextMetrics */
96 EMFDRV_GradientFill, /* pGradientFill */
97 EMFDRV_IntersectClipRect, /* pIntersectClipRect */
98 EMFDRV_InvertRgn, /* pInvertRgn */
99 EMFDRV_LineTo, /* pLineTo */
100 EMFDRV_ModifyWorldTransform, /* pModifyWorldTransform */
101 EMFDRV_MoveTo, /* pMoveTo */
102 EMFDRV_OffsetClipRgn, /* pOffsetClipRgn */
103 EMFDRV_OffsetViewportOrgEx, /* pOffsetViewportOrgEx */
104 EMFDRV_OffsetWindowOrgEx, /* pOffsetWindowOrgEx */
105 EMFDRV_PaintRgn, /* pPaintRgn */
106 EMFDRV_PatBlt, /* pPatBlt */
107 EMFDRV_Pie, /* pPie */
108 EMFDRV_PolyBezier, /* pPolyBezier */
109 EMFDRV_PolyBezierTo, /* pPolyBezierTo */
110 EMFDRV_PolyDraw, /* pPolyDraw */
111 EMFDRV_PolyPolygon, /* pPolyPolygon */
112 EMFDRV_PolyPolyline, /* pPolyPolyline */
113 EMFDRV_Polygon, /* pPolygon */
114 EMFDRV_Polyline, /* pPolyline */
115 EMFDRV_PolylineTo, /* pPolylineTo */
116 NULL, /* pPutImage */
117 NULL, /* pRealizeDefaultPalette */
118 NULL, /* pRealizePalette */
119 EMFDRV_Rectangle, /* pRectangle */
120 NULL, /* pResetDC */
121 EMFDRV_RestoreDC, /* pRestoreDC */
122 EMFDRV_RoundRect, /* pRoundRect */
123 EMFDRV_SaveDC, /* pSaveDC */
124 EMFDRV_ScaleViewportExtEx, /* pScaleViewportExtEx */
125 EMFDRV_ScaleWindowExtEx, /* pScaleWindowExtEx */
126 EMFDRV_SelectBitmap, /* pSelectBitmap */
127 EMFDRV_SelectBrush, /* pSelectBrush */
128 EMFDRV_SelectClipPath, /* pSelectClipPath */
129 EMFDRV_SelectFont, /* pSelectFont */
130 EMFDRV_SelectPalette, /* pSelectPalette */
131 EMFDRV_SelectPen, /* pSelectPen */
132 EMFDRV_SetArcDirection, /* pSetArcDirection */
133 EMFDRV_SetBkColor, /* pSetBkColor */
134 EMFDRV_SetBkMode, /* pSetBkMode */
135 NULL, /* pSetBoundsRect */
136 EMFDRV_SetDCBrushColor, /* pSetDCBrushColor*/
137 EMFDRV_SetDCPenColor, /* pSetDCPenColor*/
138 EMFDRV_SetDIBitsToDevice, /* pSetDIBitsToDevice */
139 NULL, /* pSetDeviceClipping */
140 NULL, /* pSetDeviceGammaRamp */
141 EMFDRV_SetLayout, /* pSetLayout */
142 EMFDRV_SetMapMode, /* pSetMapMode */
143 EMFDRV_SetMapperFlags, /* pSetMapperFlags */
144 EMFDRV_SetPixel, /* pSetPixel */
145 EMFDRV_SetPolyFillMode, /* pSetPolyFillMode */
146 EMFDRV_SetROP2, /* pSetROP2 */
147 NULL, /* pSetRelAbs */
148 EMFDRV_SetStretchBltMode, /* pSetStretchBltMode */
149 EMFDRV_SetTextAlign, /* pSetTextAlign */
150 NULL, /* pSetTextCharacterExtra */
151 EMFDRV_SetTextColor, /* pSetTextColor */
152 EMFDRV_SetTextJustification, /* pSetTextJustification */
153 EMFDRV_SetViewportExtEx, /* pSetViewportExtEx */
154 EMFDRV_SetViewportOrgEx, /* pSetViewportOrgEx */
155 EMFDRV_SetWindowExtEx, /* pSetWindowExtEx */
156 EMFDRV_SetWindowOrgEx, /* pSetWindowOrgEx */
157 EMFDRV_SetWorldTransform, /* pSetWorldTransform */
158 NULL, /* pStartDoc */
159 NULL, /* pStartPage */
160 EMFDRV_StretchBlt, /* pStretchBlt */
161 EMFDRV_StretchDIBits, /* pStretchDIBits */
162 EMFDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
163 EMFDRV_StrokePath, /* pStrokePath */
164 NULL, /* pUnrealizePalette */
165 EMFDRV_WidenPath, /* pWidenPath */
166 NULL, /* pD3DKMTCheckVidPnExclusiveOwnership */
167 NULL, /* pD3DKMTSetVidPnSourceOwner */
168 NULL, /* wine_get_wgl_driver */
169 NULL, /* wine_get_vulkan_driver */
170 GDI_PRIORITY_GRAPHICS_DRV /* priority */
174 /**********************************************************************
175 * EMFDRV_DeleteDC
177 static BOOL CDECL EMFDRV_DeleteDC( PHYSDEV dev )
179 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
180 UINT index;
182 HeapFree( GetProcessHeap(), 0, physDev->emh );
183 for(index = 0; index < physDev->handles_size; index++)
184 if(physDev->handles[index])
185 GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
186 HeapFree( GetProcessHeap(), 0, physDev->handles );
187 HeapFree( GetProcessHeap(), 0, physDev );
188 return TRUE;
192 /******************************************************************
193 * EMFDRV_WriteRecord
195 * Warning: this function can change the pointer to the metafile header.
197 BOOL EMFDRV_WriteRecord( PHYSDEV dev, EMR *emr )
199 DWORD len;
200 DWORD bytes_written;
201 ENHMETAHEADER *emh;
202 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
204 TRACE("record %d, size %d %s\n",
205 emr->iType, emr->nSize, physDev->hFile ? "(to disk)" : "");
207 assert( !(emr->nSize & 3) );
209 physDev->emh->nBytes += emr->nSize;
210 physDev->emh->nRecords++;
212 if(physDev->hFile) {
213 if (!WriteFile(physDev->hFile, emr, emr->nSize, &bytes_written, NULL))
214 return FALSE;
215 } else {
216 DWORD nEmfSize = HeapSize(GetProcessHeap(), 0, physDev->emh);
217 len = physDev->emh->nBytes;
218 if (len > nEmfSize) {
219 nEmfSize += (nEmfSize / 2) + emr->nSize;
220 emh = HeapReAlloc(GetProcessHeap(), 0, physDev->emh, nEmfSize);
221 if (!emh) return FALSE;
222 physDev->emh = emh;
224 memcpy((CHAR *)physDev->emh + physDev->emh->nBytes - emr->nSize, emr,
225 emr->nSize);
227 return TRUE;
231 /******************************************************************
232 * EMFDRV_UpdateBBox
234 void EMFDRV_UpdateBBox( PHYSDEV dev, RECTL *rect )
236 EMFDRV_PDEVICE *physDev = get_emf_physdev( dev );
237 RECTL *bounds = &physDev->emh->rclBounds;
238 RECTL vportRect = *rect;
240 LPtoDP( dev->hdc, (LPPOINT)&vportRect, 2 );
242 /* The coordinate systems may be mirrored
243 (LPtoDP handles points, not rectangles) */
244 if (vportRect.left > vportRect.right)
246 LONG temp = vportRect.right;
247 vportRect.right = vportRect.left;
248 vportRect.left = temp;
250 if (vportRect.top > vportRect.bottom)
252 LONG temp = vportRect.bottom;
253 vportRect.bottom = vportRect.top;
254 vportRect.top = temp;
257 if (bounds->left > bounds->right)
259 /* first bounding rectangle */
260 *bounds = vportRect;
262 else
264 bounds->left = min(bounds->left, vportRect.left);
265 bounds->top = min(bounds->top, vportRect.top);
266 bounds->right = max(bounds->right, vportRect.right);
267 bounds->bottom = max(bounds->bottom, vportRect.bottom);
271 /**********************************************************************
272 * CreateEnhMetaFileA (GDI32.@)
274 HDC WINAPI CreateEnhMetaFileA(
275 HDC hdc, /* [in] optional reference DC */
276 LPCSTR filename, /* [in] optional filename for disk metafiles */
277 const RECT *rect, /* [in] optional bounding rectangle */
278 LPCSTR description /* [in] optional description */
281 LPWSTR filenameW = NULL;
282 LPWSTR descriptionW = NULL;
283 HDC hReturnDC;
284 DWORD len1, len2, total;
286 if(filename)
288 total = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
289 filenameW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
290 MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, total );
292 if(description) {
293 len1 = strlen(description);
294 len2 = strlen(description + len1 + 1);
295 total = MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, NULL, 0 );
296 descriptionW = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) );
297 MultiByteToWideChar( CP_ACP, 0, description, len1 + len2 + 3, descriptionW, total );
300 hReturnDC = CreateEnhMetaFileW(hdc, filenameW, rect, descriptionW);
302 HeapFree( GetProcessHeap(), 0, filenameW );
303 HeapFree( GetProcessHeap(), 0, descriptionW );
305 return hReturnDC;
308 static inline BOOL devcap_is_valid( int cap )
310 if (cap >= 0 && cap <= ASPECTXY) return !(cap & 1);
311 if (cap >= PHYSICALWIDTH && cap <= COLORMGMTCAPS) return TRUE;
312 switch (cap)
314 case LOGPIXELSX:
315 case LOGPIXELSY:
316 case CAPS1:
317 case SIZEPALETTE:
318 case NUMRESERVED:
319 case COLORRES:
320 return TRUE;
322 return FALSE;
325 /**********************************************************************
326 * CreateEnhMetaFileW (GDI32.@)
328 HDC WINAPI CreateEnhMetaFileW(
329 HDC hdc, /* [in] optional reference DC */
330 LPCWSTR filename, /* [in] optional filename for disk metafiles */
331 const RECT* rect, /* [in] optional bounding rectangle */
332 LPCWSTR description /* [in] optional description */
335 HDC ret, ref_dc;
336 DC *dc;
337 EMFDRV_PDEVICE *physDev;
338 HANDLE hFile;
339 DWORD size = 0, length = 0;
340 DWORD bytes_written;
341 int cap;
343 TRACE("(%p %s %s %s)\n", hdc, debugstr_w(filename), wine_dbgstr_rect(rect), debugstr_w(description) );
345 if (!(dc = alloc_dc_ptr( OBJ_ENHMETADC ))) return 0;
347 physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
348 if (!physDev) {
349 free_dc_ptr( dc );
350 return 0;
352 if(description) { /* App name\0Title\0\0 */
353 length = lstrlenW(description);
354 length += lstrlenW(description + length + 1);
355 length += 3;
356 length *= 2;
358 size = sizeof(ENHMETAHEADER) + (length + 3) / 4 * 4;
360 if (!(physDev->emh = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size))) {
361 HeapFree( GetProcessHeap(), 0, physDev );
362 free_dc_ptr( dc );
363 return 0;
366 push_dc_driver( &dc->physDev, &physDev->dev, &emfdrv_driver );
368 physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
369 physDev->handles_size = HANDLE_LIST_INC;
370 physDev->cur_handles = 1;
371 physDev->hFile = 0;
372 physDev->dc_brush = 0;
373 physDev->dc_pen = 0;
374 physDev->restoring = 0;
375 physDev->path = FALSE;
377 if (hdc) /* if no ref, use current display */
378 ref_dc = hdc;
379 else
380 ref_dc = CreateDCW( L"DISPLAY", NULL, NULL, NULL );
382 memset( physDev->dev_caps, 0, sizeof(physDev->dev_caps) );
383 for (cap = 0; cap < ARRAY_SIZE( physDev->dev_caps ); cap++)
384 if (devcap_is_valid( cap ))
385 physDev->dev_caps[cap] = GetDeviceCaps( ref_dc, cap );
387 if (!hdc) DeleteDC( ref_dc );
389 SetVirtualResolution(physDev->dev.hdc, 0, 0, 0, 0);
391 physDev->emh->iType = EMR_HEADER;
392 physDev->emh->nSize = size;
394 physDev->emh->rclBounds.left = physDev->emh->rclBounds.top = 0;
395 physDev->emh->rclBounds.right = physDev->emh->rclBounds.bottom = -1;
397 if(rect) {
398 physDev->emh->rclFrame.left = rect->left;
399 physDev->emh->rclFrame.top = rect->top;
400 physDev->emh->rclFrame.right = rect->right;
401 physDev->emh->rclFrame.bottom = rect->bottom;
402 } else { /* Set this to {0,0 - -1,-1} and update it at the end */
403 physDev->emh->rclFrame.left = physDev->emh->rclFrame.top = 0;
404 physDev->emh->rclFrame.right = physDev->emh->rclFrame.bottom = -1;
407 physDev->emh->dSignature = ENHMETA_SIGNATURE;
408 physDev->emh->nVersion = 0x10000;
409 physDev->emh->nBytes = physDev->emh->nSize;
410 physDev->emh->nRecords = 1;
411 physDev->emh->nHandles = 1;
413 physDev->emh->sReserved = 0; /* According to docs, this is reserved and must be 0 */
414 physDev->emh->nDescription = length / 2;
416 physDev->emh->offDescription = length ? sizeof(ENHMETAHEADER) : 0;
418 physDev->emh->nPalEntries = 0; /* I guess this should start at 0 */
420 /* Size in pixels */
421 physDev->emh->szlDevice.cx = physDev->dev_caps[HORZRES];
422 physDev->emh->szlDevice.cy = physDev->dev_caps[VERTRES];
424 /* Size in millimeters */
425 physDev->emh->szlMillimeters.cx = physDev->dev_caps[HORZSIZE];
426 physDev->emh->szlMillimeters.cy = physDev->dev_caps[VERTSIZE];
428 /* Size in micrometers */
429 physDev->emh->szlMicrometers.cx = physDev->emh->szlMillimeters.cx * 1000;
430 physDev->emh->szlMicrometers.cy = physDev->emh->szlMillimeters.cy * 1000;
432 memcpy((char *)physDev->emh + sizeof(ENHMETAHEADER), description, length);
434 if (filename) /* disk based metafile */
436 if ((hFile = CreateFileW(filename, GENERIC_WRITE | GENERIC_READ, 0,
437 NULL, CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
438 free_dc_ptr( dc );
439 return 0;
441 if (!WriteFile( hFile, physDev->emh, size, &bytes_written, NULL )) {
442 free_dc_ptr( dc );
443 CloseHandle( hFile );
444 return 0;
446 physDev->hFile = hFile;
449 TRACE("returning %p\n", physDev->dev.hdc);
450 ret = physDev->dev.hdc;
451 release_dc_ptr( dc );
453 return ret;
456 /******************************************************************
457 * CloseEnhMetaFile (GDI32.@)
459 HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc) /* [in] metafile DC */
461 HENHMETAFILE hmf;
462 EMFDRV_PDEVICE *physDev;
463 DC *dc;
464 EMREOF emr;
465 HANDLE hMapping = 0;
467 TRACE("(%p)\n", hdc );
469 if (!(dc = get_dc_ptr( hdc ))) return NULL;
470 if (GetObjectType( hdc ) != OBJ_ENHMETADC)
472 release_dc_ptr( dc );
473 return NULL;
475 if (dc->refcount != 1)
477 FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
478 release_dc_ptr( dc );
479 return NULL;
481 physDev = get_emf_physdev( find_dc_driver( dc, &emfdrv_driver ));
483 if(dc->saveLevel)
484 RestoreDC(hdc, 1);
486 if (physDev->dc_brush) DeleteObject( physDev->dc_brush );
487 if (physDev->dc_pen) DeleteObject( physDev->dc_pen );
489 emr.emr.iType = EMR_EOF;
490 emr.emr.nSize = sizeof(emr);
491 emr.nPalEntries = 0;
492 emr.offPalEntries = FIELD_OFFSET(EMREOF, nSizeLast);
493 emr.nSizeLast = emr.emr.nSize;
494 EMFDRV_WriteRecord( &physDev->dev, &emr.emr );
496 /* Update rclFrame if not initialized in CreateEnhMetaFile */
497 if(physDev->emh->rclFrame.left > physDev->emh->rclFrame.right) {
498 physDev->emh->rclFrame.left = physDev->emh->rclBounds.left *
499 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
500 physDev->emh->rclFrame.top = physDev->emh->rclBounds.top *
501 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
502 physDev->emh->rclFrame.right = physDev->emh->rclBounds.right *
503 physDev->emh->szlMillimeters.cx * 100 / physDev->emh->szlDevice.cx;
504 physDev->emh->rclFrame.bottom = physDev->emh->rclBounds.bottom *
505 physDev->emh->szlMillimeters.cy * 100 / physDev->emh->szlDevice.cy;
508 if (physDev->hFile) /* disk based metafile */
510 if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0)
512 CloseHandle( physDev->hFile );
513 free_dc_ptr( dc );
514 return 0;
517 if (!WriteFile(physDev->hFile, physDev->emh, sizeof(*physDev->emh),
518 NULL, NULL))
520 CloseHandle( physDev->hFile );
521 free_dc_ptr( dc );
522 return 0;
524 HeapFree( GetProcessHeap(), 0, physDev->emh );
525 hMapping = CreateFileMappingA(physDev->hFile, NULL, PAGE_READONLY, 0,
526 0, NULL);
527 TRACE("hMapping = %p\n", hMapping );
528 physDev->emh = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
529 TRACE("view = %p\n", physDev->emh );
530 CloseHandle( hMapping );
531 CloseHandle( physDev->hFile );
534 hmf = EMF_Create_HENHMETAFILE( physDev->emh, physDev->emh->nBytes, (physDev->hFile != 0) );
535 physDev->emh = NULL; /* So it won't be deleted */
536 free_dc_ptr( dc );
537 return hmf;