Blame SOURCES/0078-Use-thread-pool-for-LOK-call-paintTile.patch

f325b2
From aec82a6e59d9d1aef25507d07ded71570527fd19 Mon Sep 17 00:00:00 2001
f325b2
From: Pranav Kant <pranavk@gnome.org>
f325b2
Date: Fri, 24 Jul 2015 01:10:42 +0530
f325b2
Subject: [PATCH 078/398] Use thread pool for LOK call: paintTile()
f325b2
f325b2
Change-Id: I45e94248013277affa11e91439fbc16995b8ed8e
f325b2
(cherry picked from commit a7f12df929226ba43356d3d092851b07c84ae1c4)
f325b2
---
f325b2
 libreofficekit/source/gtk/lokdocview.cxx | 122 ++++++++++++++-----------------
f325b2
 libreofficekit/source/gtk/tilebuffer.cxx |  61 ++++------------
f325b2
 libreofficekit/source/gtk/tilebuffer.hxx |  69 +++++++++++++----
f325b2
 3 files changed, 125 insertions(+), 127 deletions(-)
f325b2
f325b2
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
f325b2
index ddb3c386b0ca..f32c3e10546e 100644
f325b2
--- a/libreofficekit/source/gtk/lokdocview.cxx
f325b2
+++ b/libreofficekit/source/gtk/lokdocview.cxx
f325b2
@@ -135,16 +135,6 @@ enum
f325b2
     PROP_CAN_ZOOM_OUT
f325b2
 };
f325b2
 
f325b2
-enum
f325b2
-{
f325b2
-    LOK_LOAD_DOC,
f325b2
-    LOK_POST_COMMAND,
f325b2
-    LOK_SET_EDIT,
f325b2
-    LOK_SET_PARTMODE,
f325b2
-    LOK_SET_PART,
f325b2
-    LOK_POST_KEY
f325b2
-};
f325b2
-
f325b2
 static guint doc_view_signals[LAST_SIGNAL] = { 0 };
f325b2
 
f325b2
 static void lok_doc_view_initable_iface_init (GInitableIface *iface);
f325b2
@@ -161,7 +151,7 @@ G_DEFINE_TYPE_WITH_CODE (LOKDocView, lok_doc_view, GTK_TYPE_DRAWING_AREA,
f325b2
 #pragma GCC diagnostic pop
f325b2
 #endif
f325b2
 
f325b2
-static GThreadPool* lokThreadPool;
f325b2
+GThreadPool* lokThreadPool;
f325b2
 
f325b2
 /// Helper struct used to pass the data from soffice thread -> main thread.
f325b2
 struct CallbackData
f325b2
@@ -176,50 +166,6 @@ struct CallbackData
f325b2
           m_pDocView(pDocView) {}
f325b2
 };
f325b2
 
f325b2
-/**
f325b2
-   A struct that we use to store the data about the LOK call.
f325b2
-
f325b2
-   Object of this type is passed with all the LOK calls,
f325b2
-   so that they can be idenitified. Additionally, it also contains
f325b2
-   the data that LOK call needs.
f325b2
-*/
f325b2
-struct LOEvent
f325b2
-{
f325b2
-    /// To identify the type of LOK call
f325b2
-    int m_nType;
f325b2
-    const gchar* m_pCommand;
f325b2
-    const gchar* m_pArguments;
f325b2
-    gchar* m_pPath;
f325b2
-    gboolean m_bEdit;
f325b2
-    int m_nPartMode;
f325b2
-    int m_nPart;
f325b2
-    int m_nKeyEvent;
f325b2
-    int m_nCharCode;
f325b2
-    int m_nKeyCode;
f325b2
-
f325b2
-
f325b2
-    /// Constructor to easily instantiate an object for LOK call of `type' type.
f325b2
-    LOEvent(int type)
f325b2
-        : m_nType(type) {}
f325b2
-
f325b2
-    LOEvent(int type, const gchar* pCommand, const gchar* pArguments)
f325b2
-        : m_nType(type),
f325b2
-          m_pCommand(pCommand),
f325b2
-          m_pArguments(pArguments) {}
f325b2
-
f325b2
-    LOEvent(int type, const gchar* pPath)
f325b2
-        : m_nType(type)
f325b2
-    {
f325b2
-        m_pPath = g_strdup(pPath);
f325b2
-    }
f325b2
-
f325b2
-    LOEvent(int type, int nKeyEvent, int nCharCode, int nKeyCode)
f325b2
-        : m_nType(type),
f325b2
-          m_nKeyEvent(nKeyEvent),
f325b2
-          m_nCharCode(nCharCode),
f325b2
-          m_nKeyCode(nKeyCode) {}
f325b2
-};
f325b2
-
f325b2
 static void
