1 /*****************************************************************************
2 * direct2d.c : Direct2D video output plugin for vlc (Win7/Vista SP2 PF Update)
3 *****************************************************************************
4 * Copyright (C) 2010 VideoLAN and AUTHORS
7 * Author: David Kaplan <david@2of1.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_playlist.h>
36 #include <vlc_vout_display.h>
48 DEFINE_GUID(IID_ID2D1Factory, 0x6152247, 0x6f50, 0x465a, 0x92, 0x45, 0x11, 0x8b, 0xfd, 0x3b, 0x60, 0x7);
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open (vlc_object_t *);
54 static void Close(vlc_object_t *);
56 #define D2D_HELP N_("Video output for Windows 7/Windows Vista with Platform update")
59 set_category(CAT_VIDEO)
60 set_subcategory(SUBCAT_VIDEO_VOUT)
62 set_shortname("Direct2D")
63 set_description(N_("Direct2D video output"))
64 set_capability("vout display", 65)
65 add_shortcut("direct2d")
66 set_callbacks(Open, Close)
70 /*****************************************************************************
72 *****************************************************************************/
73 static picture_pool_t *Pool (vout_display_t *, unsigned);
74 static void Prepare(vout_display_t *, picture_t *);
75 static void Display(vout_display_t *, picture_t *);
76 static int Control(vout_display_t *, int, va_list);
77 static void Manage (vout_display_t *);
79 static int D2D_CreateRenderTarget(vout_display_t *vd);
80 static void D2D_ResizeRenderTarget(vout_display_t *vd);
81 static void D2D_DestroyRenderTarget(vout_display_t *vd);
84 * Initialises Direct2D vout module
86 static int Open(vlc_object_t *object)
88 vout_display_t *vd = (vout_display_t *)object;
89 vout_display_sys_t *sys;
91 vd->sys = sys = calloc(1, sizeof(*sys));
95 sys->d2_render_target = NULL;
97 sys->d2_dll = LoadLibrary(TEXT("D2D1.DLL"));
100 msg_Err(vd, "Cannot load D2D1.DLL, aborting");
104 D2D1_FACTORY_OPTIONS fo = {
105 D2D1_DEBUG_LEVEL_NONE
108 HRESULT (WINAPI *D2D1CreateFactory)(D2D1_FACTORY_TYPE, REFIID,
109 const D2D1_FACTORY_OPTIONS *,
112 D2D1CreateFactory = (void *)GetProcAddress(sys->d2_dll,
113 TEXT("D2D1CreateFactory"));
114 if (!D2D1CreateFactory) {
116 "Cannot locate reference to a D2D1CreateFactory ABI in D2D1.DLL");
121 msg_Dbg(vd, "D2D1.DLL loaded");
124 HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
125 (REFIID)&IID_ID2D1Factory,
127 (void **)&sys->d2_factory);
129 msg_Err(vd, "Cannot create Direct2D factory (hr = 0x%x)!",
137 if (D2D_CreateRenderTarget(vd) != VLC_SUCCESS)
140 vout_display_info_t info = vd->info;
141 info.is_slow = false;
142 info.has_double_click = true;
143 info.has_hide_mouse = false;
144 info.has_pictures_invalid = false;
147 vd->fmt.i_chroma = VLC_CODEC_RGB32; /* masks change this to BGR32 for ID2D1Bitmap */
148 vd->fmt.i_rmask = 0x0000ff00;
149 vd->fmt.i_gmask = 0x00ff0000;
150 vd->fmt.i_bmask = 0xff000000;
153 vd->prepare = Prepare;
154 vd->display = Display;
156 vd->control = Control;
158 EventThreadUpdateTitle(sys->event, VOUT_TITLE " (Direct2D output)");
161 msg_Dbg(vd, "Ready");
167 Close(VLC_OBJECT(vd));
172 * Close Direct2D vout
174 static void Close(vlc_object_t *object)
176 vout_display_t *vd = (vout_display_t *)object;
178 D2D_DestroyRenderTarget(vd);
181 picture_pool_Delete(vd->sys->pool);
189 * Handles pool allocations for bitmaps
191 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
193 vout_display_sys_t *sys = vd->sys;
196 sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
198 msg_Dbg(vd, "New picture pool created");
206 * Performs set up of ID2D1Bitmap memory ready for blitting
208 static void Prepare(vout_display_t *vd, picture_t *picture)
210 vout_display_sys_t *sys = vd->sys;
212 if (sys->d2_render_target && sys->d2_bitmap) {
214 HRESULT hr = ID2D1Bitmap_CopyFromMemory(sys->d2_bitmap,
216 picture->p[0].p_pixels,
217 picture->p[0].i_pitch);
219 msg_Err(vd, "Failed to copy bitmap memory (hr = 0x%x)!",
223 /*msg_Dbg(vd, "Bitmap dbg: target = %p, pitch = %d, bitmap = %p",
224 sys->d2_render_target, pitch, sys->d2_bitmap);*/
230 * Blits a scaled picture_t to the render target
232 static void Display(vout_display_t *vd, picture_t *picture)
234 vout_display_sys_t *sys = vd->sys;
236 D2D1_RECT_F r_dest = {
239 sys->rect_dest.right,
240 sys->rect_dest.bottom
243 if (sys->d2_render_target && sys->d2_bitmap) {
244 ID2D1HwndRenderTarget_BeginDraw(sys->d2_render_target);
246 ID2D1HwndRenderTarget_DrawBitmap(sys->d2_render_target,
250 D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
253 HRESULT hr = ID2D1HwndRenderTarget_EndDraw(sys->d2_render_target,
256 if (hr == D2DERR_RECREATE_TARGET) {
257 D2D_DestroyRenderTarget(vd);
258 D2D_CreateRenderTarget(vd);
262 picture_Release(picture);
268 * Control event handler
270 static int Control(vout_display_t *vd, int query, va_list args)
272 return CommonControl(vd, query, args);
276 * Handles surface management
277 * ID2D1RenderTargets cannot be resized and must be recreated
279 static void Manage(vout_display_t *vd)
281 vout_display_sys_t *sys = vd->sys;
285 if (sys->changes & DX_POSITION_CHANGE) {
286 D2D_ResizeRenderTarget(vd);
287 sys->changes &= ~DX_POSITION_CHANGE;
292 * Creates a ID2D1HwndRenderTarget and associated ID2D1Bitmap
294 static int D2D_CreateRenderTarget(vout_display_t *vd)
296 vout_display_sys_t *sys = vd->sys;
298 sys->d2_render_target = NULL;
300 D2D1_PIXEL_FORMAT pf = {
301 DXGI_FORMAT_B8G8R8A8_UNORM,
302 D2D1_ALPHA_MODE_IGNORE
305 D2D1_RENDER_TARGET_PROPERTIES rtp = {
306 D2D1_RENDER_TARGET_TYPE_DEFAULT,
310 D2D1_RENDER_TARGET_USAGE_NONE,
311 D2D1_FEATURE_LEVEL_DEFAULT
315 sys->rect_dest.right - sys->rect_dest.left,
316 sys->rect_dest.bottom - sys->rect_dest.top
319 D2D1_HWND_RENDER_TARGET_PROPERTIES hrtp = {
322 D2D1_PRESENT_OPTIONS_IMMEDIATELY /* this might need fiddling */
325 HRESULT hr = ID2D1Factory_CreateHwndRenderTarget(sys->d2_factory,
328 &sys->d2_render_target);
330 msg_Err(vd, "Cannot create render target (hvideownd = 0x%x, width = %d, height = %d, pf.format = %d, hr = 0x%x)!",
331 (unsigned)hrtp.hwnd, hrtp.pixelSize.width,
332 hrtp.pixelSize.height, pf.format, (unsigned)hr);
334 sys->d2_render_target = NULL;
341 ID2D1Factory_GetDesktopDpi(sys->d2_factory,
345 D2D1_BITMAP_PROPERTIES bp = {
351 D2D1_SIZE_U bitmap_size = {
356 hr = ID2D1HwndRenderTarget_CreateBitmap(sys->d2_render_target,
363 msg_Err(vd, "Failed to create bitmap (hr = 0x%x)!", (unsigned)hr);
365 sys->d2_bitmap = NULL;
366 D2D_DestroyRenderTarget(vd);
372 msg_Dbg(vd, "Render trgt dbg: dpi = %f, render_target = %p, bitmap = %p",
373 dpi_x, sys->d2_render_target, sys->d2_bitmap);
380 * Resizes a ID2D1HWndRenderTarget
382 static void D2D_ResizeRenderTarget(vout_display_t *vd)
384 vout_display_sys_t *sys = vd->sys;
387 sys->rect_dest.right - sys->rect_dest.left,
388 sys->rect_dest.bottom - sys->rect_dest.top
391 HRESULT hr = ID2D1HwndRenderTarget_Resize(sys->d2_render_target, &size);
393 msg_Err(vd, "Cannot resize render target (width = %d, height = %d, hr = 0x%x)!",
394 size.width, size.height, (unsigned)hr);
398 * Cleans up ID2D1HwndRenderTarget and ID2D1Bitmap
400 static void D2D_DestroyRenderTarget(vout_display_t *vd)
402 vout_display_sys_t *sys = vd->sys;
404 if (sys->d2_render_target) {
405 ID2D1HwndRenderTarget_Release(sys->d2_render_target);
406 sys->d2_render_target = NULL;
409 if (sys->d2_bitmap) {
410 ID2D1Bitmap_Release(sys->d2_bitmap);
411 sys->d2_bitmap = NULL;
415 msg_Dbg(vd, "Destroyed");