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