/src/gdal/gcore/gdalproxypool.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL Core |
4 | | * Purpose: A dataset and raster band classes that differ the opening of the |
5 | | * underlying dataset in a limited pool of opened datasets. |
6 | | * Author: Even Rouault <even dot rouault at spatialys.com> |
7 | | * |
8 | | ****************************************************************************** |
9 | | * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include "cpl_port.h" |
15 | | #include "gdal_proxy.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cstdio> |
19 | | #include <cstdlib> |
20 | | #include <cstring> |
21 | | |
22 | | #include "cpl_conv.h" |
23 | | #include "cpl_error.h" |
24 | | #include "cpl_hash_set.h" |
25 | | #include "cpl_multiproc.h" |
26 | | #include "cpl_string.h" |
27 | | #include "gdal.h" |
28 | | #include "gdal_priv.h" |
29 | | |
30 | | //! @cond Doxygen_Suppress |
31 | | |
32 | | /* We *must* share the same mutex as the gdaldataset.cpp file, as we are */ |
33 | | /* doing GDALOpen() calls that can indirectly call GDALOpenShared() on */ |
34 | | /* an auxiliary dataset ... */ |
35 | | /* Then we could get dead-locks in multi-threaded use case */ |
36 | | |
37 | | /* ******************************************************************** */ |
38 | | /* GDALDatasetPool */ |
39 | | /* ******************************************************************** */ |
40 | | |
41 | | /* This class is a singleton that maintains a pool of opened datasets */ |
42 | | /* The cache uses a LRU strategy */ |
43 | | |
44 | | class GDALDatasetPool; |
45 | | static GDALDatasetPool *singleton = nullptr; |
46 | | |
47 | | void GDALNullifyProxyPoolSingleton() |
48 | 0 | { |
49 | 0 | singleton = nullptr; |
50 | 0 | } |
51 | | |
52 | | struct _GDALProxyPoolCacheEntry |
53 | | { |
54 | | GIntBig responsiblePID; |
55 | | char *pszFileNameAndOpenOptions; |
56 | | char *pszOwner; |
57 | | GDALDataset *poDS; |
58 | | GIntBig nRAMUsage; |
59 | | |
60 | | /* Ref count of the cached dataset */ |
61 | | int refCount; |
62 | | |
63 | | GDALProxyPoolCacheEntry *prev; |
64 | | GDALProxyPoolCacheEntry *next; |
65 | | }; |
66 | | |
67 | | // This variable prevents a dataset that is going to be opened in |
68 | | // GDALDatasetPool::_RefDataset from increasing refCount if, during its |
69 | | // opening, it creates a GDALProxyPoolDataset. |
70 | | // We increment it before opening or closing a cached dataset and decrement |
71 | | // it afterwards |
72 | | // The typical use case is a VRT made of simple sources that are VRT |
73 | | // We don't want the "inner" VRT to take a reference on the pool, otherwise |
74 | | // there is a high chance that this reference will not be dropped and the pool |
75 | | // remain ghost. |
76 | | static thread_local int refCountOfDisabledRefCount = 0; |
77 | | |
78 | | class GDALDatasetPool |
79 | | { |
80 | | private: |
81 | | bool bInDestruction = false; |
82 | | |
83 | | /* Ref count of the pool singleton */ |
84 | | /* Taken by "toplevel" GDALProxyPoolDataset in its constructor and released |
85 | | */ |
86 | | /* in its destructor. See also refCountOfDisabledRefCount for the difference |
87 | | */ |
88 | | /* between toplevel and inner GDALProxyPoolDataset */ |
89 | | int refCount = 0; |
90 | | |
91 | | int maxSize = 0; |
92 | | int currentSize = 0; |
93 | | int64_t nMaxRAMUsage = 0; |
94 | | int64_t nRAMUsage = 0; |
95 | | GDALProxyPoolCacheEntry *firstEntry = nullptr; |
96 | | GDALProxyPoolCacheEntry *lastEntry = nullptr; |
97 | | |
98 | | /* Caution : to be sure that we don't run out of entries, size must be at */ |
99 | | /* least greater or equal than the maximum number of threads */ |
100 | | explicit GDALDatasetPool(int maxSize, int64_t nMaxRAMUsage); |
101 | | ~GDALDatasetPool(); |
102 | | GDALProxyPoolCacheEntry *_RefDataset(const char *pszFileName, |
103 | | GDALAccess eAccess, |
104 | | CSLConstList papszOpenOptions, |
105 | | int bShared, bool bForceOpen, |
106 | | const char *pszOwner); |
107 | | void _CloseDatasetIfZeroRefCount(const char *pszFileName, |
108 | | CSLConstList papszOpenOptions, |
109 | | GDALAccess eAccess, const char *pszOwner); |
110 | | |
111 | | #ifdef DEBUG_PROXY_POOL |
112 | | // cppcheck-suppress unusedPrivateFunction |
113 | | void ShowContent(); |
114 | | void CheckLinks(); |
115 | | #endif |
116 | | |
117 | | CPL_DISALLOW_COPY_ASSIGN(GDALDatasetPool) |
118 | | |
119 | | public: |
120 | | static void Ref(); |
121 | | static void Unref(); |
122 | | static GDALProxyPoolCacheEntry *RefDataset(const char *pszFileName, |
123 | | GDALAccess eAccess, |
124 | | char **papszOpenOptions, |
125 | | int bShared, bool bForceOpen, |
126 | | const char *pszOwner); |
127 | | static void UnrefDataset(GDALProxyPoolCacheEntry *cacheEntry); |
128 | | static void CloseDatasetIfZeroRefCount(const char *pszFileName, |
129 | | CSLConstList papszOpenOptions, |
130 | | GDALAccess eAccess, |
131 | | const char *pszOwner); |
132 | | |
133 | | static void PreventDestroy(); |
134 | | static void ForceDestroy(); |
135 | | }; |
136 | | |
137 | | /************************************************************************/ |
138 | | /* GDALDatasetPool() */ |
139 | | /************************************************************************/ |
140 | | |
141 | | GDALDatasetPool::GDALDatasetPool(int maxSizeIn, int64_t nMaxRAMUsageIn) |
142 | 1.05k | : maxSize(maxSizeIn), nMaxRAMUsage(nMaxRAMUsageIn) |
143 | 1.05k | { |
144 | 1.05k | } |
145 | | |
146 | | /************************************************************************/ |
147 | | /* ~GDALDatasetPool() */ |
148 | | /************************************************************************/ |
149 | | |
150 | | GDALDatasetPool::~GDALDatasetPool() |
151 | 1.04k | { |
152 | 1.04k | bInDestruction = true; |
153 | 1.04k | GDALProxyPoolCacheEntry *cur = firstEntry; |
154 | 1.04k | GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread(); |
155 | 7.76k | while (cur) |
156 | 6.72k | { |
157 | 6.72k | GDALProxyPoolCacheEntry *next = cur->next; |
158 | 6.72k | CPLFree(cur->pszFileNameAndOpenOptions); |
159 | 6.72k | CPLFree(cur->pszOwner); |
160 | 6.72k | CPLAssert(cur->refCount == 0); |
161 | 6.72k | if (cur->poDS) |
162 | 0 | { |
163 | 0 | GDALSetResponsiblePIDForCurrentThread(cur->responsiblePID); |
164 | 0 | GDALClose(cur->poDS); |
165 | 0 | } |
166 | 6.72k | CPLFree(cur); |
167 | 6.72k | cur = next; |
168 | 6.72k | } |
169 | 1.04k | GDALSetResponsiblePIDForCurrentThread(responsiblePID); |
170 | 1.04k | } |
171 | | |
172 | | #ifdef DEBUG_PROXY_POOL |
173 | | /************************************************************************/ |
174 | | /* ShowContent() */ |
175 | | /************************************************************************/ |
176 | | |
177 | | void GDALDatasetPool::ShowContent() |
178 | | { |
179 | | GDALProxyPoolCacheEntry *cur = firstEntry; |
180 | | int i = 0; |
181 | | while (cur) |
182 | | { |
183 | | printf("[%d] pszFileName=%s, owner=%s, refCount=%d, " /*ok*/ |
184 | | "responsiblePID=%d\n", |
185 | | i, |
186 | | cur->pszFileNameAndOpenOptions ? cur->pszFileNameAndOpenOptions |
187 | | : "(null)", |
188 | | cur->pszOwner ? cur->pszOwner : "(null)", cur->refCount, |
189 | | (int)cur->responsiblePID); |
190 | | i++; |
191 | | cur = cur->next; |
192 | | } |
193 | | } |
194 | | |
195 | | /************************************************************************/ |
196 | | /* CheckLinks() */ |
197 | | /************************************************************************/ |
198 | | |
199 | | void GDALDatasetPool::CheckLinks() |
200 | | { |
201 | | GDALProxyPoolCacheEntry *cur = firstEntry; |
202 | | int i = 0; |
203 | | while (cur) |
204 | | { |
205 | | CPLAssert(cur == firstEntry || cur->prev->next == cur); |
206 | | CPLAssert(cur == lastEntry || cur->next->prev == cur); |
207 | | ++i; |
208 | | CPLAssert(cur->next != nullptr || cur == lastEntry); |
209 | | cur = cur->next; |
210 | | } |
211 | | (void)i; |
212 | | CPLAssert(i == currentSize); |
213 | | } |
214 | | #endif |
215 | | |
216 | | /************************************************************************/ |
217 | | /* GetFilenameAndOpenOptions() */ |
218 | | /************************************************************************/ |
219 | | |
220 | | static std::string GetFilenameAndOpenOptions(const char *pszFileName, |
221 | | CSLConstList papszOpenOptions) |
222 | 175k | { |
223 | 175k | std::string osFilenameAndOO(pszFileName); |
224 | 176k | for (int i = 0; papszOpenOptions && papszOpenOptions[i]; ++i) |
225 | 622 | { |
226 | 622 | osFilenameAndOO += "||"; |
227 | 622 | osFilenameAndOO += papszOpenOptions[i]; |
228 | 622 | } |
229 | 175k | return osFilenameAndOO; |
230 | 175k | } |
231 | | |
232 | | /************************************************************************/ |
233 | | /* _RefDataset() */ |
234 | | /************************************************************************/ |
235 | | |
236 | | GDALProxyPoolCacheEntry * |
237 | | GDALDatasetPool::_RefDataset(const char *pszFileName, GDALAccess eAccess, |
238 | | CSLConstList papszOpenOptions, int bShared, |
239 | | bool bForceOpen, const char *pszOwner) |
240 | 153k | { |
241 | 153k | CPLMutex **pMutex = GDALGetphDLMutex(); |
242 | 153k | CPLMutexHolderD(pMutex); |
243 | | |
244 | 153k | if (bInDestruction) |
245 | 0 | return nullptr; |
246 | | |
247 | 153k | const GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread(); |
248 | | |
249 | 153k | const auto EvictEntryWithZeroRefCount = |
250 | 153k | [this, responsiblePID](bool evictEntryWithOpenedDataset) |
251 | 153k | { |
252 | 8.84k | GDALProxyPoolCacheEntry *cur = firstEntry; |
253 | 8.84k | GDALProxyPoolCacheEntry *candidate = nullptr; |
254 | 893k | while (cur) |
255 | 884k | { |
256 | 884k | GDALProxyPoolCacheEntry *next = cur->next; |
257 | | |
258 | 884k | if (cur->refCount == 0 && |
259 | 884k | (!evictEntryWithOpenedDataset || cur->nRAMUsage > 0)) |
260 | 884k | { |
261 | 884k | candidate = cur; |
262 | 884k | } |
263 | | |
264 | 884k | cur = next; |
265 | 884k | } |
266 | 8.84k | if (candidate == nullptr) |
267 | 0 | return false; |
268 | | |
269 | 8.84k | nRAMUsage -= candidate->nRAMUsage; |
270 | 8.84k | candidate->nRAMUsage = 0; |
271 | | |
272 | 8.84k | CPLFree(candidate->pszFileNameAndOpenOptions); |
273 | 8.84k | candidate->pszFileNameAndOpenOptions = nullptr; |
274 | | |
275 | 8.84k | if (candidate->poDS) |
276 | 0 | { |
277 | | /* Close by pretending we are the thread that GDALOpen'ed this */ |
278 | | /* dataset */ |
279 | 0 | GDALSetResponsiblePIDForCurrentThread(candidate->responsiblePID); |
280 | |
|
281 | 0 | refCountOfDisabledRefCount++; |
282 | 0 | GDALClose(candidate->poDS); |
283 | 0 | refCountOfDisabledRefCount--; |
284 | |
|
285 | 0 | candidate->poDS = nullptr; |
286 | 0 | GDALSetResponsiblePIDForCurrentThread(responsiblePID); |
287 | 0 | } |
288 | 8.84k | CPLFree(candidate->pszOwner); |
289 | 8.84k | candidate->pszOwner = nullptr; |
290 | | |
291 | 8.84k | if (!evictEntryWithOpenedDataset && candidate != firstEntry) |
292 | 8.84k | { |
293 | | /* Recycle this entry for the to-be-opened dataset and */ |
294 | | /* moves it to the top of the list */ |
295 | 8.84k | if (candidate->prev) |
296 | 8.84k | candidate->prev->next = candidate->next; |
297 | | |
298 | 8.84k | if (candidate->next) |
299 | 0 | candidate->next->prev = candidate->prev; |
300 | 8.84k | else |
301 | 8.84k | { |
302 | 8.84k | CPLAssert(candidate == lastEntry); |
303 | 8.84k | lastEntry->prev->next = nullptr; |
304 | 8.84k | lastEntry = lastEntry->prev; |
305 | 8.84k | } |
306 | 8.84k | candidate->prev = nullptr; |
307 | 8.84k | candidate->next = firstEntry; |
308 | 8.84k | firstEntry->prev = candidate; |
309 | 8.84k | firstEntry = candidate; |
310 | | |
311 | | #ifdef DEBUG_PROXY_POOL |
312 | | CheckLinks(); |
313 | | #endif |
314 | 8.84k | } |
315 | | |
316 | 8.84k | return true; |
317 | 8.84k | }; |
318 | | |
319 | 153k | GDALProxyPoolCacheEntry *cur = firstEntry; |
320 | | |
321 | 153k | const std::string osFilenameAndOO = |
322 | 153k | GetFilenameAndOpenOptions(pszFileName, papszOpenOptions); |
323 | | |
324 | 1.41M | while (cur) |
325 | 1.39M | { |
326 | 1.39M | GDALProxyPoolCacheEntry *next = cur->next; |
327 | | |
328 | 1.39M | if (cur->refCount >= 0 && cur->pszFileNameAndOpenOptions && |
329 | 1.39M | osFilenameAndOO == cur->pszFileNameAndOpenOptions && |
330 | 1.39M | ((bShared && cur->responsiblePID == responsiblePID && |
331 | 138k | ((cur->pszOwner == nullptr && pszOwner == nullptr) || |
332 | 138k | (cur->pszOwner != nullptr && pszOwner != nullptr && |
333 | 138k | strcmp(cur->pszOwner, pszOwner) == 0))) || |
334 | 138k | (!bShared && cur->refCount == 0))) |
335 | 138k | { |
336 | 138k | if (cur != firstEntry) |
337 | 14.6k | { |
338 | | /* Move to begin */ |
339 | 14.6k | if (cur->next) |
340 | 8.85k | cur->next->prev = cur->prev; |
341 | 5.83k | else |
342 | 5.83k | lastEntry = cur->prev; |
343 | 14.6k | cur->prev->next = cur->next; |
344 | 14.6k | cur->prev = nullptr; |
345 | 14.6k | firstEntry->prev = cur; |
346 | 14.6k | cur->next = firstEntry; |
347 | 14.6k | firstEntry = cur; |
348 | | |
349 | | #ifdef DEBUG_PROXY_POOL |
350 | | CheckLinks(); |
351 | | #endif |
352 | 14.6k | } |
353 | | |
354 | 138k | cur->refCount++; |
355 | 138k | return cur; |
356 | 138k | } |
357 | | |
358 | 1.25M | cur = next; |
359 | 1.25M | } |
360 | | |
361 | 15.7k | if (!bForceOpen) |
362 | 8 | return nullptr; |
363 | | |
364 | 15.7k | if (currentSize == maxSize) |
365 | 8.84k | { |
366 | 8.84k | if (!EvictEntryWithZeroRefCount(false)) |
367 | 0 | { |
368 | 0 | CPLError( |
369 | 0 | CE_Failure, CPLE_AppDefined, |
370 | 0 | "Too many threads are running for the current value of the " |
371 | 0 | "dataset pool size (%d).\n" |
372 | 0 | "or too many proxy datasets are opened in a cascaded way.\n" |
373 | 0 | "Try increasing GDAL_MAX_DATASET_POOL_SIZE.", |
374 | 0 | maxSize); |
375 | 0 | return nullptr; |
376 | 0 | } |
377 | | |
378 | 8.84k | CPLAssert(firstEntry); |
379 | 8.84k | cur = firstEntry; |
380 | 8.84k | } |
381 | 6.92k | else |
382 | 6.92k | { |
383 | | /* Prepend */ |
384 | 6.92k | cur = static_cast<GDALProxyPoolCacheEntry *>( |
385 | 6.92k | CPLCalloc(1, sizeof(GDALProxyPoolCacheEntry))); |
386 | 6.92k | if (lastEntry == nullptr) |
387 | 1.05k | lastEntry = cur; |
388 | 6.92k | cur->prev = nullptr; |
389 | 6.92k | cur->next = firstEntry; |
390 | 6.92k | if (firstEntry) |
391 | 5.86k | firstEntry->prev = cur; |
392 | 6.92k | firstEntry = cur; |
393 | 6.92k | currentSize++; |
394 | | #ifdef DEBUG_PROXY_POOL |
395 | | CheckLinks(); |
396 | | #endif |
397 | 6.92k | } |
398 | | |
399 | 15.7k | cur->pszFileNameAndOpenOptions = CPLStrdup(osFilenameAndOO.c_str()); |
400 | 15.7k | cur->pszOwner = (pszOwner) ? CPLStrdup(pszOwner) : nullptr; |
401 | 15.7k | cur->responsiblePID = responsiblePID; |
402 | 15.7k | cur->refCount = -1; // to mark loading of dataset in progress |
403 | 15.7k | cur->nRAMUsage = 0; |
404 | | |
405 | 15.7k | refCountOfDisabledRefCount++; |
406 | 15.7k | const int nFlag = |
407 | 15.7k | ((eAccess == GA_Update) ? GDAL_OF_UPDATE : GDAL_OF_READONLY) | |
408 | 15.7k | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR; |
409 | 15.7k | CPLConfigOptionSetter oSetter("CPL_ALLOW_VSISTDIN", "NO", true); |
410 | | |
411 | | // Release mutex while opening dataset to avoid lock contention. |
412 | 15.7k | CPLReleaseMutex(*pMutex); |
413 | 15.7k | auto poDS = GDALDataset::Open(pszFileName, nFlag, nullptr, papszOpenOptions, |
414 | 15.7k | nullptr); |
415 | 15.7k | CPLAcquireMutex(*pMutex, 1000.0); |
416 | | |
417 | 15.7k | cur->poDS = poDS; |
418 | 15.7k | cur->refCount = 1; |
419 | | |
420 | 15.7k | refCountOfDisabledRefCount--; |
421 | | |
422 | 15.7k | if (cur->poDS) |
423 | 13.0k | { |
424 | 13.0k | cur->nRAMUsage = |
425 | 13.0k | std::max<GIntBig>(0, cur->poDS->GetEstimatedRAMUsage()); |
426 | 13.0k | nRAMUsage += cur->nRAMUsage; |
427 | 13.0k | } |
428 | | |
429 | 15.7k | if (nMaxRAMUsage > 0 && cur->nRAMUsage > 0) |
430 | 0 | { |
431 | 0 | while (nRAMUsage > nMaxRAMUsage && nRAMUsage != cur->nRAMUsage && |
432 | 0 | EvictEntryWithZeroRefCount(true)) |
433 | 0 | { |
434 | | // ok |
435 | 0 | } |
436 | 0 | } |
437 | | |
438 | 15.7k | return cur; |
439 | 15.7k | } |
440 | | |
441 | | /************************************************************************/ |
442 | | /* _CloseDatasetIfZeroRefCount() */ |
443 | | /************************************************************************/ |
444 | | |
445 | | void GDALDatasetPool::_CloseDatasetIfZeroRefCount(const char *pszFileName, |
446 | | CSLConstList papszOpenOptions, |
447 | | GDALAccess /* eAccess */, |
448 | | const char *pszOwner) |
449 | 22.0k | { |
450 | | // May fix https://github.com/OSGeo/gdal/issues/4318 |
451 | 22.0k | if (bInDestruction) |
452 | 0 | return; |
453 | | |
454 | 22.0k | GDALProxyPoolCacheEntry *cur = firstEntry; |
455 | 22.0k | GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread(); |
456 | | |
457 | 22.0k | const std::string osFilenameAndOO = |
458 | 22.0k | GetFilenameAndOpenOptions(pszFileName, papszOpenOptions); |
459 | | |
460 | 861k | while (cur) |
461 | 852k | { |
462 | 852k | GDALProxyPoolCacheEntry *next = cur->next; |
463 | | |
464 | 852k | if (cur->refCount == 0 && cur->pszFileNameAndOpenOptions && |
465 | 852k | osFilenameAndOO == cur->pszFileNameAndOpenOptions && |
466 | 852k | ((pszOwner == nullptr && cur->pszOwner == nullptr) || |
467 | 24.6k | (pszOwner != nullptr && cur->pszOwner != nullptr && |
468 | 24.6k | strcmp(cur->pszOwner, pszOwner) == 0)) && |
469 | 852k | cur->poDS != nullptr) |
470 | 13.0k | { |
471 | | /* Close by pretending we are the thread that GDALOpen'ed this */ |
472 | | /* dataset */ |
473 | 13.0k | GDALSetResponsiblePIDForCurrentThread(cur->responsiblePID); |
474 | | |
475 | 13.0k | GDALDataset *poDS = cur->poDS; |
476 | | |
477 | 13.0k | nRAMUsage -= cur->nRAMUsage; |
478 | 13.0k | cur->nRAMUsage = 0; |
479 | | |
480 | 13.0k | cur->poDS = nullptr; |
481 | 13.0k | CPLFree(cur->pszFileNameAndOpenOptions); |
482 | 13.0k | cur->pszFileNameAndOpenOptions = nullptr; |
483 | 13.0k | CPLFree(cur->pszOwner); |
484 | 13.0k | cur->pszOwner = nullptr; |
485 | | |
486 | 13.0k | refCountOfDisabledRefCount++; |
487 | 13.0k | GDALClose(poDS); |
488 | 13.0k | refCountOfDisabledRefCount--; |
489 | | |
490 | 13.0k | GDALSetResponsiblePIDForCurrentThread(responsiblePID); |
491 | 13.0k | break; |
492 | 13.0k | } |
493 | | |
494 | 839k | cur = next; |
495 | 839k | } |
496 | 22.0k | } |
497 | | |
498 | | /************************************************************************/ |
499 | | /* GDALGetMaxDatasetPoolSize() */ |
500 | | /************************************************************************/ |
501 | | |
502 | | /** Return the maximum number of datasets simultaneously opened in the |
503 | | * dataset pool. |
504 | | */ |
505 | | int GDALGetMaxDatasetPoolSize() |
506 | 1.05k | { |
507 | 1.05k | int nSize = atoi(CPLGetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", "100")); |
508 | 1.05k | if (nSize < 2) |
509 | 0 | nSize = 2; |
510 | 1.05k | else if (nSize > 1000) |
511 | 0 | nSize = 1000; |
512 | 1.05k | return nSize; |
513 | 1.05k | } |
514 | | |
515 | | /************************************************************************/ |
516 | | /* Ref() */ |
517 | | /************************************************************************/ |
518 | | |
519 | | void GDALDatasetPool::Ref() |
520 | 22.0k | { |
521 | 22.0k | CPLMutexHolderD(GDALGetphDLMutex()); |
522 | 22.0k | if (singleton == nullptr) |
523 | 1.05k | { |
524 | | |
525 | | // Try to not consume more than 25% of the usable RAM |
526 | 1.05k | GIntBig l_nMaxRAMUsage = |
527 | 1.05k | (CPLGetUsablePhysicalRAM() - GDALGetCacheMax64()) / 4; |
528 | 1.05k | const char *pszMaxRAMUsage = |
529 | 1.05k | CPLGetConfigOption("GDAL_MAX_DATASET_POOL_RAM_USAGE", nullptr); |
530 | 1.05k | if (pszMaxRAMUsage) |
531 | 0 | { |
532 | 0 | l_nMaxRAMUsage = std::strtoll(pszMaxRAMUsage, nullptr, 10); |
533 | 0 | if (strstr(pszMaxRAMUsage, "MB")) |
534 | 0 | l_nMaxRAMUsage *= 1024 * 1024; |
535 | 0 | else if (strstr(pszMaxRAMUsage, "GB")) |
536 | 0 | l_nMaxRAMUsage *= 1024 * 1024 * 1024; |
537 | 0 | } |
538 | | |
539 | 1.05k | singleton = |
540 | 1.05k | new GDALDatasetPool(GDALGetMaxDatasetPoolSize(), l_nMaxRAMUsage); |
541 | 1.05k | } |
542 | 22.0k | if (refCountOfDisabledRefCount == 0) |
543 | 22.0k | singleton->refCount++; |
544 | 22.0k | } |
545 | | |
546 | | /* keep that in sync with gdaldrivermanager.cpp */ |
547 | | void GDALDatasetPool::PreventDestroy() |
548 | 0 | { |
549 | 0 | CPLMutexHolderD(GDALGetphDLMutex()); |
550 | 0 | if (!singleton) |
551 | 0 | return; |
552 | 0 | refCountOfDisabledRefCount++; |
553 | 0 | } |
554 | | |
555 | | /* keep that in sync with gdaldrivermanager.cpp */ |
556 | | extern void GDALDatasetPoolPreventDestroy(); |
557 | | |
558 | | void GDALDatasetPoolPreventDestroy() |
559 | 0 | { |
560 | 0 | GDALDatasetPool::PreventDestroy(); |
561 | 0 | } |
562 | | |
563 | | /************************************************************************/ |
564 | | /* Unref() */ |
565 | | /************************************************************************/ |
566 | | |
567 | | void GDALDatasetPool::Unref() |
568 | 22.0k | { |
569 | 22.0k | CPLMutexHolderD(GDALGetphDLMutex()); |
570 | 22.0k | if (!singleton) |
571 | 0 | { |
572 | 0 | CPLAssert(false); |
573 | 0 | return; |
574 | 0 | } |
575 | 22.0k | if (refCountOfDisabledRefCount == 0) |
576 | 21.9k | { |
577 | 21.9k | singleton->refCount--; |
578 | 21.9k | if (singleton->refCount == 0) |
579 | 1.04k | { |
580 | 1.04k | delete singleton; |
581 | 1.04k | singleton = nullptr; |
582 | 1.04k | } |
583 | 21.9k | } |
584 | 22.0k | } |
585 | | |
586 | | /* keep that in sync with gdaldrivermanager.cpp */ |
587 | | void GDALDatasetPool::ForceDestroy() |
588 | 0 | { |
589 | 0 | CPLMutexHolderD(GDALGetphDLMutex()); |
590 | 0 | if (!singleton) |
591 | 0 | return; |
592 | 0 | refCountOfDisabledRefCount--; |
593 | 0 | CPLAssert(refCountOfDisabledRefCount == 0); |
594 | 0 | singleton->refCount = 0; |
595 | 0 | delete singleton; |
596 | 0 | singleton = nullptr; |
597 | 0 | } |
598 | | |
599 | | /* keep that in sync with gdaldrivermanager.cpp */ |
600 | | extern void GDALDatasetPoolForceDestroy(); |
601 | | |
602 | | void GDALDatasetPoolForceDestroy() |
603 | 0 | { |
604 | 0 | GDALDatasetPool::ForceDestroy(); |
605 | 0 | } |
606 | | |
607 | | /************************************************************************/ |
608 | | /* RefDataset() */ |
609 | | /************************************************************************/ |
610 | | |
611 | | GDALProxyPoolCacheEntry * |
612 | | GDALDatasetPool::RefDataset(const char *pszFileName, GDALAccess eAccess, |
613 | | char **papszOpenOptions, int bShared, |
614 | | bool bForceOpen, const char *pszOwner) |
615 | 153k | { |
616 | 153k | return singleton->_RefDataset(pszFileName, eAccess, papszOpenOptions, |
617 | 153k | bShared, bForceOpen, pszOwner); |
618 | 153k | } |
619 | | |
620 | | /************************************************************************/ |
621 | | /* UnrefDataset() */ |
622 | | /************************************************************************/ |
623 | | |
624 | | void GDALDatasetPool::UnrefDataset(GDALProxyPoolCacheEntry *cacheEntry) |
625 | 153k | { |
626 | 153k | CPLMutexHolderD(GDALGetphDLMutex()); |
627 | 153k | cacheEntry->refCount--; |
628 | 153k | } |
629 | | |
630 | | /************************************************************************/ |
631 | | /* CloseDatasetIfZeroRefCount() */ |
632 | | /************************************************************************/ |
633 | | |
634 | | void GDALDatasetPool::CloseDatasetIfZeroRefCount(const char *pszFileName, |
635 | | CSLConstList papszOpenOptions, |
636 | | GDALAccess eAccess, |
637 | | const char *pszOwner) |
638 | 22.0k | { |
639 | 22.0k | CPLMutexHolderD(GDALGetphDLMutex()); |
640 | 22.0k | singleton->_CloseDatasetIfZeroRefCount(pszFileName, papszOpenOptions, |
641 | 22.0k | eAccess, pszOwner); |
642 | 22.0k | } |
643 | | |
644 | | struct GetMetadataElt |
645 | | { |
646 | | char *pszDomain; |
647 | | char **papszMetadata; |
648 | | }; |
649 | | |
650 | | static unsigned long hash_func_get_metadata(const void *_elt) |
651 | 0 | { |
652 | 0 | const GetMetadataElt *elt = static_cast<const GetMetadataElt *>(_elt); |
653 | 0 | return CPLHashSetHashStr(elt->pszDomain); |
654 | 0 | } |
655 | | |
656 | | static int equal_func_get_metadata(const void *_elt1, const void *_elt2) |
657 | 0 | { |
658 | 0 | const GetMetadataElt *elt1 = static_cast<const GetMetadataElt *>(_elt1); |
659 | 0 | const GetMetadataElt *elt2 = static_cast<const GetMetadataElt *>(_elt2); |
660 | 0 | return CPLHashSetEqualStr(elt1->pszDomain, elt2->pszDomain); |
661 | 0 | } |
662 | | |
663 | | static void free_func_get_metadata(void *_elt) |
664 | 0 | { |
665 | 0 | GetMetadataElt *elt = static_cast<GetMetadataElt *>(_elt); |
666 | 0 | CPLFree(elt->pszDomain); |
667 | 0 | CSLDestroy(elt->papszMetadata); |
668 | 0 | CPLFree(elt); |
669 | 0 | } |
670 | | |
671 | | struct GetMetadataItemElt |
672 | | { |
673 | | char *pszName; |
674 | | char *pszDomain; |
675 | | char *pszMetadataItem; |
676 | | }; |
677 | | |
678 | | static unsigned long hash_func_get_metadata_item(const void *_elt) |
679 | 206 | { |
680 | 206 | const GetMetadataItemElt *elt = |
681 | 206 | static_cast<const GetMetadataItemElt *>(_elt); |
682 | 206 | return CPLHashSetHashStr(elt->pszName) ^ CPLHashSetHashStr(elt->pszDomain); |
683 | 206 | } |
684 | | |
685 | | static int equal_func_get_metadata_item(const void *_elt1, const void *_elt2) |
686 | 48 | { |
687 | 48 | const GetMetadataItemElt *elt1 = |
688 | 48 | static_cast<const GetMetadataItemElt *>(_elt1); |
689 | 48 | const GetMetadataItemElt *elt2 = |
690 | 48 | static_cast<const GetMetadataItemElt *>(_elt2); |
691 | 48 | return CPLHashSetEqualStr(elt1->pszName, elt2->pszName) && |
692 | 48 | CPLHashSetEqualStr(elt1->pszDomain, elt2->pszDomain); |
693 | 48 | } |
694 | | |
695 | | static void free_func_get_metadata_item(void *_elt) |
696 | 127 | { |
697 | 127 | GetMetadataItemElt *elt = static_cast<GetMetadataItemElt *>(_elt); |
698 | 127 | CPLFree(elt->pszName); |
699 | 127 | CPLFree(elt->pszDomain); |
700 | 127 | CPLFree(elt->pszMetadataItem); |
701 | 127 | CPLFree(elt); |
702 | 127 | } |
703 | | |
704 | | /* ******************************************************************** */ |
705 | | /* GDALProxyPoolDataset */ |
706 | | /* ******************************************************************** */ |
707 | | |
708 | | /* Note : the bShared parameter must be used with caution. You can */ |
709 | | /* set it to TRUE for being used as a VRT source : in that case, */ |
710 | | /* VRTSimpleSource will take care of destroying it when there are no */ |
711 | | /* reference to it (in VRTSimpleSource::~VRTSimpleSource()) */ |
712 | | /* However this will not be registered as a genuine shared dataset, like it */ |
713 | | /* would have been with MarkAsShared(). But MarkAsShared() is not usable for */ |
714 | | /* GDALProxyPoolDataset objects, as they share the same description as their */ |
715 | | /* underlying dataset. So *NEVER* call MarkAsShared() on a GDALProxyPoolDataset |
716 | | */ |
717 | | /* object */ |
718 | | |
719 | | /* pszOwner is only honoured in the bShared case, and restrict the scope */ |
720 | | /* of the sharing. Only calls to _RefDataset() with the same value of */ |
721 | | /* pszOwner can effectively use the same dataset. The use case is */ |
722 | | /* to avoid 2 VRTs (potentially the same one) opened by a single thread, |
723 | | * pointing to */ |
724 | | /* the same source datasets. In that case, they would use the same dataset */ |
725 | | /* So even if the VRT handles themselves are used from different threads, since |
726 | | */ |
727 | | /* the underlying sources are shared, that might cause crashes (#6939). */ |
728 | | /* But we want to allow a same VRT referencing the same source dataset,*/ |
729 | | /* for example if it has multiple bands. So in practice the value of pszOwner */ |
730 | | /* is the serialized value (%p formatting) of the VRT dataset handle. */ |
731 | | |
732 | | GDALProxyPoolDataset::GDALProxyPoolDataset( |
733 | | const char *pszSourceDatasetDescription, int nRasterXSizeIn, |
734 | | int nRasterYSizeIn, GDALAccess eAccessIn, int bSharedIn, |
735 | | const char *pszProjectionRefIn, double *padfGeoTransform, |
736 | | const char *pszOwner) |
737 | 0 | : responsiblePID(GDALGetResponsiblePIDForCurrentThread()), |
738 | 0 | pszProjectionRef(pszProjectionRefIn ? CPLStrdup(pszProjectionRefIn) |
739 | 0 | : nullptr) |
740 | 0 | { |
741 | 0 | GDALDatasetPool::Ref(); |
742 | |
|
743 | 0 | SetDescription(pszSourceDatasetDescription); |
744 | |
|
745 | 0 | nRasterXSize = nRasterXSizeIn; |
746 | 0 | nRasterYSize = nRasterYSizeIn; |
747 | 0 | eAccess = eAccessIn; |
748 | |
|
749 | 0 | bShared = CPL_TO_BOOL(bSharedIn); |
750 | 0 | m_pszOwner = pszOwner ? CPLStrdup(pszOwner) : nullptr; |
751 | |
|
752 | 0 | if (padfGeoTransform) |
753 | 0 | { |
754 | 0 | memcpy(adfGeoTransform, padfGeoTransform, 6 * sizeof(double)); |
755 | 0 | bHasSrcGeoTransform = true; |
756 | 0 | } |
757 | 0 | else |
758 | 0 | { |
759 | 0 | adfGeoTransform[0] = 0; |
760 | 0 | adfGeoTransform[1] = 1; |
761 | 0 | adfGeoTransform[2] = 0; |
762 | 0 | adfGeoTransform[3] = 0; |
763 | 0 | adfGeoTransform[4] = 0; |
764 | 0 | adfGeoTransform[5] = 1; |
765 | 0 | bHasSrcGeoTransform = false; |
766 | 0 | } |
767 | |
|
768 | 0 | if (pszProjectionRefIn) |
769 | 0 | { |
770 | 0 | m_poSRS = new OGRSpatialReference(); |
771 | 0 | m_poSRS->importFromWkt(pszProjectionRefIn); |
772 | 0 | m_bHasSrcSRS = true; |
773 | 0 | } |
774 | 0 | } |
775 | | |
776 | | /* Constructor where the parameters (raster size, etc.) are obtained |
777 | | * by opening the underlying dataset. |
778 | | */ |
779 | | GDALProxyPoolDataset::GDALProxyPoolDataset( |
780 | | const char *pszSourceDatasetDescription, GDALAccess eAccessIn, |
781 | | int bSharedIn, const char *pszOwner) |
782 | 22.0k | : responsiblePID(GDALGetResponsiblePIDForCurrentThread()) |
783 | 22.0k | { |
784 | 22.0k | GDALDatasetPool::Ref(); |
785 | | |
786 | 22.0k | SetDescription(pszSourceDatasetDescription); |
787 | | |
788 | 22.0k | eAccess = eAccessIn; |
789 | | |
790 | 22.0k | bShared = CPL_TO_BOOL(bSharedIn); |
791 | 22.0k | m_pszOwner = pszOwner ? CPLStrdup(pszOwner) : nullptr; |
792 | 22.0k | } |
793 | | |
794 | | /************************************************************************/ |
795 | | /* Create() */ |
796 | | /************************************************************************/ |
797 | | |
798 | | /* Instantiate a GDALProxyPoolDataset where the parameters (raster size, etc.) |
799 | | * are obtained by opening the underlying dataset. |
800 | | * Its bands are also instantiated. |
801 | | */ |
802 | | GDALProxyPoolDataset *GDALProxyPoolDataset::Create( |
803 | | const char *pszSourceDatasetDescription, CSLConstList papszOpenOptionsIn, |
804 | | GDALAccess eAccessIn, int bSharedIn, const char *pszOwner) |
805 | 22.0k | { |
806 | 22.0k | std::unique_ptr<GDALProxyPoolDataset> poSelf(new GDALProxyPoolDataset( |
807 | 22.0k | pszSourceDatasetDescription, eAccessIn, bSharedIn, pszOwner)); |
808 | 22.0k | poSelf->SetOpenOptions(papszOpenOptionsIn); |
809 | 22.0k | GDALDataset *poUnderlyingDS = poSelf->RefUnderlyingDataset(); |
810 | 22.0k | if (!poUnderlyingDS) |
811 | 8.87k | return nullptr; |
812 | 13.1k | poSelf->nRasterXSize = poUnderlyingDS->GetRasterXSize(); |
813 | 13.1k | poSelf->nRasterYSize = poUnderlyingDS->GetRasterYSize(); |
814 | 13.1k | if (poUnderlyingDS->GetGeoTransform(poSelf->adfGeoTransform) == CE_None) |
815 | 6.36k | poSelf->bHasSrcGeoTransform = true; |
816 | 13.1k | const auto poSRS = poUnderlyingDS->GetSpatialRef(); |
817 | 13.1k | if (poSRS) |
818 | 6.51k | { |
819 | 6.51k | poSelf->m_poSRS = poSRS->Clone(); |
820 | 6.51k | poSelf->m_bHasSrcSRS = true; |
821 | 6.51k | } |
822 | 59.0k | for (int i = 1; i <= poUnderlyingDS->GetRasterCount(); ++i) |
823 | 45.9k | { |
824 | 45.9k | auto poSrcBand = poUnderlyingDS->GetRasterBand(i); |
825 | 45.9k | if (!poSrcBand) |
826 | 0 | { |
827 | 0 | poSelf->UnrefUnderlyingDataset(poUnderlyingDS); |
828 | 0 | return nullptr; |
829 | 0 | } |
830 | 45.9k | int nSrcBlockXSize, nSrcBlockYSize; |
831 | 45.9k | poSrcBand->GetBlockSize(&nSrcBlockXSize, &nSrcBlockYSize); |
832 | 45.9k | poSelf->AddSrcBandDescription(poSrcBand->GetRasterDataType(), |
833 | 45.9k | nSrcBlockXSize, nSrcBlockYSize); |
834 | 45.9k | } |
835 | 13.1k | poSelf->UnrefUnderlyingDataset(poUnderlyingDS); |
836 | 13.1k | return poSelf.release(); |
837 | 13.1k | } |
838 | | |
839 | | /************************************************************************/ |
840 | | /* ~GDALProxyPoolDataset() */ |
841 | | /************************************************************************/ |
842 | | |
843 | | GDALProxyPoolDataset::~GDALProxyPoolDataset() |
844 | 22.0k | { |
845 | 22.0k | GDALDatasetPool::CloseDatasetIfZeroRefCount( |
846 | 22.0k | GetDescription(), papszOpenOptions, eAccess, m_pszOwner); |
847 | | |
848 | | /* See comment in constructor */ |
849 | | /* It is not really a genuine shared dataset, so we don't */ |
850 | | /* want ~GDALDataset() to try to release it from its */ |
851 | | /* shared dataset hashset. This will save a */ |
852 | | /* "Should not happen. Cannot find %s, this=%p in phSharedDatasetSet" debug |
853 | | * message */ |
854 | 22.0k | bShared = false; |
855 | | |
856 | 22.0k | CPLFree(pszProjectionRef); |
857 | 22.0k | CPLFree(pszGCPProjection); |
858 | 22.0k | if (nGCPCount) |
859 | 0 | { |
860 | 0 | GDALDeinitGCPs(nGCPCount, pasGCPList); |
861 | 0 | CPLFree(pasGCPList); |
862 | 0 | } |
863 | 22.0k | if (metadataSet) |
864 | 0 | CPLHashSetDestroy(metadataSet); |
865 | 22.0k | if (metadataItemSet) |
866 | 0 | CPLHashSetDestroy(metadataItemSet); |
867 | 22.0k | CPLFree(m_pszOwner); |
868 | 22.0k | if (m_poSRS) |
869 | 6.51k | m_poSRS->Release(); |
870 | 22.0k | if (m_poGCPSRS) |
871 | 0 | m_poGCPSRS->Release(); |
872 | | |
873 | 22.0k | GDALDatasetPool::Unref(); |
874 | 22.0k | } |
875 | | |
876 | | /************************************************************************/ |
877 | | /* SetOpenOptions() */ |
878 | | /************************************************************************/ |
879 | | |
880 | | void GDALProxyPoolDataset::SetOpenOptions(CSLConstList papszOpenOptionsIn) |
881 | 22.0k | { |
882 | 22.0k | CPLAssert(papszOpenOptions == nullptr); |
883 | 22.0k | papszOpenOptions = CSLDuplicate(papszOpenOptionsIn); |
884 | 22.0k | } |
885 | | |
886 | | /************************************************************************/ |
887 | | /* AddSrcBandDescription() */ |
888 | | /************************************************************************/ |
889 | | |
890 | | void GDALProxyPoolDataset::AddSrcBandDescription(GDALDataType eDataType, |
891 | | int nBlockXSize, |
892 | | int nBlockYSize) |
893 | 45.9k | { |
894 | 45.9k | SetBand(nBands + 1, new GDALProxyPoolRasterBand(this, nBands + 1, eDataType, |
895 | 45.9k | nBlockXSize, nBlockYSize)); |
896 | 45.9k | } |
897 | | |
898 | | /************************************************************************/ |
899 | | /* AddSrcBand() */ |
900 | | /************************************************************************/ |
901 | | |
902 | | void GDALProxyPoolDataset::AddSrcBand(int nBand, GDALDataType eDataType, |
903 | | int nBlockXSize, int nBlockYSize) |
904 | 0 | { |
905 | 0 | SetBand(nBand, new GDALProxyPoolRasterBand(this, nBand, eDataType, |
906 | 0 | nBlockXSize, nBlockYSize)); |
907 | 0 | } |
908 | | |
909 | | /************************************************************************/ |
910 | | /* RefUnderlyingDataset() */ |
911 | | /************************************************************************/ |
912 | | |
913 | | GDALDataset *GDALProxyPoolDataset::RefUnderlyingDataset() const |
914 | 22.0k | { |
915 | 22.0k | return RefUnderlyingDataset(true); |
916 | 22.0k | } |
917 | | |
918 | | GDALDataset *GDALProxyPoolDataset::RefUnderlyingDataset(bool bForceOpen) const |
919 | 153k | { |
920 | | /* We pretend that the current thread is responsiblePID, that is */ |
921 | | /* to say the thread that created that GDALProxyPoolDataset object. */ |
922 | | /* This is for the case when a GDALProxyPoolDataset is created by a */ |
923 | | /* thread and used by other threads. These other threads, when doing actual |
924 | | */ |
925 | | /* IO, will come there and potentially open the underlying dataset. */ |
926 | | /* By doing this, they can indirectly call GDALOpenShared() on .aux file */ |
927 | | /* for example. So this call to GDALOpenShared() must occur as if it */ |
928 | | /* was done by the creating thread, otherwise it will not be correctly |
929 | | * closed afterwards... */ |
930 | | /* To make a long story short : this is necessary when warping with |
931 | | * ChunkAndWarpMulti */ |
932 | | /* a VRT of GeoTIFFs that have associated .aux files */ |
933 | 153k | GIntBig curResponsiblePID = GDALGetResponsiblePIDForCurrentThread(); |
934 | 153k | GDALSetResponsiblePIDForCurrentThread(responsiblePID); |
935 | 153k | cacheEntry = |
936 | 153k | GDALDatasetPool::RefDataset(GetDescription(), eAccess, papszOpenOptions, |
937 | 153k | GetShared(), bForceOpen, m_pszOwner); |
938 | 153k | GDALSetResponsiblePIDForCurrentThread(curResponsiblePID); |
939 | 153k | if (cacheEntry != nullptr) |
940 | 153k | { |
941 | 153k | if (cacheEntry->poDS != nullptr) |
942 | 144k | return cacheEntry->poDS; |
943 | 8.87k | else |
944 | 8.87k | GDALDatasetPool::UnrefDataset(cacheEntry); |
945 | 153k | } |
946 | 8.88k | return nullptr; |
947 | 153k | } |
948 | | |
949 | | /************************************************************************/ |
950 | | /* UnrefUnderlyingDataset() */ |
951 | | /************************************************************************/ |
952 | | |
953 | | void GDALProxyPoolDataset::UnrefUnderlyingDataset( |
954 | | CPL_UNUSED GDALDataset *poUnderlyingDataset) const |
955 | 144k | { |
956 | 144k | if (cacheEntry != nullptr) |
957 | 144k | { |
958 | 144k | CPLAssert(cacheEntry->poDS == poUnderlyingDataset); |
959 | 144k | if (cacheEntry->poDS != nullptr) |
960 | 144k | GDALDatasetPool::UnrefDataset(cacheEntry); |
961 | 144k | } |
962 | 144k | } |
963 | | |
964 | | /************************************************************************/ |
965 | | /* FlushCache() */ |
966 | | /************************************************************************/ |
967 | | |
968 | | CPLErr GDALProxyPoolDataset::FlushCache(bool bAtClosing) |
969 | 0 | { |
970 | 0 | CPLErr eErr = CE_None; |
971 | 0 | GDALDataset *poUnderlyingDataset = RefUnderlyingDataset(false); |
972 | 0 | if (poUnderlyingDataset) |
973 | 0 | { |
974 | 0 | eErr = poUnderlyingDataset->FlushCache(bAtClosing); |
975 | 0 | UnrefUnderlyingDataset(poUnderlyingDataset); |
976 | 0 | } |
977 | 0 | return eErr; |
978 | 0 | } |
979 | | |
980 | | /************************************************************************/ |
981 | | /* SetSpatialRef() */ |
982 | | /************************************************************************/ |
983 | | |
984 | | CPLErr GDALProxyPoolDataset::SetSpatialRef(const OGRSpatialReference *poSRS) |
985 | 0 | { |
986 | 0 | m_bHasSrcSRS = false; |
987 | 0 | return GDALProxyDataset::SetSpatialRef(poSRS); |
988 | 0 | } |
989 | | |
990 | | /************************************************************************/ |
991 | | /* GetSpatialRef() */ |
992 | | /************************************************************************/ |
993 | | |
994 | | const OGRSpatialReference *GDALProxyPoolDataset::GetSpatialRef() const |
995 | 0 | { |
996 | 0 | if (m_bHasSrcSRS) |
997 | 0 | return m_poSRS; |
998 | 0 | else |
999 | 0 | { |
1000 | 0 | if (m_poSRS) |
1001 | 0 | m_poSRS->Release(); |
1002 | 0 | m_poSRS = nullptr; |
1003 | 0 | auto poSRS = GDALProxyDataset::GetSpatialRef(); |
1004 | 0 | if (poSRS) |
1005 | 0 | m_poSRS = poSRS->Clone(); |
1006 | 0 | return m_poSRS; |
1007 | 0 | } |
1008 | 0 | } |
1009 | | |
1010 | | /************************************************************************/ |
1011 | | /* SetGeoTransform() */ |
1012 | | /************************************************************************/ |
1013 | | |
1014 | | CPLErr GDALProxyPoolDataset::SetGeoTransform(double *padfGeoTransform) |
1015 | 0 | { |
1016 | 0 | bHasSrcGeoTransform = false; |
1017 | 0 | return GDALProxyDataset::SetGeoTransform(padfGeoTransform); |
1018 | 0 | } |
1019 | | |
1020 | | /************************************************************************/ |
1021 | | /* GetGeoTransform() */ |
1022 | | /************************************************************************/ |
1023 | | |
1024 | | CPLErr GDALProxyPoolDataset::GetGeoTransform(double *padfGeoTransform) |
1025 | 0 | { |
1026 | 0 | if (bHasSrcGeoTransform) |
1027 | 0 | { |
1028 | 0 | memcpy(padfGeoTransform, adfGeoTransform, 6 * sizeof(double)); |
1029 | 0 | return CE_None; |
1030 | 0 | } |
1031 | 0 | else |
1032 | 0 | { |
1033 | 0 | return GDALProxyDataset::GetGeoTransform(padfGeoTransform); |
1034 | 0 | } |
1035 | 0 | } |
1036 | | |
1037 | | /************************************************************************/ |
1038 | | /* GetMetadata() */ |
1039 | | /************************************************************************/ |
1040 | | |
1041 | | char **GDALProxyPoolDataset::GetMetadata(const char *pszDomain) |
1042 | 0 | { |
1043 | 0 | if (metadataSet == nullptr) |
1044 | 0 | metadataSet = |
1045 | 0 | CPLHashSetNew(hash_func_get_metadata, equal_func_get_metadata, |
1046 | 0 | free_func_get_metadata); |
1047 | |
|
1048 | 0 | GDALDataset *poUnderlyingDataset = RefUnderlyingDataset(); |
1049 | 0 | if (poUnderlyingDataset == nullptr) |
1050 | 0 | return nullptr; |
1051 | | |
1052 | 0 | char **papszUnderlyingMetadata = |
1053 | 0 | poUnderlyingDataset->GetMetadata(pszDomain); |
1054 | |
|
1055 | 0 | GetMetadataElt *pElt = |
1056 | 0 | static_cast<GetMetadataElt *>(CPLMalloc(sizeof(GetMetadataElt))); |
1057 | 0 | pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr; |
1058 | 0 | pElt->papszMetadata = CSLDuplicate(papszUnderlyingMetadata); |
1059 | 0 | CPLHashSetInsert(metadataSet, pElt); |
1060 | |
|
1061 | 0 | UnrefUnderlyingDataset(poUnderlyingDataset); |
1062 | |
|
1063 | 0 | return pElt->papszMetadata; |
1064 | 0 | } |
1065 | | |
1066 | | /************************************************************************/ |
1067 | | /* GetMetadataItem() */ |
1068 | | /************************************************************************/ |
1069 | | |
1070 | | const char *GDALProxyPoolDataset::GetMetadataItem(const char *pszName, |
1071 | | const char *pszDomain) |
1072 | 0 | { |
1073 | 0 | if (metadataItemSet == nullptr) |
1074 | 0 | metadataItemSet = CPLHashSetNew(hash_func_get_metadata_item, |
1075 | 0 | equal_func_get_metadata_item, |
1076 | 0 | free_func_get_metadata_item); |
1077 | |
|
1078 | 0 | GDALDataset *poUnderlyingDataset = RefUnderlyingDataset(); |
1079 | 0 | if (poUnderlyingDataset == nullptr) |
1080 | 0 | return nullptr; |
1081 | | |
1082 | 0 | const char *pszUnderlyingMetadataItem = |
1083 | 0 | poUnderlyingDataset->GetMetadataItem(pszName, pszDomain); |
1084 | |
|
1085 | 0 | GetMetadataItemElt *pElt = static_cast<GetMetadataItemElt *>( |
1086 | 0 | CPLMalloc(sizeof(GetMetadataItemElt))); |
1087 | 0 | pElt->pszName = (pszName) ? CPLStrdup(pszName) : nullptr; |
1088 | 0 | pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr; |
1089 | 0 | pElt->pszMetadataItem = (pszUnderlyingMetadataItem) |
1090 | 0 | ? CPLStrdup(pszUnderlyingMetadataItem) |
1091 | 0 | : nullptr; |
1092 | 0 | CPLHashSetInsert(metadataItemSet, pElt); |
1093 | |
|
1094 | 0 | UnrefUnderlyingDataset(poUnderlyingDataset); |
1095 | |
|
1096 | 0 | return pElt->pszMetadataItem; |
1097 | 0 | } |
1098 | | |
1099 | | /************************************************************************/ |
1100 | | /* GetInternalHandle() */ |
1101 | | /************************************************************************/ |
1102 | | |
1103 | | void *GDALProxyPoolDataset::GetInternalHandle(const char *pszRequest) |
1104 | 0 | { |
1105 | 0 | CPLError( |
1106 | 0 | CE_Warning, CPLE_AppDefined, |
1107 | 0 | "GetInternalHandle() cannot be safely called on a proxy pool dataset\n" |
1108 | 0 | "as the returned value may be invalidated at any time.\n"); |
1109 | 0 | return GDALProxyDataset::GetInternalHandle(pszRequest); |
1110 | 0 | } |
1111 | | |
1112 | | /************************************************************************/ |
1113 | | /* GetGCPSpatialRef() */ |
1114 | | /************************************************************************/ |
1115 | | |
1116 | | const OGRSpatialReference *GDALProxyPoolDataset::GetGCPSpatialRef() const |
1117 | 0 | { |
1118 | 0 | GDALDataset *poUnderlyingDataset = RefUnderlyingDataset(); |
1119 | 0 | if (poUnderlyingDataset == nullptr) |
1120 | 0 | return nullptr; |
1121 | | |
1122 | 0 | m_poGCPSRS->Release(); |
1123 | 0 | m_poGCPSRS = nullptr; |
1124 | |
|
1125 | 0 | const auto poUnderlyingGCPSRS = poUnderlyingDataset->GetGCPSpatialRef(); |
1126 | 0 | if (poUnderlyingGCPSRS) |
1127 | 0 | m_poGCPSRS = poUnderlyingGCPSRS->Clone(); |
1128 | |
|
1129 | 0 | UnrefUnderlyingDataset(poUnderlyingDataset); |
1130 | |
|
1131 | 0 | return m_poGCPSRS; |
1132 | 0 | } |
1133 | | |
1134 | | /************************************************************************/ |
1135 | | /* GetGCPs() */ |
1136 | | /************************************************************************/ |
1137 | | |
1138 | | const GDAL_GCP *GDALProxyPoolDataset::GetGCPs() |
1139 | 0 | { |
1140 | 0 | GDALDataset *poUnderlyingDataset = RefUnderlyingDataset(); |
1141 | 0 | if (poUnderlyingDataset == nullptr) |
1142 | 0 | return nullptr; |
1143 | | |
1144 | 0 | if (nGCPCount) |
1145 | 0 | { |
1146 | 0 | GDALDeinitGCPs(nGCPCount, pasGCPList); |
1147 | 0 | CPLFree(pasGCPList); |
1148 | 0 | pasGCPList = nullptr; |
1149 | 0 | } |
1150 | |
|
1151 | 0 | const GDAL_GCP *pasUnderlyingGCPList = poUnderlyingDataset->GetGCPs(); |
1152 | 0 | nGCPCount = poUnderlyingDataset->GetGCPCount(); |
1153 | 0 | if (nGCPCount) |
1154 | 0 | pasGCPList = GDALDuplicateGCPs(nGCPCount, pasUnderlyingGCPList); |
1155 | |
|
1156 | 0 | UnrefUnderlyingDataset(poUnderlyingDataset); |
1157 | |
|
1158 | 0 | return pasGCPList; |
1159 | 0 | } |
1160 | | |
1161 | | /************************************************************************/ |
1162 | | /* GDALProxyPoolDatasetCreate() */ |
1163 | | /************************************************************************/ |
1164 | | |
1165 | | GDALProxyPoolDatasetH GDALProxyPoolDatasetCreate( |
1166 | | const char *pszSourceDatasetDescription, int nRasterXSize, int nRasterYSize, |
1167 | | GDALAccess eAccess, int bShared, const char *pszProjectionRef, |
1168 | | double *padfGeoTransform) |
1169 | 0 | { |
1170 | 0 | return reinterpret_cast<GDALProxyPoolDatasetH>(new GDALProxyPoolDataset( |
1171 | 0 | pszSourceDatasetDescription, nRasterXSize, nRasterYSize, eAccess, |
1172 | 0 | bShared, pszProjectionRef, padfGeoTransform)); |
1173 | 0 | } |
1174 | | |
1175 | | /************************************************************************/ |
1176 | | /* GDALProxyPoolDatasetDelete() */ |
1177 | | /************************************************************************/ |
1178 | | |
1179 | | void GDALProxyPoolDatasetDelete(GDALProxyPoolDatasetH hProxyPoolDataset) |
1180 | 0 | { |
1181 | 0 | delete reinterpret_cast<GDALProxyPoolDataset *>(hProxyPoolDataset); |
1182 | 0 | } |
1183 | | |
1184 | | /************************************************************************/ |
1185 | | /* GDALProxyPoolDatasetAddSrcBandDescription() */ |
1186 | | /************************************************************************/ |
1187 | | |
1188 | | void GDALProxyPoolDatasetAddSrcBandDescription( |
1189 | | GDALProxyPoolDatasetH hProxyPoolDataset, GDALDataType eDataType, |
1190 | | int nBlockXSize, int nBlockYSize) |
1191 | 0 | { |
1192 | 0 | reinterpret_cast<GDALProxyPoolDataset *>(hProxyPoolDataset) |
1193 | 0 | ->AddSrcBandDescription(eDataType, nBlockXSize, nBlockYSize); |
1194 | 0 | } |
1195 | | |
1196 | | /* ******************************************************************** */ |
1197 | | /* GDALProxyPoolRasterBand() */ |
1198 | | /* ******************************************************************** */ |
1199 | | |
1200 | | GDALProxyPoolRasterBand::GDALProxyPoolRasterBand(GDALProxyPoolDataset *poDSIn, |
1201 | | int nBandIn, |
1202 | | GDALDataType eDataTypeIn, |
1203 | | int nBlockXSizeIn, |
1204 | | int nBlockYSizeIn) |
1205 | 45.9k | { |
1206 | 45.9k | poDS = poDSIn; |
1207 | 45.9k | nBand = nBandIn; |
1208 | 45.9k | eDataType = eDataTypeIn; |
1209 | 45.9k | nRasterXSize = poDSIn->GetRasterXSize(); |
1210 | 45.9k | nRasterYSize = poDSIn->GetRasterYSize(); |
1211 | 45.9k | nBlockXSize = nBlockXSizeIn; |
1212 | 45.9k | nBlockYSize = nBlockYSizeIn; |
1213 | 45.9k | } |
1214 | | |
1215 | | /* ******************************************************************** */ |
1216 | | /* GDALProxyPoolRasterBand() */ |
1217 | | /* ******************************************************************** */ |
1218 | | |
1219 | | GDALProxyPoolRasterBand::GDALProxyPoolRasterBand( |
1220 | | GDALProxyPoolDataset *poDSIn, GDALRasterBand *poUnderlyingRasterBand) |
1221 | 109k | { |
1222 | 109k | poDS = poDSIn; |
1223 | 109k | nBand = poUnderlyingRasterBand->GetBand(); |
1224 | 109k | eDataType = poUnderlyingRasterBand->GetRasterDataType(); |
1225 | 109k | nRasterXSize = poUnderlyingRasterBand->GetXSize(); |
1226 | 109k | nRasterYSize = poUnderlyingRasterBand->GetYSize(); |
1227 | 109k | poUnderlyingRasterBand->GetBlockSize(&nBlockXSize, &nBlockYSize); |
1228 | 109k | } |
1229 | | |
1230 | | /* ******************************************************************** */ |
1231 | | /* ~GDALProxyPoolRasterBand() */ |
1232 | | /* ******************************************************************** */ |
1233 | | GDALProxyPoolRasterBand::~GDALProxyPoolRasterBand() |
1234 | 155k | { |
1235 | 155k | if (metadataSet) |
1236 | 0 | CPLHashSetDestroy(metadataSet); |
1237 | 155k | if (metadataItemSet) |
1238 | 79 | CPLHashSetDestroy(metadataItemSet); |
1239 | 155k | CPLFree(pszUnitType); |
1240 | 155k | CSLDestroy(papszCategoryNames); |
1241 | 155k | if (poColorTable) |
1242 | 0 | delete poColorTable; |
1243 | | |
1244 | 264k | for (int i = 0; i < nSizeProxyOverviewRasterBand; i++) |
1245 | 109k | { |
1246 | 109k | if (papoProxyOverviewRasterBand[i]) |
1247 | 109k | delete papoProxyOverviewRasterBand[i]; |
1248 | 109k | } |
1249 | 155k | CPLFree(papoProxyOverviewRasterBand); |
1250 | 155k | if (poProxyMaskBand) |
1251 | 0 | delete poProxyMaskBand; |
1252 | 155k | } |
1253 | | |
1254 | | /************************************************************************/ |
1255 | | /* AddSrcMaskBandDescriptionFromUnderlying() */ |
1256 | | /************************************************************************/ |
1257 | | |
1258 | | void GDALProxyPoolRasterBand::AddSrcMaskBandDescriptionFromUnderlying() |
1259 | 0 | { |
1260 | 0 | if (poProxyMaskBand != nullptr) |
1261 | 0 | return; |
1262 | 0 | GDALRasterBand *poUnderlyingBand = RefUnderlyingRasterBand(); |
1263 | 0 | if (poUnderlyingBand == nullptr) |
1264 | 0 | return; |
1265 | 0 | auto poSrcMaskBand = poUnderlyingBand->GetMaskBand(); |
1266 | 0 | int nSrcBlockXSize, nSrcBlockYSize; |
1267 | 0 | poSrcMaskBand->GetBlockSize(&nSrcBlockXSize, &nSrcBlockYSize); |
1268 | 0 | poProxyMaskBand = new GDALProxyPoolMaskBand( |
1269 | 0 | cpl::down_cast<GDALProxyPoolDataset *>(poDS), this, |
1270 | 0 | poSrcMaskBand->GetRasterDataType(), nSrcBlockXSize, nSrcBlockYSize); |
1271 | 0 | UnrefUnderlyingRasterBand(poUnderlyingBand); |
1272 | 0 | } |
1273 | | |
1274 | | /************************************************************************/ |
1275 | | /* AddSrcMaskBandDescription() */ |
1276 | | /************************************************************************/ |
1277 | | |
1278 | | void GDALProxyPoolRasterBand::AddSrcMaskBandDescription( |
1279 | | GDALDataType eDataTypeIn, int nBlockXSizeIn, int nBlockYSizeIn) |
1280 | 0 | { |
1281 | 0 | CPLAssert(poProxyMaskBand == nullptr); |
1282 | 0 | poProxyMaskBand = new GDALProxyPoolMaskBand( |
1283 | 0 | cpl::down_cast<GDALProxyPoolDataset *>(poDS), this, eDataTypeIn, |
1284 | 0 | nBlockXSizeIn, nBlockYSizeIn); |
1285 | 0 | } |
1286 | | |
1287 | | /************************************************************************/ |
1288 | | /* RefUnderlyingRasterBand() */ |
1289 | | /************************************************************************/ |
1290 | | |
1291 | | GDALRasterBand * |
1292 | | GDALProxyPoolRasterBand::RefUnderlyingRasterBand(bool bForceOpen) const |
1293 | 131k | { |
1294 | 131k | GDALDataset *poUnderlyingDataset = |
1295 | 131k | (cpl::down_cast<GDALProxyPoolDataset *>(poDS)) |
1296 | 131k | ->RefUnderlyingDataset(bForceOpen); |
1297 | 131k | if (poUnderlyingDataset == nullptr) |
1298 | 8 | return nullptr; |
1299 | | |
1300 | 131k | GDALRasterBand *poBand = poUnderlyingDataset->GetRasterBand(nBand); |
1301 | 131k | if (poBand == nullptr) |
1302 | 0 | { |
1303 | 0 | (cpl::down_cast<GDALProxyPoolDataset *>(poDS)) |
1304 | 0 | ->UnrefUnderlyingDataset(poUnderlyingDataset); |
1305 | 0 | } |
1306 | 131k | else if (nBlockXSize <= 0 || nBlockYSize <= 0) |
1307 | 0 | { |
1308 | | // Here we try to load nBlockXSize&nBlockYSize from underlying band |
1309 | | // but we must guarantee that we will not access directly to |
1310 | | // nBlockXSize/nBlockYSize before RefUnderlyingRasterBand() is called |
1311 | 0 | int nSrcBlockXSize, nSrcBlockYSize; |
1312 | 0 | poBand->GetBlockSize(&nSrcBlockXSize, &nSrcBlockYSize); |
1313 | 0 | const_cast<GDALProxyPoolRasterBand *>(this)->nBlockXSize = |
1314 | 0 | nSrcBlockXSize; |
1315 | 0 | const_cast<GDALProxyPoolRasterBand *>(this)->nBlockYSize = |
1316 | 0 | nSrcBlockYSize; |
1317 | 0 | } |
1318 | | |
1319 | 131k | return poBand; |
1320 | 131k | } |
1321 | | |
1322 | | /************************************************************************/ |
1323 | | /* UnrefUnderlyingRasterBand() */ |
1324 | | /************************************************************************/ |
1325 | | |
1326 | | void GDALProxyPoolRasterBand::UnrefUnderlyingRasterBand( |
1327 | | GDALRasterBand *poUnderlyingRasterBand) const |
1328 | 131k | { |
1329 | 131k | if (poUnderlyingRasterBand) |
1330 | 131k | (cpl::down_cast<GDALProxyPoolDataset *>(poDS)) |
1331 | 131k | ->UnrefUnderlyingDataset(poUnderlyingRasterBand->GetDataset()); |
1332 | 131k | } |
1333 | | |
1334 | | /************************************************************************/ |
1335 | | /* FlushCache() */ |
1336 | | /************************************************************************/ |
1337 | | |
1338 | | CPLErr GDALProxyPoolRasterBand::FlushCache(bool bAtClosing) |
1339 | 9.64k | { |
1340 | 9.64k | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(false); |
1341 | 9.64k | if (poUnderlyingRasterBand) |
1342 | 9.64k | { |
1343 | 9.64k | CPLErr eErr = poUnderlyingRasterBand->FlushCache(bAtClosing); |
1344 | 9.64k | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1345 | 9.64k | return eErr; |
1346 | 9.64k | } |
1347 | 8 | return CE_None; |
1348 | 9.64k | } |
1349 | | |
1350 | | /************************************************************************/ |
1351 | | /* GetMetadata() */ |
1352 | | /************************************************************************/ |
1353 | | |
1354 | | char **GDALProxyPoolRasterBand::GetMetadata(const char *pszDomain) |
1355 | 0 | { |
1356 | 0 | if (metadataSet == nullptr) |
1357 | 0 | metadataSet = |
1358 | 0 | CPLHashSetNew(hash_func_get_metadata, equal_func_get_metadata, |
1359 | 0 | free_func_get_metadata); |
1360 | |
|
1361 | 0 | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(); |
1362 | 0 | if (poUnderlyingRasterBand == nullptr) |
1363 | 0 | return nullptr; |
1364 | | |
1365 | 0 | char **papszUnderlyingMetadata = |
1366 | 0 | poUnderlyingRasterBand->GetMetadata(pszDomain); |
1367 | |
|
1368 | 0 | GetMetadataElt *pElt = |
1369 | 0 | static_cast<GetMetadataElt *>(CPLMalloc(sizeof(GetMetadataElt))); |
1370 | 0 | pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr; |
1371 | 0 | pElt->papszMetadata = CSLDuplicate(papszUnderlyingMetadata); |
1372 | 0 | CPLHashSetInsert(metadataSet, pElt); |
1373 | |
|
1374 | 0 | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1375 | |
|
1376 | 0 | return pElt->papszMetadata; |
1377 | 0 | } |
1378 | | |
1379 | | /************************************************************************/ |
1380 | | /* GetMetadataItem() */ |
1381 | | /************************************************************************/ |
1382 | | |
1383 | | const char *GDALProxyPoolRasterBand::GetMetadataItem(const char *pszName, |
1384 | | const char *pszDomain) |
1385 | 127 | { |
1386 | 127 | if (metadataItemSet == nullptr) |
1387 | 79 | metadataItemSet = CPLHashSetNew(hash_func_get_metadata_item, |
1388 | 79 | equal_func_get_metadata_item, |
1389 | 79 | free_func_get_metadata_item); |
1390 | | |
1391 | 127 | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(); |
1392 | 127 | if (poUnderlyingRasterBand == nullptr) |
1393 | 0 | return nullptr; |
1394 | | |
1395 | 127 | const char *pszUnderlyingMetadataItem = |
1396 | 127 | poUnderlyingRasterBand->GetMetadataItem(pszName, pszDomain); |
1397 | | |
1398 | 127 | GetMetadataItemElt *pElt = static_cast<GetMetadataItemElt *>( |
1399 | 127 | CPLMalloc(sizeof(GetMetadataItemElt))); |
1400 | 127 | pElt->pszName = (pszName) ? CPLStrdup(pszName) : nullptr; |
1401 | 127 | pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr; |
1402 | 127 | pElt->pszMetadataItem = (pszUnderlyingMetadataItem) |
1403 | 127 | ? CPLStrdup(pszUnderlyingMetadataItem) |
1404 | 127 | : nullptr; |
1405 | 127 | CPLHashSetInsert(metadataItemSet, pElt); |
1406 | | |
1407 | 127 | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1408 | | |
1409 | 127 | return pElt->pszMetadataItem; |
1410 | 127 | } |
1411 | | |
1412 | | /* ******************************************************************** */ |
1413 | | /* GetCategoryNames() */ |
1414 | | /* ******************************************************************** */ |
1415 | | |
1416 | | char **GDALProxyPoolRasterBand::GetCategoryNames() |
1417 | 0 | { |
1418 | 0 | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(); |
1419 | 0 | if (poUnderlyingRasterBand == nullptr) |
1420 | 0 | return nullptr; |
1421 | | |
1422 | 0 | CSLDestroy(papszCategoryNames); |
1423 | 0 | papszCategoryNames = nullptr; |
1424 | |
|
1425 | 0 | char **papszUnderlyingCategoryNames = |
1426 | 0 | poUnderlyingRasterBand->GetCategoryNames(); |
1427 | 0 | if (papszUnderlyingCategoryNames) |
1428 | 0 | papszCategoryNames = CSLDuplicate(papszUnderlyingCategoryNames); |
1429 | |
|
1430 | 0 | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1431 | |
|
1432 | 0 | return papszCategoryNames; |
1433 | 0 | } |
1434 | | |
1435 | | /* ******************************************************************** */ |
1436 | | /* GetUnitType() */ |
1437 | | /* ******************************************************************** */ |
1438 | | |
1439 | | const char *GDALProxyPoolRasterBand::GetUnitType() |
1440 | 0 | { |
1441 | 0 | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(); |
1442 | 0 | if (poUnderlyingRasterBand == nullptr) |
1443 | 0 | return nullptr; |
1444 | | |
1445 | 0 | CPLFree(pszUnitType); |
1446 | 0 | pszUnitType = nullptr; |
1447 | |
|
1448 | 0 | const char *pszUnderlyingUnitType = poUnderlyingRasterBand->GetUnitType(); |
1449 | 0 | if (pszUnderlyingUnitType) |
1450 | 0 | pszUnitType = CPLStrdup(pszUnderlyingUnitType); |
1451 | |
|
1452 | 0 | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1453 | |
|
1454 | 0 | return pszUnitType; |
1455 | 0 | } |
1456 | | |
1457 | | /* ******************************************************************** */ |
1458 | | /* GetColorTable() */ |
1459 | | /* ******************************************************************** */ |
1460 | | |
1461 | | GDALColorTable *GDALProxyPoolRasterBand::GetColorTable() |
1462 | 0 | { |
1463 | 0 | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(); |
1464 | 0 | if (poUnderlyingRasterBand == nullptr) |
1465 | 0 | return nullptr; |
1466 | | |
1467 | 0 | if (poColorTable) |
1468 | 0 | delete poColorTable; |
1469 | 0 | poColorTable = nullptr; |
1470 | |
|
1471 | 0 | GDALColorTable *poUnderlyingColorTable = |
1472 | 0 | poUnderlyingRasterBand->GetColorTable(); |
1473 | 0 | if (poUnderlyingColorTable) |
1474 | 0 | poColorTable = poUnderlyingColorTable->Clone(); |
1475 | |
|
1476 | 0 | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1477 | |
|
1478 | 0 | return poColorTable; |
1479 | 0 | } |
1480 | | |
1481 | | /* ******************************************************************** */ |
1482 | | /* GetOverview() */ |
1483 | | /* ******************************************************************** */ |
1484 | | |
1485 | | GDALRasterBand *GDALProxyPoolRasterBand::GetOverview(int nOverviewBand) |
1486 | 169k | { |
1487 | 169k | if (nOverviewBand >= 0 && nOverviewBand < nSizeProxyOverviewRasterBand) |
1488 | 59.7k | { |
1489 | 59.7k | if (papoProxyOverviewRasterBand[nOverviewBand]) |
1490 | 59.7k | return papoProxyOverviewRasterBand[nOverviewBand]; |
1491 | 59.7k | } |
1492 | | |
1493 | 109k | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(); |
1494 | 109k | if (poUnderlyingRasterBand == nullptr) |
1495 | 0 | return nullptr; |
1496 | | |
1497 | 109k | GDALRasterBand *poOverviewRasterBand = |
1498 | 109k | poUnderlyingRasterBand->GetOverview(nOverviewBand); |
1499 | 109k | if (poOverviewRasterBand == nullptr) |
1500 | 0 | { |
1501 | 0 | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1502 | 0 | return nullptr; |
1503 | 0 | } |
1504 | | |
1505 | 109k | if (nOverviewBand >= nSizeProxyOverviewRasterBand) |
1506 | 109k | { |
1507 | 109k | papoProxyOverviewRasterBand = |
1508 | 109k | static_cast<GDALProxyPoolOverviewRasterBand **>( |
1509 | 109k | CPLRealloc(papoProxyOverviewRasterBand, |
1510 | 109k | sizeof(GDALProxyPoolOverviewRasterBand *) * |
1511 | 109k | (nOverviewBand + 1))); |
1512 | 218k | for (int i = nSizeProxyOverviewRasterBand; i < nOverviewBand + 1; i++) |
1513 | 109k | papoProxyOverviewRasterBand[i] = nullptr; |
1514 | 109k | nSizeProxyOverviewRasterBand = nOverviewBand + 1; |
1515 | 109k | } |
1516 | | |
1517 | 109k | papoProxyOverviewRasterBand[nOverviewBand] = |
1518 | 109k | new GDALProxyPoolOverviewRasterBand( |
1519 | 109k | cpl::down_cast<GDALProxyPoolDataset *>(poDS), poOverviewRasterBand, |
1520 | 109k | this, nOverviewBand); |
1521 | | |
1522 | 109k | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1523 | | |
1524 | 109k | return papoProxyOverviewRasterBand[nOverviewBand]; |
1525 | 109k | } |
1526 | | |
1527 | | /* ******************************************************************** */ |
1528 | | /* GetRasterSampleOverview() */ |
1529 | | /* ******************************************************************** */ |
1530 | | |
1531 | | GDALRasterBand * |
1532 | | GDALProxyPoolRasterBand::GetRasterSampleOverview(GUIntBig /* nDesiredSamples */) |
1533 | 0 | { |
1534 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1535 | 0 | "GDALProxyPoolRasterBand::GetRasterSampleOverview : not " |
1536 | 0 | "implemented yet"); |
1537 | 0 | return nullptr; |
1538 | 0 | } |
1539 | | |
1540 | | /* ******************************************************************** */ |
1541 | | /* GetMaskBand() */ |
1542 | | /* ******************************************************************** */ |
1543 | | |
1544 | | GDALRasterBand *GDALProxyPoolRasterBand::GetMaskBand() |
1545 | 0 | { |
1546 | 0 | if (poProxyMaskBand) |
1547 | 0 | return poProxyMaskBand; |
1548 | | |
1549 | 0 | GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(); |
1550 | 0 | if (poUnderlyingRasterBand == nullptr) |
1551 | 0 | return nullptr; |
1552 | | |
1553 | 0 | GDALRasterBand *poMaskBand = poUnderlyingRasterBand->GetMaskBand(); |
1554 | |
|
1555 | 0 | poProxyMaskBand = new GDALProxyPoolMaskBand( |
1556 | 0 | cpl::down_cast<GDALProxyPoolDataset *>(poDS), poMaskBand, this); |
1557 | |
|
1558 | 0 | UnrefUnderlyingRasterBand(poUnderlyingRasterBand); |
1559 | |
|
1560 | 0 | return poProxyMaskBand; |
1561 | 0 | } |
1562 | | |
1563 | | /* ******************************************************************** */ |
1564 | | /* GDALProxyPoolOverviewRasterBand() */ |
1565 | | /* ******************************************************************** */ |
1566 | | |
1567 | | GDALProxyPoolOverviewRasterBand::GDALProxyPoolOverviewRasterBand( |
1568 | | GDALProxyPoolDataset *poDSIn, GDALRasterBand *poUnderlyingOverviewBand, |
1569 | | GDALProxyPoolRasterBand *poMainBandIn, int nOverviewBandIn) |
1570 | 109k | : GDALProxyPoolRasterBand(poDSIn, poUnderlyingOverviewBand), |
1571 | 109k | poMainBand(poMainBandIn), nOverviewBand(nOverviewBandIn) |
1572 | 109k | { |
1573 | 109k | } |
1574 | | |
1575 | | /* ******************************************************************** */ |
1576 | | /* ~GDALProxyPoolOverviewRasterBand() */ |
1577 | | /* ******************************************************************** */ |
1578 | | |
1579 | | GDALProxyPoolOverviewRasterBand::~GDALProxyPoolOverviewRasterBand() |
1580 | 109k | { |
1581 | 109k | CPLAssert(nRefCountUnderlyingMainRasterBand == 0); |
1582 | 109k | } |
1583 | | |
1584 | | /* ******************************************************************** */ |
1585 | | /* RefUnderlyingRasterBand() */ |
1586 | | /* ******************************************************************** */ |
1587 | | |
1588 | | GDALRasterBand * |
1589 | | GDALProxyPoolOverviewRasterBand::RefUnderlyingRasterBand(bool bForceOpen) const |
1590 | 0 | { |
1591 | 0 | poUnderlyingMainRasterBand = |
1592 | 0 | poMainBand->RefUnderlyingRasterBand(bForceOpen); |
1593 | 0 | if (poUnderlyingMainRasterBand == nullptr) |
1594 | 0 | return nullptr; |
1595 | | |
1596 | 0 | nRefCountUnderlyingMainRasterBand++; |
1597 | 0 | return poUnderlyingMainRasterBand->GetOverview(nOverviewBand); |
1598 | 0 | } |
1599 | | |
1600 | | /* ******************************************************************** */ |
1601 | | /* UnrefUnderlyingRasterBand() */ |
1602 | | /* ******************************************************************** */ |
1603 | | |
1604 | | void GDALProxyPoolOverviewRasterBand::UnrefUnderlyingRasterBand( |
1605 | | GDALRasterBand * /* poUnderlyingRasterBand */) const |
1606 | 0 | { |
1607 | 0 | poMainBand->UnrefUnderlyingRasterBand(poUnderlyingMainRasterBand); |
1608 | 0 | nRefCountUnderlyingMainRasterBand--; |
1609 | 0 | } |
1610 | | |
1611 | | /* ******************************************************************** */ |
1612 | | /* GDALProxyPoolMaskBand() */ |
1613 | | /* ******************************************************************** */ |
1614 | | |
1615 | | GDALProxyPoolMaskBand::GDALProxyPoolMaskBand( |
1616 | | GDALProxyPoolDataset *poDSIn, GDALRasterBand *poUnderlyingMaskBand, |
1617 | | GDALProxyPoolRasterBand *poMainBandIn) |
1618 | 0 | : GDALProxyPoolRasterBand(poDSIn, poUnderlyingMaskBand) |
1619 | 0 | { |
1620 | 0 | poMainBand = poMainBandIn; |
1621 | |
|
1622 | 0 | poUnderlyingMainRasterBand = nullptr; |
1623 | 0 | nRefCountUnderlyingMainRasterBand = 0; |
1624 | 0 | } |
1625 | | |
1626 | | /* ******************************************************************** */ |
1627 | | /* GDALProxyPoolMaskBand() */ |
1628 | | /* ******************************************************************** */ |
1629 | | |
1630 | | GDALProxyPoolMaskBand::GDALProxyPoolMaskBand( |
1631 | | GDALProxyPoolDataset *poDSIn, GDALProxyPoolRasterBand *poMainBandIn, |
1632 | | GDALDataType eDataTypeIn, int nBlockXSizeIn, int nBlockYSizeIn) |
1633 | 0 | : GDALProxyPoolRasterBand(poDSIn, 1, eDataTypeIn, nBlockXSizeIn, |
1634 | 0 | nBlockYSizeIn), |
1635 | 0 | poMainBand(poMainBandIn) |
1636 | 0 | { |
1637 | 0 | } |
1638 | | |
1639 | | /* ******************************************************************** */ |
1640 | | /* ~GDALProxyPoolMaskBand() */ |
1641 | | /* ******************************************************************** */ |
1642 | | |
1643 | | GDALProxyPoolMaskBand::~GDALProxyPoolMaskBand() |
1644 | 0 | { |
1645 | 0 | CPLAssert(nRefCountUnderlyingMainRasterBand == 0); |
1646 | 0 | } |
1647 | | |
1648 | | /* ******************************************************************** */ |
1649 | | /* RefUnderlyingRasterBand() */ |
1650 | | /* ******************************************************************** */ |
1651 | | |
1652 | | GDALRasterBand * |
1653 | | GDALProxyPoolMaskBand::RefUnderlyingRasterBand(bool bForceOpen) const |
1654 | 0 | { |
1655 | 0 | poUnderlyingMainRasterBand = |
1656 | 0 | poMainBand->RefUnderlyingRasterBand(bForceOpen); |
1657 | 0 | if (poUnderlyingMainRasterBand == nullptr) |
1658 | 0 | return nullptr; |
1659 | | |
1660 | 0 | nRefCountUnderlyingMainRasterBand++; |
1661 | 0 | return poUnderlyingMainRasterBand->GetMaskBand(); |
1662 | 0 | } |
1663 | | |
1664 | | /* ******************************************************************** */ |
1665 | | /* UnrefUnderlyingRasterBand() */ |
1666 | | /* ******************************************************************** */ |
1667 | | |
1668 | | void GDALProxyPoolMaskBand::UnrefUnderlyingRasterBand( |
1669 | | GDALRasterBand * /* poUnderlyingRasterBand */) const |
1670 | 0 | { |
1671 | 0 | poMainBand->UnrefUnderlyingRasterBand(poUnderlyingMainRasterBand); |
1672 | 0 | nRefCountUnderlyingMainRasterBand--; |
1673 | 0 | } |
1674 | | |
1675 | | //! @endcond |