commit ea217202db7c9815be25914d73cad2c8a4ddab7c
Author: Lukasz Foniok <lukasz.foniok@navico.com>
Date:   Wed Dec 4 12:54:33 2019 +1300

    HFP-4639 QtCompositor will force releasing the buffers when it hides.

diff --git a/qtwayland/src/compositor/compositor_api/qwaylandbufferref.cpp b/qtwayland/src/compositor/compositor_api/qwaylandbufferref.cpp
index ab34d12532..37917d88da 100644
--- a/qtwayland/src/compositor/compositor_api/qwaylandbufferref.cpp
+++ b/qtwayland/src/compositor/compositor_api/qwaylandbufferref.cpp
@@ -106,6 +106,11 @@ QWaylandBufferRef::~QWaylandBufferRef()
     delete d;
 }
 
+void QWaylandBufferRef::forceDref()
+{
+    d->buffer->deref(true);
+}
+
 /*!
  * Assigns \a ref to this buffer and adds a reference to it. The previously referenced buffer is
  * dereferenced.
diff --git a/qtwayland/src/compositor/compositor_api/qwaylandbufferref.h b/qtwayland/src/compositor/compositor_api/qwaylandbufferref.h
index e84d7f3a29..db51da3907 100644
--- a/qtwayland/src/compositor/compositor_api/qwaylandbufferref.h
+++ b/qtwayland/src/compositor/compositor_api/qwaylandbufferref.h
@@ -75,6 +75,7 @@ public:
     bool isDestroyed() const;
     bool operator==(const QWaylandBufferRef &ref);
     bool operator!=(const QWaylandBufferRef &ref);
+    void forceDref();
 
     struct wl_resource *wl_buffer() const;
 
diff --git a/qtwayland/src/compositor/compositor_api/qwaylandoutput.cpp b/qtwayland/src/compositor/compositor_api/qwaylandoutput.cpp
index 1f34f4eacd..73b14c3691 100644
--- a/qtwayland/src/compositor/compositor_api/qwaylandoutput.cpp
+++ b/qtwayland/src/compositor/compositor_api/qwaylandoutput.cpp
@@ -910,6 +910,17 @@ void QWaylandOutput::setWindow(QWindow *window)
     emit windowChanged();
 }
 
+void QWaylandOutput::forceDrefBuffers()
+{
+    Q_D(QWaylandOutput);
+    for (auto& surfacemapper : d->surfaceViews ) {
+        for (auto& view : surfacemapper.views) {
+            view->forceDrefCurrent();
+            view->forceDrefNext();
+        }
+    }
+}
+
 /*!
  * Informs QWaylandOutput that a frame has started.
  */
diff --git a/qtwayland/src/compositor/compositor_api/qwaylandoutput.h b/qtwayland/src/compositor/compositor_api/qwaylandoutput.h
index 31791a3c1b..064c65e203 100644
--- a/qtwayland/src/compositor/compositor_api/qwaylandoutput.h
+++ b/qtwayland/src/compositor/compositor_api/qwaylandoutput.h
@@ -154,6 +154,7 @@ public:
     bool physicalSizeFollowsSize() const;
     void setPhysicalSizeFollowsSize(bool follow);
 
+    void forceDrefBuffers();
     void frameStarted();
     void sendFrameCallbacks();
 
diff --git a/qtwayland/src/compositor/compositor_api/qwaylandquickoutput.cpp b/qtwayland/src/compositor/compositor_api/qwaylandquickoutput.cpp
index c6294eead7..693e0f4acb 100644
--- a/qtwayland/src/compositor/compositor_api/qwaylandquickoutput.cpp
+++ b/qtwayland/src/compositor/compositor_api/qwaylandquickoutput.cpp
@@ -68,6 +68,10 @@ void QWaylandQuickOutput::initialize()
 
     connect(quickWindow, &QQuickWindow::afterRendering,
             this, &QWaylandQuickOutput::doFrameCallbacks);
+
+    connect(quickWindow, &QQuickWindow::sceneGraphAboutToStop,
+            this, &QWaylandQuickOutput::forceDrefBuffers,
+            Qt::DirectConnection);
 }
 
 void QWaylandQuickOutput::classBegin()
diff --git a/qtwayland/src/compositor/compositor_api/qwaylandview.cpp b/qtwayland/src/compositor/compositor_api/qwaylandview.cpp
index 1a6bf1a648..fbaca56f68 100644
--- a/qtwayland/src/compositor/compositor_api/qwaylandview.cpp
+++ b/qtwayland/src/compositor/compositor_api/qwaylandview.cpp
@@ -260,6 +260,18 @@ bool QWaylandView::advance()
     return true;
 }
 
+void QWaylandView::forceDrefCurrent()
+{
+    Q_D(QWaylandView);
+    d->currentBuffer.forceDref();
+}
+
+void QWaylandView::forceDrefNext()
+{
+    Q_D(QWaylandView);
+    d->nextBuffer.forceDref();
+}
+
 /*!
  * Force the view to discard its current buffer, to allow it to be reused on the client side.
  */
diff --git a/qtwayland/src/compositor/compositor_api/qwaylandview.h b/qtwayland/src/compositor/compositor_api/qwaylandview.h
index c98b81053b..5d22a81701 100644
--- a/qtwayland/src/compositor/compositor_api/qwaylandview.h
+++ b/qtwayland/src/compositor/compositor_api/qwaylandview.h
@@ -78,7 +78,8 @@ public:
     virtual void discardCurrentBuffer();
     virtual QWaylandBufferRef currentBuffer();
     virtual QRegion currentDamage();
-
+    void forceDrefCurrent();
+    void forceDrefNext();
     bool isBufferLocked() const;
     void setBufferLocked(bool locked);
 
diff --git a/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer.cpp b/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer.cpp
index 7df9ead3ce..8fa8afc305 100644
--- a/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer.cpp
+++ b/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer.cpp
@@ -90,8 +90,14 @@ void ClientBuffer::ref()
     m_refCount.ref();
 }
 
-void ClientBuffer::deref()
+void ClientBuffer::deref(bool force)
 {
+    if (m_refCount == 0)
+        return;
+
+    if (force) {
+        m_refCount = 1;
+    }
     if (!m_refCount.deref()) {
         if (isCommitted() && m_buffer && !m_destroyed)
             sendRelease();
diff --git a/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer_p.h b/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer_p.h
index ac8c1ed01f..b06128036d 100644
--- a/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer_p.h
+++ b/qtwayland/src/compositor/wayland_wrapper/qwlclientbuffer_p.h
@@ -108,7 +108,11 @@ public:
 
 protected:
     void ref();
-    void deref();
+    //! deref function is used to decrease number of reference on buffer which this class is holding
+	//! when this reach 0 implementation will release the buffer. force flag allows us to skip
+	//! dereferencing one by one and release the resource on demand. Default value is false so it still
+	//! act as normal dereferencing function without parameters.
+    void deref(bool force = false);
     void sendRelease();
     void setDestroyed();
 
