顯示具有 DirectDraw 標籤的文章。 顯示所有文章
顯示具有 DirectDraw 標籤的文章。 顯示所有文章

2012年9月10日 星期一

[DirectDraw] Notes

參考資料: MSDN DirectDraw

定義

DirectDraw為一個特殊的記憶體管理員,提供 2-D存取 Windows Graphics Device Interface (GDI)的硬體加速, 個人覺得定義最好的是下面這段
DirectDraw is a low-level application programming interface (API) for graphics. It controls memory, the hardware blitter, and surface flipping operations. DirectDraw is a software interface that provides direct access to display devices while maintaining compatibility with the Windows Embedded CE graphics device interface (GDI).
何時該用 DirectDraw? 主要是以效能考量的時候, 為了提升 GDI速度的不足。
DirectDraw主要任務:
  1. 直接存取影像記憶體(video meory), 而不是系統記憶體(system memory)
  2. Page flipping
  3. Back buffering
  4. Clipping
相關專有名詞可參考 Graphics Concept. 架構圖如下:

介面

  1. Graphics Devices
    1. 定義: The device context is a structure containing information about the device.
  2. DirectDraw Objects: IDirectDraw
    1. 互動介面: Cooperative Level
    2. 一個 DirectDraw application可區分為兩類, 一種是視窗式 (windowed), 另一種為全螢幕式 (full-screen或 exclusive), 最大的差別在前者無法實現 page flipping, 後者則能避免其他應用程式存取 surfaces與畫圖於 primary display之上。
    3. Display Mode
      1. Primary Surfaces給 monitor的硬體設定值
      2. 主要為 dimension與 depth, e.g. 800*600 pixels and 8-bit/pixel
      3. 分為兩種: palettized與 non-palettized, 調色盤每個顏色是對應到一個 index value
      4. 重要 WinCE特性:
        1. DirectDraw cannot change the display mode.
      5. 呼叫  IDirectDraw::EnumDisplayModes 可查看硬體支援的 Display mode
    4. Singleton for process
      1. 對於每個 process, directdraw 是 singleton的, 若已經存在 instance, 則 call DirectDrawCreate 仍會回原本那個 instance.
  3. DirectDraw Surfaces: IDirectDrawSurface
    1. 定義: The DirectDrawSurface object represents a surface that usually resides in the display memory, but can exist in system memory if display memory is exhausted or if it is explicitly requested.
    2. 所有 surfaces都由 DirectDraw object來 create
    3. Blt
      1. 從 source 的 surface貼到 destination
      2. Blt會自動 scale且支援內插 (interpolation)
  4. DirectDraw Clippers: IDirectDrawClipper

程式

初始化程式, 其實就宣告 DirectDraw, DirectDraw的 Surface而已, 注意 Caps代表的是 capabilities, 也就是此實作 HAL (Hardware Abstract Layer)的 chip廠提供哪些 DirectDraw的功能。

#include <ddraw.h>

HRESULT hr;
IDirectDraw * pDD = NULL;
IDirectDrawSurface * pDDSPrimary = NULL;
DDSURFACEDESC ddsd;

hr = DirectDrawCreate(NULL, &pDD, NULL);
hr = pDD->SetCooperativeLevel(hwnd, DDSCL_FULLSCREEN);
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = pDD->CreateSurface(&ddsd, &pDDSPrimary, NULL);

細節:

  1. HEL (Hardware Emulation Layer) 僅提供 8, 16, 24, 32 bpp(bit per pixel)的 Surfaces。為了避免混用 HAL, HEL反而使效能降低,建議若有使用到硬體不支援的功能時,另外建立 system memory的 Surfaces來實作 (參考: Software Emulation)

2012年9月9日 星期日

[Graphic] Graphics Concepts

Blitting

Bit Block Transfer的縮寫。簡單說,blitting就是記憶體 copy的動作。把某塊 copy到另一塊去。

Flipping

flipping 在圖學的角度, 指的是畫面中部分簡單的變換, 藉由翻頁(個人覺得翻頁比翻轉好理解)來達成. 請想像漫畫快速翻頁形成動畫的效果。而 flipping就是這動作。由於翻頁的需求,常常會有 back buffer的機制,也就是準備好兩塊頁面, 在這兩塊間切換以形成動畫。

可參考 MSDN DirectDraw附的 Graphics Concepts, 其中還有 DIB, Surfaces與 rounding rectangle