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