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