/gdal/gcore/gdaldefaultasync.cpp

https://github.com/aashish24/gdal-cmake · C++ · 320 lines · 143 code · 45 blank · 132 comment · 5 complexity · 5f22f68b6089d60b24fcf57a416d70b3 MD5 · raw file

  1. /******************************************************************************
  2. * $Id: gdaldataset.cpp 16796 2009-04-17 23:35:04Z normanb $
  3. *
  4. * Project: GDAL Core
  5. * Purpose: Implementation of GDALDefaultAsyncReader and the
  6. * GDALAsyncReader base class.
  7. * Author: Frank Warmerdam, warmerdam@pobox.com
  8. *
  9. ******************************************************************************
  10. * Copyright (c) 2010, Frank Warmerdam
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ****************************************************************************/
  30. #include "gdal_priv.h"
  31. CPL_CVSID("$Id: gdaldataset.cpp 16796 2009-04-17 23:35:04Z normanb $");
  32. CPL_C_START
  33. GDALAsyncReader *
  34. GDALGetDefaultAsyncReader( GDALDataset* poDS,
  35. int nXOff, int nYOff, int nXSize, int nYSize,
  36. void *pBuf, int nBufXSize, int nBufYSize,
  37. GDALDataType eBufType,
  38. int nBandCount, int* panBandMap,
  39. int nPixelSpace, int nLineSpace,
  40. int nBandSpace, char **papszOptions );
  41. CPL_C_END
  42. /************************************************************************/
  43. /* ==================================================================== */
  44. /* GDALAsyncReader */
  45. /* ==================================================================== */
  46. /************************************************************************/
  47. /************************************************************************/
  48. /* GDALAsyncReader() */
  49. /************************************************************************/
  50. GDALAsyncReader::GDALAsyncReader()
  51. {
  52. }
  53. /************************************************************************/
  54. /* ~GDALAsyncReader() */
  55. /************************************************************************/
  56. GDALAsyncReader::~GDALAsyncReader()
  57. {
  58. }
  59. /************************************************************************/
  60. /* GetNextUpdatedRegion() */
  61. /************************************************************************/
  62. /**
  63. * \fn GDALAsyncStatusType GDALAsyncReader::GetNextUpdatedRegion( double dfTimeout, int* pnBufXOff, int* pnBufYOff, int* pnBufXSize, int* pnBufXSize) = 0;
  64. *
  65. * \brief Get async IO update
  66. *
  67. * Provide an opportunity for an asynchronous IO request to update the
  68. * image buffer and return an indication of the area of the buffer that
  69. * has been updated.
  70. *
  71. * The dfTimeout parameter can be used to wait for additional data to
  72. * become available. The timeout does not limit the amount
  73. * of time this method may spend actually processing available data.
  74. *
  75. * The following return status are possible.
  76. * - GARIO_PENDING: No imagery was altered in the buffer, but there is still
  77. * activity pending, and the application should continue to call
  78. * GetNextUpdatedRegion() as time permits.
  79. * - GARIO_UPDATE: Some of the imagery has been updated, but there is still
  80. * activity pending.
  81. * - GARIO_ERROR: Something has gone wrong. The asynchronous request should
  82. * be ended.
  83. * - GARIO_COMPLETE: An update has occured and there is no more pending work
  84. * on this request. The request should be ended and the buffer used.
  85. *
  86. * @param dfTimeout the number of seconds to wait for additional updates. Use
  87. * -1 to wait indefinately, or zero to not wait at all if there is no data
  88. * available.
  89. * @param pnBufXOff location to return the X offset of the area of the
  90. * request buffer that has been updated.
  91. * @param pnBufYOff location to return the Y offset of the area of the
  92. * request buffer that has been updated.
  93. * @param pnBufXSize location to return the X size of the area of the
  94. * request buffer that has been updated.
  95. * @param pnBufYSize location to return the Y size of the area of the
  96. * request buffer that has been updated.
  97. *
  98. * @return GARIO_ status, details described above.
  99. */
  100. /************************************************************************/
  101. /* GDALARGetNextUpdatedRegion() */
  102. /************************************************************************/
  103. GDALAsyncStatusType CPL_STDCALL
  104. GDALARGetNextUpdatedRegion(GDALAsyncReaderH hARIO, double timeout,
  105. int* pnxbufoff, int* pnybufoff,
  106. int* pnxbufsize, int* pnybufsize)
  107. {
  108. VALIDATE_POINTER1(hARIO, "GDALARGetNextUpdatedRegion", GARIO_ERROR);
  109. return ((GDALAsyncReader *)hARIO)->GetNextUpdatedRegion(
  110. timeout, pnxbufoff, pnybufoff, pnxbufsize, pnybufsize);
  111. }
  112. /************************************************************************/
  113. /* LockBuffer() */
  114. /************************************************************************/
  115. /**
  116. * \brief Lock image buffer.
  117. *
  118. * Locks the image buffer passed into GDALDataset::BeginAsyncReader().
  119. * This is useful to ensure the image buffer is not being modified while
  120. * it is being used by the application. UnlockBuffer() should be used
  121. * to release this lock when it is no longer needed.
  122. *
  123. * @param dfTimeout the time in seconds to wait attempting to lock the buffer.
  124. * -1.0 to wait indefinately and 0 to not wait at all if it can't be
  125. * acquired immediately. Default is -1.0 (infinite wait).
  126. *
  127. * @return TRUE if successful, or FALSE on an error.
  128. */
  129. int GDALAsyncReader::LockBuffer( double dfTimeout )
  130. {
  131. return TRUE;
  132. }
  133. /************************************************************************/
  134. /* GDALARLockBuffer() */
  135. /************************************************************************/
  136. int CPL_STDCALL GDALARLockBuffer(GDALAsyncReaderH hARIO, double dfTimeout )
  137. {
  138. VALIDATE_POINTER1(hARIO, "GDALARLockBuffer",FALSE);
  139. return ((GDALAsyncReader *)hARIO)->LockBuffer( dfTimeout );
  140. }
  141. /************************************************************************/
  142. /* UnlockBuffer() */
  143. /************************************************************************/
  144. /**
  145. * \brief Unlock image buffer.
  146. *
  147. * Releases a lock on the image buffer previously taken with LockBuffer().
  148. */
  149. void GDALAsyncReader::UnlockBuffer()
  150. {
  151. }
  152. /************************************************************************/
  153. /* GDALARUnlockBuffer() */
  154. /************************************************************************/
  155. void CPL_STDCALL GDALARUnlockBuffer(GDALAsyncReaderH hARIO)
  156. {
  157. VALIDATE_POINTER0(hARIO, "GDALARUnlockBuffer");
  158. ((GDALAsyncReader *)hARIO)->UnlockBuffer();
  159. }
  160. /************************************************************************/
  161. /* ==================================================================== */
  162. /* GDALDefaultAsyncReader */
  163. /* ==================================================================== */
  164. /************************************************************************/
  165. class GDALDefaultAsyncReader : public GDALAsyncReader
  166. {
  167. private:
  168. char ** papszOptions;
  169. public:
  170. GDALDefaultAsyncReader(GDALDataset* poDS,
  171. int nXOff, int nYOff,
  172. int nXSize, int nYSize,
  173. void *pBuf,
  174. int nBufXSize, int nBufYSize,
  175. GDALDataType eBufType,
  176. int nBandCount, int* panBandMap,
  177. int nPixelSpace, int nLineSpace,
  178. int nBandSpace, char **papszOptions);
  179. ~GDALDefaultAsyncReader();
  180. virtual GDALAsyncStatusType GetNextUpdatedRegion(double dfTimeout,
  181. int* pnBufXOff,
  182. int* pnBufYOff,
  183. int* pnBufXSize,
  184. int* pnBufYSize);
  185. };
  186. /************************************************************************/
  187. /* GDALGetDefaultAsyncReader() */
  188. /************************************************************************/
  189. GDALAsyncReader *
  190. GDALGetDefaultAsyncReader( GDALDataset* poDS,
  191. int nXOff, int nYOff,
  192. int nXSize, int nYSize,
  193. void *pBuf,
  194. int nBufXSize, int nBufYSize,
  195. GDALDataType eBufType,
  196. int nBandCount, int* panBandMap,
  197. int nPixelSpace, int nLineSpace,
  198. int nBandSpace, char **papszOptions)
  199. {
  200. return new GDALDefaultAsyncReader( poDS,
  201. nXOff, nYOff, nXSize, nYSize,
  202. pBuf, nBufXSize, nBufYSize, eBufType,
  203. nBandCount, panBandMap,
  204. nPixelSpace, nLineSpace, nBandSpace,
  205. papszOptions );
  206. }
  207. /************************************************************************/
  208. /* GDALDefaultAsyncReader() */
  209. /************************************************************************/
  210. GDALDefaultAsyncReader::
  211. GDALDefaultAsyncReader( GDALDataset* poDS,
  212. int nXOff, int nYOff,
  213. int nXSize, int nYSize,
  214. void *pBuf,
  215. int nBufXSize, int nBufYSize,
  216. GDALDataType eBufType,
  217. int nBandCount, int* panBandMap,
  218. int nPixelSpace, int nLineSpace,
  219. int nBandSpace, char **papszOptions)
  220. {
  221. this->poDS = poDS;
  222. this->nXOff = nXOff;
  223. this->nYOff = nYOff;
  224. this->nXSize = nXSize;
  225. this->nYSize = nYSize;
  226. this->pBuf = pBuf;
  227. this->nBufXSize = nBufXSize;
  228. this->nBufYSize = nBufYSize;
  229. this->eBufType = eBufType;
  230. this->nBandCount = nBandCount;
  231. this->panBandMap = (int *) CPLMalloc(sizeof(int)*nBandCount);
  232. if( panBandMap != NULL )
  233. memcpy( this->panBandMap, panBandMap, sizeof(int)*nBandCount );
  234. else
  235. {
  236. for( int i = 0; i < nBandCount; i++ )
  237. this->panBandMap[i] = i+1;
  238. }
  239. this->nPixelSpace = nPixelSpace;
  240. this->nLineSpace = nLineSpace;
  241. this->nBandSpace = nBandSpace;
  242. this->papszOptions = CSLDuplicate(papszOptions);
  243. }
  244. /************************************************************************/
  245. /* ~GDALDefaultAsyncReader() */
  246. /************************************************************************/
  247. GDALDefaultAsyncReader::~GDALDefaultAsyncReader()
  248. {
  249. CPLFree( panBandMap );
  250. CSLDestroy( papszOptions );
  251. }
  252. /************************************************************************/
  253. /* GetNextUpdatedRegion() */
  254. /************************************************************************/
  255. GDALAsyncStatusType
  256. GDALDefaultAsyncReader::GetNextUpdatedRegion(double dfTimeout,
  257. int* pnBufXOff,
  258. int* pnBufYOff,
  259. int* pnBufXSize,
  260. int* pnBufYSize )
  261. {
  262. CPLErr eErr;
  263. eErr = poDS->RasterIO( GF_Read, nXOff, nYOff, nXSize, nYSize,
  264. pBuf, nBufXSize, nBufYSize, eBufType,
  265. nBandCount, panBandMap,
  266. nPixelSpace, nLineSpace, nBandSpace );
  267. *pnBufXOff = 0;
  268. *pnBufYOff = 0;
  269. *pnBufXSize = nBufXSize;
  270. *pnBufYSize = nBufYSize;
  271. if( eErr == CE_None )
  272. return GARIO_COMPLETE;
  273. else
  274. return GARIO_ERROR;
  275. }