blob: 72b563b42accf90c723e22a0278507374b9802a0 [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>
Greg Hackmann41909dd2014-04-25 10:39:50 -070024#include <unistd.h>
25
Greg Hackmann41909dd2014-04-25 10:39:50 -070026#include <adf/adf.h>
Ethan Yonker8373cfe2017-09-08 06:50:54 -050027#ifdef HAS_LIBSYNC
xinglong.zhuc4fa2c22016-08-05 14:52:22 +080028#include <sync/sync.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070029#endif
30
Tao Bao557fa1f2017-02-07 12:51:00 -080031#include "minui/minui.h"
Greg Hackmann41909dd2014-04-25 10:39:50 -070032
Alistair Strachan4697d8b2017-04-28 15:05:33 -070033MinuiBackendAdf::MinuiBackendAdf()
34 : intf_fd(-1), dev(), current_surface(0), n_surfaces(0), surfaces() {}
Greg Hackmann41909dd2014-04-25 10:39:50 -070035
Tao Bao557fa1f2017-02-07 12:51:00 -080036int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080037 *surf = {};
38 surf->fence_fd = -1;
Tao Bao557fa1f2017-02-07 12:51:00 -080039 surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
40 &surf->offset, &surf->pitch);
Tao Bao8f0e21b2017-02-07 13:00:18 -080041 if (surf->fd < 0) {
42 return surf->fd;
43 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070044
Tao Bao557fa1f2017-02-07 12:51:00 -080045 surf->width = mode->hdisplay;
46 surf->height = mode->vdisplay;
47 surf->row_bytes = surf->pitch;
48 surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4;
Greg Hackmann41909dd2014-04-25 10:39:50 -070049
Tao Bao557fa1f2017-02-07 12:51:00 -080050 surf->data = static_cast<uint8_t*>(
51 mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset));
52 if (surf->data == MAP_FAILED) {
Tao Baof04592b2017-02-09 12:59:19 -080053 int saved_errno = errno;
Greg Hackmann41909dd2014-04-25 10:39:50 -070054 close(surf->fd);
Tao Baof04592b2017-02-09 12:59:19 -080055 return -saved_errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080056 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070057
Tao Bao8f0e21b2017-02-07 13:00:18 -080058 return 0;
Greg Hackmann41909dd2014-04-25 10:39:50 -070059}
60
Tao Bao557fa1f2017-02-07 12:51:00 -080061int MinuiBackendAdf::InterfaceInit() {
Tao Bao8f0e21b2017-02-07 13:00:18 -080062 adf_interface_data intf_data;
Tao Bao557fa1f2017-02-07 12:51:00 -080063 int err = adf_get_interface_data(intf_fd, &intf_data);
Tao Bao8f0e21b2017-02-07 13:00:18 -080064 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070065
Tao Bao8f0e21b2017-02-07 13:00:18 -080066 int ret = 0;
Tao Bao557fa1f2017-02-07 12:51:00 -080067 err = SurfaceInit(&intf_data.current_mode, &surfaces[0]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080068 if (err < 0) {
69 fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
70 ret = err;
71 goto done;
72 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070073
Tao Bao557fa1f2017-02-07 12:51:00 -080074 err = SurfaceInit(&intf_data.current_mode, &surfaces[1]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080075 if (err < 0) {
76 fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -080077 surfaces[1] = {};
78 n_surfaces = 1;
Tao Bao8f0e21b2017-02-07 13:00:18 -080079 } else {
Tao Bao557fa1f2017-02-07 12:51:00 -080080 n_surfaces = 2;
Tao Bao8f0e21b2017-02-07 13:00:18 -080081 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070082
83done:
Tao Bao8f0e21b2017-02-07 13:00:18 -080084 adf_free_interface_data(&intf_data);
85 return ret;
Greg Hackmann41909dd2014-04-25 10:39:50 -070086}
87
Tao Bao557fa1f2017-02-07 12:51:00 -080088int MinuiBackendAdf::DeviceInit(adf_device* dev) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080089 adf_id_t intf_id;
Tao Bao557fa1f2017-02-07 12:51:00 -080090 int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080091 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070092
Tao Bao557fa1f2017-02-07 12:51:00 -080093 err = adf_device_attach(dev, eng_id, intf_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080094 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070095
Tao Bao557fa1f2017-02-07 12:51:00 -080096 intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
97 if (intf_fd < 0) return intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -070098
Tao Bao557fa1f2017-02-07 12:51:00 -080099 err = InterfaceInit();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800100 if (err < 0) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800101 close(intf_fd);
102 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800103 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700104
Tao Bao8f0e21b2017-02-07 13:00:18 -0800105 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700106}
107
Tao Bao557fa1f2017-02-07 12:51:00 -0800108GRSurface* MinuiBackendAdf::Init() {
Greg Hackmann41909dd2014-04-25 10:39:50 -0700109#if defined(RECOVERY_ABGR)
Tao Bao557fa1f2017-02-07 12:51:00 -0800110 format = DRM_FORMAT_ABGR8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700111#elif defined(RECOVERY_BGRA)
Tao Bao557fa1f2017-02-07 12:51:00 -0800112 format = DRM_FORMAT_BGRA8888;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500113#elif defined(RECOVERY_RGBA)
114 format = DRM_FORMAT_RGBA8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700115#elif defined(RECOVERY_RGBX)
Tao Bao557fa1f2017-02-07 12:51:00 -0800116 format = DRM_FORMAT_RGBX8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700117#else
Tao Bao557fa1f2017-02-07 12:51:00 -0800118 format = DRM_FORMAT_RGB565;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700119#endif
120
Tao Bao8f0e21b2017-02-07 13:00:18 -0800121 adf_id_t* dev_ids = nullptr;
122 ssize_t n_dev_ids = adf_devices(&dev_ids);
123 if (n_dev_ids == 0) {
124 return nullptr;
125 } else if (n_dev_ids < 0) {
126 fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
127 return nullptr;
128 }
129
Tao Bao557fa1f2017-02-07 12:51:00 -0800130 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800131
Tao Bao557fa1f2017-02-07 12:51:00 -0800132 for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) {
133 int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800134 if (err < 0) {
135 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
136 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700137 }
138
Tao Bao557fa1f2017-02-07 12:51:00 -0800139 err = DeviceInit(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800140 if (err < 0) {
141 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -0800142 adf_device_close(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800143 }
144 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700145
Tao Bao8f0e21b2017-02-07 13:00:18 -0800146 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700147
Tao Bao557fa1f2017-02-07 12:51:00 -0800148 if (intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700149
Tao Bao557fa1f2017-02-07 12:51:00 -0800150 GRSurface* ret = Flip();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800151
Tao Bao557fa1f2017-02-07 12:51:00 -0800152 Blank(true);
153 Blank(false);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800154
155 return ret;
156}
157
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500158void MinuiBackendAdf::Sync(__unused GRSurfaceAdf* surf) {
159#ifdef HAS_LIBSYNC
Tao Bao8f0e21b2017-02-07 13:00:18 -0800160 static constexpr unsigned int warningTimeout = 3000;
161
162 if (surf == nullptr) return;
163
164 if (surf->fence_fd >= 0) {
165 int err = sync_wait(surf->fence_fd, warningTimeout);
166 if (err < 0) {
167 perror("adf sync fence wait error\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700168 }
169
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800170 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800171 surf->fence_fd = -1;
172 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500173#endif
Greg Hackmann41909dd2014-04-25 10:39:50 -0700174}
175
Tao Bao557fa1f2017-02-07 12:51:00 -0800176GRSurface* MinuiBackendAdf::Flip() {
177 GRSurfaceAdf* surf = &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700178
Tao Bao557fa1f2017-02-07 12:51:00 -0800179 int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format,
180 surf->fd, surf->offset, surf->pitch, -1);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800181 if (fence_fd >= 0) surf->fence_fd = fence_fd;
182
Tao Bao557fa1f2017-02-07 12:51:00 -0800183 current_surface = (current_surface + 1) % n_surfaces;
184 Sync(&surfaces[current_surface]);
185 return &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700186}
187
Tao Bao557fa1f2017-02-07 12:51:00 -0800188void MinuiBackendAdf::Blank(bool blank) {
189 adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800190}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700191
Tao Bao557fa1f2017-02-07 12:51:00 -0800192void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) {
193 munmap(surf->data, surf->pitch * surf->height);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800194 close(surf->fence_fd);
195 close(surf->fd);
196}
197
Tao Bao557fa1f2017-02-07 12:51:00 -0800198MinuiBackendAdf::~MinuiBackendAdf() {
199 adf_device_close(&dev);
200 for (unsigned int i = 0; i < n_surfaces; i++) {
201 SurfaceDestroy(&surfaces[i]);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800202 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800203 if (intf_fd >= 0) close(intf_fd);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700204}