diff --git a/README.md b/README.md index 531414aba1..807ee52887 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/android/filament-android/src/main/cpp/nativewindow/Win32.cpp b/android/filament-android/src/main/cpp/nativewindow/Win32.cpp index 8b32b5b84d..65a16174e8 100644 --- a/android/filament-android/src/main/cpp/nativewindow/Win32.cpp +++ b/android/filament-android/src/main/cpp/nativewindow/Win32.cpp @@ -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; } @@ -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); } diff --git a/filament/backend/src/opengl/PlatformWGL.cpp b/filament/backend/src/opengl/PlatformWGL.cpp index 6ab35ca5e5..00768697dc 100644 --- a/filament/backend/src/opengl/PlatformWGL.cpp +++ b/filament/backend/src/opengl/PlatformWGL.cpp @@ -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(); @@ -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); } diff --git a/filament/backend/src/vulkan/PlatformVkWindows.cpp b/filament/backend/src/vulkan/PlatformVkWindows.cpp index e7a418ba8c..7c5ea259fb 100644 --- a/filament/backend/src/vulkan/PlatformVkWindows.cpp +++ b/filament/backend/src/vulkan/PlatformVkWindows.cpp @@ -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; diff --git a/java/filament/src/java/com/google/android/filament/FilamentCanvas.java b/java/filament/src/java/com/google/android/filament/FilamentCanvas.java index 4c740e5dd2..fc15a9fa92 100644 --- a/java/filament/src/java/com/google/android/filament/FilamentCanvas.java +++ b/java/filament/src/java/com/google/android/filament/FilamentCanvas.java @@ -63,5 +63,10 @@ public class FilamentCanvas extends Canvas implements FilamentTarget { } public void destroy(@NonNull Engine engine) { + if (mSwapChain == null) { + return; + } + engine.destroySwapChain(mSwapChain); + mSwapChain = null; } } diff --git a/samples/app/NativeWindowHelperWindows.cpp b/samples/app/NativeWindowHelperWindows.cpp index 335ac32073..8d0219d7c1 100644 --- a/samples/app/NativeWindowHelperWindows.cpp +++ b/samples/app/NativeWindowHelperWindows.cpp @@ -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; }