Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-77265

UWP: Consider switching port to cppwinrt

    XMLWordPrintable

Details

    • Task
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.12, 5.13
    • QPA
    • None
    • UWP

    • WinRT

    Description

      What’s cppwinrt

      Cppwinrt is a modern C++ implementation of the UWP platform APIs. Basically, it is a wrapper around the COM based SDKs by Microsoft.

      More information can be found here:

      https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/index

      https://github.com/microsoft/cppwinrt

      https://github.com/Microsoft/cppwinrt/blob/master/Docs/Migrating%20C%2B%2B%20CX%20source%20code%20to%20C%2B%2B%20WinRT.md

      https://kennykerr.ca

       

      Furthermore, since a couple of years, the cppwinrt headers are part of the Windows platform SDKs. That way Qt can use them without any external dependency. It raises the minimum SDK for the port though.

       

      What’s the motivation

      Using COM is error-prone and required many lines of code. Cppwinrt promises to remove this burden and provide a new, modern approach. As an example from the Qt perspective:

       

      Old code (using COM):

      ComPtr<IDataPackageView> view;{{    }}

      HRESULT hr;

      hr = m_nativeClipBoard->GetContent(&view);

      RETURN_NULLPTR_IF_FAILED("Could not get clipboard content."); {{    }}

      ComPtr<IAsyncOperation<HSTRING>> op;{{    }}

      HString result;{{    }}

      hr = view->GetTextAsync(&op);{{    }}

      RETURN_NULLPTR_IF_FAILED("Could not get clipboard text."); {{    }}

      hr = QWinRTFunctions::await(op, result.GetAddressOf());{{    }}

      RETURN_NULLPTR_IF_FAILED("Could not get clipboard text content"); {{    }}

      quint32 size;{{    }}

      const wchar_t *textStr = result.GetRawBuffer(&size);{{    }}

      QString text = QString::fromWCharArray(textStr, int(size));

       

      New code (using cppwinrt):

      auto view = winrt::Clipboard::GetContent();{{    }}

      auto action = view.GetTextAsync();{{    }}

      QWinRTFunctions::cppwinrtAwait(action);{{    }}

      winrt::hstring htext = action.GetResults(); {{    }}

      QString text = QString::fromWCharArray(htext.c_str(), int(htext.size()));

       

      Current Progress

      WIP-Changes can be found here:

      https://codereview.qt-project.org/c/qt/qtbase/+/264710/4

      https://codereview.qt-project.org/c/qt/qtbase/+/264709/4

      https://codereview.qt-project.org/c/qt/qtbase/+/264242/7

      https://codereview.qt-project.org/c/qt/qtbase/+/264790/4

       

      Outcome and impression

      Generally, using cppwinrt makes development much easier and modern, as can be seen in above example.

      Switching the event loop to a cppwinrt-approach resulted in getting rid of one always-busy thread, which was constantly polling for events. However, a similar approach should also be doable with the previous implementation.

      Contrary to the blog posts by the author of cppwinrt we did not recognize any performance boost. This might be related to the fact that the header-only implementation wraps around COM.

      However, we have not been using the parts, which are most interesting for other third-party developers. Using coroutines, it is possible to switch context within a function from the UI to a worker thread and vice-versa. Those might have a performance impact.

      Additionally, Qt/WinRT contains a lot of await() calls, which manually block the calling thread. With cppwinrt, we might use coroutines and have the system take care of the waiting, utilizing whatever it provides to improve on the situation.

      This will cause a bigger change, as callees from Qt side would need to change their signature and behavior. (### Still needs to be tested).

       

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              ablasche Alex Blasche
              mkalinow Maurice Kalinowski
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes