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#ifndef __QCAMERA3HWI_MEM_H__
31#define __QCAMERA3HWI_MEM_H__
32#include <hardware/camera3.h>
33#include <utils/Mutex.h>
34
35extern "C" {
36#include <sys/types.h>
37#include <linux/msm_ion.h>
38#include <mm_camera_interface.h>
39}
40
41namespace qcamera {
42
43// Base class for all memory types. Abstract.
44class QCamera3Memory {
45
46public:
47    int cleanCache(int index) {return cacheOps(index, ION_IOC_CLEAN_CACHES);}
48    int invalidateCache(int index) {return cacheOps(index, ION_IOC_INV_CACHES);}
49    int cleanInvalidateCache(int index) {return cacheOps(index, ION_IOC_CLEAN_INV_CACHES);}
50    int getFd(int index) const;
51    int getSize(int index) const;
52    int getCnt() const;
53
54    virtual int cacheOps(int index, unsigned int cmd) = 0;
55    virtual int getRegFlags(uint8_t *regFlags) const = 0;
56    virtual int getMatchBufIndex(void *object) = 0;
57    virtual void *getPtr(int index) const= 0;
58
59    QCamera3Memory();
60    virtual ~QCamera3Memory();
61
62    void getBufDef(const cam_frame_len_offset_t &offset,
63                mm_camera_buf_def_t &bufDef, int index) const;
64
65protected:
66    struct QCamera3MemInfo {
67        int fd;
68        int main_ion_fd;
69        struct ion_handle *handle;
70        uint32_t size;
71    };
72
73    int cacheOpsInternal(int index, unsigned int cmd, void *vaddr);
74
75    int mBufferCount;
76    struct QCamera3MemInfo mMemInfo[MM_CAMERA_MAX_NUM_FRAMES];
77    void *mPtr[MM_CAMERA_MAX_NUM_FRAMES];
78};
79
80// Internal heap memory is used for memories used internally
81// They are allocated from /dev/ion. Examples are: capabilities,
82// parameters, metadata, and internal YUV data for jpeg encoding.
83class QCamera3HeapMemory : public QCamera3Memory {
84public:
85    QCamera3HeapMemory();
86    virtual ~QCamera3HeapMemory();
87
88    int allocate(int count, int size, bool queueAll);
89    void deallocate();
90
91    virtual int cacheOps(int index, unsigned int cmd);
92    virtual int getRegFlags(uint8_t *regFlags) const;
93    virtual int getMatchBufIndex(void *object);
94    virtual void *getPtr(int index) const;
95private:
96    int alloc(int count, int size, int heap_id);
97    void dealloc();
98
99    int allocOneBuffer(struct QCamera3MemInfo &memInfo, int heap_id, int size);
100    void deallocOneBuffer(struct QCamera3MemInfo &memInfo);
101    bool mQueueAll;
102};
103
104// Gralloc Memory shared with frameworks
105class QCamera3GrallocMemory : public QCamera3Memory {
106public:
107    QCamera3GrallocMemory();
108    virtual ~QCamera3GrallocMemory();
109
110    int registerBuffers(uint32_t num_buffers, buffer_handle_t **buffers);
111    void unregisterBuffers();
112    virtual int cacheOps(int index, unsigned int cmd);
113    virtual int getRegFlags(uint8_t *regFlags) const;
114    virtual int getMatchBufIndex(void *object);
115    virtual void *getPtr(int index) const;
116    int32_t markFrameNumber(int index, uint32_t frameNumber);
117    int32_t getFrameNumber(int index);
118
119private:
120    buffer_handle_t *mBufferHandle[MM_CAMERA_MAX_NUM_FRAMES];
121    struct private_handle_t *mPrivateHandle[MM_CAMERA_MAX_NUM_FRAMES];
122    uint32_t mCurrentFrameNumbers[MM_CAMERA_MAX_NUM_FRAMES];
123};
124
125};
126#endif
127