f325b2
From d52f669ccaad0522c52716779acddd2715be440e Mon Sep 17 00:00:00 2001
f325b2
From: Pranav Kant <pranavk@gnome.org>
f325b2
Date: Sun, 19 Jul 2015 01:03:56 +0530
f325b2
Subject: [PATCH 076/398] lokdocview: Make paintTile() async
f325b2
f325b2
Change-Id: I57db9e3adf26996e6e1e105b8b95f53e88e7760f
f325b2
(cherry picked from commit 4edbf5a01fb8d93f3e6f2b9f7100a0c3d2eafa6e)
f325b2
---
f325b2
 libreofficekit/source/gtk/lokdocview.cxx | 12 ++++-
f325b2
 libreofficekit/source/gtk/tilebuffer.cxx | 91 ++++++++++++++++++++------------
f325b2
 libreofficekit/source/gtk/tilebuffer.hxx | 28 ++++++++--
f325b2
 3 files changed, 91 insertions(+), 40 deletions(-)
f325b2
f325b2
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
f325b2
index 46b031950e8a..0740e04dd6c8 100644
f325b2
--- a/libreofficekit/source/gtk/lokdocview.cxx
f325b2
+++ b/libreofficekit/source/gtk/lokdocview.cxx
f325b2
@@ -520,7 +520,7 @@ setTilesInvalid (LOKDocView* pDocView, const GdkRectangle& rRectangle)
f325b2
 
f325b2
     for (int i = aStart.x; i < aEnd.x; i++)
f325b2
         for (int j = aStart.y; j < aEnd.y; j++)
f325b2
-            priv->m_aTileBuffer.setInvalid(i, j);
f325b2
+            priv->m_aTileBuffer.setInvalid(i, j, priv->m_fZoom);
f325b2
 }
f325b2
 
f325b2
 static gboolean
f325b2
@@ -741,6 +741,12 @@ renderGraphicHandle(LOKDocView* pDocView,
f325b2
     }
f325b2
 }
f325b2
 
f325b2
+static void
f325b2
+renderDocumentCallback(GObject* source_object, GAsyncResult*, gpointer)
f325b2
+{
f325b2
+    LOKDocView* pDocView = LOK_DOC_VIEW(source_object);
f325b2
+    gtk_widget_queue_draw(GTK_WIDGET(pDocView));
f325b2
+}
f325b2
 
f325b2
 static gboolean
f325b2
 renderDocument(LOKDocView* pDocView, cairo_t* pCairo)
f325b2
@@ -790,7 +796,9 @@ renderDocument(LOKDocView* pDocView, cairo_t* pCairo)
f325b2
 
f325b2
             if (bPaint)
