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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
|
/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#define LOG_TAG "QCameraHWI_Mem"
// System dependencies
#include <fcntl.h>
#define MMAN_H <SYSTEM_HEADER_PREFIX/mman.h>
#include MMAN_H
#include "gralloc_priv.h"
// Display dependencies
#include "qdMetaData.h"
// Camera dependencies
#include "QCamera3HWI.h"
#include "QCamera3Mem.h"
#include "QCameraTrace.h"
extern "C" {
#include "mm_camera_dbg.h"
#include "mm_camera_interface.h"
}
using namespace android;
namespace qcamera {
// QCaemra2Memory base class
/*===========================================================================
* FUNCTION : QCamera3Memory
*
* DESCRIPTION: default constructor of QCamera3Memory
*
* PARAMETERS : none
*
* RETURN : None
*==========================================================================*/
QCamera3Memory::QCamera3Memory()
{
mBufferCount = 0;
for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i++) {
mMemInfo[i].fd = -1;
mMemInfo[i].main_ion_fd = -1;
mMemInfo[i].handle = 0;
mMemInfo[i].size = 0;
mCurrentFrameNumbers[i] = -1;
}
}
/*===========================================================================
* FUNCTION : ~QCamera3Memory
*
* DESCRIPTION: deconstructor of QCamera3Memory
*
* PARAMETERS : none
*
* RETURN : None
*==========================================================================*/
QCamera3Memory::~QCamera3Memory()
{
}
/*===========================================================================
* FUNCTION : cacheOpsInternal
*
* DESCRIPTION: ion related memory cache operations
*
* PARAMETERS :
* @index : index of the buffer
* @cmd : cache ops command
* @vaddr : ptr to the virtual address
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3Memory::cacheOpsInternal(uint32_t index, unsigned int cmd, void *vaddr)
{
Mutex::Autolock lock(mLock);
struct ion_flush_data cache_inv_data;
struct ion_custom_data custom_data;
int ret = OK;
if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
LOGE("index %d out of bound [0, %d)",
index, MM_CAMERA_MAX_NUM_FRAMES);
return BAD_INDEX;
}
if (0 == mMemInfo[index].handle) {
LOGE("Buffer at %d not registered", index);
return BAD_INDEX;
}
memset(&cache_inv_data, 0, sizeof(cache_inv_data));
memset(&custom_data, 0, sizeof(custom_data));
cache_inv_data.vaddr = vaddr;
cache_inv_data.fd = mMemInfo[index].fd;
cache_inv_data.handle = mMemInfo[index].handle;
cache_inv_data.length = (unsigned int)mMemInfo[index].size;
custom_data.cmd = cmd;
custom_data.arg = (unsigned long)&cache_inv_data;
LOGD("addr = %p, fd = %d, handle = %lx length = %d, ION Fd = %d",
cache_inv_data.vaddr, cache_inv_data.fd,
(unsigned long)cache_inv_data.handle, cache_inv_data.length,
mMemInfo[index].main_ion_fd);
ret = ioctl(mMemInfo[index].main_ion_fd, ION_IOC_CUSTOM, &custom_data);
if (ret < 0)
LOGE("Cache Invalidate failed: %s\n", strerror(errno));
return ret;
}
/*===========================================================================
* FUNCTION : getFd
*
* DESCRIPTION: return file descriptor of the indexed buffer
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : file descriptor
*==========================================================================*/
int QCamera3Memory::getFd(uint32_t index)
{
Mutex::Autolock lock(mLock);
if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
return BAD_INDEX;
}
if (0 == mMemInfo[index].handle) {
return BAD_INDEX;
}
return mMemInfo[index].fd;
}
/*===========================================================================
* FUNCTION : getSize
*
* DESCRIPTION: return buffer size of the indexed buffer
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer size
*==========================================================================*/
ssize_t QCamera3Memory::getSize(uint32_t index)
{
Mutex::Autolock lock(mLock);
if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
return BAD_INDEX;
}
if (0 == mMemInfo[index].handle) {
return BAD_INDEX;
}
return (ssize_t)mMemInfo[index].size;
}
/*===========================================================================
* FUNCTION : getCnt
*
* DESCRIPTION: query number of buffers allocated
*
* PARAMETERS : none
*
* RETURN : number of buffers allocated
*==========================================================================*/
uint32_t QCamera3Memory::getCnt()
{
Mutex::Autolock lock(mLock);
return mBufferCount;
}
/*===========================================================================
* FUNCTION : getBufDef
*
* DESCRIPTION: query detailed buffer information
*
* PARAMETERS :
* @offset : [input] frame buffer offset
* @bufDef : [output] reference to struct to store buffer definition
* @index : [input] index of the buffer
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int32_t QCamera3Memory::getBufDef(const cam_frame_len_offset_t &offset,
mm_camera_buf_def_t &bufDef, uint32_t index)
{
Mutex::Autolock lock(mLock);
if (!mBufferCount) {
LOGE("Memory not allocated");
return NO_INIT;
}
bufDef.fd = mMemInfo[index].fd;
bufDef.frame_len = mMemInfo[index].size;
bufDef.mem_info = (void *)this;
bufDef.buffer = getPtrLocked(index);
bufDef.planes_buf.num_planes = (int8_t)offset.num_planes;
bufDef.buf_idx = (uint8_t)index;
/* Plane 0 needs to be set separately. Set other planes in a loop */
bufDef.planes_buf.planes[0].length = offset.mp[0].len;
bufDef.planes_buf.planes[0].m.userptr = (long unsigned int)mMemInfo[index].fd;
bufDef.planes_buf.planes[0].data_offset = offset.mp[0].offset;
bufDef.planes_buf.planes[0].reserved[0] = 0;
for (int i = 1; i < bufDef.planes_buf.num_planes; i++) {
bufDef.planes_buf.planes[i].length = offset.mp[i].len;
bufDef.planes_buf.planes[i].m.userptr = (long unsigned int)mMemInfo[i].fd;
bufDef.planes_buf.planes[i].data_offset = offset.mp[i].offset;
bufDef.planes_buf.planes[i].reserved[0] =
bufDef.planes_buf.planes[i-1].reserved[0] +
bufDef.planes_buf.planes[i-1].length;
}
return NO_ERROR;
}
/*===========================================================================
* FUNCTION : QCamera3HeapMemory
*
* DESCRIPTION: constructor of QCamera3HeapMemory for ion memory used internally in HAL
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
QCamera3HeapMemory::QCamera3HeapMemory(uint32_t maxCnt)
: QCamera3Memory()
{
mMaxCnt = MIN(maxCnt, MM_CAMERA_MAX_NUM_FRAMES);
for (uint32_t i = 0; i < mMaxCnt; i ++)
mPtr[i] = NULL;
}
/*===========================================================================
* FUNCTION : ~QCamera3HeapMemory
*
* DESCRIPTION: deconstructor of QCamera3HeapMemory
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
QCamera3HeapMemory::~QCamera3HeapMemory()
{
}
/*===========================================================================
* FUNCTION : allocOneBuffer
*
* DESCRIPTION: impl of allocating one buffers of certain size
*
* PARAMETERS :
* @memInfo : [output] reference to struct to store additional memory allocation info
* @heap : [input] heap id to indicate where the buffers will be allocated from
* @size : [input] lenght of the buffer to be allocated
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3HeapMemory::allocOneBuffer(QCamera3MemInfo &memInfo,
unsigned int heap_id, size_t size)
{
int rc = OK;
struct ion_handle_data handle_data;
struct ion_allocation_data allocData;
struct ion_fd_data ion_info_fd;
int main_ion_fd = -1;
main_ion_fd = open("/dev/ion", O_RDONLY);
if (main_ion_fd < 0) {
LOGE("Ion dev open failed: %s\n", strerror(errno));
goto ION_OPEN_FAILED;
}
memset(&allocData, 0, sizeof(allocData));
allocData.len = size;
/* to make it page size aligned */
allocData.len = (allocData.len + 4095U) & (~4095U);
allocData.align = 4096;
allocData.flags = ION_FLAG_CACHED;
allocData.heap_id_mask = heap_id;
rc = ioctl(main_ion_fd, ION_IOC_ALLOC, &allocData);
if (rc < 0) {
LOGE("ION allocation for len %d failed: %s\n", allocData.len,
strerror(errno));
goto ION_ALLOC_FAILED;
}
memset(&ion_info_fd, 0, sizeof(ion_info_fd));
ion_info_fd.handle = allocData.handle;
rc = ioctl(main_ion_fd, ION_IOC_SHARE, &ion_info_fd);
if (rc < 0) {
LOGE("ION map failed %s\n", strerror(errno));
goto ION_MAP_FAILED;
}
memInfo.main_ion_fd = main_ion_fd;
memInfo.fd = ion_info_fd.fd;
memInfo.handle = ion_info_fd.handle;
memInfo.size = allocData.len;
return OK;
ION_MAP_FAILED:
memset(&handle_data, 0, sizeof(handle_data));
handle_data.handle = ion_info_fd.handle;
ioctl(main_ion_fd, ION_IOC_FREE, &handle_data);
ION_ALLOC_FAILED:
close(main_ion_fd);
ION_OPEN_FAILED:
return NO_MEMORY;
}
/*===========================================================================
* FUNCTION : deallocOneBuffer
*
* DESCRIPTION: impl of deallocating one buffers
*
* PARAMETERS :
* @memInfo : reference to struct that stores additional memory allocation info
*
* RETURN : none
*==========================================================================*/
void QCamera3HeapMemory::deallocOneBuffer(QCamera3MemInfo &memInfo)
{
struct ion_handle_data handle_data;
if (memInfo.fd >= 0) {
close(memInfo.fd);
memInfo.fd = -1;
}
if (memInfo.main_ion_fd >= 0) {
memset(&handle_data, 0, sizeof(handle_data));
handle_data.handle = memInfo.handle;
ioctl(memInfo.main_ion_fd, ION_IOC_FREE, &handle_data);
close(memInfo.main_ion_fd);
memInfo.main_ion_fd = -1;
}
memInfo.handle = 0;
memInfo.size = 0;
}
/*===========================================================================
* FUNCTION : getPtrLocked
*
* DESCRIPTION: Return buffer pointer.
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer ptr
*==========================================================================*/
void *QCamera3HeapMemory::getPtrLocked(uint32_t index)
{
if (index >= mBufferCount) {
LOGE("index out of bound");
return (void *)BAD_INDEX;
}
return mPtr[index];
}
/*===========================================================================
* FUNCTION : markFrameNumber
*
* DESCRIPTION: We use this function from the request call path to mark the
* buffers with the frame number they are intended for this info
* is used later when giving out callback & it is duty of PP to
* ensure that data for that particular frameNumber/Request is
* written to this buffer.
* PARAMETERS :
* @index : index of the buffer
* @frame# : Frame number from the framework
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int32_t QCamera3HeapMemory::markFrameNumber(uint32_t index, uint32_t frameNumber)
{
Mutex::Autolock lock(mLock);
if (index >= mBufferCount) {
LOGE("Index %d out of bounds, current buffer count is %d",
index, mBufferCount);
return BAD_INDEX;
}
if (0 == mMemInfo[index].handle) {
LOGE("Buffer at %d not allocated", index);
return BAD_INDEX;
}
mCurrentFrameNumbers[index] = (int32_t)frameNumber;
return NO_ERROR;
}
/*===========================================================================
* FUNCTION : getFrameNumber
*
* DESCRIPTION: We use this to fetch the frameNumber for the request with which
* this buffer was given to HAL
*
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : int32_t frameNumber
* positive/zero -- success
* negative failure
*==========================================================================*/
int32_t QCamera3HeapMemory::getFrameNumber(uint32_t index)
{
Mutex::Autolock lock(mLock);
if (index >= mBufferCount) {
LOGE("Index %d out of bounds, current buffer count is %d",
index, mBufferCount);
return -1;
}
if (0 == mMemInfo[index].handle) {
LOGE("Buffer at %d not registered", index);
return -1;
}
return mCurrentFrameNumbers[index];
}
/*===========================================================================
* FUNCTION : getBufferIndex
*
* DESCRIPTION: We use this to fetch the buffer index for the request with
* a particular frame number
*
*
* PARAMETERS :
* @frameNumber : frame number of the buffer
*
* RETURN : int32_t buffer index
* negative failure
*==========================================================================*/
int32_t QCamera3HeapMemory::getBufferIndex(uint32_t frameNumber)
{
Mutex::Autolock lock(mLock);
for (uint32_t index = 0;
index < mBufferCount; index++) {
if (mMemInfo[index].handle &&
mCurrentFrameNumbers[index] == (int32_t)frameNumber)
return (int32_t)index;
}
return -1;
}
/*===========================================================================
* FUNCTION : getPtr
*
* DESCRIPTION: Return buffer pointer
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer ptr
*==========================================================================*/
void *QCamera3HeapMemory::getPtr(uint32_t index)
{
return getPtrLocked(index);
}
/*===========================================================================
* FUNCTION : allocate
*
* DESCRIPTION: allocate requested number of buffers of certain size
*
* PARAMETERS :
* @size : lenght of the buffer to be allocated
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3HeapMemory::allocate(size_t size)
{
unsigned int heap_id_mask = 0x1 << ION_IOMMU_HEAP_ID;
uint32_t i;
int rc = NO_ERROR;
//Note that now we allow incremental allocation. In other words, we allow
//multiple alloc being called as long as the sum of count does not exceed
//mMaxCnt.
if (mBufferCount > 0) {
LOGE("There is already buffer allocated.");
return BAD_INDEX;
}
for (i = 0; i < mMaxCnt; i ++) {
rc = allocOneBuffer(mMemInfo[i], heap_id_mask, size);
if (rc < 0) {
LOGE("AllocateIonMemory failed");
goto ALLOC_FAILED;
}
void *vaddr = mmap(NULL,
mMemInfo[i].size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
mMemInfo[i].fd, 0);
if (vaddr == MAP_FAILED) {
deallocOneBuffer(mMemInfo[i]);
LOGE("mmap failed for buffer %d", i);
goto ALLOC_FAILED;
} else
mPtr[i] = vaddr;
}
if (rc == 0)
mBufferCount = mMaxCnt;
return OK;
ALLOC_FAILED:
for (uint32_t j = 0; j < i; j++) {
munmap(mPtr[j], mMemInfo[j].size);
mPtr[j] = NULL;
deallocOneBuffer(mMemInfo[j]);
}
return NO_MEMORY;
}
/*===========================================================================
* FUNCTION : allocateOne
*
* DESCRIPTION: allocate one buffer
*
* PARAMETERS :
* @size : lenght of the buffer to be allocated
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3HeapMemory::allocateOne(size_t size)
{
unsigned int heap_id_mask = 0x1 << ION_IOMMU_HEAP_ID;
int rc = NO_ERROR;
//Note that now we allow incremental allocation. In other words, we allow
//multiple alloc being called as long as the sum of count does not exceed
//mMaxCnt.
if (mBufferCount + 1 > mMaxCnt) {
LOGE("Buffer count %d + 1 out of bound. Max is %d",
mBufferCount, mMaxCnt);
return BAD_INDEX;
}
rc = allocOneBuffer(mMemInfo[mBufferCount], heap_id_mask, size);
if (rc < 0) {
LOGE("AllocateIonMemory failed");
return NO_MEMORY;
}
void *vaddr = mmap(NULL,
mMemInfo[mBufferCount].size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
mMemInfo[mBufferCount].fd, 0);
if (vaddr == MAP_FAILED) {
deallocOneBuffer(mMemInfo[mBufferCount]);
LOGE("mmap failed for buffer");
return NO_MEMORY;
} else
mPtr[mBufferCount] = vaddr;
if (rc == 0)
mBufferCount += 1;
return mBufferCount-1;
}
/*===========================================================================
* FUNCTION : deallocate
*
* DESCRIPTION: deallocate buffers
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
void QCamera3HeapMemory::deallocate()
{
for (uint32_t i = 0; i < mBufferCount; i++) {
munmap(mPtr[i], mMemInfo[i].size);
mPtr[i] = NULL;
deallocOneBuffer(mMemInfo[i]);
mCurrentFrameNumbers[i] = -1;
}
mBufferCount = 0;
}
/*===========================================================================
* FUNCTION : cacheOps
*
* DESCRIPTION: ion related memory cache operations
*
* PARAMETERS :
* @index : index of the buffer
* @cmd : cache ops command
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3HeapMemory::cacheOps(uint32_t index, unsigned int cmd)
{
if (index >= mBufferCount)
return BAD_INDEX;
return cacheOpsInternal(index, cmd, mPtr[index]);
}
/*===========================================================================
* FUNCTION : getMatchBufIndex
*
* DESCRIPTION: query buffer index by object ptr
*
* PARAMETERS :
* @object : object ptr
*
* RETURN : buffer index if match found,
* -1 if failed
*==========================================================================*/
int QCamera3HeapMemory::getMatchBufIndex(void * /*object*/)
{
/*
TODO for HEAP memory type, would there be an equivalent requirement?
int index = -1;
buffer_handle_t *key = (buffer_handle_t*) object;
if (!key) {
return BAD_VALUE;
}
for (int i = 0; i < mBufferCount; i++) {
if (mBufferHandle[i] == key) {
index = i;
break;
}
}
return index;
*/
LOGE("FATAL: Not supposed to come here");
return -1;
}
/*===========================================================================
* FUNCTION : QCamera3GrallocMemory
*
* DESCRIPTION: constructor of QCamera3GrallocMemory
* preview stream buffers are allocated from gralloc native_windoe
*
* PARAMETERS :
* @startIdx : start index of array after which we can register buffers in.
*
* RETURN : none
*==========================================================================*/
QCamera3GrallocMemory::QCamera3GrallocMemory(uint32_t startIdx)
: QCamera3Memory(), mStartIdx(startIdx)
{
for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i ++) {
mBufferHandle[i] = NULL;
mPrivateHandle[i] = NULL;
}
}
/*===========================================================================
* FUNCTION : ~QCamera3GrallocMemory
*
* DESCRIPTION: deconstructor of QCamera3GrallocMemory
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
QCamera3GrallocMemory::~QCamera3GrallocMemory()
{
}
/*===========================================================================
* FUNCTION : registerBuffer
*
* DESCRIPTION: registers frameworks-allocated gralloc buffer_handle_t
*
* PARAMETERS :
* @buffers : buffer_handle_t pointer
* @type : cam_stream_type_t
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3GrallocMemory::registerBuffer(buffer_handle_t *buffer,
__unused cam_stream_type_t type)
{
status_t ret = NO_ERROR;
struct ion_fd_data ion_info_fd;
void *vaddr = NULL;
int32_t colorSpace = ITU_R_601_FR;
int32_t idx = -1;
LOGD("E");
memset(&ion_info_fd, 0, sizeof(ion_info_fd));
if (0 <= getMatchBufIndex((void *) buffer)) {
LOGL("Buffer already registered");
return ALREADY_EXISTS;
}
Mutex::Autolock lock(mLock);
if (mBufferCount >= (MM_CAMERA_MAX_NUM_FRAMES - 1 - mStartIdx)) {
LOGE("Number of buffers %d greater than what's supported %d",
mBufferCount, MM_CAMERA_MAX_NUM_FRAMES - mStartIdx);
return BAD_INDEX;
}
idx = getFreeIndexLocked();
if (0 > idx) {
LOGE("No available memory slots");
return BAD_INDEX;
}
mBufferHandle[idx] = buffer;
mPrivateHandle[idx] = (struct private_handle_t *)(*mBufferHandle[idx]);
setMetaData(mPrivateHandle[idx], UPDATE_COLOR_SPACE, &colorSpace);
mMemInfo[idx].main_ion_fd = open("/dev/ion", O_RDONLY);
if (mMemInfo[idx].main_ion_fd < 0) {
LOGE("failed: could not open ion device");
ret = NO_MEMORY;
goto end;
} else {
ion_info_fd.fd = mPrivateHandle[idx]->fd;
if (ioctl(mMemInfo[idx].main_ion_fd,
ION_IOC_IMPORT, &ion_info_fd) < 0) {
LOGE("ION import failed\n");
close(mMemInfo[idx].main_ion_fd);
ret = NO_MEMORY;
goto end;
}
}
LOGD("idx = %d, fd = %d, size = %d, offset = %d",
idx, mPrivateHandle[idx]->fd,
mPrivateHandle[idx]->size,
mPrivateHandle[idx]->offset);
mMemInfo[idx].fd = mPrivateHandle[idx]->fd;
mMemInfo[idx].size =
( /* FIXME: Should update ION interface */ size_t)
mPrivateHandle[idx]->size;
mMemInfo[idx].handle = ion_info_fd.handle;
vaddr = mmap(NULL,
mMemInfo[idx].size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
mMemInfo[idx].fd, 0);
if (vaddr == MAP_FAILED) {
mMemInfo[idx].handle = 0;
ret = NO_MEMORY;
} else {
mPtr[idx] = vaddr;
mBufferCount++;
}
end:
LOGD("X ");
return ret;
}
/*===========================================================================
* FUNCTION : unregisterBufferLocked
*
* DESCRIPTION: Unregister buffer. Please note that this method has to be
* called with 'mLock' acquired.
*
* PARAMETERS :
* @idx : unregister buffer at index 'idx'
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int32_t QCamera3GrallocMemory::unregisterBufferLocked(size_t idx)
{
munmap(mPtr[idx], mMemInfo[idx].size);
mPtr[idx] = NULL;
struct ion_handle_data ion_handle;
memset(&ion_handle, 0, sizeof(ion_handle));
ion_handle.handle = mMemInfo[idx].handle;
if (ioctl(mMemInfo[idx].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
LOGE("ion free failed");
}
close(mMemInfo[idx].main_ion_fd);
memset(&mMemInfo[idx], 0, sizeof(struct QCamera3MemInfo));
mMemInfo[idx].main_ion_fd = -1;
mBufferHandle[idx] = NULL;
mPrivateHandle[idx] = NULL;
mCurrentFrameNumbers[idx] = -1;
mBufferCount--;
return NO_ERROR;
}
/*===========================================================================
* FUNCTION : unregisterBuffer
*
* DESCRIPTION: unregister buffer
*
* PARAMETERS :
* @idx : unregister buffer at index 'idx'
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int32_t QCamera3GrallocMemory::unregisterBuffer(size_t idx)
{
int32_t rc = NO_ERROR;
Mutex::Autolock lock(mLock);
LOGD("E ", __FUNCTION__);
if (MM_CAMERA_MAX_NUM_FRAMES <= idx) {
LOGE("Buffer index %d greater than what is supported %d",
idx, MM_CAMERA_MAX_NUM_FRAMES);
return BAD_VALUE;
}
if (idx < mStartIdx) {
LOGE("buffer index %d less than starting index %d",
idx, mStartIdx);
return BAD_INDEX;
}
if (0 == mMemInfo[idx].handle) {
LOGE("Trying to unregister buffer at %d which still not registered",
idx);
return BAD_VALUE;
}
rc = unregisterBufferLocked(idx);
LOGD("X ",__FUNCTION__);
return rc;
}
/*===========================================================================
* FUNCTION : unregisterBuffers
*
* DESCRIPTION: unregister buffers
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
void QCamera3GrallocMemory::unregisterBuffers()
{
int err = NO_ERROR;
Mutex::Autolock lock(mLock);
LOGD("E ", __FUNCTION__);
for (uint32_t cnt = mStartIdx; cnt < MM_CAMERA_MAX_NUM_FRAMES; cnt++) {
if (0 == mMemInfo[cnt].handle) {
continue;
}
err = unregisterBufferLocked(cnt);
if (NO_ERROR != err) {
LOGE("Error unregistering buffer %d error %d",
cnt, err);
}
}
mBufferCount = 0;
LOGD("X ",__FUNCTION__);
}
/*===========================================================================
* FUNCTION : markFrameNumber
*
* DESCRIPTION: We use this function from the request call path to mark the
* buffers with the frame number they are intended for this info
* is used later when giving out callback & it is duty of PP to
* ensure that data for that particular frameNumber/Request is
* written to this buffer.
* PARAMETERS :
* @index : index of the buffer
* @frame# : Frame number from the framework
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int32_t QCamera3GrallocMemory::markFrameNumber(uint32_t index, uint32_t frameNumber)
{
Mutex::Autolock lock(mLock);
if (index >= MM_CAMERA_MAX_NUM_FRAMES) {
LOGE("Index out of bounds");
return BAD_INDEX;
}
if (index < mStartIdx) {
LOGE("buffer index %d less than starting index %d",
index, mStartIdx);
return BAD_INDEX;
}
if (0 == mMemInfo[index].handle) {
LOGE("Buffer at %d not registered", index);
return BAD_INDEX;
}
mCurrentFrameNumbers[index] = (int32_t)frameNumber;
return NO_ERROR;
}
/*===========================================================================
* FUNCTION : getFrameNumber
*
* DESCRIPTION: We use this to fetch the frameNumber for the request with which
* this buffer was given to HAL
*
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : int32_t frameNumber
* positive/zero -- success
* negative failure
*==========================================================================*/
int32_t QCamera3GrallocMemory::getFrameNumber(uint32_t index)
{
Mutex::Autolock lock(mLock);
if (index >= MM_CAMERA_MAX_NUM_FRAMES) {
LOGE("Index out of bounds");
return -1;
}
if (index < mStartIdx) {
LOGE("buffer index %d less than starting index %d",
index, mStartIdx);
return BAD_INDEX;
}
if (0 == mMemInfo[index].handle) {
LOGE("Buffer at %d not registered", index);
return -1;
}
return mCurrentFrameNumbers[index];
}
/*===========================================================================
* FUNCTION : getBufferIndex
*
* DESCRIPTION: We use this to fetch the buffer index for the request with
* a particular frame number
*
*
* PARAMETERS :
* @frameNumber : frame number of the buffer
*
* RETURN : int32_t buffer index
* negative failure
*==========================================================================*/
int32_t QCamera3GrallocMemory::getBufferIndex(uint32_t frameNumber)
{
for (uint32_t index = mStartIdx;
index < MM_CAMERA_MAX_NUM_FRAMES; index++) {
if (mMemInfo[index].handle &&
mCurrentFrameNumbers[index] == (int32_t)frameNumber)
return (int32_t)index;
}
return -1;
}
/*===========================================================================
* FUNCTION : cacheOps
*
* DESCRIPTION: ion related memory cache operations
*
* PARAMETERS :
* @index : index of the buffer
* @cmd : cache ops command
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3GrallocMemory::cacheOps(uint32_t index, unsigned int cmd)
{
if (index >= MM_CAMERA_MAX_NUM_FRAMES) {
LOGE("Index out of bounds");
return -1;
}
if (index < mStartIdx) {
LOGE("buffer index %d less than starting index %d",
index, mStartIdx);
return BAD_INDEX;
}
return cacheOpsInternal(index, cmd, mPtr[index]);
}
/*===========================================================================
* FUNCTION : getMatchBufIndex
*
* DESCRIPTION: query buffer index by object ptr
*
* PARAMETERS :
* @opaque : opaque ptr
*
* RETURN : buffer index if match found,
* -1 if failed
*==========================================================================*/
int QCamera3GrallocMemory::getMatchBufIndex(void *object)
{
Mutex::Autolock lock(mLock);
int index = -1;
buffer_handle_t *key = (buffer_handle_t*) object;
if (!key) {
return BAD_VALUE;
}
for (uint32_t i = mStartIdx; i < MM_CAMERA_MAX_NUM_FRAMES; i++) {
if (mBufferHandle[i] == key) {
index = (int)i;
break;
}
}
return index;
}
/*===========================================================================
* FUNCTION : getFreeIndexLocked
*
* DESCRIPTION: Find free index slot. Note 'mLock' needs to be acquired
* before calling this method.
*
* PARAMETERS : None
*
* RETURN : free buffer index if found,
* -1 if failed
*==========================================================================*/
int QCamera3GrallocMemory::getFreeIndexLocked()
{
int index = -1;
if (mBufferCount >= (MM_CAMERA_MAX_NUM_FRAMES - 1)) {
LOGE("Number of buffers %d greater than what's supported %d",
mBufferCount, MM_CAMERA_MAX_NUM_FRAMES);
return index;
}
for (size_t i = mStartIdx; i < MM_CAMERA_MAX_NUM_FRAMES; i++) {
if (0 == mMemInfo[i].handle) {
index = i;
break;
}
}
return index;
}
/*===========================================================================
* FUNCTION : getPtrLocked
*
* DESCRIPTION: Return buffer pointer. Please note 'mLock' must be acquired
* before calling this method.
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer ptr
*==========================================================================*/
void *QCamera3GrallocMemory::getPtrLocked(uint32_t index)
{
if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
LOGE("index %d out of bound [0, %d)",
index, MM_CAMERA_MAX_NUM_FRAMES);
return NULL;
}
if (index < mStartIdx) {
LOGE("buffer index %d less than starting index %d",
index, mStartIdx);
return NULL;
}
if (0 == mMemInfo[index].handle) {
LOGE("Buffer at %d not registered", index);
return NULL;
}
return mPtr[index];
}
/*===========================================================================
* FUNCTION : getPtr
*
* DESCRIPTION: Return buffer pointer.
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer ptr
*==========================================================================*/
void *QCamera3GrallocMemory::getPtr(uint32_t index)
{
Mutex::Autolock lock(mLock);
return getPtrLocked(index);
}
/*===========================================================================
* FUNCTION : getBufferHandle
*
* DESCRIPTION: return framework pointer
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer ptr if match found
NULL if failed
*==========================================================================*/
void *QCamera3GrallocMemory::getBufferHandle(uint32_t index)
{
Mutex::Autolock lock(mLock);
if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
LOGE("index %d out of bound [0, %d)",
index, MM_CAMERA_MAX_NUM_FRAMES);
return NULL;
}
if (index < mStartIdx) {
LOGE("buffer index %d less than starting index %d",
index, mStartIdx);
return NULL;
}
if (0 == mMemInfo[index].handle) {
LOGE("Buffer at %d not registered", index);
return NULL;
}
return mBufferHandle[index];
}
}; //namespace qcamera
|