Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/reform ncw #306

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,11 @@ MainWindow::createRightCefView()
// create the QCefView widget and add it to the layout container
// m_pRightCefViewWidget = new CefViewWidget("https://cefview.github.io/QCefView/", &setting, this);

// this site is for test web events
m_pRightCefViewWidget = new CefViewWidget("", &setting, this);
m_pRightCefViewWidget->navigateToUrl(
"https://www.javatpoint.com/oprweb/test.jsp?filename=javascript-window-close-method1");

//
// m_pRightCefViewWidget = new CefViewWidget("https://mdn.dev/", &setting, this);

// this site is for test OSR performance
// m_pRightCefViewWidget = new CefViewWidget("https://www.testufo.com", &setting, this);
m_pRightCefViewWidget = new CefViewWidget("https://www.testufo.com", &setting, this);

// this site is test for input devices
// m_pRightCefViewWidget = new CefViewWidget("https://devicetests.com", &setting);
Expand Down
6 changes: 0 additions & 6 deletions include/QCefView.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,6 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// <param name="urls">The urls</param>
void faviconURLChanged(const QStringList& urls);

/// <summary>
/// Gets called on favicon changed
/// </summary>
/// <param name="icon">The icon</param>
void faviconChanged(const QIcon& icon);

/// <summary>
/// Gets called on fullscreen mode changed
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ QCefView supports CEF offscreen rendering mode and this feature is enabled by de
config.setWindowlessRenderingEnabled(false)
```

QCefView currently uses Qt Software Rasterizer as the backend paint engine for best compatibility, thus the rendering performance is not so good to be applied for all business scenarios. If you are going to host WegGL games, video sites or some other Web Apps with high rendering performance, Non OSR mode (Native Child Window mode) is recommended.
QCefView currently uses Qt Software Rasterizer as the backend paint engine for best compatibility, thus the rendering performance is not so good to be applied for all business scenarios. If you want to host WebGL games, video sites or some other Web Apps with high rendering performance, Non OSR mode (Native Child Window mode) is recommended.

## Question
### Why not OpenGL/DirectX/Metal?
Expand Down
5 changes: 0 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ if(OS_LINUX)
BUILD_WITH_INSTALL_RPATH TRUE
)

target_link_libraries(QCefView
PRIVATE
Qt${QT_VERSION_MAJOR}::GuiPrivate
)

add_custom_command(TARGET QCefView
PRE_BUILD

Expand Down
1 change: 1 addition & 0 deletions src/QCefView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ QCefView::onNewBrowser(qint64 sourceFrameId,
if (!name.isEmpty()) {
popup->setWindowTitle(name);
}
popup->setAttribute(Qt::WA_DeleteOnClose, true);
popup->resize(rect.size());
popup->show();

Expand Down
7 changes: 7 additions & 0 deletions src/details/CCefAppDelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "CCefAppDelegate.h"

#include <QDebug>

#include <CefViewCoreProtocol.h>

#include "QCefContextPrivate.h"
Expand All @@ -10,6 +12,11 @@ CCefAppDelegate::CCefAppDelegate(QCefContextPrivate* context, CommandLineArgs ar
{
}

CCefAppDelegate::~CCefAppDelegate()
{
qDebug() << "CCefAppDelegate is being destructed";
}

void
CCefAppDelegate::onBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line)
{
Expand Down
2 changes: 2 additions & 0 deletions src/details/CCefAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CCefAppDelegate : public CefViewBrowserAppDelegateInterface
public:
CCefAppDelegate(QCefContextPrivate* context, CommandLineArgs args);

~CCefAppDelegate();

virtual void onBeforeCommandLineProcessing(const CefString& process_type,
CefRefPtr<CefCommandLine> command_line) override;

Expand Down
3 changes: 2 additions & 1 deletion src/details/CCefClientDelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "CCefClientDelegate.h"

#include <QDebug>
#include <QImage>
#include <QScreen>

Expand All @@ -14,7 +15,7 @@ CCefClientDelegate::CCefClientDelegate(QCefViewPrivate* p)

CCefClientDelegate::~CCefClientDelegate()
{
return;
qDebug() << "CCefClientDelegate is being destructed";
}

void
Expand Down
1 change: 0 additions & 1 deletion src/details/CCefClientDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class CCefClientDelegate
virtual void addressChanged(CefRefPtr<CefBrowser>& browser, int64_t frameId, const std::string& url) override;
virtual void titleChanged(CefRefPtr<CefBrowser>& browser, const std::string& title) override;
virtual void faviconURLChanged(CefRefPtr<CefBrowser> browser, const std::vector<CefString>& icon_urls) override;
virtual void faviconChanged(CefRefPtr<CefImage> image) override;
virtual void fullscreenModeChanged(CefRefPtr<CefBrowser>& browser, bool fullscreen) override;
virtual bool tooltipMessage(CefRefPtr<CefBrowser>& browser, const std::string& text) override;
virtual void statusMessage(CefRefPtr<CefBrowser>& browser, const std::string& value) override;
Expand Down
22 changes: 0 additions & 22 deletions src/details/CCefClientDelegate_DisplayHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,6 @@ CCefClientDelegate::faviconURLChanged(CefRefPtr<CefBrowser> browser, const std::
pCefViewPrivate_->q_ptr->faviconURLChanged(urls);
}

void
CCefClientDelegate::faviconChanged(CefRefPtr<CefImage> image)
{
QIcon icon;

int width = 0;
int height = 0;
CefRefPtr<CefBinaryValue> data = image->GetAsPNG(1.0, true, width, height);
int bufsize = (int)data->GetSize();
if (bufsize > 0) {
QByteArray buffer(bufsize + 4, char(0));
data->GetData(buffer.data(), bufsize, 0);

QBitmap bitmap;
bitmap.loadFromData(buffer);

icon = QIcon(bitmap);
}

pCefViewPrivate_->q_ptr->faviconChanged(icon);
}

void
CCefClientDelegate::fullscreenModeChanged(CefRefPtr<CefBrowser>& browser, bool fullscreen)
{
Expand Down
114 changes: 55 additions & 59 deletions src/details/QCefViewPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,15 @@ QCefViewPrivate::createCefBrowser(QCefView* view, const QString url, const QCefS
if (isOSRModeEnabled_) {
windowInfo.SetAsWindowless(0);
} else { // #else
#if defined(Q_OS_LINUX)
// Don't know why, on Linux platform if we use QCefView's winId() as
// the parent, it will complain about `BadWindow`,
// and the browser window will not be created, this never happens
// on Windows and macOS, so we create a temporal QWindow as the
// parent to create CEF browser window.
QWindow* win = new QWindow();
CefWindowHandle parent = (CefWindowHandle)win->winId();
#else
CefWindowHandle parent = (CefWindowHandle)view->winId();
#endif
// create CEF browser parent window
ncw.qBrowserWindow_ = new QCefWindow();
ncw.qBrowserWindow_->setFlags(Qt::Window | Qt::FramelessWindowHint);

// use INT_MAX as the width and height to prevent black screen blink
#if CEF_VERSION_MAJOR > 85
windowInfo.SetAsChild(parent, { 0, 0, 0, 0 });
windowInfo.SetAsChild((CefWindowHandle)ncw.qBrowserWindow_->winId(), { 0, 0, INT_MAX, INT_MAX });
#else
windowInfo.SetAsChild(parent, 0, 0, 0, 0);
windowInfo.SetAsChild((CefWindowHandle)ncw.qBrowserWindow_->winId(), 0, 0, INT_MAX, INT_MAX);
#endif
}
// #endif
Expand Down Expand Up @@ -142,13 +135,10 @@ QCefViewPrivate::destroyCefBrowser()
if (!pClient_)
return;

// #if !defined(CEF_USE_OSR)
if (!isOSRModeEnabled_) {
// remove parent, or CEF will send close to the parent
// this will lead the top level window to be closed
ncw.qBrowserWindow_->setParent(nullptr);
// remove from children, prevent from being destroyed
ncw.qBrowserWindow_->detachCefWindow();
}
// #endif

// clean all browsers
pClient_->CloseAllBrowsers();
Expand Down Expand Up @@ -223,33 +213,40 @@ QCefViewPrivate::onCefBrowserCreated(CefRefPtr<CefBrowser> browser, QWindow* win
// emit signal
q_ptr->nativeBrowserCreated(window);

qDebug() << "CEF Window Native ID:" << window->winId();

// adjust size and attach to cef window
ncw.qBrowserWindow_->resize(q_ptr->size());
ncw.qBrowserWindow_->attachCefWindow(window);

// create QWidget from cef browser widow, this will re-parent the CEF browser window
QWidget* browserWidget = QWidget::createWindowContainer(
window,
q_ptr,
Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::WindowTransparentForInput | Qt::WindowDoesNotAcceptFocus);
ncw.qBrowserWindow_, q_ptr, Qt::WindowTransparentForInput | Qt::WindowDoesNotAcceptFocus);

Q_ASSERT_X(browserWidget, "QCefViewPrivateNCW::createBrowser", "Failed to create QWidget from cef browser window");
if (!browserWidget) {
qWarning("Failed to create QWidget from cef browser window");
qWarning() << "Failed to create QWidget from cef browser window";
browser->GetHost()->CloseBrowser(true);
return;
}

// capture the resource
ncw.qBrowserWindow_ = window;
ncw.qBrowserWidget_ = browserWidget;
ncw.qBrowserWidget_->resize(q_ptr->size());

// monitor the focus changed event globally
connect(qApp, &QApplication::focusChanged, this, &QCefViewPrivate::onAppFocusChanged);

// initialize the layout and add browser widget to the layout
auto* layout = new QGridLayout();
QGridLayout* layout = new QGridLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(ncw.qBrowserWidget_);
q_ptr->setLayout(layout);

// update mask
UpdateCefWindowMask(ncw.qBrowserWindow_, q_ptr->mask());
if (ncw.qBrowserWindow_) {
ncw.qBrowserWindow_->setMask(q_ptr->mask());
}
}
// #endif
}
Expand Down Expand Up @@ -371,7 +368,8 @@ QCefViewPrivate::onViewScreenChanged(QScreen* screen)
pCefBrowser_->GetHost()->NotifyScreenInfoChanged();
} else { // #else
Q_Q(QCefView);
UpdateCefWindowMask(ncw.qBrowserWindow_, q->mask());
if (ncw.qBrowserWindow_)
ncw.qBrowserWindow_->setMask(q->mask());
}
// #endif
}
Expand Down Expand Up @@ -489,15 +487,15 @@ QCefViewPrivate::onContextMenuDestroyed(QObject* obj)
void
QCefViewPrivate::onOsrUpdateViewFrame(const QImage& frame, const QRegion& region)
{
// #if defined(QT_DEBUG)
// qint64 elapsedMs = paintTimer_.restart();
// // qDebug() << "===== CEF view frame update since last frame:" << elapsedMs;
// if (elapsedMs >= 20)
// qDebug() << "===== CEF view frame update stutter detected:" << elapsedMs;
//
// QElapsedTimer updateDurationTimer;
// updateDurationTimer.start();
// #endif
//#if defined(QT_DEBUG)
// qint64 elapsedMs = paintTimer_.restart();
// // qDebug() << "===== CEF view frame update since last frame:" << elapsedMs;
// if (elapsedMs >= 20)
// qDebug() << "===== CEF view frame update stutter detected:" << elapsedMs;
//
// QElapsedTimer updateDurationTimer;
// updateDurationTimer.start();
//#endif

if (osr.qCefViewFrame_.size() != frame.size() || osr.transparentPaintingEnabled) {
// update full image
Expand All @@ -513,9 +511,9 @@ QCefViewPrivate::onOsrUpdateViewFrame(const QImage& frame, const QRegion& region
}
emit updateOsrFrame();

// #if defined(QT_DEBUG)
// qDebug() << "===== CEF frame update duration:" << elapsedMs;
// #endif
//#if defined(QT_DEBUG)
// qDebug() << "===== CEF frame update duration:" << elapsedMs;
//#endif
}

void
Expand Down Expand Up @@ -637,7 +635,6 @@ QCefViewPrivate::eventFilter(QObject* watched, QEvent* event)
}
#endif

// #if defined(CEF_USE_OSR)
if (isOSRModeEnabled_) {
// if the parent chain changed, we need to re-connect the screenChanged signal
if (et == QEvent::ParentChange) {
Expand All @@ -662,17 +659,18 @@ QCefViewPrivate::eventFilter(QObject* watched, QEvent* event)
return true;
}
}
} else { // #else
// filter event to the browser window
if (watched == ncw.qBrowserWindow_ && et == QEvent::PlatformSurface) {
} else {
// the surface is about to be destroyed (top-level window is being closed)
if (et == QEvent::PlatformSurface) {
auto t = ((QPlatformSurfaceEvent*)event)->surfaceEventType();
if (QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed == t) {
// browser window is being destroyed, need to close the browser window in advance
destroyCefBrowser();
if (watched == ncw.qBrowserWindow_->cefWindow()) {
// detach the cef window
ncw.qBrowserWindow_->detachCefWindow();
}
}
}
}
// #endif

return QObject::eventFilter(watched, event);
}
Expand Down Expand Up @@ -764,11 +762,6 @@ QCefViewPrivate::onViewVisibilityChanged(bool visible)
return;

if (visible) {
#if defined(Q_OS_LINUX)
XRemapWindow(ncw.qBrowserWidget_, ncw.qBrowserWindow_);
// BUG-TO-BE-FIXED after remap, the browser window will not resize automatically
// with the QCefView widget
#endif
// restore cef window size
ncw.qBrowserWidget_->resize(q->frameSize());
} else {
Expand Down Expand Up @@ -806,7 +799,8 @@ QCefViewPrivate::onViewSizeChanged(const QSize& size, const QSize& oldSize)
pCefBrowser_->GetHost()->WasResized();
} else { // #else
Q_Q(QCefView);
UpdateCefWindowMask(ncw.qBrowserWindow_, q->mask());
if (ncw.qBrowserWindow_)
ncw.qBrowserWindow_->setMask(q->mask());
}
// #endif
}
Expand All @@ -819,14 +813,16 @@ QCefViewPrivate::onViewKeyEvent(QKeyEvent* event)
if (!pCefBrowser_)
return;

// qDebug("==== onViewKeyEvent:key=%d, nativeVirtualKey=0x%02x, nativeScanCode=0x%02x, modifiers=0x%08x, "
// "nativeModifiers=0x%08x, text=%s",
// (Qt::Key)(event->key()),
// event->nativeVirtualKey(),
// event->nativeScanCode(),
// (quint32)(event->modifiers()),
// event->nativeModifiers(),
// event->text().toStdString().c_str());
//#if defined(QT_DEBUG)
// qDebug("==== onViewKeyEvent:key=%d, nativeVirtualKey=0x%02x, nativeScanCode=0x%02x, modifiers=0x%08x, "
// "nativeModifiers=0x%08x, text=%s",
// (Qt::Key)(event->key()),
// event->nativeVirtualKey(),
// event->nativeScanCode(),
// (quint32)(event->modifiers()),
// event->nativeModifiers(),
// event->text().toStdString().c_str());
//#endif

CefKeyEvent e;
MapQKeyEventToCefKeyEvent(event, e);
Expand Down
6 changes: 4 additions & 2 deletions src/details/QCefViewPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <QMutex>
#include <QPointer>
#include <QString>
#include <QWindow>

#if defined(QT_DEBUG)
#include <QElapsedTimer>
Expand All @@ -16,10 +15,13 @@

#include "CCefClientDelegate.h"
#include "QCefContextPrivate.h"
#include "QCefWindow.h"
#include "utils/MenuBuilder.h"

#include <QCefView.h>

class QAutoResizeWindow;

class QCefViewPrivate : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -145,7 +147,7 @@ class QCefViewPrivate : public QObject
/// <summary>
///
/// </summary>
QWindow* qBrowserWindow_ = nullptr;
QCefWindow* qBrowserWindow_ = nullptr;

/// <summary>
///
Expand Down
Loading
Loading