f325b2
             {
f325b2
-                Tile& currentTile = priv->m_aTileBuffer.getTile(nRow, nColumn, priv->m_fZoom);
f325b2
+                GTask* task = g_task_new(pDocView, NULL, renderDocumentCallback, NULL);
f325b2
+                Tile& currentTile = priv->m_aTileBuffer.getTile(nRow, nColumn, priv->m_fZoom, task);
f325b2
+
f325b2
                 GdkPixbuf* pPixBuf = currentTile.getBuffer();
f325b2
                 gdk_cairo_set_source_pixbuf (pCairo, pPixBuf,
f325b2
                                              twipToPixel(aTileRectangleTwips.x, priv->m_fZoom),
f325b2
diff --git a/libreofficekit/source/gtk/tilebuffer.cxx b/libreofficekit/source/gtk/tilebuffer.cxx
f325b2
index 60aa16f6c50a..d488f8b0516c 100644
f325b2
--- a/libreofficekit/source/gtk/tilebuffer.cxx
f325b2
+++ b/libreofficekit/source/gtk/tilebuffer.cxx
f325b2
@@ -27,6 +27,42 @@ float twipToPixel(float fInput, float zoom)
f325b2
     return fInput / 1440.0f * DPI * zoom;
f325b2
 }
f325b2
 
f325b2
+static void getTileFunc(GTask*, gpointer, gpointer task_data, GCancellable*)
f325b2
+{
f325b2
+    GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, nTileSizePixels, nTileSizePixels);
f325b2
+    GetTileCallbackData* pCallback = static_cast<GetTileCallbackData*>(task_data);
f325b2
+    TileBuffer* buffer = pCallback->m_pBuffer;
f325b2
+    int index = pCallback->m_nX * buffer->m_nWidth + pCallback->m_nY;
f325b2
+    if (!pPixBuf)
f325b2
+    {
f325b2
+        g_info ("Error allocating memory to pixbuf");
f325b2
+        return;
f325b2
+    }
f325b2
+
f325b2
+    unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
f325b2
+    GdkRectangle aTileRectangle;
f325b2
+    aTileRectangle.x = pixelToTwip(nTileSizePixels, pCallback->m_fZoom) * pCallback->m_nY;
f325b2
+    aTileRectangle.y = pixelToTwip(nTileSizePixels, pCallback->m_fZoom) * pCallback->m_nX;
f325b2
+
f325b2
+    g_test_timer_start();
f325b2
+    buffer->m_pLOKDocument->pClass->paintTile(buffer->m_pLOKDocument,
f325b2
+                                      pBuffer,
f325b2
+                                      nTileSizePixels, nTileSizePixels,
f325b2
+                                      aTileRectangle.x, aTileRectangle.y,
f325b2
+                                      pixelToTwip(nTileSizePixels, pCallback->m_fZoom),
f325b2
+                                      pixelToTwip(nTileSizePixels, pCallback->m_fZoom));
f325b2
+
f325b2
+    double elapsedTime = g_test_timer_elapsed();
f325b2
+    g_info ("Rendered (%d, %d) in %f seconds",
f325b2
+            pCallback->m_nX,
f325b2
+            pCallback->m_nY,
f325b2
+            elapsedTime);
f325b2
+
f325b2
+    //create a mapping for it
f325b2
+    buffer->m_mTiles[index].setPixbuf(pPixBuf);
f325b2
+    buffer->m_mTiles[index].valid = true;
f325b2
+}
f325b2
+
f325b2
 /* ----------------------------
f325b2
    Tile class member functions
f325b2
    ----------------------------
f325b2
@@ -56,55 +92,42 @@ void TileBuffer::resetAllTiles()
f325b2
     std::map<int, Tile>::iterator it = m_mTiles.begin();
f325b2
     for (; it != m_mTiles.end(); ++it)
f325b2
     {
f325b2
-        it->second.release();
f325b2
+        it->second.valid = false;
f325b2
     }
f325b2
-    m_mTiles.clear();
f325b2
 }
f325b2
 
f325b2
-void TileBuffer::setInvalid(int x, int y)
f325b2
+void TileBuffer::setInvalid(int x, int y, float fZoom)
f325b2
 {
f325b2
     int index = x * m_nWidth + y;
f325b2
     g_info("Setting tile invalid (%d, %d)", x, y);
f325b2
     if (m_mTiles.find(index) != m_mTiles.end())
f325b2
     {
f325b2
         m_mTiles[index].valid = false;
f325b2
-        m_mTiles[index].release();
f325b2
-        m_mTiles.erase(index);
f325b2
+        GTask* task = g_task_new(this, NULL, NULL, NULL);
f325b2
+        GetTileCallbackData* pCallback = new GetTileCallbackData(x, y, fZoom, this);
f325b2
+        g_task_set_task_data(task, pCallback, g_free);
f325b2
+        g_task_run_in_thread(task, getTileFunc);
f325b2
     }
f325b2
 }
f325b2
 
f325b2
-Tile& TileBuffer::getTile(int x, int y, float aZoom)
f325b2
+Tile& TileBuffer::getTile(int x, int y, float aZoom, GTask* task)
f325b2
 {
f325b2
     int index = x * m_nWidth + y;
f325b2
-    if(m_mTiles.find(index) == m_mTiles.end() || !m_mTiles[index].valid)
f325b2
-    {
f325b2
 
f325b2
-        GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, nTileSizePixels, nTileSizePixels);
f325b2
-        if (!pPixBuf)
f325b2
-        {
f325b2
-            g_info ("Error allocating memory to pixbuf");
f325b2
-            return m_mTiles[index];
f325b2
-        }
f325b2
-
f325b2
-        unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
f325b2
-        GdkRectangle aTileRectangle;
f325b2
-        aTileRectangle.x = pixelToTwip(nTileSizePixels, aZoom) * y;
f325b2
-        aTileRectangle.y = pixelToTwip(nTileSizePixels, aZoom) * x;
f325b2
-
f325b2
-        g_test_timer_start();
f325b2
-        m_pLOKDocument->pClass->paintTile(m_pLOKDocument,
f325b2
-                                          pBuffer,
f325b2
-                                          nTileSizePixels, nTileSizePixels,
f325b2
-                                          aTileRectangle.x, aTileRectangle.y,
f325b2
-                                          pixelToTwip(nTileSizePixels, aZoom),
f325b2
-                                          pixelToTwip(nTileSizePixels, aZoom));
f325b2
-
f325b2
-        double elapsedTime = g_test_timer_elapsed();
f325b2
-        g_info ("Rendered (%d, %d) in %f seconds", x, y, elapsedTime);
f325b2
-
f325b2
-        //create a mapping for it
f325b2
-        m_mTiles[index].setPixbuf(pPixBuf);
f325b2
-        m_mTiles[index].valid = true;
f325b2
+    if (m_mTiles.find(index) != m_mTiles.end() && !m_mTiles[index].valid)
f325b2
+    {
f325b2
+        GetTileCallbackData* pCallback = new GetTileCallbackData(x, y, aZoom, this);
f325b2
+        g_task_set_task_data(task, pCallback, g_free);
f325b2
+        g_task_run_in_thread(task, getTileFunc);
f325b2
+        return m_mTiles[index];
f325b2
+    }
f325b2
+    else if(m_mTiles.find(index) == m_mTiles.end())
f325b2
+    {
f325b2
+        GetTileCallbackData* pCallback = new GetTileCallbackData(x, y, aZoom, this);
f325b2
+        g_task_set_task_data(task, pCallback, g_free);
f325b2
+        g_info ("running in thread new tile");
f325b2
+        g_task_run_in_thread(task, getTileFunc);
f325b2
+        return m_DummyTile;
f325b2
     }
f325b2
 
f325b2
     return m_mTiles[index];
f325b2
diff --git a/libreofficekit/source/gtk/tilebuffer.hxx b/libreofficekit/source/gtk/tilebuffer.hxx
f325b2
index 6e6c0beb48f3..50de72d9d3b9 100644
f325b2
--- a/libreofficekit/source/gtk/tilebuffer.hxx
f325b2
+++ b/libreofficekit/source/gtk/tilebuffer.hxx
f325b2
@@ -86,7 +86,10 @@ class TileBuffer
f325b2
             int columns)
f325b2
      : m_pLOKDocument(document)
f325b2
         , m_nWidth(columns)
f325b2
-    {  }
f325b2
+    {
f325b2
+        GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, nTileSizePixels, nTileSizePixels);
f325b2
+        m_DummyTile.setPixbuf(pPixBuf);
f325b2
+    }
f325b2
 
f325b2
     ~TileBuffer() {}
f325b2
 
f325b2
@@ -104,7 +107,7 @@ class TileBuffer
f325b2
 
f325b2
        @return the tile at the mentioned position (x, y)
f325b2
      */