f325b2
 payloadToSize(const char* pPayload, long& rWidth, long& rHeight)
f325b2
 {
f325b2
@@ -529,10 +475,12 @@ setTilesInvalid (LOKDocView* pDocView, const GdkRectangle& rRectangle)
f325b2
     aStart.y = aRectanglePixels.x / nTileSizePixels;
f325b2
     aEnd.x = (aRectanglePixels.y + aRectanglePixels.height + nTileSizePixels) / nTileSizePixels;
f325b2
     aEnd.y = (aRectanglePixels.x + aRectanglePixels.width + nTileSizePixels) / nTileSizePixels;
f325b2
-
f325b2
+    GTask* task = g_task_new(pDocView, NULL, NULL, NULL);
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, priv->m_fZoom);
f325b2
+            priv->m_aTileBuffer.setInvalid(i, j, priv->m_fZoom, task);
f325b2
+
f325b2
+    g_object_unref(task);
f325b2
 }
f325b2
 
f325b2
 static gboolean
f325b2
@@ -753,13 +701,6 @@ 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
 {
f325b2
@@ -808,14 +749,14 @@ renderDocument(LOKDocView* pDocView, cairo_t* pCairo)
f325b2
 
f325b2
             if (bPaint)
f325b2
             {
f325b2
-                GTask* task = g_task_new(pDocView, NULL, renderDocumentCallback, NULL);
f325b2
+                GTask* task = g_task_new(pDocView, NULL, NULL, 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
                                              twipToPixel(aTileRectangleTwips.y, priv->m_fZoom));
f325b2
                 cairo_paint(pCairo);
f325b2
+                g_object_unref(task);
f325b2
             }
f325b2
         }
f325b2
     }
f325b2
@@ -1212,6 +1153,52 @@ lok_doc_view_post_command_in_thread (gpointer data)
f325b2
 }
f325b2
 
f325b2
 static void
f325b2
+paintTileInThread (gpointer data)
f325b2
+{
f325b2
+    GTask* task = G_TASK(data);
f325b2
+    LOKDocView* pDocView = LOK_DOC_VIEW(g_task_get_source_object(task));
f325b2
+    LOKDocViewPrivate *priv = static_cast<LOKDocViewPrivate*>(lok_doc_view_get_instance_private (pDocView));
f325b2
+    LOEvent* pLOEvent = static_cast<LOEvent*>(g_task_get_task_data(task));
f325b2
+    TileBuffer& buffer = priv->m_aTileBuffer;
f325b2
+    int index = pLOEvent->m_nX * buffer.m_nWidth + pLOEvent->m_nY;
f325b2
+    if (buffer.m_mTiles.find(index) != buffer.m_mTiles.end() &&
f325b2
+        buffer.m_mTiles[index].valid)
f325b2
+        return;
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;
f325b2
+    }
f325b2
+
f325b2
+    unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
f325b2
+    GdkRectangle aTileRectangle;
f325b2
+    aTileRectangle.x = pixelToTwip(nTileSizePixels, pLOEvent->m_fZoom) * pLOEvent->m_nY;
f325b2
+    aTileRectangle.y = pixelToTwip(nTileSizePixels, pLOEvent->m_fZoom) * pLOEvent->m_nX;
f325b2
+
f325b2
+    g_test_timer_start();
f325b2
+    priv->m_pDocument->pClass->paintTile(priv->m_pDocument,
f325b2
+                                         pBuffer,
f325b2
+                                         nTileSizePixels, nTileSizePixels,
f325b2
+                                         aTileRectangle.x, aTileRectangle.y,
f325b2
+                                         pixelToTwip(nTileSizePixels, pLOEvent->m_fZoom),
f325b2
+                                         pixelToTwip(nTileSizePixels, pLOEvent->m_fZoom));
f325b2
+
f325b2
+    double elapsedTime = g_test_timer_elapsed();
f325b2
+    g_info ("Rendered (%d, %d) in %f seconds",
f325b2
+            pLOEvent->m_nX,
f325b2
+            pLOEvent->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
+    gtk_widget_queue_draw(GTK_WIDGET(pDocView));
f325b2
+}
f325b2
+
f325b2
+
f325b2
+static void
f325b2
 lokThreadFunc(gpointer data, gpointer /*user_data*/)
