Skip to content

Commit 6f843d8

Browse files
trying to add a continuiation task for evalasync
1 parent 3adf348 commit 6f843d8

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#include "pch.h"
22
#include "StyleTransferEffect.h"
33
#include "StyleTransferEffect.g.cpp"
4+
#include <ppltasks.h>
5+
46
using namespace std;
57
using namespace winrt::Windows::Storage;
68
using namespace winrt::Windows::Storage::Streams;
9+
using namespace concurrency;
710

811
namespace winrt::StyleTransferEffectCpp::implementation
912
{
1013
StyleTransferEffect::StyleTransferEffect() :
1114
outputTransformed(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
12-
inputTransformed(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
13-
copyBounds(Windows::Graphics::Imaging::BitmapBounds{ 0, 0, 640, 360 }),
15+
cachedOutput(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
1416
Session(nullptr),
1517
Binding(nullptr)
1618
{
19+
1720
}
1821

1922
IVectorView<VideoEncodingProperties> StyleTransferEffect::SupportedEncodingProperties() {
@@ -38,13 +41,25 @@ namespace winrt::StyleTransferEffectCpp::implementation
3841

3942

4043
void StyleTransferEffect::ProcessFrame(ProcessVideoFrameContext context) {
44+
45+
46+
OutputDebugString(L"PF Start | ");
47+
if (evalStatus != nullptr && evalStatus.Status() == Windows::Foundation::AsyncStatus::Started) {
48+
context.OutputFrame() = cachedOutput;
49+
OutputDebugString(L"PF Cache | ");
50+
return;
51+
}
52+
VideoFrame inputFrame = context.InputFrame();
53+
VideoFrame outputFrame = context.OutputFrame();
54+
4155
std::lock_guard<mutex> guard{ Processing };
4256
auto now = std::chrono::high_resolution_clock::now();
4357
std::chrono::milliseconds timePassed;
4458
// If the first time calling ProcessFrame, just start the timer
4559
if (firstProcessFrameCall) {
4660
m_StartTime = now;
4761
firstProcessFrameCall = false;
62+
4863
}
4964
// On the second and any proceding process,
5065
else {
@@ -53,23 +68,20 @@ namespace winrt::StyleTransferEffectCpp::implementation
5368
Notifier.SetFrameRate(1000.f / timePassed.count()); // Convert to FPS: milli to seconds, invert
5469
}
5570

56-
OutputDebugString(L"PF Start | ");
57-
58-
VideoFrame inputFrame = context.InputFrame();
59-
VideoFrame outputFrame = context.OutputFrame();
60-
61-
// X, Y, Width, Height
62-
//inputFrame.CopyToAsync(inputTransformed, copyBounds, copyBounds).get();
6371
OutputDebugString(L"PF Locked | ");
6472
Binding.Bind(InputImageDescription, inputFrame);
6573
Binding.Bind(OutputImageDescription, outputTransformed);
6674

6775
OutputDebugString(L"PF Eval | ");
68-
Session.Evaluate(Binding, L"test");
69-
OutputDebugString(L"PF Copy | ");
70-
outputTransformed.CopyToAsync(outputFrame).get();
76+
evalStatus = Session.EvaluateAsync(Binding, L"test");
77+
auto evalTask = create_task(evalStatus);
78+
evalTask.then([&](LearningModelEvaluationResult result) {
79+
OutputDebugString(L"PF Copy | ");
80+
outputTransformed.CopyToAsync(context.OutputFrame()).get();
81+
cachedOutput = context.OutputFrame();
82+
OutputDebugString(L"PF End\n ");
83+
});
7184

72-
OutputDebugString(L"PF End\n ");
7385
}
7486

7587
void StyleTransferEffect::SetEncodingProperties(VideoEncodingProperties props, IDirect3DDevice device) {

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
1515
{
1616
StyleTransferEffect();
1717
VideoFrame outputTransformed;
18-
VideoFrame inputTransformed;
1918

2019
IVectorView<VideoEncodingProperties> SupportedEncodingProperties();
2120
bool TimeIndependent();
@@ -40,6 +39,8 @@ namespace winrt::StyleTransferEffectCpp::implementation
4039
std::chrono::time_point<std::chrono::steady_clock> m_StartTime;
4140
bool firstProcessFrameCall = true;
4241
Windows::Graphics::Imaging::BitmapBounds copyBounds;
42+
Windows::Foundation::IAsyncOperation<LearningModelEvaluationResult> evalStatus;
43+
VideoFrame cachedOutput;
4344
};
4445
}
4546

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffectCpp.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
109109
<ClCompile>
110110
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
111+
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
111112
</ClCompile>
112113
</ItemDefinitionGroup>
113114
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">

0 commit comments

Comments
 (0)