Blob Blame History Raw
From a8675df04f316dce72483c62f578467dfb446493 Mon Sep 17 00:00:00 2001
From: Pranav Kant <pranavk@gnome.org>
Date: Thu, 4 Jun 2015 01:44:47 +0530
Subject: [PATCH 013/398] lokdocview: Use maps instead of vector

Using vector each tile needs to be allocated memory irrespective of
whether tile is required or not. This approach fails when we zoom in to
a very high level to have thousands of tiles due to lot of memory
required. Using maps instead of vector takes care of this, and only
allocates Tiles when required.

Change-Id: I523f815618451a7f014e28258e0de7b1c0693370
(cherry picked from commit cc78267f274a42d268b1d56d0a83888dc69a4c91)
---
 libreofficekit/source/gtk/tilebuffer.cxx | 17 +++++++++--------
 libreofficekit/source/gtk/tilebuffer.hxx |  8 +++-----
 2 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/libreofficekit/source/gtk/tilebuffer.cxx b/libreofficekit/source/gtk/tilebuffer.cxx
index ca66ae904f71..e1b5b3294cc5 100644
--- a/libreofficekit/source/gtk/tilebuffer.cxx
+++ b/libreofficekit/source/gtk/tilebuffer.cxx
@@ -41,22 +41,22 @@ void TileBuffer::tile_buffer_set_zoom(float newZoomFactor, int rows, int columns
   // set new buffer width and height
   m_nWidth = columns;
   m_nHeight = rows;
-  m_aTiles.resize(m_nWidth * m_nHeight);
 }
 
 void TileBuffer::tile_buffer_reset_all_tiles()
 {
-  for (size_t i = 0; i < m_aTiles.size(); i++)
+  std::map<int, Tile>::iterator it = m_mTiles.begin();
+  for (; it != m_mTiles.end(); it++)
     {
-      m_aTiles[i].tile_release();
+       it->second.tile_release();
     }
-  m_aTiles.clear();
+  m_mTiles.clear();
 }
 
 Tile& TileBuffer::tile_buffer_get_tile(int x, int y)
 {
   int index = x * m_nWidth + y;
-  if(!m_aTiles[index].valid)
+  if(m_mTiles.find(index) == m_mTiles.end())
     {
       GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, m_nTileSize, m_nTileSize);
       if (!pPixBuf){
@@ -77,11 +77,12 @@ Tile& TileBuffer::tile_buffer_get_tile(int x, int y)
                                         // Size of the tile, depends on the zoom factor and the tile position only.
                                         pixelToTwip(m_nTileSize, m_fZoomFactor), pixelToTwip(m_nTileSize, m_fZoomFactor));
 
-      m_aTiles[index].tile_set_pixbuf(pPixBuf);
-      m_aTiles[index].valid = 1;
+      //create a mapping for it
+      m_mTiles[index].tile_set_pixbuf(pPixBuf);
+      m_mTiles[index].valid = 1;
     }
 
-  return m_aTiles[index];
+  return m_mTiles[index];
 }
 
 void Tile::tile_set_pixbuf(GdkPixbuf *buffer)
diff --git a/libreofficekit/source/gtk/tilebuffer.hxx b/libreofficekit/source/gtk/tilebuffer.hxx
index a5ed0dc8ec61..0bc2d38dd641 100644
--- a/libreofficekit/source/gtk/tilebuffer.hxx
+++ b/libreofficekit/source/gtk/tilebuffer.hxx
@@ -12,7 +12,7 @@
 
 #include <gdk/gdkkeysyms.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
-#include <vector>
+#include <map>
 
 #define LOK_USE_UNSTABLE_API
 #include <LibreOfficeKit/LibreOfficeKit.h>
@@ -55,9 +55,7 @@ public:
     , m_fZoomFactor(1)
     , m_nWidth(columns)
     , m_nHeight(rows)
-  {
-    m_aTiles.resize(rows * columns);
-  }
+    {  }
 
   ~TileBuffer() {}
 
@@ -69,7 +67,7 @@ private:
   LibreOfficeKitDocument *m_pLOKDocument;
   int m_nTileSize;
   float m_fZoomFactor;
-  std::vector<Tile> m_aTiles;
+  std::map<int, Tile> m_mTiles;
   //TODO: Also set width and height when document size changes
   int m_nWidth;
   int m_nHeight;
-- 
2.12.0