f325b2
 {
f325b2
     GTask* task = G_TASK(data);
f325b2
@@ -1237,6 +1224,9 @@ lokThreadFunc(gpointer data, gpointer /*user_data*/)
f325b2
     case LOK_POST_KEY:
f325b2
         postKeyEventInThread(task);
f325b2
         break;
f325b2
+    case LOK_PAINT_TILE:
f325b2
+        paintTileInThread(task);
f325b2
+        break;
f325b2
     }
f325b2
 
f325b2
     g_object_unref(task);
f325b2
diff --git a/libreofficekit/source/gtk/tilebuffer.cxx b/libreofficekit/source/gtk/tilebuffer.cxx
f325b2
index d488f8b0516c..a8594fc916f0 100644
f325b2
--- a/libreofficekit/source/gtk/tilebuffer.cxx
f325b2
+++ b/libreofficekit/source/gtk/tilebuffer.cxx
f325b2
@@ -13,6 +13,8 @@
f325b2
 #define g_info(...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__)
f325b2
 #endif
f325b2
 
f325b2
+extern GThreadPool* lokThreadPool;
f325b2
+
f325b2
 /* ------------------
f325b2
    Utility functions
f325b2
    ------------------
f325b2
@@ -27,42 +29,6 @@ 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
@@ -96,17 +62,17 @@ void TileBuffer::resetAllTiles()
f325b2
     }
f325b2
 }
f325b2
 
f325b2
-void TileBuffer::setInvalid(int x, int y, float fZoom)
f325b2
+void TileBuffer::setInvalid(int x, int y, float fZoom, GTask* task)
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
-        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
+        LOEvent* pLOEvent = new LOEvent(LOK_PAINT_TILE, x, y, fZoom);
f325b2
+        g_task_set_task_data(task, pLOEvent, g_free);
f325b2
+        g_thread_pool_push(lokThreadPool, g_object_ref(task), NULL);
f325b2
     }
f325b2
 }
f325b2
 
f325b2
@@ -116,17 +82,16 @@ Tile& TileBuffer::getTile(int x, int y, float aZoom, GTask* task)
f325b2
 
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
+        LOEvent* pLOEvent = new LOEvent(LOK_PAINT_TILE, x, y, aZoom);
f325b2
+        g_task_set_task_data(task, pLOEvent, g_free);
f325b2
+        g_thread_pool_push(lokThreadPool, g_object_ref(task), NULL);
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
+        LOEvent* pLOEvent = new LOEvent(LOK_PAINT_TILE, x, y, aZoom);
f325b2
+        g_task_set_task_data(task, pLOEvent, g_free);
f325b2
+        g_thread_pool_push(lokThreadPool, g_object_ref(task), NULL);
f325b2
         return m_DummyTile;
f325b2
     }
f325b2
 
f325b2
diff --git a/libreofficekit/source/gtk/tilebuffer.hxx b/libreofficekit/source/gtk/tilebuffer.hxx
f325b2
index 40fb2abbae6d..f23b02330616 100644
f325b2
--- a/libreofficekit/source/gtk/tilebuffer.hxx
f325b2
+++ b/libreofficekit/source/gtk/tilebuffer.hxx
f325b2
@@ -107,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, GTask*);
f325b2
+    Tile& getTile(int x, int y, float aZoom, GTask* task);
f325b2
     /// Destroys all the tiles in the tile buffer; also frees the memory allocated
f325b2
     /// for all the Tile objects.
f325b2
     void resetAllTiles();
f325b2
@@ -118,8 +118,7 @@ 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, float zoom);
f325b2
-
f325b2
+    void setInvalid(int x, int y, float zoom, GTask* task);
f325b2
 
f325b2
     /// Contains the reference to the LOK Document that this tile buffer is for.
f325b2
     LibreOfficeKitDocument *m_pLOKDocument;
f325b2
@@ -131,26 +130,70 @@ class TileBuffer
f325b2
     Tile m_DummyTile;
f325b2
 };
f325b2
 
f325b2
+enum
f325b2
+{
f325b2
+    LOK_LOAD_DOC,
f325b2
+    LOK_POST_COMMAND,
f325b2
+    LOK_SET_EDIT,
f325b2
+    LOK_SET_PARTMODE,
f325b2
+    LOK_SET_PART,
f325b2
+    LOK_POST_KEY,
f325b2
+    LOK_PAINT_TILE
f325b2
+};
f325b2
+
f325b2
 /**
f325b2
-   Helper struct used to pass the data from main thread to spawned threads.
f325b2
-   Spawned threads are responsible for calling paintTile, and store the result
f325b2
-   in tile buffer.
f325b2
+   A struct that we use to store the data about the LOK call.
f325b2
+
f325b2
+   Object of this type is passed with all the LOK calls,
f325b2
+   so that they can be idenitified. Additionally, it also contains
f325b2
+   the data that LOK call needs.
f325b2
 */
