Home | History | Annotate | Download | only in gralloc
      1 /*
      2 * Copyright (C) 2011 The Android Open Source Project
      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 #include <string.h>
     17 #include <pthread.h>
     18 #ifdef HAVE_ANDROID_OS      // just want PAGE_SIZE define
     19 # include <asm/page.h>
     20 #else
     21 # include <sys/user.h>
     22 #endif
     23 #include <cutils/ashmem.h>
     24 #include <unistd.h>
     25 #include <errno.h>
     26 #include <dlfcn.h>
     27 #include <sys/mman.h>
     28 #include "gralloc_cb.h"
     29 #include "HostConnection.h"
     30 #include "glUtils.h"
     31 #include <cutils/log.h>
     32 #include <cutils/properties.h>
     33 
     34 /* Set to 1 or 2 to enable debug traces */
     35 #define DEBUG  0
     36 
     37 #if DEBUG >= 1
     38 #  define D(...)   LOGD(__VA_ARGS__)
     39 #else
     40 #  define D(...)   ((void)0)
     41 #endif
     42 
     43 #if DEBUG >= 2
     44 #  define DD(...)  LOGD(__VA_ARGS__)
     45 #else
     46 #  define DD(...)  ((void)0)
     47 #endif
     48 
     49 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
     50 //
     51 // our private gralloc module structure
     52 //
     53 struct private_module_t {
     54     gralloc_module_t base;
     55 };
     56 
     57 /* If not NULL, this is a pointer to the fallback module.
     58  * This really is gralloc.default, which we'll use if we detect
     59  * that the emulator we're running in does not support GPU emulation.
     60  */
     61 static gralloc_module_t*  sFallback;
     62 static pthread_once_t     sFallbackOnce = PTHREAD_ONCE_INIT;
     63 
     64 static void fallback_init(void);  // forward
     65 
     66 
     67 typedef struct _alloc_list_node {
     68     buffer_handle_t handle;
     69     _alloc_list_node *next;
     70     _alloc_list_node *prev;
     71 } AllocListNode;
     72 
     73 //
     74 // Our gralloc device structure (alloc interface)
     75 //
     76 struct gralloc_device_t {
     77     alloc_device_t  device;
     78 
     79     AllocListNode *allocListHead;    // double linked list of allocated buffers
     80     pthread_mutex_t lock;
     81 };
     82 
     83 //
     84 // Our framebuffer device structure
     85 //
     86 struct fb_device_t {
     87     framebuffer_device_t  device;
     88 };
     89 
     90 static int map_buffer(cb_handle_t *cb, void **vaddr)
     91 {
     92     if (cb->fd < 0 || cb->ashmemSize <= 0) {
     93         return -EINVAL;
     94     }
     95 
     96     void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
     97                       MAP_SHARED, cb->fd, 0);
     98     if (addr == MAP_FAILED) {
     99         return -errno;
    100     }
    101 
    102     cb->ashmemBase = intptr_t(addr);
    103     cb->ashmemBasePid = getpid();
    104 
    105     *vaddr = addr;
    106     return 0;
    107 }
    108 
    109 #define DEFINE_HOST_CONNECTION \
    110     HostConnection *hostCon = HostConnection::get(); \
    111     renderControl_encoder_context_t *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
    112 
    113 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \
    114     HostConnection *hostCon = HostConnection::get(); \
    115     if (!hostCon) { \
    116         LOGE("gralloc: Failed to get host connection\n"); \
    117         return -EIO; \
    118     } \
    119     renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \
    120     if (!rcEnc) { \
    121         LOGE("gralloc: Failed to get renderControl encoder context\n"); \
    122         return -EIO; \
    123     }
    124 
    125 
    126 //
    127 // gralloc device functions (alloc interface)
    128 //
    129 static int gralloc_alloc(alloc_device_t* dev,
    130                          int w, int h, int format, int usage,
    131                          buffer_handle_t* pHandle, int* pStride)
    132 {
    133     D("gralloc_alloc w=%d h=%d usage=0x%x\n", w, h, usage);
    134 
    135     gralloc_device_t *grdev = (gralloc_device_t *)dev;
    136     if (!grdev || !pHandle || !pStride)
    137         return -EINVAL;
    138 
    139     //
    140     // Validate usage: buffer cannot be written both by s/w and h/w access.
    141     //
    142     bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
    143     bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
    144     if (hw_write && sw_write) {
    145         return -EINVAL;
    146     }
    147 
    148     int ashmem_size = 0;
    149     *pStride = 0;
    150     GLenum glFormat = 0;
    151     GLenum glType = 0;
    152 
    153     int bpp = 0;
    154     switch (format) {
    155         case HAL_PIXEL_FORMAT_RGBA_8888:
    156         case HAL_PIXEL_FORMAT_RGBX_8888:
    157         case HAL_PIXEL_FORMAT_BGRA_8888:
    158             bpp = 4;
    159             glFormat = GL_RGBA;
    160             glType = GL_UNSIGNED_BYTE;
    161             break;
    162         case HAL_PIXEL_FORMAT_RGB_888:
    163             bpp = 3;
    164             glFormat = GL_RGB;
    165             glType = GL_UNSIGNED_BYTE;
    166             break;
    167         case HAL_PIXEL_FORMAT_RGB_565:
    168             bpp = 2;
    169             glFormat = GL_RGB;
    170             glType = GL_UNSIGNED_SHORT_5_6_5;
    171             break;
    172         case HAL_PIXEL_FORMAT_RGBA_5551:
    173             bpp = 2;
    174             glFormat = GL_RGB5_A1_OES;
    175             glType = GL_UNSIGNED_SHORT_5_5_5_1;
    176             break;
    177         case HAL_PIXEL_FORMAT_RGBA_4444:
    178             bpp = 2;
    179             glFormat = GL_RGBA4_OES;
    180             glType = GL_UNSIGNED_SHORT_4_4_4_4;
    181             break;
    182 
    183         default:
    184             return -EINVAL;
    185     }
    186 
    187     if (usage & GRALLOC_USAGE_HW_FB) {
    188         // keep space for postCounter
    189         ashmem_size += sizeof(uint32_t);
    190     }
    191 
    192     if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
    193         // keep space for image on guest memory if SW access is needed
    194         int align = 1;
    195         size_t bpr = (w*bpp + (align-1)) & ~(align-1);
    196         ashmem_size += (bpr * h);
    197         *pStride = bpr / bpp;
    198     }
    199 
    200     D("gralloc_alloc ashmem_size=%d, tid %d\n", ashmem_size, gettid());
    201 
    202     //
    203     // Allocate space in ashmem if needed
    204     //
    205     int fd = -1;
    206     if (ashmem_size > 0) {
    207         // round to page size;
    208         ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
    209 
    210         fd = ashmem_create_region("gralloc-buffer", ashmem_size);
    211         if (fd < 0) {
    212             LOGE("gralloc_alloc failed to create ashmem region: %s\n", strerror(errno));
    213             return -errno;
    214         }
    215     }
    216 
    217     cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage,
    218                                       w, h, glFormat, glType);
    219 
    220     if (ashmem_size > 0) {
    221         //
    222         // map ashmem region if exist
    223         //
    224         void *vaddr;
    225         int err = map_buffer(cb, &vaddr);
    226         if (err) {
    227             close(fd);
    228             delete cb;
    229             return err;
    230         }
    231 
    232         cb->setFd(fd);
    233     }
    234 
    235     //
    236     // Allocate ColorBuffer handle on the host (only if h/w access is allowed)
    237     //
    238     if (usage & GRALLOC_USAGE_HW_MASK) {
    239         DEFINE_HOST_CONNECTION;
    240         if (hostCon && rcEnc) {
    241             cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat);
    242             D("Created host ColorBuffer 0x%x\n", cb->hostHandle);
    243         }
    244 
    245         if (!cb->hostHandle) {
    246            // Could not create colorbuffer on host !!!
    247            close(fd);
    248            delete cb;
    249            return -EIO;
    250         }
    251     }
    252 
    253     //
    254     // alloc succeeded - insert the allocated handle to the allocated list
    255     //
    256     AllocListNode *node = new AllocListNode();
    257     pthread_mutex_lock(&grdev->lock);
    258     node->handle = cb;
    259     node->next =  grdev->allocListHead;
    260     node->prev =  NULL;
    261     if (grdev->allocListHead) {
    262         grdev->allocListHead->prev = node;
    263     }
    264     grdev->allocListHead = node;
    265     pthread_mutex_unlock(&grdev->lock);
    266 
    267     *pHandle = cb;
    268     return 0;
    269 }
    270 
    271 static int gralloc_free(alloc_device_t* dev,
    272                         buffer_handle_t handle)
    273 {
    274     const cb_handle_t *cb = (const cb_handle_t *)handle;
    275     if (!cb_handle_t::validate((cb_handle_t*)cb)) {
    276         ERR("gralloc_free: invalid handle");
    277         return -EINVAL;
    278     }
    279 
    280     if (cb->hostHandle != 0) {
    281         DEFINE_AND_VALIDATE_HOST_CONNECTION;
    282         D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
    283         rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
    284     }
    285 
    286     //
    287     // detach and unmap ashmem area if present
    288     //
    289     if (cb->fd > 0) {
    290         if (cb->ashmemSize > 0 && cb->ashmemBase) {
    291             munmap((void *)cb->ashmemBase, cb->ashmemSize);
    292         }
    293         close(cb->fd);
    294     }
    295 
    296     // remove it from the allocated list
    297     gralloc_device_t *grdev = (gralloc_device_t *)dev;
    298     pthread_mutex_lock(&grdev->lock);
    299     AllocListNode *n = grdev->allocListHead;
    300     while( n && n->handle != cb ) {
    301         n = n->next;
    302     }
    303     if (n) {
    304        // buffer found on list - remove it from list
    305        if (n->next) {
    306            n->next->prev = n->prev;
    307        }
    308        if (n->prev) {
    309            n->prev->next = n->next;
    310        }
    311        else {
    312            grdev->allocListHead = n->next;
    313        }
    314 
    315        delete n;
    316     }
    317     pthread_mutex_unlock(&grdev->lock);
    318 
    319     delete cb;
    320 
    321     return 0;
    322 }
    323 
    324 static int gralloc_device_close(struct hw_device_t *dev)
    325 {
    326     gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev);
    327     if (d) {
    328 
    329         // free still allocated buffers
    330         while( d->allocListHead != NULL ) {
    331             gralloc_free(&d->device, d->allocListHead->handle);
    332         }
    333 
    334         // free device
    335         free(d);
    336     }
    337     return 0;
    338 }
    339 
    340 static int fb_compositionComplete(struct framebuffer_device_t* dev)
    341 {
    342     return 0;
    343 }
    344 
    345 //
    346 // Framebuffer device functions
    347 //
    348 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
    349 {
    350     fb_device_t *fbdev = (fb_device_t *)dev;
    351     cb_handle_t *cb = (cb_handle_t *)buffer;
    352 
    353     if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) {
    354         return -EINVAL;
    355     }
    356 
    357     // Make sure we have host connection
    358     DEFINE_AND_VALIDATE_HOST_CONNECTION;
    359 
    360     // increment the post count of the buffer
    361     uint32_t *postCountPtr = (uint32_t *)cb->ashmemBase;
    362     if (!postCountPtr) {
    363         // This should not happen
    364         return -EINVAL;
    365     }
    366     (*postCountPtr)++;
    367 
    368     // send post request to host
    369     rcEnc->rcFBPost(rcEnc, cb->hostHandle);
    370     hostCon->flush();
    371 
    372     return 0;
    373 }
    374 
    375 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
    376         int l, int t, int w, int h)
    377 {
    378     fb_device_t *fbdev = (fb_device_t *)dev;
    379 
    380     if (!fbdev) {
    381         return -EINVAL;
    382     }
    383 
    384     // Make sure we have host connection
    385     DEFINE_AND_VALIDATE_HOST_CONNECTION;
    386 
    387     // send request to host
    388     // TODO: XXX - should be implemented
    389     //rcEnc->rc_XXX
    390 
    391     return 0;
    392 }
    393 
    394 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
    395             int interval)
    396 {
    397     fb_device_t *fbdev = (fb_device_t *)dev;
    398 
    399     if (!fbdev) {
    400         return -EINVAL;
    401     }
    402 
    403     // Make sure we have host connection
    404     DEFINE_AND_VALIDATE_HOST_CONNECTION;
    405 
    406     // send request to host
    407     rcEnc->rcFBSetSwapInterval(rcEnc, interval);
    408     hostCon->flush();
    409 
    410     return 0;
    411 }
    412 
    413 static int fb_close(struct hw_device_t *dev)
    414 {
    415     fb_device_t *fbdev = (fb_device_t *)dev;
    416 
    417     delete fbdev;
    418 
    419     return 0;
    420 }
    421 
    422 
    423 //
    424 // gralloc module functions - refcount + locking interface
    425 //
    426 static int gralloc_register_buffer(gralloc_module_t const* module,
    427                                    buffer_handle_t handle)
    428 {
    429     pthread_once(&sFallbackOnce, fallback_init);
    430     if (sFallback != NULL) {
    431         return sFallback->registerBuffer(sFallback, handle);
    432     }
    433 
    434     D("gralloc_register_buffer(%p) called", handle);
    435 
    436     private_module_t *gr = (private_module_t *)module;
    437     cb_handle_t *cb = (cb_handle_t *)handle;
    438     if (!gr || !cb_handle_t::validate(cb)) {
    439         ERR("gralloc_register_buffer(%p): invalid buffer", cb);
    440         return -EINVAL;
    441     }
    442 
    443     if (cb->hostHandle != 0) {
    444         DEFINE_AND_VALIDATE_HOST_CONNECTION;
    445         D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
    446         rcEnc->rcOpenColorBuffer(rcEnc, cb->hostHandle);
    447     }
    448 
    449     //
    450     // if the color buffer has ashmem region and it is not mapped in this
    451     // process map it now.
    452     //
    453     if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
    454         void *vaddr;
    455         int err = map_buffer(cb, &vaddr);
    456         if (err) {
    457             ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err));
    458             return -err;
    459         }
    460         cb->mappedPid = getpid();
    461     }
    462 
    463     return 0;
    464 }
    465 
    466 static int gralloc_unregister_buffer(gralloc_module_t const* module,
    467                                      buffer_handle_t handle)
    468 {
    469     if (sFallback != NULL) {
    470         return sFallback->unregisterBuffer(sFallback, handle);
    471     }
    472 
    473     private_module_t *gr = (private_module_t *)module;
    474     cb_handle_t *cb = (cb_handle_t *)handle;
    475     if (!gr || !cb_handle_t::validate(cb)) {
    476         ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
    477         return -EINVAL;
    478     }
    479 
    480     if (cb->hostHandle != 0) {
    481         DEFINE_AND_VALIDATE_HOST_CONNECTION;
    482         D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
    483         rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
    484     }
    485 
    486     //
    487     // unmap ashmem region if it was previously mapped in this process
    488     // (through register_buffer)
    489     //
    490     if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
    491         void *vaddr;
    492         int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
    493         if (err) {
    494             ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
    495             return -EINVAL;
    496         }
    497         cb->ashmemBase = NULL;
    498         cb->mappedPid = 0;
    499     }
    500 
    501     D("gralloc_unregister_buffer(%p) done\n", cb);
    502 
    503     return 0;
    504 }
    505 
    506 static int gralloc_lock(gralloc_module_t const* module,
    507                         buffer_handle_t handle, int usage,
    508                         int l, int t, int w, int h,
    509                         void** vaddr)
    510 {
    511     if (sFallback != NULL) {
    512         return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr);
    513     }
    514 
    515     private_module_t *gr = (private_module_t *)module;
    516     cb_handle_t *cb = (cb_handle_t *)handle;
    517     if (!gr || !cb_handle_t::validate(cb)) {
    518         LOGE("gralloc_lock bad handle\n");
    519         return -EINVAL;
    520     }
    521 
    522     // Validate usage,
    523     //   1. cannot be locked for hw access
    524     //   2. lock for either sw read or write.
    525     //   3. locked sw access must match usage during alloc time.
    526     bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
    527     bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
    528     bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE);
    529     bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
    530     bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK));
    531     bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK));
    532 
    533     if ( (hw_read || hw_write) ||
    534          (!sw_read && !sw_write) ||
    535          (sw_read && !sw_read_allowed) ||
    536          (sw_write && !sw_write_allowed) ) {
    537         LOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage, cb->usage);
    538         return -EINVAL;
    539     }
    540 
    541     EGLint postCount = 0;
    542     void *cpu_addr = NULL;
    543 
    544     //
    545     // make sure ashmem area is mapped if needed
    546     //
    547     if (cb->canBePosted() || sw_read || sw_write) {
    548         if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
    549             return -EACCES;
    550         }
    551 
    552         if (cb->canBePosted()) {
    553             postCount = *((int *)cb->ashmemBase);
    554             cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
    555         }
    556         else {
    557             cpu_addr = (void *)(cb->ashmemBase);
    558         }
    559     }
    560 
    561     if (cb->hostHandle) {
    562         // Make sure we have host connection
    563         DEFINE_AND_VALIDATE_HOST_CONNECTION;
    564 
    565         //
    566         // flush color buffer write cache on host and get its sync status.
    567         //
    568         int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
    569                                                             postCount,
    570                                                             sw_read);
    571         if (hostSyncStatus < 0) {
    572             // host failed the color buffer sync - probably since it was already
    573             // locked for write access. fail the lock.
    574             LOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n",
    575                  postCount, sw_read);
    576             return -EBUSY;
    577         }
    578 
    579         //
    580         // is virtual address required ?
    581         //
    582         if (sw_read || sw_write) {
    583             *vaddr = cpu_addr;
    584         }
    585     }
    586 
    587     if (sw_write) {
    588         //
    589         // Keep locked region if locked for s/w write access.
    590         //
    591         cb->lockedLeft = l;
    592         cb->lockedTop = t;
    593         cb->lockedWidth = w;
    594         cb->lockedHeight = h;
    595     }
    596 
    597     return 0;
    598 }
    599 
    600 static int gralloc_unlock(gralloc_module_t const* module,
    601                           buffer_handle_t handle)
    602 {
    603     if (sFallback != NULL) {
    604         return sFallback->unlock(sFallback, handle);
    605     }
    606 
    607     private_module_t *gr = (private_module_t *)module;
    608     cb_handle_t *cb = (cb_handle_t *)handle;
    609     if (!gr || !cb_handle_t::validate(cb)) {
    610         return -EINVAL;
    611     }
    612 
    613     //
    614     // if buffer was locked for s/w write, we need to update the host with
    615     // the updated data
    616     //
    617     if (cb->lockedWidth > 0 && cb->lockedHeight > 0 && cb->hostHandle) {
    618 
    619         // Make sure we have host connection
    620         DEFINE_AND_VALIDATE_HOST_CONNECTION;
    621 
    622         void *cpu_addr;
    623         if (cb->canBePosted()) {
    624             cpu_addr = (void *)(cb->ashmemBase + sizeof(int));
    625         }
    626         else {
    627             cpu_addr = (void *)(cb->ashmemBase);
    628         }
    629 
    630         if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
    631             int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3;
    632             char *tmpBuf = new char[cb->lockedWidth * cb->lockedHeight * bpp];
    633 
    634             int dst_line_len = cb->lockedWidth * bpp;
    635             int src_line_len = cb->width * bpp;
    636             char *src = (char *)cpu_addr + cb->lockedTop*src_line_len + cb->lockedLeft*bpp;
    637             char *dst = tmpBuf;
    638             for (int y=0; y<cb->lockedHeight; y++) {
    639                 memcpy(dst, src, dst_line_len);
    640                 src += src_line_len;
    641                 dst += dst_line_len;
    642             }
    643 
    644             rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
    645                                        cb->lockedLeft, cb->lockedTop,
    646                                        cb->lockedWidth, cb->lockedHeight,
    647                                        cb->glFormat, cb->glType,
    648                                        tmpBuf);
    649 
    650             delete [] tmpBuf;
    651         }
    652         else {
    653             rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0,
    654                                        cb->width, cb->height,
    655                                        cb->glFormat, cb->glType,
    656                                        cpu_addr);
    657         }
    658     }
    659 
    660     cb->lockedWidth = cb->lockedHeight = 0;
    661     return 0;
    662 }
    663 
    664 
    665 static int gralloc_device_open(const hw_module_t* module,
    666                                const char* name,
    667                                hw_device_t** device)
    668 {
    669     int status = -EINVAL;
    670 
    671     D("gralloc_device_open %s\n", name);
    672 
    673     pthread_once( &sFallbackOnce, fallback_init );
    674     if (sFallback != NULL) {
    675         return sFallback->common.methods->open(&sFallback->common, name, device);
    676     }
    677 
    678     if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
    679 
    680         // Create host connection and keep it in the TLS.
    681         // return error if connection with host can not be established
    682         HostConnection *hostCon = HostConnection::get();
    683         if (!hostCon) {
    684             LOGE("gralloc: failed to get host connection while opening %s\n", name);
    685             return -EIO;
    686         }
    687 
    688         //
    689         // Allocate memory for the gralloc device (alloc interface)
    690         //
    691         gralloc_device_t *dev;
    692         dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t));
    693         if (NULL == dev) {
    694             return -ENOMEM;
    695         }
    696 
    697         // Initialize our device structure
    698         //
    699         dev->device.common.tag = HARDWARE_DEVICE_TAG;
    700         dev->device.common.version = 0;
    701         dev->device.common.module = const_cast<hw_module_t*>(module);
    702         dev->device.common.close = gralloc_device_close;
    703 
    704         dev->device.alloc   = gralloc_alloc;
    705         dev->device.free    = gralloc_free;
    706         dev->allocListHead  = NULL;
    707         pthread_mutex_init(&dev->lock, NULL);
    708 
    709         *device = &dev->device.common;
    710         status = 0;
    711     }
    712     else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
    713 
    714         // return error if connection with host can not be established
    715         DEFINE_AND_VALIDATE_HOST_CONNECTION;
    716 
    717         //
    718         // Query the host for Framebuffer attributes
    719         //
    720         D("gralloc: query Frabuffer attribs\n");
    721         EGLint width = rcEnc->rcGetFBParam(rcEnc, FB_WIDTH);
    722         D("gralloc: width=%d\n", width);
    723         EGLint height = rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT);
    724         D("gralloc: height=%d\n", height);
    725         EGLint xdpi = rcEnc->rcGetFBParam(rcEnc, FB_XDPI);
    726         D("gralloc: xdpi=%d\n", xdpi);
    727         EGLint ydpi = rcEnc->rcGetFBParam(rcEnc, FB_YDPI);
    728         D("gralloc: ydpi=%d\n", ydpi);
    729         EGLint fps = rcEnc->rcGetFBParam(rcEnc, FB_FPS);
    730         D("gralloc: fps=%d\n", fps);
    731         EGLint min_si = rcEnc->rcGetFBParam(rcEnc, FB_MIN_SWAP_INTERVAL);
    732         D("gralloc: min_swap=%d\n", min_si);
    733         EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL);
    734         D("gralloc: max_swap=%d\n", max_si);
    735 
    736         //
    737         // Allocate memory for the framebuffer device
    738         //
    739         fb_device_t *dev;
    740         dev = (fb_device_t*)malloc(sizeof(fb_device_t));
    741         if (NULL == dev) {
    742             return -ENOMEM;
    743         }
    744         memset(dev, 0, sizeof(fb_device_t));
    745 
    746         // Initialize our device structure
    747         //
    748         dev->device.common.tag = HARDWARE_DEVICE_TAG;
    749         dev->device.common.version = 0;
    750         dev->device.common.module = const_cast<hw_module_t*>(module);
    751         dev->device.common.close = fb_close;
    752         dev->device.setSwapInterval = fb_setSwapInterval;
    753         dev->device.post            = fb_post;
    754         dev->device.setUpdateRect   = 0; //fb_setUpdateRect;
    755         dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy
    756 
    757         const_cast<uint32_t&>(dev->device.flags) = 0;
    758         const_cast<uint32_t&>(dev->device.width) = width;
    759         const_cast<uint32_t&>(dev->device.height) = height;
    760         const_cast<int&>(dev->device.stride) = width;
    761         const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGBA_8888;
    762         const_cast<float&>(dev->device.xdpi) = xdpi;
    763         const_cast<float&>(dev->device.ydpi) = ydpi;
    764         const_cast<float&>(dev->device.fps) = fps;
    765         const_cast<int&>(dev->device.minSwapInterval) = min_si;
    766         const_cast<int&>(dev->device.maxSwapInterval) = max_si;
    767         *device = &dev->device.common;
    768 
    769         status = 0;
    770     }
    771 
    772     return status;
    773 }
    774 
    775 //
    776 // define the HMI symbol - our module interface
    777 //
    778 static struct hw_module_methods_t gralloc_module_methods = {
    779         open: gralloc_device_open
    780 };
    781 
    782 struct private_module_t HAL_MODULE_INFO_SYM = {
    783     base: {
    784         common: {
    785             tag: HARDWARE_MODULE_TAG,
    786             version_major: 1,
    787             version_minor: 0,
    788             id: GRALLOC_HARDWARE_MODULE_ID,
    789             name: "Graphics Memory Allocator Module",
    790             author: "The Android Open Source Project",
    791             methods: &gralloc_module_methods,
    792             dso: NULL,
    793             reserved: {0, }
    794         },
    795         registerBuffer: gralloc_register_buffer,
    796         unregisterBuffer: gralloc_unregister_buffer,
    797         lock: gralloc_lock,
    798         unlock: gralloc_unlock,
    799         perform: NULL,
    800         reserved_proc : {NULL, }
    801     }
    802 };
    803 
    804 /* This function is called once to detect whether the emulator supports
    805  * GPU emulation (this is done by looking at the qemu.gles kernel
    806  * parameter, which must be > 0 if this is the case).
    807  *
    808  * If not, then load gralloc.default instead as a fallback.
    809  */
    810 static void
    811 fallback_init(void)
    812 {
    813     char  prop[PROPERTY_VALUE_MAX];
    814     void* module;
    815 
    816     property_get("ro.kernel.qemu.gles", prop, "0");
    817     if (atoi(prop) > 0) {
    818         return;
    819     }
    820     LOGD("Emulator without GPU emulation detected.");
    821     module = dlopen("/system/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
    822     if (module != NULL) {
    823         sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
    824         if (sFallback == NULL) {
    825             dlclose(module);
    826         }
    827     }
    828     if (sFallback == NULL) {
    829         LOGE("Could not find software fallback module!?");
    830     }
    831 }
    832