1 /* Copyright (c) 2012-2016, The Linux Foundation. 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 // System dependencies
33 #include <fcntl.h>
34 #define MMAN_H <SYSTEM_HEADER_PREFIX/mman.h>
35 #include MMAN_H
36 #include "gralloc_priv.h"
37
38 // Display dependencies
39 #include "qdMetaData.h"
40
41 // Camera dependencies
42 #include "QCamera3HWI.h"
43 #include "QCamera3Mem.h"
44 #include "QCameraTrace.h"
45
46 extern "C" {
47 #include "mm_camera_dbg.h"
48 #include "mm_camera_interface.h"
49 }
50
51 using namespace android;
52
53 namespace qcamera {
54
55 // QCaemra2Memory base class
56
57 /*===========================================================================
58 * FUNCTION : QCamera3Memory
59 *
60 * DESCRIPTION: default constructor of QCamera3Memory
61 *
62 * PARAMETERS : none
63 *
64 * RETURN : None
65 *==========================================================================*/
QCamera3Memory()66 QCamera3Memory::QCamera3Memory()
67 {
68 mBufferCount = 0;
69 for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i++) {
70 mMemInfo[i].fd = -1;
71 mMemInfo[i].handle = 0;
72 mMemInfo[i].size = 0;
73 mCurrentFrameNumbers[i] = -1;
74 }
75 main_ion_fd = open("/dev/ion", O_RDONLY);
76 }
77
78 /*===========================================================================
79 * FUNCTION : ~QCamera3Memory
80 *
81 * DESCRIPTION: deconstructor of QCamera3Memory
82 *
83 * PARAMETERS : none
84 *
85 * RETURN : None
86 *==========================================================================*/
~QCamera3Memory()87 QCamera3Memory::~QCamera3Memory()
88 {
89 close(main_ion_fd);
90 }
91
92 /*===========================================================================
93 * FUNCTION : cacheOpsInternal
94 *
95 * DESCRIPTION: ion related memory cache operations
96 *
97 * PARAMETERS :
98 * @index : index of the buffer
99 * @cmd : cache ops command
100 * @vaddr : ptr to the virtual address
101 *
102 * RETURN : int32_t type of status
103 * NO_ERROR -- success
104 * none-zero failure code
105 *==========================================================================*/
cacheOpsInternal(uint32_t index,unsigned int cmd,void * vaddr)106 int QCamera3Memory::cacheOpsInternal(uint32_t index, unsigned int cmd, void *vaddr)
107 {
108 ATRACE_CALL();
109 Mutex::Autolock lock(mLock);
110
111 struct ion_flush_data cache_inv_data;
112 struct ion_custom_data custom_data;
113 int ret = OK;
114
115 if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
116 LOGE("index %d out of bound [0, %d)",
117 index, MM_CAMERA_MAX_NUM_FRAMES);
118 return BAD_INDEX;
119 }
120
121 if (0 == mMemInfo[index].handle) {
122 LOGE("Buffer at %d not registered", index);
123 return BAD_INDEX;
124 }
125
126 memset(&cache_inv_data, 0, sizeof(cache_inv_data));
127 memset(&custom_data, 0, sizeof(custom_data));
128 cache_inv_data.vaddr = vaddr;
129 cache_inv_data.fd = mMemInfo[index].fd;
130 cache_inv_data.handle = mMemInfo[index].handle;
131 cache_inv_data.length = (unsigned int)mMemInfo[index].size;
132 custom_data.cmd = cmd;
133 custom_data.arg = (unsigned long)&cache_inv_data;
134
135 LOGD("addr = %p, fd = %d, handle = %lx length = %d, ION Fd = %d",
136 cache_inv_data.vaddr, cache_inv_data.fd,
137 (unsigned long)cache_inv_data.handle, cache_inv_data.length,
138 main_ion_fd);
139 ret = ioctl(main_ion_fd, ION_IOC_CUSTOM, &custom_data);
140 if (ret < 0)
141 LOGE("Cache Invalidate failed: %s\n", strerror(errno));
142
143 return ret;
144 }
145
146 /*===========================================================================
147 * FUNCTION : getFd
148 *
149 * DESCRIPTION: return file descriptor of the indexed buffer
150 *
151 * PARAMETERS :
152 * @index : index of the buffer
153 *
154 * RETURN : file descriptor
155 *==========================================================================*/
getFd(uint32_t index)156 int QCamera3Memory::getFd(uint32_t index)
157 {
158 Mutex::Autolock lock(mLock);
159
160 if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
161 return BAD_INDEX;
162 }
163
164 if (0 == mMemInfo[index].handle) {
165 return BAD_INDEX;
166 }
167
168 return mMemInfo[index].fd;
169 }
170
171 /*===========================================================================
172 * FUNCTION : getSize
173 *
174 * DESCRIPTION: return buffer size of the indexed buffer
175 *
176 * PARAMETERS :
177 * @index : index of the buffer
178 *
179 * RETURN : buffer size
180 *==========================================================================*/
getSize(uint32_t index)181 ssize_t QCamera3Memory::getSize(uint32_t index)
182 {
183 Mutex::Autolock lock(mLock);
184
185 if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
186 return BAD_INDEX;
187 }
188
189 if (0 == mMemInfo[index].handle) {
190 return BAD_INDEX;
191 }
192
193 return (ssize_t)mMemInfo[index].size;
194 }
195
196 /*===========================================================================
197 * FUNCTION : getCnt
198 *
199 * DESCRIPTION: query number of buffers allocated
200 *
201 * PARAMETERS : none
202 *
203 * RETURN : number of buffers allocated
204 *==========================================================================*/
getCnt()205 uint32_t QCamera3Memory::getCnt()
206 {
207 Mutex::Autolock lock(mLock);
208
209 return mBufferCount;
210 }
211
212 /*===========================================================================
213 * FUNCTION : getBufDef
214 *
215 * DESCRIPTION: query detailed buffer information
216 *
217 * PARAMETERS :
218 * @offset : [input] frame buffer offset
219 * @bufDef : [output] reference to struct to store buffer definition
220 * @index : [input] index of the buffer
221 * @virtualAddr : [input] Whether to fill out virtual address
222 *
223 * RETURN : int32_t type of status
224 * NO_ERROR -- success
225 * none-zero failure code
226 *==========================================================================*/
getBufDef(const cam_frame_len_offset_t & offset,mm_camera_buf_def_t & bufDef,uint32_t index,bool virtualAddr)227 int32_t QCamera3Memory::getBufDef(const cam_frame_len_offset_t &offset,
228 mm_camera_buf_def_t &bufDef, uint32_t index, bool virtualAddr)
229 {
230 Mutex::Autolock lock(mLock);
231
232 if (!mBufferCount) {
233 LOGE("Memory not allocated");
234 return NO_INIT;
235 }
236
237 bufDef.fd = mMemInfo[index].fd;
238 bufDef.frame_len = mMemInfo[index].size;
239 bufDef.mem_info = (void *)this;
240 bufDef.buffer = virtualAddr ? getPtrLocked(index) : nullptr;
241 bufDef.planes_buf.num_planes = (int8_t)offset.num_planes;
242 bufDef.buf_idx = (uint8_t)index;
243
244 /* Plane 0 needs to be set separately. Set other planes in a loop */
245 bufDef.planes_buf.planes[0].length = offset.mp[0].len;
246 bufDef.planes_buf.planes[0].m.userptr = (long unsigned int)mMemInfo[index].fd;
247 bufDef.planes_buf.planes[0].data_offset = offset.mp[0].offset;
248 bufDef.planes_buf.planes[0].reserved[0] = 0;
249 for (int i = 1; i < bufDef.planes_buf.num_planes; i++) {
250 bufDef.planes_buf.planes[i].length = offset.mp[i].len;
251 bufDef.planes_buf.planes[i].m.userptr = (long unsigned int)mMemInfo[i].fd;
252 bufDef.planes_buf.planes[i].data_offset = offset.mp[i].offset;
253 bufDef.planes_buf.planes[i].reserved[0] =
254 bufDef.planes_buf.planes[i-1].reserved[0] +
255 bufDef.planes_buf.planes[i-1].length;
256 }
257
258 return NO_ERROR;
259 }
260
261 /*===========================================================================
262 * FUNCTION : QCamera3HeapMemory
263 *
264 * DESCRIPTION: constructor of QCamera3HeapMemory for ion memory used internally in HAL
265 *
266 * PARAMETERS : none
267 *
268 * RETURN : none
269 *==========================================================================*/
QCamera3HeapMemory(uint32_t maxCnt)270 QCamera3HeapMemory::QCamera3HeapMemory(uint32_t maxCnt)
271 : QCamera3Memory()
272 {
273 mMaxCnt = MIN(maxCnt, MM_CAMERA_MAX_NUM_FRAMES);
274 for (uint32_t i = 0; i < mMaxCnt; i ++)
275 mPtr[i] = NULL;
276 }
277
278 /*===========================================================================
279 * FUNCTION : ~QCamera3HeapMemory
280 *
281 * DESCRIPTION: deconstructor of QCamera3HeapMemory
282 *
283 * PARAMETERS : none
284 *
285 * RETURN : none
286 *==========================================================================*/
~QCamera3HeapMemory()287 QCamera3HeapMemory::~QCamera3HeapMemory()
288 {
289 }
290
291 /*===========================================================================
292 * FUNCTION : allocOneBuffer
293 *
294 * DESCRIPTION: impl of allocating one buffers of certain size
295 *
296 * PARAMETERS :
297 * @memInfo : [output] reference to struct to store additional memory allocation info
298 * @heap : [input] heap id to indicate where the buffers will be allocated from
299 * @size : [input] lenght of the buffer to be allocated
300 *
301 * RETURN : int32_t type of status
302 * NO_ERROR -- success
303 * none-zero failure code
304 *==========================================================================*/
allocOneBuffer(QCamera3MemInfo & memInfo,unsigned int heap_id,size_t size,bool isCached)305 int QCamera3HeapMemory::allocOneBuffer(QCamera3MemInfo &memInfo,
306 unsigned int heap_id, size_t size, bool isCached)
307 {
308 int rc = OK;
309 struct ion_handle_data handle_data;
310 struct ion_allocation_data allocData;
311 struct ion_fd_data ion_info_fd;
312
313 if (main_ion_fd < 0) {
314 LOGE("Ion dev open failed: %s\n", strerror(errno));
315 goto ION_OPEN_FAILED;
316 }
317
318 memset(&allocData, 0, sizeof(allocData));
319 allocData.len = size;
320 /* to make it page size aligned */
321 allocData.len = (allocData.len + 4095U) & (~4095U);
322 allocData.align = 4096;
323 if (isCached) {
324 allocData.flags = ION_FLAG_CACHED;
325 }
326 allocData.heap_id_mask = heap_id;
327 rc = ioctl(main_ion_fd, ION_IOC_ALLOC, &allocData);
328 if (rc < 0) {
329 LOGE("ION allocation for len %d failed: %s\n", allocData.len,
330 strerror(errno));
331 goto ION_ALLOC_FAILED;
332 }
333
334 memset(&ion_info_fd, 0, sizeof(ion_info_fd));
335 ion_info_fd.handle = allocData.handle;
336 rc = ioctl(main_ion_fd, ION_IOC_SHARE, &ion_info_fd);
337 if (rc < 0) {
338 LOGE("ION map failed %s\n", strerror(errno));
339 goto ION_MAP_FAILED;
340 }
341
342 memInfo.fd = ion_info_fd.fd;
343 memInfo.handle = ion_info_fd.handle;
344 memInfo.size = allocData.len;
345 return OK;
346
347 ION_MAP_FAILED:
348 memset(&handle_data, 0, sizeof(handle_data));
349 handle_data.handle = ion_info_fd.handle;
350 ioctl(main_ion_fd, ION_IOC_FREE, &handle_data);
351 ION_ALLOC_FAILED:
352 ION_OPEN_FAILED:
353 return NO_MEMORY;
354 }
355
356 /*===========================================================================
357 * FUNCTION : deallocOneBuffer
358 *
359 * DESCRIPTION: impl of deallocating one buffers
360 *
361 * PARAMETERS :
362 * @memInfo : reference to struct that stores additional memory allocation info
363 *
364 * RETURN : none
365 *==========================================================================*/
deallocOneBuffer(QCamera3MemInfo & memInfo)366 void QCamera3HeapMemory::deallocOneBuffer(QCamera3MemInfo &memInfo)
367 {
368 struct ion_handle_data handle_data;
369
370 if (memInfo.fd >= 0) {
371 close(memInfo.fd);
372 memInfo.fd = -1;
373 }
374
375 if (main_ion_fd >= 0) {
376 memset(&handle_data, 0, sizeof(handle_data));
377 handle_data.handle = memInfo.handle;
378 ioctl(main_ion_fd, ION_IOC_FREE, &handle_data);
379 }
380 memInfo.handle = 0;
381 memInfo.size = 0;
382 }
383
384 /*===========================================================================
385 * FUNCTION : getPtrLocked
386 *
387 * DESCRIPTION: Return buffer pointer.
388 *
389 * PARAMETERS :
390 * @index : index of the buffer
391 *
392 * RETURN : buffer ptr
393 *==========================================================================*/
getPtrLocked(uint32_t index)394 void *QCamera3HeapMemory::getPtrLocked(uint32_t index)
395 {
396 if (index >= mBufferCount) {
397 LOGE("index out of bound");
398 return NULL;
399 }
400 return mPtr[index];
401 }
402
403 /*===========================================================================
404 * FUNCTION : markFrameNumber
405 *
406 * DESCRIPTION: We use this function from the request call path to mark the
407 * buffers with the frame number they are intended for this info
408 * is used later when giving out callback & it is duty of PP to
409 * ensure that data for that particular frameNumber/Request is
410 * written to this buffer.
411 * PARAMETERS :
412 * @index : index of the buffer
413 * @frame# : Frame number from the framework
414 *
415 * RETURN : int32_t type of status
416 * NO_ERROR -- success
417 * none-zero failure code
418 *==========================================================================*/
markFrameNumber(uint32_t index,uint32_t frameNumber)419 int32_t QCamera3HeapMemory::markFrameNumber(uint32_t index, uint32_t frameNumber)
420 {
421 Mutex::Autolock lock(mLock);
422
423 if (index >= mBufferCount) {
424 LOGE("Index %d out of bounds, current buffer count is %d",
425 index, mBufferCount);
426 return BAD_INDEX;
427 }
428
429 if (0 == mMemInfo[index].handle) {
430 LOGE("Buffer at %d not allocated", index);
431 return BAD_INDEX;
432 }
433
434 mCurrentFrameNumbers[index] = (int32_t)frameNumber;
435
436 return NO_ERROR;
437 }
438
439
440 /*===========================================================================
441 * FUNCTION : getFrameNumber
442 *
443 * DESCRIPTION: We use this to fetch the frameNumber for the request with which
444 * this buffer was given to HAL
445 *
446 *
447 * PARAMETERS :
448 * @index : index of the buffer
449 *
450 * RETURN : int32_t frameNumber
451 * positive/zero -- success
452 * negative failure
453 *==========================================================================*/
getFrameNumber(uint32_t index)454 int32_t QCamera3HeapMemory::getFrameNumber(uint32_t index)
455 {
456 Mutex::Autolock lock(mLock);
457
458 if (index >= mBufferCount) {
459 LOGE("Index %d out of bounds, current buffer count is %d",
460 index, mBufferCount);
461 return -1;
462 }
463
464 if (0 == mMemInfo[index].handle) {
465 LOGE("Buffer at %d not registered", index);
466 return -1;
467 }
468
469 return mCurrentFrameNumbers[index];
470 }
471
472
473 /*===========================================================================
474 * FUNCTION : getOldestFrameNumber
475 *
476 * DESCRIPTION: We use this to fetch the oldest frame number expected per FIFO
477 *
478 *
479 * PARAMETERS :
480 *
481 *
482 * RETURN : int32_t frameNumber
483 * negative failure
484 *==========================================================================*/
getOldestFrameNumber(uint32_t & bufIndex)485 int32_t QCamera3HeapMemory::getOldestFrameNumber(uint32_t &bufIndex)
486 {
487 Mutex::Autolock lock(mLock);
488
489 int32_t oldest = INT_MAX;
490 bool empty = true;
491
492 for (uint32_t index = 0;
493 index < mBufferCount; index++) {
494 if (mMemInfo[index].handle) {
495 if ((empty) || (!empty && oldest > mCurrentFrameNumbers[index]
496 && mCurrentFrameNumbers[index] != -1)) {
497 oldest = mCurrentFrameNumbers[index];
498 bufIndex = index;
499 }
500 empty = false;
501 }
502 }
503 if (empty)
504 return -1;
505 else
506 return oldest;
507 }
508
509
510 /*===========================================================================
511 * FUNCTION : getBufferIndex
512 *
513 * DESCRIPTION: We use this to fetch the buffer index for the request with
514 * a particular frame number
515 *
516 *
517 * PARAMETERS :
518 * @frameNumber : frame number of the buffer
519 *
520 * RETURN : int32_t buffer index
521 * negative failure
522 *==========================================================================*/
getBufferIndex(uint32_t frameNumber)523 int32_t QCamera3HeapMemory::getBufferIndex(uint32_t frameNumber)
524 {
525 Mutex::Autolock lock(mLock);
526
527 for (uint32_t index = 0;
528 index < mBufferCount; index++) {
529 if (mMemInfo[index].handle &&
530 mCurrentFrameNumbers[index] == (int32_t)frameNumber)
531 return (int32_t)index;
532 }
533 return -1;
534 }
535
536 /*===========================================================================
537 * FUNCTION : getPtr
538 *
539 * DESCRIPTION: Return buffer pointer
540 *
541 * PARAMETERS :
542 * @index : index of the buffer
543 *
544 * RETURN : buffer ptr
545 *==========================================================================*/
getPtr(uint32_t index)546 void *QCamera3HeapMemory::getPtr(uint32_t index)
547 {
548 return getPtrLocked(index);
549 }
550
551 /*===========================================================================
552 * FUNCTION : allocate
553 *
554 * DESCRIPTION: allocate requested number of buffers of certain size
555 *
556 * PARAMETERS :
557 * @size : lenght of the buffer to be allocated
558 *
559 * RETURN : int32_t type of status
560 * NO_ERROR -- success
561 * none-zero failure code
562 *==========================================================================*/
allocate(size_t size)563 int QCamera3HeapMemory::allocate(size_t size)
564 {
565 unsigned int heap_id_mask = 0x1 << ION_IOMMU_HEAP_ID;
566 uint32_t i;
567 int rc = NO_ERROR;
568
569 //Note that now we allow incremental allocation. In other words, we allow
570 //multiple alloc being called as long as the sum of count does not exceed
571 //mMaxCnt.
572 if (mBufferCount > 0) {
573 LOGE("There is already buffer allocated.");
574 return BAD_INDEX;
575 }
576
577 for (i = 0; i < mMaxCnt; i ++) {
578 rc = allocOneBuffer(mMemInfo[i], heap_id_mask, size);
579 if (rc < 0) {
580 LOGE("AllocateIonMemory failed");
581 goto ALLOC_FAILED;
582 }
583
584 void *vaddr = mmap(NULL,
585 mMemInfo[i].size,
586 PROT_READ | PROT_WRITE,
587 MAP_SHARED,
588 mMemInfo[i].fd, 0);
589 if (vaddr == MAP_FAILED) {
590 deallocOneBuffer(mMemInfo[i]);
591 LOGE("mmap failed for buffer %d", i);
592 goto ALLOC_FAILED;
593 } else
594 mPtr[i] = vaddr;
595 }
596 if (rc == 0)
597 mBufferCount = mMaxCnt;
598
599 return OK;
600
601 ALLOC_FAILED:
602 for (uint32_t j = 0; j < i; j++) {
603 munmap(mPtr[j], mMemInfo[j].size);
604 mPtr[j] = NULL;
605 deallocOneBuffer(mMemInfo[j]);
606 }
607 return NO_MEMORY;
608 }
609
610 /*===========================================================================
611 * FUNCTION : allocateOne
612 *
613 * DESCRIPTION: allocate one buffer
614 *
615 * PARAMETERS :
616 * @size : lenght of the buffer to be allocated
617 *
618 * RETURN : int32_t type of status
619 * NO_ERROR -- success
620 * none-zero failure code
621 *==========================================================================*/
allocateOne(size_t size,bool isCached)622 int QCamera3HeapMemory::allocateOne(size_t size, bool isCached)
623 {
624 unsigned int heap_id_mask = 0x1 << ION_IOMMU_HEAP_ID;
625 int rc = NO_ERROR;
626
627 //Note that now we allow incremental allocation. In other words, we allow
628 //multiple alloc being called as long as the sum of count does not exceed
629 //mMaxCnt.
630 if (mBufferCount + 1 > mMaxCnt) {
631 LOGE("Buffer count %d + 1 out of bound. Max is %d",
632 mBufferCount, mMaxCnt);
633 return BAD_INDEX;
634 }
635
636 rc = allocOneBuffer(mMemInfo[mBufferCount], heap_id_mask, size, isCached);
637 if (rc < 0) {
638 LOGE("AllocateIonMemory failed");
639 return NO_MEMORY;
640 }
641
642 void *vaddr = mmap(NULL,
643 mMemInfo[mBufferCount].size,
644 PROT_READ | PROT_WRITE,
645 MAP_SHARED,
646 mMemInfo[mBufferCount].fd, 0);
647 if (vaddr == MAP_FAILED) {
648 deallocOneBuffer(mMemInfo[mBufferCount]);
649 LOGE("mmap failed for buffer");
650 return NO_MEMORY;
651 } else
652 mPtr[mBufferCount] = vaddr;
653
654 if (rc == 0)
655 mBufferCount += 1;
656
657 return mBufferCount-1;
658 }
659
660 /*===========================================================================
661 * FUNCTION : deallocate
662 *
663 * DESCRIPTION: deallocate buffers
664 *
665 * PARAMETERS : none
666 *
667 * RETURN : none
668 *==========================================================================*/
deallocate()669 void QCamera3HeapMemory::deallocate()
670 {
671 for (uint32_t i = 0; i < mBufferCount; i++) {
672 munmap(mPtr[i], mMemInfo[i].size);
673 mPtr[i] = NULL;
674 deallocOneBuffer(mMemInfo[i]);
675 mCurrentFrameNumbers[i] = -1;
676 }
677 mBufferCount = 0;
678 }
679
680 /*===========================================================================
681 * FUNCTION : cacheOps
682 *
683 * DESCRIPTION: ion related memory cache operations
684 *
685 * PARAMETERS :
686 * @index : index of the buffer
687 * @cmd : cache ops command
688 *
689 * RETURN : int32_t type of status
690 * NO_ERROR -- success
691 * none-zero failure code
692 *==========================================================================*/
cacheOps(uint32_t index,unsigned int cmd)693 int QCamera3HeapMemory::cacheOps(uint32_t index, unsigned int cmd)
694 {
695 if (index >= mBufferCount)
696 return BAD_INDEX;
697 return cacheOpsInternal(index, cmd, mPtr[index]);
698 }
699
700 /*===========================================================================
701 * FUNCTION : getMatchBufIndex
702 *
703 * DESCRIPTION: query buffer index by object ptr
704 *
705 * PARAMETERS :
706 * @object : object ptr
707 *
708 * RETURN : buffer index if match found,
709 * -1 if failed
710 *==========================================================================*/
getMatchBufIndex(void *)711 int QCamera3HeapMemory::getMatchBufIndex(void * /*object*/)
712 {
713
714 /*
715 TODO for HEAP memory type, would there be an equivalent requirement?
716
717 int index = -1;
718 buffer_handle_t *key = (buffer_handle_t*) object;
719 if (!key) {
720 return BAD_VALUE;
721 }
722 for (int i = 0; i < mBufferCount; i++) {
723 if (mBufferHandle[i] == key) {
724 index = i;
725 break;
726 }
727 }
728 return index;
729 */
730 LOGE("FATAL: Not supposed to come here");
731 return -1;
732 }
733
734 /*===========================================================================
735 * FUNCTION : QCamera3GrallocMemory
736 *
737 * DESCRIPTION: constructor of QCamera3GrallocMemory
738 * preview stream buffers are allocated from gralloc native_windoe
739 *
740 * PARAMETERS :
741 * @startIdx : start index of array after which we can register buffers in.
742 *
743 * RETURN : none
744 *==========================================================================*/
QCamera3GrallocMemory(uint32_t startIdx)745 QCamera3GrallocMemory::QCamera3GrallocMemory(uint32_t startIdx)
746 : QCamera3Memory(), mStartIdx(startIdx)
747 {
748 for (int i = 0; i < MM_CAMERA_MAX_NUM_FRAMES; i ++) {
749 mBufferHandle[i] = NULL;
750 mPrivateHandle[i] = NULL;
751 mPtr[i] = nullptr;
752 }
753 }
754
755 /*===========================================================================
756 * FUNCTION : ~QCamera3GrallocMemory
757 *
758 * DESCRIPTION: deconstructor of QCamera3GrallocMemory
759 *
760 * PARAMETERS : none
761 *
762 * RETURN : none
763 *==========================================================================*/
~QCamera3GrallocMemory()764 QCamera3GrallocMemory::~QCamera3GrallocMemory()
765 {
766 }
767
768 /*===========================================================================
769 * FUNCTION : registerBuffer
770 *
771 * DESCRIPTION: registers frameworks-allocated gralloc buffer_handle_t
772 *
773 * PARAMETERS :
774 * @buffers : buffer_handle_t pointer
775 * @type : cam_stream_type_t
776 *
777 * RETURN : int32_t type of status
778 * NO_ERROR -- success
779 * none-zero failure code
780 *==========================================================================*/
registerBuffer(buffer_handle_t * buffer,__unused cam_stream_type_t type)781 int QCamera3GrallocMemory::registerBuffer(buffer_handle_t *buffer,
782 __unused cam_stream_type_t type)
783 {
784 status_t ret = NO_ERROR;
785 struct ion_fd_data ion_info_fd;
786 int32_t colorSpace = ITU_R_601_FR;
787 int32_t idx = -1;
788
789 LOGD("E");
790
791 memset(&ion_info_fd, 0, sizeof(ion_info_fd));
792
793 if (0 <= getMatchBufIndex((void *) buffer)) {
794 LOGL("Buffer already registered");
795 return ALREADY_EXISTS;
796 }
797
798 Mutex::Autolock lock(mLock);
799 if (mBufferCount >= (MM_CAMERA_MAX_NUM_FRAMES - 1 - mStartIdx)) {
800 LOGE("Number of buffers %d greater than what's supported %d",
801 mBufferCount, MM_CAMERA_MAX_NUM_FRAMES - mStartIdx);
802 return BAD_INDEX;
803 }
804
805 idx = getFreeIndexLocked();
806 if (0 > idx) {
807 LOGE("No available memory slots");
808 return BAD_INDEX;
809 }
810
811 mBufferHandle[idx] = buffer;
812 mPrivateHandle[idx] = (struct private_handle_t *)(*mBufferHandle[idx]);
813
814 setMetaData(mPrivateHandle[idx], UPDATE_COLOR_SPACE, &colorSpace);
815
816 if (main_ion_fd < 0) {
817 LOGE("failed: could not open ion device");
818 ret = NO_MEMORY;
819 goto end;
820 } else {
821 ion_info_fd.fd = mPrivateHandle[idx]->fd;
822 if (ioctl(main_ion_fd,
823 ION_IOC_IMPORT, &ion_info_fd) < 0) {
824 LOGE("ION import failed\n");
825 ret = NO_MEMORY;
826 goto end;
827 }
828 }
829 LOGD("idx = %d, fd = %d, size = %d, offset = %d",
830 idx, mPrivateHandle[idx]->fd,
831 mPrivateHandle[idx]->size,
832 mPrivateHandle[idx]->offset);
833 mMemInfo[idx].fd = mPrivateHandle[idx]->fd;
834 mMemInfo[idx].size =
835 ( /* FIXME: Should update ION interface */ size_t)
836 mPrivateHandle[idx]->size;
837 mMemInfo[idx].handle = ion_info_fd.handle;
838
839 mBufferCount++;
840
841 end:
842 LOGD("X ");
843 return ret;
844 }
845 /*===========================================================================
846 * FUNCTION : unregisterBufferLocked
847 *
848 * DESCRIPTION: Unregister buffer. Please note that this method has to be
849 * called with 'mLock' acquired.
850 *
851 * PARAMETERS :
852 * @idx : unregister buffer at index 'idx'
853 *
854 * RETURN : int32_t type of status
855 * NO_ERROR -- success
856 * none-zero failure code
857 *==========================================================================*/
unregisterBufferLocked(size_t idx)858 int32_t QCamera3GrallocMemory::unregisterBufferLocked(size_t idx)
859 {
860 if (mPtr[idx] != nullptr) {
861 munmap(mPtr[idx], mMemInfo[idx].size);
862 mPtr[idx] = nullptr;
863 }
864
865 struct ion_handle_data ion_handle;
866 memset(&ion_handle, 0, sizeof(ion_handle));
867 ion_handle.handle = mMemInfo[idx].handle;
868 if (ioctl(main_ion_fd, ION_IOC_FREE, &ion_handle) < 0) {
869 LOGE("ion free failed");
870 }
871 memset(&mMemInfo[idx], 0, sizeof(struct QCamera3MemInfo));
872 mBufferHandle[idx] = NULL;
873 mPrivateHandle[idx] = NULL;
874 mCurrentFrameNumbers[idx] = -1;
875 mBufferCount--;
876
877 return NO_ERROR;
878 }
879
880 /*===========================================================================
881 * FUNCTION : unregisterBuffer
882 *
883 * DESCRIPTION: unregister buffer
884 *
885 * PARAMETERS :
886 * @idx : unregister buffer at index 'idx'
887 *
888 * RETURN : int32_t type of status
889 * NO_ERROR -- success
890 * none-zero failure code
891 *==========================================================================*/
unregisterBuffer(size_t idx)892 int32_t QCamera3GrallocMemory::unregisterBuffer(size_t idx)
893 {
894 int32_t rc = NO_ERROR;
895 Mutex::Autolock lock(mLock);
896
897 LOGD("E ", __FUNCTION__);
898
899 if (MM_CAMERA_MAX_NUM_FRAMES <= idx) {
900 LOGE("Buffer index %d greater than what is supported %d",
901 idx, MM_CAMERA_MAX_NUM_FRAMES);
902 return BAD_VALUE;
903 }
904 if (idx < mStartIdx) {
905 LOGE("buffer index %d less than starting index %d",
906 idx, mStartIdx);
907 return BAD_INDEX;
908 }
909
910 if (0 == mMemInfo[idx].handle) {
911 LOGE("Trying to unregister buffer at %d which still not registered",
912 idx);
913 return BAD_VALUE;
914 }
915
916 rc = unregisterBufferLocked(idx);
917
918 LOGD("X ",__FUNCTION__);
919
920 return rc;
921 }
922
923 /*===========================================================================
924 * FUNCTION : unregisterBuffers
925 *
926 * DESCRIPTION: unregister buffers
927 *
928 * PARAMETERS : none
929 *
930 * RETURN : none
931 *==========================================================================*/
unregisterBuffers()932 void QCamera3GrallocMemory::unregisterBuffers()
933 {
934 int err = NO_ERROR;
935 Mutex::Autolock lock(mLock);
936
937 LOGD("E ", __FUNCTION__);
938
939 for (uint32_t cnt = mStartIdx; cnt < MM_CAMERA_MAX_NUM_FRAMES; cnt++) {
940 if (0 == mMemInfo[cnt].handle) {
941 continue;
942 }
943 err = unregisterBufferLocked(cnt);
944 if (NO_ERROR != err) {
945 LOGE("Error unregistering buffer %d error %d",
946 cnt, err);
947 }
948 }
949 mBufferCount = 0;
950 LOGD("X ",__FUNCTION__);
951 }
952
953 /*===========================================================================
954 * FUNCTION : markFrameNumber
955 *
956 * DESCRIPTION: We use this function from the request call path to mark the
957 * buffers with the frame number they are intended for this info
958 * is used later when giving out callback & it is duty of PP to
959 * ensure that data for that particular frameNumber/Request is
960 * written to this buffer.
961 * PARAMETERS :
962 * @index : index of the buffer
963 * @frame# : Frame number from the framework
964 *
965 * RETURN : int32_t type of status
966 * NO_ERROR -- success
967 * none-zero failure code
968 *==========================================================================*/
markFrameNumber(uint32_t index,uint32_t frameNumber)969 int32_t QCamera3GrallocMemory::markFrameNumber(uint32_t index, uint32_t frameNumber)
970 {
971 Mutex::Autolock lock(mLock);
972
973 if (index >= MM_CAMERA_MAX_NUM_FRAMES) {
974 LOGE("Index out of bounds");
975 return BAD_INDEX;
976 }
977 if (index < mStartIdx) {
978 LOGE("buffer index %d less than starting index %d",
979 index, mStartIdx);
980 return BAD_INDEX;
981 }
982
983 if (0 == mMemInfo[index].handle) {
984 LOGE("Buffer at %d not registered", index);
985 return BAD_INDEX;
986 }
987
988 mCurrentFrameNumbers[index] = (int32_t)frameNumber;
989
990 return NO_ERROR;
991 }
992
993 /*===========================================================================
994 * FUNCTION : getFrameNumber
995 *
996 * DESCRIPTION: We use this to fetch the frameNumber for the request with which
997 * this buffer was given to HAL
998 *
999 *
1000 * PARAMETERS :
1001 * @index : index of the buffer
1002 *
1003 * RETURN : int32_t frameNumber
1004 * positive/zero -- success
1005 * negative failure
1006 *==========================================================================*/
getFrameNumber(uint32_t index)1007 int32_t QCamera3GrallocMemory::getFrameNumber(uint32_t index)
1008 {
1009 Mutex::Autolock lock(mLock);
1010
1011 if (index >= MM_CAMERA_MAX_NUM_FRAMES) {
1012 LOGE("Index out of bounds");
1013 return -1;
1014 }
1015 if (index < mStartIdx) {
1016 LOGE("buffer index %d less than starting index %d",
1017 index, mStartIdx);
1018 return BAD_INDEX;
1019 }
1020
1021 if (0 == mMemInfo[index].handle) {
1022 LOGE("Buffer at %d not registered", index);
1023 return -1;
1024 }
1025
1026 return mCurrentFrameNumbers[index];
1027 }
1028
1029 /*===========================================================================
1030 * FUNCTION : getOldestFrameNumber
1031 *
1032 * DESCRIPTION: We use this to fetch the oldest frame number expected per FIFO
1033 *
1034 *
1035 * PARAMETERS :
1036 *
1037 *
1038 * RETURN : int32_t frameNumber
1039 * negative failure
1040 *==========================================================================*/
getOldestFrameNumber(uint32_t & bufIndex)1041 int32_t QCamera3GrallocMemory::getOldestFrameNumber(uint32_t &bufIndex)
1042 {
1043 int32_t oldest = INT_MAX;
1044 bool empty = true;
1045 for (uint32_t index = mStartIdx;
1046 index < MM_CAMERA_MAX_NUM_FRAMES; index++) {
1047 if (mMemInfo[index].handle) {
1048 if ((empty) ||
1049 (!empty && oldest > mCurrentFrameNumbers[index]
1050 && mCurrentFrameNumbers[index] != -1)) {
1051 oldest = mCurrentFrameNumbers[index];
1052 bufIndex = index;
1053 }
1054 empty = false;
1055 }
1056 }
1057 if (empty)
1058 return -1;
1059 else
1060 return oldest;
1061 }
1062
1063
1064 /*===========================================================================
1065 * FUNCTION : getBufferIndex
1066 *
1067 * DESCRIPTION: We use this to fetch the buffer index for the request with
1068 * a particular frame number
1069 *
1070 *
1071 * PARAMETERS :
1072 * @frameNumber : frame number of the buffer
1073 *
1074 * RETURN : int32_t buffer index
1075 * negative failure
1076 *==========================================================================*/
getBufferIndex(uint32_t frameNumber)1077 int32_t QCamera3GrallocMemory::getBufferIndex(uint32_t frameNumber)
1078 {
1079 for (uint32_t index = mStartIdx;
1080 index < MM_CAMERA_MAX_NUM_FRAMES; index++) {
1081 if (mMemInfo[index].handle &&
1082 mCurrentFrameNumbers[index] == (int32_t)frameNumber)
1083 return (int32_t)index;
1084 }
1085 return -1;
1086 }
1087
1088 /*===========================================================================
1089 * FUNCTION : cacheOps
1090 *
1091 * DESCRIPTION: ion related memory cache operations
1092 *
1093 * PARAMETERS :
1094 * @index : index of the buffer
1095 * @cmd : cache ops command
1096 *
1097 * RETURN : int32_t type of status
1098 * NO_ERROR -- success
1099 * none-zero failure code
1100 *==========================================================================*/
cacheOps(uint32_t index,unsigned int cmd)1101 int QCamera3GrallocMemory::cacheOps(uint32_t index, unsigned int cmd)
1102 {
1103 int rc = 0;
1104 bool needToInvalidate = false;
1105 struct private_handle_t *privateHandle = NULL;
1106 privateHandle = mPrivateHandle[index];
1107
1108 if (privateHandle != NULL){
1109 if(privateHandle->flags &
1110 (private_handle_t::PRIV_FLAGS_NON_CPU_WRITER)){
1111 needToInvalidate = true;
1112 }
1113 }
1114
1115 if (index >= MM_CAMERA_MAX_NUM_FRAMES) {
1116 LOGE("Index out of bounds");
1117 return -1;
1118 }
1119 if (index < mStartIdx) {
1120 LOGE("buffer index %d less than starting index %d",
1121 index, mStartIdx);
1122 return BAD_INDEX;
1123 }
1124
1125 LOGD("needToInvalidate %d buf idx %d", needToInvalidate, index);
1126 if(((cmd == ION_IOC_INV_CACHES) || (cmd == ION_IOC_CLEAN_INV_CACHES))
1127 && needToInvalidate) {
1128 rc = cacheOpsInternal(index, cmd, mPtr[index]);
1129 }
1130 else if(cmd == ION_IOC_CLEAN_CACHES) {
1131 rc = cacheOpsInternal(index, cmd, mPtr[index]);
1132 }
1133 return rc;
1134 }
1135
1136 /*===========================================================================
1137 * FUNCTION : getMatchBufIndex
1138 *
1139 * DESCRIPTION: query buffer index by object ptr
1140 *
1141 * PARAMETERS :
1142 * @opaque : opaque ptr
1143 *
1144 * RETURN : buffer index if match found,
1145 * -1 if failed
1146 *==========================================================================*/
getMatchBufIndex(void * object)1147 int QCamera3GrallocMemory::getMatchBufIndex(void *object)
1148 {
1149 Mutex::Autolock lock(mLock);
1150
1151 int index = -1;
1152 buffer_handle_t *key = (buffer_handle_t*) object;
1153 if (!key) {
1154 return BAD_VALUE;
1155 }
1156 for (uint32_t i = mStartIdx; i < MM_CAMERA_MAX_NUM_FRAMES; i++) {
1157 if (mBufferHandle[i] == key) {
1158 index = (int)i;
1159 break;
1160 }
1161 }
1162
1163 return index;
1164 }
1165
1166 /*===========================================================================
1167 * FUNCTION : getFreeIndexLocked
1168 *
1169 * DESCRIPTION: Find free index slot. Note 'mLock' needs to be acquired
1170 * before calling this method.
1171 *
1172 * PARAMETERS : None
1173 *
1174 * RETURN : free buffer index if found,
1175 * -1 if failed
1176 *==========================================================================*/
getFreeIndexLocked()1177 int QCamera3GrallocMemory::getFreeIndexLocked()
1178 {
1179 int index = -1;
1180
1181 if (mBufferCount >= (MM_CAMERA_MAX_NUM_FRAMES - 1)) {
1182 LOGE("Number of buffers %d greater than what's supported %d",
1183 mBufferCount, MM_CAMERA_MAX_NUM_FRAMES);
1184 return index;
1185 }
1186
1187 for (size_t i = mStartIdx; i < MM_CAMERA_MAX_NUM_FRAMES; i++) {
1188 if (0 == mMemInfo[i].handle) {
1189 index = i;
1190 break;
1191 }
1192 }
1193
1194 return index;
1195 }
1196
1197 /*===========================================================================
1198 * FUNCTION : getPtrLocked
1199 *
1200 * DESCRIPTION: Return buffer pointer. Please note 'mLock' must be acquired
1201 * before calling this method.
1202 *
1203 * PARAMETERS :
1204 * @index : index of the buffer
1205 *
1206 * RETURN : buffer ptr
1207 *==========================================================================*/
getPtrLocked(uint32_t index)1208 void *QCamera3GrallocMemory::getPtrLocked(uint32_t index)
1209 {
1210 if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
1211 LOGE("index %d out of bound [0, %d)",
1212 index, MM_CAMERA_MAX_NUM_FRAMES);
1213 return NULL;
1214 }
1215 if (index < mStartIdx) {
1216 LOGE("buffer index %d less than starting index %d",
1217 index, mStartIdx);
1218 return NULL;
1219 }
1220
1221
1222 if (0 == mMemInfo[index].handle) {
1223 LOGE("Buffer at %d not registered", index);
1224 return NULL;
1225 }
1226
1227 if (mPtr[index] == nullptr) {
1228 void *vaddr = NULL;
1229 vaddr = mmap(NULL,
1230 mMemInfo[index].size,
1231 PROT_READ | PROT_WRITE,
1232 MAP_SHARED,
1233 mMemInfo[index].fd, 0);
1234
1235 if (vaddr == MAP_FAILED) {
1236 LOGE("mmap failed for buffer index %d, size %d: %s(%d)",
1237 index, mMemInfo[index].size, strerror(errno), errno);
1238 return NULL;
1239 } else {
1240 mPtr[index] = vaddr;
1241 }
1242 }
1243
1244 return mPtr[index];
1245 }
1246
1247 /*===========================================================================
1248 * FUNCTION : getPtr
1249 *
1250 * DESCRIPTION: Return buffer pointer.
1251 *
1252 * PARAMETERS :
1253 * @index : index of the buffer
1254 *
1255 * RETURN : buffer ptr
1256 *==========================================================================*/
getPtr(uint32_t index)1257 void *QCamera3GrallocMemory::getPtr(uint32_t index)
1258 {
1259 Mutex::Autolock lock(mLock);
1260 return getPtrLocked(index);
1261 }
1262
1263 /*===========================================================================
1264 * FUNCTION : getBufferHandle
1265 *
1266 * DESCRIPTION: return framework pointer
1267 *
1268 * PARAMETERS :
1269 * @index : index of the buffer
1270 *
1271 * RETURN : buffer ptr if match found
1272 NULL if failed
1273 *==========================================================================*/
getBufferHandle(uint32_t index)1274 void *QCamera3GrallocMemory::getBufferHandle(uint32_t index)
1275 {
1276 Mutex::Autolock lock(mLock);
1277
1278 if (MM_CAMERA_MAX_NUM_FRAMES <= index) {
1279 LOGE("index %d out of bound [0, %d)",
1280 index, MM_CAMERA_MAX_NUM_FRAMES);
1281 return NULL;
1282 }
1283 if (index < mStartIdx) {
1284 LOGE("buffer index %d less than starting index %d",
1285 index, mStartIdx);
1286 return NULL;
1287 }
1288
1289 if (0 == mMemInfo[index].handle) {
1290 LOGE("Buffer at %d not registered", index);
1291 return NULL;
1292 }
1293
1294 return mBufferHandle[index];
1295 }
1296 }; //namespace qcamera
1297