Skip to content

Commit db290a5

Browse files
committed
Add windows_dx_interop.cpp
1 parent 9bd969a commit db290a5

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

native/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ set(JCEF_SRCS_WINDOWS
172172
temp_window_win.cc
173173
temp_window_win.h
174174
util_win.cpp
175+
windows_dx_interop.cpp
175176
)
176177
APPEND_PLATFORM_SOURCES(JCEF_SRCS)
177178
source_group(jcef FILES ${JCEF_SRCS})
@@ -238,7 +239,8 @@ if(OS_LINUX)
238239
add_library(${JCEF_TARGET} SHARED ${JCEF_SRCS})
239240
SET_LIBRARY_TARGET_PROPERTIES(${JCEF_TARGET})
240241
add_dependencies(${JCEF_TARGET} libcef_dll_wrapper ${JCEF_HELPER_TARGET})
241-
target_link_libraries(${JCEF_TARGET} libcef_lib libcef_dll_wrapper ${CEF_STANDARD_LIBS} ${JNI_LIBRARIES})
242+
target_link_libraries(${JCEF_TARGET} libcef_lib libcef_dll_wrapper ${CEF_STANDARD_LIBS} ${JNI_LIBRARIES}
243+
d3d11 dxgi dxguid)
242244
target_include_directories(${JCEF_TARGET} PUBLIC ${JNI_INCLUDE_DIRS})
243245

244246
# Compile flags specific to the JCEF library target.

native/windows_dx_interop.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) 2025 The Chromium Embedded Framework Authors. All rights
2+
// reserved. Use of this source code is governed by a BSD-style license that
3+
// can be found in the LICENSE file.
4+
5+
#include "jni_util.h"
6+
7+
#include "include/base/cef_logging.h"
8+
9+
#include <d3d11.h>
10+
#include <iomanip>
11+
#include <wrl/client.h>
12+
13+
namespace {
14+
15+
using Microsoft::WRL::ComPtr;
16+
17+
} // namespace
18+
19+
JNIEXPORT jlong JNICALL
20+
Java_net_ccbluex_liquidbounce_mcef_cef_MCEFWindowsDxInterop_nCreateD3DDevice(
21+
JNIEnv*,
22+
jclass) {
23+
ComPtr<ID3D11Device> device;
24+
ComPtr<ID3D11DeviceContext> context;
25+
D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0;
26+
27+
static constexpr D3D_FEATURE_LEVEL feature_levels[] = {
28+
D3D_FEATURE_LEVEL_11_0,
29+
};
30+
31+
const HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE,
32+
nullptr,
33+
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
34+
feature_levels,
35+
ARRAYSIZE(feature_levels),
36+
D3D11_SDK_VERSION, &device,
37+
&feature_level, &context);
38+
if (FAILED(hr)) {
39+
LOG(ERROR) << "Failed to create D3D11 device for DX interop. hr=0x"
40+
<< std::hex << hr;
41+
return 0;
42+
}
43+
44+
LOG(INFO) << "Created D3D11 device for DX interop at feature level 0x"
45+
<< std::hex << static_cast<int>(feature_level);
46+
return reinterpret_cast<jlong>(device.Detach());
47+
}
48+
49+
JNIEXPORT jlong JNICALL
50+
Java_net_ccbluex_liquidbounce_mcef_cef_MCEFWindowsDxInterop_nOpenSharedTexture(
51+
JNIEnv*,
52+
jclass,
53+
jlong d3dDevice,
54+
jlong sharedTextureHandle) {
55+
if (d3dDevice == 0 || sharedTextureHandle == 0) {
56+
return 0;
57+
}
58+
59+
auto* device = reinterpret_cast<ID3D11Device*>(d3dDevice);
60+
ComPtr<ID3D11Texture2D> texture;
61+
const HRESULT hr = device->OpenSharedResource(
62+
reinterpret_cast<HANDLE>(sharedTextureHandle),
63+
__uuidof(ID3D11Texture2D),
64+
reinterpret_cast<void**>(texture.GetAddressOf()));
65+
if (FAILED(hr)) {
66+
LOG(ERROR) << "Failed to open shared D3D11 texture 0x" << std::hex
67+
<< sharedTextureHandle << ". hr=0x" << hr;
68+
return 0;
69+
}
70+
71+
return reinterpret_cast<jlong>(texture.Detach());
72+
}
73+
74+
JNIEXPORT void JNICALL
75+
Java_net_ccbluex_liquidbounce_mcef_cef_MCEFWindowsDxInterop_nReleaseComObject(
76+
JNIEnv*,
77+
jclass,
78+
jlong comObject) {
79+
if (comObject == 0) {
80+
return;
81+
}
82+
83+
auto* object = reinterpret_cast<IUnknown*>(comObject);
84+
object->Release();
85+
}

0 commit comments

Comments
 (0)