Home | History | Annotate | Download | only in libgralloc
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * Copyright (c) 2010-2014 The Linux Foundation. All rights reserved.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 #include <sys/mman.h>
     19 
     20 #include <cutils/log.h>
     21 #include <cutils/properties.h>
     22 #include <dlfcn.h>
     23 
     24 #include <hardware/hardware.h>
     25 
     26 #include <fcntl.h>
     27 #include <errno.h>
     28 #include <sys/ioctl.h>
     29 #include <string.h>
     30 #include <stdlib.h>
     31 #include <pthread.h>
     32 #include <cutils/atomic.h>
     33 
     34 #include <linux/fb.h>
     35 #include <linux/msm_mdp.h>
     36 
     37 #ifndef TARGET_HEADLESS
     38 #include <GLES/gl.h>
     39 #endif
     40 
     41 #include "gralloc_priv.h"
     42 #include "fb_priv.h"
     43 #include "gr.h"
     44 #include <cutils/properties.h>
     45 #include <profiler.h>
     46 
     47 #define EVEN_OUT(x) if (x & 0x0001) {x--;}
     48 
     49 enum {
     50     PAGE_FLIP = 0x00000001,
     51 };
     52 
     53 struct fb_context_t {
     54     framebuffer_device_t  device;
     55     //fd - which is returned on open
     56     int fbFd;
     57 };
     58 
     59 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
     60                               int interval)
     61 {
     62     //XXX: Get the value here and implement along with
     63     //single vsync in HWC
     64     char pval[PROPERTY_VALUE_MAX];
     65     property_get("debug.egl.swapinterval", pval, "-1");
     66     int property_interval = atoi(pval);
     67     if (property_interval >= 0)
     68         interval = property_interval;
     69 
     70     private_module_t* m = reinterpret_cast<private_module_t*>(
     71         dev->common.module);
     72     if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
     73         return -EINVAL;
     74 
     75     m->swapInterval = interval;
     76     return 0;
     77 }
     78 
     79 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
     80 {
     81     private_module_t* m =
     82         reinterpret_cast<private_module_t*>(dev->common.module);
     83     private_handle_t *hnd = static_cast<private_handle_t*>
     84         (const_cast<native_handle_t*>(buffer));
     85     fb_context_t *ctx = reinterpret_cast<fb_context_t*>(dev);
     86     const unsigned int offset = (unsigned int) (hnd->base -
     87             m->framebuffer->base);
     88     m->info.activate = FB_ACTIVATE_VBL;
     89     m->info.yoffset = (int)(offset / m->finfo.line_length);
     90     if (ioctl(ctx->fbFd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
     91         ALOGE("%s: FBIOPUT_VSCREENINFO for primary failed, str: %s",
     92                 __FUNCTION__, strerror(errno));
     93         return -errno;
     94     }
     95     return 0;
     96 }
     97 
     98 static int fb_compositionComplete(struct framebuffer_device_t* dev)
     99 {
    100     // TODO: Properly implement composition complete callback
    101     if(!dev) {
    102         return -1;
    103     }
    104 #ifndef TARGET_HEADLESS
    105     glFinish();
    106 #endif
    107 
    108     return 0;
    109 }
    110 
    111 int mapFrameBufferLocked(framebuffer_device_t *dev)
    112 {
    113     private_module_t* module =
    114         reinterpret_cast<private_module_t*>(dev->common.module);
    115     fb_context_t *ctx = reinterpret_cast<fb_context_t*>(dev);
    116     // already initialized...
    117     if (module->framebuffer) {
    118         return 0;
    119     }
    120     char const * const device_template[] = {
    121         "/dev/graphics/fb%u",
    122         "/dev/fb%u",
    123         0 };
    124 
    125     int fd = -1;
    126     int i=0;
    127     char name[64];
    128     char property[PROPERTY_VALUE_MAX];
    129 
    130     while ((fd==-1) && device_template[i]) {
    131         snprintf(name, 64, device_template[i], 0);
    132         fd = open(name, O_RDWR, 0);
    133         i++;
    134     }
    135     if (fd < 0)
    136         return -errno;
    137 
    138     struct fb_fix_screeninfo finfo;
    139     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
    140         close(fd);
    141         return -errno;
    142     }
    143 
    144     struct fb_var_screeninfo info;
    145     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
    146         close(fd);
    147         return -errno;
    148     }
    149 
    150     info.reserved[0] = 0;
    151     info.reserved[1] = 0;
    152     info.reserved[2] = 0;
    153     info.xoffset = 0;
    154     info.yoffset = 0;
    155     info.activate = FB_ACTIVATE_NOW;
    156 
    157     /* Interpretation of offset for color fields: All offsets are from the
    158      * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
    159      * (means: you can use the offset as right argument to <<). A pixel
    160      * afterwards is a bit stream and is written to video memory as that
    161      * unmodified. This implies big-endian byte order if bits_per_pixel is
    162      * greater than 8.
    163      */
    164 
    165     if(info.bits_per_pixel == 32) {
    166         /*
    167          * Explicitly request RGBA_8888
    168          */
    169         info.bits_per_pixel = 32;
    170         info.red.offset     = 24;
    171         info.red.length     = 8;
    172         info.green.offset   = 16;
    173         info.green.length   = 8;
    174         info.blue.offset    = 8;
    175         info.blue.length    = 8;
    176         info.transp.offset  = 0;
    177         info.transp.length  = 8;
    178 
    179         /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
    180          * do not use the MDP for composition (i.e. hw composition == 0), ask
    181          * for RGBA instead of RGBX. */
    182         if (property_get("debug.sf.hw", property, NULL) > 0 &&
    183                                                            atoi(property) == 0)
    184             module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
    185         else if(property_get("debug.composition.type", property, NULL) > 0 &&
    186                 (strncmp(property, "mdp", 3) == 0))
    187             module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
    188         else
    189             module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
    190     } else {
    191         /*
    192          * Explicitly request 5/6/5
    193          */
    194         info.bits_per_pixel = 16;
    195         info.red.offset     = 11;
    196         info.red.length     = 5;
    197         info.green.offset   = 5;
    198         info.green.length   = 6;
    199         info.blue.offset    = 0;
    200         info.blue.length    = 5;
    201         info.transp.offset  = 0;
    202         info.transp.length  = 0;
    203         module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
    204     }
    205 
    206     //adreno needs 4k aligned offsets. Max hole size is 4096-1
    207     unsigned int size = roundUpToPageSize(info.yres * info.xres *
    208                                                (info.bits_per_pixel/8));
    209 
    210     /*
    211      * Request NUM_BUFFERS screens (at least 2 for page flipping)
    212      */
    213     int numberOfBuffers = (int)(finfo.smem_len/size);
    214     ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
    215 
    216     if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
    217         int num = atoi(property);
    218         if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
    219             numberOfBuffers = num;
    220         }
    221     }
    222     if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
    223         numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
    224 
    225     ALOGV("We support %d buffers", numberOfBuffers);
    226 
    227     //consider the included hole by 4k alignment
    228     uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
    229     info.yres_virtual = (uint32_t) ((size * numberOfBuffers) / line_length);
    230 
    231     uint32_t flags = PAGE_FLIP;
    232 
    233     if (info.yres_virtual < ((size * 2) / line_length) ) {
    234         // we need at least 2 for page-flipping
    235         info.yres_virtual = (int)(size / line_length);
    236         flags &= ~PAGE_FLIP;
    237         ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
    238               info.yres_virtual, info.yres*2);
    239     }
    240 
    241     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
    242         close(fd);
    243         return -errno;
    244     }
    245 
    246     if (int(info.width) <= 0 || int(info.height) <= 0) {
    247         // the driver doesn't return that information
    248         // default to 160 dpi
    249         info.width  = (uint32_t)(((float)(info.xres) * 25.4f)/160.0f + 0.5f);
    250         info.height = (uint32_t)(((float)(info.yres) * 25.4f)/160.0f + 0.5f);
    251     }
    252 
    253     float xdpi = ((float)(info.xres) * 25.4f) / (float)info.width;
    254     float ydpi = ((float)(info.yres) * 25.4f) / (float)info.height;
    255 
    256 #ifdef MSMFB_METADATA_GET
    257     struct msmfb_metadata metadata;
    258     memset(&metadata, 0 , sizeof(metadata));
    259     metadata.op = metadata_op_frame_rate;
    260     if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
    261         ALOGE("Error retrieving panel frame rate");
    262         close(fd);
    263         return -errno;
    264     }
    265     float fps = (float)metadata.data.panel_frame_rate;
    266 #else
    267     //XXX: Remove reserved field usage on all baselines
    268     //The reserved[3] field is used to store FPS by the driver.
    269     float fps  = info.reserved[3] & 0xFF;
    270 #endif
    271     ALOGI("using (fd=%d)\n"
    272           "id           = %s\n"
    273           "xres         = %d px\n"
    274           "yres         = %d px\n"
    275           "xres_virtual = %d px\n"
    276           "yres_virtual = %d px\n"
    277           "bpp          = %d\n"
    278           "r            = %2u:%u\n"
    279           "g            = %2u:%u\n"
    280           "b            = %2u:%u\n",
    281           fd,
    282           finfo.id,
    283           info.xres,
    284           info.yres,
    285           info.xres_virtual,
    286           info.yres_virtual,
    287           info.bits_per_pixel,
    288           info.red.offset, info.red.length,
    289           info.green.offset, info.green.length,
    290           info.blue.offset, info.blue.length
    291          );
    292 
    293     ALOGI("width        = %d mm (%f dpi)\n"
    294           "height       = %d mm (%f dpi)\n"
    295           "refresh rate = %.2f Hz\n",
    296           info.width,  xdpi,
    297           info.height, ydpi,
    298           fps
    299          );
    300 
    301 
    302     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
    303         close(fd);
    304         return -errno;
    305     }
    306 
    307     if (finfo.smem_len <= 0) {
    308         close(fd);
    309         return -errno;
    310     }
    311 
    312     module->flags = flags;
    313     module->info = info;
    314     module->finfo = finfo;
    315     module->xdpi = xdpi;
    316     module->ydpi = ydpi;
    317     module->fps = fps;
    318     module->swapInterval = 1;
    319 
    320     CALC_INIT();
    321 
    322     /*
    323      * map the framebuffer
    324      */
    325 
    326     module->numBuffers = info.yres_virtual / info.yres;
    327     module->bufferMask = 0;
    328     //adreno needs page aligned offsets. Align the fbsize to pagesize.
    329     unsigned int fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
    330                     module->numBuffers;
    331     void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    332     if (vaddr == MAP_FAILED) {
    333         ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
    334         close(fd);
    335         return -errno;
    336     }
    337     //store the framebuffer fd in the ctx
    338     ctx->fbFd = fd;
    339 #ifdef MSMFB_METADATA_GET
    340     memset(&metadata, 0 , sizeof(metadata));
    341     metadata.op = metadata_op_get_ion_fd;
    342     // get the ION fd for the framebuffer, as GPU needs ION fd
    343     if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
    344         ALOGE("Error getting ION fd (%s)", strerror(errno));
    345         close(fd);
    346         return -errno;
    347     }
    348     if(metadata.data.fbmem_ionfd < 0) {
    349         ALOGE("Error: Ioctl returned invalid ION fd = %d",
    350                                         metadata.data.fbmem_ionfd);
    351         close(fd);
    352         return -errno;
    353     }
    354     fd = metadata.data.fbmem_ionfd;
    355 #endif
    356     // Create framebuffer handle using the ION fd
    357     module->framebuffer = new private_handle_t(fd, fbSize,
    358                                         private_handle_t::PRIV_FLAGS_USES_ION,
    359                                         BUFFER_TYPE_UI,
    360                                         module->fbFormat, info.xres, info.yres);
    361     module->framebuffer->base = uint64_t(vaddr);
    362     memset(vaddr, 0, fbSize);
    363     //Enable vsync
    364     int enable = 1;
    365     ioctl(ctx->fbFd, MSMFB_OVERLAY_VSYNC_CTRL, &enable);
    366     return 0;
    367 }
    368 
    369 static int mapFrameBuffer(framebuffer_device_t *dev)
    370 {
    371     int err = -1;
    372     char property[PROPERTY_VALUE_MAX];
    373     if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
    374        (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
    375         (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
    376         private_module_t* module =
    377             reinterpret_cast<private_module_t*>(dev->common.module);
    378         pthread_mutex_lock(&module->lock);
    379         err = mapFrameBufferLocked(dev);
    380         pthread_mutex_unlock(&module->lock);
    381     }
    382     return err;
    383 }
    384 
    385 /*****************************************************************************/
    386 
    387 static int fb_close(struct hw_device_t *dev)
    388 {
    389     fb_context_t* ctx = (fb_context_t*)dev;
    390     if (ctx) {
    391 #ifdef MSMFB_METADATA_GET
    392         if(ctx->fbFd >=0) {
    393             close(ctx->fbFd);
    394         }
    395 #endif
    396         //Hack until fbdev is removed. Framework could close this causing hwc a
    397         //pain.
    398         //free(ctx);
    399     }
    400     return 0;
    401 }
    402 
    403 int fb_device_open(hw_module_t const* module, const char* name,
    404                    hw_device_t** device)
    405 {
    406     int status = -EINVAL;
    407     if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
    408         alloc_device_t* gralloc_device;
    409         status = gralloc_open(module, &gralloc_device);
    410         if (status < 0)
    411             return status;
    412 
    413         /* initialize our state here */
    414         fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
    415         if(dev == NULL) {
    416             gralloc_close(gralloc_device);
    417             return status;
    418         }
    419         memset(dev, 0, sizeof(*dev));
    420 
    421         /* initialize the procs */
    422         dev->device.common.tag      = HARDWARE_DEVICE_TAG;
    423         dev->device.common.version  = 0;
    424         dev->device.common.module   = const_cast<hw_module_t*>(module);
    425         dev->device.common.close    = fb_close;
    426         dev->device.setSwapInterval = fb_setSwapInterval;
    427         dev->device.post            = fb_post;
    428         dev->device.setUpdateRect   = 0;
    429         dev->device.compositionComplete = fb_compositionComplete;
    430 
    431         status = mapFrameBuffer((framebuffer_device_t*)dev);
    432         private_module_t* m = (private_module_t*)dev->device.common.module;
    433         if (status >= 0) {
    434             int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
    435             const_cast<uint32_t&>(dev->device.flags) = 0;
    436             const_cast<uint32_t&>(dev->device.width) = m->info.xres;
    437             const_cast<uint32_t&>(dev->device.height) = m->info.yres;
    438             const_cast<int&>(dev->device.stride) = stride;
    439             const_cast<int&>(dev->device.format) = m->fbFormat;
    440             const_cast<float&>(dev->device.xdpi) = m->xdpi;
    441             const_cast<float&>(dev->device.ydpi) = m->ydpi;
    442             const_cast<float&>(dev->device.fps) = m->fps;
    443             const_cast<int&>(dev->device.minSwapInterval) =
    444                                                         PRIV_MIN_SWAP_INTERVAL;
    445             const_cast<int&>(dev->device.maxSwapInterval) =
    446                                                         PRIV_MAX_SWAP_INTERVAL;
    447             const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
    448             dev->device.setUpdateRect = 0;
    449 
    450             *device = &dev->device.common;
    451         }
    452 
    453         // Close the gralloc module
    454         gralloc_close(gralloc_device);
    455     }
    456     return status;
    457 }
    458