aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Util/Util_ImageWindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'LibOVR/Src/Util/Util_ImageWindow.cpp')
-rw-r--r--LibOVR/Src/Util/Util_ImageWindow.cpp265
1 files changed, 164 insertions, 101 deletions
diff --git a/LibOVR/Src/Util/Util_ImageWindow.cpp b/LibOVR/Src/Util/Util_ImageWindow.cpp
index cb091c7..4c120f9 100644
--- a/LibOVR/Src/Util/Util_ImageWindow.cpp
+++ b/LibOVR/Src/Util/Util_ImageWindow.cpp
@@ -5,16 +5,16 @@ Content : An output object for windows that can display raw images for tes
Created : March 13, 2014
Authors : Dean Beeler
-Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
+Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
-Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
+Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
-http://www.oculusvr.com/licenses/LICENSE-3.1
+http://www.oculusvr.com/licenses/LICENSE-3.2
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,12 +23,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************************/
-#include "../../Include/OVR.h"
+#include "../../Include/OVR_Kernel.h"
#include "Util_ImageWindow.h"
#if defined(OVR_OS_WIN32)
+#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "DWrite.h"
@@ -51,7 +52,16 @@ namespace OVR { namespace Util {
ID2D1Factory* ImageWindow::pD2DFactory = NULL;
IDWriteFactory* ImageWindow::pDWriteFactory = NULL;
-ImageWindow* ImageWindow::globalWindow[4];
+HINSTANCE ImageWindow::hInstD2d1 = NULL;
+HINSTANCE ImageWindow::hInstDwrite = NULL;
+
+
+// TODO(review): This appears to be (at present) necessary, the global list is accessed by the
+// render loop in Samples. In the current version, windows will just be lost when windowCount
+// exceeds MaxWindows; I've left that in place, since this is unfamiliar code. I'm not sure what
+// thread-safety guarantees this portion of the code needs to satisfy, so I don't want to
+// change it to a list or whatever. Asserts added to catch the error.
+ImageWindow* ImageWindow::globalWindow[ImageWindow::MaxWindows];
int ImageWindow::windowCount = 0;
LRESULT CALLBACK MainWndProc(
@@ -95,55 +105,69 @@ LRESULT CALLBACK MainWndProc(
//return 0;
}
+
ImageWindow::ImageWindow( uint32_t width, uint32_t height ) :
- frontBufferMutex( new Mutex() )
+ hWindow(NULL),
+ pRT(NULL),
+ //resolution(),
+ frontBufferMutex( new Mutex() ),
+ frames(),
+ greyBitmap(NULL),
+ colorBitmap(NULL)
{
-
- HINSTANCE hInst = LoadLibrary( L"d2d1.dll" );
- HINSTANCE hInstWrite = LoadLibrary( L"Dwrite.dll" );
-
D2D1CreateFactoryFn createFactory = NULL;
DWriteCreateFactoryFn writeFactory = NULL;
- if( hInst )
+ if (!hInstD2d1)
+ {
+ hInstD2d1 = LoadLibraryW( L"d2d1.dll" );
+ }
+
+ if (!hInstD2d1)
+ {
+ hInstD2d1 = LoadLibraryW( L"Dwrite.dll" );
+ }
+
+ if( hInstD2d1 )
{
- createFactory = (D2D1CreateFactoryFn)GetProcAddress( hInst, "D2D1CreateFactory" );
+ createFactory = (D2D1CreateFactoryFn)GetProcAddress( hInstD2d1, "D2D1CreateFactory" );
}
- if( hInstWrite )
+ if( hInstDwrite )
{
- writeFactory = (DWriteCreateFactoryFn)GetProcAddress( hInstWrite, "DWriteCreateFactory" );
+ writeFactory = (DWriteCreateFactoryFn)GetProcAddress( hInstDwrite, "DWriteCreateFactory" );
}
- globalWindow[windowCount] = this;
-
- ++windowCount;
+ // TODO: see note where globalWindow is declared.
+ globalWindow[windowCount++ % MaxWindows] = this;
+ OVR_ASSERT(windowCount < MaxWindows);
if( pD2DFactory == NULL && createFactory && writeFactory )
{
- createFactory(
+ // Create a Direct2D factory.
+ HRESULT hResult = createFactory(
D2D1_FACTORY_TYPE_MULTI_THREADED,
__uuidof(ID2D1Factory),
NULL,
- &pD2DFactory
+ &pD2DFactory // This will be AddRef'd for us.
);
+ OVR_ASSERT_AND_UNUSED(hResult == S_OK, hResult);
// Create a DirectWrite factory.
- writeFactory(
+ hResult = writeFactory(
DWRITE_FACTORY_TYPE_SHARED,
- __uuidof(pDWriteFactory),
- reinterpret_cast<IUnknown **>(&pDWriteFactory)
+ __uuidof(pDWriteFactory), // This probably should instead be __uuidof(IDWriteFactory)
+ reinterpret_cast<IUnknown **>(&pDWriteFactory) // This will be AddRef'd for us.
);
-
+ OVR_ASSERT_AND_UNUSED(hResult == S_OK, hResult);
}
resolution = D2D1::SizeU( width, height );
- SetWindowLongPtr( hWindow, GWLP_USERDATA, (LONG_PTR)this );
-
- pRT = NULL;
- greyBitmap = NULL;
- colorBitmap = NULL;
+ if (hWindow)
+ {
+ SetWindowLongPtr( hWindow, GWLP_USERDATA, (LONG_PTR)this );
+ }
}
ImageWindow::~ImageWindow()
@@ -155,7 +179,7 @@ ImageWindow::~ImageWindow()
globalWindow[i] = NULL;
break;
}
-}
+ }
if( greyBitmap )
greyBitmap->Release();
@@ -177,81 +201,110 @@ ImageWindow::~ImageWindow()
delete frontBufferMutex;
- ShowWindow( hWindow, SW_HIDE );
- DestroyWindow( hWindow );
+ if (hWindow)
+ {
+ ShowWindow( hWindow, SW_HIDE );
+ DestroyWindow( hWindow );
+ }
+
+ if (pD2DFactory)
+ {
+ pD2DFactory->Release();
+ pD2DFactory = NULL;
+ }
+
+ if (pDWriteFactory)
+ {
+ pDWriteFactory->Release();
+ pDWriteFactory = NULL;
+ }
+
+ if( hInstD2d1 )
+ {
+ FreeLibrary(hInstD2d1);
+ hInstD2d1 = NULL;
+ }
+
+ if( hInstDwrite )
+ {
+ FreeLibrary(hInstDwrite);
+ hInstDwrite = NULL;
+ }
}
void ImageWindow::AssociateSurface( void* surface )
{
- // Assume an IUnknown
- IUnknown* unknown = (IUnknown*)surface;
-
- IDXGISurface *pDxgiSurface = NULL;
- HRESULT hr = unknown->QueryInterface(&pDxgiSurface);
- if( hr == S_OK )
- {
- D2D1_RENDER_TARGET_PROPERTIES props =
- D2D1::RenderTargetProperties(
- D2D1_RENDER_TARGET_TYPE_DEFAULT,
- D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
- 96,
- 96
- );
-
-
- pRT = NULL;
- ID2D1RenderTarget* tmpTarget;
-
- hr = pD2DFactory->CreateDxgiSurfaceRenderTarget( pDxgiSurface, &props, &tmpTarget );
-
- if( hr == S_OK )
- {
- DXGI_SURFACE_DESC desc = {0};
- pDxgiSurface->GetDesc( &desc );
- int width = desc.Width;
- int height = desc.Height;
-
- D2D1_SIZE_U size = D2D1::SizeU( width, height );
-
- D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(
- DXGI_FORMAT_A8_UNORM,
- D2D1_ALPHA_MODE_PREMULTIPLIED
- );
-
- D2D1_PIXEL_FORMAT colorPixelFormat = D2D1::PixelFormat(
- DXGI_FORMAT_B8G8R8A8_UNORM,
- D2D1_ALPHA_MODE_PREMULTIPLIED
- );
-
- D2D1_BITMAP_PROPERTIES bitmapProps;
- bitmapProps.dpiX = 96;
- bitmapProps.dpiY = 96;
- bitmapProps.pixelFormat = pixelFormat;
-
- D2D1_BITMAP_PROPERTIES colorBitmapProps;
- colorBitmapProps.dpiX = 96;
- colorBitmapProps.dpiY = 96;
- colorBitmapProps.pixelFormat = colorPixelFormat;
-
- HRESULT result = tmpTarget->CreateBitmap( size, bitmapProps, &greyBitmap );
- if( result != S_OK )
- {
- tmpTarget->Release();
- tmpTarget = NULL;
- }
-
- result = tmpTarget->CreateBitmap( size, colorBitmapProps, &colorBitmap );
- if( result != S_OK )
- {
- greyBitmap->Release();
- greyBitmap = NULL;
-
- tmpTarget->Release();
- tmpTarget = NULL;
- }
- pRT = tmpTarget;
- }
- }
+ if (pD2DFactory)
+ {
+ // Assume an IUnknown
+ IUnknown* unknown = (IUnknown*)surface;
+
+ IDXGISurface *pDxgiSurface = NULL;
+ HRESULT hr = unknown->QueryInterface(&pDxgiSurface);
+ if( hr == S_OK )
+ {
+ D2D1_RENDER_TARGET_PROPERTIES props =
+ D2D1::RenderTargetProperties(
+ D2D1_RENDER_TARGET_TYPE_DEFAULT,
+ D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
+ 96,
+ 96
+ );
+
+ pRT = NULL;
+ ID2D1RenderTarget* tmpTarget;
+
+ hr = pD2DFactory->CreateDxgiSurfaceRenderTarget( pDxgiSurface, &props, &tmpTarget );
+
+ if( hr == S_OK )
+ {
+ DXGI_SURFACE_DESC desc = {0};
+ pDxgiSurface->GetDesc( &desc );
+ int width = desc.Width;
+ int height = desc.Height;
+
+ D2D1_SIZE_U size = D2D1::SizeU( width, height );
+
+ D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(
+ DXGI_FORMAT_A8_UNORM,
+ D2D1_ALPHA_MODE_PREMULTIPLIED
+ );
+
+ D2D1_PIXEL_FORMAT colorPixelFormat = D2D1::PixelFormat(
+ DXGI_FORMAT_B8G8R8A8_UNORM,
+ D2D1_ALPHA_MODE_PREMULTIPLIED
+ );
+
+ D2D1_BITMAP_PROPERTIES bitmapProps;
+ bitmapProps.dpiX = 96;
+ bitmapProps.dpiY = 96;
+ bitmapProps.pixelFormat = pixelFormat;
+
+ D2D1_BITMAP_PROPERTIES colorBitmapProps;
+ colorBitmapProps.dpiX = 96;
+ colorBitmapProps.dpiY = 96;
+ colorBitmapProps.pixelFormat = colorPixelFormat;
+
+ HRESULT result = tmpTarget->CreateBitmap( size, bitmapProps, &greyBitmap );
+ if( result != S_OK )
+ {
+ tmpTarget->Release();
+ tmpTarget = NULL;
+ }
+
+ if (tmpTarget)
+ {
+ result = tmpTarget->CreateBitmap(size, colorBitmapProps, &colorBitmap);
+ if (result != S_OK)
+ {
+ tmpTarget->Release();
+ tmpTarget = NULL;
+ }
+ }
+ pRT = tmpTarget;
+ }
+ }
+ }
}
void ImageWindow::Process()
@@ -508,4 +561,14 @@ void ImageWindow::addText( float x, float y, float r, float g, float b, OVR::Str
}}
-#endif //defined(OVR_OS_WIN32) \ No newline at end of file
+#else //defined(OVR_OS_WIN32)
+
+namespace OVR { namespace Util {
+
+ImageWindow* ImageWindow::globalWindow[4];
+int ImageWindow::windowCount = 0;
+
+}}
+
+#endif //#else //defined(OVR_OS_WIN32)
+