2 * Copyright © 2014 Jon Turney
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "windowsgl.h"
25 #include "windowsgl_internal.h"
26 #include "windowsdriconst.h"
36 HDC
window_getdc(windowsDrawable
*d
)
38 return GetDC(d
->hWnd
);
42 void window_releasedc(windowsDrawable
*d
, HDC dc
)
44 ReleaseDC(d
->hWnd
, dc
);
47 static struct windowsdrawable_callbacks window_callbacks
= {
48 .type
= WindowsDRIDrawableWindow
,
49 .getdc
= window_getdc
,
50 .releasedc
= window_releasedc
58 HDC
pixmap_getdc(windowsDrawable
*d
)
64 void pixmap_releasedc(windowsDrawable
*d
, HDC dc
)
69 static struct windowsdrawable_callbacks pixmap_callbacks
= {
70 .type
= WindowsDRIDrawablePixmap
,
71 .getdc
= pixmap_getdc
,
72 .releasedc
= pixmap_releasedc
80 HDC
pbuffer_getdc(windowsDrawable
*d
)
82 return wglGetPbufferDCARB(d
->hPbuffer
);
86 void pbuffer_releasedc(windowsDrawable
*d
, HDC dc
)
88 wglReleasePbufferDCARB(d
->hPbuffer
, dc
);
91 static struct windowsdrawable_callbacks pbuffer_callbacks
= {
92 .type
= WindowsDRIDrawablePbuffer
,
93 .getdc
= pbuffer_getdc
,
94 .releasedc
= pbuffer_releasedc
102 windows_create_drawable(int type
, void *handle
)
106 d
= calloc(1, sizeof *d
);
112 case WindowsDRIDrawableWindow
:
114 d
->callbacks
= &window_callbacks
;
117 case WindowsDRIDrawablePixmap
:
119 BITMAPINFOHEADER
*pBmpHeader
;
124 d
->callbacks
= &pixmap_callbacks
;
126 // Access file mapping object by a name
127 snprintf(name
, sizeof(name
), "Local\\CYGWINX_WINDOWSDRI_%08x", (unsigned int)(uintptr_t)handle
);
128 d
->hSection
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, name
);
130 printf("OpenFileMapping failed %x\n", (int)GetLastError());
132 // Create a screen-compatible DC
133 d
->dibDC
= CreateCompatibleDC(NULL
);
135 // Map the shared memory section to access the BITMAPINFOHEADER
136 pBmpHeader
= (BITMAPINFOHEADER
*)MapViewOfFile(d
->hSection
, FILE_MAP_ALL_ACCESS
, 0, 0, sizeof(BITMAPINFOHEADER
));
138 printf("MapViewOfFile failed %x\n", (int)GetLastError());
140 // Create a DIB using the file mapping
141 d
->hDIB
= CreateDIBSection(d
->dibDC
, (BITMAPINFO
*) pBmpHeader
,
142 DIB_RGB_COLORS
, &pBits
, d
->hSection
,
143 sizeof(BITMAPINFOHEADER
));
145 // Done with the BITMAPINFOHEADER
146 UnmapViewOfFile(pBmpHeader
);
148 // Select the DIB into the DC
149 d
->hOldDIB
= SelectObject(d
->dibDC
, d
->hDIB
);
153 case WindowsDRIDrawablePbuffer
:
154 d
->hPbuffer
= handle
;
155 d
->callbacks
= &pbuffer_callbacks
;
163 windows_destroy_drawable(windowsDrawable
*drawable
)
165 switch (drawable
->callbacks
->type
)
167 case WindowsDRIDrawableWindow
:
170 case WindowsDRIDrawablePixmap
:
172 // Select the default DIB into the DC
173 SelectObject(drawable
->dibDC
, drawable
->hOldDIB
);
175 // delete the screen-compatible DC
176 DeleteDC(drawable
->dibDC
);
179 DeleteObject(drawable
->hDIB
);
181 // Close the file mapping object
182 CloseHandle(drawable
->hSection
);
186 case WindowsDRIDrawablePbuffer
: