forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdalvirtualmem.cpp
1547 lines (1412 loc) · 61.7 KB
/
gdalvirtualmem.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
*
* Name: gdalvirtualmem.cpp
* Project: GDAL
* Purpose: Dataset and rasterband exposed as a virtual memory mapping.
* Author: Even Rouault, <even dot rouault at spatialys.com>
*
**********************************************************************
* Copyright (c) 2014, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "gdal.h"
#include "gdal_priv.h"
#include <cstddef>
#include <cstring>
#include <algorithm>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_virtualmem.h"
// To be changed if we go to 64-bit RasterIO coordinates and spacing.
using coord_type = int;
using spacing_type = int;
/************************************************************************/
/* GDALVirtualMem */
/************************************************************************/
class GDALVirtualMem
{
GDALDatasetH hDS = nullptr;
GDALRasterBandH hBand = nullptr;
coord_type nXOff = 0;
coord_type nYOff = 0;
// int nXSize;
// int nYSize;
coord_type nBufXSize = 0;
coord_type nBufYSize = 0;
GDALDataType eBufType = GDT_Byte;
int nBandCount = 0;
int *panBandMap = nullptr;
int nPixelSpace = 0;
GIntBig nLineSpace = 0;
GIntBig nBandSpace = 0;
bool bIsCompact = false;
bool bIsBandSequential = false;
bool IsCompact() const
{
return bIsCompact;
}
bool IsBandSequential() const
{
return bIsBandSequential;
}
void GetXYBand(size_t nOffset, coord_type &x, coord_type &y,
int &band) const;
size_t GetOffset(const coord_type &x, const coord_type &y, int band) const;
bool GotoNextPixel(coord_type &x, coord_type &y, int &band) const;
void DoIOBandSequential(GDALRWFlag eRWFlag, size_t nOffset, void *pPage,
size_t nBytes) const;
void DoIOPixelInterleaved(GDALRWFlag eRWFlag, size_t nOffset, void *pPage,
size_t nBytes) const;
CPL_DISALLOW_COPY_ASSIGN(GDALVirtualMem)
public:
GDALVirtualMem(GDALDatasetH hDS, GDALRasterBandH hBand,
const coord_type &nXOff, const coord_type &nYOff,
const coord_type &nXSize, const coord_type &nYSize,
const coord_type &nBufXSize, const coord_type &nBufYSize,
GDALDataType eBufType, int nBandCount,
const int *panBandMapIn, int nPixelSpace, GIntBig nLineSpace,
GIntBig nBandSpace);
~GDALVirtualMem();
static void FillCacheBandSequential(CPLVirtualMem *ctxt, size_t nOffset,
void *pPageToFill, size_t nToFill,
void *pUserData);
static void SaveFromCacheBandSequential(CPLVirtualMem *ctxt, size_t nOffset,
const void *pPageToBeEvicted,
size_t nToEvicted, void *pUserData);
static void FillCachePixelInterleaved(CPLVirtualMem *ctxt, size_t nOffset,
void *pPageToFill, size_t nToFill,
void *pUserData);
static void SaveFromCachePixelInterleaved(CPLVirtualMem *ctxt,
size_t nOffset,
const void *pPageToBeEvicted,
size_t nToEvicted,
void *pUserData);
static void Destroy(void *pUserData);
};
/************************************************************************/
/* GDALVirtualMem() */
/************************************************************************/
GDALVirtualMem::GDALVirtualMem(
GDALDatasetH hDSIn, GDALRasterBandH hBandIn, const coord_type &nXOffIn,
const coord_type &nYOffIn, const coord_type & /* nXSize */,
const coord_type & /* nYSize */, const coord_type &nBufXSizeIn,
const coord_type &nBufYSizeIn, GDALDataType eBufTypeIn, int nBandCountIn,
const int *panBandMapIn, int nPixelSpaceIn, GIntBig nLineSpaceIn,
GIntBig nBandSpaceIn)
: hDS(hDSIn), hBand(hBandIn), nXOff(nXOffIn), nYOff(nYOffIn),
// TODO(schwehr): Why not used or removed?
// nXSize(nXSize),
// nYSize(nYSize),
nBufXSize(nBufXSizeIn), nBufYSize(nBufYSizeIn), eBufType(eBufTypeIn),
nBandCount(nBandCountIn), nPixelSpace(nPixelSpaceIn),
nLineSpace(nLineSpaceIn), nBandSpace(nBandSpaceIn)
{
if (hDS != nullptr)
{
panBandMap = static_cast<int *>(CPLMalloc(nBandCount * sizeof(int)));
if (panBandMapIn)
{
memcpy(panBandMap, panBandMapIn, nBandCount * sizeof(int));
}
else
{
for (int i = 0; i < nBandCount; i++)
panBandMap[i] = i + 1;
}
}
else
{
panBandMap = nullptr;
nBandCount = 1;
}
const int nDataTypeSize = GDALGetDataTypeSizeBytes(eBufType);
if (nPixelSpace == nDataTypeSize &&
nLineSpace == static_cast<GIntBig>(nBufXSize) * nPixelSpace &&
nBandSpace == nBufYSize * nLineSpace)
bIsCompact = true;
else if (nBandSpace == nDataTypeSize &&
nPixelSpace == nBandCount * nBandSpace &&
nLineSpace == static_cast<GIntBig>(nBufXSize) * nPixelSpace)
bIsCompact = true;
else
bIsCompact = false;
bIsBandSequential = nBandSpace >= nBufYSize * nLineSpace;
}
/************************************************************************/
/* ~GDALVirtualMem() */
/************************************************************************/
GDALVirtualMem::~GDALVirtualMem()
{
CPLFree(panBandMap);
}
/************************************************************************/
/* GetXYBand() */
/************************************************************************/
void GDALVirtualMem::GetXYBand(size_t nOffset, coord_type &x, coord_type &y,
int &band) const
{
if (IsBandSequential())
{
if (nBandCount == 1)
band = 0;
else
band = static_cast<int>(nOffset / nBandSpace);
y = static_cast<coord_type>((nOffset - band * nBandSpace) / nLineSpace);
x = static_cast<coord_type>(
(nOffset - band * nBandSpace - y * nLineSpace) / nPixelSpace);
}
else
{
y = static_cast<coord_type>(nOffset / nLineSpace);
x = static_cast<coord_type>((nOffset - y * nLineSpace) / nPixelSpace);
if (nBandCount == 1)
band = 0;
else
band = static_cast<int>((nOffset - y * nLineSpace -
static_cast<size_t>(x) * nPixelSpace) /
nBandSpace);
}
}
/************************************************************************/
/* GotoNextPixel() */
/************************************************************************/
bool GDALVirtualMem::GotoNextPixel(coord_type &x, coord_type &y,
int &band) const
{
if (IsBandSequential())
{
++x;
if (x == nBufXSize)
{
x = 0;
++y;
}
if (y == nBufYSize)
{
y = 0;
band++;
if (band == nBandCount)
return false;
}
}
else
{
band++;
if (band == nBandCount)
{
band = 0;
++x;
}
if (x == nBufXSize)
{
x = 0;
++y;
if (y == nBufYSize)
return false;
}
}
return true;
}
/************************************************************************/
/* GetOffset() */
/************************************************************************/
size_t GDALVirtualMem::GetOffset(const coord_type &x, const coord_type &y,
int band) const
{
return static_cast<size_t>(static_cast<size_t>(x) * nPixelSpace +
y * nLineSpace + band * nBandSpace);
}
/************************************************************************/
/* DoIOPixelInterleaved() */
/************************************************************************/
void GDALVirtualMem::DoIOPixelInterleaved(GDALRWFlag eRWFlag,
const size_t nOffset, void *pPage,
size_t nBytes) const
{
coord_type x = 0;
coord_type y = 0;
int band = 0;
GetXYBand(nOffset, x, y, band);
#ifdef DEBUG_VERBOSE
fprintf(stderr, "eRWFlag=%d, nOffset=%d, x=%d, y=%d, band=%d\n", /*ok*/
eRWFlag, static_cast<int>(nOffset), x, y, band);
#endif
if (eRWFlag == GF_Read && !IsCompact())
memset(pPage, 0, nBytes);
if (band >= nBandCount)
{
band = nBandCount - 1;
if (!GotoNextPixel(x, y, band))
return;
}
else if (x >= nBufXSize)
{
x = nBufXSize - 1;
band = nBandCount - 1;
if (!GotoNextPixel(x, y, band))
return;
}
size_t nOffsetRecompute = GetOffset(x, y, band);
CPLAssert(nOffsetRecompute >= nOffset);
size_t nOffsetShift = nOffsetRecompute - nOffset;
if (nOffsetShift >= nBytes)
return;
// If we don't start at the first band for that given pixel, load/store
// the remaining bands
if (band > 0)
{
size_t nEndOffsetEndOfPixel = GetOffset(x, y, nBandCount);
int bandEnd = nBandCount;
// Check that we have enough space to load/store until last band
// Should be always OK unless the number of bands is really huge
if (nEndOffsetEndOfPixel - nOffset > nBytes)
{
// Not enough space: find last possible band
coord_type xEnd, yEnd;
GetXYBand(nOffset + nBytes, xEnd, yEnd, bandEnd);
CPLAssert(x == xEnd);
CPLAssert(y == yEnd);
}
// Finish reading/writing the remaining bands for that pixel
CPL_IGNORE_RET_VAL(GDALDatasetRasterIO(
hDS, eRWFlag, nXOff + x, nYOff + y, 1, 1,
static_cast<char *>(pPage) + nOffsetShift, 1, 1, eBufType,
bandEnd - band, panBandMap + band, nPixelSpace,
static_cast<spacing_type>(nLineSpace),
static_cast<spacing_type>(nBandSpace)));
if (bandEnd < nBandCount)
return;
band = nBandCount - 1;
if (!GotoNextPixel(x, y, band))
return;
nOffsetRecompute = GetOffset(x, y, 0);
nOffsetShift = nOffsetRecompute - nOffset;
if (nOffsetShift >= nBytes)
return;
}
// Is there enough place to store/load up to the end of current line ?
size_t nEndOffsetEndOfLine = GetOffset(nBufXSize - 1, y, nBandCount);
if (nEndOffsetEndOfLine - nOffset > nBytes)
{
// No : read/write as many pixels on this line as possible
coord_type xEnd, yEnd;
int bandEnd;
GetXYBand(nOffset + nBytes, xEnd, yEnd, bandEnd);
CPLAssert(y == yEnd);
if (x < xEnd)
{
CPL_IGNORE_RET_VAL(GDALDatasetRasterIO(
hDS, eRWFlag, nXOff + x, nYOff + y, xEnd - x, 1,
static_cast<char *>(pPage) + nOffsetShift, xEnd - x, 1,
eBufType, nBandCount, panBandMap, nPixelSpace,
static_cast<spacing_type>(nLineSpace),
static_cast<spacing_type>(nBandSpace)));
}
// Are there partial bands to read/write for the last pixel ?
if (bandEnd > 0)
{
x = xEnd;
nOffsetRecompute = GetOffset(x, y, 0);
nOffsetShift = nOffsetRecompute - nOffset;
if (nOffsetShift >= nBytes)
return;
if (bandEnd >= nBandCount)
bandEnd = nBandCount;
CPL_IGNORE_RET_VAL(GDALDatasetRasterIO(
hDS, eRWFlag, nXOff + x, nYOff + y, 1, 1,
static_cast<char *>(pPage) + nOffsetShift, 1, 1, eBufType,
bandEnd, panBandMap, nPixelSpace,
static_cast<spacing_type>(nLineSpace),
static_cast<spacing_type>(nBandSpace)));
}
return;
}
// Yes, enough place to read/write until end of line
if (x > 0 || nBytes - nOffsetShift < static_cast<size_t>(nLineSpace))
{
CPL_IGNORE_RET_VAL(GDALDatasetRasterIO(
hDS, eRWFlag, nXOff + x, nYOff + y, nBufXSize - x, 1,
static_cast<char *>(pPage) + nOffsetShift, nBufXSize - x, 1,
eBufType, nBandCount, panBandMap, nPixelSpace,
static_cast<spacing_type>(nLineSpace),
static_cast<spacing_type>(nBandSpace)));
// Go to beginning of next line
x = nBufXSize - 1;
band = nBandCount - 1;
if (!GotoNextPixel(x, y, band))
return;
nOffsetRecompute = GetOffset(x, y, 0);
nOffsetShift = nOffsetRecompute - nOffset;
if (nOffsetShift >= nBytes)
return;
}
// How many whole lines can we store/load ?
coord_type nLineCount =
static_cast<coord_type>((nBytes - nOffsetShift) / nLineSpace);
if (y + nLineCount > nBufYSize)
nLineCount = nBufYSize - y;
if (nLineCount > 0)
{
CPL_IGNORE_RET_VAL(GDALDatasetRasterIO(
hDS, eRWFlag, nXOff + 0, nYOff + y, nBufXSize, nLineCount,
static_cast<GByte *>(pPage) + nOffsetShift, nBufXSize, nLineCount,
eBufType, nBandCount, panBandMap, nPixelSpace,
static_cast<spacing_type>(nLineSpace),
static_cast<spacing_type>(nBandSpace)));
y += nLineCount;
if (y == nBufYSize)
return;
nOffsetRecompute = GetOffset(x, y, 0);
nOffsetShift = nOffsetRecompute - nOffset;
}
if (nOffsetShift < nBytes)
{
DoIOPixelInterleaved(eRWFlag, nOffsetRecompute,
static_cast<char *>(pPage) + nOffsetShift,
nBytes - nOffsetShift);
}
}
/************************************************************************/
/* DoIOPixelInterleaved() */
/************************************************************************/
void GDALVirtualMem::DoIOBandSequential(GDALRWFlag eRWFlag,
const size_t nOffset, void *pPage,
size_t nBytes) const
{
coord_type x = 0;
coord_type y = 0;
int band = 0;
GetXYBand(nOffset, x, y, band);
#if DEBUG_VERBOSE
fprintf(stderr, "eRWFlag=%d, nOffset=%d, x=%d, y=%d, band=%d\n", /*ok*/
eRWFlag, static_cast<int>(nOffset), x, y, band);
#endif
if (eRWFlag == GF_Read && !IsCompact())
memset(pPage, 0, nBytes);
if (x >= nBufXSize)
{
x = nBufXSize - 1;
if (!GotoNextPixel(x, y, band))
return;
}
else if (y >= nBufYSize)
{
x = nBufXSize - 1;
y = nBufYSize - 1;
if (!GotoNextPixel(x, y, band))
return;
}
size_t nOffsetRecompute = GetOffset(x, y, band);
CPLAssert(nOffsetRecompute >= nOffset);
size_t nOffsetShift = nOffsetRecompute - nOffset;
if (nOffsetShift >= nBytes)
return;
// Is there enough place to store/load up to the end of current line?
size_t nEndOffsetEndOfLine = GetOffset(nBufXSize, y, band);
if (nEndOffsetEndOfLine - nOffset > nBytes)
{
// No : read/write as many pixels on this line as possible
coord_type xEnd, yEnd;
int bandEnd;
GetXYBand(nOffset + nBytes, xEnd, yEnd, bandEnd);
CPLAssert(y == yEnd);
CPLAssert(band == bandEnd);
CPL_IGNORE_RET_VAL(GDALRasterIO(
hBand ? hBand : GDALGetRasterBand(hDS, panBandMap[band]), eRWFlag,
nXOff + x, nYOff + y, xEnd - x, 1,
static_cast<char *>(pPage) + nOffsetShift, xEnd - x, 1, eBufType,
nPixelSpace, static_cast<spacing_type>(nLineSpace)));
return;
}
// Yes, enough place to read/write until end of line
if (x > 0 || nBytes - nOffsetShift < static_cast<size_t>(nLineSpace))
{
CPL_IGNORE_RET_VAL(GDALRasterIO(
hBand ? hBand : GDALGetRasterBand(hDS, panBandMap[band]), eRWFlag,
nXOff + x, nYOff + y, nBufXSize - x, 1,
static_cast<char *>(pPage) + nOffsetShift, nBufXSize - x, 1,
eBufType, nPixelSpace, static_cast<spacing_type>(nLineSpace)));
// Go to beginning of next line
x = nBufXSize - 1;
if (!GotoNextPixel(x, y, band))
return;
nOffsetRecompute = GetOffset(x, y, band);
nOffsetShift = nOffsetRecompute - nOffset;
if (nOffsetShift >= nBytes)
return;
}
// How many whole lines can we store/load ?
coord_type nLineCount =
static_cast<coord_type>((nBytes - nOffsetShift) / nLineSpace);
if (y + nLineCount > nBufYSize)
nLineCount = nBufYSize - y;
if (nLineCount > 0)
{
CPL_IGNORE_RET_VAL(GDALRasterIO(
hBand ? hBand : GDALGetRasterBand(hDS, panBandMap[band]), eRWFlag,
nXOff + 0, nYOff + y, nBufXSize, nLineCount,
static_cast<GByte *>(pPage) + nOffsetShift, nBufXSize, nLineCount,
eBufType, nPixelSpace, static_cast<spacing_type>(nLineSpace)));
y += nLineCount;
if (y == nBufYSize)
{
y = 0;
band++;
if (band == nBandCount)
return;
}
nOffsetRecompute = GetOffset(x, y, band);
nOffsetShift = nOffsetRecompute - nOffset;
}
if (nOffsetShift < nBytes)
{
DoIOBandSequential(eRWFlag, nOffsetRecompute,
static_cast<char *>(pPage) + nOffsetShift,
nBytes - nOffsetShift);
}
}
/************************************************************************/
/* FillCacheBandSequential() */
/************************************************************************/
void GDALVirtualMem::FillCacheBandSequential(CPLVirtualMem *, size_t nOffset,
void *pPageToFill, size_t nToFill,
void *pUserData)
{
const GDALVirtualMem *psParams = static_cast<GDALVirtualMem *>(pUserData);
psParams->DoIOBandSequential(GF_Read, nOffset, pPageToFill, nToFill);
}
/************************************************************************/
/* SaveFromCacheBandSequential() */
/************************************************************************/
void GDALVirtualMem::SaveFromCacheBandSequential(CPLVirtualMem *,
size_t nOffset,
const void *pPageToBeEvicted,
size_t nToEvicted,
void *pUserData)
{
const GDALVirtualMem *psParams = static_cast<GDALVirtualMem *>(pUserData);
psParams->DoIOBandSequential(
GF_Write, nOffset, const_cast<void *>(pPageToBeEvicted), nToEvicted);
}
/************************************************************************/
/* FillCachePixelInterleaved() */
/************************************************************************/
void GDALVirtualMem::FillCachePixelInterleaved(CPLVirtualMem *, size_t nOffset,
void *pPageToFill,
size_t nToFill, void *pUserData)
{
const GDALVirtualMem *psParams = static_cast<GDALVirtualMem *>(pUserData);
psParams->DoIOPixelInterleaved(GF_Read, nOffset, pPageToFill, nToFill);
}
/************************************************************************/
/* SaveFromCachePixelInterleaved() */
/************************************************************************/
void GDALVirtualMem::SaveFromCachePixelInterleaved(CPLVirtualMem *,
size_t nOffset,
const void *pPageToBeEvicted,
size_t nToEvicted,
void *pUserData)
{
const GDALVirtualMem *psParams = static_cast<GDALVirtualMem *>(pUserData);
psParams->DoIOPixelInterleaved(
GF_Write, nOffset, const_cast<void *>(pPageToBeEvicted), nToEvicted);
}
/************************************************************************/
/* Destroy() */
/************************************************************************/
void GDALVirtualMem::Destroy(void *pUserData)
{
GDALVirtualMem *psParams = static_cast<GDALVirtualMem *>(pUserData);
delete psParams;
}
/************************************************************************/
/* GDALCheckBandParameters() */
/************************************************************************/
static bool GDALCheckBandParameters(GDALDatasetH hDS, int nBandCount,
int *panBandMap)
{
if (nBandCount == 0)
{
CPLError(CE_Failure, CPLE_AppDefined, "nBandCount == 0");
return false;
}
if (panBandMap != nullptr)
{
for (int i = 0; i < nBandCount; i++)
{
if (panBandMap[i] < 1 || panBandMap[i] > GDALGetRasterCount(hDS))
{
CPLError(CE_Failure, CPLE_AppDefined, "panBandMap[%d]=%d", i,
panBandMap[i]);
return false;
}
}
}
else if (nBandCount > GDALGetRasterCount(hDS))
{
CPLError(CE_Failure, CPLE_AppDefined,
"nBandCount > GDALGetRasterCount(hDS)");
return false;
}
return true;
}
/************************************************************************/
/* GDALGetVirtualMem() */
/************************************************************************/
static CPLVirtualMem *
GDALGetVirtualMem(GDALDatasetH hDS, GDALRasterBandH hBand, GDALRWFlag eRWFlag,
coord_type nXOff, coord_type nYOff, coord_type nXSize,
coord_type nYSize, coord_type nBufXSize, coord_type nBufYSize,
GDALDataType eBufType, int nBandCount, int *panBandMap,
int nPixelSpace, GIntBig nLineSpace, GIntBig nBandSpace,
size_t nCacheSize, size_t nPageSizeHint,
int bSingleThreadUsage, CSLConstList /*papszOptions*/)
{
CPLVirtualMem *view = nullptr;
GDALVirtualMem *psParams = nullptr;
GUIntBig nReqMem = 0;
if (nXSize != nBufXSize || nYSize != nBufYSize)
{
CPLError(CE_Failure, CPLE_NotSupported,
"nXSize != nBufXSize || nYSize != nBufYSize");
return nullptr;
}
int nRasterXSize =
hDS ? GDALGetRasterXSize(hDS) : GDALGetRasterBandXSize(hBand);
int nRasterYSize =
hDS ? GDALGetRasterYSize(hDS) : GDALGetRasterBandYSize(hBand);
if (nXOff < 0 || nYOff < 0 || nXSize == 0 || nYSize == 0 || nBufXSize < 0 ||
nBufYSize < 0 || nXOff + nXSize > nRasterXSize ||
nYOff + nYSize > nRasterYSize)
{
CPLError(CE_Failure, CPLE_AppDefined, "Invalid window request");
return nullptr;
}
if (nPixelSpace < 0 || nLineSpace < 0 || nBandSpace < 0)
{
CPLError(CE_Failure, CPLE_NotSupported,
"nPixelSpace < 0 || nLineSpace < 0 || nBandSpace < 0");
return nullptr;
}
if (hDS != nullptr && !GDALCheckBandParameters(hDS, nBandCount, panBandMap))
return nullptr;
const int nDataTypeSize = GDALGetDataTypeSizeBytes(eBufType);
if (nPixelSpace == 0)
nPixelSpace = nDataTypeSize;
if (nLineSpace == 0)
nLineSpace = static_cast<GIntBig>(nBufXSize) * nPixelSpace;
if (nBandSpace == 0)
nBandSpace = static_cast<GIntBig>(nBufYSize) * nLineSpace;
// OFFSET = offset(x,y,band) = x * nPixelSpace + y * nLineSpace + band *
// nBandSpace where 0 <= x < nBufXSize and 0 <= y < nBufYSize and 0 <= band
// < nBandCount if nPixelSpace, nLineSpace and nBandSpace can have arbitrary
// values, there is no way of finding a unique(x,y,band) solution. We need
// to restrict the space of possibilities strongly.
// if nBandSpace >= nBufYSize * nLineSpace and
// nLineSpace >= nBufXSize * nPixelSpace, INTERLEAVE = BAND
// band = OFFSET / nBandSpace
// y = (OFFSET - band * nBandSpace) / nLineSpace
// x = (OFFSET - band * nBandSpace - y * nLineSpace) / nPixelSpace
// else if nPixelSpace >= nBandCount * nBandSpace and
// nLineSpace >= nBufXSize * nPixelSpace, INTERLEAVE = PIXEL
// y = OFFSET / nLineSpace
// x = (OFFSET - y * nLineSpace) / nPixelSpace
// band = (OFFSET - y * nLineSpace - x * nPixelSpace) / nBandSpace
if (nDataTypeSize == 0 || /* to please Coverity. not needed */
nLineSpace < static_cast<GIntBig>(nBufXSize) * nPixelSpace ||
(nBandCount > 1 &&
(nBandSpace == nPixelSpace ||
(nBandSpace < nPixelSpace &&
(nBandSpace < nDataTypeSize ||
nPixelSpace < nBandCount * nBandSpace)) ||
(nBandSpace > nPixelSpace && (nPixelSpace < nDataTypeSize ||
nBandSpace < nBufYSize * nLineSpace)))))
{
CPLError(CE_Failure, CPLE_NotSupported,
"Only pixel interleaving or band interleaving are supported");
return nullptr;
}
/* Avoid odd spacings that would complicate I/O operations */
/* Ensuring they are multiple of nDataTypeSize should be fine, because */
/* the page size is a power of 2 that is also a multiple of nDataTypeSize */
if ((nPixelSpace % nDataTypeSize) != 0 ||
(nLineSpace % nDataTypeSize) != 0 || (nBandSpace % nDataTypeSize) != 0)
{
CPLError(CE_Failure, CPLE_NotSupported, "Unsupported spacing");
return nullptr;
}
bool bIsBandSequential = nBandSpace >= nBufYSize * nLineSpace;
if (bIsBandSequential)
nReqMem = nBandCount * nBandSpace;
else
nReqMem = nBufYSize * nLineSpace;
if (nReqMem != static_cast<GUIntBig>(static_cast<size_t>(nReqMem)))
{
CPLError(CE_Failure, CPLE_OutOfMemory,
"Cannot reserve " CPL_FRMT_GUIB " bytes", nReqMem);
return nullptr;
}
psParams = new GDALVirtualMem(
hDS, hBand, nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize,
eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace, nBandSpace);
view = CPLVirtualMemNew(
static_cast<size_t>(nReqMem), nCacheSize, nPageSizeHint,
bSingleThreadUsage,
eRWFlag == GF_Read ? VIRTUALMEM_READONLY_ENFORCED
: VIRTUALMEM_READWRITE,
bIsBandSequential ? GDALVirtualMem::FillCacheBandSequential
: GDALVirtualMem::FillCachePixelInterleaved,
bIsBandSequential ? GDALVirtualMem::SaveFromCacheBandSequential
: GDALVirtualMem::SaveFromCachePixelInterleaved,
GDALVirtualMem::Destroy, psParams);
if (view == nullptr)
{
delete psParams;
}
return view;
}
/************************************************************************/
/* GDALDatasetGetVirtualMem() */
/************************************************************************/
/** Create a CPLVirtualMem object from a GDAL dataset object.
*
* Only supported on Linux for now.
*
* This method allows creating a virtual memory object for a region of one
* or more GDALRasterBands from this dataset. The content of the virtual
* memory object is automatically filled from dataset content when a virtual
* memory page is first accessed, and it is released (or flushed in case of a
* "dirty" page) when the cache size limit has been reached.
*
* The pointer to access the virtual memory object is obtained with
* CPLVirtualMemGetAddr(). It remains valid until CPLVirtualMemFree() is called.
* CPLVirtualMemFree() must be called before the dataset object is destroyed.
*
* If p is such a pointer and base_type the C type matching eBufType, for
* default values of spacing parameters, the element of image coordinates (x, y)
* (relative to xOff, yOff) for band b can be accessed with
* ((base_type*)p)[x + y * nBufXSize + (b-1)*nBufXSize*nBufYSize].
*
* Note that the mechanism used to transparently fill memory pages when they are
* accessed is the same (but in a controlled way) than what occurs when a memory
* error occurs in a program. Debugging software will generally interrupt
* program execution when that happens. If needed, CPLVirtualMemPin() can be
* used to avoid that by ensuring memory pages are allocated before being
* accessed.
*
* The size of the region that can be mapped as a virtual memory object depends
* on hardware and operating system limitations.
* On Linux AMD64 platforms, the maximum value is 128 TB.
* On Linux x86 platforms, the maximum value is 2 GB.
*
* Data type translation is automatically done if the data type
* (eBufType) of the buffer is different than
* that of the GDALRasterBand.
*
* Image decimation / replication is currently not supported, i.e. if the
* size of the region being accessed (nXSize x nYSize) is different from the
* buffer size (nBufXSize x nBufYSize).
*
* The nPixelSpace, nLineSpace and nBandSpace parameters allow reading into or
* writing from various organization of buffers. Arbitrary values for the
* spacing parameters are not supported. Those values must be multiple of the
* size of thebuffer data type, and must be either band sequential
* organization (typically nPixelSpace = GDALGetDataTypeSizeBytes(eBufType),
* nLineSpace = nPixelSpace * nBufXSize,
* nBandSpace = nLineSpace * nBufYSize), or pixel-interleaved organization
* (typically nPixelSpace = nBandSpace * nBandCount,
* nLineSpace = nPixelSpace * nBufXSize,
* nBandSpace = GDALGetDataTypeSizeBytes(eBufType))
*
* @param hDS Dataset object
*
* @param eRWFlag Either GF_Read to read a region of data, or GF_Write to
* write a region of data.
*
* @param nXOff The pixel offset to the top left corner of the region
* of the band to be accessed. This would be zero to start from the left side.
*
* @param nYOff The line offset to the top left corner of the region
* of the band to be accessed. This would be zero to start from the top.
*
* @param nXSize The width of the region of the band to be accessed in pixels.
*
* @param nYSize The height of the region of the band to be accessed in lines.
*
* @param nBufXSize the width of the buffer image into which the desired region
* is to be read, or from which it is to be written.
*
* @param nBufYSize the height of the buffer image into which the desired
* region is to be read, or from which it is to be written.
*
* @param eBufType the type of the pixel values in the data buffer. The
* pixel values will automatically be translated to/from the GDALRasterBand
* data type as needed.
*
* @param nBandCount the number of bands being read or written.
*
* @param panBandMap the list of nBandCount band numbers being read/written.
* Note band numbers are 1 based. This may be NULL to select the first
* nBandCount bands.
*
* @param nPixelSpace The byte offset from the start of one pixel value in
* the buffer to the start of the next pixel value within a scanline. If
* defaulted (0) the size of the datatype eBufType is used.
*
* @param nLineSpace The byte offset from the start of one scanline in
* the buffer to the start of the next. If defaulted (0) the size of the
* datatype eBufType * nBufXSize is used.
*
* @param nBandSpace the byte offset from the start of one bands data to the
* start of the next. If defaulted (0) the value will be
* nLineSpace * nBufYSize implying band sequential organization
* of the data buffer.
*
* @param nCacheSize size in bytes of the maximum memory that will be really
* allocated (must ideally fit into RAM)
*
* @param nPageSizeHint hint for the page size. Must be a multiple of the
* system page size, returned by CPLGetPageSize().
* Minimum value is generally 4096. Might be set to 0 to
* let the function determine a default page size.
*
* @param bSingleThreadUsage set to TRUE if there will be no concurrent threads
* that will access the virtual memory mapping. This
* can optimize performance a bit. If set to FALSE,
* CPLVirtualMemDeclareThread() must be called.
*
* @param papszOptions NULL terminated list of options. Unused for now.
*
* @return a virtual memory object that must be freed by CPLVirtualMemFree(),
* or NULL in case of failure.
*
* @since GDAL 1.11
*/
CPLVirtualMem *GDALDatasetGetVirtualMem(
GDALDatasetH hDS, GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
int nYSize, int nBufXSize, int nBufYSize, GDALDataType eBufType,
int nBandCount, int *panBandMap, int nPixelSpace, GIntBig nLineSpace,
GIntBig nBandSpace, size_t nCacheSize, size_t nPageSizeHint,
int bSingleThreadUsage, CSLConstList papszOptions)
{
return GDALGetVirtualMem(hDS, nullptr, eRWFlag, nXOff, nYOff, nXSize,
nYSize, nBufXSize, nBufYSize, eBufType, nBandCount,
panBandMap, nPixelSpace, nLineSpace, nBandSpace,
nCacheSize, nPageSizeHint, bSingleThreadUsage,
papszOptions);
}
/************************************************************************/
/* GDALRasterBandGetVirtualMem() */
/************************************************************************/
/** Create a CPLVirtualMem object from a GDAL raster band object.
*
* Only supported on Linux for now.
*
* This method allows creating a virtual memory object for a region of a
* GDALRasterBand. The content of the virtual
* memory object is automatically filled from dataset content when a virtual
* memory page is first accessed, and it is released (or flushed in case of a
* "dirty" page) when the cache size limit has been reached.
*
* The pointer to access the virtual memory object is obtained with
* CPLVirtualMemGetAddr(). It remains valid until CPLVirtualMemFree() is called.
* CPLVirtualMemFree() must be called before the raster band object is
* destroyed.
*
* If p is such a pointer and base_type the C type matching eBufType, for
* values of spacing parameters, the element of image coordinates (x, y)
* default (relative to xOff, yOff) can be accessed with
* ((base_type*)p)[x + y * nBufXSize].
*
* Note that the mechanism used to transparently fill memory pages when they are
* accessed is the same (but in a controlled way) than what occurs when a memory
* error occurs in a program. Debugging software will generally interrupt
* program execution when that happens. If needed, CPLVirtualMemPin() can be
* used to avoid that by ensuring memory pages are allocated before being
* accessed.
*
* The size of the region that can be mapped as a virtual memory object depends
* on hardware and operating system limitations.
* On Linux AMD64 platforms, the maximum value is 128 TB.
* On Linux x86 platforms, the maximum value is 2 GB.
*
* Data type translation is automatically done if the data type
* (eBufType) of the buffer is different than
* that of the GDALRasterBand.
*
* Image decimation / replication is currently not supported, i.e. if the
* size of the region being accessed (nXSize x nYSize) is different from the
* buffer size (nBufXSize x nBufYSize).
*
* The nPixelSpace and nLineSpace parameters allow reading into or
* writing from various organization of buffers. Arbitrary values for the
* spacing parameters are not supported. Those values must be multiple of the
* size of the buffer data type and must be such that nLineSpace >=
* nPixelSpace * nBufXSize.
*
* @param hBand Rasterband object
*
* @param eRWFlag Either GF_Read to read a region of data, or GF_Write to
* write a region of data.
*
* @param nXOff The pixel offset to the top left corner of the region
* of the band to be accessed. This would be zero to start from the left side.
*
* @param nYOff The line offset to the top left corner of the region
* of the band to be accessed. This would be zero to start from the top.
*
* @param nXSize The width of the region of the band to be accessed in pixels.
*
* @param nYSize The height of the region of the band to be accessed in lines.
*
* @param nBufXSize the width of the buffer image into which the desired region
* is to be read, or from which it is to be written.
*
* @param nBufYSize the height of the buffer image into which the desired
* region is to be read, or from which it is to be written.
*
* @param eBufType the type of the pixel values in the data buffer. The
* pixel values will automatically be translated to/from the GDALRasterBand
* data type as needed.
*
* @param nPixelSpace The byte offset from the start of one pixel value in the
* buffer to the start of the next pixel value within a scanline. If defaulted
* (0) the size of the datatype eBufType is used.
*
* @param nLineSpace The byte offset from the start of one scanline in the
* buffer to the start of the next. If defaulted (0) the size of the datatype
* eBufType * nBufXSize is used.
*
* @param nCacheSize size in bytes of the maximum memory that will be really
* allocated (must ideally fit into RAM)
*
* @param nPageSizeHint hint for the page size. Must be a multiple of the
* system page size, returned by CPLGetPageSize().
* Minimum value is generally 4096. Might be set to 0 to
* let the function determine a default page size.