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