Skip to content

Commit

Permalink
refactor: determine the drawing of the window border by judging wheth…
Browse files Browse the repository at this point in the history
…er the window supports the alpha channel

在窗管支持混成,但窗口色彩模式不支持alpha时,也应当将窗口按照2D模式绘制
即在此种情况下禁用窗口阴影等效果
  • Loading branch information
zccrs committed May 25, 2019
1 parent c50d83e commit a179edf
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 25 deletions.
14 changes: 7 additions & 7 deletions platformplugin/dframewindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,13 @@ void DFrameWindow::drawShadowTo(QPaintDevice *device)

pa.setCompositionMode(QPainter::CompositionMode_Source);

if (!disableFrame() && Q_LIKELY(DXcbWMSupport::instance()->hasComposite())
if (!disableFrame() && Q_LIKELY(DXcbWMSupport::instance()->hasWindowAlpha())
&& !m_shadowImage.isNull()) {
pa.drawImage(offset * device_pixel_ratio, m_shadowImage);
}

if (m_borderWidth > 0 && m_borderColor != Qt::transparent) {
if (Q_LIKELY(DXcbWMSupport::instance()->hasComposite())) {
if (Q_LIKELY(DXcbWMSupport::instance()->hasWindowAlpha())) {
pa.setRenderHint(QPainter::Antialiasing);
pa.fillPath(m_borderPath, m_borderColor);
} else {
Expand Down Expand Up @@ -920,15 +920,15 @@ void DFrameWindow::updateMask()

if (disableFrame()) {
QRegion region(m_contentGeometry * devicePixelRatio());
Utility::setShapeRectangles(winId(), region, DWMSupport::instance()->hasComposite(), flags().testFlag(Qt::WindowTransparentForInput));
Utility::setShapeRectangles(winId(), region, DWMSupport::instance()->hasWindowAlpha(), flags().testFlag(Qt::WindowTransparentForInput));

return;
}

// Set window clip mask
int mouse_margins;

if (DWMSupport::instance()->hasComposite())
if (DWMSupport::instance()->hasWindowAlpha())
mouse_margins = canResize() ? MOUSE_MARGINS : 0;
else
mouse_margins = qRound(m_borderWidth * devicePixelRatio());
Expand All @@ -948,10 +948,10 @@ void DFrameWindow::updateMask()
p = path;
}

Utility::setShapePath(winId(), p, DWMSupport::instance()->hasComposite(), flags().testFlag(Qt::WindowTransparentForInput));
Utility::setShapePath(winId(), p, DWMSupport::instance()->hasWindowAlpha(), flags().testFlag(Qt::WindowTransparentForInput));
} else {
QRegion region((m_contentGeometry * devicePixelRatio()).adjusted(-mouse_margins, -mouse_margins, mouse_margins, mouse_margins));
Utility::setShapeRectangles(winId(), region, DWMSupport::instance()->hasComposite(), flags().testFlag(Qt::WindowTransparentForInput));
Utility::setShapeRectangles(winId(), region, DWMSupport::instance()->hasWindowAlpha(), flags().testFlag(Qt::WindowTransparentForInput));
}

QPainterPathStroker stroker;
Expand All @@ -972,7 +972,7 @@ void DFrameWindow::updateFrameMask()
// if (!xw || !xw->wmWindowTypes().testFlag(QXcbWindowFunctions::Dock))
// return;

// if (!m_enableAutoFrameMask || !DWMSupport::instance()->hasComposite())
// if (!m_enableAutoFrameMask || !DWMSupport::instance()->hasWindowAlpha())
// return;

// const QRect rect(QRect(QPoint(0, 0), size()));
Expand Down
12 changes: 6 additions & 6 deletions platformplugin/dplatformbackingstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ DPlatformBackingStore::DPlatformBackingStore(QWindow *window, QXcbBackingStore *
shadowPixmap.fill(Qt::transparent);

#ifdef Q_OS_LINUX
m_enableShadow = DXcbWMSupport::instance()->hasComposite();
m_enableShadow = DXcbWMSupport::instance()->hasWindowAlpha();

QObject::connect(DXcbWMSupport::instance(), &DXcbWMSupport::hasCompositeChanged,
m_eventListener, [this, window] (bool hasComposite) {
Expand Down Expand Up @@ -600,7 +600,7 @@ void DPlatformBackingStore::flush(QWindow *window, const QRegion &region, const

pa.setCompositionMode(QPainter::CompositionMode_Source);
#ifdef Q_OS_LINUX
if (DXcbWMSupport::instance()->hasComposite())
if (DXcbWMSupport::instance()->hasWindowAlpha())
#endif
pa.setRenderHint(QPainter::Antialiasing);

Expand All @@ -624,7 +624,7 @@ void DPlatformBackingStore::flush(QWindow *window, const QRegion &region, const
QColor border_color = m_borderColor;

#ifdef Q_OS_LINUX
if (!DXcbWMSupport::instance()->hasComposite())
if (!DXcbWMSupport::instance()->hasWindowAlpha())
border_color = colorBlend(QColor("#e0e0e0"), m_borderColor);
#endif
tmp_region += QRect(QPoint(0, 0), m_size);
Expand Down Expand Up @@ -1072,14 +1072,14 @@ void DPlatformBackingStore::updateInputShapeRegion()

Utility::setShapePath(window()->winId(), p
#ifdef Q_OS_LINUX
, DXcbWMSupport::instance()->hasComposite()
, DXcbWMSupport::instance()->hasWindowAlpha()
#endif
);
} else {
QRegion region(windowGeometry().adjusted(-mouse_margins, -mouse_margins, mouse_margins, mouse_margins));
Utility::setShapeRectangles(window()->winId(), region
#ifdef Q_OS_LINUX
, DXcbWMSupport::instance()->hasComposite()
, DXcbWMSupport::instance()->hasWindowAlpha()
#endif
);
}
Expand Down Expand Up @@ -1657,7 +1657,7 @@ QWidgetWindow *DPlatformBackingStore::widgetWindow() const
int DPlatformBackingStore::getWindowRadius() const
{
#ifdef Q_OS_LINUX
return (isUserSetWindowRadius || DXcbWMSupport::instance()->hasComposite()) ? m_windowRadius : 0;
return (isUserSetWindowRadius || DXcbWMSupport::instance()->hasWindowAlpha()) ? m_windowRadius : 0;
#else
return m_windowRadius;
#endif
Expand Down
2 changes: 1 addition & 1 deletion platformplugin/dplatformbackingstorehelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void DPlatformBackingStoreHelper::flush(QWindow *window, const QRegion &region,
if (!backingStore()->paintDevice())
return;

if (Q_LIKELY(DWMSupport::instance()->hasComposite())) {
if (Q_LIKELY(DWMSupport::instance()->hasWindowAlpha())) {
DPlatformWindowHelper *window_helper = DPlatformWindowHelper::mapped.value(window->handle());

if (!window_helper)
Expand Down
2 changes: 2 additions & 0 deletions platformplugin/dplatformnativeinterfacehook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ QFunctionPointer DPlatformNativeInterfaceHook::platformFunction(QPlatformNativeI
return reinterpret_cast<QFunctionPointer>(&Utility::hasBlurWindow);
} else if (function == hasComposite) {
return reinterpret_cast<QFunctionPointer>(&Utility::hasComposite);
} else if (function == hasWindowAlpha) {
return reinterpret_cast<QFunctionPointer>(&Utility::hasWindowAlpha);
} else if (function == windowManagerName) {
return reinterpret_cast<QFunctionPointer>(&Utility::windowManagerName);
} else if (function == connectWindowManagerChangedSignal) {
Expand Down
2 changes: 1 addition & 1 deletion platformplugin/dplatformopenglcontexthelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void drawCornerImage(const QImage &source, const QPoint &source_offset, Q

void DPlatformOpenGLContextHelper::swapBuffers(QPlatformSurface *surface)
{
if (!DWMSupport::instance()->hasComposite())
if (!DWMSupport::instance()->hasWindowAlpha())
goto end;

if (surface->surface()->surfaceClass() == QSurface::Window) {
Expand Down
14 changes: 7 additions & 7 deletions platformplugin/dplatformwindowhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,17 +1061,17 @@ int DPlatformWindowHelper::getWindowRadius() const
return 0;
//#endif

return (m_isUserSetWindowRadius || DWMSupport::instance()->hasComposite()) ? m_windowRadius : 0;
return (m_isUserSetWindowRadius || DWMSupport::instance()->hasWindowAlpha()) ? m_windowRadius : 0;
}

int DPlatformWindowHelper::getShadowRadius() const
{
return DWMSupport::instance()->hasComposite() ? m_shadowRadius : 0;
return DWMSupport::instance()->hasWindowAlpha() ? m_shadowRadius : 0;
}

int DPlatformWindowHelper::getBorderWidth() const
{
if (m_isUserSetBorderWidth || DWMSupport::instance()->hasComposite())
if (m_isUserSetBorderWidth || DWMSupport::instance()->hasWindowAlpha())
return m_borderWidth;

return m_frameWindow->canResize() ? 2 : m_borderWidth;
Expand All @@ -1096,7 +1096,7 @@ static QColor colorBlend(const QColor &color1, const QColor &color2)

QColor DPlatformWindowHelper::getBorderColor() const
{
return DWMSupport::instance()->hasComposite() ? m_borderColor : colorBlend(QColor("#e0e0e0"), m_borderColor);
return DWMSupport::instance()->hasWindowAlpha() ? m_borderColor : colorBlend(QColor("#e0e0e0"), m_borderColor);
}

void DPlatformWindowHelper::updateWindowRadiusFromProperty()
Expand Down Expand Up @@ -1175,7 +1175,7 @@ void DPlatformWindowHelper::updateShadowRadiusFromProperty()
if (ok && radius != m_shadowRadius) {
m_shadowRadius = radius;

if (DWMSupport::instance()->hasComposite())
if (DWMSupport::instance()->hasWindowAlpha())
m_frameWindow->setShadowRadius(radius);
}
}
Expand Down Expand Up @@ -1373,15 +1373,15 @@ void DPlatformWindowHelper::onWMHasCompositeChanged()

updateClipPathByWindowRadius(window_size);

if (!DXcbWMSupport::instance()->hasComposite())
if (!DXcbWMSupport::instance()->hasWindowAlpha())
m_frameWindow->disableRepaintShadow();

m_frameWindow->setShadowRadius(getShadowRadius());
m_frameWindow->enableRepaintShadow();

// QPainterPath clip_path = m_clipPath * m_nativeWindow->window()->devicePixelRatio();

// if (DXcbWMSupport::instance()->hasComposite()) {
// if (DXcbWMSupport::instance()->hasWindowAlpha()) {
// QPainterPathStroker stroker;

// stroker.setJoinStyle(Qt::MiterJoin);
Expand Down
24 changes: 22 additions & 2 deletions platformplugin/dxcbwmsupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ DXcbWMSupport::DXcbWMSupport()
}
}
});

// 测试窗口visual是否支持alpha通道
QWindow test_window;
QSurfaceFormat sf = test_window.format();
sf.setDepthBufferSize(32);
sf.setAlphaBufferSize(8);
test_window.setFormat(sf);
test_window.create();
// 当窗口位深不等于32时即认为它不支持alpha通道
m_windowHasAlpha = static_cast<QXcbWindow*>(test_window.handle())->depth() == 32;
}

void DXcbWMSupport::updateWMName(bool emitSignal)
Expand Down Expand Up @@ -161,7 +171,10 @@ void DXcbWMSupport::updateHasBlurWindow()
return;

m_hasBlurWindow = hasBlurWindow;
emit hasBlurWindowChanged(hasBlurWindow);

// 当窗口visual不支持alpha通道时,也等价于不支持窗口背景模糊
if (m_windowHasAlpha)
emit hasBlurWindowChanged(hasBlurWindow);
}

void DXcbWMSupport::updateHasComposite()
Expand All @@ -181,6 +194,7 @@ void DXcbWMSupport::updateHasComposite()
return;

m_hasComposite = hasComposite;

emit hasCompositeChanged(hasComposite);
}

Expand Down Expand Up @@ -357,12 +371,18 @@ bool DXcbWMSupport::isContainsForRootWindow(xcb_atom_t atom) const

bool DXcbWMSupport::hasBlurWindow() const
{
return m_hasBlurWindow;
return m_hasBlurWindow && m_windowHasAlpha;
}

bool DXcbWMSupport::hasComposite() const
{
return m_hasComposite;
}

bool DXcbWMSupport::hasWindowAlpha() const
{
// 窗管不支持混成时也等价于窗口visual不支持alpha通道
return m_hasComposite && m_windowHasAlpha;
}

DPP_END_NAMESPACE
4 changes: 4 additions & 0 deletions platformplugin/dxcbwmsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class DXcbWMSupport : public QObject

Q_PROPERTY(bool hasBlurWindow READ hasBlurWindow NOTIFY hasBlurWindowChanged)
Q_PROPERTY(bool hasComposite READ hasComposite NOTIFY hasCompositeChanged)
// 属性值只有可能在窗管是否支持混成时发生改变
Q_PROPERTY(bool hasWindowAlpha READ hasWindowAlpha FINAL)

public:
enum {
Expand Down Expand Up @@ -88,6 +90,7 @@ class DXcbWMSupport : public QObject
bool isContainsForRootWindow(xcb_atom_t atom) const;
bool hasBlurWindow() const;
bool hasComposite() const;
bool hasWindowAlpha() const;

QString windowManagerName() const;

Expand Down Expand Up @@ -116,6 +119,7 @@ class DXcbWMSupport : public QObject
bool m_isKwin = false;
bool m_hasBlurWindow = false;
bool m_hasComposite = false;
bool m_windowHasAlpha = true;

QString m_wmName;

Expand Down
1 change: 1 addition & 0 deletions platformplugin/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ DEFINE_CONST_CHAR(setWmBlurWindowBackgroundPathList);
DEFINE_CONST_CHAR(setWmBlurWindowBackgroundMaskImage);
DEFINE_CONST_CHAR(hasBlurWindow);
DEFINE_CONST_CHAR(hasComposite);
DEFINE_CONST_CHAR(hasWindowAlpha);
DEFINE_CONST_CHAR(windowManagerName);
DEFINE_CONST_CHAR(connectWindowManagerChangedSignal);
DEFINE_CONST_CHAR(connectHasBlurWindowChanged);
Expand Down
1 change: 1 addition & 0 deletions platformplugin/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Utility
// by Deepin Window Manager
static bool hasBlurWindow();
static bool hasComposite();
static bool hasWindowAlpha();
static QString windowManagerName();
static bool blurWindowBackground(const quint32 WId, const QVector<BlurArea> &areas);
static bool blurWindowBackgroundByPaths(const quint32 WId, const QList<QPainterPath> &paths);
Expand Down
10 changes: 9 additions & 1 deletion platformplugin/utility_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,15 @@ bool Utility::hasBlurWindow()

bool Utility::hasComposite()
{
return DXcbWMSupport::instance()->hasComposite();
// 为了兼容现有的dtk应用中的逻辑,此处默认认为窗管是否支持混成等价于窗口是否支持alpha通道
static bool composite_with_alpha = qgetenv("D_DXCB_COMPOSITE_WITH_WINDOW_ALPHA") != "0";

return composite_with_alpha ? hasWindowAlpha() : DXcbWMSupport::instance()->hasComposite();
}

bool Utility::hasWindowAlpha()
{
return DXcbWMSupport::instance()->hasWindowAlpha();
}

QString Utility::windowManagerName()
Expand Down

0 comments on commit a179edf

Please sign in to comment.