forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdaldefaultasync.cpp
364 lines (315 loc) · 14.2 KB
/
gdaldefaultasync.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/******************************************************************************
*
* Project: GDAL Core
* Purpose: Implementation of GDALDefaultAsyncReader and the
* GDALAsyncReader base class.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2010, Frank Warmerdam
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_priv.h"
#include <cstring>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_string.h"
#include "gdal.h"
CPL_C_START
GDALAsyncReader *GDALGetDefaultAsyncReader(GDALDataset *poDS, int nXOff,
int nYOff, int nXSize, int nYSize,
void *pBuf, int nBufXSize,
int nBufYSize, GDALDataType eBufType,
int nBandCount, int *panBandMap,
int nPixelSpace, int nLineSpace,
int nBandSpace, char **papszOptions);
CPL_C_END
/************************************************************************/
/* ==================================================================== */
/* GDALAsyncReader */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* GDALAsyncReader() */
/************************************************************************/
GDALAsyncReader::GDALAsyncReader()
: poDS(nullptr), nXOff(0), nYOff(0), nXSize(0), nYSize(0), pBuf(nullptr),
nBufXSize(0), nBufYSize(0), eBufType(GDT_Unknown), nBandCount(0),
panBandMap(nullptr), nPixelSpace(0), nLineSpace(0), nBandSpace(0)
{
}
/************************************************************************/
/* ~GDALAsyncReader() */
/************************************************************************/
GDALAsyncReader::~GDALAsyncReader() = default;
/************************************************************************/
/* GetNextUpdatedRegion() */
/************************************************************************/
/**
* \fn GDALAsyncStatusType GDALAsyncReader::GetNextUpdatedRegion( double
* dfTimeout, int* pnBufXOff, int* pnBufYOff, int* pnBufXSize, int* pnBufYSize)
* = 0;
*
* \brief Get async IO update
*
* Provide an opportunity for an asynchronous IO request to update the
* image buffer and return an indication of the area of the buffer that
* has been updated.
*
* The dfTimeout parameter can be used to wait for additional data to
* become available. The timeout does not limit the amount
* of time this method may spend actually processing available data.
*
* The following return status are possible.
* - GARIO_PENDING: No imagery was altered in the buffer, but there is still
* activity pending, and the application should continue to call
* GetNextUpdatedRegion() as time permits.
* - GARIO_UPDATE: Some of the imagery has been updated, but there is still
* activity pending.
* - GARIO_ERROR: Something has gone wrong. The asynchronous request should
* be ended.
* - GARIO_COMPLETE: An update has occurred and there is no more pending work
* on this request. The request should be ended and the buffer used.
*
* @param dfTimeout the number of seconds to wait for additional updates. Use
* -1 to wait indefinitely, or zero to not wait at all if there is no data
* available.
* @param pnBufXOff location to return the X offset of the area of the
* request buffer that has been updated.
* @param pnBufYOff location to return the Y offset of the area of the
* request buffer that has been updated.
* @param pnBufXSize location to return the X size of the area of the
* request buffer that has been updated.
* @param pnBufYSize location to return the Y size of the area of the
* request buffer that has been updated.
*
* @return GARIO_ status, details described above.
*/
/************************************************************************/
/* GDALARGetNextUpdatedRegion() */
/************************************************************************/
/**
* \brief Get async IO update
*
* Provide an opportunity for an asynchronous IO request to update the
* image buffer and return an indication of the area of the buffer that
* has been updated.
*
* The dfTimeout parameter can be used to wait for additional data to
* become available. The timeout does not limit the amount
* of time this method may spend actually processing available data.
*
* The following return status are possible.
* - GARIO_PENDING: No imagery was altered in the buffer, but there is still
* activity pending, and the application should continue to call
* GetNextUpdatedRegion() as time permits.
* - GARIO_UPDATE: Some of the imagery has been updated, but there is still
* activity pending.
* - GARIO_ERROR: Something has gone wrong. The asynchronous request should
* be ended.
* - GARIO_COMPLETE: An update has occurred and there is no more pending work
* on this request. The request should be ended and the buffer used.
*
* This is the same as GDALAsyncReader::GetNextUpdatedRegion()
*
* @param hARIO handle to the async reader.
* @param dfTimeout the number of seconds to wait for additional updates. Use
* -1 to wait indefinitely, or zero to not wait at all if there is no data
* available.
* @param pnBufXOff location to return the X offset of the area of the
* request buffer that has been updated.
* @param pnBufYOff location to return the Y offset of the area of the
* request buffer that has been updated.
* @param pnBufXSize location to return the X size of the area of the
* request buffer that has been updated.
* @param pnBufYSize location to return the Y size of the area of the
* request buffer that has been updated.
*
* @return GARIO_ status, details described above.
*/
GDALAsyncStatusType CPL_STDCALL GDALARGetNextUpdatedRegion(
GDALAsyncReaderH hARIO, double dfTimeout, int *pnBufXOff, int *pnBufYOff,
int *pnBufXSize, int *pnBufYSize)
{
VALIDATE_POINTER1(hARIO, "GDALARGetNextUpdatedRegion", GARIO_ERROR);
return static_cast<GDALAsyncReader *>(hARIO)->GetNextUpdatedRegion(
dfTimeout, pnBufXOff, pnBufYOff, pnBufXSize, pnBufYSize);
}
/************************************************************************/
/* LockBuffer() */
/************************************************************************/
/**
* \fn GDALAsyncReader::LockBuffer(double)
* \brief Lock image buffer.
*
* Locks the image buffer passed into GDALDataset::BeginAsyncReader().
* This is useful to ensure the image buffer is not being modified while
* it is being used by the application. UnlockBuffer() should be used
* to release this lock when it is no longer needed.
*
* @param dfTimeout the time in seconds to wait attempting to lock the buffer.
* -1.0 to wait indefinitely and 0 to not wait at all if it can't be
* acquired immediately. Default is -1.0 (infinite wait).
*
* @return TRUE if successful, or FALSE on an error.
*/
/**/
/**/
int GDALAsyncReader::LockBuffer(double /* dfTimeout */)
{
return TRUE;
}
/************************************************************************/
/* GDALARLockBuffer() */
/************************************************************************/
/**
* \brief Lock image buffer.
*
* Locks the image buffer passed into GDALDataset::BeginAsyncReader().
* This is useful to ensure the image buffer is not being modified while
* it is being used by the application. UnlockBuffer() should be used
* to release this lock when it is no longer needed.
*
* This is the same as GDALAsyncReader::LockBuffer()
*
* @param hARIO handle to async reader.
* @param dfTimeout the time in seconds to wait attempting to lock the buffer.
* -1.0 to wait indefinitely and 0 to not wait at all if it can't be
* acquired immediately. Default is -1.0 (infinite wait).
*
* @return TRUE if successful, or FALSE on an error.
*/
int CPL_STDCALL GDALARLockBuffer(GDALAsyncReaderH hARIO, double dfTimeout)
{
VALIDATE_POINTER1(hARIO, "GDALARLockBuffer", FALSE);
return static_cast<GDALAsyncReader *>(hARIO)->LockBuffer(dfTimeout);
}
/************************************************************************/
/* UnlockBuffer() */
/************************************************************************/
/**
* \brief Unlock image buffer.
*
* Releases a lock on the image buffer previously taken with LockBuffer().
*/
void GDALAsyncReader::UnlockBuffer()
{
}
/************************************************************************/
/* GDALARUnlockBuffer() */
/************************************************************************/
/**
* \brief Unlock image buffer.
*
* Releases a lock on the image buffer previously taken with LockBuffer().
*
* This is the same as GDALAsyncReader::UnlockBuffer()
*
* @param hARIO handle to async reader.
*/
void CPL_STDCALL GDALARUnlockBuffer(GDALAsyncReaderH hARIO)
{
VALIDATE_POINTER0(hARIO, "GDALARUnlockBuffer");
static_cast<GDALAsyncReader *>(hARIO)->UnlockBuffer();
}
/************************************************************************/
/* ==================================================================== */
/* GDALDefaultAsyncReader */
/* ==================================================================== */
/************************************************************************/
class GDALDefaultAsyncReader : public GDALAsyncReader
{
private:
char **papszOptions = nullptr;
CPL_DISALLOW_COPY_ASSIGN(GDALDefaultAsyncReader)
public:
GDALDefaultAsyncReader(GDALDataset *poDS, int nXOff, int nYOff, int nXSize,
int nYSize, void *pBuf, int nBufXSize, int nBufYSize,
GDALDataType eBufType, int nBandCount,
int *panBandMap, int nPixelSpace, int nLineSpace,
int nBandSpace, char **papszOptions);
~GDALDefaultAsyncReader() override;
GDALAsyncStatusType GetNextUpdatedRegion(double dfTimeout, int *pnBufXOff,
int *pnBufYOff, int *pnBufXSize,
int *pnBufYSize) override;
};
/************************************************************************/
/* GDALGetDefaultAsyncReader() */
/************************************************************************/
GDALAsyncReader *GDALGetDefaultAsyncReader(GDALDataset *poDS, int nXOff,
int nYOff, int nXSize, int nYSize,
void *pBuf, int nBufXSize,
int nBufYSize, GDALDataType eBufType,
int nBandCount, int *panBandMap,
int nPixelSpace, int nLineSpace,
int nBandSpace, char **papszOptions)
{
return new GDALDefaultAsyncReader(poDS, nXOff, nYOff, nXSize, nYSize, pBuf,
nBufXSize, nBufYSize, eBufType,
nBandCount, panBandMap, nPixelSpace,
nLineSpace, nBandSpace, papszOptions);
}
/************************************************************************/
/* GDALDefaultAsyncReader() */
/************************************************************************/
GDALDefaultAsyncReader::GDALDefaultAsyncReader(
GDALDataset *poDSIn, int nXOffIn, int nYOffIn, int nXSizeIn, int nYSizeIn,
void *pBufIn, int nBufXSizeIn, int nBufYSizeIn, GDALDataType eBufTypeIn,
int nBandCountIn, int *panBandMapIn, int nPixelSpaceIn, int nLineSpaceIn,
int nBandSpaceIn, char **papszOptionsIn)
{
poDS = poDSIn;
nXOff = nXOffIn;
nYOff = nYOffIn;
nXSize = nXSizeIn;
nYSize = nYSizeIn;
pBuf = pBufIn;
nBufXSize = nBufXSizeIn;
nBufYSize = nBufYSizeIn;
eBufType = eBufTypeIn;
nBandCount = nBandCountIn;
panBandMap = static_cast<int *>(CPLMalloc(sizeof(int) * nBandCountIn));
if (panBandMapIn != nullptr)
memcpy(panBandMap, panBandMapIn, sizeof(int) * nBandCount);
else
{
for (int i = 0; i < nBandCount; i++)
panBandMap[i] = i + 1;
}
nPixelSpace = nPixelSpaceIn;
nLineSpace = nLineSpaceIn;
nBandSpace = nBandSpaceIn;
papszOptions = CSLDuplicate(papszOptionsIn);
}
/************************************************************************/
/* ~GDALDefaultAsyncReader() */
/************************************************************************/
GDALDefaultAsyncReader::~GDALDefaultAsyncReader()
{
CPLFree(panBandMap);
CSLDestroy(papszOptions);
}
/************************************************************************/
/* GetNextUpdatedRegion() */
/************************************************************************/
GDALAsyncStatusType
GDALDefaultAsyncReader::GetNextUpdatedRegion(double /*dfTimeout*/,
int *pnBufXOff, int *pnBufYOff,
int *pnBufXSize, int *pnBufYSize)
{
CPLErr eErr;
eErr =
poDS->RasterIO(GF_Read, nXOff, nYOff, nXSize, nYSize, pBuf, nBufXSize,
nBufYSize, eBufType, nBandCount, panBandMap, nPixelSpace,
nLineSpace, nBandSpace, nullptr);
*pnBufXOff = 0;
*pnBufYOff = 0;
*pnBufXSize = nBufXSize;
*pnBufYSize = nBufYSize;
if (eErr == CE_None)
return GARIO_COMPLETE;
else
return GARIO_ERROR;
}