Skip to content

Commit

Permalink
feat: disable DirectComposition if target windows 7
Browse files Browse the repository at this point in the history
  • Loading branch information
tishion committed Feb 26, 2025
1 parent c8b04da commit 064eb1f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 21 deletions.
20 changes: 13 additions & 7 deletions example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,24 @@ MainWindow::createRightCefView()
#endif
setting.setWindowlessFrameRate(240);
setting.setHardwareAcceleration(true);
// setting.setBackgroundColor(Qt::cyan);
// setting.setBackgroundColor(QColor::fromRgba(qRgba(255, 255, 220, 255)));
QColor background(18, 18, 20, 255);
setting.setBackgroundColor(background);

// create the QCefView widget and add it to the layout container
// m_pRightCefViewWidget = new QCefView(RIGHT_INDEX_URL, &setting, this);
m_pRightCefViewWidget = new QCefView("https://www.testufo.com/", &setting, this);

auto vl = new QVBoxLayout(m_pRightCefViewWidget);
auto btn = new QPushButton("TEST BUTTON OVERLAY", m_pRightCefViewWidget);
vl->setAlignment(Qt::AlignTop);
vl->addWidget(btn);
m_pRightCefViewWidget->setLayout(vl);
// auto vl = new QVBoxLayout(m_pRightCefViewWidget);
// auto btn = new QPushButton("TEST BUTTON OVERLAY", m_pRightCefViewWidget);
//// btn->setFixedSize(320, 240);
// btn->setStyleSheet("background-color: rgba(1, 1, 1, 0);");
// btn->setAttribute(Qt::WA_TranslucentBackground);
// btn->setWindowFlags(Qt::FramelessWindowHint);
// btn->setAttribute(Qt::WA_NoSystemBackground);
// vl->setAlignment(Qt::AlignVCenter);

// vl->addWidget(btn);
// m_pRightCefViewWidget->setLayout(vl);

// all the following values will disable the context menu for both NCW and OSR mode
// m_pRightCefViewWidget->setContextMenuPolicy(Qt::NoContextMenu);
Expand Down
8 changes: 6 additions & 2 deletions example/QCefViewTest/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@
</property>
<widget class="QWidget" name="leftCefViewContainer" native="true">
<property name="styleSheet">
<string notr="true">background-color: rgb(217, 183, 255)</string>
<string notr="true">QWidget#leftCefViewContainer {
background-color: rgb(217, 183, 255);
}</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_cefContainer">
<property name="spacing">
Expand All @@ -203,7 +205,9 @@
</widget>
<widget class="QWidget" name="rightCefViewContainer" native="true">
<property name="styleSheet">
<string notr="true">background-color: rgb(217, 183, 255)</string>
<string notr="true">QWidget#rightCefViewContainer {
background-color: rgb(217, 183, 255);
}</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_cefContainer_2">
<property name="spacing">
Expand Down
12 changes: 11 additions & 1 deletion src/details/QCefViewPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ QCefViewPrivate::createCefBrowser(QCefView* view, const QString& url, const QCef
if (isOSRModeEnabled_) {
auto winSize = q_ptr->size();
auto winBgColor = browserSettings.background_color;
std::shared_ptr<ICefViewRenderer> renderer;

// set QWidget background
QPalette palette = q_ptr->palette();
palette.setColor(q_ptr->backgroundRole(), //
QColor(CefColorGetR(winBgColor), //
CefColorGetG(winBgColor), //
CefColorGetB(winBgColor), //
CefColorGetA(winBgColor)) //
);
q_ptr->setPalette(palette);

std::shared_ptr<ICefViewRenderer> renderer;
#if defined(OS_WINDOWS)
// if hardware is enabled
if (setting && setting->hardwareAcceleration_) {
Expand Down
59 changes: 51 additions & 8 deletions src/win/details/render/hardware/DX11RenderBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ float4 main(VS_OUTPUT input) : SV_Target
bool
DX11RenderBackend::CreateDeviceAndSwapchain()
{
// we don't render directly to the window, but we
// use the DirectComposition to render the CefView

// Create D3D11 device and context
ComPtr<ID3D11Device> pD3dDevice;
ComPtr<ID3D11DeviceContext> pD3dContext;
#if _WIN32_WINNT >= 0x602
HR_CHECK(S_OK == ::D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
Expand Down Expand Up @@ -135,6 +134,40 @@ DX11RenderBackend::CreateDeviceAndSwapchain()
// Save
m_dcompositionTarget = pDCompositionTarget;
m_dcompositionDevice = pDecompositionDevice;
#else
// create device and swapchain
DXGI_SWAP_CHAIN_DESC sd = {};
sd.BufferCount = 1;
sd.BufferDesc.Width = m_width;
sd.BufferDesc.Height = m_height;
sd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;

sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = m_hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;

ComPtr<IDXGISwapChain> pSwapChain;
HRESULT hr = ::D3D11CreateDeviceAndSwapChain(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&sd,
pSwapChain.ReleaseAndGetAddressOf(),
pD3dDevice.ReleaseAndGetAddressOf(),
nullptr,
pD3dContext.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
return false;
}
#endif

m_swapChain = pSwapChain;
m_d3dContext = pD3dContext;
m_d3dDevice = pD3dDevice;
Expand Down Expand Up @@ -413,7 +446,7 @@ DX11RenderBackend::UpdateTextureResource(Microsoft::WRL::ComPtr<ID3D11Texture2D>

// need to recreate?
bool needRecreate = false;
if (needRecreate //
if (!pTargetTexture //
|| sharedTextureDesc.Width != targetTextureDesc.Width //
|| sharedTextureDesc.Height != targetTextureDesc.Height //
|| sharedTextureDesc.Format != targetTextureDesc.Format //
Expand Down Expand Up @@ -469,10 +502,10 @@ DX11RenderBackend::ClearTargetView()
{
// clear the back buffer (RGBA)
float clearColor[4] = {
static_cast<float>(CefColorGetR(m_backgroundColor)),
static_cast<float>(CefColorGetG(m_backgroundColor)),
static_cast<float>(CefColorGetB(m_backgroundColor)),
static_cast<float>(CefColorGetA(m_backgroundColor)),
static_cast<float>(CefColorGetR(m_backgroundColor) / 255.f),
static_cast<float>(CefColorGetG(m_backgroundColor) / 255.f),
static_cast<float>(CefColorGetB(m_backgroundColor) / 255.f),
static_cast<float>(CefColorGetA(m_backgroundColor) / 255.f),
};
m_d3dContext->ClearRenderTargetView(m_renderTargetView.Get(), clearColor);
}
Expand All @@ -495,7 +528,7 @@ DX11RenderBackend::DrawCefView()
void
DX11RenderBackend::DrawCefPopup()
{
if (!m_showPopup) {
if (!m_showPopup || !m_popupSRV) {
return;
}

Expand Down Expand Up @@ -580,6 +613,11 @@ DX11RenderBackend::initialize(void* wid, int width, int height, float scale, con
void
DX11RenderBackend::uninitialize()
{
#if _WIN32_WINNT >= 0x602
m_dcompositionDevice.Reset();
m_dcompositionTarget.Reset();
#endif

// Release resources
m_inputLayout.Reset();
m_vertexShader.Reset();
Expand Down Expand Up @@ -654,6 +692,11 @@ void
DX11RenderBackend::updatePopupVisibility(bool visible)
{
m_showPopup = visible;

if (!m_showPopup) {
m_popupTexture.Reset();
m_popupSRV.Reset();
}
}

void
Expand Down
15 changes: 12 additions & 3 deletions src/win/details/render/hardware/DX11RenderBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
#include <d3d11.h>
#include <d3d11_1.h>
#include <d3d11_2.h>
#include <dcomp.h>
#include <dxgi.h>
#include <dxgi1_2.h>
#include <wrl.h>

#if _WIN32_WINNT >= 0x602
#include <dcomp.h>
#endif

#include <mutex>

#include "details/render/ICefViewRenderer.h"
Expand All @@ -33,12 +36,18 @@ class DX11RenderBackend : public ICefViewRenderer
// lock
std::mutex m_d3dContextLock;

// device/context/swapchain
// device/context
Microsoft::WRL::ComPtr<ID3D11Device> m_d3dDevice;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_d3dContext;

// only works for windows 8
#if _WIN32_WINNT >= 0x602
Microsoft::WRL::ComPtr<IDXGISwapChain> m_swapChain;
Microsoft::WRL::ComPtr<IDCompositionDevice> m_dcompositionDevice;
Microsoft::WRL::ComPtr<IDCompositionTarget> m_dcompositionTarget;
Microsoft::WRL::ComPtr<IDXGISwapChain1> m_swapChain;
#else
Microsoft::WRL::ComPtr<IDXGISwapChain> m_swapChain;
#endif

// IA stage
Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
Expand Down

0 comments on commit 064eb1f

Please sign in to comment.