e56f6c9bf1853391118b1102ba0365934bcd7ee7
[platform/upstream/tbb.git] / examples / parallel_for / tachyon / msvs / win8ui / DirectXPage.xaml.cpp
1 /*
2     Copyright (c) 2005-2019 Intel Corporation
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8         http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16
17
18 #include "pch.h"
19 #include "DirectXPage.xaml.h"
20 #include "tbb/tbb.h"
21
22 using namespace tbbTachyon;
23
24 using namespace Platform;
25 using namespace Windows::Foundation;
26 using namespace Windows::Foundation::Collections;
27 using namespace Windows::UI::Input;
28 using namespace Windows::UI::Core;
29 using namespace Windows::UI::Xaml;
30 using namespace Windows::UI::Xaml::Controls;
31 using namespace Windows::UI::Xaml::Controls::Primitives;
32 using namespace Windows::UI::Xaml::Data;
33 using namespace Windows::UI::Xaml::Input;
34 using namespace Windows::UI::Xaml::Media;
35 using namespace Windows::UI::Xaml::Navigation;
36 using namespace Windows::Graphics::Display;
37
38 #include "src/tachyon_video.h"
39 extern int volatile global_number_of_threads;
40 extern volatile bool global_isCancelled;
41
42 #pragma intrinsic(_BitScanReverse)
43 static int log2( unsigned int x ) {
44     DWORD i;
45     _BitScanReverse(&i,(DWORD)x);
46     return (int)i;
47 }
48
49 const unsigned interval_step_power = 1;
50 const unsigned num_interval_steps = 1 << (interval_step_power+1);
51
52 DirectXPage::DirectXPage() :
53     m_renderNeeded(true)
54 {
55     InitializeComponent();
56
57     m_renderer = ref new tbbTachyonRenderer();
58
59     m_renderer->Initialize(
60         Window::Current->CoreWindow,
61         this,
62         DisplayProperties::LogicalDpi
63         );
64
65     m_eventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(this, &DirectXPage::OnRendering));
66
67     int num_threads = 2*tbb::task_scheduler_init::default_num_threads();
68     // The thread slider has geometric sequence with several intermediate steps for each interval between 2^N and 2^(N+1).
69     // The nearest (from below) the power of 2.
70     int i_base = log2(num_threads);
71     int base = 1 << i_base;
72     // The step size for the current interval.
73     int step = base / num_interval_steps;
74     // The number of steps inside the interval.
75     int i_step = (num_threads-base)/step;
76
77     ThreadsSlider->Maximum = (i_base-interval_step_power)*num_interval_steps + i_step;
78     global_number_of_threads = m_number_of_threads = tbb::task_scheduler_init::automatic;
79 }
80
81 DirectXPage::~DirectXPage()
82 {
83 }
84
85 void DirectXPage::OnRendering(Platform::Object^ sender, Platform::Object^ args)
86 {
87     if (m_renderNeeded){
88         m_renderer->Render();
89         m_renderer->Present();
90         m_renderNeeded = true;
91     }
92 }
93
94 void tbbTachyon::DirectXPage::ThreadsApply_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
95 {
96     if (global_number_of_threads != m_number_of_threads){
97         global_number_of_threads = m_number_of_threads;
98         global_isCancelled = true;
99         video->running = false;
100         m_renderNeeded = true;
101         ThreadsApply->Visibility=Windows::UI::Xaml::Visibility::Collapsed;
102     }
103 }
104
105 void tbbTachyon::DirectXPage::Exit_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
106 {
107     m_renderNeeded = false;
108     Application::Current->Exit();
109 }
110
111 void tbbTachyon::DirectXPage::ThreadsSliderValueChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::Primitives::RangeBaseValueChangedEventArgs^ e)
112 {
113     int pos = (int) e->NewValue;
114
115     // The nearest (from below) the power of 2.
116     int base = pos<num_interval_steps ? 0 : 1 << (pos/num_interval_steps+interval_step_power);
117     // The step size for the current interval.
118     int step = max(1,base/num_interval_steps);
119     m_number_of_threads = base + (pos%num_interval_steps)*step;
120
121     if (m_number_of_threads == 0) m_number_of_threads = tbb::task_scheduler_init::automatic;
122
123     NumberOfThreadsTextBlock->Text="Number Of Threads: " + (m_number_of_threads == tbb::task_scheduler_init::automatic? "Auto": m_number_of_threads.ToString());
124     if (global_number_of_threads != m_number_of_threads){
125         ThreadsApply->Visibility=Windows::UI::Xaml::Visibility::Visible;
126     }else{
127         ThreadsApply->Visibility=Windows::UI::Xaml::Visibility::Collapsed;
128     }
129 }