Skip to content

Commit

Permalink
fix: [vnc]Fix display incorrect image issue
Browse files Browse the repository at this point in the history
1. Skip the first image which may incomplete. 2. Add screen size changed callback in order to handle target phone rotated.

Log: Fix display incorrect image issue.
  • Loading branch information
re2zero committed Jan 17, 2025
1 parent b8ea357 commit d5800f7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ typedef char* (*GetUserProc)(struct _rfbClient* client);
typedef char* (*GetSASLMechanismProc)(struct _rfbClient* client, char* mechlist);
#endif /* LIBVNCSERVER_HAVE_SASL */

/** Callback when the screen size of the client has changed. */
typedef void (*ScreenSizeChangedProc)(struct _rfbClient* client, int width, int height);

typedef struct _rfbClient {
uint8_t* frameBuffer;
int width, height;
Expand Down Expand Up @@ -453,6 +456,9 @@ typedef struct _rfbClient {

#endif /* LIBVNCSERVER_HAVE_SASL */

/** screen size changed nofication */
ScreenSizeChangedProc ScreenSizeChanged;

#ifdef LIBVNCSERVER_HAVE_LIBZ
#ifdef LIBVNCSERVER_HAVE_LIBJPEG
/** JPEG decoder state. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,7 @@ HandleRFBServerMessage(rfbClient* client)
return FALSE;
}
rfbClientLog("Updated desktop size: %dx%d\n", rect.r.w, rect.r.h);
client->ScreenSizeChanged(client, rect.r.w, rect.r.h);
}
client->requestedResize = FALSE;

Expand Down
16 changes: 16 additions & 0 deletions src/lib/cooperation/core/gui/phone/vncrecvthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "vncrecvthread.h"

bool VNCRecvThread::_skipFirst = false;

VNCRecvThread::VNCRecvThread(QObject *parent): QThread(parent)
{
Expand All @@ -17,6 +18,7 @@ void VNCRecvThread::startRun(rfbClient *cl)

_cl = cl;
_cl->FinishedFrameBufferUpdate = frameBufferUpdated;
_cl->ScreenSizeChanged = screenSizeChanged;
rfbClientSetClientData(_cl, nullptr, this);
_runFlag = true;

Expand All @@ -29,9 +31,11 @@ void VNCRecvThread::stopRun()
return;

_runFlag = false;
_skipFirst = false;
if (_cl) {
rfbClientSetClientData(_cl, nullptr, nullptr);
_cl->FinishedFrameBufferUpdate = nullptr;
_cl->ScreenSizeChanged = nullptr;
}
}

Expand All @@ -52,7 +56,19 @@ void VNCRecvThread::run()

void VNCRecvThread::frameBufferUpdated(rfbClient *cl)
{
if (!_skipFirst) {
// skip the first image buffer which may be incomplete
_skipFirst = true;
return;
}
VNCRecvThread *vncRecvThread = static_cast<VNCRecvThread *>(rfbClientGetClientData(cl, nullptr));

emit vncRecvThread->updateImageSignal(QImage(cl->frameBuffer, cl->width, cl->height, QImage::Format_RGBA8888));
}

void VNCRecvThread::screenSizeChanged(rfbClient *cl, int width, int height)
{
VNCRecvThread *vncRecvThread = static_cast<VNCRecvThread *>(rfbClientGetClientData(cl, nullptr));

emit vncRecvThread->sizeChangedSignal(width, height);
}
4 changes: 4 additions & 0 deletions src/lib/cooperation/core/gui/phone/vncrecvthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ class VNCRecvThread : public QThread
void stopRun();

static void frameBufferUpdated(rfbClient *cl);
static void screenSizeChanged(rfbClient *cl, int width, int height);

signals:
void updateImageSignal(QImage);
void sizeChangedSignal(int width, int height);

protected:
void run() override;

private:
bool _runFlag = false;
rfbClient *_cl;

static bool _skipFirst; // skip the first image
};

#endif // VNCRECVTHREAD_H
16 changes: 13 additions & 3 deletions src/lib/cooperation/core/gui/phone/vncviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ VncViewer::VncViewer(QWidget *parent)
m_scaled(true),
m_buttonMask(0)
{
//init the background color
m_backgroundBrush = QBrush(Qt::black);

setFocusPolicy(Qt::StrongFocus);
#ifdef TOUCH_MODE
setMouseTracking(false);
Expand All @@ -44,6 +47,7 @@ VncViewer::VncViewer(QWidget *parent)

_vncRecvThread = new VNCRecvThread(this);
connect(_vncRecvThread, &VNCRecvThread::updateImageSignal, this, &VncViewer::updateImage, Qt::BlockingQueuedConnection);
connect(_vncRecvThread, &VNCRecvThread::sizeChangedSignal, this, &VncViewer::onSizeChange, Qt::QueuedConnection);
connect(_vncRecvThread, &VNCRecvThread::finished, this, &VncViewer::stop);

m_frameTimer = new QTimer(this);
Expand Down Expand Up @@ -72,16 +76,19 @@ void VncViewer::frameTimerTimeout()
#ifdef QT_DEBUG
qWarning() << " FPS: " << currentFps();
#endif
}

void VncViewer::onSizeChange(int width, int height)
{
if (!m_connected)
return;

// Check the screen has been rotated
int curentMode = (m_rfbCli->width < m_rfbCli->height) ? PORTRAIT : LANDSCAPE;
int curentMode = (width < height) ? PORTRAIT : LANDSCAPE;
if (curentMode != m_phoneMode) {
m_phoneMode = curentMode;
int w = (m_phoneMode == PORTRAIT) ? m_realSize.width() : m_realSize.height();
const QSize size = {static_cast<int>(w * m_phoneScale), m_rfbCli->height};
const QSize size = {static_cast<int>(w * m_phoneScale), height};

setSurfaceSize(size);
emit sizeChanged(size);
Expand Down Expand Up @@ -154,6 +161,9 @@ void VncViewer::updateImage(const QImage &image)
void VncViewer::paintEvent(QPaintEvent *event)
{
if (m_connected) {
if (m_image.isNull())
return;

m_painter.begin(&m_surfacePixmap);
// if (m_image.hasAlphaChannel()) {
// // 设置合成模式为源模式
Expand All @@ -169,7 +179,7 @@ void VncViewer::paintEvent(QPaintEvent *event)

m_painter.begin(this);
m_painter.setRenderHints(QPainter::SmoothPixmapTransform);
m_painter.fillRect(rect(), m_backgroundBrush);
m_painter.fillRect(rect(), backgroundBrush());
if (scaled()) {
m_surfaceRect.moveCenter(rect().center());
m_painter.scale(m_scale, m_scale);
Expand Down
1 change: 1 addition & 0 deletions src/lib/cooperation/core/gui/phone/vncviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class VncViewer : public QWidget

public slots:
void frameTimerTimeout();
void onSizeChange(int width, int height);
void onShortcutAction(int action);

void updateSurface();
Expand Down

0 comments on commit d5800f7

Please sign in to comment.