Scroom  0.14
CompressedTile Class Reference

#include <tiledbitmaplayer.hh>

Inheritance diagram for CompressedTile:
Inheritance graph
Collaboration diagram for CompressedTile:
Collaboration graph

Public Types

using Ptr = boost::shared_ptr< CompressedTile >
 
- Public Types inherited from Scroom::Utils::Observable< TileInitialisationObserver >
using Observer = boost::shared_ptr< TileInitialisationObserver >
 
using Ptr = boost::shared_ptr< Observable< TileInitialisationObserver > >
 
- Public Types inherited from Scroom::Utils::Observable< TileLoadingObserver >
using Observer = boost::shared_ptr< TileLoadingObserver >
 
using Ptr = boost::shared_ptr< Observable< TileLoadingObserver > >
 
- Public Types inherited from Viewable
using Ptr = boost::shared_ptr< Viewable >
 
using WeakPtr = boost::weak_ptr< Viewable >
 

Public Member Functions

Tile::Ptr initialize ()
 
Scroom::Bookkeeping::Token registerStrongObserver (TileInitialisationObserver::Ptr const &observer)
 
Scroom::Bookkeeping::Token registerObserver (TileInitialisationObserver::WeakPtr const &observer)
 
Scroom::Bookkeeping::Token registerStrongObserver (TileLoadingObserver::Ptr const &observer)
 
Scroom::Bookkeeping::Token registerObserver (TileLoadingObserver::WeakPtr const &observer)
 
Tile::Ptr getTileSync ()
 
ConstTile::Ptr getConstTileSync ()
 
ConstTile::Ptr getConstTileAsync ()
 
void reportFinished ()
 
TileState getState ()
 
boost::shared_ptr< TileViewStategetViewState (const ViewInterface::WeakPtr &vi)
 
void open (ViewInterface::WeakPtr vi) override
 
void close (ViewInterface::WeakPtr vi) override
 
- Public Member Functions inherited from Scroom::Utils::Observable< TileInitialisationObserver >
 Observable ()
 
 Observable (const Observable &)=delete
 
 Observable (Observable &&)=delete
 
 ~Observable () override
 
Observable operator= (const Observable &)=delete
 
Observable operator= (Observable &&)=delete
 
Scroom::Bookkeeping::Token registerStrongObserver (Observer const &observer)
 
Scroom::Bookkeeping::Token registerObserver (ObserverWeak const &observer)
 
- Public Member Functions inherited from Scroom::Utils::Observable< TileLoadingObserver >
 Observable ()
 
 Observable (const Observable &)=delete
 
 Observable (Observable &&)=delete
 
 ~Observable () override
 
Observable operator= (const Observable &)=delete
 
Observable operator= (Observable &&)=delete
 
Scroom::Bookkeeping::Token registerStrongObserver (Observer const &observer)
 
Scroom::Bookkeeping::Token registerObserver (ObserverWeak const &observer)
 

Static Public Member Functions

static Ptr create (int depth, int x, int y, int bpp, const Scroom::MemoryBlobs::PageProvider::Ptr &provider, TileStateInternal state=TSI_UNINITIALIZED)
 

Public Attributes

const int depth
 
const int x
 
const int y
 
const int bpp
 

Protected Member Functions

void observerAdded (TileInitialisationObserver::Ptr const &observer, Scroom::Bookkeeping::Token const &token) override
 
void observerAdded (TileLoadingObserver::Ptr const &observer, Scroom::Bookkeeping::Token const &token) override
 
- Protected Member Functions inherited from Scroom::Utils::Observable< TileInitialisationObserver >
std::list< ObservergetObservers ()
 
- Protected Member Functions inherited from Scroom::Utils::Observable< TileLoadingObserver >
std::list< ObservergetObservers ()
 

Private Member Functions

 CompressedTile (int depth, int x, int y, int bpp, const Scroom::MemoryBlobs::PageProvider::Ptr &provider, TileStateInternal state)
 CompressedTile. More...
 
