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 "QCamera3StreamMem"
31
32 // System dependencies
33 #include "gralloc_priv.h"
34
35 // Camera dependencies
36 #include "QCamera3StreamMem.h"
37
38 using namespace android;
39
40 namespace qcamera {
41
42 /*===========================================================================
43 * FUNCTION : QCamera3StreamMem
44 *
45 * DESCRIPTION: default constructor of QCamera3StreamMem
46 *
47 * PARAMETERS : none
48 *
49 * RETURN : None
50 *==========================================================================*/
QCamera3StreamMem(uint32_t maxHeapBuffer)51 QCamera3StreamMem::QCamera3StreamMem(uint32_t maxHeapBuffer) :
52 mHeapMem(maxHeapBuffer),
53 mGrallocMem(maxHeapBuffer),
54 mMaxHeapBuffers(maxHeapBuffer)
55 {
56 }
57
58 /*===========================================================================
59 * FUNCTION : QCamera3StreamMem
60 *
61 * DESCRIPTION: destructor of QCamera3StreamMem
62 *
63 * PARAMETERS : none
64 *
65 * RETURN : None
66 *==========================================================================*/
~QCamera3StreamMem()67 QCamera3StreamMem::~QCamera3StreamMem()
68 {
69 clear();
70 }
71
72 /*===========================================================================
73 * FUNCTION : getCnt
74 *
75 * DESCRIPTION: query number of buffers allocated/registered
76 *
77 * PARAMETERS : none
78 *
79 * RETURN : number of buffers allocated
80 *==========================================================================*/
getCnt()81 uint32_t QCamera3StreamMem::getCnt()
82 {
83 Mutex::Autolock lock(mLock);
84
85 return (mHeapMem.getCnt() + mGrallocMem.getCnt());
86 }
87
88 /*===========================================================================
89 * FUNCTION : getRegFlags
90 *
91 * DESCRIPTION: query initial reg flags
92 *
93 * PARAMETERS :
94 * @regFlags: initial reg flags of the allocated/registered buffers
95 *
96 * RETURN : int32_t type of status
97 * NO_ERROR -- success
98 * none-zero failure code
99 *==========================================================================*/
getRegFlags(uint8_t * regFlags)100 int QCamera3StreamMem::getRegFlags(uint8_t * regFlags)
101 {
102 // Queue all Heap buffers that are allocated.
103 for (uint32_t i = 0; i < mHeapMem.getCnt(); i ++) {
104 regFlags[i] = 1;
105 }
106 // Queue all gralloc buffers that are registered.
107 for (uint32_t i = 0; i < mGrallocMem.getCnt(); i++) {
108 regFlags[mMaxHeapBuffers+i] = 1;
109 }
110 return NO_ERROR;
111 }
112
113 /*===========================================================================
114 * FUNCTION : getFd
115 *
116 * DESCRIPTION: return file descriptor of the indexed buffer
117 *
118 * PARAMETERS :
119 * @index : index of the buffer
120 *
121 * RETURN : file descriptor
122 *==========================================================================*/
getFd(uint32_t index)123 int QCamera3StreamMem::getFd(uint32_t index)
124 {
125 Mutex::Autolock lock(mLock);
126
127 if (index < mMaxHeapBuffers)
128 return mHeapMem.getFd(index);
129 else
130 return mGrallocMem.getFd(index);
131 }
132
133 /*===========================================================================
134 * FUNCTION : getSize
135 *
136 * DESCRIPTION: return buffer size of the indexed buffer
137 *
138 * PARAMETERS :
139 * @index : index of the buffer
140 *
141 * RETURN : buffer size
142 *==========================================================================*/
getSize(uint32_t index)143 ssize_t QCamera3StreamMem::getSize(uint32_t index)
144 {
145 Mutex::Autolock lock(mLock);
146
147 if (index < mMaxHeapBuffers)
148 return mHeapMem.getSize(index);
149 else
150 return mGrallocMem.getSize(index);
151 }
152
153 /*===========================================================================
154 * FUNCTION : invalidateCache
155 *
156 * DESCRIPTION: invalidate the cache of the indexed buffer
157 *
158 * PARAMETERS :
159 * @index : index of the buffer
160 *
161 * RETURN : int32_t type of status
162 * NO_ERROR -- success
163 * none-zero failure code
164 *==========================================================================*/
invalidateCache(uint32_t index)165 int QCamera3StreamMem::invalidateCache(uint32_t index)
166 {
167 Mutex::Autolock lock(mLock);
168
169 if (index < mMaxHeapBuffers)
170 return mHeapMem.invalidateCache(index);
171 else
172 return mGrallocMem.invalidateCache(index);
173 }
174
175 /*===========================================================================
176 * FUNCTION : cleanInvalidateCache
177 *
178 * DESCRIPTION: clean and invalidate the cache of the indexed buffer
179 *
180 * PARAMETERS :
181 * @index : index of the buffer
182 *
183 * RETURN : int32_t type of status
184 * NO_ERROR -- success
185 * none-zero failure code
186 *==========================================================================*/
cleanInvalidateCache(uint32_t index)187 int QCamera3StreamMem::cleanInvalidateCache(uint32_t index)
188 {
189 Mutex::Autolock lock(mLock);
190
191 if (index < mMaxHeapBuffers)
192 return mHeapMem.cleanInvalidateCache(index);
193 else
194 return mGrallocMem.cleanInvalidateCache(index);
195 }
196
197 /*===========================================================================
198 * FUNCTION : cleanCache
199 *
200 * DESCRIPTION: clean the cache of the indexed buffer
201 *
202 * PARAMETERS :
203 * @index : index of the buffer
204 *
205 * RETURN : int32_t type of status
206 * NO_ERROR -- success
207 * none-zero failure code
208 *==========================================================================*/
cleanCache(uint32_t index)209 int QCamera3StreamMem::cleanCache(uint32_t index)
210 {
211 Mutex::Autolock lock(mLock);
212
213 if (index < mMaxHeapBuffers)
214 return mHeapMem.cleanCache(index);
215 else
216 return mGrallocMem.cleanCache(index);
217 }
218
219
220 /*===========================================================================
221 * FUNCTION : getBufDef
222 *
223 * DESCRIPTION: query detailed buffer information
224 *
225 * PARAMETERS :
226 * @offset : [input] frame buffer offset
227 * @bufDef : [output] reference to struct to store buffer definition
228 * @index : [input] index of the buffer
229 * @virtualAddr : [input] whether to fill out virtual address
230 *
231 * RETURN : int32_t type of status
232 * NO_ERROR -- success
233 * none-zero failure code
234 *==========================================================================*/
getBufDef(const cam_frame_len_offset_t & offset,mm_camera_buf_def_t & bufDef,uint32_t index,bool virtualAddr)235 int32_t QCamera3StreamMem::getBufDef(const cam_frame_len_offset_t &offset,
236 mm_camera_buf_def_t &bufDef, uint32_t index, bool virtualAddr)
237 {
238 int32_t ret = NO_ERROR;
239
240 if (index < mMaxHeapBuffers)
241 ret = mHeapMem.getBufDef(offset, bufDef, index, virtualAddr);
242 else
243 ret = mGrallocMem.getBufDef(offset, bufDef, index, virtualAddr);
244
245 bufDef.mem_info = (void *)this;
246
247 return ret;
248 }
249
250 /*===========================================================================
251 * FUNCTION : getPtr
252 *
253 * DESCRIPTION: return virtual address of the indexed buffer
254 *
255 * PARAMETERS :
256 * @index : index of the buffer
257 *
258 * RETURN : virtual address
259 *==========================================================================*/
getPtr(uint32_t index)260 void* QCamera3StreamMem::getPtr(uint32_t index)
261 {
262 Mutex::Autolock lock(mLock);
263
264 if (index < mMaxHeapBuffers)
265 return mHeapMem.getPtr(index);
266 else
267 return mGrallocMem.getPtr(index);
268 }
269
270 /*===========================================================================
271 * FUNCTION : valid
272 *
273 * DESCRIPTION: return whether there is a valid buffer at the current index
274 *
275 * PARAMETERS :
276 * @index : index of the buffer
277 *
278 * RETURN : true if there is a buffer, false otherwise
279 *==========================================================================*/
valid(uint32_t index)280 bool QCamera3StreamMem::valid(uint32_t index)
281 {
282 Mutex::Autolock lock(mLock);
283
284 if (index < mMaxHeapBuffers)
285 return (mHeapMem.getSize(index) > 0);
286 else
287 return (mGrallocMem.getSize(index) > 0);
288 }
289
290 /*===========================================================================
291 * FUNCTION : registerBuffer
292 *
293 * DESCRIPTION: registers frameworks-allocated gralloc buffer_handle_t
294 *
295 * PARAMETERS :
296 * @buffers : buffer_handle_t pointer
297 * @type : cam_stream_type_t
298 *
299 * RETURN : int32_t type of status
300 * NO_ERROR -- success
301 * none-zero failure code
302 *==========================================================================*/
registerBuffer(buffer_handle_t * buffer,cam_stream_type_t type)303 int QCamera3StreamMem::registerBuffer(buffer_handle_t *buffer,
304 cam_stream_type_t type)
305 {
306 Mutex::Autolock lock(mLock);
307 return mGrallocMem.registerBuffer(buffer, type);
308 }
309
310
311 /*===========================================================================
312 * FUNCTION : unregisterBuffer
313 *
314 * DESCRIPTION: unregister buffer
315 *
316 * PARAMETERS :
317 * @idx : unregister buffer at index 'idx'
318 *
319 * RETURN : int32_t type of status
320 * NO_ERROR -- success
321 * none-zero failure code
322 *==========================================================================*/
unregisterBuffer(size_t idx)323 int32_t QCamera3StreamMem::unregisterBuffer(size_t idx)
324 {
325 Mutex::Autolock lock(mLock);
326 return mGrallocMem.unregisterBuffer(idx);
327 }
328
329 /*===========================================================================
330 * FUNCTION : getMatchBufIndex
331 *
332 * DESCRIPTION: query buffer index by object ptr
333 *
334 * PARAMETERS :
335 * @opaque : opaque ptr
336 *
337 * RETURN : buffer index if match found,
338 * -1 if failed
339 *==========================================================================*/
getMatchBufIndex(void * object)340 int QCamera3StreamMem::getMatchBufIndex(void *object)
341 {
342 Mutex::Autolock lock(mLock);
343 return mGrallocMem.getMatchBufIndex(object);
344 }
345
346 /*===========================================================================
347 * FUNCTION : getBufferHandle
348 *
349 * DESCRIPTION: return framework pointer
350 *
351 * PARAMETERS :
352 * @index : index of the buffer
353 *
354 * RETURN : buffer ptr if match found
355 NULL if failed
356 *==========================================================================*/
getBufferHandle(uint32_t index)357 void *QCamera3StreamMem::getBufferHandle(uint32_t index)
358 {
359 Mutex::Autolock lock(mLock);
360 return mGrallocMem.getBufferHandle(index);
361 }
362
363 /*===========================================================================
364 * FUNCTION : unregisterBuffers
365 *
366 * DESCRIPTION: unregister buffers
367 *
368 * PARAMETERS : none
369 *
370 * RETURN : none
371 *==========================================================================*/
unregisterBuffers()372 void QCamera3StreamMem::unregisterBuffers()
373 {
374 Mutex::Autolock lock(mLock);
375 mGrallocMem.unregisterBuffers();
376 }
377
378
379 /*===========================================================================
380 * FUNCTION : allocate
381 *
382 * DESCRIPTION: allocate requested number of buffers of certain size
383 *
384 * PARAMETERS :
385 * @count : number of buffers to be allocated
386 * @size : lenght of the buffer to be allocated
387 *
388 * RETURN : int32_t type of status
389 * NO_ERROR -- success
390 * none-zero failure code
391 *==========================================================================*/
allocateAll(size_t size)392 int QCamera3StreamMem::allocateAll(size_t size)
393 {
394 Mutex::Autolock lock(mLock);
395 return mHeapMem.allocate(size);
396 }
397
allocateOne(size_t size,bool isCached)398 int QCamera3StreamMem::allocateOne(size_t size, bool isCached)
399 {
400 Mutex::Autolock lock(mLock);
401 return mHeapMem.allocateOne(size, isCached);
402 }
403
404 /*===========================================================================
405 * FUNCTION : deallocate
406 *
407 * DESCRIPTION: deallocate heap buffers
408 *
409 * PARAMETERS : none
410 *
411 * RETURN : none
412 *==========================================================================*/
deallocate()413 void QCamera3StreamMem::deallocate()
414 {
415 Mutex::Autolock lock(mLock);
416 mHeapMem.deallocate();
417 }
418
419 /*===========================================================================
420 * FUNCTION : markFrameNumber
421 *
422 * DESCRIPTION: We use this function from the request call path to mark the
423 * buffers with the frame number they are intended for this info
424 * is used later when giving out callback & it is duty of PP to
425 * ensure that data for that particular frameNumber/Request is
426 * written to this buffer.
427 * PARAMETERS :
428 * @index : index of the buffer
429 * @frame# : Frame number from the framework
430 *
431 * RETURN : int32_t type of status
432 * NO_ERROR -- success
433 * none-zero failure code
434 *==========================================================================*/
markFrameNumber(uint32_t index,uint32_t frameNumber)435 int32_t QCamera3StreamMem::markFrameNumber(uint32_t index, uint32_t frameNumber)
436 {
437 Mutex::Autolock lock(mLock);
438 if (index < mMaxHeapBuffers)
439 return mHeapMem.markFrameNumber(index, frameNumber);
440 else
441 return mGrallocMem.markFrameNumber(index, frameNumber);
442 }
443
444 /*===========================================================================
445 * FUNCTION : getOldestFrameNumber
446 *
447 * DESCRIPTION: We use this to fetch the frameNumber expected as per FIFO
448 *
449 *
450 * PARAMETERS :
451 * @index : index of the buffer
452 *
453 * RETURN : int32_t frameNumber
454 * positive/zero -- success
455 * negative failure
456 *==========================================================================*/
getOldestFrameNumber(uint32_t & bufIdx)457 int32_t QCamera3StreamMem::getOldestFrameNumber(uint32_t &bufIdx)
458 {
459 Mutex::Autolock lock(mLock);
460 int32_t oldest = INT_MAX;
461 bool empty = true;
462 if (mHeapMem.getCnt()){
463 empty = false;
464 oldest = mHeapMem.getOldestFrameNumber(bufIdx);
465 }
466
467 if (mGrallocMem.getCnt()) {
468 uint32_t grallocBufIdx;
469 int32_t oldestGrallocFrameNumber = mGrallocMem.getOldestFrameNumber(grallocBufIdx);
470
471 if (empty || (!empty && (oldestGrallocFrameNumber < oldest))){
472 oldest = oldestGrallocFrameNumber;
473 bufIdx = grallocBufIdx;
474 }
475 empty = false;
476 }
477
478 if (empty )
479 return -1;
480 else
481 return oldest;
482 }
483
484
485 /*===========================================================================
486 * FUNCTION : getFrameNumber
487 *
488 * DESCRIPTION: We use this to fetch the frameNumber for the request with which
489 * this buffer was given to HAL
490 *
491 *
492 * PARAMETERS :
493 * @index : index of the buffer
494 *
495 * RETURN : int32_t frameNumber
496 * positive/zero -- success
497 * negative failure
498 *==========================================================================*/
getFrameNumber(uint32_t index)499 int32_t QCamera3StreamMem::getFrameNumber(uint32_t index)
500 {
501 Mutex::Autolock lock(mLock);
502 if (index < mMaxHeapBuffers)
503 return mHeapMem.getFrameNumber(index);
504 else
505 return mGrallocMem.getFrameNumber(index);
506 }
507
508 /*===========================================================================
509 * FUNCTION : getGrallocBufferIndex
510 *
511 * DESCRIPTION: We use this to fetch the gralloc buffer index based on frameNumber
512 *
513 * PARAMETERS :
514 * @frameNumber : frame Number
515 *
516 * RETURN : int32_t buffer index
517 * positive/zero -- success
518 * negative failure
519 *==========================================================================*/
getGrallocBufferIndex(uint32_t frameNumber)520 int32_t QCamera3StreamMem::getGrallocBufferIndex(uint32_t frameNumber)
521 {
522 Mutex::Autolock lock(mLock);
523 int32_t index = mGrallocMem.getBufferIndex(frameNumber);
524 return index;
525 }
526
527 /*===========================================================================
528 * FUNCTION : getHeapBufferIndex
529 *
530 * DESCRIPTION: We use this to fetch the heap buffer index based on frameNumber
531 *
532 * PARAMETERS :
533 * @frameNumber : frame Number
534 *
535 * RETURN : int32_t buffer index
536 * positive/zero -- success
537 * negative failure
538 *==========================================================================*/
getHeapBufferIndex(uint32_t frameNumber)539 int32_t QCamera3StreamMem::getHeapBufferIndex(uint32_t frameNumber)
540 {
541 Mutex::Autolock lock(mLock);
542 int32_t index = mHeapMem.getBufferIndex(frameNumber);
543 return index;
544 }
545
546
547 /*===========================================================================
548 * FUNCTION : getBufferIndex
549 *
550 * DESCRIPTION: We use this to fetch the buffer index based on frameNumber
551 *
552 * PARAMETERS :
553 * @frameNumber : frame Number
554 *
555 * RETURN : int32_t buffer index
556 * positive/zero -- success
557 * negative failure
558 *==========================================================================*/
getBufferIndex(uint32_t frameNumber)559 int32_t QCamera3StreamMem::getBufferIndex(uint32_t frameNumber)
560 {
561 Mutex::Autolock lock(mLock);
562 int32_t index = mGrallocMem.getBufferIndex(frameNumber);
563
564 if (index < 0)
565 return mHeapMem.getBufferIndex(frameNumber);
566 else
567 return index;
568 }
569
570
571
572 }; //namespace qcamera
573