Skip to content

Commit

Permalink
simplify sample
Browse files Browse the repository at this point in the history
  • Loading branch information
robmikh committed Aug 28, 2020
1 parent d302e8b commit a243c6a
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 78 deletions.
16 changes: 3 additions & 13 deletions Win32CaptureSample/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ winrt::IAsyncOperation<winrt::GraphicsCaptureItem> App::StartCaptureWithPickerAs

winrt::IAsyncOperation<winrt::StorageFile> App::TakeSnapshotAsync()
{
// First, get a GraphicsCaptureItem. Here we're using the picker, but
// this would also work for any other type of GraphicsCaptureItem.
auto item = co_await m_capturePicker.PickSingleItemAsync();
if (item == nullptr)
// Use what we're currently capturing
if (m_capture == nullptr)
{
// The user decided not to capture anything.
co_return nullptr;
}
auto item = m_capture->CaptureItem();

// Ask the user where they want to save the snapshot.
m_savePicker.SuggestedStartLocation(winrt::PickerLocationId::PicturesLibrary);
Expand Down Expand Up @@ -153,14 +151,6 @@ winrt::IAsyncOperation<winrt::StorageFile> App::TakeSnapshotAsync()
co_return file;
}

void App::SnapshotCurrentCapture()
{
if (m_capture)
{
m_capture->SaveNextFrame();
}
}

void App::StartCaptureFromItem(winrt::GraphicsCaptureItem item)
{
m_capture = std::make_unique<SimpleCapture>(m_device, item, m_pixelFormat);
Expand Down
1 change: 0 additions & 1 deletion Win32CaptureSample/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class App
winrt::Windows::Graphics::Capture::GraphicsCaptureItem StartCaptureFromMonitorHandle(HMONITOR hmon);
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Graphics::Capture::GraphicsCaptureItem> StartCaptureWithPickerAsync();
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> TakeSnapshotAsync();
void SnapshotCurrentCapture();
winrt::Windows::Graphics::DirectX::DirectXPixelFormat PixelFormat() { return m_pixelFormat; }
void PixelFormat(winrt::Windows::Graphics::DirectX::DirectXPixelFormat pixelFormat);

Expand Down
27 changes: 10 additions & 17 deletions Win32CaptureSample/SampleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ LRESULT SampleWindow::MessageHandler(UINT const message, WPARAM const wparam, LP
SetSubTitle(std::wstring(item.DisplayName()));
SendMessageW(m_monitorComboBoxHwnd, CB_SETCURSEL, -1, 0);
SendMessageW(m_cursorCheckBoxHwnd, BM_SETCHECK, BST_CHECKED, 0);
EnableWindow(m_snapshotButtonHwnd, true);
}
else if (hwnd == m_monitorComboBoxHwnd)
{
Expand All @@ -96,6 +97,7 @@ LRESULT SampleWindow::MessageHandler(UINT const message, WPARAM const wparam, LP
SetSubTitle(std::wstring(item.DisplayName()));
SendMessageW(m_windowComboBoxHwnd, CB_SETCURSEL, -1, 0);
SendMessageW(m_cursorCheckBoxHwnd, BM_SETCHECK, BST_CHECKED, 0);
EnableWindow(m_snapshotButtonHwnd, true);
}
else if (hwnd == m_pixelFormatComboBoxHwnd)
{
Expand All @@ -114,10 +116,6 @@ LRESULT SampleWindow::MessageHandler(UINT const message, WPARAM const wparam, LP
{
StopCapture();
}
else if (hwnd == m_currentSnapshotHwnd)
{
m_app->SnapshotCurrentCapture();
}
else if (hwnd == m_snapshotButtonHwnd)
{
OnSnapshotButtonClicked();
Expand Down Expand Up @@ -169,6 +167,7 @@ winrt::fire_and_forget SampleWindow::OnPickerButtonClicked()
SendMessageW(m_monitorComboBoxHwnd, CB_SETCURSEL, -1, 0);
SendMessageW(m_windowComboBoxHwnd, CB_SETCURSEL, -1, 0);
SendMessageW(m_cursorCheckBoxHwnd, BM_SETCHECK, BST_CHECKED, 0);
EnableWindow(m_snapshotButtonHwnd, true);
}
}

Expand Down Expand Up @@ -223,22 +222,16 @@ void SampleWindow::CreateControls(HINSTANCE instance)
10, 120, 200, 30, m_window, nullptr, instance, nullptr);
WINRT_VERIFY(stopButtonHwnd);

// Create current snapshot button
HWND currentSnapshotButtonHwnd = CreateWindowW(WC_BUTTON, L"Snapshot Current Capture",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 160, 200, 30, m_window, nullptr, instance, nullptr);
WINRT_VERIFY(currentSnapshotButtonHwnd);

// Create independent snapshot button
HWND snapshotButtonHwnd = CreateWindowW(WC_BUTTON, L"Take Independent Snapshot",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 200, 200, 30, m_window, nullptr, instance, nullptr);
HWND snapshotButtonHwnd = CreateWindowW(WC_BUTTON, L"Take Snapshot",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_DISABLED,
10, 160, 200, 30, m_window, nullptr, instance, nullptr);
WINRT_VERIFY(snapshotButtonHwnd);

// Create pixel format combo box
HWND pixelFormatComboBox = CreateWindowW(WC_COMBOBOX, L"",
CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_VSCROLL | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
10, 240, 200, 200, m_window, nullptr, instance, nullptr);
10, 200, 200, 200, m_window, nullptr, instance, nullptr);
WINRT_VERIFY(pixelFormatComboBox);

// Populate pixel format combo box
Expand All @@ -253,7 +246,7 @@ void SampleWindow::CreateControls(HINSTANCE instance)
// Create cursor checkbox
HWND cursorCheckBoxHwnd = CreateWindowW(WC_BUTTON, L"Enable Cursor",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX | cursorEnableStyle,
10, 280, 200, 30, m_window, nullptr, instance, nullptr);
10, 240, 200, 30, m_window, nullptr, instance, nullptr);
WINRT_VERIFY(cursorCheckBoxHwnd);