void cleanupState ()
 
ConstTile::Ptr do_load ()
 
void notifyObservers (const ConstTile::Ptr &tile)
 

Private Attributes

TileStateInternal state
 
Tile::WeakPtr tile
 
ConstTile::WeakPtr constTile
 
Scroom::MemoryBlobs::PageProvider::Ptr provider
 
Scroom::MemoryBlobs::Blob::Ptr data
 
boost::mutex stateData
 
boost::mutex tileData
 
ThreadPool::Queue::WeakPtr queue
 
std::map< ViewInterface::WeakPtr, boost::weak_ptr< TileViewState > > viewStates
 

Detailed Description

Internal data structure representing a Tile.

The CompressedTile class contains administrative information about the Tile. The Tile itself (and associated bitmap data) will be loaded and unloaded as needed. This class will stay in memory always.

Observers can receive events related to this tile.

Member Typedef Documentation

◆ Ptr

using CompressedTile::Ptr = boost::shared_ptr<CompressedTile>

Constructor & Destructor Documentation

◆ CompressedTile()

CompressedTile::CompressedTile ( int  depth,
int  x,
int  y,
int  bpp,
const Scroom::MemoryBlobs::PageProvider::Ptr provider,
TileStateInternal  state 
)
private

CompressedTile.

22  : depth(depth_)
23  , x(x_)
24  , y(y_)
25  , bpp(bpp_)
26  , state(state_)
27  , provider(provider_)
28  , data(Blob::create(provider_, TILESIZE * TILESIZE * bpp / 8))
29 {
30 }

Referenced by create().

Here is the caller graph for this function:

Member Function Documentation

◆ cleanupState()

void CompressedTile::cleanupState ( )
private

Does some internal state maintenance.

Call only while stateData is locked.

262 {
263  if(state == TSI_LOADING_ASYNCHRONOUSLY && !queue.lock())
264  {
265  // Someone, after triggering an asynchronous load, decided not to need
266  // the tile. Changing back to normal.
267  state = TSI_NORMAL;
268  }
269 }

Referenced by do_load(), and observerAdded().

Here is the caller graph for this function:

◆ close()

void CompressedTile::close ( ViewInterface::WeakPtr  vi)
overridevirtual

Gets called just before the View is destroyed

Precondition
Should be called from within a Gdk critical section (i.e. between gdk_threads_enter() and gdk_threads_leave() calls)

Implements Viewable.

284 { viewStates.erase(vi); }

◆ create()

CompressedTile::Ptr CompressedTile::create ( int  depth,
int  x,
int  y,
int  bpp,
const Scroom::MemoryBlobs::PageProvider::Ptr provider,
TileStateInternal  state = TSI_UNINITIALIZED 
)
static
38 {
40 
41  return tile;
42 }

Referenced by Layer::Layer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_load()

ConstTile::Ptr CompressedTile::do_load ( )
private
131 {
132  bool didLoad = false;
133 
134  ConstTile::Ptr result;
135  {
136  boost::mutex::scoped_lock const lock(stateData);
137  cleanupState();
139  }
140  {
141  boost::mutex::scoped_lock const lock(tileData);
142  result = constTile.lock(); // This ought to fail
143  if(!result)
144  {
145  result = ConstTile::Ptr(new ConstTile(TILESIZE, TILESIZE, bpp, data->getConst()));
146  constTile = result;
147  didLoad = true;
148  }
149  }
150  {
151  boost::mutex::scoped_lock const lock(stateData);
152  cleanupState();
153  state = TSI_NORMAL;
154  if(didLoad)
155  {
156  notifyObservers(result);
157  }
158  }
159 
160  return result;
161 }

Referenced by getConstTileSync(), observerAdded(), and reportFinished().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getConstTileAsync()

