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