libkwineffects: Add alpha channel option in OffscreenQuickView

We want some quick views to be opaque, for example SceneEffect views.
This changes adds an api allowing to specify whether the given
OffscreenQuickView must be opaque or translucent.
master
Vlad Zahorodnii 11 months ago
parent 22dfc8978e
commit 7478c81a48

@ -1497,10 +1497,17 @@ void EffectsHandlerImpl::renderOffscreenQuickView(const RenderTarget &renderTarg
} }
shader->setColorspaceUniformsFromSRGB(renderTarget.colorDescription()); shader->setColorspaceUniformsFromSRGB(renderTarget.colorDescription());
glEnable(GL_BLEND); const bool alphaBlending = w->hasAlphaChannel() || (a != 1.0);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); if (alphaBlending) {
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
t->render(w->size(), viewport.scale()); t->render(w->size(), viewport.scale());
glDisable(GL_BLEND);
if (alphaBlending) {
glDisable(GL_BLEND);
}
ShaderManager::instance()->popShader(); ShaderManager::instance()->popShader();
} else if (compositingType() == QPainterCompositing) { } else if (compositingType() == QPainterCompositing) {

@ -49,6 +49,7 @@ public:
// Used for either software QtQuick rendering and nonGL kwin rendering // Used for either software QtQuick rendering and nonGL kwin rendering
bool m_useBlit = false; bool m_useBlit = false;
bool m_visible = true; bool m_visible = true;
bool m_hasAlphaChannel = true;
bool m_automaticRepaint = true; bool m_automaticRepaint = true;
QList<QEventPoint> touchPoints; QList<QEventPoint> touchPoints;
@ -73,7 +74,7 @@ public:
std::unique_ptr<QQuickItem> quickItem; std::unique_ptr<QQuickItem> quickItem;
}; };
OffscreenQuickView::OffscreenQuickView(ExportMode exportMode) OffscreenQuickView::OffscreenQuickView(ExportMode exportMode, bool alpha)
: d(new OffscreenQuickView::Private) : d(new OffscreenQuickView::Private)
{ {
d->m_renderControl = std::make_unique<QQuickRenderControl>(); d->m_renderControl = std::make_unique<QQuickRenderControl>();
@ -83,6 +84,7 @@ OffscreenQuickView::OffscreenQuickView(ExportMode exportMode)
d->m_view->setFlags(Qt::FramelessWindowHint); d->m_view->setFlags(Qt::FramelessWindowHint);
d->m_view->setColor(Qt::transparent); d->m_view->setColor(Qt::transparent);
d->m_hasAlphaChannel = alpha;
if (exportMode == ExportMode::Image) { if (exportMode == ExportMode::Image) {
d->m_useBlit = true; d->m_useBlit = true;
} }
@ -98,6 +100,9 @@ OffscreenQuickView::OffscreenQuickView(ExportMode exportMode)
format.setOption(QSurfaceFormat::ResetNotification); format.setOption(QSurfaceFormat::ResetNotification);
format.setDepthBufferSize(16); format.setDepthBufferSize(16);
format.setStencilBufferSize(8); format.setStencilBufferSize(8);
if (alpha) {
format.setAlphaBufferSize(8);
}
d->m_view->setFormat(format); d->m_view->setFormat(format);
@ -211,7 +216,12 @@ void OffscreenQuickView::update()
const QSize nativeSize = d->m_view->size() * d->m_view->effectiveDevicePixelRatio(); const QSize nativeSize = d->m_view->size() * d->m_view->effectiveDevicePixelRatio();
if (!d->m_fbo || d->m_fbo->size() != nativeSize) { if (!d->m_fbo || d->m_fbo->size() != nativeSize) {
d->m_textureExport.reset(nullptr); d->m_textureExport.reset(nullptr);
d->m_fbo = std::make_unique<QOpenGLFramebufferObject>(nativeSize, QOpenGLFramebufferObject::CombinedDepthStencil);
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
fboFormat.setInternalTextureFormat(d->m_hasAlphaChannel ? GL_RGBA8 : GL_RGB8);
d->m_fbo = std::make_unique<QOpenGLFramebufferObject>(nativeSize, fboFormat);
if (!d->m_fbo->isValid()) { if (!d->m_fbo->isValid()) {
d->m_fbo.reset(); d->m_fbo.reset();
d->m_glcontext->doneCurrent(); d->m_glcontext->doneCurrent();
@ -361,6 +371,11 @@ qreal OffscreenQuickView::opacity() const
return d->m_view->opacity(); return d->m_view->opacity();
} }
bool OffscreenQuickView::hasAlphaChannel() const
{
return d->m_hasAlphaChannel;
}
QQuickItem *OffscreenQuickView::contentItem() const QQuickItem *OffscreenQuickView::contentItem() const
{ {
return d->m_view->contentItem(); return d->m_view->contentItem();
@ -514,8 +529,8 @@ void OffscreenQuickView::Private::updateTouchState(Qt::TouchPointState state, qi
} }
} }
OffscreenQuickScene::OffscreenQuickScene(OffscreenQuickView::ExportMode exportMode) OffscreenQuickScene::OffscreenQuickScene(OffscreenQuickView::ExportMode exportMode, bool alpha)
: OffscreenQuickView(exportMode) : OffscreenQuickView(exportMode, alpha)
, d(new OffscreenQuickScene::Private) , d(new OffscreenQuickScene::Private)
{ {
} }

@ -55,9 +55,10 @@ public:
}; };
/** /**
* Construct a new KWinQuickView explicitly stating an export mode * Construct a new KWinQuickView explicitly stating an export mode. \a alpha indicates
* whether the view is translucent or not.
*/ */
explicit OffscreenQuickView(ExportMode exportMode = ExportMode::Texture); explicit OffscreenQuickView(ExportMode exportMode = ExportMode::Texture, bool alpha = true);
/** /**
* Note that this may change the current GL Context * Note that this may change the current GL Context
@ -75,6 +76,7 @@ public:
void setOpacity(qreal opacity); void setOpacity(qreal opacity);
qreal opacity() const; qreal opacity() const;
bool hasAlphaChannel() const;
/** /**
* Render the current scene graph into the FBO. * Render the current scene graph into the FBO.
@ -156,7 +158,7 @@ private:
class KWIN_EXPORT OffscreenQuickScene : public OffscreenQuickView class KWIN_EXPORT OffscreenQuickScene : public OffscreenQuickView
{ {
public: public:
explicit OffscreenQuickScene(ExportMode exportMode = ExportMode::Texture); explicit OffscreenQuickScene(ExportMode exportMode = ExportMode::Texture, bool alpha = true);
~OffscreenQuickScene(); ~OffscreenQuickScene();
/** top level item in the given source*/ /** top level item in the given source*/

Loading…
Cancel
Save