ConstTile::Ptr CompressedTile::getConstTileAsync ( )

Get a reference to the ConstTile.

If the tile is currently TSI_LOADED, you'll get a reference, otherwise, you'll receive an empty reference.

85 {
86  ConstTile::Ptr result = constTile.lock();
87  return result;
88 }

◆ getConstTileSync()

ConstTile::Ptr CompressedTile::getConstTileSync ( )

Get a reference to the Const Tile.

If the tile is currently TSI_NORMAL, it will be loaded, if necessary. If the tile is TSI_UNINITIALIZED, you'll receive an empty reference.

45 {
46  ConstTile::Ptr result = constTile.lock();
47  if(!result)
48  {
49  result = do_load();
50  }
51 
52  return result;
53 }

Referenced by getTileSync().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getState()

TileState CompressedTile::getState ( )
177 {
178  TileState result = TILE_UNINITIALIZED;
179  switch(state)
180  {
181  case TSI_NORMAL:
182  {
183  Tile::Ptr const t = tile.lock();
184  if(t)
185  {
186  result = TILE_LOADED;
187  }
188  else
189  {
190  result = TILE_UNLOADED;
191  }
192  }
193  break;
194  case TSI_OUT_OF_BOUNDS:
195  result = TILE_OUT_OF_BOUNDS;
196  break;
199  result = TILE_UNLOADED;
200  break;
201  case TSI_UNINITIALIZED:
202  result = TILE_UNINITIALIZED;
203  break;
204  }
205  return result;
206 }

◆ getTileSync()

Tile::Ptr CompressedTile::getTileSync ( )

Get a reference to the Tile.

If the tile is currently TSI_NORMAL, it will be loaded, if necessary. If the tile is TSI_UNINITIALIZED, you'll receive an empty reference.

56 {
57  Tile::Ptr result;
58  {
59  boost::mutex::scoped_lock const lock(tileData);
60  result = tile.lock();
61  }
62  if(!result)
63  {
64  // Retrieve the const tile, such that all observers are properly
65  // notified of the loading
66  ConstTile::Ptr const temp = getConstTileSync();
67  require(temp);
68  {
69  // Check again. Maybe someone else has beaten us to it...
70  boost::mutex::scoped_lock const lock(tileData);
71  result = tile.lock();
72  }
73  if(!result)
74  {
75  boost::mutex::scoped_lock const lock(tileData);
76  result = Tile::Ptr(new Tile(TILESIZE, TILESIZE, bpp, data->get()));
77  tile = result;
78  }
79  }
80 
81  return result;
82 }

Referenced by initialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getViewState()

TileViewState::Ptr CompressedTile::getViewState ( const ViewInterface::WeakPtr vi)
164 {
165  TileViewState::Ptr result = viewStates[vi].lock();
166 
167  if(!result)
168  {
169  result = TileViewState::create(shared_from_this<CompressedTile>());
170  viewStates[vi] = result;
171  }
172 
173  return result;
174 }
Here is the call graph for this function:

◆ initialize()

Tile::Ptr CompressedTile::initialize ( )

Initializes the tile data

Allocates memory, initializes it to 0, and changes state to TSI_LOADED

91 {
93  Tile::Ptr tile_;
94 
95  bool didInitialize = false;
96  {
97  boost::mutex::scoped_lock const stateLock(stateData);
98  boost::mutex::scoped_lock const dataLock(tileData);
99 
100  if(state == TSI_UNINITIALIZED)
101  {
102  s = data->initialize(0);
103  state = TSI_NORMAL;
104  didInitialize = true;
105  }
106  }
107 
108  if(didInitialize)
109  {
110  tile_ = getTileSync(); // Trigger notifyObservers(), without holding the lock
111  }
112 
113  return tile_;
114 }
Here is the call graph for this function:

◆ notifyObservers()

void CompressedTile::notifyObservers ( const ConstTile::Ptr tile)
private
272 {
274  {
275  observer->tileLoaded(tile_);
276  }
277 }

