QCamera3Mem.cpp revision e6ab32d89cf169705236988f0f74309f914c88b7
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 = ION_FLAG_CACHED;
331    alloc.heap_mask = heap_id;
332    rc = ioctl(main_ion_fd, ION_IOC_ALLOC, &alloc);
333    if (rc < 0) {
334        ALOGE("ION allocation for len %d failed: %s\n", alloc.len,
335            strerror(errno));
336        goto ION_ALLOC_FAILED;
337    }
338
339    memset(&ion_info_fd, 0, sizeof(ion_info_fd));
340    ion_info_fd.handle = alloc.handle;
341    rc = ioctl(main_ion_fd, ION_IOC_SHARE, &ion_info_fd);
342    if (rc < 0) {
343        ALOGE("ION map failed %s\n", strerror(errno));
344        goto ION_MAP_FAILED;
345    }
346
347    memInfo.main_ion_fd = main_ion_fd;
348    memInfo.fd = ion_info_fd.fd;
349    memInfo.handle = ion_info_fd.handle;
350    memInfo.size = alloc.len;
351    return OK;
352
353ION_MAP_FAILED:
354    memset(&handle_data, 0, sizeof(handle_data));
355    handle_data.handle = ion_info_fd.handle;
356    ioctl(main_ion_fd, ION_IOC_FREE, &handle_data);
357ION_ALLOC_FAILED:
358    close(main_ion_fd);
359ION_OPEN_FAILED:
360    return NO_MEMORY;
361}
362
363/*===========================================================================
364 * FUNCTION   : deallocOneBuffer
365 *
366 * DESCRIPTION: impl of deallocating one buffers
367 *
368 * PARAMETERS :
369 *   @memInfo : reference to struct that stores additional memory allocation info
370 *
371 * RETURN     : none
372 *==========================================================================*/
373void QCamera3HeapMemory::deallocOneBuffer(QCamera3MemInfo &memInfo)
374{
375    struct ion_handle_data handle_data;
376
377    if (memInfo.fd > 0) {
378        close(memInfo.fd);
379        memInfo.fd = 0;
380    }
381
382    if (memInfo.main_ion_fd > 0) {
383        memset(&handle_data, 0, sizeof(handle_data));
384        handle_data.handle = memInfo.handle;
385        ioctl(memInfo.main_ion_fd, ION_IOC_FREE, &handle_data);
386        close(memInfo.main_ion_fd);
387        memInfo.main_ion_fd = 0;
388    }
389    memInfo.handle = NULL;
390    memInfo.size = 0;
391}
392
393/*===========================================================================
394 * FUNCTION   : getPtr
395 *
396 * DESCRIPTION: return buffer pointer
397 *
398 * PARAMETERS :
399 *   @index   : index of the buffer
400 *
401 * RETURN     : buffer ptr
402 *==========================================================================*/
403void *QCamera3HeapMemory::getPtr(int index) const
404{
405    if (index >= mBufferCount) {
406        ALOGE("index out of bound");
407        return (void *)BAD_INDEX;
408    }
409    return mPtr[index];
410}
411
412/*===========================================================================
413 * FUNCTION   : allocate
414 *
415 * DESCRIPTION: allocate requested number of buffers of certain size
416 *
417 * PARAMETERS :
418 *   @count   : number of buffers to be allocated
419 *   @size    : lenght of the buffer to be allocated
420 *   @queueAll: whether to queue all allocated buffers at the beginning
421 *
422 * RETURN     : int32_t type of status
423 *              NO_ERROR  -- success
424 *              none-zero failure code
425 *==========================================================================*/
426int QCamera3HeapMemory::allocate(int count, int size, bool queueAll)
427{
428    int heap_mask = 0x1 << ION_IOMMU_HEAP_ID;
429    int rc = alloc(count, size, heap_mask);
430    if (rc < 0)
431        return rc;
432
433    for (int i = 0; i < count; i ++) {
434        void *vaddr = mmap(NULL,
435                    mMemInfo[i].size,
436                    PROT_READ | PROT_WRITE,
437                    MAP_SHARED,
438                    mMemInfo[i].fd, 0);
439        if (vaddr == MAP_FAILED) {
440            for (int j = i-1; j >= 0; j --) {
441                munmap(mPtr[i], mMemInfo[i].size);
442                rc = NO_MEMORY;
443                break;
444            }
445        } else
446            mPtr[i] = vaddr;
447    }
448    if (rc == 0)
449        mBufferCount = count;
450
451    mQueueAll = queueAll;
452    return OK;
453}
454
455/*===========================================================================
456 * FUNCTION   : deallocate
457 *
458 * DESCRIPTION: deallocate buffers
459 *
460 * PARAMETERS : none
461 *
462 * RETURN     : none
463 *==========================================================================*/
464void QCamera3HeapMemory::deallocate()
465{
466    for (int i = 0; i < mBufferCount; i++) {
467        munmap(mPtr[i], mMemInfo[i].size);
468        mPtr[i] = NULL;
469    }
470    dealloc();
471    mBufferCount = 0;
472}
473
474/*===========================================================================
475 * FUNCTION   : cacheOps
476 *
477 * DESCRIPTION: ion related memory cache operations
478 *
479 * PARAMETERS :
480 *   @index   : index of the buffer
481 *   @cmd     : cache ops command
482 *
483 * RETURN     : int32_t type of status
484 *              NO_ERROR  -- success
485 *              none-zero failure code
486 *==========================================================================*/
487int QCamera3HeapMemory::cacheOps(int index, unsigned int cmd)
488{
489    if (index >= mBufferCount)
490        return BAD_INDEX;
491    return cacheOpsInternal(index, cmd, mPtr[index]);
492}
493
494/*===========================================================================
495 * FUNCTION   : getRegFlags
496 *
497 * DESCRIPTION: query initial reg flags
498 *
499 * PARAMETERS :
500 *   @regFlags: initial reg flags of the allocated buffers
501 *
502 * RETURN     : int32_t type of status
503 *              NO_ERROR  -- success
504 *              none-zero failure code
505 *==========================================================================*/
506int QCamera3HeapMemory::getRegFlags(uint8_t * regFlags) const
507{
508    int i;
509    for (i = 0; i < mBufferCount; i ++)
510        regFlags[i] = (mQueueAll ? 1 : 0);
511    return NO_ERROR;
512}
513
514/*===========================================================================
515 * FUNCTION   : getMatchBufIndex
516 *
517 * DESCRIPTION: query buffer index by object ptr
518 *
519 * PARAMETERS :
520 *   @object  : object ptr
521 *
522 * RETURN     : buffer index if match found,
523 *              -1 if failed
524 *==========================================================================*/
525int QCamera3HeapMemory::getMatchBufIndex(void * /*object*/)
526{
527
528/*
529    TODO for HEAP memory type, would there be an equivalent requirement?
530
531    int index = -1;
532    buffer_handle_t *key = (buffer_handle_t*) object;
533    if (!key) {
534        return BAD_VALUE;
535    }
536    for (int i = 0; i < mBufferCount; i++) {
537        if (mBufferHandle[i] == key) {
538            index = i;
539            break;
540        }
541    }
542    return index;
543*/
544    ALOGE("%s: FATAL: Not supposed to come here", __func__);
545    return -1;
546}
547
548/*===========================================================================
549 * FUNCTION   : QCamera3GrallocMemory
550 *
551 * DESCRIPTION: constructor of QCamera3GrallocMemory
552 *              preview stream buffers are allocated from gralloc native_windoe
553 *
554 * PARAMETERS :
555 *   @getMemory : camera memory request ops table
556 *
557 * RETURN     : none
558 *==========================================================================*/
559QCamera3GrallocMemory::QCamera3GrallocMemory()
560        : QCamera3Memory()
561{
562    for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i ++) {
563        mBufferHandle[i] = NULL;
564        mPrivateHandle[i] = NULL;
565        mCurrentFrameNumbers[i] = -1;
566    }
567}
568
569/*===========================================================================
570 * FUNCTION   : ~QCamera3GrallocMemory
571 *
572 * DESCRIPTION: deconstructor of QCamera3GrallocMemory
573 *
574 * PARAMETERS : none
575 *
576 * RETURN     : none
577 *==========================================================================*/
578QCamera3GrallocMemory::~QCamera3GrallocMemory()
579{
580}
581
582/*===========================================================================
583 * FUNCTION   : registerBuffers
584 *
585 * DESCRIPTION: register frameworks-allocated gralloc buffer_handle_t
586 *
587 * PARAMETERS :
588 *   @num_buffer : number of buffers to be registered
589 *   @buffers    : array of buffer_handle_t pointers
590 *
591 * RETURN     : int32_t type of status
592 *              NO_ERROR  -- success
593 *              none-zero failure code
594 *==========================================================================*/
595int QCamera3GrallocMemory::registerBuffers(uint32_t num_buffers, buffer_handle_t **buffers)
596{
597    status_t ret = NO_ERROR;
598    struct ion_fd_data ion_info_fd;
599    memset(&ion_info_fd, 0, sizeof(ion_info_fd));
600
601    ALOGI(" %s : E ", __FUNCTION__);
602
603    if (num_buffers > MM_CAMERA_MAX_NUM_FRAMES) {
604        ALOGE("%s: Number of buffers %d greater than what's supported %d",
605            __func__, num_buffers, MM_CAMERA_MAX_NUM_FRAMES);
606        return -EINVAL;
607    }
608
609    for (size_t cnt = 0; cnt < num_buffers; cnt++) {
610        if (buffers[cnt] == NULL) {
611            ALOGE("%s: Invalid buffers[%d].", __func__, cnt);
612            return -EINVAL;
613        }
614        mBufferHandle[cnt] = buffers[cnt];
615        mPrivateHandle[cnt] =
616            (struct private_handle_t *)(*mBufferHandle[cnt]);
617        mMemInfo[cnt].main_ion_fd = open("/dev/ion", O_RDONLY);
618        if (mMemInfo[cnt].main_ion_fd < 0) {
619            ALOGE("%s: failed: could not open ion device", __func__);
620            for(size_t i = 0; i < cnt; i++) {
621                struct ion_handle_data ion_handle;
622                memset(&ion_handle, 0, sizeof(ion_handle));
623                ion_handle.handle = mMemInfo[i].handle;
624                if (ioctl(mMemInfo[i].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
625                    ALOGE("%s: ion free failed", __func__);
626                }
627                close(mMemInfo[i].main_ion_fd);
628                ALOGD("%s: cancel_buffer: hdl =%p", __func__, (*mBufferHandle[i]));
629                mBufferHandle[i] = NULL;
630            }
631            memset(&mMemInfo, 0, sizeof(mMemInfo));
632            ret = -ENOMEM;
633            goto end;
634        } else {
635            ion_info_fd.fd = mPrivateHandle[cnt]->fd;
636            if (ioctl(mMemInfo[cnt].main_ion_fd,
637                      ION_IOC_IMPORT, &ion_info_fd) < 0) {
638                ALOGE("%s: ION import failed\n", __func__);
639                for(size_t i = 0; i < cnt; i++) {
640                    struct ion_handle_data ion_handle;
641                    memset(&ion_handle, 0, sizeof(ion_handle));
642                    ion_handle.handle = mMemInfo[i].handle;
643                    if (ioctl(mMemInfo[i].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
644                        ALOGE("ion free failed");
645                    }
646                    close(mMemInfo[i].main_ion_fd);
647                    mBufferHandle[i] = NULL;
648                }
649                close(mMemInfo[cnt].main_ion_fd);
650                memset(&mMemInfo, 0, sizeof(mMemInfo));
651                ret = -ENOMEM;
652                goto end;
653            }
654        }
655        ALOGD("%s: idx = %d, fd = %d, size = %d, offset = %d",
656              __func__, cnt, mPrivateHandle[cnt]->fd,
657              mPrivateHandle[cnt]->size,
658              mPrivateHandle[cnt]->offset);
659        mMemInfo[cnt].fd =
660            mPrivateHandle[cnt]->fd;
661        mMemInfo[cnt].size =
662            mPrivateHandle[cnt]->size;
663        mMemInfo[cnt].handle = ion_info_fd.handle;
664
665        void *vaddr = mmap(NULL,
666                    mMemInfo[cnt].size,
667                    PROT_READ | PROT_WRITE,
668                    MAP_SHARED,
669                    mMemInfo[cnt].fd, 0);
670        if (vaddr == MAP_FAILED) {
671            for (int j = cnt-1; j >= 0; j --) {
672                munmap(mPtr[cnt], mMemInfo[cnt].size);
673                ret = -ENOMEM;
674                break;
675            }
676        } else
677            mPtr[cnt] = vaddr;
678    }
679    mBufferCount = num_buffers;
680
681end:
682    ALOGI(" %s : X ",__func__);
683    return ret;
684}
685
686/*===========================================================================
687 * FUNCTION   : unregisterBuffers
688 *
689 * DESCRIPTION: unregister buffers
690 *
691 * PARAMETERS : none
692 *
693 * RETURN     : none
694 *==========================================================================*/
695void QCamera3GrallocMemory::unregisterBuffers()
696{
697    ALOGI("%s: E ", __FUNCTION__);
698
699    for (int cnt = 0; cnt < mBufferCount; cnt++) {
700        munmap(mPtr[cnt], mMemInfo[cnt].size);
701        mPtr[cnt] = NULL;
702
703        struct ion_handle_data ion_handle;
704        memset(&ion_handle, 0, sizeof(ion_handle));
705        ion_handle.handle = mMemInfo[cnt].handle;
706        if (ioctl(mMemInfo[cnt].main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
707            ALOGE("ion free failed");
708        }
709        close(mMemInfo[cnt].main_ion_fd);
710        ALOGD("put buffer %d successfully", cnt);
711    }
712    mBufferCount = 0;
713    ALOGI(" %s : X ",__FUNCTION__);
714}
715
716/*===========================================================================
717 * FUNCTION   : markFrameNumber
718 *
719 * DESCRIPTION: We use this function from the request call path to mark the
720 *              buffers with the frame number they are intended for this info
721 *              is used later when giving out callback & it is duty of PP to
722 *              ensure that data for that particular frameNumber/Request is
723 *              written to this buffer.
724 * PARAMETERS :
725 *   @index   : index of the buffer
726 *   @frame#  : Frame number from the framework
727 *
728 * RETURN     : int32_t type of status
729 *              NO_ERROR  -- success
730 *              none-zero failure code
731 *==========================================================================*/
732int32_t QCamera3GrallocMemory::markFrameNumber(int index, uint32_t frameNumber)
733{
734    if(index >= mBufferCount || index >= MM_CAMERA_MAX_NUM_FRAMES) {
735        ALOGE("%s: Index out of bounds",__func__);
736        return BAD_INDEX;
737    }
738    mCurrentFrameNumbers[index] = frameNumber;
739    return NO_ERROR;
740}
741
742/*===========================================================================
743 * FUNCTION   : getFrameNumber
744 *
745 * DESCRIPTION: We use this to fetch the frameNumber for the request with which
746 *              this buffer was given to HAL
747 *
748 *
749 * PARAMETERS :
750 *   @index   : index of the buffer
751 *
752 * RETURN     : int32_t frameNumber
753 *              postive/zero  -- success
754 *              negetive failure
755 *==========================================================================*/
756int32_t QCamera3GrallocMemory::getFrameNumber(int index)
757{
758    if(index >= mBufferCount || index >= MM_CAMERA_MAX_NUM_FRAMES) {
759        ALOGE("%s: Index out of bounds",__func__);
760        return -1;
761    }
762
763    return mCurrentFrameNumbers[index];
764}
765
766/*===========================================================================
767 * FUNCTION   : cacheOps
768 *
769 * DESCRIPTION: ion related memory cache operations
770 *
771 * PARAMETERS :
772 *   @index   : index of the buffer
773 *   @cmd     : cache ops command
774 *
775 * RETURN     : int32_t type of status
776 *              NO_ERROR  -- success
777 *              none-zero failure code
778 *==========================================================================*/
779int QCamera3GrallocMemory::cacheOps(int index, unsigned int cmd)
780{
781    if (index >= mBufferCount)
782        return BAD_INDEX;
783    return cacheOpsInternal(index, cmd, mPtr[index]);
784}
785
786/*===========================================================================
787 * FUNCTION   : getRegFlags
788 *
789 * DESCRIPTION: query initial reg flags
790 *
791 * PARAMETERS :
792 *   @regFlags: initial reg flags of the allocated buffers
793 *
794 * RETURN     : int32_t type of status
795 *              NO_ERROR  -- success
796 *              none-zero failure code
797 *==========================================================================*/
798int QCamera3GrallocMemory::getRegFlags(uint8_t *regFlags) const
799{
800    int i;
801    for (i = 0; i < mBufferCount; i ++)
802        regFlags[i] = 0;
803    return NO_ERROR;
804}
805
806/*===========================================================================
807 * FUNCTION   : getMatchBufIndex
808 *
809 * DESCRIPTION: query buffer index by object ptr
810 *
811 * PARAMETERS :
812 *   @opaque  : opaque ptr
813 *
814 * RETURN     : buffer index if match found,
815 *              -1 if failed
816 *==========================================================================*/
817int QCamera3GrallocMemory::getMatchBufIndex(void *object)
818{
819    int index = -1;
820    buffer_handle_t *key = (buffer_handle_t*) object;
821    if (!key) {
822        return BAD_VALUE;
823    }
824    for (int i = 0; i < mBufferCount; i++) {
825        if (mBufferHandle[i] == key) {
826            index = i;
827            break;
828        }
829    }
830    return index;
831}
832
833/*===========================================================================
834 * FUNCTION   : getPtr
835 *
836 * DESCRIPTION: return buffer pointer
837 *
838 * PARAMETERS :
839 *   @index   : index of the buffer
840 *
841 * RETURN     : buffer ptr
842 *==========================================================================*/
843void *QCamera3GrallocMemory::getPtr(int index) const
844{
845    if (index >= mBufferCount) {
846        ALOGE("index out of bound");
847        return (void *)BAD_INDEX;
848    }
849    return mPtr[index];
850}
851
852}; //namespace qcamera
853