// The default state is true for cursor rendering
Expand All @@ -262,7 +255,7 @@ void SampleWindow::CreateControls(HINSTANCE instance)
// Create capture exclude checkbox
HWND captureExcludeCheckBoxHwnd = CreateWindowW(WC_BUTTON, L"Exclude this window",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX | win32CaptureExcludeStyle,
10, 320, 200, 30, m_window, nullptr, instance, nullptr);
10, 280, 200, 30, m_window, nullptr, instance, nullptr);
WINRT_VERIFY(captureExcludeCheckBoxHwnd);

// The default state is false for capture exclusion
Expand All @@ -272,7 +265,6 @@ void SampleWindow::CreateControls(HINSTANCE instance)
m_monitorComboBoxHwnd = monitorComboBoxHwnd;
m_pickerButtonHwnd = pickerButtonHwnd;
m_stopButtonHwnd = stopButtonHwnd;
m_currentSnapshotHwnd = currentSnapshotButtonHwnd;
m_snapshotButtonHwnd = snapshotButtonHwnd;
m_cursorCheckBoxHwnd = cursorCheckBoxHwnd;
m_captureExcludeCheckBoxHwnd = captureExcludeCheckBoxHwnd;
Expand All @@ -296,6 +288,7 @@ void SampleWindow::StopCapture()
SendMessageW(m_monitorComboBoxHwnd, CB_SETCURSEL, -1, 0);
SendMessageW(m_windowComboBoxHwnd, CB_SETCURSEL, -1, 0);
SendMessageW(m_cursorCheckBoxHwnd, BM_SETCHECK, BST_CHECKED, 0);
EnableWindow(m_snapshotButtonHwnd, false);
}

void SampleWindow::OnCaptureItemClosed(winrt::GraphicsCaptureItem const&, winrt::Windows::Foundation::IInspectable const&)
Expand Down
1 change: 0 additions & 1 deletion Win32CaptureSample/SampleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ struct SampleWindow : util::desktop::DesktopWindow<SampleWindow>
HWND m_monitorComboBoxHwnd = nullptr;
HWND m_pickerButtonHwnd = nullptr;
HWND m_stopButtonHwnd = nullptr;
HWND m_currentSnapshotHwnd = nullptr;
HWND m_snapshotButtonHwnd = nullptr;
HWND m_pixelFormatComboBoxHwnd = nullptr;
HWND m_cursorCheckBoxHwnd = nullptr;
Expand Down
25 changes: 0 additions & 25 deletions Win32CaptureSample/SimpleCapture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "pch.h"
#include "SimpleCapture.h"
#include <DirectXTex.h>

