11#include " pch.h"
22#include " StyleTransferEffect.h"
33#include " StyleTransferEffect.g.cpp"
4+ #include < ppltasks.h>
5+
46using namespace std ;
57using namespace winrt ::Windows::Storage;
68using namespace winrt ::Windows::Storage::Streams;
9+ using namespace concurrency ;
710
811namespace 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) {
0 commit comments