direct2d.c 11.8 KB
Newer Older
David Kaplan's avatar
David Kaplan committed
1 2 3 4 5 6 7 8
/*****************************************************************************
 * direct2d.c : Direct2D video output plugin for vlc (Win7/Vista SP2 PF Update)
 *****************************************************************************
 * Copyright (C) 2010 VideoLAN and AUTHORS
 * $Id$
 *
 * Author: David Kaplan <david@2of1.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
David Kaplan's avatar
David Kaplan committed
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
David Kaplan's avatar
David Kaplan committed
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
David Kaplan's avatar
David Kaplan committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout_display.h>

#include <windows.h>
#include <commctrl.h>
39
#include <initguid.h>
David Kaplan's avatar
David Kaplan committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <d2d1.h>

#include "common.h"


/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

#define D2D_HELP N_("Video output for Windows 7/Windows Vista with Platform update")

vlc_module_begin ()
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VOUT)
    set_help(D2D_HELP)
    set_shortname("Direct2D")
    set_description(N_("Direct2D video output"))
59
    set_capability("vout display", 180)
David Kaplan's avatar
David Kaplan committed
60 61 62 63 64 65 66 67 68
    add_shortcut("direct2d")
    set_callbacks(Open, Close)
vlc_module_end ()


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static picture_pool_t *Pool  (vout_display_t *, unsigned);
69 70
static void           Prepare(vout_display_t *, picture_t *, subpicture_t *);
static void           Display(vout_display_t *, picture_t *, subpicture_t *);
David Kaplan's avatar
David Kaplan committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static int            Control(vout_display_t *, int, va_list);
static void           Manage (vout_display_t *);

static int      D2D_CreateRenderTarget(vout_display_t *vd);
static void     D2D_ResizeRenderTarget(vout_display_t *vd);
static void     D2D_DestroyRenderTarget(vout_display_t *vd);

/**
 * Initialises Direct2D vout module
 */
static int Open(vlc_object_t *object)
{
    vout_display_t *vd = (vout_display_t *)object;
    vout_display_sys_t *sys;

    vd->sys = sys = calloc(1, sizeof(*sys));
    if (!sys)
        return VLC_ENOMEM;

    sys->d2_render_target = NULL;

    sys->d2_dll = LoadLibrary(TEXT("D2D1.DLL"));
    if (!sys->d2_dll) {
94
        if (object->obj.force)
David Kaplan's avatar
David Kaplan committed
95 96 97 98 99 100 101 102 103 104 105 106 107
            msg_Err(vd, "Cannot load D2D1.DLL, aborting");
        goto error;
    }

    D2D1_FACTORY_OPTIONS fo = {
        D2D1_DEBUG_LEVEL_NONE
    };

    HRESULT (WINAPI *D2D1CreateFactory)(D2D1_FACTORY_TYPE, REFIID,
                                        const D2D1_FACTORY_OPTIONS *,
                                        void **);

    D2D1CreateFactory = (void *)GetProcAddress(sys->d2_dll,
108
                                               "D2D1CreateFactory");
David Kaplan's avatar
David Kaplan committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    if (!D2D1CreateFactory) {
        msg_Err(vd,
                "Cannot locate reference to a D2D1CreateFactory ABI in D2D1.DLL");
        goto error;
    }

#ifndef NDEBUG
    msg_Dbg(vd, "D2D1.DLL loaded");
#endif

    HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
                                   (REFIID)&IID_ID2D1Factory,
                                   &fo,
                                   (void **)&sys->d2_factory);
    if (hr != S_OK) {
        msg_Err(vd, "Cannot create Direct2D factory (hr = 0x%x)!",
                (unsigned)hr);
        goto error;
    }

    if (CommonInit(vd))
        goto error;

    if (D2D_CreateRenderTarget(vd) != VLC_SUCCESS)
        goto error;

    vout_display_info_t info = vd->info;
    info.is_slow              = false;
    info.has_double_click     = true;
    info.has_hide_mouse       = false;
    info.has_pictures_invalid = false;
    vd->info = info;

    vd->fmt.i_chroma = VLC_CODEC_RGB32; /* masks change this to BGR32 for ID2D1Bitmap */
    vd->fmt.i_rmask  = 0x0000ff00;
    vd->fmt.i_gmask  = 0x00ff0000;
    vd->fmt.i_bmask  = 0xff000000;

    vd->pool    = Pool;
    vd->prepare = Prepare;
    vd->display = Display;
    vd->manage  = Manage;
    vd->control = Control;

    EventThreadUpdateTitle(sys->event, VOUT_TITLE " (Direct2D output)");