namespace winrt
{
Expand Down Expand Up @@ -100,12 +99,6 @@ void SimpleCapture::OnFrameArrived(winrt::Direct3D11CaptureFramePool const& send
auto surfaceTexture = GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());
// copy surfaceTexture to backBuffer
m_d3dContext->CopyResource(backBuffer.get(), surfaceTexture.get());

if (m_captureNextImage)
{
m_captureNextImage = false;
TakeSnapshot(surfaceTexture);
}
}

DXGI_PRESENT_PARAMETERS presentParameters{};
Expand All @@ -116,21 +109,3 @@ void SimpleCapture::OnFrameArrived(winrt::Direct3D11CaptureFramePool const& send
m_framePool.Recreate(m_device, m_pixelFormat, 2, m_lastSize);
}
}

void SimpleCapture::TakeSnapshot(winrt::com_ptr<ID3D11Texture2D> const& frame)
{
DirectX::ScratchImage im;
winrt::check_hresult(DirectX::CaptureTexture(GetDXGIInterfaceFromObject<ID3D11Device>(m_device).get(),
m_d3dContext.get(), frame.get(), im));
const auto& realImage = *im.GetImage(0, 0, 0);
if (m_pixelFormat == winrt::DirectXPixelFormat::R16G16B16A16Float)
{
winrt::check_hresult(DirectX::SaveToWICFile(realImage, DirectX::WIC_FLAGS_NONE,
GUID_ContainerFormatWmp, L"output.jxr"));
}
else // BGRA8
{
winrt::check_hresult(DirectX::SaveToWICFile(realImage, DirectX::WIC_FLAGS_NONE,
GUID_ContainerFormatPng, L"output.png"));
}
}
6 changes: 2 additions & 4 deletions Win32CaptureSample/SimpleCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class SimpleCapture
winrt::Windows::UI::Composition::ICompositionSurface CreateSurface(
winrt::Windows::UI::Composition::Compositor const& compositor);

void SaveNextFrame() { m_captureNextImage = true; }
bool IsCursorEnabled() { CheckClosed(); return m_session.IsCursorCaptureEnabled(); }
void IsCursorEnabled(bool value) { CheckClosed(); m_session.IsCursorCaptureEnabled(value); }
bool IsCursorEnabled() { CheckClosed(); return m_session.IsCursorCaptureEnabled(); }
void IsCursorEnabled(bool value) { CheckClosed(); m_session.IsCursorCaptureEnabled(value); }
winrt::Windows::Graphics::Capture::GraphicsCaptureItem CaptureItem() { return m_item; }

void Close();
Expand All @@ -24,7 +23,6 @@ class SimpleCapture
void OnFrameArrived(
winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const& sender,
winrt::Windows::Foundation::IInspectable const& args);
void TakeSnapshot(winrt::com_ptr<ID3D11Texture2D> const& frame);

inline void CheckClosed()
{
Expand Down
23 changes: 12 additions & 11 deletions Win32CaptureSample/Win32CaptureSample.vcxproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.props')" />
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" />
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|ARM64">
<Configuration>Debug</Configuration>
Expand Down Expand Up @@ -106,6 +106,7 @@
<ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -120,6 +121,7 @@
<ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -134,6 +136,7 @@
<ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -150,7 +153,7 @@
<ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand All @@ -169,7 +172,7 @@
<ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand All @@ -188,7 +191,7 @@
<ConformanceMode>false</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down Expand Up @@ -234,17 +237,15 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\directxtex_desktop_win10.2020.2.15.1\build\native\directxtex_desktop_win10.targets" Condition="Exists('..\packages\directxtex_desktop_win10.2020.2.15.1\build\native\directxtex_desktop_win10.targets')" />
<Import Project="..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="..\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\directxtex_desktop_win10.2020.2.15.1\build\native\directxtex_desktop_win10.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\directxtex_desktop_win10.2020.2.15.1\build\native\directxtex_desktop_win10.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.200224.2\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
</Target>
</Project>
6 changes: 3 additions & 3 deletions Win32CaptureSample/Win32CaptureSample.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Filter Include="Util">
<UniqueIdentifier>{bd52fd2f-19e3-47c1-afb1-969ba28603f5}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
5 changes: 2 additions & 3 deletions Win32CaptureSample/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="directxtex_desktop_win10" version="2020.2.15.1" targetFramework="native" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.200224.2" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.191107.2" targetFramework="native" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.200729.8" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.200519.2" targetFramework="native" />
</packages>

0 comments on commit a243c6a

Please sign in to comment.