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