//
// ddraw_render.h
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2011 Jack (jack.wgm@gmail.com)
//

#ifndef __DDRAW_RENDER_H__
#define __DDRAW_RENDER_H__

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

#include "video_render.h"

typedef struct directx_fourcc
{
	char*  img_format_name;      // human readable name
	uint32_t img_format;         // as MPlayer image format
	uint32_t drv_caps;           // what hw supports with this format
	DDPIXELFORMAT ddpf_overlay;  // as Directx Sourface description
} directx_fourcc;

extern directx_fourcc dx_fourcc[];
#define DDPF_NUM_FORMATS (sizeof(dx_fourcc) / sizeof(dx_fourcc[0]))

class ddraw_render
	: public video_render
{
public:
	ddraw_render();
	virtual ~ddraw_render();

public:
	// 鍒濆render.
	virtual bool init_render(void* ctx, int w, int h, int pix_fmt);

	// 娓叉煋涓€甯?
	virtual bool render_one_frame(AVFrame* data, int pix_fmt);

	// 璋冩暣澶у皬.
	virtual void re_size(int width, int height);

	// 璁剧疆瀹介珮姣?
	virtual void aspect_ratio(int srcw, int srch, bool enable_aspect);

	// 鏄惁浣跨敤overlay妯″紡娓叉煋.
	virtual bool use_overlay() { return m_support_double_buffer; }

	// 鎾ら攢render.
	virtual void destory_render();

private:
	void check_overlay();
	bool create_overlay_face(uint32_t fmt, bool must_overlay);
	bool manage_display();
	void* copy_yuv_pic(void * dst, const void * src, int bytesPerLine, int height, int dstStride, int srcStride);
	bool draw_slice(uint8_t* dst, uint8_t* src[], int stride[], int dstStride, int w, int h, int x, int y);

private:
	// ddraw鎸囬拡.
	LPDIRECTDRAW7 m_ddraw;

	// 涓昏〃灞?
	LPDIRECTDRAWSURFACE7 m_main_face;

	// 绂诲睆琛ㄥ眰.
	LPDIRECTDRAWSURFACE7 m_back_face;

	// overlay琛ㄥ眰.
	LPDIRECTDRAWSURFACE7 m_overlay_face;

	// 瑁佸壀鍣?
	LPDIRECTDRAWCLIPPER m_clipper;

	// DDRAW鏄惁鏀寔overlay.
	bool m_support_overlay;

	// DDRAW鏄惁鏀寔doublebuffer.
	bool m_support_double_buffer;

	// DDRAW鏄惁鏀寔fourcc.
	bool m_can_blit_fourcc;

	// colorkey for our surface.
	DWORD m_destcolorkey;

	// windowcolor == colorkey
	DWORD m_windowcolor;

	// 杞崲棰滆壊涓婁笅鏂囨寚閽?
	SwsContext* m_swsctx;

	// 杞崲棰滆壊buffer.
	uint8_t* m_swsbuffer;

	// 褰撳墠琛ㄥ眰鍍忕礌鏍煎紡.
	DWORD m_pix_fmt;

	// 瑙嗛鏁版嵁鍍忕礌鏍煎紡.
	int m_video_fmt;

	// 褰撳墠鏄剧ず鍣ㄧ殑鍖哄煙.
	RECT m_monitor_rect;

	// 褰撳墠鍖哄煙.
	RECT m_last_rect;

	// 淇濇寔瀹介珮.
	bool m_keep_aspect;

	// 瀹介珮姣?
	float m_window_aspect;

	// 瀹?
	int m_image_width;

	// 楂?
	int m_image_height;

	// 绐楀彛鍙ユ焺.
	HWND m_hWnd;
};

#endif // __DDRAW_RENDER_H__