#ifndef NDEBUG
    msg_Dbg(vd, "Ready");
#endif

    return VLC_SUCCESS;

error:
    Close(VLC_OBJECT(vd));
    return VLC_EGENERIC;
}

/**
 * Close Direct2D vout
 */
static void Close(vlc_object_t *object)
{
    vout_display_t *vd = (vout_display_t *)object;

    D2D_DestroyRenderTarget(vd);

    if (vd->sys->pool)
176
        picture_pool_Release(vd->sys->pool);
David Kaplan's avatar
David Kaplan committed
177 178 179

    CommonClean(vd);

180 181 182 183 184
    if (vd->sys->d2_factory)
        ID2D1Factory_Release(vd->sys->d2_factory);
    if (vd->sys->d2_dll)
        FreeLibrary(vd->sys->d2_dll);

David Kaplan's avatar
David Kaplan committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    free(vd->sys);
}

/**
 * Handles pool allocations for bitmaps
 */
static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
{
    vout_display_sys_t *sys = vd->sys;

    if (!sys->pool) {
        sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
#ifndef NDEBUG
        msg_Dbg(vd, "New picture pool created");
#endif
    }

    return sys->pool;
}

/**
 * Performs set up of ID2D1Bitmap memory ready for blitting
 */
208
static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
David Kaplan's avatar
David Kaplan committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
{
    vout_display_sys_t *sys = vd->sys;

    if (sys->d2_render_target && sys->d2_bitmap) {

        HRESULT hr = ID2D1Bitmap_CopyFromMemory(sys->d2_bitmap,
                                                NULL /*&r_src*/,
                                                picture->p[0].p_pixels,
                                                picture->p[0].i_pitch);
        if (hr != S_OK)
            msg_Err(vd, "Failed to copy bitmap memory (hr = 0x%x)!",
                    (unsigned)hr);

#ifndef NDEBUG
        /*msg_Dbg(vd, "Bitmap dbg: target = %p, pitch = %d, bitmap = %p",
224
                (void *)sys->d2_render_target, pitch, (void *)sys->d2_bitmap);*/
David Kaplan's avatar
David Kaplan committed
225 226
#endif
    }
227
    VLC_UNUSED(subpicture);
David Kaplan's avatar
David Kaplan committed
228 229 230 231 232
}

/**
 * Blits a scaled picture_t to the render target
 */
233
static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
David Kaplan's avatar
David Kaplan committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
{
    vout_display_sys_t *sys = vd->sys;

    D2D1_RECT_F r_dest = {
        sys->rect_dest.left,
        sys->rect_dest.top,
        sys->rect_dest.right,
        sys->rect_dest.bottom
    };

    if (sys->d2_render_target && sys->d2_bitmap) {
        ID2D1HwndRenderTarget_BeginDraw(sys->d2_render_target);

        ID2D1HwndRenderTarget_DrawBitmap(sys->d2_render_target,
                                         sys->d2_bitmap,
                                         &r_dest,
                                         1.0f,
                                         D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
                                         NULL);

        HRESULT hr = ID2D1HwndRenderTarget_EndDraw(sys->d2_render_target,
                                                   NULL,
                                                   NULL);
257
        if (hr == D2DERR_RECREATE_TARGET) {
David Kaplan's avatar
David Kaplan committed
258 259 260 261 262 263
            D2D_DestroyRenderTarget(vd);
            D2D_CreateRenderTarget(vd);
        }
    }

    picture_Release(picture);
264
    VLC_UNUSED(subpicture);
David Kaplan's avatar
David Kaplan committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

    CommonDisplay(vd);
}

 /**
  * Control event handler
  */
static int Control(vout_display_t *vd, int query, va_list args)
{
    return CommonControl(vd, query, args);
}

/**
 * Handles surface management
 * ID2D1RenderTargets cannot be resized and must be recreated
 */
static void Manage(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    CommonManage(vd);

    if (sys->changes & DX_POSITION_CHANGE) {
        D2D_ResizeRenderTarget(vd);
        sys->changes &= ~DX_POSITION_CHANGE;
    }
}

