Scroom 0.14-48-ga0fee447
Loading...
Searching...
No Matches
CompressedTile Class Reference

#include <tiledbitmaplayer.hh>

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

Public Types

using Ptr = std::shared_ptr< CompressedTile >
 
- Public Types inherited from Scroom::Utils::Observable< TileInitialisationObserver >
using Observer = std::shared_ptr< TileInitialisationObserver >
 
using Ptr = std::shared_ptr< Observable< TileInitialisationObserver > >
 
- Public Types inherited from Scroom::Utils::Observable< TileLoadingObserver >
using Observer = std::shared_ptr< TileLoadingObserver >
 
using Ptr = std::shared_ptr< Observable< TileLoadingObserver > >
 
- Public Types inherited from Viewable
using Ptr = std::shared_ptr< Viewable >
 
using WeakPtr = std::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 ()
 
std::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::Base
 Base ()=default
 
 Base (const Base &)=delete
 
 Base (Base &&)=delete
 
Baseoperator= (const Base &)=delete
 
Baseoperator= (Base &&)=delete
 
virtual ~Base ()=default
 
template<typename R >
std::shared_ptr< R > shared_from_this ()
 
template<typename R >
std::shared_ptr< R const > shared_from_this () const
 
- 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 ()
 
virtual void observerAdded (Observer const &observer, Scroom::Bookkeeping::Token const &token)
 
- Protected Member Functions inherited from Scroom::Utils::Observable< TileLoadingObserver >
std::list< ObservergetObservers ()
 
virtual void observerAdded (Observer const &observer, Scroom::Bookkeeping::Token const &token)
 

Private Member Functions

 CompressedTile (int depth, int x, int y, int bpp, const Scroom::MemoryBlobs::PageProvider::Ptr &provider, TileStateInternal state)
 CompressedTile.
 
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
 
Scroom::Utils::WeakKeyMap< ViewInterface::WeakPtr, std::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 = std::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.

23 : depth(depth_)
24 , x(x_)
25 , y(y_)
26 , bpp(bpp_)
27 , state(state_)
28 , provider(provider_)
29 , data(Blob::create(provider_, TILESIZE * TILESIZE * bpp / 8))
30{
31}
Scroom::MemoryBlobs::PageProvider::Ptr provider
Definition tiledbitmaplayer.hh:119
TileStateInternal state
Definition tiledbitmaplayer.hh:116
const int depth
Definition tiledbitmaplayer.hh:110
const int y
Definition tiledbitmaplayer.hh:112
const int x
Definition tiledbitmaplayer.hh:111
Scroom::MemoryBlobs::Blob::Ptr data
Definition tiledbitmaplayer.hh:120
const int bpp
Definition tiledbitmaplayer.hh:113
static Ptr create(PageProvider::Ptr provider, size_t size)
Definition memoryblobs.cc:71
#define TILESIZE
Definition tiledbitmaplayer.hh:27

Member Function Documentation

◆ cleanupState()

void CompressedTile::cleanupState ( )
private

Does some internal state maintenance.

Call only while stateData is locked.

265{
266 if(state == TSI_LOADING_ASYNCHRONOUSLY && !queue.lock())
267 {
268 // Someone, after triggering an asynchronous load, decided not to need
269 // the tile. Changing back to normal.
271 }
272}
ThreadPool::Queue::WeakPtr queue
Definition tiledbitmaplayer.hh:124
@ TSI_NORMAL
Definition tiledbitmaplayer.hh:36
@ TSI_LOADING_ASYNCHRONOUSLY
Definition tiledbitmaplayer.hh:39

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.

287{ viewStates.erase(vi); }
Scroom::Utils::WeakKeyMap< ViewInterface::WeakPtr, std::weak_ptr< TileViewState > > viewStates
Definition tiledbitmaplayer.hh:126
ViewInterface::Ptr const vi
Definition pipette-tests.cc:186

◆ create()