f325b2
-struct GetTileCallbackData
f325b2
+struct LOEvent
f325b2
 {
f325b2
+    /// To identify the type of LOK call
f325b2
+    int m_nType;
f325b2
+    const gchar* m_pCommand;
f325b2
+    const gchar* m_pArguments;
f325b2
+    gchar* m_pPath;
f325b2
+    gboolean m_bEdit;
f325b2
+    int m_nPartMode;
f325b2
+    int m_nPart;
f325b2
+    int m_nKeyEvent;
f325b2
+    int m_nCharCode;
f325b2
+    int m_nKeyCode;
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
+    /// Constructor to easily instantiate an object for LOK call of `type' type.
f325b2
+    LOEvent(int type)
f325b2
+        : m_nType(type) {}
f325b2
+
f325b2
+    LOEvent(int type, const gchar* pCommand, const gchar* pArguments)
f325b2
+        : m_nType(type),
f325b2
+          m_pCommand(pCommand),
f325b2
+          m_pArguments(pArguments) {}
f325b2
+
f325b2
+    LOEvent(int type, const gchar* pPath)
f325b2
+        : m_nType(type)
f325b2
+    {
f325b2
+        m_pPath = g_strdup(pPath);
f325b2
+    }
f325b2
+
f325b2
+    LOEvent(int type, int nKeyEvent, int nCharCode, int nKeyCode)
f325b2
+        : m_nType(type),
f325b2
+          m_nKeyEvent(nKeyEvent),
f325b2
+          m_nCharCode(nCharCode),
f325b2
+          m_nKeyCode(nKeyCode) {}
f325b2
+
f325b2
+    LOEvent(int type, int x, int y, float zoom)
f325b2
+        : m_nType(type),
f325b2
+          m_nX(x),
f325b2
           m_nY(y),
f325b2
-          m_fZoom(zoom),
f325b2
-          m_pBuffer(buffer) { }
f325b2
+          m_fZoom(zoom) {}
f325b2
 };
f325b2
 
f325b2
-
f325b2
 #endif // INCLUDED_TILEBUFFER_HXX
f325b2
 
f325b2
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
f325b2
-- 
f325b2
2.12.0
f325b2