Home | History | Annotate | Download | only in src
      1 /*
      2 ** Copyright (c) 2011-2012 The Linux Foundation. All rights reserved.
      3 **
      4 ** Licensed under the Apache License, Version 2.0 (the "License");
      5 ** you may not use this file except in compliance with the License.
      6 ** You may obtain a copy of the License at
      7 **
      8 **     http://www.apache.org/licenses/LICENSE-2.0
      9 **
     10 ** Unless required by applicable law or agreed to in writing, software
     11 ** distributed under the License is distributed on an "AS IS" BASIS,
     12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 ** See the License for the specific language governing permissions and
     14 ** limitations under the License.
     15 */
     16 
     17 /*#error uncomment this for compiler test!*/
     18 
     19 //#define LOG_NDEBUG 0
     20 #define LOG_NIDEBUG 0
     21 #define LOG_TAG "QCameraHWI_Mem"
     22 #include <utils/Log.h>
     23 
     24 #include <utils/Errors.h>
     25 #include <utils/threads.h>
     26 #include <binder/MemoryHeapPmem.h>
     27 #include <utils/String16.h>
     28 #include <sys/types.h>
     29 #include <sys/stat.h>
     30 #include <unistd.h>
     31 #include <fcntl.h>
     32 #include <cutils/properties.h>
     33 #include <math.h>
     34 #if HAVE_ANDROID_OS
     35 #include <linux/android_pmem.h>
     36 #endif
     37 #include <linux/ioctl.h>
     38 #include <camera/CameraParameters.h>
     39 #include <media/mediarecorder.h>
     40 #include <gralloc_priv.h>
     41 
     42 #include "QCameraHWI_Mem.h"
     43 
     44 #define CAMERA_HAL_UNUSED(expr) do { (void)(expr); } while (0)
     45 
     46 /* QCameraHardwareInterface class implementation goes here*/
     47 /* following code implement the contol logic of this class*/
     48 
     49 namespace android {
     50 
     51 
     52 static bool register_buf(int size,
     53                          int frame_size,
     54                          int cbcr_offset,
     55                          int yoffset,
     56                          int pmempreviewfd,
     57                          uint32_t offset,
     58                          uint8_t *buf,
     59                          int pmem_type,
     60                          bool vfe_can_write,
     61                          bool register_buffer = true);
     62 
     63 #if 0
     64 MMCameraDL::MMCameraDL(){
     65     LOGV("MMCameraDL: E");
     66     libmmcamera = NULL;
     67 #if DLOPEN_LIBMMCAMERA
     68     libmmcamera = ::dlopen("liboemcamera.so", RTLD_NOW);
     69 #endif
     70     LOGV("Open MM camera DL libeomcamera loaded at %p ", libmmcamera);
     71     LOGV("MMCameraDL: X");
     72 }
     73 
     74 void * MMCameraDL::pointer(){
     75     return libmmcamera;
     76 }
     77 
     78 MMCameraDL::~MMCameraDL(){
     79     LOGV("~MMCameraDL: E");
     80     LINK_mm_camera_destroy();
     81     if (libmmcamera != NULL) {
     82         ::dlclose(libmmcamera);
     83         LOGV("closed MM Camera DL ");
     84     }
     85     libmmcamera = NULL;
     86     LOGV("~MMCameraDL: X");
     87 }
     88 
     89 
     90 wp<MMCameraDL> MMCameraDL::instance;
     91 Mutex MMCameraDL::singletonLock;
     92 
     93 
     94 sp<MMCameraDL> MMCameraDL::getInstance(){
     95     Mutex::Autolock instanceLock(singletonLock);
     96     sp<MMCameraDL> mmCamera = instance.promote();
     97     if(mmCamera == NULL){
     98         mmCamera = new MMCameraDL();
     99         instance = mmCamera;
    100     }
    101     return mmCamera;
    102 }
    103 #endif
    104 
    105 MemPool::MemPool(int buffer_size, int num_buffers,
    106                                          int frame_size,
    107                                          const char *name) :
    108     mBufferSize(buffer_size),
    109     mNumBuffers(num_buffers),
    110     mFrameSize(frame_size),
    111     mBuffers(NULL), mName(name)
    112 {
    113     int page_size_minus_1 = getpagesize() - 1;
    114     mAlignedBufferSize = (buffer_size + page_size_minus_1) & (~page_size_minus_1);
    115 }
    116 
    117 void MemPool::completeInitialization()
    118 {
    119     // If we do not know how big the frame will be, we wait to allocate
    120     // the buffers describing the individual frames until we do know their
    121     // size.
    122 
    123     if (mFrameSize > 0) {
    124         mBuffers = new sp<MemoryBase>[mNumBuffers];
    125         for (int i = 0; i < mNumBuffers; i++) {
    126             mBuffers[i] = new
    127                 MemoryBase(mHeap,
    128                            i * mAlignedBufferSize,
    129                            mFrameSize);
    130         }
    131     }
    132 }
    133 
    134 AshmemPool::AshmemPool(int buffer_size, int num_buffers,
    135                                                int frame_size,
    136                                                const char *name) :
    137     MemPool(buffer_size,
    138                                     num_buffers,
    139                                     frame_size,
    140                                     name)
    141 {
    142     LOGV("constructing MemPool %s backed by ashmem: "
    143          "%d frames @ %d uint8_ts, "
    144          "buffer size %d",
    145          mName,
    146          num_buffers, frame_size, buffer_size);
    147 
    148     int page_mask = getpagesize() - 1;
    149     int ashmem_size = buffer_size * num_buffers;
    150     ashmem_size += page_mask;
    151     ashmem_size &= ~page_mask;
    152 
    153     mHeap = new MemoryHeapBase(ashmem_size);
    154 
    155     completeInitialization();
    156 }
    157 
    158 static bool register_buf(int size,
    159                          int frame_size,
    160                          int cbcr_offset,
    161                          int yoffset,
    162                          int pmempreviewfd,
    163                          uint32_t offset,
    164                          uint8_t *buf,
    165                          int pmem_type,
    166                          bool vfe_can_write,
    167                          bool register_buffer)
    168 {
    169     struct msm_pmem_info pmemBuf;
    170     CAMERA_HAL_UNUSED(frame_size);
    171 
    172     pmemBuf.type     = pmem_type;
    173     pmemBuf.fd       = pmempreviewfd;
    174     pmemBuf.offset   = offset;
    175     pmemBuf.len      = size;
    176     pmemBuf.vaddr    = buf;
    177     pmemBuf.y_off    = yoffset;
    178     pmemBuf.cbcr_off = cbcr_offset;
    179 
    180     pmemBuf.active   = vfe_can_write;
    181 
    182     LOGV("register_buf:  reg = %d buffer = %p",
    183          !register_buffer, buf);
    184     /*TODO*/
    185     /*if(native_start_ops(register_buffer ? CAMERA_OPS_REGISTER_BUFFER :
    186         CAMERA_OPS_UNREGISTER_BUFFER ,(void *)&pmemBuf) < 0) {
    187          LOGE("register_buf: MSM_CAM_IOCTL_(UN)REGISTER_PMEM  error %s",
    188                strerror(errno));
    189          return false;
    190          }*/
    191 
    192     return true;
    193 
    194 }
    195 
    196 #if 0
    197 bool register_record_buffers(bool register_buffer) {
    198     LOGI("%s: (%d) E", __FUNCTION__, register_buffer);
    199     struct msm_pmem_info pmemBuf;
    200 
    201     for (int cnt = 0; cnt < VIDEO_BUFFER_COUNT; ++cnt) {
    202         pmemBuf.type     = MSM_PMEM_VIDEO;
    203         pmemBuf.fd       = mRecordHeap->mHeap->getHeapID();
    204         pmemBuf.offset   = mRecordHeap->mAlignedBufferSize * cnt;
    205         pmemBuf.len      = mRecordHeap->mBufferSize;
    206         pmemBuf.vaddr    = (uint8_t *)mRecordHeap->mHeap->base() + mRecordHeap->mAlignedBufferSize * cnt;
    207         pmemBuf.y_off    = 0;
    208         pmemBuf.cbcr_off = recordframes[0].cbcr_off;
    209         if(register_buffer == true) {
    210             pmemBuf.active   = (cnt<ACTIVE_VIDEO_BUFFERS);
    211             if( (mVpeEnabled) && (cnt == kRecordBufferCount-1)) {
    212                 pmemBuf.type = MSM_PMEM_VIDEO_VPE;
    213                 pmemBuf.active = 1;
    214             }
    215         } else {
    216             pmemBuf.active   = false;
    217         }
    218 
    219         LOGV("register_buf:  reg = %d buffer = %p", !register_buffer,
    220           (void *)pmemBuf.vaddr);
    221         if(native_start_ops(register_buffer ? CAMERA_OPS_REGISTER_BUFFER :
    222                 CAMERA_OPS_UNREGISTER_BUFFER ,(void *)&pmemBuf) < 0) {
    223             LOGE("register_buf: MSM_CAM_IOCTL_(UN)REGISTER_PMEM  error %s",
    224                 strerror(errno));
    225             return false;
    226         }
    227     }
    228     return true;
    229 }
    230 #endif
    231 PmemPool::PmemPool(const char *pmem_pool,
    232                                            int flags,
    233                                            int pmem_type,
    234                                            int buffer_size, int num_buffers,
    235                                            int frame_size, int cbcr_offset,
    236                                            int yOffset, const char *name) :
    237     MemPool(buffer_size,num_buffers,frame_size,name),
    238     mPmemType(pmem_type),
    239     mCbCrOffset(cbcr_offset),
    240     myOffset(yOffset)
    241 {
    242     LOGI("constructing MemPool %s backed by pmem pool %s: "
    243          "%d frames @ %d bytes, buffer size %d",
    244          mName,
    245          pmem_pool, num_buffers, frame_size,
    246          buffer_size);
    247 
    248     //mMMCameraDLRef = MMCameraDL::getInstance();
    249 
    250 
    251     // Make a new mmap'ed heap that can be shared across processes.
    252     // mAlignedBufferSize is already in 4k aligned. (do we need total size necessary to be in power of 2??)
    253     mAlignedSize = mAlignedBufferSize * num_buffers;
    254 
    255     sp<MemoryHeapBase> masterHeap =
    256         new MemoryHeapBase(pmem_pool, mAlignedSize, flags);
    257 
    258     if (masterHeap->getHeapID() < 0) {
    259         LOGE("failed to construct master heap for pmem pool %s", pmem_pool);
    260         masterHeap.clear();
    261         return;
    262     }
    263 
    264     sp<MemoryHeapPmem> pmemHeap = new MemoryHeapPmem(masterHeap, flags);
    265     if (pmemHeap->getHeapID() >= 0) {
    266         pmemHeap->slap();
    267         masterHeap.clear();
    268         mHeap = pmemHeap;
    269         pmemHeap.clear();
    270 
    271         mFd = mHeap->getHeapID();
    272         if (::ioctl(mFd, PMEM_GET_SIZE, &mSize)) {
    273             LOGE("pmem pool %s ioctl(PMEM_GET_SIZE) error %s (%d)",
    274                  pmem_pool,
    275                  ::strerror(errno), errno);
    276             mHeap.clear();
    277             return;
    278         }
    279 
    280         LOGE("pmem pool %s ioctl(fd = %d, PMEM_GET_SIZE) is %ld",
    281              pmem_pool,
    282              mFd,
    283              mSize.len);
    284         LOGE("mBufferSize=%d, mAlignedBufferSize=%d\n", mBufferSize, mAlignedBufferSize);
    285 
    286 #if 0
    287         // Unregister preview buffers with the camera drivers.  Allow the VFE to write
    288         // to all preview buffers except for the last one.
    289         // Only Register the preview, snapshot and thumbnail buffers with the kernel.
    290         if( (strcmp("postview", mName) != 0) ){
    291             int num_buf = num_buffers;
    292             if(!strcmp("preview", mName)) num_buf = kPreviewBufferCount;
    293             LOGD("num_buffers = %d", num_buf);
    294             for (int cnt = 0; cnt < num_buf; ++cnt) {
    295                 int active = 1;
    296                 if(pmem_type == MSM_PMEM_VIDEO){
    297                      active = (cnt<ACTIVE_VIDEO_BUFFERS);
    298                      //When VPE is enabled, set the last record
    299                      //buffer as active and pmem type as PMEM_VIDEO_VPE
    300                      //as this is a requirement from VPE operation.
    301                      //No need to set this pmem type to VIDEO_VPE while unregistering,
    302                      //because as per camera stack design: "the VPE AXI is also configured
    303                      //when VFE is configured for VIDEO, which is as part of preview
    304                      //initialization/start. So during this VPE AXI config camera stack
    305                      //will lookup the PMEM_VIDEO_VPE buffer and give it as o/p of VPE and
    306                      //change it's type to PMEM_VIDEO".
    307                      if( (mVpeEnabled) && (cnt == kRecordBufferCount-1)) {
    308                          active = 1;
    309                          pmem_type = MSM_PMEM_VIDEO_VPE;
    310                      }
    311                      LOGV(" pmempool creating video buffers : active %d ", active);
    312                 }
    313                 else if (pmem_type == MSM_PMEM_PREVIEW){
    314                     active = (cnt < ACTIVE_PREVIEW_BUFFERS);
    315                 }
    316                 else if ((pmem_type == MSM_PMEM_MAINIMG)
    317                      || (pmem_type == MSM_PMEM_THUMBNAIL)){
    318                     active = (cnt < ACTIVE_ZSL_BUFFERS);
    319                 }
    320                 register_buf(mBufferSize,
    321                          mFrameSize, mCbCrOffset, myOffset,
    322                          mHeap->getHeapID(),
    323                          mAlignedBufferSize * cnt,
    324                          (uint8_t *)mHeap->base() + mAlignedBufferSize * cnt,
    325                          pmem_type,
    326                          active);
    327             }
    328         }
    329 #endif
    330         completeInitialization();
    331     }
    332     else LOGE("pmem pool %s error: could not create master heap!",
    333               pmem_pool);
    334     LOGI("%s: (%s) X ", __FUNCTION__, mName);
    335 }
    336 
    337 PmemPool::~PmemPool()
    338 {
    339     LOGI("%s: %s E", __FUNCTION__, mName);
    340 #if 0
    341     if (mHeap != NULL) {
    342         // Unregister preview buffers with the camera drivers.
    343         //  Only Unregister the preview, snapshot and thumbnail
    344         //  buffers with the kernel.
    345         if( (strcmp("postview", mName) != 0) ){
    346             int num_buffers = mNumBuffers;
    347             if(!strcmp("preview", mName)) num_buffers = PREVIEW_BUFFER_COUNT;
    348             for (int cnt = 0; cnt < num_buffers; ++cnt) {
    349                 register_buf(mBufferSize,
    350                          mFrameSize,
    351                          mCbCrOffset,
    352                          myOffset,
    353                          mHeap->getHeapID(),
    354                          mAlignedBufferSize * cnt,
    355                          (uint8_t *)mHeap->base() + mAlignedBufferSize * cnt,
    356                          mPmemType,
    357                          false,
    358                          false /* unregister */);
    359             }
    360         }
    361     }
    362     mMMCameraDLRef.clear();
    363 #endif
    364     LOGI("%s: %s X", __FUNCTION__, mName);
    365 }
    366 MemPool::~MemPool()
    367 {
    368     LOGV("destroying MemPool %s", mName);
    369     if (mFrameSize > 0)
    370         delete [] mBuffers;
    371     mHeap.clear();
    372     LOGV("destroying MemPool %s completed", mName);
    373 }
    374 
    375 
    376 status_t MemPool::dump(int fd, const Vector<String16>& args) const
    377 {
    378     const size_t SIZE = 256;
    379     char buffer[SIZE];
    380     String8 result;
    381     CAMERA_HAL_UNUSED(args);
    382     snprintf(buffer, 255, "QualcommCameraHardware::AshmemPool::dump\n");
    383     result.append(buffer);
    384     if (mName) {
    385         snprintf(buffer, 255, "mem pool name (%s)\n", mName);
    386         result.append(buffer);
    387     }
    388     if (mHeap != 0) {
    389         snprintf(buffer, 255, "heap base(%p), size(%d), flags(%d), device(%s)\n",
    390                  mHeap->getBase(), mHeap->getSize(),
    391                  mHeap->getFlags(), mHeap->getDevice());
    392         result.append(buffer);
    393     }
    394     snprintf(buffer, 255,
    395              "buffer size (%d), number of buffers (%d), frame size(%d)",
    396              mBufferSize, mNumBuffers, mFrameSize);
    397     result.append(buffer);
    398     write(fd, result.string(), result.size());
    399     return NO_ERROR;
    400 }
    401 
    402 };
    403