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 #ifndef __QCAMERA3_STREAMMEM_H__
31 #define __QCAMERA3_STREAMMEM_H__
32 
33 // System dependencies
34 #include <utils/Mutex.h>
35 #include <hardware/gralloc1.h>
36 // Camera dependencies
37 #include "QCamera3Mem.h"
38 
39 extern "C" {
40 #include "mm_camera_interface.h"
41 }
42 
43 using namespace android;
44 
45 namespace qcamera {
46 
47 class QCamera3StreamMem {
48 public:
49     QCamera3StreamMem(uint32_t maxHeapBuffer);
50     virtual ~QCamera3StreamMem();
51 
52     uint32_t getCnt();
53     int getRegFlags(uint8_t *regFlags);
54 
55     // Helper function to access individual QCamera3Buffer object
56     int getFd(uint32_t index);
57     ssize_t getSize(uint32_t index);
58     int invalidateCache(uint32_t index);
59     int cleanInvalidateCache(uint32_t index);
60     int cleanCache(uint32_t index);
61     int32_t getBufDef(const cam_frame_len_offset_t &offset,
62             mm_camera_buf_def_t &bufDef, uint32_t index,
63             bool virtualAddr);
64     void *getPtr(uint32_t index);
65 
66     bool valid(uint32_t index);
67 
68     // Gralloc buffer related functions
69     int registerBuffer(buffer_handle_t *buffer, cam_stream_type_t type);
70     int unregisterBuffer(uint32_t index);
71     int getMatchBufIndex(void *object);
72     void *getBufferHandle(uint32_t index);
73     void unregisterBuffers(); //TODO: relace with unififed clear() function?
74 
75     // Heap buffer related functions
76     int allocateAll(size_t size);
77     int allocateOne(size_t size, bool isCached = true);
78     void deallocate(); //TODO: replace with unified clear() function?
79 
80     // Clear function: unregister for gralloc buffer, and deallocate for heap buffer
clear()81     void clear() {unregisterBuffers(); deallocate(); }
82 
83     // Frame number getter and setter
84     int32_t markFrameNumber(uint32_t index, uint32_t frameNumber);
85     int32_t getFrameNumber(uint32_t index);
86     int32_t getOldestFrameNumber(uint32_t &index);
87     int32_t getGrallocBufferIndex(uint32_t frameNumber);
88     int32_t getHeapBufferIndex(uint32_t frameNumber);
89     int32_t getBufferIndex(uint32_t frameNumber);
90 
91 private:
92     //variables
93     QCamera3HeapMemory mHeapMem;
94     QCamera3GrallocMemory mGrallocMem;
95     uint32_t mMaxHeapBuffers;
96     Mutex mLock;
97 };
98 
99 /// @brief Gralloc1 interface functions
100 struct Gralloc1Interface
101 {
102     int32_t (*CreateDescriptor)(
103         gralloc1_device_t*             pGralloc1Device,
104         gralloc1_buffer_descriptor_t*  pCreatedDescriptor);
105     int32_t (*DestroyDescriptor)(
106         gralloc1_device_t*            pGralloc1Device,
107         gralloc1_buffer_descriptor_t  descriptor);
108     int32_t (*SetDimensions)(
109         gralloc1_device_t*           pGralloc1Device,
110         gralloc1_buffer_descriptor_t descriptor,
111         uint32_t                       width,
112         uint32_t                       height);
113     int32_t (*SetFormat)(
114         gralloc1_device_t*           pGralloc1Device,
115         gralloc1_buffer_descriptor_t descriptor,
116         int32_t                        format);
117     int32_t (*SetLayerCount)(
118         gralloc1_device_t*           pGralloc1Device,
119         gralloc1_buffer_descriptor_t descriptor,
120         uint32_t                     layerCount);
121     int32_t (*SetProducerUsage)(
122         gralloc1_device_t*           pGralloc1Device,
123         gralloc1_buffer_descriptor_t descriptor,
124         uint64_t                     usage);
125     int32_t (*SetConsumerUsage)(
126         gralloc1_device_t*           pGralloc1Device,
127         gralloc1_buffer_descriptor_t descriptor,
128         uint64_t                     usage);
129     int32_t (*Allocate)(
130         gralloc1_device_t*                  pGralloc1Device,
131         uint32_t                              numDescriptors,
132         const gralloc1_buffer_descriptor_t* pDescriptors,
133         buffer_handle_t*                    pAllocatedBuffers);
134     int32_t (*GetStride)(
135         gralloc1_device_t* pGralloc1Device,
136         buffer_handle_t    buffer,
137         uint32_t*            pStride);
138     int32_t (*Release)(
139         gralloc1_device_t* pGralloc1Device,
140         buffer_handle_t    buffer);
141     int32_t (*Lock)(
142             gralloc1_device_t*      device,
143             buffer_handle_t         buffer,
144             uint64_t                producerUsage,
145             uint64_t                consumerUsage,
146             const gralloc1_rect_t*  accessRegion,
147             void**                  outData,
148             int32_t                 acquireFence);
149 };
150 
151 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
152 /// @brief General native buffer implementation
153 ///
154 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
155 class NativeBufferInterface final
156 {
157 public:
GetInstance()158     static NativeBufferInterface* GetInstance() {
159         static NativeBufferInterface mInstance;
160         return &mInstance;
161     }
162     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
163     /// GetGrallocBufferStride
164     ///
165     /// @brief Get buffer stride from gralloc interface
166     ///
167     /// @param handle Native HAL buffer handle
168     ///
169     /// @return Return the stride size
170     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
171     uint32_t GetGrallocBufferStride(uint32_t width, uint32_t height, uint32_t fmt);
172 private:
173     NativeBufferInterface();
174     ~NativeBufferInterface();
175 
176     gralloc1_device_t* m_pGralloc1Device;
177     Gralloc1Interface  m_grallocInterface;
178 };
179 
180 };
181 #endif // __QCAMERA3_STREAMMEM_H__
182