[Spice-devel] [PATCH 12/14] Use a single class for every escape command

Frediano Ziglio fziglio at redhat.com
Thu Sep 1 12:00:54 UTC 2016


Do not use two classes one for XPDM and the other for WDDM.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 vdagent/display_configuration.cpp | 76 +++++++++++++++++----------------------
 vdagent/display_configuration.h   | 17 +++++++++
 2 files changed, 49 insertions(+), 44 deletions(-)

diff --git a/vdagent/display_configuration.cpp b/vdagent/display_configuration.cpp
index 301e136..2387073 100644
--- a/vdagent/display_configuration.cpp
+++ b/vdagent/display_configuration.cpp
@@ -204,48 +204,40 @@ struct D3DKMT_OPENADAPTERFROMGDIDISPLAYNAME {
     UINT VidPnSourceId;
 };
 
-struct QXLMonitorEscape {
-    QXLMonitorEscape(DEVMODE* dev_mode)
-    {
-        ZeroMemory(&_head, sizeof(_head));
-        _head.x = dev_mode->dmPosition.x;
-        _head.y = dev_mode->dmPosition.y;
-        _head.width = dev_mode->dmPelsWidth;
-        _head.height = dev_mode->dmPelsHeight;
-    }
-    QXLHead _head;
-};
+template <typename Content>
+static inline int ExtEscape(HDC hdc, EscapeData<Content>& data)
+{
+    return ExtEscape(hdc, data._ioctl, sizeof(data._data), (LPCSTR) &data._data, 0, NULL);
+}
 