Referenced by do_load().

Here is the caller graph for this function:

◆ observerAdded() [1/2]

void CompressedTile::observerAdded ( TileInitialisationObserver::Ptr const &  observer,
Scroom::Bookkeeping::Token const &  token 
)
overrideprotectedvirtual

Keep track of new TileInitialisationObserver registrations.

Upon registering an observer, you'll receive the TileInitialisationObserver::tileCreated() event immediately, on your thread. Be careful with your mutexes :-)

Reimplemented from Scroom::Utils::Observable< TileInitialisationObserver >.

209 {
210  observer->tileCreated(shared_from_this<CompressedTile>());
211 }

◆ observerAdded() [2/2]

void CompressedTile::observerAdded ( TileLoadingObserver::Ptr const &  observer,
Scroom::Bookkeeping::Token const &  token 
)
overrideprotectedvirtual

Keep track of new TileLoadingObserver registrations.

Reimplemented from Scroom::Utils::Observable< TileLoadingObserver >.

214 {
215  ConstTile::Ptr const result = constTile.lock();
216  ThreadPool::Queue::Ptr queue_ = queue.lock();
217 
218  if(!result)
219  {
220  boost::mutex::scoped_lock const lock(stateData);
221  cleanupState();
222  switch(state)
223  {
224  case TSI_UNINITIALIZED:
227  // Need to do nothing. All will be well.
228  break;
229 
230  default:
231  case TSI_OUT_OF_BOUNDS:
232  // Shouldn't happen
233  break;
234 
235  case TSI_NORMAL:
236  // Start an asynchronous load
237  if(!queue_)
238  {
239  queue_ = ThreadPool::Queue::create();
240  queue = queue_;
241  }
242  CpuBound()->schedule(boost::bind(&CompressedTile::do_load, this), LOAD_PRIO, queue_);
244 
245  break;
246  }
247  }
248 
249  // When the last observer goes away, cancel the load
250  if(queue_)
251  {
252  token.add(queue_);
253  }
254 
255  if(result)
256  {
257  observer->tileLoaded(result);
258  }
259 }
Here is the call graph for this function:

◆ open()

void CompressedTile::open ( ViewInterface::WeakPtr  vi)
overridevirtual

Gets called just after the View is created

Precondition
Should be called from within a Gdk critical section (i.e. between gdk_threads_enter() and gdk_threads_leave() calls)

Implements Viewable.

280 {
281  // On open, we do nothing. On close, we destroy any resources related to the view.
282 }

◆ registerObserver() [1/2]

Scroom::Bookkeeping::Token CompressedTile::registerObserver ( TileInitialisationObserver::WeakPtr const &  observer)
inline
Here is the call graph for this function:

◆ registerObserver() [2/2]

Scroom::Bookkeeping::Token CompressedTile::registerObserver ( TileLoadingObserver::WeakPtr const &  observer)
inline
Here is the call graph for this function:

◆ registerStrongObserver() [1/2]

Scroom::Bookkeeping::Token CompressedTile::registerStrongObserver ( TileInitialisationObserver::Ptr const &  observer)
inline
Here is the call graph for this function:

◆ registerStrongObserver() [2/2]

Scroom::Bookkeeping::Token CompressedTile::registerStrongObserver ( TileLoadingObserver::Ptr const &  observer)
inline
Here is the call graph for this function:

◆ reportFinished()

void CompressedTile::reportFinished ( )

Report that the tile is completely filled with data

This will be used to notify our observers.

117 {
118  CompressedTile::Ptr const me = shared_from_this<CompressedTile>();
119  ConstTile::Ptr const t = do_load();
121  {
122  observer->tileFinished(me);
123  }
125  {
126  observer->tileLoaded(t);
127  }
128 }
Here is the call graph for this function:

Member Data Documentation

◆ bpp

