Converted wingdi/wingapi to vout display.
[vlc.git] / modules / video_output / msw / wingdi.c
1 /*****************************************************************************
2  * wingdi.c : Win32 / WinCE GDI video output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_playlist.h>
37 #include <vlc_vout_display.h>
38
39 #include <windows.h>
40 #include <commctrl.h>
41
42 #include "common.h"
43
44 #ifndef WS_NONAVDONEBUTTON
45 #   define WS_NONAVDONEBUTTON 0
46 #endif
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open (vlc_object_t *);
52 static void Close(vlc_object_t *);
53
54 vlc_module_begin ()
55     set_category(CAT_VIDEO)
56     set_subcategory(SUBCAT_VIDEO_VOUT)
57 #ifdef MODULE_NAME_IS_wingapi
58     set_shortname("Windows GAPI")
59     set_description(N_("Windows GAPI video output"))
60     set_capability("vout display", 20)
61 #else
62     set_shortname("Windows GDI")
63     set_description(N_("Windows GDI video output"))
64     set_capability("vout display", 10)
65 #endif
66     set_callbacks(Open, Close)
67 vlc_module_end ()
68
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static picture_pool_t *Pool  (vout_display_t *, unsigned);
74 static void           Display(vout_display_t *, picture_t *);
75 static int            Control(vout_display_t *, int, va_list);
76 static void           Manage (vout_display_t *);
77
78 static int            Init(vout_display_t *, video_format_t *, int, int);
79 static void           Clean(vout_display_t *);
80
81 /* */
82 static int Open(vlc_object_t *object)
83 {
84     vout_display_t *vd = (vout_display_t *)object;
85     vout_display_sys_t *sys;
86
87     vd->sys = sys = calloc(1, sizeof(*sys));
88     if (!sys)
89         return VLC_ENOMEM;
90
91 #ifdef MODULE_NAME_IS_wingapi
92     /* Load GAPI */
93     sys->gapi_dll = LoadLibrary(_T("GX.DLL"));
94     if (!sys->gapi_dll) {
95         msg_Warn(vd, "failed loading gx.dll");
96         free(sys);
97         return VLC_EGENERIC;
98     }
99
100     GXOpenDisplay = (void *)GetProcAddress(sys->gapi_dll,
101         _T("?GXOpenDisplay@@YAHPAUHWND__@@K@Z"));
102     GXCloseDisplay = (void *)GetProcAddress(sys->gapi_dll,
103         _T("?GXCloseDisplay@@YAHXZ"));
104     GXBeginDraw = (void *)GetProcAddress(sys->gapi_dll,
105         _T("?GXBeginDraw@@YAPAXXZ"));
106     GXEndDraw = (void *)GetProcAddress(sys->gapi_dll,
107         _T("?GXEndDraw@@YAHXZ"));
108     GXGetDisplayProperties = (void *)GetProcAddress(sys->gapi_dll,
109         _T("?GXGetDisplayProperties@@YA?AUGXDisplayProperties@@XZ"));
110     GXSuspend = (void *)GetProcAddress(sys->gapi_dll,
111         _T("?GXSuspend@@YAHXZ"));
112     GXResume = GetProcAddress(sys->gapi_dll,
113         _T("?GXResume@@YAHXZ"));
114
115     if (!GXOpenDisplay || !GXCloseDisplay ||
116         !GXBeginDraw || !GXEndDraw ||
117         !GXGetDisplayProperties || !GXSuspend || !GXResume) {
118         msg_Err(vd, "failed GetProcAddress on gapi.dll");
119         free(sys);
120         return VLC_EGENERIC;
121     }
122
123     msg_Dbg(vd, "GAPI DLL loaded");
124 #endif
125
126     if (CommonInit(vd))
127         goto error;
128
129     /* */
130     video_format_t fmt = vd->fmt;
131     if (Init(vd, &fmt, fmt.i_width, fmt.i_height))
132         goto error;
133
134     vout_display_info_t info = vd->info;
135     info.is_slow              = false;
136     info.has_double_click     = true;
137     info.has_hide_mouse       = true;
138     info.has_pictures_invalid = true;
139
140     /* */
141     vd->fmt  = fmt;
142     vd->info = info;
143
144     vd->pool    = Pool;
145     vd->prepare = NULL;
146     vd->display = Display;
147     vd->manage  = Manage;
148     vd->control = Control;
149     return VLC_SUCCESS;
150
151 error:
152     Close(VLC_OBJECT(vd));
153     return VLC_EGENERIC;
154 }
155
156 /* */
157 static void Close(vlc_object_t *object)
158 {
159     vout_display_t *vd = (vout_display_t *)object;
160
161     Clean(vd);
162
163     CommonClean(vd);
164
165 #ifdef MODULE_NAME_IS_wingapi
166     FreeLibrary(vd->sys->gapi_dll);
167 #endif
168
169     free(vd->sys);
170 }
171
172 /* */
173 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
174 {
175     VLC_UNUSED(count);
176     return vd->sys->pool;
177 }
178 static void Display(vout_display_t *vd, picture_t *picture)
179 {
180     vout_display_sys_t *sys = vd->sys;
181
182 #ifdef MODULE_NAME_IS_wingapi
183     /* */
184 #else
185 #define rect_src vd->sys->rect_src
186 #define rect_src_clipped vd->sys->rect_src_clipped
187 #define rect_dest vd->sys->rect_dest
188 #define rect_dest_clipped vd->sys->rect_dest_clipped
189     RECT rect_dst = rect_dest_clipped;
190     HDC hdc = GetDC(sys->hvideownd);
191
192     OffsetRect(&rect_dst, -rect_dest.left, -rect_dest.top);
193     SelectObject(sys->off_dc, sys->off_bitmap);
194
195     if (rect_dest_clipped.right - rect_dest_clipped.left !=
196         rect_src_clipped.right - rect_src_clipped.left ||
197         rect_dest_clipped.bottom - rect_dest_clipped.top !=
198         rect_src_clipped.bottom - rect_src_clipped.top) {
199         StretchBlt(hdc, rect_dst.left, rect_dst.top,
200                    rect_dst.right, rect_dst.bottom,
201                    sys->off_dc,
202                    rect_src_clipped.left,  rect_src_clipped.top,
203                    rect_src_clipped.right, rect_src_clipped.bottom,
204                    SRCCOPY);
205     } else {
206         BitBlt(hdc, rect_dst.left, rect_dst.top,
207                rect_dst.right, rect_dst.bottom,
208                sys->off_dc,
209                rect_src_clipped.left, rect_src_clipped.top,
210                SRCCOPY);
211     }
212
213     ReleaseDC(sys->hvideownd, hdc);
214 #undef rect_src
215 #undef rect_src_clipped
216 #undef rect_dest
217 #undef rect_dest_clipped
218 #endif
219     /* TODO */
220     picture_Release(picture);
221
222     CommonDisplay(vd);
223 }
224 static int Control(vout_display_t *vd, int query, va_list args)
225 {
226     switch (query) {
227     case VOUT_DISPLAY_RESET_PICTURES:
228         assert(0);
229         return VLC_EGENERIC;
230     default:
231         return CommonControl(vd, query, args);
232     }
233
234 }
235 static void Manage(vout_display_t *vd)
236 {
237     CommonManage(vd);
238 }
239
240 #ifdef MODULE_NAME_IS_wingapi
241 struct picture_sys_t {
242     vout_display_t *vd;
243 };
244
245 static int Lock(picture_t *picture)
246 {
247     vout_display_t *vd = picture->p_sys->vd;
248
249     /* */
250     if (sys->rect_dest_clipped.right  - sys->rect_dest_clipped.left != vd->fmt.i_width ||
251         sys->rect_dest_clipped.bottom - sys->rect_dest_clipped.top  != vd->fmt.i_height)
252         return VLC_EGENERIC;
253
254     /* */
255     GXDisplayProperties gxdisplayprop = GXGetDisplayProperties();
256     uint8_t *p_pic_buffer = GXBeginDraw();
257     if (!sys->p_pic_buffer) {
258         msg_Err(vd, "GXBeginDraw error %d ", GetLastError());
259         return VLC_EGENERIC;
260     }
261     p_pic_buffer += sys->rect_dest.top  * gxdisplayprop.cbyPitch +
262                     sys->rect_dest.left * gxdisplayprop.cbxPitch;
263
264     /* */
265     picture->p[0].i_pitch  = gxdisplayprop.cbyPitch;
266     picture->p[0].p_pixels = p_pic_buffer;
267
268     return VLC_SUCCESS;
269 }
270 static void Unlock(picture_t *picture)
271 {
272     GXEndDraw();
273 }
274 #endif
275
276 static int Init(vout_display_t *vd,
277                 video_format_t *fmt, int width, int height)
278 {
279     vout_display_sys_t *sys = vd->sys;
280
281     /* */
282     RECT *display = &sys->rect_display;
283     display->left   = 0;
284     display->top    = 0;
285 #ifdef MODULE_NAME_IS_wingapi
286     display->right  = GXGetDisplayProperties().cxWidth;
287     display->bottom = GXGetDisplayProperties().cyHeight;
288 #else
289     display->right  = GetSystemMetrics(SM_CXSCREEN);;
290     display->bottom = GetSystemMetrics(SM_CYSCREEN);;
291 #endif
292
293     /* Initialize an offscreen bitmap for direct buffer operations. */
294
295     /* */
296     HDC window_dc = GetDC(sys->hvideownd);
297
298     /* */
299 #ifdef MODULE_NAME_IS_wingapi
300     GXDisplayProperties gx_displayprop = GXGetDisplayProperties();
301     sys->i_depth = gx_displayprop.cBPP;
302 #else
303
304     sys->i_depth = GetDeviceCaps(window_dc, PLANES) *
305                    GetDeviceCaps(window_dc, BITSPIXEL);
306 #endif
307
308     /* */
309     msg_Dbg(vd, "GDI depth is %i", sys->i_depth);
310     switch (sys->i_depth) {
311     case 8:
312         fmt->i_chroma = VLC_CODEC_RGB8;
313         break;
314     case 15:
315         fmt->i_chroma = VLC_CODEC_RGB15;
316         fmt->i_rmask  = 0x7c00;
317         fmt->i_gmask  = 0x03e0;
318         fmt->i_bmask  = 0x001f;
319         break;
320     case 16:
321         fmt->i_chroma = VLC_CODEC_RGB16;
322         fmt->i_rmask  = 0xf800;
323         fmt->i_gmask  = 0x07e0;
324         fmt->i_bmask  = 0x001f;
325         break;
326     case 24:
327         fmt->i_chroma = VLC_CODEC_RGB24;
328         fmt->i_rmask  = 0x00ff0000;
329         fmt->i_gmask  = 0x0000ff00;
330         fmt->i_bmask  = 0x000000ff;
331         break;
332     case 32:
333         fmt->i_chroma = VLC_CODEC_RGB32;
334         fmt->i_rmask  = 0x00ff0000;
335         fmt->i_gmask  = 0x0000ff00;
336         fmt->i_bmask  = 0x000000ff;
337         break;
338     default:
339         msg_Err(vd, "screen depth %i not supported", sys->i_depth);
340         return VLC_EGENERIC;
341     }
342     fmt->i_width  = width;
343     fmt->i_height = height;
344
345     uint8_t *p_pic_buffer;
346     int     i_pic_pitch;
347 #ifdef MODULE_NAME_IS_wingapi
348     GXOpenDisplay(sys->hvideownd, GX_FULLSCREEN);
349     EventThreadUpdateTitle(sys->event, VOUT_TITLE " (WinGAPI output)");
350
351     /* Filled by pool::lock() */
352     p_pic_buffer = NULL;
353     i_pic_pitch  = 0;
354 #else
355     /* Initialize offscreen bitmap */
356     BITMAPINFO *bi = &sys->bitmapinfo;
357     memset(bi, 0, sizeof(BITMAPINFO) + 3 * sizeof(RGBQUAD));
358     if (sys->i_depth > 8) {
359         ((DWORD*)bi->bmiColors)[0] = fmt->i_rmask;
360         ((DWORD*)bi->bmiColors)[1] = fmt->i_gmask;
361         ((DWORD*)bi->bmiColors)[2] = fmt->i_bmask;;
362     }
363
364     BITMAPINFOHEADER *bih = &sys->bitmapinfo.bmiHeader;
365     bih->biSize = sizeof(BITMAPINFOHEADER);
366     bih->biSizeImage     = 0;
367     bih->biPlanes        = 1;
368     bih->biCompression   = (sys->i_depth == 15 ||
369                             sys->i_depth == 16) ? BI_BITFIELDS : BI_RGB;
370     bih->biBitCount      = sys->i_depth;
371     bih->biWidth         = fmt->i_width;
372     bih->biHeight        = -fmt->i_height;
373     bih->biClrImportant  = 0;
374     bih->biClrUsed       = 0;
375     bih->biXPelsPerMeter = 0;
376     bih->biYPelsPerMeter = 0;
377
378     i_pic_pitch = bih->biBitCount * bih->biWidth / 8;
379     sys->off_bitmap = CreateDIBSection(window_dc,
380                                        (BITMAPINFO *)bih,
381                                        DIB_RGB_COLORS,
382                                        &p_pic_buffer, NULL, 0);
383
384     sys->off_dc = CreateCompatibleDC(window_dc);
385
386     SelectObject(sys->off_dc, sys->off_bitmap);
387     ReleaseDC(sys->hvideownd, window_dc);
388
389     EventThreadUpdateTitle(sys->event, VOUT_TITLE " (WinGDI output)");
390 #endif
391
392     /* */
393     picture_resource_t rsc;
394     memset(&rsc, 0, sizeof(rsc));
395 #ifdef MODULE_NAME_IS_wingapi
396     rsc.p_sys = malloc(sizeof(*rsc.p_sys));
397     if (!rsc.p_sys)
398         return VLC_EGENERIC;
399     rsc.p_sys->vd = vd;
400 #endif
401     rsc.p[0].p_pixels = p_pic_buffer;
402     rsc.p[0].i_lines  = fmt->i_height;
403     rsc.p[0].i_pitch  = i_pic_pitch;;
404
405     picture_t *picture = picture_NewFromResource(fmt, &rsc);
406     if (picture) {
407         picture_pool_configuration_t cfg;
408         memset(&cfg, 0, sizeof(cfg));
409         cfg.picture_count = 1;
410         cfg.picture = &picture;
411 #ifdef MODULE_NAME_IS_wingapi
412         cfg.lock    = Lock;
413         cfg.unlock  = Unkock;
414 #endif
415         sys->pool = picture_pool_NewExtended(&cfg);
416     } else {
417         free(rsc.p_sys);
418         sys->pool = NULL;
419     }
420
421     UpdateRects(vd, NULL, NULL, true);
422
423     return VLC_SUCCESS;
424 }
425
426 static void Clean(vout_display_t *vd)
427 {
428     vout_display_sys_t *sys = vd->sys;
429
430     if (sys->pool)
431         picture_pool_Delete(sys->pool);
432     sys->pool = NULL;
433
434 #ifdef MODULE_NAME_IS_wingapi
435     GXCloseDisplay();
436 #else
437     if (sys->off_dc)
438         DeleteDC(sys->off_dc);
439     if (sys->off_bitmap)
440         DeleteObject(sys->off_bitmap);
441 #endif
442 }
443