blob: 7439df9ac5bae3688b927d3c7b11bd443ca17426 [file] [log] [blame]
Greg Hackmann41909dd2014-04-25 10:39:50 -07001/*
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
Tao Bao557fa1f2017-02-07 12:51:00 -080017#include "graphics_adf.h"
18
Greg Hackmann41909dd2014-04-25 10:39:50 -070019#include <errno.h>
20#include <fcntl.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070021#include <stdio.h>
22#include <stdlib.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070023#include <sys/mman.h>
Tao Bao8f0e21b2017-02-07 13:00:18 -080024#include <unistd.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070025
26#include <adf/adf.h>
xinglong.zhuc4fa2c22016-08-05 14:52:22 +080027#include <sync/sync.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070028
Tao Bao557fa1f2017-02-07 12:51:00 -080029#include "minui/minui.h"
Greg Hackmann41909dd2014-04-25 10:39:50 -070030
Alistair Strachan4697d8b2017-04-28 15:05:33 -070031MinuiBackendAdf::MinuiBackendAdf()
32 : intf_fd(-1), dev(), current_surface(0), n_surfaces(0), surfaces() {}
Greg Hackmann41909dd2014-04-25 10:39:50 -070033
Tao Bao557fa1f2017-02-07 12:51:00 -080034int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080035 *surf = {};
36 surf->fence_fd = -1;
Tao Bao557fa1f2017-02-07 12:51:00 -080037 surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
38 &surf->offset, &surf->pitch);
Tao Bao8f0e21b2017-02-07 13:00:18 -080039 if (surf->fd < 0) {
40 return surf->fd;
41 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070042
Tao Bao557fa1f2017-02-07 12:51:00 -080043 surf->width = mode->hdisplay;
44 surf->height = mode->vdisplay;
45 surf->row_bytes = surf->pitch;
46 surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4;
Greg Hackmann41909dd2014-04-25 10:39:50 -070047
Tao Bao557fa1f2017-02-07 12:51:00 -080048 surf->data = static_cast<uint8_t*>(
49 mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset));
50 if (surf->data == MAP_FAILED) {
Tao Baof04592b2017-02-09 12:59:19 -080051 int saved_errno = errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080052 close(surf->fd);
Tao Baof04592b2017-02-09 12:59:19 -080053 return -saved_errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080054 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070055
Tao Bao8f0e21b2017-02-07 13:00:18 -080056 return 0;
Greg Hackmann41909dd2014-04-25 10:39:50 -070057}
58
Tao Bao557fa1f2017-02-07 12:51:00 -080059int MinuiBackendAdf::InterfaceInit() {
Tao Bao8f0e21b2017-02-07 13:00:18 -080060 adf_interface_data intf_data;
Tao Bao557fa1f2017-02-07 12:51:00 -080061 int err = adf_get_interface_data(intf_fd, &intf_data);
Tao Bao8f0e21b2017-02-07 13:00:18 -080062 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070063
Tao Bao8f0e21b2017-02-07 13:00:18 -080064 int ret = 0;
Tao Bao557fa1f2017-02-07 12:51:00 -080065 err = SurfaceInit(&intf_data.current_mode, &surfaces[0]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080066 if (err < 0) {
67 fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
68 ret = err;
69 goto done;
70 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070071
Tao Bao557fa1f2017-02-07 12:51:00 -080072 err = SurfaceInit(&intf_data.current_mode, &surfaces[1]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080073 if (err < 0) {
74 fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -080075 surfaces[1] = {};
76 n_surfaces = 1;
Tao Bao8f0e21b2017-02-07 13:00:18 -080077 } else {
Tao Bao557fa1f2017-02-07 12:51:00 -080078 n_surfaces = 2;
Tao Bao8f0e21b2017-02-07 13:00:18 -080079 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070080
81done:
Tao Bao8f0e21b2017-02-07 13:00:18 -080082 adf_free_interface_data(&intf_data);
83 return ret;
Greg Hackmann41909dd2014-04-25 10:39:50 -070084}
85
Tao Bao557fa1f2017-02-07 12:51:00 -080086int MinuiBackendAdf::DeviceInit(adf_device* dev) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080087 adf_id_t intf_id;
Tao Bao557fa1f2017-02-07 12:51:00 -080088 int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080089 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070090
Tao Bao557fa1f2017-02-07 12:51:00 -080091 err = adf_device_attach(dev, eng_id, intf_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080092 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070093
Tao Bao557fa1f2017-02-07 12:51:00 -080094 intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
95 if (intf_fd < 0) return intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -070096
Tao Bao557fa1f2017-02-07 12:51:00 -080097 err = InterfaceInit();
Tao Bao8f0e21b2017-02-07 13:00:18 -080098 if (err < 0) {
Tao Bao557fa1f2017-02-07 12:51:00 -080099 close(intf_fd);
100 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800101 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700102
Tao Bao8f0e21b2017-02-07 13:00:18 -0800103 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700104}
105
Tao Bao557fa1f2017-02-07 12:51:00 -0800106GRSurface* MinuiBackendAdf::Init() {
Tao Baoed876a72018-07-31 21:32:50 -0700107 PixelFormat pixel_format = gr_pixel_format();
108 if (pixel_format == PixelFormat::ABGR) {
109 format = DRM_FORMAT_ABGR8888;
110 } else if (pixel_format == PixelFormat::BGRA) {
111 format = DRM_FORMAT_BGRA8888;
112 } else if (pixel_format == PixelFormat::RGBX) {
113 format = DRM_FORMAT_RGBX8888;
114 } else {
115 format = DRM_FORMAT_RGB565;
116 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700117
Tao Bao8f0e21b2017-02-07 13:00:18 -0800118 adf_id_t* dev_ids = nullptr;
119 ssize_t n_dev_ids = adf_devices(&dev_ids);
120 if (n_dev_ids == 0) {
121 return nullptr;
122 } else if (n_dev_ids < 0) {
123 fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
124 return nullptr;
125 }
126
Tao Bao557fa1f2017-02-07 12:51:00 -0800127 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800128
Tao Bao557fa1f2017-02-07 12:51:00 -0800129 for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) {
130 int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800131 if (err < 0) {
132 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
133 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700134 }
135
Tao Bao557fa1f2017-02-07 12:51:00 -0800136 err = DeviceInit(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800137 if (err < 0) {
138 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -0800139 adf_device_close(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800140 }
141 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700142
Tao Bao8f0e21b2017-02-07 13:00:18 -0800143 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700144
Tao Bao557fa1f2017-02-07 12:51:00 -0800145 if (intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700146
Tao Bao557fa1f2017-02-07 12:51:00 -0800147 GRSurface* ret = Flip();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800148
Tao Bao557fa1f2017-02-07 12:51:00 -0800149 Blank(true);
150 Blank(false);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800151
152 return ret;
153}
154
Tao Bao557fa1f2017-02-07 12:51:00 -0800155void MinuiBackendAdf::Sync(GRSurfaceAdf* surf) {
Tao Bao8f0e21b2017-02-07 13:00:18 -0800156 static constexpr unsigned int warningTimeout = 3000;
157
158 if (surf == nullptr) return;
159
160 if (surf->fence_fd >= 0) {
161 int err = sync_wait(surf->fence_fd, warningTimeout);
162 if (err < 0) {
163 perror("adf sync fence wait error\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700164 }
165
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800166 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800167 surf->fence_fd = -1;
168 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700169}
170
Tao Bao557fa1f2017-02-07 12:51:00 -0800171GRSurface* MinuiBackendAdf::Flip() {
172 GRSurfaceAdf* surf = &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700173
Tao Bao557fa1f2017-02-07 12:51:00 -0800174 int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format,
175 surf->fd, surf->offset, surf->pitch, -1);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800176 if (fence_fd >= 0) surf->fence_fd = fence_fd;
177
Tao Bao557fa1f2017-02-07 12:51:00 -0800178 current_surface = (current_surface + 1) % n_surfaces;
179 Sync(&surfaces[current_surface]);
180 return &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700181}
182
Tao Bao557fa1f2017-02-07 12:51:00 -0800183void MinuiBackendAdf::Blank(bool blank) {
184 adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800185}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700186
Tao Bao557fa1f2017-02-07 12:51:00 -0800187void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) {
188 munmap(surf->data, surf->pitch * surf->height);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800189 close(surf->fence_fd);
190 close(surf->fd);
191}
192
Tao Bao557fa1f2017-02-07 12:51:00 -0800193MinuiBackendAdf::~MinuiBackendAdf() {
194 adf_device_close(&dev);
195 for (unsigned int i = 0; i < n_surfaces; i++) {
196 SurfaceDestroy(&surfaces[i]);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800197 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800198 if (intf_fd >= 0) close(intf_fd);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700199}