1 /*
2 ** Copyright (c) 2012 The Linux Foundation. All rights reserved.
3 **
4 ** Not a Contribution, Apache license notifications and license are retained
5 ** for attribution purposes only.
6 **
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
10 **
11 **     http://www.apache.org/licenses/LICENSE-2.0
12 **
13 ** Unless required by applicable law or agreed to in writing, software
14 ** distributed under the License is distributed on an "AS IS" BASIS,
15 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ** See the License for the specific language governing permissions and
17 ** limitations under the License.
18 */
19 
20 /*#error uncomment this for compiler test!*/
21 
22 //#define ALOG_NDEBUG 0
23 #define ALOG_NIDEBUG 0
24 #define LOG_TAG "QCameraHWI_Mem"
25 #include <utils/Log.h>
26 
27 #include <utils/Errors.h>
28 #include <utils/threads.h>
29 #include <utils/String16.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <cutils/properties.h>
35 #include <math.h>
36 #if __ANDROID__
37 #include <linux/android_pmem.h>
38 #endif
39 #include <linux/ioctl.h>
40 #include <camera/QCameraParameters.h>
41 #include <media/mediarecorder.h>
42 #include <gralloc_priv.h>
43 
44 #include "QCameraHWI_Mem.h"
45 
46 #define CAMERA_HAL_UNUSED(expr) do { (void)(expr); } while (0)
47 
48 /* QCameraHardwareInterface class implementation goes here*/
49 /* following code implement the contol logic of this class*/
50 
51 namespace android {
52 
53 
54 static bool register_buf(int size,
55                          int frame_size,
56                          int cbcr_offset,
57                          int yoffset,
58                          int pmempreviewfd,
59                          uint32_t offset,
60                          uint8_t *buf,
61                          int pmem_type,
62                          bool vfe_can_write,
63                          bool register_buffer = true);
64 
MemPool(int buffer_size,int num_buffers,int frame_size,const char * name)65 MemPool::MemPool(int buffer_size, int num_buffers,
66                                          int frame_size,
67                                          const char *name) :
68     mBufferSize(buffer_size),
69     mNumBuffers(num_buffers),
70     mFrameSize(frame_size),
71     mBuffers(NULL), mName(name)
72 {
73     int page_size_minus_1 = getpagesize() - 1;
74     mAlignedBufferSize = (buffer_size + page_size_minus_1) & (~page_size_minus_1);
75 }
76 
completeInitialization()77 void MemPool::completeInitialization()
78 {
79     // If we do not know how big the frame will be, we wait to allocate
80     // the buffers describing the individual frames until we do know their
81     // size.
82 
83     if (mFrameSize > 0) {
84         mBuffers = new sp<MemoryBase>[mNumBuffers];
85         for (int i = 0; i < mNumBuffers; i++) {
86             mBuffers[i] = new
87                 MemoryBase(mHeap,
88                            i * mAlignedBufferSize,
89                            mFrameSize);
90         }
91     }
92 }
93 
AshmemPool(int buffer_size,int num_buffers,int frame_size,const char * name)94 AshmemPool::AshmemPool(int buffer_size, int num_buffers,
95                                                int frame_size,
96                                                const char *name) :
97     MemPool(buffer_size,
98                                     num_buffers,
99                                     frame_size,
100                                     name)
101 {
102     ALOGV("constructing MemPool %s backed by ashmem: "
103          "%d frames @ %d uint8_ts, "
104          "buffer size %d",
105          mName,
106          num_buffers, frame_size, buffer_size);
107 
108     int page_mask = getpagesize() - 1;
109     int ashmem_size = buffer_size * num_buffers;
110     ashmem_size += page_mask;
111     ashmem_size &= ~page_mask;
112 
113     mHeap = new MemoryHeapBase(ashmem_size);
114 
115     completeInitialization();
116 }
117 
register_buf(int size,int frame_size,int cbcr_offset,int yoffset,int pmempreviewfd,uint32_t offset,uint8_t * buf,int pmem_type,bool vfe_can_write,bool register_buffer)118 static bool register_buf(int size,
119                          int frame_size,
120                          int cbcr_offset,
121                          int yoffset,
122                          int pmempreviewfd,
123                          uint32_t offset,
124                          uint8_t *buf,
125                          int pmem_type,
126                          bool vfe_can_write,
127                          bool register_buffer)
128 {
129     return true;
130 
131 }
132 
~PmemPool()133 PmemPool::~PmemPool()
134 {
135     ALOGI("%s: %s E", __FUNCTION__, mName);
136 
137     ALOGI("%s: %s X", __FUNCTION__, mName);
138 }
~MemPool()139 MemPool::~MemPool()
140 {
141     ALOGV("destroying MemPool %s", mName);
142     if (mFrameSize > 0)
143         delete [] mBuffers;
144     mHeap.clear();
145     ALOGV("destroying MemPool %s completed", mName);
146 }
147 
148 
dump(int fd,const Vector<String16> & args) const149 status_t MemPool::dump(int fd, const Vector<String16>& args) const
150 {
151     const size_t SIZE = 256;
152     char buffer[SIZE];
153     String8 result;
154     CAMERA_HAL_UNUSED(args);
155     snprintf(buffer, 255, "QualcommCameraHardware::AshmemPool::dump\n");
156     result.append(buffer);
157     if (mName) {
158         snprintf(buffer, 255, "mem pool name (%s)\n", mName);
159         result.append(buffer);
160     }
161     if (mHeap != 0) {
162         snprintf(buffer, 255, "heap base(%p), size(%d), flags(%d), device(%s)\n",
163                  mHeap->getBase(), mHeap->getSize(),
164                  mHeap->getFlags(), mHeap->getDevice());
165         result.append(buffer);
166     }
167     snprintf(buffer, 255,
168              "buffer size (%d), number of buffers (%d), frame size(%d)",
169              mBufferSize, mNumBuffers, mFrameSize);
170     result.append(buffer);
171     write(fd, result.string(), result.size());
172     return NO_ERROR;
173 }
174 
175 };
176