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