blob: 1b15a04fb99a54afabf3e9b38aee595bd9dcefc0 [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
Tao Bao557fa1f2017-02-07 12:51:00 -080031MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), n_surfaces(0), surfaces() {}
Greg Hackmann41909dd2014-04-25 10:39:50 -070032
Tao Bao557fa1f2017-02-07 12:51:00 -080033int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080034 *surf = {};
35 surf->fence_fd = -1;
Tao Bao557fa1f2017-02-07 12:51:00 -080036 surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
37 &surf->offset, &surf->pitch);
Tao Bao8f0e21b2017-02-07 13:00:18 -080038 if (surf->fd < 0) {
39 return surf->fd;
40 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070041
Tao Bao557fa1f2017-02-07 12:51:00 -080042 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;
Greg Hackmann41909dd2014-04-25 10:39:50 -070046
Tao Bao557fa1f2017-02-07 12:51:00 -080047 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) {
Tao Baof04592b2017-02-09 12:59:19 -080050 int saved_errno = errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080051 close(surf->fd);
Tao Baof04592b2017-02-09 12:59:19 -080052 return -saved_errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080053 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070054
Tao Bao8f0e21b2017-02-07 13:00:18 -080055 return 0;
Greg Hackmann41909dd2014-04-25 10:39:50 -070056}
57
Tao Bao557fa1f2017-02-07 12:51:00 -080058int MinuiBackendAdf::InterfaceInit() {
Tao Bao8f0e21b2017-02-07 13:00:18 -080059 adf_interface_data intf_data;
Tao Bao557fa1f2017-02-07 12:51:00 -080060 int err = adf_get_interface_data(intf_fd, &intf_data);
Tao Bao8f0e21b2017-02-07 13:00:18 -080061 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070062
Tao Bao8f0e21b2017-02-07 13:00:18 -080063 int ret = 0;
Tao Bao557fa1f2017-02-07 12:51:00 -080064 err = SurfaceInit(&intf_data.current_mode, &surfaces[0]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080065 if (err < 0) {
66 fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
67 ret = err;
68 goto done;
69 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070070
Tao Bao557fa1f2017-02-07 12:51:00 -080071 err = SurfaceInit(&intf_data.current_mode, &surfaces[1]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080072 if (err < 0) {
73 fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -080074 surfaces[1] = {};
75 n_surfaces = 1;
Tao Bao8f0e21b2017-02-07 13:00:18 -080076 } else {
Tao Bao557fa1f2017-02-07 12:51:00 -080077 n_surfaces = 2;
Tao Bao8f0e21b2017-02-07 13:00:18 -080078 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070079
80done:
Tao Bao8f0e21b2017-02-07 13:00:18 -080081 adf_free_interface_data(&intf_data);
82 return ret;
Greg Hackmann41909dd2014-04-25 10:39:50 -070083}
84
Tao Bao557fa1f2017-02-07 12:51:00 -080085int MinuiBackendAdf::DeviceInit(adf_device* dev) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080086 adf_id_t intf_id;
Tao Bao557fa1f2017-02-07 12:51:00 -080087 int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080088 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070089
Tao Bao557fa1f2017-02-07 12:51:00 -080090 err = adf_device_attach(dev, eng_id, intf_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080091 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070092
Tao Bao557fa1f2017-02-07 12:51:00 -080093 intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
94 if (intf_fd < 0) return intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -070095
Tao Bao557fa1f2017-02-07 12:51:00 -080096 err = InterfaceInit();
Tao Bao8f0e21b2017-02-07 13:00:18 -080097 if (err < 0) {
Tao Bao557fa1f2017-02-07 12:51:00 -080098 close(intf_fd);
99 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800100 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700101
Tao Bao8f0e21b2017-02-07 13:00:18 -0800102 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700103}
104
Tao Bao557fa1f2017-02-07 12:51:00 -0800105GRSurface* MinuiBackendAdf::Init() {
Tony Kuofd778e32015-02-05 21:25:56 +0800106#if defined(RECOVERY_ABGR)
Tao Bao557fa1f2017-02-07 12:51:00 -0800107 format = DRM_FORMAT_ABGR8888;
Tony Kuofd778e32015-02-05 21:25:56 +0800108#elif defined(RECOVERY_BGRA)
Tao Bao557fa1f2017-02-07 12:51:00 -0800109 format = DRM_FORMAT_BGRA8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700110#elif defined(RECOVERY_RGBX)
Tao Bao557fa1f2017-02-07 12:51:00 -0800111 format = DRM_FORMAT_RGBX8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700112#else
Tao Bao557fa1f2017-02-07 12:51:00 -0800113 format = DRM_FORMAT_RGB565;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700114#endif
115
Tao Bao8f0e21b2017-02-07 13:00:18 -0800116 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
Tao Bao557fa1f2017-02-07 12:51:00 -0800125 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800126
Tao Bao557fa1f2017-02-07 12:51:00 -0800127 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);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800129 if (err < 0) {
130 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
131 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700132 }
133
Tao Bao557fa1f2017-02-07 12:51:00 -0800134 err = DeviceInit(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800135 if (err < 0) {
136 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -0800137 adf_device_close(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800138 }
139 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700140
Tao Bao8f0e21b2017-02-07 13:00:18 -0800141 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700142
Tao Bao557fa1f2017-02-07 12:51:00 -0800143 if (intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700144
Tao Bao557fa1f2017-02-07 12:51:00 -0800145 GRSurface* ret = Flip();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800146
Tao Bao557fa1f2017-02-07 12:51:00 -0800147 Blank(true);
148 Blank(false);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800149
150 return ret;
151}
152
Tao Bao557fa1f2017-02-07 12:51:00 -0800153void MinuiBackendAdf::Sync(GRSurfaceAdf* surf) {
Tao Bao8f0e21b2017-02-07 13:00:18 -0800154 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");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700162 }
163
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800164 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800165 surf->fence_fd = -1;
166 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700167}
168
Tao Bao557fa1f2017-02-07 12:51:00 -0800169GRSurface* MinuiBackendAdf::Flip() {
170 GRSurfaceAdf* surf = &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700171
Tao Bao557fa1f2017-02-07 12:51:00 -0800172 int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format,
173 surf->fd, surf->offset, surf->pitch, -1);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800174 if (fence_fd >= 0) surf->fence_fd = fence_fd;
175
Tao Bao557fa1f2017-02-07 12:51:00 -0800176 current_surface = (current_surface + 1) % n_surfaces;
177 Sync(&surfaces[current_surface]);
178 return &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700179}
180
Tao Bao557fa1f2017-02-07 12:51:00 -0800181void MinuiBackendAdf::Blank(bool blank) {
182 adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800183}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700184
Tao Bao557fa1f2017-02-07 12:51:00 -0800185void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) {
186 munmap(surf->data, surf->pitch * surf->height);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800187 close(surf->fence_fd);
188 close(surf->fd);
189}
190
Tao Bao557fa1f2017-02-07 12:51:00 -0800191MinuiBackendAdf::~MinuiBackendAdf() {
192 adf_device_close(&dev);
193 for (unsigned int i = 0; i < n_surfaces; i++) {
194 SurfaceDestroy(&surfaces[i]);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800195 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800196 if (intf_fd >= 0) close(intf_fd);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700197}