Util_ImageWindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /************************************************************************************
  2. Filename : Util_ImageWindow.cpp
  3. Content : An output object for windows that can display raw images for testing
  4. Created : March 13, 2014
  5. Authors : Dean Beeler
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. *************************************************************************************/
  19. #include "Kernel/OVR_Types.h"
  20. OVR_DISABLE_ALL_MSVC_WARNINGS()
  21. #include "Kernel/OVR_Allocator.h"
  22. #include "Kernel/OVR_RefCount.h"
  23. #include "Kernel/OVR_Log.h"
  24. #include "Kernel/OVR_System.h"
  25. #include "Kernel/OVR_Nullptr.h"
  26. #include "Kernel/OVR_String.h"
  27. #include "Kernel/OVR_Array.h"
  28. #include "Kernel/OVR_Timer.h"
  29. #include "Kernel/OVR_SysFile.h"
  30. #include "Util_ImageWindow.h"
  31. #if defined(OVR_OS_WIN32)
  32. #include "Kernel/OVR_Win32_IncludeWindows.h"
  33. #include "DWrite.h"
  34. #endif
  35. OVR_RESTORE_ALL_MSVC_WARNINGS()
  36. #if defined(OVR_OS_WIN32)
  37. typedef HRESULT (WINAPI *D2D1CreateFactoryFn)(
  38. _In_ D2D1_FACTORY_TYPE,
  39. _In_ REFIID,
  40. _In_opt_ const D2D1_FACTORY_OPTIONS*,
  41. _Out_ ID2D1Factory **
  42. );
  43. typedef HRESULT (WINAPI *DWriteCreateFactoryFn)(
  44. _In_ DWRITE_FACTORY_TYPE factoryType,
  45. _In_ REFIID iid,
  46. _Out_ IUnknown **factory
  47. );
  48. namespace OVR { namespace Util {
  49. ID2D1Factory* ImageWindow::pD2DFactory = NULL;
  50. IDWriteFactory* ImageWindow::pDWriteFactory = NULL;
  51. HINSTANCE ImageWindow::hInstD2d1 = NULL;
  52. HINSTANCE ImageWindow::hInstDwrite = NULL;
  53. // TODO(review): This appears to be (at present) necessary, the global list is accessed by the
  54. // render loop in Samples. In the current version, windows will just be lost when windowCount
  55. // exceeds MaxWindows; I've left that in place, since this is unfamiliar code. I'm not sure what
  56. // thread-safety guarantees this portion of the code needs to satisfy, so I don't want to
  57. // change it to a list or whatever. Asserts added to catch the error.
  58. ImageWindow* ImageWindow::globalWindow[ImageWindow::MaxWindows];
  59. int ImageWindow::windowCount = 0;
  60. LRESULT CALLBACK MainWndProc(
  61. HWND hwnd,
  62. UINT uMsg,
  63. WPARAM wParam,
  64. LPARAM lParam)
  65. {
  66. switch (uMsg)
  67. {
  68. case WM_CREATE:
  69. return 0;
  70. case WM_PAINT:
  71. {
  72. LONG_PTR ptr = GetWindowLongPtr( hwnd, GWLP_USERDATA );
  73. if( ptr )
  74. {
  75. ImageWindow* iw = (ImageWindow*)ptr;
  76. iw->OnPaint();
  77. }
  78. }
  79. return 0;
  80. case WM_SIZE:
  81. // Set the size and position of the window.
  82. return 0;
  83. case WM_DESTROY:
  84. // Clean up window-specific data objects.
  85. return 0;
  86. //
  87. // Process other messages.
  88. //
  89. default:
  90. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  91. }
  92. //return 0;
  93. }
  94. ImageWindow::ImageWindow( uint32_t width, uint32_t height ) :
  95. hWindow(NULL),
  96. pRT(NULL),
  97. //resolution(),
  98. frontBufferMutex( new Mutex() ),
  99. frames(),
  100. greyBitmap(NULL),
  101. colorBitmap(NULL)
  102. {
  103. D2D1CreateFactoryFn createFactory = NULL;
  104. DWriteCreateFactoryFn writeFactory = NULL;
  105. if (!hInstD2d1)
  106. {
  107. hInstD2d1 = LoadLibraryW( L"d2d1.dll" );
  108. }
  109. if (!hInstD2d1)
  110. {
  111. hInstD2d1 = LoadLibraryW( L"Dwrite.dll" );
  112. }
  113. if( hInstD2d1 )
  114. {
  115. createFactory = (D2D1CreateFactoryFn)GetProcAddress( hInstD2d1, "D2D1CreateFactory" );
  116. }
  117. if( hInstDwrite )
  118. {
  119. writeFactory = (DWriteCreateFactoryFn)GetProcAddress( hInstDwrite, "DWriteCreateFactory" );
  120. }
  121. // TODO: see note where globalWindow is declared.
  122. globalWindow[windowCount++ % MaxWindows] = this;
  123. OVR_ASSERT(windowCount < MaxWindows);
  124. if( pD2DFactory == NULL && createFactory && writeFactory )
  125. {
  126. // Create a Direct2D factory.
  127. HRESULT hResult = createFactory(
  128. D2D1_FACTORY_TYPE_MULTI_THREADED,
  129. __uuidof(ID2D1Factory),
  130. NULL,
  131. &pD2DFactory // This will be AddRef'd for us.
  132. );
  133. OVR_ASSERT_AND_UNUSED(hResult == S_OK, hResult);
  134. // Create a DirectWrite factory.
  135. hResult = writeFactory(
  136. DWRITE_FACTORY_TYPE_SHARED,
  137. __uuidof(pDWriteFactory), // This probably should instead be __uuidof(IDWriteFactory)
  138. reinterpret_cast<IUnknown **>(&pDWriteFactory) // This will be AddRef'd for us.
  139. );
  140. OVR_ASSERT_AND_UNUSED(hResult == S_OK, hResult);
  141. }
  142. resolution = D2D1::SizeU( width, height );
  143. if (hWindow)
  144. {
  145. SetWindowLongPtr( hWindow, GWLP_USERDATA, (LONG_PTR)this );
  146. }
  147. }
  148. ImageWindow::~ImageWindow()
  149. {
  150. for( int i = 0; i < MaxWindows; ++i )
  151. {
  152. if( globalWindow[i] == this )
  153. {
  154. globalWindow[i] = NULL;
  155. break;
  156. }
  157. }
  158. if( greyBitmap )
  159. greyBitmap->Release();
  160. if( colorBitmap )
  161. colorBitmap->Release();
  162. if( pRT )
  163. pRT->Release();
  164. {
  165. Mutex::Locker locker( frontBufferMutex );
  166. while( frames.GetSize() )
  167. {
  168. Ptr<Frame> aFrame = frames.PopBack();
  169. }
  170. }
  171. delete frontBufferMutex;
  172. if (hWindow)
  173. {
  174. ShowWindow( hWindow, SW_HIDE );
  175. DestroyWindow( hWindow );
  176. }
  177. if (pD2DFactory)
  178. {
  179. pD2DFactory->Release();
  180. pD2DFactory = NULL;
  181. }
  182. if (pDWriteFactory)
  183. {
  184. pDWriteFactory->Release();
  185. pDWriteFactory = NULL;
  186. }
  187. if( hInstD2d1 )
  188. {
  189. FreeLibrary(hInstD2d1);
  190. hInstD2d1 = NULL;
  191. }
  192. if( hInstDwrite )
  193. {
  194. FreeLibrary(hInstDwrite);
  195. hInstDwrite = NULL;
  196. }
  197. }
  198. void ImageWindow::AssociateSurface( void* surface )
  199. {
  200. if (pD2DFactory)
  201. {
  202. // Assume an IUnknown
  203. IUnknown* unknown = (IUnknown*)surface;
  204. IDXGISurface *pDxgiSurface = NULL;
  205. HRESULT hr = unknown->QueryInterface(&pDxgiSurface);
  206. if( hr == S_OK )
  207. {
  208. D2D1_RENDER_TARGET_PROPERTIES props =
  209. D2D1::RenderTargetProperties(
  210. D2D1_RENDER_TARGET_TYPE_DEFAULT,
  211. D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
  212. 96,
  213. 96
  214. );
  215. pRT = NULL;
  216. ID2D1RenderTarget* tmpTarget;
  217. hr = pD2DFactory->CreateDxgiSurfaceRenderTarget( pDxgiSurface, &props, &tmpTarget );
  218. if( hr == S_OK )
  219. {
  220. DXGI_SURFACE_DESC desc = {0};
  221. pDxgiSurface->GetDesc( &desc );
  222. int width = desc.Width;
  223. int height = desc.Height;
  224. D2D1_SIZE_U size = D2D1::SizeU( width, height );
  225. D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(
  226. DXGI_FORMAT_A8_UNORM,
  227. D2D1_ALPHA_MODE_PREMULTIPLIED
  228. );
  229. D2D1_PIXEL_FORMAT colorPixelFormat = D2D1::PixelFormat(
  230. DXGI_FORMAT_B8G8R8A8_UNORM,
  231. D2D1_ALPHA_MODE_PREMULTIPLIED
  232. );
  233. D2D1_BITMAP_PROPERTIES bitmapProps;
  234. bitmapProps.dpiX = 96;
  235. bitmapProps.dpiY = 96;
  236. bitmapProps.pixelFormat = pixelFormat;
  237. D2D1_BITMAP_PROPERTIES colorBitmapProps;
  238. colorBitmapProps.dpiX = 96;
  239. colorBitmapProps.dpiY = 96;
  240. colorBitmapProps.pixelFormat = colorPixelFormat;
  241. HRESULT result = tmpTarget->CreateBitmap( size, bitmapProps, &greyBitmap );
  242. if( result != S_OK )
  243. {
  244. tmpTarget->Release();
  245. tmpTarget = NULL;
  246. }
  247. if (tmpTarget)
  248. {
  249. result = tmpTarget->CreateBitmap(size, colorBitmapProps, &colorBitmap);
  250. if (result != S_OK)
  251. {
  252. tmpTarget->Release();
  253. tmpTarget = NULL;
  254. }
  255. }
  256. pRT = tmpTarget;
  257. }
  258. }
  259. }
  260. }
  261. void ImageWindow::Process()
  262. {
  263. if( pRT && greyBitmap )
  264. {
  265. OnPaint();
  266. pRT->Flush();
  267. }
  268. }
  269. void ImageWindow::Complete()
  270. {
  271. Mutex::Locker locker( frontBufferMutex );
  272. if( frames.IsEmpty() )
  273. return;
  274. if( frames.PeekBack(0)->ready )
  275. return;
  276. Ptr<Frame> frame = frames.PeekBack(0);
  277. frame->ready = true;
  278. }
  279. void ImageWindow::OnPaint()
  280. {
  281. Mutex::Locker locker( frontBufferMutex );
  282. // Nothing to do
  283. if( frames.IsEmpty() )
  284. return;
  285. if( !frames.PeekFront(0)->ready )
  286. return;
  287. Ptr<Frame> currentFrame = frames.PopFront();
  288. Ptr<Frame> nextFrame = NULL;
  289. if( !frames.IsEmpty() )
  290. nextFrame = frames.PeekFront(0);
  291. while( nextFrame && nextFrame->ready )
  292. {
  293. // Free up the current frame since it's been removed from the deque
  294. currentFrame = frames.PopFront();
  295. if( frames.IsEmpty() )
  296. break;
  297. nextFrame = frames.PeekFront(0);
  298. }
  299. if( currentFrame->imageData )
  300. greyBitmap->CopyFromMemory( NULL, currentFrame->imageData, currentFrame->width );
  301. if( currentFrame->colorImageData )
  302. colorBitmap->CopyFromMemory( NULL, currentFrame->colorImageData, currentFrame->colorPitch );
  303. pRT->BeginDraw();
  304. pRT->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
  305. pRT->Clear( D2D1::ColorF(D2D1::ColorF::Black) );
  306. // This will mirror our image
  307. D2D1_MATRIX_3X2_F m;
  308. m._11 = -1; m._12 = 0;
  309. m._21 = 0; m._22 = 1;
  310. m._31 = 0; m._32 = 0;
  311. pRT->SetTransform( m );
  312. ID2D1SolidColorBrush* whiteBrush;
  313. pRT->CreateSolidColorBrush( D2D1::ColorF(D2D1::ColorF::White, 1.0f), &whiteBrush );
  314. if( currentFrame->imageData )
  315. {
  316. pRT->FillOpacityMask( greyBitmap, whiteBrush,
  317. D2D1_OPACITY_MASK_CONTENT_TEXT_NATURAL,
  318. D2D1::RectF( -(FLOAT)resolution.width, 0.0f, (FLOAT)0.0f, (FLOAT)resolution.height ),
  319. //D2D1::RectF( 0.0f, 0.0f, (FLOAT)0.0f, (FLOAT)resolution.height ),
  320. D2D1::RectF( 0.0f, 0.0f, (FLOAT)resolution.width, (FLOAT)resolution.height ) );
  321. }
  322. else if( currentFrame->colorImageData )
  323. {
  324. pRT->DrawBitmap( colorBitmap,
  325. D2D1::RectF( -(FLOAT)resolution.width, 0.0f, (FLOAT)0.0f, (FLOAT)resolution.height ) );
  326. }
  327. pRT->SetTransform(D2D1::Matrix3x2F::Identity());
  328. whiteBrush->Release();
  329. Array<CirclePlot>::Iterator it;
  330. for( it = currentFrame->plots.Begin(); it != currentFrame->plots.End(); ++it )
  331. {
  332. ID2D1SolidColorBrush* aBrush;
  333. pRT->CreateSolidColorBrush( D2D1::ColorF( it->r, it->g, it->b), &aBrush );
  334. D2D1_ELLIPSE ellipse;
  335. ellipse.point.x = it->x;
  336. ellipse.point.y = it->y;
  337. ellipse.radiusX = it->radius;
  338. ellipse.radiusY = it->radius;
  339. if( it->fill )
  340. pRT->FillEllipse( &ellipse, aBrush );
  341. else
  342. pRT->DrawEllipse( &ellipse, aBrush );
  343. aBrush->Release();
  344. }
  345. static const WCHAR msc_fontName[] = L"Verdana";
  346. static const FLOAT msc_fontSize = 20;
  347. IDWriteTextFormat* textFormat = NULL;
  348. // Create a DirectWrite text format object.
  349. pDWriteFactory->CreateTextFormat(
  350. msc_fontName,
  351. NULL,
  352. DWRITE_FONT_WEIGHT_NORMAL,
  353. DWRITE_FONT_STYLE_NORMAL,
  354. DWRITE_FONT_STRETCH_NORMAL,
  355. msc_fontSize,
  356. L"", //locale
  357. &textFormat
  358. );
  359. D2D1_SIZE_F renderTargetSize = pRT->GetSize();
  360. Array<TextPlot>::Iterator textIt;
  361. for( textIt = currentFrame->textLines.Begin(); textIt != currentFrame->textLines.End(); ++textIt )
  362. {
  363. ID2D1SolidColorBrush* aBrush;
  364. pRT->CreateSolidColorBrush( D2D1::ColorF( textIt->r, textIt->g, textIt->b), &aBrush );
  365. WCHAR* tmpString = (WCHAR*)calloc( textIt->text.GetLength(), sizeof( WCHAR ) );
  366. for( unsigned i = 0; i < textIt->text.GetLength(); ++i )
  367. {
  368. tmpString[i] = (WCHAR)textIt->text.GetCharAt( i );
  369. }
  370. pRT->DrawText( tmpString, (UINT32)textIt->text.GetLength(), textFormat,
  371. D2D1::RectF(textIt->x, textIt->y, renderTargetSize.width, renderTargetSize.height), aBrush );
  372. free( tmpString );
  373. aBrush->Release();
  374. }
  375. if( textFormat )
  376. textFormat->Release();
  377. pRT->EndDraw();
  378. pRT->Flush();
  379. }
  380. Ptr<Frame> ImageWindow::lastUnreadyFrame()
  381. {
  382. static int framenumber = 0;
  383. if( frames.GetSize() && !frames.PeekBack( 0 )->ready )
  384. return frames.PeekBack( 0 );
  385. // Create a new frame if an unready one doesn't already exist
  386. Ptr<Frame> tmpFrame = *new Frame( framenumber );
  387. frames.PushBack( tmpFrame );
  388. ++framenumber;
  389. return tmpFrame;
  390. }
  391. void ImageWindow::UpdateImageBW( const uint8_t* imageData, uint32_t width, uint32_t height )
  392. {
  393. if( pRT && greyBitmap )
  394. {
  395. Mutex::Locker locker( frontBufferMutex );
  396. Ptr<Frame> frame = lastUnreadyFrame();
  397. frame->imageData = malloc( width * height );
  398. frame->width = width;
  399. frame->height = height;
  400. memcpy( frame->imageData, imageData, width * height );
  401. }
  402. }
  403. void ImageWindow::UpdateImageRGBA( const uint8_t* imageData, uint32_t width, uint32_t height, uint32_t pitch )
  404. {
  405. if( pRT && colorBitmap )
  406. {
  407. Mutex::Locker locker( frontBufferMutex );
  408. Ptr<Frame> frame = lastUnreadyFrame();
  409. frame->colorImageData = malloc( pitch * height );
  410. frame->width = width;
  411. frame->height = height;
  412. frame->colorPitch = pitch;
  413. memcpy( frame->colorImageData, imageData, pitch * height );
  414. }
  415. }
  416. void ImageWindow::addCircle( float x, float y, float radius, float r, float g, float b, bool fill )
  417. {
  418. if( pRT )
  419. {
  420. CirclePlot cp;
  421. cp.x = x;
  422. cp.y = y;
  423. cp.radius = radius;
  424. cp.r = r;
  425. cp.g = g;
  426. cp.b = b;
  427. cp.fill = fill;
  428. Mutex::Locker locker( frontBufferMutex );
  429. Ptr<Frame> frame = lastUnreadyFrame();
  430. frame->plots.PushBack( cp );
  431. }
  432. }
  433. void ImageWindow::addText( float x, float y, float r, float g, float b, OVR::String text )
  434. {
  435. if( pRT )
  436. {
  437. TextPlot tp;
  438. tp.x = x;
  439. tp.y = y;
  440. tp.r = r;
  441. tp.g = g;
  442. tp.b = b;
  443. tp.text = text;
  444. Mutex::Locker locker( frontBufferMutex );
  445. Ptr<Frame> frame = lastUnreadyFrame();
  446. frame->textLines.PushBack( tp );
  447. }
  448. }
  449. }}
  450. #else //defined(OVR_OS_WIN32)
  451. namespace OVR { namespace Util {
  452. ImageWindow* ImageWindow::globalWindow[4];
  453. int ImageWindow::windowCount = 0;
  454. }}
  455. #endif //#else //defined(OVR_OS_WIN32)