/**
 * Creates a ID2D1HwndRenderTarget and associated ID2D1Bitmap
 */
static int D2D_CreateRenderTarget(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    sys->d2_render_target = NULL;

    D2D1_PIXEL_FORMAT pf = {
        DXGI_FORMAT_B8G8R8A8_UNORM,
        D2D1_ALPHA_MODE_IGNORE
    };

    D2D1_RENDER_TARGET_PROPERTIES rtp = {
        D2D1_RENDER_TARGET_TYPE_DEFAULT,
        pf,
        0,
        0,
        D2D1_RENDER_TARGET_USAGE_NONE,
        D2D1_FEATURE_LEVEL_DEFAULT
    };

    D2D1_SIZE_U size = {
        sys->rect_dest.right - sys->rect_dest.left,
        sys->rect_dest.bottom - sys->rect_dest.top
    };

    D2D1_HWND_RENDER_TARGET_PROPERTIES hrtp = {
        sys->hvideownd,
        size,
        D2D1_PRESENT_OPTIONS_IMMEDIATELY /* this might need fiddling */
    };

    HRESULT hr  = ID2D1Factory_CreateHwndRenderTarget(sys->d2_factory,
                                                      &rtp,
                                                      &hrtp,
                                                      &sys->d2_render_target);
    if (hr != S_OK) {
        msg_Err(vd, "Cannot create render target (hvideownd = 0x%x, width = %d, height = %d, pf.format = %d, hr = 0x%x)!",
                (unsigned)hrtp.hwnd, hrtp.pixelSize.width,
                hrtp.pixelSize.height, pf.format, (unsigned)hr);

        sys->d2_render_target = NULL;

        return VLC_EGENERIC;
    }

    FLOAT dpi_x, dpi_y;

    ID2D1Factory_GetDesktopDpi(sys->d2_factory,
                               &dpi_x,
                               &dpi_y);

    D2D1_BITMAP_PROPERTIES bp = {
        pf,
        dpi_x,
        dpi_y
    };

    D2D1_SIZE_U bitmap_size = {
354 355
        vd->fmt.i_visible_width,
        vd->fmt.i_visible_height
David Kaplan's avatar
David Kaplan committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    };

    hr = ID2D1HwndRenderTarget_CreateBitmap(sys->d2_render_target,
                                            bitmap_size,
                                            NULL,
                                            0,
                                            &bp,
                                            &sys->d2_bitmap);
    if (hr != S_OK) {
        msg_Err(vd, "Failed to create bitmap (hr = 0x%x)!", (unsigned)hr);

        sys->d2_bitmap = NULL;
        D2D_DestroyRenderTarget(vd);

        return VLC_EGENERIC;
    }

#ifndef NDEBUG
    msg_Dbg(vd, "Render trgt dbg: dpi = %f, render_target = %p, bitmap = %p",
375
            dpi_x, (void *)sys->d2_render_target, (void *)sys->d2_bitmap);
David Kaplan's avatar
David Kaplan committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
#endif

    return VLC_SUCCESS;
}

/**
 * Resizes a ID2D1HWndRenderTarget
 */
static void D2D_ResizeRenderTarget(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    D2D1_SIZE_U size = {
        sys->rect_dest.right - sys->rect_dest.left,
        sys->rect_dest.bottom - sys->rect_dest.top
    };

    HRESULT hr  = ID2D1HwndRenderTarget_Resize(sys->d2_render_target, &size);
    if (hr != S_OK)
        msg_Err(vd, "Cannot resize render target (width = %d, height = %d, hr = 0x%x)!",
                size.width, size.height, (unsigned)hr);
}

/**
 * Cleans up ID2D1HwndRenderTarget and ID2D1Bitmap
 */
static void D2D_DestroyRenderTarget(vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    if (sys->d2_render_target) {
        ID2D1HwndRenderTarget_Release(sys->d2_render_target);
        sys->d2_render_target = NULL;
    }

    if (sys->d2_bitmap) {
        ID2D1Bitmap_Release(sys->d2_bitmap);
        sys->d2_bitmap = NULL;
    }

#ifndef NDEBUG
    msg_Dbg(vd, "Destroyed");
#endif
}