Skip to content

Commit

Permalink
Fix issue with Windows JAWT swapchains (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
bejado committed Mar 25, 2019
1 parent 25eef9d commit 8b7c215
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -730,7 +730,7 @@ value is the desired roughness between 0 and 1.
### Native Linux, macOS and Windows

You must create an `Engine`, a `Renderer` and a `SwapChain`. The `SwapChain` is created from a
native window pointer (an `NSView` on macOS or a `HDC` on Windows for instance):
native window pointer (an `NSView` on macOS or a `HWND` on Windows for instance):

```c++
Engine* engine = Engine::create();
Expand Down
41 changes: 5 additions & 36 deletions android/filament-android/src/main/cpp/nativewindow/Win32.cpp
Expand Up @@ -27,46 +27,20 @@

extern "C" {

void chooseAndSetPixelFormat(HDC dc) {
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, // Number of bits for the depthbuffer
0, // Number of bits for the stencilbuffer
0, // Number of aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelFormat = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc, pixelFormat, &pfd);
}

void* getNativeWindow(JNIEnv* env, jclass, jobject surface) {
void* win = nullptr;
JAWT_DrawingSurface* ds = nullptr;
JAWT_DrawingSurfaceInfo* dsi = nullptr;

if (!acquireDrawingSurface(env, surface, &ds, &dsi)) {
return win;
return nullptr;
}

JAWT_Win32DrawingSurfaceInfo* dsi_win32 = (JAWT_Win32DrawingSurfaceInfo*) dsi->platformInfo;
HDC dc = dsi_win32->hdc;
chooseAndSetPixelFormat(dsi_win32->hdc);
HWND hWnd = dsi_win32->hwnd;

win = (void*) dsi_win32->hdc;
releaseDrawingSurface(ds, dsi);

return win;
return (void*) hWnd;
}


Expand All @@ -82,16 +56,11 @@ jlong createNativeSurface(jint width, jint height) {
HWND window = CreateWindowA("STATIC", "dummy", 0, 0, 0, width, height, NULL, NULL, NULL, NULL);
SetWindowLong(window, GWL_STYLE, 0); //remove all window styles

HDC dc = GetDC(window);
chooseAndSetPixelFormat(dc);

return (jlong) dc;
return (jlong) window;
}

void destroyNativeSurface(jlong surface) {
HDC dc = (HDC) surface;
HWND window = WindowFromDC(dc);
ReleaseDC(window, dc);
HWND window = (HWND) surface;
DestroyWindow(window);
}

Expand Down
9 changes: 6 additions & 3 deletions filament/backend/src/opengl/PlatformWGL.cpp
Expand Up @@ -158,8 +158,8 @@ void PlatformWGL::terminate() noexcept {
}

Platform::SwapChain* PlatformWGL::createSwapChain(void* nativeWindow, uint64_t& flags) noexcept {
// on Windows, the nativeWindow maps directly to a HDC
HDC hdc = (HDC) nativeWindow;
// on Windows, the nativeWindow maps to a HWND
HDC hdc = GetDC((HWND) nativeWindow);
if (!ASSERT_POSTCONDITION_NON_FATAL(hdc,
"Unable to create the SwapChain (nativeWindow = %p)", nativeWindow)) {
reportLastWindowsError();
Expand All @@ -169,11 +169,14 @@ Platform::SwapChain* PlatformWGL::createSwapChain(void* nativeWindow, uint64_t&
int pixelFormat = ChoosePixelFormat(hdc, &mPfd);
SetPixelFormat(hdc, pixelFormat, &mPfd);

SwapChain* swapChain = (SwapChain *)hdc;
SwapChain* swapChain = (SwapChain*) hdc;
return swapChain;
}

void PlatformWGL::destroySwapChain(Platform::SwapChain* swapChain) noexcept {
HDC dc = (HDC) swapChain;
HWND window = WindowFromDC(dc);
ReleaseDC(window, dc);
// make this swapChain not current (by making a dummy one current)
wglMakeCurrent(mWhdc, mContext);
}
Expand Down
3 changes: 1 addition & 2 deletions filament/backend/src/vulkan/PlatformVkWindows.cpp
Expand Up @@ -43,8 +43,7 @@ void* PlatformVkWindows::createVkSurfaceKHR(void* nativeWindow, void* instance,
uint32_t* width, uint32_t* height) noexcept {
VkSurfaceKHR surface = nullptr;

HDC deviceContext = (HDC) nativeWindow;
HWND window = WindowFromDC(deviceContext);
HWND window = (HWND) nativeWindow;

VkWin32SurfaceCreateInfoKHR createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
Expand Down
Expand Up @@ -63,5 +63,10 @@ private SwapChain getSwapChain(@NonNull Engine engine) {
}

public void destroy(@NonNull Engine engine) {
if (mSwapChain == null) {
return;
}
engine.destroySwapChain(mSwapChain);
mSwapChain = null;
}
}
2 changes: 1 addition & 1 deletion samples/app/NativeWindowHelperWindows.cpp
Expand Up @@ -24,6 +24,6 @@ void* getNativeWindow(SDL_Window* sdlWindow) {
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
ASSERT_POSTCONDITION(SDL_GetWindowWMInfo(sdlWindow, &wmi), "SDL version unsupported!");
HDC win = (HDC) wmi.info.win.hdc;
HWND win = (HWND) wmi.info.win.window;
return (void*) win;
}

0 comments on commit 8b7c215

Please sign in to comment.