QCameraMem.h revision 6f83d735d8e3b918da42e6b559fcd0efb78133e5
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 __QCAMERA2HWI_MEM_H__
31#define __QCAMERA2HWI_MEM_H__
32
33#include <hardware/camera.h>
34#include <utils/Mutex.h>
35
36extern "C" {
37#include <sys/types.h>
38#include <linux/msm_ion.h>
39#include <mm_camera_interface.h>
40}
41
42namespace qcamera {
43
44// Base class for all memory types. Abstract.
45class QCameraMemory {
46
47public:
48    int cleanCache(int index) {return cacheOps(index, ION_IOC_CLEAN_CACHES);}
49    int invalidateCache(int index) {return cacheOps(index, ION_IOC_INV_CACHES);}
50    int cleanInvalidateCache(int index) {return cacheOps(index, ION_IOC_CLEAN_INV_CACHES);}
51    int getFd(int index) const;
52    int getSize(int index) const;
53    int getCnt() const;
54
55    virtual int allocate(int count, int size) = 0;
56    virtual void deallocate() = 0;
57    virtual int cacheOps(int index, unsigned int cmd) = 0;
58    virtual int getRegFlags(uint8_t *regFlags) const = 0;
59    virtual camera_memory_t *getMemory(int index, bool metadata) const = 0;
60    virtual int getMatchBufIndex(const void *opaque, bool metadata) const = 0;
61    virtual void *getPtr(int index) const= 0;
62
63    QCameraMemory();
64    virtual ~QCameraMemory();
65
66    void getBufDef(const cam_frame_len_offset_t &offset,
67                mm_camera_buf_def_t &bufDef, int index) const;
68
69protected:
70    struct QCameraMemInfo {
71        int fd;
72        int main_ion_fd;
73        struct ion_handle *handle;
74        uint32_t size;
75    };
76
77    int alloc(int count, int size, int heap_id);
78    void dealloc();
79    int allocOneBuffer(struct QCameraMemInfo &memInfo, int heap_id, int size);
80    void deallocOneBuffer(struct QCameraMemInfo &memInfo);
81    int cacheOpsInternal(int index, unsigned int cmd, void *vaddr);
82
83    int mBufferCount;
84    struct QCameraMemInfo mMemInfo[MM_CAMERA_MAX_NUM_FRAMES];
85};
86
87// Internal heap memory is used for memories used internally
88// They are allocated from /dev/ion.
89class QCameraHeapMemory : public QCameraMemory {
90public:
91    QCameraHeapMemory();
92    virtual ~QCameraHeapMemory();
93
94    virtual int allocate(int count, int size);
95    virtual void deallocate();
96    virtual int cacheOps(int index, unsigned int cmd);
97    virtual int getRegFlags(uint8_t *regFlags) const;
98    virtual camera_memory_t *getMemory(int index, bool metadata) const;
99    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
100	virtual void *getPtr(int index) const;
101
102private:
103    void *mPtr[MM_CAMERA_MAX_NUM_FRAMES];
104};
105
106// Externel heap memory is used for memories shared with
107// framework. They are allocated from /dev/ion or gralloc.
108class QCameraStreamMemory : public QCameraMemory {
109public:
110    QCameraStreamMemory(camera_request_memory getMemory);
111    virtual ~QCameraStreamMemory();
112
113    virtual int allocate(int count, int size);
114    virtual void deallocate();
115    virtual int cacheOps(int index, unsigned int cmd);
116    virtual int getRegFlags(uint8_t *regFlags) const;
117    virtual camera_memory_t *getMemory(int index, bool metadata) const;
118    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
119	virtual void *getPtr(int index) const;
120
121protected:
122    camera_request_memory mGetMemory;
123    camera_memory_t *mCameraMemory[MM_CAMERA_MAX_NUM_FRAMES];
124};
125
126// Externel heap memory is used for memories shared with
127// framework. They are allocated from /dev/ion or gralloc.
128class QCameraVideoMemory : public QCameraStreamMemory {
129public:
130    QCameraVideoMemory(camera_request_memory getMemory);
131    virtual ~QCameraVideoMemory();
132
133    virtual int allocate(int count, int size);
134    virtual void deallocate();
135    virtual camera_memory_t *getMemory(int index, bool metadata) const;
136    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
137
138private:
139    camera_memory_t *mMetadata[MM_CAMERA_MAX_NUM_FRAMES];
140};
141;
142
143// Gralloc Memory is acquired from preview window
144class QCameraGrallocMemory : public QCameraMemory {
145    enum {
146        BUFFER_NOT_OWNED,
147        BUFFER_OWNED,
148    };
149public:
150    QCameraGrallocMemory(camera_request_memory getMemory);
151    void setNativeWindow(preview_stream_ops_t *anw);
152    virtual ~QCameraGrallocMemory();
153
154    virtual int allocate(int count, int size);
155    virtual void deallocate();
156    virtual int cacheOps(int index, unsigned int cmd);
157    virtual int getRegFlags(uint8_t *regFlags) const;
158    virtual camera_memory_t *getMemory(int index, bool metadata) const;
159    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
160	virtual void *getPtr(int index) const;
161
162    void setWindowInfo(preview_stream_ops_t *window, int width, int height, int format);
163    // Enqueue/display buffer[index] onto the native window,
164    // and dequeue one buffer from it.
165    // Returns the buffer index of the dequeued buffer.
166    int displayBuffer(int index);
167
168private:
169    buffer_handle_t *mBufferHandle[MM_CAMERA_MAX_NUM_FRAMES];
170    int mLocalFlag[MM_CAMERA_MAX_NUM_FRAMES];
171    struct private_handle_t *mPrivateHandle[MM_CAMERA_MAX_NUM_FRAMES];
172    preview_stream_ops_t *mWindow;
173    int mWidth, mHeight, mFormat;
174    camera_request_memory mGetMemory;
175    camera_memory_t *mCameraMemory[MM_CAMERA_MAX_NUM_FRAMES];
176    int mMinUndequeuedBuffers;
177};
178
179}; // namespace qcamera
180
181#endif /* __QCAMERA2HWI_MEM_H__ */
182