CompressedTile::Ptr CompressedTile::create ( int  depth,
int  x,
int  y,
int  bpp,
const Scroom::MemoryBlobs::PageProvider::Ptr provider,
TileStateInternal  state = TSI_UNINITIALIZED 
)
static
41{
43
44 return tile;
45}
Definition tiledbitmaplayer.hh:105
Tile::WeakPtr tile
Definition tiledbitmaplayer.hh:117
std::shared_ptr< CompressedTile > Ptr
Definition tiledbitmaplayer.hh:107

Referenced by Layer::Layer().

Here is the caller graph for this function:

◆ do_load()

ConstTile::Ptr CompressedTile::do_load ( )
private
134{
135 bool didLoad = false;
136
138 {
139 boost::mutex::scoped_lock const lock(stateData);
140 cleanupState();
142 }
143 {
144 boost::mutex::scoped_lock const lock(tileData);
145 result = constTile.lock(); // This ought to fail
146 if(!result)
147 {
148 result = std::make_shared<ConstTile>(TILESIZE, TILESIZE, bpp, data->getConst());
150 didLoad = true;
151 }
152 }
153 {
154 boost::mutex::scoped_lock const lock(stateData);
155 cleanupState();
157 if(didLoad)
158 {
160 }
161 }
162
163 return result;
164}
void notifyObservers(const ConstTile::Ptr &tile)
Definition compressedtile.cc:274
boost::mutex tileData
Definition tiledbitmaplayer.hh:122
void cleanupState()
Definition compressedtile.cc:264
boost::mutex stateData
Definition tiledbitmaplayer.hh:121
ConstTile::WeakPtr constTile
Definition tiledbitmaplayer.hh:118
std::shared_ptr< ConstTile > Ptr
Definition tile.hh:45
SampleIterator< const uint8_t > result
Definition sampleiterator-tests.cc:94
@ TSI_LOADING_SYNCHRONOUSLY
Definition tiledbitmaplayer.hh:38

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.

88{
90 return result;
91}

◆ 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.

48{
50 if(!result)
51 {
52 result = do_load();
53 }
54
55 return result;
56}
ConstTile::Ptr do_load()
Definition compressedtile.cc:133

Referenced by getTileSync().

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

◆ getState()

TileState CompressedTile::getState ( )
180{
182 switch(state)
183 {
184 case TSI_NORMAL:
185 {
186 Tile::Ptr const t = tile.lock();
187 if(t)
188 {
190 }
191 else
192 {
194 }
195 }
196 break;
199 break;
203 break;
206 break;
207 }
208 return result;
209}
std::shared_ptr< Tile > Ptr
Definition tile.hh:19
ThreadPool t(0)
TileState
Definition tiledbitmapinterface.hh:31
@ TILE_UNLOADED
Definition tiledbitmapinterface.hh:33
@ TILE_LOADED
Definition tiledbitmapinterface.hh:34
@ TILE_UNINITIALIZED
Definition tiledbitmapinterface.hh:32
@ TILE_OUT_OF_BOUNDS
Definition tiledbitmapinterface.hh:35
@ TSI_OUT_OF_BOUNDS
Definition tiledbitmaplayer.hh:37
@ TSI_UNINITIALIZED
Definition tiledbitmaplayer.hh:35

◆ 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.

59{
61 {
62 boost::mutex::scoped_lock const lock(tileData);
63 result = tile.lock();
64 }
65 if(!result)
66 {
67 // Retrieve the const tile, such that all observers are properly
68 // notified of the loading
69 ConstTile::Ptr const temp = getConstTileSync();
70 require(temp);
71 {
72 // Check again. Maybe someone else has beaten us to it...
73 boost::mutex::scoped_lock const lock(tileData);
74 result = tile.lock();
75 }
76 if(!result)
77 {
78 boost::mutex::scoped_lock const lock(tileData);
79 result = std::make_shared<Tile>(TILESIZE, TILESIZE, bpp, data->get());
80 tile = result;
81 }
82 }
83
84 return result;
85}
#define require(expr)
Definition assertions.hh:30
ConstTile::Ptr getConstTileSync()
Definition compressedtile.cc:47

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)
167{
169
170 if(!result)
171 {
172 result = TileViewState::create(shared_from_this<CompressedTile>());
174 }
175
176 return result;
177}
static Ptr create(const std::shared_ptr< CompressedTile > &parent)
Definition tileviewstate.cc:26
std::shared_ptr< TileViewState > Ptr
Definition tileviewstate.hh:29
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