f325b2
-    Tile& getTile(int x, int y, float aZoom);
f325b2
+    Tile& getTile(int x, int y, float aZoom, GTask*);
f325b2
     /// Destroys all the tiles in the tile buffer; also frees the memory allocated
f325b2
     /// for all the Tile objects.
f325b2
     void resetAllTiles();
f325b2
@@ -115,17 +118,34 @@ class TileBuffer
f325b2
        @param x the position of tile along x-axis
f325b2
        @param y the position of tile along y-axis
f325b2
      */
f325b2
-    void setInvalid(int x, int y);
f325b2
+    void setInvalid(int x, int y, float zoom);
f325b2
+
f325b2
 
f325b2
- private:
f325b2
     /// Contains the reference to the LOK Document that this tile buffer is for.
f325b2
     LibreOfficeKitDocument *m_pLOKDocument;
f325b2
     /// Stores all the tiles cached by this tile buffer.
f325b2
     std::map<int, Tile> m_mTiles;
f325b2
     /// Width of the current tile buffer (number of columns)
f325b2
     int m_nWidth;
f325b2
+    /// Dummy tile
f325b2
+    Tile m_DummyTile;
f325b2
 };
f325b2
 
f325b2
+struct GetTileCallbackData
f325b2
+{
f325b2
+    int m_nX;
f325b2
+    int m_nY;
f325b2
+    float m_fZoom;
f325b2
+    TileBuffer* m_pBuffer;
f325b2
+
f325b2
+    GetTileCallbackData(int x, int y, float zoom, TileBuffer* buffer)
f325b2
+        : m_nX(x),
f325b2
+          m_nY(y),
f325b2
+          m_fZoom(zoom),
f325b2
+          m_pBuffer(buffer) { }
f325b2
+};
f325b2
+
f325b2
+
f325b2
 #endif // INCLUDED_TILEBUFFER_HXX
f325b2
 
f325b2
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
f325b2
-- 
f325b2
2.12.0
f325b2