QCameraMem.h revision 744f5403bebb0e27140d16a5df7a341713c24a13
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(bool cached);
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    bool m_bCached;
84    int mBufferCount;
85    struct QCameraMemInfo mMemInfo[MM_CAMERA_MAX_NUM_FRAMES];
86};
87
88// Internal heap memory is used for memories used internally
89// They are allocated from /dev/ion.
90class QCameraHeapMemory : public QCameraMemory {
91public:
92    QCameraHeapMemory(bool cached);
93    virtual ~QCameraHeapMemory();
94
95    virtual int allocate(int count, int size);
96    virtual void deallocate();
97    virtual int cacheOps(int index, unsigned int cmd);
98    virtual int getRegFlags(uint8_t *regFlags) const;
99    virtual camera_memory_t *getMemory(int index, bool metadata) const;
100    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
101	virtual void *getPtr(int index) const;
102
103private:
104    void *mPtr[MM_CAMERA_MAX_NUM_FRAMES];
105};
106
107// Externel heap memory is used for memories shared with
108// framework. They are allocated from /dev/ion or gralloc.
109class QCameraStreamMemory : public QCameraMemory {
110public:
111    QCameraStreamMemory(camera_request_memory getMemory, bool cached);
112    virtual ~QCameraStreamMemory();
113
114    virtual int allocate(int count, int size);
115    virtual void deallocate();
116    virtual int cacheOps(int index, unsigned int cmd);
117    virtual int getRegFlags(uint8_t *regFlags) const;
118    virtual camera_memory_t *getMemory(int index, bool metadata) const;
119    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
120	virtual void *getPtr(int index) const;
121
122protected:
123    camera_request_memory mGetMemory;
124    camera_memory_t *mCameraMemory[MM_CAMERA_MAX_NUM_FRAMES];
125};
126
127// Externel heap memory is used for memories shared with
128// framework. They are allocated from /dev/ion or gralloc.
129class QCameraVideoMemory : public QCameraStreamMemory {
130public:
131    QCameraVideoMemory(camera_request_memory getMemory, bool cached);
132    virtual ~QCameraVideoMemory();
133
134    virtual int allocate(int count, int size);
135    virtual void deallocate();
136    virtual camera_memory_t *getMemory(int index, bool metadata) const;
137    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
138
139private:
140    camera_memory_t *mMetadata[MM_CAMERA_MAX_NUM_FRAMES];
141};
142;
143
144// Gralloc Memory is acquired from preview window
145class QCameraGrallocMemory : public QCameraMemory {
146    enum {
147        BUFFER_NOT_OWNED,
148        BUFFER_OWNED,
149    };
150public:
151    QCameraGrallocMemory(camera_request_memory getMemory);
152    void setNativeWindow(preview_stream_ops_t *anw);
153    virtual ~QCameraGrallocMemory();
154
155    virtual int allocate(int count, int size);
156    virtual void deallocate();
157    virtual int cacheOps(int index, unsigned int cmd);
158    virtual int getRegFlags(uint8_t *regFlags) const;
159    virtual camera_memory_t *getMemory(int index, bool metadata) const;
160    virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
161	virtual void *getPtr(int index) const;
162
163    void setWindowInfo(preview_stream_ops_t *window, int width, int height, int format);
164    // Enqueue/display buffer[index] onto the native window,
165    // and dequeue one buffer from it.
166    // Returns the buffer index of the dequeued buffer.
167    int displayBuffer(int index);
168
169private:
170    buffer_handle_t *mBufferHandle[MM_CAMERA_MAX_NUM_FRAMES];
171    int mLocalFlag[MM_CAMERA_MAX_NUM_FRAMES];
172    struct private_handle_t *mPrivateHandle[MM_CAMERA_MAX_NUM_FRAMES];
173    preview_stream_ops_t *mWindow;
174    int mWidth, mHeight, mFormat;
175    camera_request_memory mGetMemory;
176    camera_memory_t *mCameraMemory[MM_CAMERA_MAX_NUM_FRAMES];
177    int mMinUndequeuedBuffers;
178};
179
180}; // namespace qcamera
181
182#endif /* __QCAMERA2HWI_MEM_H__ */
183