kbrown / rpms / libreoffice

Forked from rpms/libreoffice 2 years ago
Clone

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

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