QCamera3Mem.cpp revision 625515beb9c1347216a2d261930ceb0d85ba1c47
1/* Copyright (c) 2012-2013, The Linux Foundataion. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#define LOG_TAG "QCameraHWI_Mem"
31
32#include <string.h>
33#include <fcntl.h>
34#include <sys/mman.h>
35#include <utils/Log.h>
36#include <utils/Errors.h>
37#include <gralloc_priv.h>
38#include "QCamera3Mem.h"
39
40extern "C" {
41#include <mm_camera_interface.h>
42}
43
44using namespace android;
45
46namespace qcamera {
47
48// QCaemra2Memory base class
49
50/*===========================================================================
51 * FUNCTION   : QCamera3Memory
52 *
53 * DESCRIPTION: default constructor of QCamera3Memory
54 *
55 * PARAMETERS : none
56 *
57 * RETURN     : None
58 *==========================================================================*/
59QCamera3Memory::QCamera3Memory()
60{
61    mBufferCount = 0;
62    for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i++) {
63        mMemInfo[i].fd = 0;
64        mMemInfo[i].main_ion_fd = 0;
65        mMemInfo[i].handle = NULL;
66        mMemInfo[i].size = 0;
67    }
68}
69
70/*===========================================================================
71 * FUNCTION   : ~QCamera3Memory
72 *
73 * DESCRIPTION: deconstructor of QCamera3Memory
74 *
75 * PARAMETERS : none
76 *
77 * RETURN     : None
78 *==========================================================================*/
79QCamera3Memory::~QCamera3Memory()
80{
81}
82
83/*===========================================================================
84 * FUNCTION   : cacheOpsInternal
85 *
86 * DESCRIPTION: ion related memory cache operations
87 *
88 * PARAMETERS :
89 *   @index   : index of the buffer
90 *   @cmd     : cache ops command
91 *   @vaddr   : ptr to the virtual address
92 *
93 * RETURN     : int32_t type of status
94 *              NO_ERROR  -- success
95 *              none-zero failure code
96 *==========================================================================*/
97int QCamera3Memory::cacheOpsInternal(int index, unsigned int cmd, void *vaddr)
98{
99    struct ion_flush_data cache_inv_data;
100    int ret = OK;
101
102    if (index >= mBufferCount) {
103        ALOGE("%s: index %d out of bound [0, %d)", __func__, index, mBufferCount);
104        return BAD_INDEX;
105    }
106
107    memset(&cache_inv_data, 0, sizeof(cache_inv_data));
108    cache_inv_data.vaddr = vaddr;
109    cache_inv_data.fd = mMemInfo[index].fd;
110    cache_inv_data.handle = mMemInfo[index].handle;
111    cache_inv_data.length = mMemInfo[index].size;
112
113    ALOGD("%s: addr = %p, fd = %d, handle = %p length = %d, ION Fd = %d",
114         __func__, cache_inv_data.vaddr, cache_inv_data.fd,
115         cache_inv_data.handle, cache_inv_data.length,
116         mMemInfo[index].main_ion_fd);
117    ret = ioctl(mMemInfo[index].main_ion_fd, cmd, &cache_inv_data);
118    if (ret < 0)
119        ALOGE("%s: Cache Invalidate failed: %s\n", __func__, strerror(errno));
120
121    return ret;
122}
123
124/*===========================================================================
125 * FUNCTION   : getFd
126 *
127 * DESCRIPTION: return file descriptor of the indexed buffer
128 *
129 * PARAMETERS :
130 *   @index   : index of the buffer
131 *
132 * RETURN     : file descriptor
133 *==========================================================================*/
134int QCamera3Memory::getFd(int index) const
135{
136    if (index >= mBufferCount)
137        return BAD_INDEX;
138
139    return mMemInfo[index].fd;
140}
141
142/*===========================================================================
143 * FUNCTION   : getSize
144 *
145 * DESCRIPTION: return buffer size of the indexed buffer
146 *
147 * PARAMETERS :
148 *   @index   : index of the buffer
149 *
150 * RETURN     : buffer size
151 *==========================================================================*/
152int QCamera3Memory::getSize(int index) const
153{
154    if (index >= mBufferCount)
155        return BAD_INDEX;
156
157    return (int)mMemInfo[index].size;
158}
159
160/*===========================================================================
161 * FUNCTION   : getCnt
162 *
163 * DESCRIPTION: query number of buffers allocated
164 *
165 * PARAMETERS : none
166 *
167 * RETURN     : number of buffers allocated
168 *==========================================================================*/
169int QCamera3Memory::getCnt() const
170{
171    return mBufferCount;
172}
173
174/*===========================================================================
175 * FUNCTION   : getBufDef
176 *
177 * DESCRIPTION: query detailed buffer information
178 *
179 * PARAMETERS :
180 *   @offset  : [input] frame buffer offset
181 *   @bufDef  : [output] reference to struct to store buffer definition
182 *   @index   : [input] index of the buffer
183 *
184 * RETURN     : none
185 *==========================================================================*/
186void QCamera3Memory::getBufDef(const cam_frame_len_offset_t &offset,
187        mm_camera_buf_def_t &bufDef, int index) const
188{
189    if (!mBufferCount) {
190        ALOGE("Memory not allocated");
191        return;
192    }
193    bufDef.fd = mMemInfo[index].fd;
194    bufDef.frame_len = mMemInfo[index].size;
195    bufDef.mem_info = (void *)this;
196    bufDef.num_planes = offset.num_planes;
197    bufDef.buffer = getPtr(index);
198    bufDef.buf_idx = index;
199
200    /* Plane 0 needs to be set separately. Set other planes in a loop */
201    bufDef.planes[0].length = offset.mp[0].len;
202    bufDef.planes[0].m.userptr = mMemInfo[index].fd;
203    bufDef.planes[0].data_offset = offset.mp[0].offset;
204    bufDef.planes[0].reserved[0] = 0;
205    for (int i = 1; i < bufDef.num_planes; i++) {
206         bufDef.planes[i].length = offset.mp[i].len;
207         bufDef.planes[i].m.userptr = mMemInfo[i].fd;
208         bufDef.planes[i].data_offset = offset.mp[i].offset;
209         bufDef.planes[i].reserved[0] =
210                 bufDef.planes[i-1].reserved[0] +
211                 bufDef.planes[i-1].length;
212    }
213}
214
215/*===========================================================================
216 * FUNCTION   : QCamera3HeapMemory
217 *
218 * DESCRIPTION: constructor of QCamera3HeapMemory for ion memory used internally in HAL
219 *
220 * PARAMETERS : none
221 *
222 * RETURN     : none
223 *==========================================================================*/
224QCamera3HeapMemory::QCamera3HeapMemory()
225    : QCamera3Memory()
226{
227    for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i ++)
228        mPtr[i] = NULL;
229}
230
231/*===========================================================================
232 * FUNCTION   : ~QCamera3HeapMemory
233 *
234 * DESCRIPTION: deconstructor of QCamera3HeapMemory
235 *
236 * PARAMETERS : none
237 *
238 * RETURN     : none
239 *==========================================================================*/
240QCamera3HeapMemory::~QCamera3HeapMemory()
241{
242}
243
244/*===========================================================================
245 * FUNCTION   : alloc
246 *
247 * DESCRIPTION: allocate requested number of buffers of certain size
248 *
249 * PARAMETERS :
250 *   @count   : number of buffers to be allocated
251 *   @size    : lenght of the buffer to be allocated
252 *   @heap_id : heap id to indicate where the buffers will be allocated from
253 *
254 * RETURN     : int32_t type of status
255 *              NO_ERROR  -- success
256 *              none-zero failure code
257 *==========================================================================*/
258int QCamera3HeapMemory::alloc(int count, int size, int heap_id)
259{
260    int rc = OK;
261    if (count > MM_CAMERA_MAX_NUM_FRAMES) {
262        ALOGE("Buffer count %d out of bound. Max is %d", count, MM_CAMERA_MAX_NUM_FRAMES);
263        return BAD_INDEX;
264    }
265    if (mBufferCount) {
266        ALOGE("Allocating a already allocated heap memory");
267        return INVALID_OPERATION;
268    }
269
270    for (int i = 0; i < count; i ++) {
271        rc = allocOneBuffer(mMemInfo[i], heap_id, size);
272        if (rc < 0) {
273            ALOGE("AllocateIonMemory failed");
274            for (int j = i-1; j >= 0; j--)
275                deallocOneBuffer(mMemInfo[j]);
276            break;
277        }
278    }
279    return rc;
280}
281
282/*===========================================================================
283 * FUNCTION   : dealloc
284 *
285 * DESCRIPTION: deallocate buffers
286 *
287 * PARAMETERS : none
288 *
289 * RETURN     : none
290 *==========================================================================*/
291void QCamera3HeapMemory::dealloc()
292{
293    for (int i = 0; i < mBufferCount; i++)
294        deallocOneBuffer(mMemInfo[i]);
295}
296
297/*===========================================================================
298 * FUNCTION   : allocOneBuffer
299 *
300 * DESCRIPTION: impl of allocating one buffers of certain size
301 *
302 * PARAMETERS :
303 *   @memInfo : [output] reference to struct to store additional memory allocation info
304 *   @heap    : [input] heap id to indicate where the buffers will be allocated from
305 *   @size    : [input] lenght of the buffer to be allocated
306 *
307 * RETURN     : int32_t type of status
308 *              NO_ERROR  -- success
309 *              none-zero failure code
310 *==========================================================================*/
311int QCamera3HeapMemory::allocOneBuffer(QCamera3MemInfo &memInfo, int heap_id, int size)
312{
313    int rc = OK;
314    struct ion_handle_data handle_data;
315    struct ion_allocation_data alloc;
316    struct ion_fd_data ion_info_fd;
317    int main_ion_fd = 0;
318
319    main_ion_fd = open("/dev/ion", O_RDONLY);
320    if (main_ion_fd <= 0) {
321        ALOGE("Ion dev open failed: %s\n", strerror(errno));
322        goto ION_OPEN_FAILED;
323    }
324
325    memset(&alloc, 0, sizeof(alloc));
326    alloc.len = size;
327    /* to make it page size aligned */
328    alloc.len = (alloc.len + 4095) & (~4095);
329    alloc.align = 4096;
330    alloc.flags = heap_id;
331    rc = ioctl(main_ion_fd, ION_IOC_ALLOC, &alloc);
332    if (rc < 0) {
333        ALOGE("ION allocation for len %d failed: %s\n", alloc.len,
334            strerror(errno));
335        goto ION_ALLOC_FAILED;
336    }
337
338    memset(&ion_info_fd, 0, sizeof(ion_info_fd));
339    ion_info_fd.handle = alloc.handle;
340    rc = ioctl(main_ion_fd, ION_IOC_SHARE, &ion_info_fd);
341    if (rc < 0) {
342        ALOGE("ION map failed %s\n", strerror(errno));
343        goto ION_MAP_FAILED;
344    }
345
346    memInfo.main_ion_fd = main_ion_fd;
347    memInfo.fd = ion_info_fd.fd;
348    memInfo.handle = ion_info_fd.handle;
349    memInfo.size = alloc.len;
350    return OK;
351
352ION_MAP_FAILED:
353    memset(&handle_data, 0, sizeof(handle_data));
354    handle_data.handle = ion_info_fd.handle;
355    ioctl(main_ion_fd, ION_IOC_FREE, &handle_data);
356ION_ALLOC_FAILED:
357    close(main_ion_fd);
358ION_OPEN_FAILED:
359    return NO_MEMORY;
360}
361
362/*===========================================================================
363 * FUNCTION   : deallocOneBuffer
364 *
365 * DESCRIPTION: impl of deallocating one buffers
366 *
367 * PARAMETERS :
368 *   @memInfo : reference to struct that stores additional memory allocation info
369 *
370 * RETURN     : none
371 *==========================================================================*/
372void QCamera3HeapMemory::deallocOneBuffer(QCamera3MemInfo &memInfo)
373{
374    struct ion_handle_data handle_data;
375
376    if (memInfo.fd > 0) {
377        close(memInfo.fd);
378        memInfo.fd = 0;
379    }
380
381    if (memInfo.main_ion_fd > 0) {
382        memset(&handle_data, 0, sizeof(handle_data));
383        handle_data.handle = memInfo.handle;
384        ioctl(memInfo.main_ion_fd, ION_IOC_FREE, &handle_data);
385        close(memInfo.main_ion_fd);
386        memInfo.main_ion_fd = 0;
387    }
388    memInfo.handle = NULL;
389    memInfo.size = 0;
390}
391
392/*===========================================================================
393 * FUNCTION   : getPtr
394 *
395 * DESCRIPTION: return buffer pointer
396 *
397 * PARAMETERS :
398 *   @index   : index of the buffer
399 *
400 * RETURN     : buffer ptr
401 *==========================================================================*/
402void *QCamera3HeapMemory::getPtr(int index) const
403{
404    if (index >= mBufferCount) {
405        ALOGE("index out of bound");
406        return (void *)BAD_INDEX;
407    }
408    return mPtr[index];
409}
410
411/*===========================================================================
412 * FUNCTION   : allocate
413 *
414 * DESCRIPTION: allocate requested number of buffers of certain size
415 *
416 * PARAMETERS :
417 *   @count   : number of buffers to be allocated
418 *   @size    : lenght of the buffer to be allocated
419 *   @queueAll: whether to queue all allocated buffers at the beginning
420 *
421 * RETURN     : int32_t type of status
422 *              NO_ERROR  -- success
423 *              none-zero failure code
424 *==========================================================================*/
425int QCamera3HeapMemory::allocate(int count, int size, bool queueAll)
426{
427    int heap_mask = 0x1 << ION_IOMMU_HEAP_ID;
428    int rc = alloc(count, size, heap_mask);
429    if (rc < 0)
430        return rc;
431
432    for (int i = 0; i < count; i ++) {
433        void *vaddr = mmap(NULL,
434                    mMemInfo[i].size,
435                    PROT_READ | PROT_WRITE,
436                    MAP_SHARED,
437                    mMemInfo[i].fd, 0);
438        if (vaddr == MAP_FAILED) {
439            for (int j = i-1; j >= 0; j --) {
440                munmap(mPtr[i], mMemInfo[i].size);
441                rc = NO_MEMORY;
442                break;
443            }
444        } else
445            mPtr[i] = vaddr;
446    }
447    if (rc == 0)
448        mBufferCount = count;
449
450    mQueueAll = queueAll;
451    return OK;
452}
453
454/*===========================================================================
455 * FUNCTION   : deallocate
456 *
457 * DESCRIPTION: deallocate buffers
458 *
459 * PARAMETERS : none
460 *
461 * RETURN     : none
462 *==========================================================================*/
463void QCamera3HeapMemory::deallocate()
464{
465    for (int i = 0; i < mBufferCount; i++) {
466        munmap(mPtr[i], mMemInfo[i].size);
467        mPtr[i] = NULL;
468    }
469    dealloc();
470    mBufferCount = 0;
471}
472
473/*===========================================================================
474 * FUNCTION   : cacheOps
475 *
476 * DESCRIPTION: ion related memory cache operations
477 *
478 * PARAMETERS :
479 *   @index   : index of the buffer
480 *   @cmd     : cache ops command
481 *
482 * RETURN     : int32_t type of status
483 *              NO_ERROR  -- success
484 *              none-zero failure code
485 *==========================================================================*/
486int QCamera3HeapMemory::cacheOps(int index, unsigned int cmd)
487{
488    if (index >= mBufferCount)
489        return BAD_INDEX;
490    return cacheOpsInternal(index, cmd, mPtr[index]);
491}
492
493/*===========================================================================
494 * FUNCTION   : getRegFlags
495 *
496 * DESCRIPTION: query initial reg flags
497 *
498 * PARAMETERS :
499 *   @regFlags: initial reg flags of the allocated buffers
500 *
501 * RETURN     : int32_t type of status
502 *              NO_ERROR  -- success
503 *              none-zero failure code
504 *==========================================================================*/
505int QCamera3HeapMemory::getRegFlags(uint8_t * regFlags) const
506{
507    int i;
508    for (i = 0; i < mBufferCount; i ++)
509        regFlags[i] = (mQueueAll ? 1 : 0);
510    return NO_ERROR;
511}
512
513/*===========================================================================
514 * FUNCTION   : getMatchBufIndex
515 *
516 * DESCRIPTION: query buffer index by opaque ptr
517 *
518 * PARAMETERS :
519 *   @opaque  : opaque ptr
520 *   @metadata: flag if it's metadata
521 *
522 * RETURN     : buffer index if match found,
523 *              -1 if failed
524 *==========================================================================*/
525int QCamera3HeapMemory::getMatchBufIndex(const void *opaque,
526                                        bool metadata) const
527{
528    int index = -1;
529    if (metadata) {
530        return -1;
531    }
532    for (int i = 0; i < mBufferCount; i++) {
533        if (mPtr[i] == opaque) {
534            index = i;
535            break;
536        }
537    }
538    return index;
539}
540
541/*===========================================================================
542 * FUNCTION   : QCamera3GrallocMemory
543 *
544 * DESCRIPTION: constructor of QCamera3GrallocMemory
545 *              preview stream buffers are allocated from gralloc native_windoe
546 *
547 * PARAMETERS :
548 *   @getMemory : camera memory request ops table
549 *
550 * RETURN     : none
551 *==========================================================================*/
552QCamera3GrallocMemory::QCamera3GrallocMemory()
553        : QCamera3Memory()
554{
555    for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i ++) {
556        mBufferHandle[i] = NULL;
557        mPrivateHandle[i] = NULL;
558    }
559}
560
561/*===========================================================================
562 * FUNCTION   : ~QCamera3GrallocMemory
563 *
564 * DESCRIPTION: deconstructor of QCamera3GrallocMemory
565 *
566 * PARAMETERS : none
567 *
568 * RETURN     : none
569 *==========================================================================*/
570QCamera3GrallocMemory::~QCamera3GrallocMemory()
571{
572}
573
574/*===========================================================================
575 * FUNCTION   : registerBuffers
576 *
577 * DESCRIPTION: register frameworks-allocated gralloc buffer_handle_t
578 *
579 * PARAMETERS :
580 *   @num_buffer : number of buffers to be registered
581 *   @buffers    : array of buffer_handle_t pointers
582 *
583 * RETURN     : int32_t type of status
584 *              NO_ERROR  -- success
585 *              none-zero failure code
586 *==========================================================================*/
587int QCamera3GrallocMemory::registerBuffers(uint32_t num_buffers, buffer_handle_t **buffers)
588{
589    status_t ret = NO_ERROR;
590    struct ion_fd_data ion_info_fd;
591    memset(&ion_info_fd, 0, sizeof(ion_info_fd));
592
593    ALOGI(" %s : E ", __FUNCTION__);
594
595    if (num_buffers > MM_CAMERA_MAX_NUM_FRAMES) {
596        ALOGE("%s: Number of buffers %d greater than what's supported %d",
597            __func__, num_buffers, MM_CAMERA_MAX_NUM_FRAMES);
598        return -EINVAL;
599    }
600
601    for (size_t cnt = 0; cnt < num_buffers; cnt++) {
602        if (buffers[cnt] == NULL) {
603            ALOGE("%s: Invalid buffers[%d].", __func__, cnt);
604            return -EINVAL;
605        }
606        mBufferHandle[cnt] = buffers[cnt];
607        mPrivateHandle[cnt] =
608            (struct private_handle_t *)(*mBufferHandle[cnt]);
609        mMemInfo[cnt].main_ion_fd = open("/dev/ion", O_RDONLY);
610        if (mMemInfo[cnt].main_ion_fd < 0) {
611            ALOGE("%s: failed: could not open ion device", __func__);
612            for(size_t i = 0; i < cnt; i++) {
613                struct ion_handle_data ion_handle;
614                memset(&ion_handle, 0, sizeof(ion_handle));
615                ion_handle.handle = mMemInfo[i].handle;
616                if (ioctl(mMemInfo[i].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
617                    ALOGE("%s: ion free failed", __func__);
618                }
619                close(mMemInfo[i].main_ion_fd);
620                ALOGD("%s: cancel_buffer: hdl =%p", __func__, (*mBufferHandle[i]));
621                mBufferHandle[i] = NULL;
622            }
623            memset(&mMemInfo, 0, sizeof(mMemInfo));
624            ret = -ENOMEM;
625            goto end;
626        } else {
627            ion_info_fd.fd = mPrivateHandle[cnt]->fd;
628            if (ioctl(mMemInfo[cnt].main_ion_fd,
629                      ION_IOC_IMPORT, &ion_info_fd) < 0) {
630                ALOGE("%s: ION import failed\n", __func__);
631                for(size_t i = 0; i < cnt; i++) {
632                    struct ion_handle_data ion_handle;
633                    memset(&ion_handle, 0, sizeof(ion_handle));
634                    ion_handle.handle = mMemInfo[i].handle;
635                    if (ioctl(mMemInfo[i].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
636                        ALOGE("ion free failed");
637                    }
638                    close(mMemInfo[i].main_ion_fd);
639                    mBufferHandle[i] = NULL;
640                }
641                close(mMemInfo[cnt].main_ion_fd);
642                memset(&mMemInfo, 0, sizeof(mMemInfo));
643                ret = -ENOMEM;
644                goto end;
645            }
646        }
647        ALOGD("%s: idx = %d, fd = %d, size = %d, offset = %d",
648              __func__, cnt, mPrivateHandle[cnt]->fd,
649              mPrivateHandle[cnt]->size,
650              mPrivateHandle[cnt]->offset);
651        mMemInfo[cnt].fd =
652            mPrivateHandle[cnt]->fd;
653        mMemInfo[cnt].size =
654            mPrivateHandle[cnt]->size;
655        mMemInfo[cnt].handle = ion_info_fd.handle;
656
657        void *vaddr = mmap(NULL,
658                    mMemInfo[cnt].size,
659                    PROT_READ | PROT_WRITE,
660                    MAP_SHARED,
661                    mMemInfo[cnt].fd, 0);
662        if (vaddr == MAP_FAILED) {
663            for (int j = cnt-1; j >= 0; j --) {
664                munmap(mPtr[cnt], mMemInfo[cnt].size);
665                ret = -ENOMEM;
666                break;
667            }
668        } else
669            mPtr[cnt] = vaddr;
670    }
671    mBufferCount = num_buffers;
672
673end:
674    ALOGI(" %s : X ",__func__);
675    return ret;
676}
677
678/*===========================================================================
679 * FUNCTION   : unregisterBuffers
680 *
681 * DESCRIPTION: unregister buffers
682 *
683 * PARAMETERS : none
684 *
685 * RETURN     : none
686 *==========================================================================*/
687void QCamera3GrallocMemory::unregisterBuffers()
688{
689    ALOGI("%s: E ", __FUNCTION__);
690
691    for (int cnt = 0; cnt < mBufferCount; cnt++) {
692        munmap(mPtr[cnt], mMemInfo[cnt].size);
693        mPtr[cnt] = NULL;
694
695        struct ion_handle_data ion_handle;
696        memset(&ion_handle, 0, sizeof(ion_handle));
697        ion_handle.handle = mMemInfo[cnt].handle;
698        if (ioctl(mMemInfo[cnt].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
699            ALOGE("ion free failed");
700        }
701        close(mMemInfo[cnt].main_ion_fd);
702        ALOGD("put buffer %d successfully", cnt);
703    }
704    mBufferCount = 0;
705    ALOGI(" %s : X ",__FUNCTION__);
706}
707
708/*===========================================================================
709 * FUNCTION   : cacheOps
710 *
711 * DESCRIPTION: ion related memory cache operations
712 *
713 * PARAMETERS :
714 *   @index   : index of the buffer
715 *   @cmd     : cache ops command
716 *
717 * RETURN     : int32_t type of status
718 *              NO_ERROR  -- success
719 *              none-zero failure code
720 *==========================================================================*/
721int QCamera3GrallocMemory::cacheOps(int index, unsigned int cmd)
722{
723    if (index >= mBufferCount)
724        return BAD_INDEX;
725    return cacheOpsInternal(index, cmd, mPtr[index]);
726}
727
728/*===========================================================================
729 * FUNCTION   : getRegFlags
730 *
731 * DESCRIPTION: query initial reg flags
732 *
733 * PARAMETERS :
734 *   @regFlags: initial reg flags of the allocated buffers
735 *
736 * RETURN     : int32_t type of status
737 *              NO_ERROR  -- success
738 *              none-zero failure code
739 *==========================================================================*/
740int QCamera3GrallocMemory::getRegFlags(uint8_t *regFlags) const
741{
742    int i;
743    for (i = 0; i < mBufferCount; i ++)
744        regFlags[i] = 0;
745    return NO_ERROR;
746}
747
748/*===========================================================================
749 * FUNCTION   : getMatchBufIndex
750 *
751 * DESCRIPTION: query buffer index by opaque ptr
752 *
753 * PARAMETERS :
754 *   @opaque  : opaque ptr
755 *   @metadata: flag if it's metadata
756 *
757 * RETURN     : buffer index if match found,
758 *              -1 if failed
759 *==========================================================================*/
760int QCamera3GrallocMemory::getMatchBufIndex(const void *opaque,
761                                           bool metadata) const
762{
763    int index = -1;
764    if (metadata) {
765        return -1;
766    }
767    for (int i = 0; i < mBufferCount; i++) {
768        if (mPtr[i] == opaque) {
769            index = i;
770            break;
771        }
772    }
773    return index;
774}
775
776/*===========================================================================
777 * FUNCTION   : getPtr
778 *
779 * DESCRIPTION: return buffer pointer
780 *
781 * PARAMETERS :
782 *   @index   : index of the buffer
783 *
784 * RETURN     : buffer ptr
785 *==========================================================================*/
786void *QCamera3GrallocMemory::getPtr(int index) const
787{
788    if (index >= mBufferCount) {
789        ALOGE("index out of bound");
790        return (void *)BAD_INDEX;
791    }
792    return mPtr[index];
793}
794
795}; //namespace qcamera
796