effects/zoom: Rework how cursor texture is managed

Update the cursor texture on demand to avoid changing the current opengl
context in the middle of compositing cycle.

CCBUG: 445412
This commit is contained in:
Vlad Zahorodnii 2021-11-29 13:14:56 +02:00
parent 95caa51536
commit 1f318a2245
2 changed files with 47 additions and 48 deletions

View file

@ -39,8 +39,6 @@ ZoomEffect::ZoomEffect()
, mouseTracking(MouseTrackingProportional) , mouseTracking(MouseTrackingProportional)
, mousePointer(MousePointerScale) , mousePointer(MousePointerScale)
, focusDelay(350) // in milliseconds , focusDelay(350) // in milliseconds
, imageWidth(0)
, imageHeight(0)
, isMouseHidden(false) , isMouseHidden(false)
, xMove(0) , xMove(0)
, yMove(0) , yMove(0)
@ -157,13 +155,32 @@ bool ZoomEffect::isTextCaretTrackingEnabled() const
#endif #endif
} }
GLTexture *ZoomEffect::ensureCursorTexture()
{
if (!m_cursorTexture || m_cursorTextureDirty) {
m_cursorTexture.reset();
m_cursorTextureDirty = false;
const auto cursor = effects->cursorImage();
if (!cursor.image().isNull()) {
m_cursorTexture.reset(new GLTexture(cursor.image()));
m_cursorTexture->setWrapMode(GL_CLAMP_TO_EDGE);
}
}
return m_cursorTexture.data();
}
void ZoomEffect::markCursorTextureDirty()
{
m_cursorTextureDirty = true;
}
void ZoomEffect::showCursor() void ZoomEffect::showCursor()
{ {
if (isMouseHidden) { if (isMouseHidden) {
disconnect(effects, &EffectsHandler::cursorShapeChanged, this, &ZoomEffect::recreateTexture); disconnect(effects, &EffectsHandler::cursorShapeChanged, this, &ZoomEffect::markCursorTextureDirty);
// show the previously hidden mouse-pointer again and free the loaded texture/picture. // show the previously hidden mouse-pointer again and free the loaded texture/picture.
effects->showCursor(); effects->showCursor();
texture.reset(); m_cursorTexture.reset();
isMouseHidden = false; isMouseHidden = false;
} }
} }
@ -174,38 +191,18 @@ void ZoomEffect::hideCursor()
return; // don't replace the actual cursor by a static image for no reason. return; // don't replace the actual cursor by a static image for no reason.
if (!isMouseHidden) { if (!isMouseHidden) {
// try to load the cursor-theme into a OpenGL texture and if successful then hide the mouse-pointer // try to load the cursor-theme into a OpenGL texture and if successful then hide the mouse-pointer
recreateTexture(); GLTexture *texture = nullptr;
bool shouldHide = false;
if (effects->isOpenGLCompositing()) { if (effects->isOpenGLCompositing()) {
shouldHide = !texture.isNull(); texture = ensureCursorTexture();
} }
if (shouldHide) { if (texture) {
effects->hideCursor(); effects->hideCursor();
connect(effects, &EffectsHandler::cursorShapeChanged, this, &ZoomEffect::recreateTexture); connect(effects, &EffectsHandler::cursorShapeChanged, this, &ZoomEffect::markCursorTextureDirty);
isMouseHidden = true; isMouseHidden = true;
} }
} }
} }
void ZoomEffect::recreateTexture()
{
effects->makeOpenGLContextCurrent();
const auto cursor = effects->cursorImage();
if (!cursor.image().isNull()) {
imageWidth = cursor.image().width();
imageHeight = cursor.image().height();
cursorHotSpot = cursor.hotSpot();
if (effects->isOpenGLCompositing()) {
texture.reset(new GLTexture(cursor.image()));
texture->setWrapMode(GL_CLAMP_TO_EDGE);
}
}
else {
qCDebug(KWINEFFECTS) << "Falling back to proportional mouse tracking!";
mouseTracking = MouseTrackingProportional;
}
}
void ZoomEffect::reconfigure(ReconfigureFlags) void ZoomEffect::reconfigure(ReconfigureFlags)
{ {
ZoomConfig::self()->read(); ZoomConfig::self()->read();
@ -325,29 +322,31 @@ void ZoomEffect::paintScreen(int mask, const QRegion &region, ScreenPaintData& d
effects->paintScreen(mask, region, data); effects->paintScreen(mask, region, data);
if (zoom != 1.0 && mousePointer != MousePointerHide) { if (zoom != 1.0 && mousePointer != MousePointerHide) {
// Draw the mouse-texture at the position matching to zoomed-in image of the desktop. Hiding the GLTexture *cursorTexture = ensureCursorTexture();
// previous mouse-cursor and drawing our own fake mouse-cursor is needed to be able to scale the if (cursorTexture) {
// mouse-cursor up and to re-position those mouse-cursor to match to the chosen zoom-level. const auto cursor = effects->cursorImage();
int w = imageWidth;
int h = imageHeight;
if (mousePointer == MousePointerScale) {
w *= zoom;
h *= zoom;
}
const QPoint p = effects->cursorPos() - cursorHotSpot;
QRect rect(p.x() * zoom + data.xTranslation(), p.y() * zoom + data.yTranslation(), w, h);
if (texture) { // Draw the mouse-texture at the position matching to zoomed-in image of the desktop. Hiding the
texture->bind(); // previous mouse-cursor and drawing our own fake mouse-cursor is needed to be able to scale the
// mouse-cursor up and to re-position those mouse-cursor to match to the chosen zoom-level.
QSize cursorSize = cursor.image().size() / cursor.image().devicePixelRatio();
if (mousePointer == MousePointerScale) {
cursorSize *= zoom;
}
const QPoint p = effects->cursorPos() - cursor.hotSpot();
QRect rect(p * zoom + QPoint(data.xTranslation(), data.yTranslation()), cursorSize);
cursorTexture->bind();
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
auto s = ShaderManager::instance()->pushShader(ShaderTrait::MapTexture); auto s = ShaderManager::instance()->pushShader(ShaderTrait::MapTexture);
QMatrix4x4 mvp = data.projectionMatrix(); QMatrix4x4 mvp = data.projectionMatrix();
mvp.translate(rect.x(), rect.y()); mvp.translate(rect.x(), rect.y());
s->setUniform(GLShader::ModelViewProjectionMatrix, mvp); s->setUniform(GLShader::ModelViewProjectionMatrix, mvp);
texture->render(region, rect); cursorTexture->render(region, rect);
ShaderManager::instance()->popShader(); ShaderManager::instance()->popShader();
texture->unbind(); cursorTexture->unbind();
glDisable(GL_BLEND); glDisable(GL_BLEND);
} }
} }

View file

@ -84,12 +84,14 @@ private Q_SLOTS:
Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons, Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons,
Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers); Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers);
void slotWindowDamaged(); void slotWindowDamaged();
void recreateTexture();
private: private:
void showCursor(); void showCursor();
void hideCursor(); void hideCursor();
void moveZoom(int x, int y); void moveZoom(int x, int y);
private: private:
GLTexture *ensureCursorTexture();
void markCursorTextureDirty();
#if HAVE_ACCESSIBILITY #if HAVE_ACCESSIBILITY
ZoomAccessibilityIntegration *m_accessibilityIntegration = nullptr; ZoomAccessibilityIntegration *m_accessibilityIntegration = nullptr;
#endif #endif
@ -113,14 +115,12 @@ private:
MousePointerType mousePointer; MousePointerType mousePointer;
int focusDelay; int focusDelay;
QPoint cursorPoint; QPoint cursorPoint;
QPoint cursorHotSpot;
QPoint focusPoint; QPoint focusPoint;
QPoint prevPoint; QPoint prevPoint;
QTime lastMouseEvent; QTime lastMouseEvent;
QTime lastFocusEvent; QTime lastFocusEvent;
QScopedPointer<GLTexture> texture; QScopedPointer<GLTexture> m_cursorTexture;
int imageWidth; bool m_cursorTextureDirty = false;
int imageHeight;
bool isMouseHidden; bool isMouseHidden;
QTimeLine timeline; QTimeLine timeline;
int xMove, yMove; int xMove, yMove;