Committing TBB 2019 Update 9 source code
[platform/upstream/tbb.git] / examples / parallel_for / tachyon / msvs / uwp / tbbTachyonRenderer.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 #include "pch.h"
18 #include "tbbTachyonRenderer.h"
19 #include <DirectXMath.h>
20 #include <process.h>
21 #include <thread>
22 #include "../../src/tachyon_video.h"
23
24 using namespace Microsoft::WRL;
25 using namespace Windows::Foundation;
26 using namespace Windows::Foundation::Collections;
27 using namespace Windows::UI::Core;
28 using namespace DirectX;
29
30 tbbTachyonRenderer::tbbTachyonRenderer() :
31     m_renderNeeded(true)
32 {
33 }
34
35 tbbTachyonRenderer::~tbbTachyonRenderer()
36 {
37 }
38
39 void tbbTachyonRenderer::CreateDeviceIndependentResources()
40 {
41     DirectXBase::CreateDeviceIndependentResources();
42
43     DX::ThrowIfFailed(
44         m_dwriteFactory->CreateTextFormat(
45         L"Segoe UI",
46         nullptr,
47         DWRITE_FONT_WEIGHT_NORMAL,
48         DWRITE_FONT_STYLE_NORMAL,
49         DWRITE_FONT_STRETCH_NORMAL,
50         32.0f,
51         L"en-US",
52         &m_textFormat
53         )
54         );
55
56     DX::ThrowIfFailed(
57         m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING)
58         );
59
60 }
61
62 unsigned int __stdcall example_main(void*);
63
64 float g_ratiox, g_ratioy;
65 extern unsigned int *g_pImg;
66 extern int g_sizex, g_sizey;
67 extern int global_xsize, global_ysize;
68 extern int volatile global_number_of_threads;
69 extern volatile long global_startTime;
70 extern volatile long global_elapsedTime;
71
72 #define SHOW_TEXT 1
73
74 void tbbTachyonRenderer::CreateDeviceResources()
75 {
76
77     DirectXBase::CreateDeviceResources();
78
79     DX::ThrowIfFailed(
80         m_d2dContext->CreateSolidColorBrush(
81         D2D1::ColorF(D2D1::ColorF::Green),
82         &m_Brush
83         )
84         );
85
86     D2D1_BITMAP_PROPERTIES1 properties = D2D1::BitmapProperties1(
87         D2D1_BITMAP_OPTIONS_TARGET,
88         D2D1::PixelFormat(
89         DXGI_FORMAT_R8G8B8A8_UNORM,
90         D2D1_ALPHA_MODE_IGNORE
91         )
92         );
93
94
95     //Setting manual rendering size
96     global_xsize = 800;
97     global_ysize = int(global_xsize/m_window->Bounds.Width*m_window->Bounds.Height);
98     D2D1_SIZE_U opacityBitmapSize = D2D1::SizeU(global_xsize, global_ysize);
99
100     DX::ThrowIfFailed(
101         m_d2dContext->CreateBitmap(
102         opacityBitmapSize,
103         (BYTE*)g_pImg,
104         sizeof(unsigned int)*g_sizex,
105         &properties,
106         &m_opacityBitmap
107         )
108         );
109
110     m_d2dContext->SetTarget(m_opacityBitmap.Get());
111     m_d2dContext->BeginDraw();
112
113     m_d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::Black, 0.0f));
114
115     DX::ThrowIfFailed(
116         m_d2dContext->EndDraw()
117         );
118
119     std::thread* thread_tmp=new std::thread(example_main, (void*)NULL);
120
121 }
122
123 void tbbTachyonRenderer::CreateWindowSizeDependentResources()
124 {
125     DirectXBase::CreateWindowSizeDependentResources();
126 }
127
128 void tbbTachyonRenderer::Render()
129 {
130     D2D1_SIZE_F size = m_d2dContext->GetSize();
131
132 #if SHOW_TEXT
133     if (video && video->running)
134         global_elapsedTime=(long)(time(NULL)-global_startTime);
135
136     Platform::String^ text= "Running in " +
137         (global_number_of_threads == utility::get_default_num_threads()? "all hardware threads: ":
138             global_number_of_threads.ToString() + (global_number_of_threads==1?" thread: ":" threads: ")) +
139         global_elapsedTime.ToString() + (global_elapsedTime>1?" seconds":" second");
140
141     g_ratiox=float(size.width/1024.0);
142     g_ratioy=float(size.height/512.0);
143
144     DX::ThrowIfFailed(
145         m_dwriteFactory->CreateTextLayout(
146         text->Data(),
147         text->Length(),
148         m_textFormat.Get(),
149         1000, // maxWidth
150         1000, // maxHeight
151         &m_textLayout
152         )
153         );
154
155     m_textLayout->GetMetrics(&m_textMetrics);
156 #endif
157
158     m_d2dContext->BeginDraw();
159
160     if(g_pImg)m_opacityBitmap->CopyFromMemory( NULL,(BYTE*)g_pImg, sizeof(unsigned int)*g_sizex );
161
162     m_d2dContext->DrawBitmap( m_opacityBitmap.Get(), D2D1::RectF(0,0,size.width,size.height) );
163
164 #if SHOW_TEXT
165     m_d2dContext->DrawTextLayout(
166         D2D1::Point2F(0.0f, 0.0f),
167         m_textLayout.Get(),
168         m_Brush.Get(),
169         D2D1_DRAW_TEXT_OPTIONS_CLIP
170         );
171 #endif
172
173     HRESULT hr = m_d2dContext->EndDraw();
174
175     if (hr == D2DERR_RECREATE_TARGET){
176         m_d2dContext->SetTarget(nullptr);
177         m_d2dTargetBitmap = nullptr;
178         CreateWindowSizeDependentResources();
179     }else{
180         DX::ThrowIfFailed(hr);
181     }
182
183     m_renderNeeded = false;
184 }
185