94{
96 Tile::Ptr tile_;
97
98 bool didInitialize = false;
99 {
100 boost::mutex::scoped_lock const stateLock(stateData);
101 boost::mutex::scoped_lock const dataLock(tileData);
102
104 {
105 s = data->initialize(0);
107 didInitialize = true;
108 }
109 }
110
111 if(didInitialize)
112 {
113 tile_ = getTileSync(); // Trigger notifyObservers(), without holding the lock
114 }
115
116 return tile_;
117}
Tile::Ptr getTileSync()
Definition compressedtile.cc:58
std::shared_ptr< void > Stuff
Definition stuff.hh:17
BitmapSurface::Ptr const s
Definition transformpresentation_test.cc:70
Here is the call graph for this function:

◆ notifyObservers()

void CompressedTile::notifyObservers ( const ConstTile::Ptr tile)
private
275{
277 {
278 observer->tileLoaded(tile_);
279 }
280}
Definition observable.hh:71
std::list< Observer > getObservers()
Definition observable.hh:196
Definition tiledbitmaplayer.hh:80
std::shared_ptr< TileLoadingObserver > Ptr
Definition tiledbitmaplayer.hh:82
TestObserver::Ptr observer
Definition observable-tests.cc:91

Referenced by do_load().

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

◆ observerAdded() [1/2]

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

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 :-)

212{
213 observer->tileCreated(shared_from_this<CompressedTile>());
214}

◆ observerAdded() [2/2]

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

Keep track of new TileLoadingObserver registrations.

217{
218 ConstTile::Ptr const result = constTile.lock();
219 ThreadPool::Queue::Ptr queue_ = queue.lock();
220
221 if(!result)
222 {
223 boost::mutex::scoped_lock const lock(stateData);
224 cleanupState();
225 switch(state)
226 {
230 // Need to do nothing. All will be well.
231 break;
232
233 default:
235 // Shouldn't happen
236 break;
237
238 case TSI_NORMAL:
239 // Start an asynchronous load
240 if(!queue_)
241 {
242 queue_ = ThreadPool::Queue::create();
243 queue = queue_;
244 }
245 CpuBound()->schedule([this] { do_load(); }, LOAD_PRIO, queue_);
247
248 break;
249 }
250 }
251
252 // When the last observer goes away, cancel the load
253 if(queue_)
254 {
255 token.add(queue_);
256 }
257
258 if(result)
259 {
260 observer->tileLoaded(result);
261 }
262}
void add(const Stuff &s) const
Definition bookkeepingimpl.hh:191
std::shared_ptr< Queue > Ptr
Definition threadpool.hh:78
static Ptr create()
ThreadPool::Queue.
Definition threadpoolimpl.cc:380
#define LOAD_PRIO
Definition local.hh:10
Scroom::Bookkeeping::Token const token
Definition pipette-tests.cc:269
ThreadPool::Ptr CpuBound()
Definition threadpoolimpl.cc:455
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.

283{
284 // On open, we do nothing. On close, we destroy any resources related to the view.
285}

◆ registerObserver() [1/2]

Scroom::Bookkeeping::Token CompressedTile::registerObserver ( TileInitialisationObserver::WeakPtr const &  observer)
inline
177 {
179 }
Scroom::Bookkeeping::Token registerObserver(ObserverWeak const &observer)
Definition observable.hh:232
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
173 {
175 }
Scroom::Bookkeeping::Token registerStrongObserver(Observer const &observer)
Definition observable.hh:212
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.

120{
121 CompressedTile::Ptr const me = shared_from_this<CompressedTile>();
122 ConstTile::Ptr const t = do_load();
124 {
125 observer->tileFinished(me);
126 }
128 {
129 observer->tileLoaded(t);
130 }
131}
Definition tiledbitmaplayer.hh:49
std::shared_ptr< TileInitialisationObserver > Ptr
Definition tiledbitmaplayer.hh:51
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

Scroom::Utils::WeakKeyMap<ViewInterface::WeakPtr, std::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: