Home | History | Annotate | Download | only in minui
      1 /*
      2  * Copyright (C) 2014 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 
     17 #include "graphics_adf.h"
     18 
     19 #include <errno.h>
     20 #include <fcntl.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #include <sys/mman.h>
     24 #include <unistd.h>
     25 
     26 #include <adf/adf.h>
     27 #include <sync/sync.h>
     28 
     29 #include "minui/minui.h"
     30 
     31 MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), n_surfaces(0), surfaces() {}
     32 
     33 int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) {
     34   *surf = {};
     35   surf->fence_fd = -1;
     36   surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
     37                                                &surf->offset, &surf->pitch);
     38   if (surf->fd < 0) {
     39     return surf->fd;
     40   }
     41 
     42   surf->width = mode->hdisplay;
     43   surf->height = mode->vdisplay;
     44   surf->row_bytes = surf->pitch;
     45   surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4;
     46 
     47   surf->data = static_cast<uint8_t*>(
     48       mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset));
     49   if (surf->data == MAP_FAILED) {
     50     int saved_errno = errno;
     51     close(surf->fd);
     52     return -saved_errno;
     53   }
     54 
     55   return 0;
     56 }
     57 
     58 int MinuiBackendAdf::InterfaceInit() {
     59   adf_interface_data intf_data;
     60   int err = adf_get_interface_data(intf_fd, &intf_data);
     61   if (err < 0) return err;
     62 
     63   int ret = 0;
     64   err = SurfaceInit(&intf_data.current_mode, &surfaces[0]);
     65   if (err < 0) {
     66     fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
     67     ret = err;
     68     goto done;
     69   }
     70 
     71   err = SurfaceInit(&intf_data.current_mode, &surfaces[1]);
     72   if (err < 0) {
     73     fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
     74     surfaces[1] = {};
     75     n_surfaces = 1;
     76   } else {
     77     n_surfaces = 2;
     78   }
     79 
     80 done:
     81   adf_free_interface_data(&intf_data);
     82   return ret;
     83 }
     84 
     85 int MinuiBackendAdf::DeviceInit(adf_device* dev) {
     86   adf_id_t intf_id;
     87   int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
     88   if (err < 0) return err;
     89 
     90   err = adf_device_attach(dev, eng_id, intf_id);
     91   if (err < 0 && err != -EALREADY) return err;
     92 
     93   intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
     94   if (intf_fd < 0) return intf_fd;
     95 
     96   err = InterfaceInit();
     97   if (err < 0) {
     98     close(intf_fd);
     99     intf_fd = -1;
    100   }
    101 
    102   return err;
    103 }
    104 
    105 GRSurface* MinuiBackendAdf::Init() {
    106 #if defined(RECOVERY_ABGR)
    107   format = DRM_FORMAT_ABGR8888;
    108 #elif defined(RECOVERY_BGRA)
    109   format = DRM_FORMAT_BGRA8888;
    110 #elif defined(RECOVERY_RGBX)
    111   format = DRM_FORMAT_RGBX8888;
    112 #else
    113   format = DRM_FORMAT_RGB565;
    114 #endif
    115 
    116   adf_id_t* dev_ids = nullptr;
    117   ssize_t n_dev_ids = adf_devices(&dev_ids);
    118   if (n_dev_ids == 0) {
    119     return nullptr;
    120   } else if (n_dev_ids < 0) {
    121     fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
    122     return nullptr;
    123   }
    124 
    125   intf_fd = -1;
    126 
    127   for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) {
    128     int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
    129     if (err < 0) {
    130       fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
    131       continue;
    132     }
    133 
    134     err = DeviceInit(&dev);
    135     if (err < 0) {
    136       fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
    137       adf_device_close(&dev);
    138     }
    139   }
    140 
    141   free(dev_ids);
    142 
    143   if (intf_fd < 0) return nullptr;
    144 
    145   GRSurface* ret = Flip();
    146 
    147   Blank(true);
    148   Blank(false);
    149 
    150   return ret;
    151 }
    152 
    153 void MinuiBackendAdf::Sync(GRSurfaceAdf* surf) {
    154   static constexpr unsigned int warningTimeout = 3000;
    155 
    156   if (surf == nullptr) return;
    157 
    158   if (surf->fence_fd >= 0) {
    159     int err = sync_wait(surf->fence_fd, warningTimeout);
    160     if (err < 0) {
    161       perror("adf sync fence wait error\n");
    162     }
    163 
    164     close(surf->fence_fd);
    165     surf->fence_fd = -1;
    166   }
    167 }
    168 
    169 GRSurface* MinuiBackendAdf::Flip() {
    170   GRSurfaceAdf* surf = &surfaces[current_surface];
    171 
    172   int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format,
    173                                            surf->fd, surf->offset, surf->pitch, -1);
    174   if (fence_fd >= 0) surf->fence_fd = fence_fd;
    175 
    176   current_surface = (current_surface + 1) % n_surfaces;
    177   Sync(&surfaces[current_surface]);
    178   return &surfaces[current_surface];
    179 }
    180 
    181 void MinuiBackendAdf::Blank(bool blank) {
    182   adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
    183 }
    184 
    185 void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) {
    186   munmap(surf->data, surf->pitch * surf->height);
    187   close(surf->fence_fd);
    188   close(surf->fd);
    189 }
    190 
    191 MinuiBackendAdf::~MinuiBackendAdf() {
    192   adf_device_close(&dev);
    193   for (unsigned int i = 0; i < n_surfaces; i++) {
    194     SurfaceDestroy(&surfaces[i]);
    195   }
    196   if (intf_fd >= 0) close(intf_fd);
    197 }
    198