-struct QxlCustomEscapeObj : public QXLEscapeSetCustomDisplay {
-    QxlCustomEscapeObj(uint32_t bitsPerPel, uint32_t width, uint32_t height)
+struct QxlMonitorEscape: public EscapeData<QXLHead> {
+    QxlMonitorEscape(const DEVMODE& dev_mode):
+        EscapeData<QXLHead>(QXL_ESCAPE_MONITOR_CONFIG)
     {
-        xres = width;
-        yres = height;
-        bpp = bitsPerPel;
+        _data.x = dev_mode.dmPosition.x;
+        _data.y = dev_mode.dmPosition.y;
+        _data.width = dev_mode.dmPelsWidth;
+        _data.height = dev_mode.dmPelsHeight;
     }
-};
-
-struct WDDMCustomDisplayEscape {
-    WDDMCustomDisplayEscape(DEVMODE* dev_mode)
+    QxlMonitorEscape(const DisplayMode& mode):
+        EscapeData<QXLHead>(QXL_ESCAPE_MONITOR_CONFIG)
     {
-        _ioctl = QXL_ESCAPE_SET_CUSTOM_DISPLAY;
-        _custom.bpp = dev_mode->dmBitsPerPel;
-        _custom.xres = dev_mode->dmPelsWidth;
-        _custom.yres = dev_mode->dmPelsHeight;
+        _data.x = mode.get_pos_x();
+        _data.y = mode.get_pos_y();
+        _data.width = mode.get_width();
+        _data.height = mode.get_height();
     }
     uint32_t                    _ioctl;
     QXLEscapeSetCustomDisplay   _custom;
 };
 
-struct WDDMMonitorConfigEscape {
-    WDDMMonitorConfigEscape(DisplayMode* mode)
+struct QxlCustomEscape : public EscapeData<QXLEscapeSetCustomDisplay> {
+    QxlCustomEscape(const DEVMODE& dev_mode):
+        EscapeData<QXLEscapeSetCustomDisplay>(QXL_ESCAPE_SET_CUSTOM_DISPLAY)
     {
-        _ioctl = QXL_ESCAPE_MONITOR_CONFIG;
-        _head.id = _head.surface_id = 0;
-        _head.x = mode->get_pos_x();
-        _head.y = mode->get_pos_y();
-        _head.width = mode->get_width();
-        _head.height = mode->get_height();
+        _data.xres = dev_mode.dmPelsWidth;
+        _data.yres = dev_mode.dmPelsHeight;
+        _data.bpp = dev_mode.dmBitsPerPel;
     }
     uint32_t    _ioctl;
     QXLHead     _head;
@@ -331,11 +323,8 @@ bool XPDMInterface::custom_display_escape(LPCTSTR device_name, DEVMODE* dev_mode
         }
     }
 
-    QxlCustomEscapeObj custom_escape(dev_mode->dmBitsPerPel,
-                                            dev_mode->dmPelsWidth, dev_mode->dmPelsHeight);
-
-    int err = ExtEscape(hdc, QXL_ESCAPE_SET_CUSTOM_DISPLAY,
-              sizeof(QXLEscapeSetCustomDisplay), (LPCSTR) &custom_escape, 0, NULL);
+    QxlCustomEscape custom_escape(*dev_mode);
+    int err = ExtEscape(hdc, custom_escape);
     if (err <= 0) {
         vd_printf("%s: Can't set custom display, perhaps running with an older driver?",
             __FUNCTION__);
@@ -356,7 +345,7 @@ bool XPDMInterface::update_monitor_config(LPCTSTR device_name, DisplayMode* mode
         return false;
     }
 
-    QXLMonitorEscape monitor_config(dev_mode);
+    QxlMonitorEscape monitor_config(*dev_mode);
     HDC hdc(CreateDC(device_name, NULL, NULL, NULL));
     int err(0);
 
@@ -364,8 +353,7 @@ bool XPDMInterface::update_monitor_config(LPCTSTR device_name, DisplayMode* mode
         return false;
     }
 
-    err = ExtEscape(hdc, QXL_ESCAPE_MONITOR_CONFIG, sizeof(QXLHead),
-                    (LPCSTR) &monitor_config, 0, NULL);
+    err = ExtEscape(hdc, monitor_config);
     if (err < 0) {
         vd_printf("%s: %S can't update monitor config, may have old, old driver",
                  __FUNCTION__, device_name);
@@ -459,8 +447,8 @@ bool WDDMInterface::custom_display_escape(LPCTSTR device_name, DEVMODE* dev_mode
 
     vd_printf("updating %S resolution", device_name);
 
-    WDDMCustomDisplayEscape wddm_escape(dev_mode);
-    if (escape(device_name, &wddm_escape, sizeof(wddm_escape))) {
+    QxlCustomEscape custom_escape(*dev_mode);
+    if (escape(device_name, custom_escape)) {
         return _ccd.update_mode_size(device_name, dev_mode);
     }
 
@@ -478,8 +466,8 @@ bool WDDMInterface::update_monitor_config(LPCTSTR device_name, DisplayMode* disp
     if (!mode || !_send_monitors_config)
         return false;
 
-    WDDMMonitorConfigEscape wddm_escape(display_mode);
-    if (escape(device_name, &wddm_escape, sizeof(wddm_escape))) {
+    QxlMonitorEscape monitor_escape(*display_mode);
+    if (escape(device_name, monitor_escape)) {
         //Update the path position
         return _ccd.update_mode_position(device_name, dev_mode);
     }
diff --git a/vdagent/display_configuration.h b/vdagent/display_configuration.h
index ef4f989..454c45d 100644
--- a/vdagent/display_configuration.h
+++ b/vdagent/display_configuration.h
@@ -89,6 +89,18 @@ private:
     PATH_STATE _path_state;
 };
 
+template <typename Content>
+struct EscapeData {
+    int _ioctl;
+    Content _data;
+    EscapeData(int ioctl):_ioctl(ioctl)
+    {
+        static_assert(sizeof(*this) == sizeof(int) + sizeof(Content),
+                      "structure not correctly packed");
+        ZeroMemory(&_data, sizeof(_data));
+    }
+};
+
 class DisplayMode;
 
 //Class provides interface to get/set display configurations
@@ -160,6 +172,11 @@ private:
 
     void close_adapter(D3D_HANDLE handle);
     bool escape(LPCTSTR device_name, void* data, UINT sizeData);
+    template <typename Content>
+    bool escape(LPCTSTR device_name, EscapeData<Content>& data)
+    {
+        return escape(device_name, &data, sizeof(data));
+    }
 
     //GDI Function pointers
     PFND3DKMT_OPENADAPTERFROMHDC _pfnOpen_adapter_hdc;
-- 
2.7.4



More information about the Spice-devel mailing list