MeVisLab Toolbox Reference
mlTileRequest.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2009, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_TILE_REQUEST_H
14#define ML_TILE_REQUEST_H
15
17
18// ML-includes
19#include "mlInitSystemML.h"
20
21#include "mlSubImage.h"
22#include "mlPagedImage.h"
23#include "mlPageIDIterator.h"
24
25#include <set>
26
27ML_START_NAMESPACE
28
29class TileRequest;
30class PageRequest;
31class PageRequestQueueInterface;
32
34typedef void TileRequestFinishedCB(void* data, TileRequest* request);
35
36//-------------------------------------------------------------------------------------------
48//-------------------------------------------------------------------------------------------
49class ML_UNIX_ONLY_EXPORT(MLEXPORT) TileRequest
50{
51
52public:
54 static TileRequest* allocate();
55
57 static void deallocate(TileRequest* request);
58
60 static void clearFreeList();
61
62private:
63 TileRequest();
64
66 void cleanup();
67
69 TileRequest* _freeListNext;
70
72 static TileRequest* _freeListHead;
74 static Mutex _freeListMutex;
75
76public:
77
82 void init(PagedImage* inputImage, const SubImageBox& box, MLDataType dataType,
83 const ScaleShiftData& scaleShift, bool readOnlyInputTile = false, bool tryToBecomeMemoryImage = false);
84
87 UseMemoryManager = 0, //<! This will allocate the memory using the MemoryManager.
88 UseMLAlloc = 1, //<! This will allocate the memory using MLAlloc.
89 NoAllocation = 2, //<! This can be used if no data should be allocated at all (e.g., for processAllPages).
90 ExternalBuffer = 3 //<! This can be used to provide the data by the user and to avoid freeing the data if something goes wrong.
91 };
92
93 //------------------------------------------------------
96 //------------------------------------------------------
97
102
105
109 void setNeededBy(PageRequest* request) {
110 _neededByPageRequest = request;
111 }
112
116
121
123
124 //------------------------------------------------------
127 //------------------------------------------------------
128
134
137
140
142 void forgetData() { _subImage.setData(nullptr); }
143
146
148 const SubImage& getSubImage() const { return _subImage; }
149
152 void setExternalDataBuffer(void* data) {
154 _subImage.setData(data);
155 }
156
158 void setUseTileReadOnly(bool flag) { _readOnlyInputTile = flag; }
159
161
162 //------------------------------------------------------
165 //------------------------------------------------------
166
168 bool hasFinished() const;
169
173 _finishedCB = cb;
174 _finishedCBData = data;
175 }
176
183
185 double getProgress() const;
186
188
189 //------------------------------------------------------
192 //------------------------------------------------------
193
195 bool hasError() const { return _error!=ML_RESULT_OK; }
196
199
201 MLErrorCode getError() const { return _error; }
202
206
211
213 void collectPageRequestsWithErrors(std::set<PageRequest*>& result);
214
216
217 //------------------------------------------------------
220 //------------------------------------------------------
221
224
226 int getProcessingScope() const { return _processingScope; }
227
229 void setProcessingScope(int scope) { _processingScope = scope; }
230
232 const SubImageBox& getBox() const { return _subImage.getBox(); }
233
236
239
241
242protected:
244 friend class PageRequestCursor;
245
253
256 bool copyDataFromSubImg(SubImage& pageImage);
257
260
263
266
269
272
277
280
283
286
290
293
296
299
302
307
312
313public:
315 static void enableAllocationFailure(int count = 1);
317 static void disableAllocationFailure() { _forceAllocationFailure = false; }
318
319private:
320 static bool _forceAllocationFailure;
321 static int _forceAllocationFailureCount;
322 static int _forceAllocationFailureModulo;
323
324};
325
326ML_END_NAMESPACE
327
328#endif //of __mlTileRequest_H
329
Virtual interface for queuing PageRequests that are ready for processing.
void createInputPageIds()
Creates the page IDs for all required input pages.
int getProcessingScope() const
Returns the assigned processing scope.
bool isBlockedByMemoryImage()
void setProcessingScope(int scope)
Returns the assigned processing scope.
static void enableAllocationFailure(int count=1)
Enables failing on every count allocation.
void freeData()
Frees the allocated data using SubImage::free(), except if the AllocationPolicy is UserAllocation.
bool propagateCancellationDownIfRequestNotNeededAnymore()
void forgetData()
Tells the tile to forget its data so that it is not freed in freeData().
int _processingScope
The processing scope.
void allocateData()
Allocates the data using the internal AllocationPolicy.
static TileRequest * allocate()
Tile requests are allocated via the allocate method, they can not be created with new.
double getProgress() const
Returns the progress of this tile (TODO: not very detailed yet, only the ratio of copied/total pages)...
TileRequestFinishedCB * _internalFinishedCB
Callback to signal that the TileRequest has finished.
bool _readOnlyInputTile
Flag that the TileRequest should try to not copy data, since it will not be written to.
PageIDIterator _inputPageIds
bool copyDataFromSubImg(SubImage &pageImage)
void sendTileRequestFinished(PageRequestQueueInterface &queue)
Tells the dependend page requests that the tile request has finished.
void setUseTileReadOnly(bool flag)
This allows to make read-only use of input pages or memory image if box/data type match and shift/sca...
MLint _inputPagesNeeded
Overall number of pages needed. This is getNumPages() of _inputPageIds at the beginning and will decr...
bool hasFinished() const
Returns whether the request has finished. If an error happened, this returns true as well.
bool _tileWillBecomeMemoryImage
Flag that the TileRequest should become the MemoryImage when it has finished.
PagedImage * _image
The image from which the data should be requested.
void setInternalTileRequestFinishedCB(TileRequestFinishedCB *cb, void *data)
void emitFinishedCallback()
Calls the finished callbacks.
void propagateErrorUpwards(MLErrorCode error)
MLint _traversalCursorPosition
Current _inputPageIds position to indicate the next page request to create.
const SubImageBox & getBox() const
Returns the box that this tile request covers.
void setError(MLErrorCode error)
Sets the error that happened.
static void disableAllocationFailure()
Disables allocation failure (default!).
Mutex _inputPagesNeededMutex
TODO: replace above with atomic counter and remove the mutex.
PageRequest * createPageRequest(MLint pageId, PageRequestQueueInterface &queue)
AllocationPolicy getAllocationPolicy() const
Returns the allocation policy.
bool hasError() const
Returns whether the request has an error (and the data is thus unusable/invalid).
MLErrorCode _error
The current error state.
friend class PageRequestCursor
The cursor needs to be our friend, since it has to access the internal traversal state.
void updateSourceImageExtent()
Updates the source image extent according to the image.
void setTileRequestFinishedCB(TileRequestFinishedCB *cb, void *data)
static void deallocate(TileRequest *request)
Tile requests are deallocated with the deallocate method, they can not be deleted directly.
const SubImage & getSubImage() const
Returns the stored subimage including the data pointer.
void collectPageRequestsWithErrors(std::set< PageRequest * > &result)
Collects all page requests that have error()!=ML_RESULT_OK up to the roots of the tree.
bool prepareForCursorVisit()
void setAllocationPolicy(AllocationPolicy policy)
void setNeededBy(PageRequest *request)
AllocationPolicy
Defines how the TileRequest will allocate its memory.
MLErrorCode getError() const
Returns the error that happened (or ML_RESULT_OK).
bool intersectsPagedImage() const
Returns whether the request has an intersection with the paged image.
PageRequest * _neededByPageRequest
The PageRequest that depends on this TileRequest (or NULL if it is a root TileRequest).
SubImage _subImage
The subimage that represents the TileRequest's box, data type, and data.
MLErrorCode updateImageProperties()
Updates the image properties of the complete graph starting at this tile request via the host.
void init(PagedImage *inputImage, const SubImageBox &box, MLDataType dataType, const ScaleShiftData &scaleShift, bool readOnlyInputTile=false, bool tryToBecomeMemoryImage=false)
TileRequestFinishedCB * _finishedCB
Callback to signal that the TileRequest has finished.
AllocationPolicy _allocationPolicy
Defines which allocation method to use.
void * _internalFinishedCBData
Callback data to signal that the TileRequest has finished.
ScaleShiftData _scaleShiftData
The additional scale/shift information.
void setExternalDataBuffer(void *data)
void pageRequestFinished(SubImage &pageImage, PageRequestQueueInterface &queue)
void * _finishedCBData
Callback data to signal that the TileRequest has finished.
static void clearFreeList()
Clears the internal free list of deallocated TileRequests.
MLint32 MLDataType
Definition mlTypeDefs.h:595
MLint32 MLErrorCode
Type of an ML Error code.
Definition mlTypeDefs.h:715
#define ML_RESULT_OK
No error. Everything seems to be okay.
Definition mlTypeDefs.h:723
boost::mutex Mutex
Definition mlMutex.h:39
MLint64 MLint
Definition mlTypeDefs.h:489
void TileRequestFinishedCB(void *data, TileRequest *request)
Callback for a finished TileRequest.
TScaleShiftData< MLdouble > ScaleShiftData
Double version of TScaleShiftData for maximum reasonable precision.
TSubImageBox< MLint > SubImageBox
Defines the standard SubImageBox type used in the ML. Its size varies with the size of the MLint type...