[Libreoffice-commits] core.git: Branch 'feature/gsoc-tiled-rendering' - libreofficekit/source

Pranav Kant pranavk at gnome.org
Sat Jul 18 07:09:16 PDT 2015


Rebased ref, commits from common ancestor:
commit 8e79ef65b9aa6a2572744fa464f68ee520ba99ab
Author: Pranav Kant <pranavk at gnome.org>
Date:   Sat Jul 18 19:18:23 2015 +0530

    lokdocview: [WIP] paintTile in another thread
    
    Change-Id: Iab85fb1778f1c5283c2979e992eb7e106dd6bf8b

diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
index 0a5eca4..8883cf7 100644
--- a/libreofficekit/source/gtk/lokdocview.cxx
+++ b/libreofficekit/source/gtk/lokdocview.cxx
@@ -715,6 +715,13 @@ renderGraphicHandle(LOKDocView* pDocView,
     }
 }
 
+static void
+renderDocumentCallback(GObject* source_object, GAsyncResult* res, gpointer)
+{
+    LOKDocView* pDocView = LOK_DOC_VIEW(source_object);
+    gtk_widget_queue_draw(GTK_WIDGET(pDocView));
+}
+
 
 static gboolean
 renderDocument(LOKDocView* pDocView, cairo_t* pCairo)
@@ -732,7 +739,7 @@ renderDocument(LOKDocView* pDocView, cairo_t* pCairo)
     aVisibleArea.y = pixelToTwip (aVisibleArea.y, priv->m_fZoom);
     aVisibleArea.width = pixelToTwip (aVisibleArea.width, priv->m_fZoom);
     aVisibleArea.height = pixelToTwip (aVisibleArea.height, priv->m_fZoom);
-
+    gboolean status = true;
     // Render the tiles.
     for (guint nRow = 0; nRow < nRows; ++nRow)
     {
@@ -764,17 +771,23 @@ renderDocument(LOKDocView* pDocView, cairo_t* pCairo)
 
             if (bPaint)
             {
-                Tile& currentTile = priv->m_aTileBuffer.getTile(nRow, nColumn, priv->m_fZoom);
-                GdkPixbuf* pPixBuf = currentTile.getBuffer();
-                gdk_cairo_set_source_pixbuf (pCairo, pPixBuf,
-                                             twipToPixel(aTileRectangleTwips.x, priv->m_fZoom),
-                                             twipToPixel(aTileRectangleTwips.y, priv->m_fZoom));
-                cairo_paint(pCairo);
+                GTask* task = g_task_new(pDocView, NULL, renderDocumentCallback, NULL);
+                gboolean status_tmp;
+                Tile& currentTile = priv->m_aTileBuffer.getTile(nRow, nColumn, priv->m_fZoom, &status_tmp, task);
+                if (!status_tmp)
+                    status = status_tmp;
+                if (status_tmp) {
+                    GdkPixbuf* pPixBuf = currentTile.getBuffer();
+                    gdk_cairo_set_source_pixbuf (pCairo, pPixBuf,
+                                                 twipToPixel(aTileRectangleTwips.x, priv->m_fZoom),
+                                                 twipToPixel(aTileRectangleTwips.y, priv->m_fZoom));
+                    cairo_paint(pCairo);
+                }
             }
         }
     }
 
-    return FALSE;
+    return status;
 }
 
 static gboolean
@@ -1148,8 +1161,8 @@ static gboolean lok_doc_view_draw (GtkWidget* pWidget, cairo_t* pCairo)
 {
     LOKDocView *pDocView = LOK_DOC_VIEW (pWidget);
 
-    renderDocument (pDocView, pCairo);
-    renderOverlay (pDocView, pCairo);
+    gboolean status = renderDocument (pDocView, pCairo);
+    if (status) renderOverlay (pDocView, pCairo);
 
     return FALSE;
 }
diff --git a/libreofficekit/source/gtk/tilebuffer.cxx b/libreofficekit/source/gtk/tilebuffer.cxx
index 60aa16f..4bac5f0 100644
--- a/libreofficekit/source/gtk/tilebuffer.cxx
+++ b/libreofficekit/source/gtk/tilebuffer.cxx
@@ -73,40 +73,55 @@ void TileBuffer::setInvalid(int x, int y)
     }
 }
 
-Tile& TileBuffer::getTile(int x, int y, float aZoom)
+static void getTileFunc(GTask* task, gpointer source_object, gpointer task_data, GCancellable*)
+{
+    GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, nTileSizePixels, nTileSizePixels);
+    GetTileCallbackData* pCallback = static_cast<GetTileCallbackData*>(task_data);
+    TileBuffer* buffer = pCallback->m_pBuffer;
+    int index = pCallback->m_nX * buffer->m_nWidth + pCallback->m_nY;
+    if (!pPixBuf)
+    {
+        g_info ("Error allocating memory to pixbuf");
+        return;
+    }
+
+    unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
+    GdkRectangle aTileRectangle;
+    aTileRectangle.x = pixelToTwip(nTileSizePixels, pCallback->m_fZoom) * pCallback->m_nY;
+    aTileRectangle.y = pixelToTwip(nTileSizePixels, pCallback->m_fZoom) * pCallback->m_nX;
+
+    g_test_timer_start();
+    buffer->m_pLOKDocument->pClass->paintTile(buffer->m_pLOKDocument,
+                                      pBuffer,
+                                      nTileSizePixels, nTileSizePixels,
+                                      aTileRectangle.x, aTileRectangle.y,
+                                      pixelToTwip(nTileSizePixels, pCallback->m_fZoom),
+                                      pixelToTwip(nTileSizePixels, pCallback->m_fZoom));
+
+    double elapsedTime = g_test_timer_elapsed();
+    g_info ("Rendered (%d, %d) in %f seconds",
+            pCallback->m_nX,
+            pCallback->m_nY,
+            elapsedTime);
+
+    //create a mapping for it
+    buffer->m_mTiles[index].setPixbuf(pPixBuf);
+    buffer->m_mTiles[index].valid = true;
+}
+
+Tile& TileBuffer::getTile(int x, int y, float aZoom, gboolean *status, GTask* task)
 {
     int index = x * m_nWidth + y;
     if(m_mTiles.find(index) == m_mTiles.end() || !m_mTiles[index].valid)
     {
-
-        GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, nTileSizePixels, nTileSizePixels);
-        if (!pPixBuf)
-        {
-            g_info ("Error allocating memory to pixbuf");
-            return m_mTiles[index];
-        }
-
-        unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
-        GdkRectangle aTileRectangle;
-        aTileRectangle.x = pixelToTwip(nTileSizePixels, aZoom) * y;
-        aTileRectangle.y = pixelToTwip(nTileSizePixels, aZoom) * x;
-
-        g_test_timer_start();
-        m_pLOKDocument->pClass->paintTile(m_pLOKDocument,
-                                          pBuffer,
-                                          nTileSizePixels, nTileSizePixels,
-                                          aTileRectangle.x, aTileRectangle.y,
-                                          pixelToTwip(nTileSizePixels, aZoom),
-                                          pixelToTwip(nTileSizePixels, aZoom));
-
-        double elapsedTime = g_test_timer_elapsed();
-        g_info ("Rendered (%d, %d) in %f seconds", x, y, elapsedTime);
-
-        //create a mapping for it
-        m_mTiles[index].setPixbuf(pPixBuf);
-        m_mTiles[index].valid = true;
+        GetTileCallbackData* pCallback = new GetTileCallbackData(x, y, aZoom, this);
+        g_task_set_task_data(task, pCallback, g_free);
+        g_task_run_in_thread(task, getTileFunc);
+        *status = false;
+        return mDummyTile;
     }
 
+    *status = true;
     return m_mTiles[index];
 }
 
diff --git a/libreofficekit/source/gtk/tilebuffer.hxx b/libreofficekit/source/gtk/tilebuffer.hxx
index 6e6c0be..84c5abe 100644
--- a/libreofficekit/source/gtk/tilebuffer.hxx
+++ b/libreofficekit/source/gtk/tilebuffer.hxx
@@ -72,6 +72,7 @@ private:
     GdkPixbuf *m_pBuffer;
 };
 
+
 /**
    This class represents the tile buffer which is responsible for managing,
    reusing and caching all the already rendered tiles. If the given tile is not
@@ -86,7 +87,15 @@ class TileBuffer
             int columns)
      : m_pLOKDocument(document)
         , m_nWidth(columns)
-    {  }
+    {
+        mDummyTile = Tile();
+        GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
+                                           true,
+                                           8,
+                                           nTileSizePixels,
+                                           nTileSizePixels);
+        mDummyTile.setPixbuf(pixbuf);
+    }
 
     ~TileBuffer() {}
 
@@ -104,7 +113,7 @@ class TileBuffer
 
        @return the tile at the mentioned position (x, y)
      */
-    Tile& getTile(int x, int y, float aZoom);
+    Tile& getTile(int x, int y, float aZoom, gboolean *status, GTask* task);
     /// Destroys all the tiles in the tile buffer; also frees the memory allocated
     /// for all the Tile objects.
     void resetAllTiles();
@@ -117,15 +126,31 @@ class TileBuffer
      */
     void setInvalid(int x, int y);
 
- private:
     /// Contains the reference to the LOK Document that this tile buffer is for.
     LibreOfficeKitDocument *m_pLOKDocument;
     /// Stores all the tiles cached by this tile buffer.
     std::map<int, Tile> m_mTiles;
     /// Width of the current tile buffer (number of columns)
     int m_nWidth;
+    /// Dummy tile
+    Tile mDummyTile;
 };
 
+struct GetTileCallbackData
+{
+    int m_nX;
+    int m_nY;
+    float m_fZoom;
+    TileBuffer* m_pBuffer;
+
+    GetTileCallbackData(int x, int y, float zoom, TileBuffer* buffer)
+        : m_nX(x),
+          m_nY(y),
+          m_fZoom(zoom),
+          m_pBuffer(buffer) { }
+};
+
+
 #endif // INCLUDED_TILEBUFFER_HXX
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list