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
|
/* Copyright (c) 2012-2013, The Linux Foundataion. 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 "QCamera3HWI_Mem"
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <utils/Log.h>
#include <utils/Errors.h>
#include <gralloc_priv.h>
#include "QCamera3Mem.h"
extern "C" {
#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 = 0;
mMemInfo[i].main_ion_fd = 0;
mMemInfo[i].handle = NULL;
mMemInfo[i].size = 0;
}
}
/*===========================================================================
* 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(int index, unsigned int cmd, void *vaddr)
{
struct ion_flush_data cache_inv_data;
struct ion_custom_data custom_data;
int ret = OK;
if (index >= mBufferCount) {
ALOGE("%s: index %d out of bound [0, %d)", __func__, index, mBufferCount);
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 = mMemInfo[index].size;
custom_data.cmd = cmd;
custom_data.arg = (unsigned long)&cache_inv_data;
ALOGV("%s: addr = %p, fd = %d, handle = %p length = %d, ION Fd = %d",
__func__, cache_inv_data.vaddr, cache_inv_data.fd,
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)
ALOGE("%s: Cache Invalidate failed: %s\n", __func__, 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(int index) const
{
if (index >= mBufferCount)
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
*==========================================================================*/
int QCamera3Memory::getSize(int index) const
{
if (index >= mBufferCount)
return BAD_INDEX;
return (int)mMemInfo[index].size;
}
/*===========================================================================
* FUNCTION : getCnt
*
* DESCRIPTION: query number of buffers allocated
*
* PARAMETERS : none
*
* RETURN : number of buffers allocated
*==========================================================================*/
int QCamera3Memory::getCnt() const
{
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, int index) const
{
if (!mBufferCount) {
ALOGE("Memory not allocated");
return NO_INIT;
}
bufDef.fd = mMemInfo[index].fd;
bufDef.frame_len = mMemInfo[index].size;
bufDef.mem_info = (void *)this;
bufDef.num_planes = offset.num_planes;
bufDef.buffer = getPtr(index);
bufDef.buf_idx = index;
/* Plane 0 needs to be set separately. Set other planes in a loop */
bufDef.planes[0].length = offset.mp[0].len;
bufDef.planes[0].m.userptr = mMemInfo[index].fd;
bufDef.planes[0].data_offset = offset.mp[0].offset;
bufDef.planes[0].reserved[0] = 0;
for (int i = 1; i < bufDef.num_planes; i++) {
bufDef.planes[i].length = offset.mp[i].len;
bufDef.planes[i].m.userptr = mMemInfo[i].fd;
bufDef.planes[i].data_offset = offset.mp[i].offset;
bufDef.planes[i].reserved[0] =
bufDef.planes[i-1].reserved[0] +
bufDef.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()
: QCamera3Memory()
{
for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i ++)
mPtr[i] = NULL;
}
/*===========================================================================
* FUNCTION : ~QCamera3HeapMemory
*
* DESCRIPTION: deconstructor of QCamera3HeapMemory
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
QCamera3HeapMemory::~QCamera3HeapMemory()
{
}
/*===========================================================================
* FUNCTION : alloc
*
* DESCRIPTION: allocate requested number of buffers of certain size
*
* PARAMETERS :
* @count : number of buffers to be allocated
* @size : lenght of the buffer to be allocated
* @heap_id : heap id to indicate where the buffers will be allocated from
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3HeapMemory::alloc(int count, int size, int heap_id)
{
int rc = OK;
if (count > MM_CAMERA_MAX_NUM_FRAMES) {
ALOGE("Buffer count %d out of bound. Max is %d", count, MM_CAMERA_MAX_NUM_FRAMES);
return BAD_INDEX;
}
if (mBufferCount) {
ALOGE("Allocating a already allocated heap memory");
return INVALID_OPERATION;
}
for (int i = 0; i < count; i ++) {
rc = allocOneBuffer(mMemInfo[i], heap_id, size);
if (rc < 0) {
ALOGE("AllocateIonMemory failed");
for (int j = i-1; j >= 0; j--)
deallocOneBuffer(mMemInfo[j]);
break;
}
}
return rc;
}
/*===========================================================================
* FUNCTION : dealloc
*
* DESCRIPTION: deallocate buffers
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
void QCamera3HeapMemory::dealloc()
{
for (int i = 0; i < mBufferCount; i++)
deallocOneBuffer(mMemInfo[i]);
}
/*===========================================================================
* 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, int heap_id, int size)
{
int rc = OK;
struct ion_handle_data handle_data;
struct ion_allocation_data alloc;
struct ion_fd_data ion_info_fd;
int main_ion_fd = 0;
main_ion_fd = open("/dev/ion", O_RDONLY);
if (main_ion_fd <= 0) {
ALOGE("Ion dev open failed: %s\n", strerror(errno));
goto ION_OPEN_FAILED;
}
memset(&alloc, 0, sizeof(alloc));
alloc.len = size;
/* to make it page size aligned */
alloc.len = (alloc.len + 4095) & (~4095);
alloc.align = 4096;
alloc.flags = ION_FLAG_CACHED;
alloc.heap_id_mask = heap_id;
rc = ioctl(main_ion_fd, ION_IOC_ALLOC, &alloc);
if (rc < 0) {
ALOGE("ION allocation for len %d failed: %s\n", alloc.len,
strerror(errno));
goto ION_ALLOC_FAILED;
}
memset(&ion_info_fd, 0, sizeof(ion_info_fd));
ion_info_fd.handle = alloc.handle;
rc = ioctl(main_ion_fd, ION_IOC_SHARE, &ion_info_fd);
if (rc < 0) {
ALOGE("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 = alloc.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 = 0;
}
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 = 0;
}
memInfo.handle = NULL;
memInfo.size = 0;
}
/*===========================================================================
* FUNCTION : getPtr
*
* DESCRIPTION: return buffer pointer
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer ptr
*==========================================================================*/
void *QCamera3HeapMemory::getPtr(int index) const
{
if (index >= mBufferCount) {
ALOGE("index out of bound");
return (void *)BAD_INDEX;
}
return mPtr[index];
}
/*===========================================================================
* FUNCTION : allocate
*
* DESCRIPTION: allocate requested number of buffers of certain size
*
* PARAMETERS :
* @count : number of buffers to be allocated
* @size : lenght of the buffer to be allocated
* @queueAll: whether to queue all allocated buffers at the beginning
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3HeapMemory::allocate(int count, int size, bool queueAll)
{
int heap_id_mask = 0x1 << ION_IOMMU_HEAP_ID;
int rc = alloc(count, size, heap_id_mask);
if (rc < 0)
return rc;
for (int i = 0; i < count; i ++) {
void *vaddr = mmap(NULL,
mMemInfo[i].size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
mMemInfo[i].fd, 0);
if (vaddr == MAP_FAILED) {
for (int j = i-1; j >= 0; j --) {
munmap(mPtr[i], mMemInfo[i].size);
rc = NO_MEMORY;
break;
}
} else
mPtr[i] = vaddr;
}
if (rc == 0)
mBufferCount = count;
mQueueAll = queueAll;
return OK;
}
/*===========================================================================
* FUNCTION : deallocate
*
* DESCRIPTION: deallocate buffers
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
void QCamera3HeapMemory::deallocate()
{
for (int i = 0; i < mBufferCount; i++) {
munmap(mPtr[i], mMemInfo[i].size);
mPtr[i] = NULL;
}
dealloc();
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(int index, unsigned int cmd)
{
if (index >= mBufferCount)
return BAD_INDEX;
return cacheOpsInternal(index, cmd, mPtr[index]);
}
/*===========================================================================
* FUNCTION : getRegFlags
*
* DESCRIPTION: query initial reg flags
*
* PARAMETERS :
* @regFlags: initial reg flags of the allocated buffers
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3HeapMemory::getRegFlags(uint8_t * regFlags) const
{
int i;
for (i = 0; i < mBufferCount; i ++)
regFlags[i] = (mQueueAll ? 1 : 0);
return NO_ERROR;
}
/*===========================================================================
* 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;
*/
ALOGE("%s: FATAL: Not supposed to come here", __func__);
return -1;
}
/*===========================================================================
* FUNCTION : QCamera3GrallocMemory
*
* DESCRIPTION: constructor of QCamera3GrallocMemory
* preview stream buffers are allocated from gralloc native_windoe
*
* PARAMETERS :
* @getMemory : camera memory request ops table
*
* RETURN : none
*==========================================================================*/
QCamera3GrallocMemory::QCamera3GrallocMemory()
: QCamera3Memory()
{
for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i ++) {
mBufferHandle[i] = NULL;
mPrivateHandle[i] = NULL;
mCurrentFrameNumbers[i] = -1;
}
}
/*===========================================================================
* 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
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3GrallocMemory::registerBuffer(buffer_handle_t *buffer)
{
status_t ret = NO_ERROR;
struct ion_fd_data ion_info_fd;
void *vaddr = NULL;
ALOGV(" %s : E ", __FUNCTION__);
memset(&ion_info_fd, 0, sizeof(ion_info_fd));
if (mBufferCount >= (MM_CAMERA_MAX_NUM_FRAMES - 1)) {
ALOGE("%s: Number of buffers %d greater than what's supported %d",
__func__, mBufferCount, MM_CAMERA_MAX_NUM_FRAMES);
return -EINVAL;
}
if (0 <= getMatchBufIndex((void *) buffer)) {
ALOGV("%s: Buffer already registered", __func__);
return ALREADY_EXISTS;
}
mBufferHandle[mBufferCount] = buffer;
mPrivateHandle[mBufferCount] =
(struct private_handle_t *)(*mBufferHandle[mBufferCount]);
mMemInfo[mBufferCount].main_ion_fd = open("/dev/ion", O_RDONLY);
if (mMemInfo[mBufferCount].main_ion_fd < 0) {
ALOGE("%s: failed: could not open ion device", __func__);
ret = NO_MEMORY;
goto end;
} else {
ion_info_fd.fd = mPrivateHandle[mBufferCount]->fd;
if (ioctl(mMemInfo[mBufferCount].main_ion_fd,
ION_IOC_IMPORT, &ion_info_fd) < 0) {
ALOGE("%s: ION import failed\n", __func__);
close(mMemInfo[mBufferCount].main_ion_fd);
ret = NO_MEMORY;
goto end;
}
}
ALOGV("%s: idx = %d, fd = %d, size = %d, offset = %d",
__func__, mBufferCount, mPrivateHandle[mBufferCount]->fd,
mPrivateHandle[mBufferCount]->size,
mPrivateHandle[mBufferCount]->offset);
mMemInfo[mBufferCount].fd =
mPrivateHandle[mBufferCount]->fd;
mMemInfo[mBufferCount].size =
mPrivateHandle[mBufferCount]->size;
mMemInfo[mBufferCount].handle = ion_info_fd.handle;
vaddr = mmap(NULL,
mMemInfo[mBufferCount].size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
mMemInfo[mBufferCount].fd, 0);
if (vaddr == MAP_FAILED) {
ret = NO_MEMORY;
} else {
mPtr[mBufferCount] = vaddr;
mBufferCount++;
}
end:
ALOGV(" %s : X ",__func__);
return ret;
}
/*===========================================================================
* FUNCTION : unregisterBuffers
*
* DESCRIPTION: unregister buffers
*
* PARAMETERS : none
*
* RETURN : none
*==========================================================================*/
void QCamera3GrallocMemory::unregisterBuffers()
{
ALOGV("%s: E ", __FUNCTION__);
for (int cnt = 0; cnt < mBufferCount; cnt++) {
munmap(mPtr[cnt], mMemInfo[cnt].size);
mPtr[cnt] = NULL;
struct ion_handle_data ion_handle;
memset(&ion_handle, 0, sizeof(ion_handle));
ion_handle.handle = mMemInfo[cnt].handle;
if (ioctl(mMemInfo[cnt].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
ALOGE("ion free failed");
}
close(mMemInfo[cnt].main_ion_fd);
ALOGV("put buffer %d successfully", cnt);
}
mBufferCount = 0;
ALOGV(" %s : 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(int index, uint32_t frameNumber)
{
if(index >= mBufferCount || index >= MM_CAMERA_MAX_NUM_FRAMES) {
ALOGE("%s: Index out of bounds",__func__);
return BAD_INDEX;
}
mCurrentFrameNumbers[index] = 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
* postive/zero -- success
* negetive failure
*==========================================================================*/
int32_t QCamera3GrallocMemory::getFrameNumber(int index)
{
if(index >= mBufferCount || index >= MM_CAMERA_MAX_NUM_FRAMES) {
ALOGE("%s: Index out of bounds",__func__);
return -1;
}
return mCurrentFrameNumbers[index];
}
/*===========================================================================
* 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(int index, unsigned int cmd)
{
if (index >= mBufferCount)
return BAD_INDEX;
return cacheOpsInternal(index, cmd, mPtr[index]);
}
/*===========================================================================
* FUNCTION : getRegFlags
*
* DESCRIPTION: query initial reg flags
*
* PARAMETERS :
* @regFlags: initial reg flags of the allocated buffers
*
* RETURN : int32_t type of status
* NO_ERROR -- success
* none-zero failure code
*==========================================================================*/
int QCamera3GrallocMemory::getRegFlags(uint8_t *regFlags) const
{
int i;
for (i = 0; i < mBufferCount; i ++)
regFlags[i] = 0;
return NO_ERROR;
}
/*===========================================================================
* 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)
{
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;
}
/*===========================================================================
* FUNCTION : getPtr
*
* DESCRIPTION: return buffer pointer
*
* PARAMETERS :
* @index : index of the buffer
*
* RETURN : buffer ptr
*==========================================================================*/
void *QCamera3GrallocMemory::getPtr(int index) const
{
if (index >= mBufferCount) {
ALOGE("index out of bound");
return (void *)BAD_INDEX;
}
return mPtr[index];
}
}; //namespace qcamera
|