MeVisLabToolboxReference
MeVis/Foundation/Sources/MLBackgroundTasks/mlImagingBackgroundTask.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00009 //----------------------------------------------------------------------------------
00010 
00011 
00012 #ifndef __mlImagingBackgroundTask_H
00013 #define __mlImagingBackgroundTask_H
00014 
00015 
00016 // Local includes
00017 #include "MLBackgroundTasksSystem.h"
00018 
00019 #include "mlMessagingBackgroundTask.h"
00020 #include "mlManagedSubImage.h"
00021 #include "mlTimeCounter.h"
00022 #include "host/mlTileRequest.h"
00023 
00024 ML_START_NAMESPACE
00025 
00026 //-----------------------------------------------------------------------
00027 
00029 typedef MLuint32 BackgroundRequestId;
00030 
00031 class MLBACKGROUNDTASKS_EXPORT ImagingBackgroundTask : public MessagingBackgroundTask
00032 {
00033   ML_DISALLOW_COPY_AND_ASSIGN(ImagingBackgroundTask)
00034 
00035 public:
00036 
00037   ImagingBackgroundTask(void* owner);
00038   virtual ~ImagingBackgroundTask();
00039 
00041   enum TileRequestFlags {
00042     UseMemoryManager = TileRequest::UseMemoryManager,  //<! This will allocate the memory using the MemoryManager
00043     UseMLAlloc       = TileRequest::UseMLAlloc,        //<! This will allocate the memory using MLAlloc
00044     NoAllocation     = TileRequest::NoAllocation,      //<! This can be used if no data should be allocated at all (e.g. for processAllPages)
00045     ExternalBuffer   = TileRequest::ExternalBuffer,    //<! This can be used to provide the data by the user
00046     AllocationPolicyMask = 0xf,                        //<! Mask to get a AsyncTileRequest ::AllocationPolicy enum from the flags value.
00047 
00048     RequestReadOnlyPage     = 16,                      //<! Request the data of the AsyncTileRequest  as read-only page data, this requires that the given box
00049                                                        //<! matches a page's box, datatype and that scale/shift has the default value. This override the
00050                                                        //<! allocation policy above.
00051     ReceiveProgressMessages = 32                       //<! Receive progress messages for the AsyncTileRequest .
00052   };
00053 
00055   class AsyncTileRequest  {
00056   public:
00057     AsyncTileRequest ():_tileId(0),_task(NULL) {};
00058     AsyncTileRequest (BackgroundRequestId tileId, ImagingBackgroundTask* task)
00059       : _tileId(tileId), _task(task)
00060     {}
00061 
00064     ManagedSubImage* getTile() {
00065       bool found;
00066       return _task->getFinishedRequest(_tileId, found);
00067     };
00068 
00069   private:
00070     BackgroundRequestId   _tileId;
00071     ImagingBackgroundTask* _task;
00072   };
00073 
00074 
00076   class ProcessAllPagesRequest {
00077   public:
00078     ProcessAllPagesRequest():_id(0),_task(NULL) {};
00079     ProcessAllPagesRequest(BackgroundRequestId tileId, ImagingBackgroundTask* task)
00080       : _id(tileId), _task(task)
00081     {}
00082 
00087     bool waitUntilFinished() {
00088       return _task->waitForProcessAllPages(_id);
00089     }
00090 
00091   private:
00092     BackgroundRequestId _id;
00093     ImagingBackgroundTask* _task;
00094   };
00095 
00096 protected:
00098   BackgroundRequestId generateBackgroundRequestId() {
00099     return _nextTileRequestId++;
00100   }
00101 
00102 private:
00103   friend class  BackgroundTaskMLGetTileResultMessage;
00104   friend class  BackgroundTaskMLProcessAllPagesResultMessage;
00105   friend class  AsyncTileRequest ;
00106   friend class  ProcessAllPagesRequest;
00107 
00109   struct RequestResult
00110   {
00111     RequestResult(BackgroundRequestId id, ProcessAllPagesHandler* handler) {
00112       this->id = id;
00113       this->handler = handler;
00114       this->image = NULL;
00115     }
00116 
00117     RequestResult(BackgroundRequestId id, ManagedSubImage* image) {
00118       this->id = id;
00119       this->image = image;
00120       this->handler = NULL;
00121     }
00122 
00123     void free() {
00124       ML_DELETE(image);
00125       ML_DELETE(handler);
00126     }
00127 
00128     // public access to variables
00129     BackgroundRequestId     id;
00130     ProcessAllPagesHandler* handler;
00131     ManagedSubImage*        image;
00132   };
00133 
00136   ManagedSubImage* takeRequestIfAvailable(BackgroundRequestId tileId, bool& found);
00137 
00139   ManagedSubImage* getFinishedRequest(BackgroundRequestId tileId, bool& found);
00140 
00142   bool             waitForProcessAllPages(BackgroundRequestId id);
00143 
00145   void addFinishedRequest(BackgroundRequestId tileId, ManagedSubImage* image);
00146 
00148   void addFinishedRequest(BackgroundRequestId tileId, ProcessAllPagesHandler* handler);
00149 
00151   std::deque<RequestResult> _availableTiles;
00153   BackgroundRequestId _nextTileRequestId;
00154 
00155 };
00156 
00157 
00158 ML_END_NAMESPACE
00159 
00160 #endif
00161 
00162