const int CompressedTile::bpp

Bits per pixel of this tile. Must be a divisor of 8.

Referenced by create(), do_load(), and getTileSync().

◆ constTile

ConstTile::WeakPtr CompressedTile::constTile
private

Reference to the actual Tile

Referenced by do_load(), getConstTileAsync(), getConstTileSync(), and observerAdded().

◆ data

Scroom::MemoryBlobs::Blob::Ptr CompressedTile::data
private

Data associated with the Tile

Referenced by do_load(), getTileSync(), and initialize().

◆ depth

const int CompressedTile::depth

Layer number of this tile

Referenced by create().

◆ provider

Scroom::MemoryBlobs::PageProvider::Ptr CompressedTile::provider
private

Provider of blocks of memory

Referenced by create().

◆ queue

ThreadPool::Queue::WeakPtr CompressedTile::queue
private

Queue on which the load operation is executed

Referenced by cleanupState(), and observerAdded().

◆ state

TileStateInternal CompressedTile::state
private

State of this tile

Referenced by cleanupState(), create(), do_load(), getState(), initialize(), and observerAdded().

◆ stateData

boost::mutex CompressedTile::stateData
private

Mutex protecting the state field

Referenced by do_load(), initialize(), and observerAdded().

◆ tile

Tile::WeakPtr CompressedTile::tile
private

Reference to the actual Tile

Referenced by create(), getState(), and getTileSync().

◆ tileData

boost::mutex CompressedTile::tileData
private

Mutex protecting the data-related fields

Referenced by do_load(), getTileSync(), and initialize().

◆ viewStates

std::map<ViewInterface::WeakPtr, boost::weak_ptr<TileViewState> > CompressedTile::viewStates
private

Referenced by close(), and getViewState().

◆ x

const int CompressedTile::x

x-coordinate of this tile (i.e. number of tiles to the left of this tile)

Referenced by create().

◆ y

const int CompressedTile::y

y-coordinate of this tile (i.e. number of tiles above this tile)

Referenced by create().


The documentation for this class was generated from the following files:
CompressedTile::viewStates
std::map< ViewInterface::WeakPtr, boost::weak_ptr< TileViewState > > viewStates
Definition: tiledbitmaplayer.hh:127
TileLoadingObserver::Ptr
boost::shared_ptr< TileLoadingObserver > Ptr
Definition: tiledbitmaplayer.hh:83
TileInitialisationObserver::Ptr
boost::shared_ptr< TileInitialisationObserver > Ptr
Definition: tiledbitmaplayer.hh:52
CompressedTile::x
const int x
Definition: tiledbitmaplayer.hh:112
TILE_UNLOADED
@ TILE_UNLOADED
Definition: tiledbitmapinterface.hh:35
ThreadPool::Queue::Ptr
boost::shared_ptr< Queue > Ptr
Definition: threadpool.hh:82
CompressedTile::tile
Tile::WeakPtr tile
Definition: tiledbitmaplayer.hh:118
Scroom::Utils::Observable::registerStrongObserver
Scroom::Bookkeeping::Token registerStrongObserver(Observer const &observer)
Definition: observable.hh:213
CompressedTile::constTile
ConstTile::WeakPtr constTile
Definition: tiledbitmaplayer.hh:119
CompressedTile::cleanupState
void cleanupState()
Definition: compressedtile.cc:261
CompressedTile::getTileSync
Tile::Ptr getTileSync()
Definition: compressedtile.cc:55
Tile
Definition: tile.hh:18
ConstTile::Ptr
boost::shared_ptr< ConstTile > Ptr
Definition: tile.hh:47
CompressedTile::getConstTileSync
ConstTile::Ptr getConstTileSync()
Definition: compressedtile.cc:44
TSI_LOADING_SYNCHRONOUSLY
@ TSI_LOADING_SYNCHRONOUSLY
Definition: tiledbitmaplayer.hh:39
CompressedTile::do_load
ConstTile::Ptr do_load()
Definition: compressedtile.cc:130
require
#define require(expr)
Definition: assertions.hh:28
TileViewState::Ptr
boost::shared_ptr< TileViewState > Ptr
Definition: tileviewstate.hh:27
TILE_LOADED
@ TILE_LOADED
Definition: tiledbitmapinterface.hh:36
ThreadPool::Queue::create
static Ptr create()
ThreadPool::Queue.
Definition: threadpoolimpl.cc:377
CompressedTile::stateData
boost::mutex stateData
Definition: tiledbitmaplayer.hh:122
Scroom::Utils::Observable::registerObserver
Scroom::Bookkeeping::Token registerObserver(ObserverWeak const &observer)
Definition: observable.hh:233
CompressedTile::depth
const int depth
Definition: tiledbitmaplayer.hh:111
TileState
TileState
Definition: tiledbitmapinterface.hh:32
CompressedTile::provider
Scroom::MemoryBlobs::PageProvider::Ptr provider
Definition: tiledbitmaplayer.hh:120
TILE_OUT_OF_BOUNDS
@ TILE_OUT_OF_BOUNDS
Definition: tiledbitmapinterface.hh:37
CpuBound
ThreadPool::Ptr CpuBound()
Definition: threadpoolimpl.cc:452
CompressedTile::CompressedTile
CompressedTile(int depth, int x, int y, int bpp, const Scroom::MemoryBlobs::PageProvider::Ptr &provider, TileStateInternal state)
CompressedTile.
Definition: compressedtile.cc:21
CompressedTile::notifyObservers
void notifyObservers(const ConstTile::Ptr &tile)
Definition: compressedtile.cc:271
TSI_UNINITIALIZED
@ TSI_UNINITIALIZED
Definition: tiledbitmaplayer.hh:36
CompressedTile::state
TileStateInternal state
Definition: tiledbitmaplayer.hh:117
Tile::Ptr
boost::shared_ptr< Tile > Ptr
Definition: tile.hh:21
CompressedTile::bpp
const int bpp
Definition: tiledbitmaplayer.hh:114
Scroom::Utils::Stuff
boost::shared_ptr< void > Stuff
Definition: stuff.hh:18
Scroom::Utils::Observable
Definition: observable.hh:24
TSI_OUT_OF_BOUNDS
@ TSI_OUT_OF_BOUNDS
Definition: tiledbitmaplayer.hh:38
TILE_UNINITIALIZED
@ TILE_UNINITIALIZED
Definition: tiledbitmapinterface.hh:34
CompressedTile::data
Scroom::MemoryBlobs::Blob::Ptr data
Definition: tiledbitmaplayer.hh:121
CompressedTile::Ptr
boost::shared_ptr< CompressedTile > Ptr
Definition: tiledbitmaplayer.hh:108
TILESIZE
#define TILESIZE
Definition: tiledbitmaplayer.hh:28
TileViewState::create
static Ptr create(const boost::shared_ptr< CompressedTile > &parent)
Definition: tileviewstate.cc:27
CompressedTile::tileData
boost::mutex tileData
Definition: tiledbitmaplayer.hh:123
TSI_LOADING_ASYNCHRONOUSLY
@ TSI_LOADING_ASYNCHRONOUSLY
Definition: tiledbitmaplayer.hh:40
CompressedTile::y
const int y
Definition: tiledbitmaplayer.hh:113
TSI_NORMAL
@ TSI_NORMAL
Definition: tiledbitmaplayer.hh:37
LOAD_PRIO
#define LOAD_PRIO
Definition: local.hh:10
CompressedTile::queue
ThreadPool::Queue::WeakPtr queue
Definition: tiledbitmaplayer.hh:125
ConstTile
Definition: tile.hh:44
create
void create(NewPresentationInterface *interface)
Definition: loader.cc:175