blob: 10cd60709504482046093af70f441b244e80be11 [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>
Tao Bao1b18cf52018-10-31 00:12:11 -070023#include <string.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070024#include <sys/mman.h>
Tao Bao8f0e21b2017-02-07 13:00:18 -080025#include <unistd.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070026
27#include <adf/adf.h>
xinglong.zhuc4fa2c22016-08-05 14:52:22 +080028#include <sync/sync.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070029
Tao Bao557fa1f2017-02-07 12:51:00 -080030#include "minui/minui.h"
Greg Hackmann41909dd2014-04-25 10:39:50 -070031
Tao Bao1b18cf52018-10-31 00:12:11 -070032GRSurfaceAdf::~GRSurfaceAdf() {
33 if (mmapped_buffer_) {
34 munmap(mmapped_buffer_, pitch * height);
35 }
36 if (fence_fd != -1) {
37 close(fence_fd);
38 }
39 if (fd != -1) {
40 close(fd);
41 }
42}
Greg Hackmann41909dd2014-04-25 10:39:50 -070043
Tao Bao1b18cf52018-10-31 00:12:11 -070044std::unique_ptr<GRSurfaceAdf> GRSurfaceAdf::Create(int intf_fd, const drm_mode_modeinfo* mode,
45 __u32 format, int* err) {
46 __u32 offset;
47 __u32 pitch;
48 auto fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
49 &offset, &pitch);
50
51 if (fd < 0) {
52 *err = fd;
53 return nullptr;
Tao Bao8f0e21b2017-02-07 13:00:18 -080054 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070055
Tao Bao1b18cf52018-10-31 00:12:11 -070056 std::unique_ptr<GRSurfaceAdf> surf = std::unique_ptr<GRSurfaceAdf>(
57 new GRSurfaceAdf(mode->hdisplay, mode->vdisplay, pitch, (format == DRM_FORMAT_RGB565 ? 2 : 4),
58 offset, pitch, fd));
Greg Hackmann41909dd2014-04-25 10:39:50 -070059
Tao Bao92bdb5a2018-10-21 12:12:37 -070060 auto mmapped =
61 mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset);
62 if (mmapped == MAP_FAILED) {
Tao Bao1b18cf52018-10-31 00:12:11 -070063 *err = -errno;
64 return nullptr;
Tao Bao8f0e21b2017-02-07 13:00:18 -080065 }
Tao Bao92bdb5a2018-10-21 12:12:37 -070066 surf->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
Tao Bao1b18cf52018-10-31 00:12:11 -070067 return surf;
Greg Hackmann41909dd2014-04-25 10:39:50 -070068}
69
Tao Bao1b18cf52018-10-31 00:12:11 -070070MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), current_surface(0), n_surfaces(0) {}
71
Tao Bao557fa1f2017-02-07 12:51:00 -080072int MinuiBackendAdf::InterfaceInit() {
Tao Bao8f0e21b2017-02-07 13:00:18 -080073 adf_interface_data intf_data;
Tao Bao1b18cf52018-10-31 00:12:11 -070074 if (int err = adf_get_interface_data(intf_fd, &intf_data); err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070075
Tao Bao1b18cf52018-10-31 00:12:11 -070076 int result = 0;
77 surfaces[0] = GRSurfaceAdf::Create(intf_fd, &intf_data.current_mode, format, &result);
78 if (!surfaces[0]) {
79 fprintf(stderr, "Failed to allocate surface 0: %s\n", strerror(-result));
Tao Bao8f0e21b2017-02-07 13:00:18 -080080 goto done;
81 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070082
Tao Bao1b18cf52018-10-31 00:12:11 -070083 surfaces[1] = GRSurfaceAdf::Create(intf_fd, &intf_data.current_mode, format, &result);
84 if (!surfaces[1]) {
85 fprintf(stderr, "Failed to allocate surface 1: %s\n", strerror(-result));
Tao Bao557fa1f2017-02-07 12:51:00 -080086 n_surfaces = 1;
Tao Bao8f0e21b2017-02-07 13:00:18 -080087 } else {
Tao Bao557fa1f2017-02-07 12:51:00 -080088 n_surfaces = 2;
Tao Bao8f0e21b2017-02-07 13:00:18 -080089 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070090
91done:
Tao Bao8f0e21b2017-02-07 13:00:18 -080092 adf_free_interface_data(&intf_data);
Tao Bao1b18cf52018-10-31 00:12:11 -070093 return result;
Greg Hackmann41909dd2014-04-25 10:39:50 -070094}
95
Tao Bao557fa1f2017-02-07 12:51:00 -080096int MinuiBackendAdf::DeviceInit(adf_device* dev) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080097 adf_id_t intf_id;
Tao Bao557fa1f2017-02-07 12:51:00 -080098 int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080099 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700100
Tao Bao557fa1f2017-02-07 12:51:00 -0800101 err = adf_device_attach(dev, eng_id, intf_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800102 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700103
Tao Baob5492432019-01-16 09:29:17 -0800104 intf_fd = adf_interface_open(dev, intf_id, O_RDWR | O_CLOEXEC);
Tao Bao557fa1f2017-02-07 12:51:00 -0800105 if (intf_fd < 0) return intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700106
Tao Bao557fa1f2017-02-07 12:51:00 -0800107 err = InterfaceInit();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800108 if (err < 0) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800109 close(intf_fd);
110 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800111 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700112
Tao Bao8f0e21b2017-02-07 13:00:18 -0800113 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700114}
115
Tao Bao557fa1f2017-02-07 12:51:00 -0800116GRSurface* MinuiBackendAdf::Init() {
Tao Baoed876a72018-07-31 21:32:50 -0700117 PixelFormat pixel_format = gr_pixel_format();
118 if (pixel_format == PixelFormat::ABGR) {
119 format = DRM_FORMAT_ABGR8888;
120 } else if (pixel_format == PixelFormat::BGRA) {
121 format = DRM_FORMAT_BGRA8888;
122 } else if (pixel_format == PixelFormat::RGBX) {
123 format = DRM_FORMAT_RGBX8888;
124 } else {
125 format = DRM_FORMAT_RGB565;
126 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700127
Tao Bao8f0e21b2017-02-07 13:00:18 -0800128 adf_id_t* dev_ids = nullptr;
129 ssize_t n_dev_ids = adf_devices(&dev_ids);
130 if (n_dev_ids == 0) {
131 return nullptr;
132 } else if (n_dev_ids < 0) {
133 fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
134 return nullptr;
135 }
136
Tao Bao557fa1f2017-02-07 12:51:00 -0800137 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800138
Tao Bao557fa1f2017-02-07 12:51:00 -0800139 for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) {
140 int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800141 if (err < 0) {
142 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
143 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700144 }
145
Tao Bao557fa1f2017-02-07 12:51:00 -0800146 err = DeviceInit(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800147 if (err < 0) {
148 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -0800149 adf_device_close(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800150 }
151 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700152
Tao Bao8f0e21b2017-02-07 13:00:18 -0800153 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700154
Tao Bao557fa1f2017-02-07 12:51:00 -0800155 if (intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700156
Tao Bao557fa1f2017-02-07 12:51:00 -0800157 GRSurface* ret = Flip();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800158
Tao Bao557fa1f2017-02-07 12:51:00 -0800159 Blank(true);
160 Blank(false);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800161
162 return ret;
163}
164
Tao Bao557fa1f2017-02-07 12:51:00 -0800165void MinuiBackendAdf::Sync(GRSurfaceAdf* surf) {
Tao Bao1b18cf52018-10-31 00:12:11 -0700166 static constexpr unsigned int kWarningTimeout = 3000;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800167
168 if (surf == nullptr) return;
169
170 if (surf->fence_fd >= 0) {
Tao Bao1b18cf52018-10-31 00:12:11 -0700171 int err = sync_wait(surf->fence_fd, kWarningTimeout);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800172 if (err < 0) {
173 perror("adf sync fence wait error\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700174 }
175
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800176 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800177 surf->fence_fd = -1;
178 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700179}
180
Tao Bao557fa1f2017-02-07 12:51:00 -0800181GRSurface* MinuiBackendAdf::Flip() {
Tao Bao1b18cf52018-10-31 00:12:11 -0700182 const auto& surf = surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700183
Tao Bao557fa1f2017-02-07 12:51:00 -0800184 int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format,
185 surf->fd, surf->offset, surf->pitch, -1);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800186 if (fence_fd >= 0) surf->fence_fd = fence_fd;
187
Tao Bao557fa1f2017-02-07 12:51:00 -0800188 current_surface = (current_surface + 1) % n_surfaces;
Tao Bao1b18cf52018-10-31 00:12:11 -0700189 Sync(surfaces[current_surface].get());
190 return surfaces[current_surface].get();
Greg Hackmann41909dd2014-04-25 10:39:50 -0700191}
192
Tao Bao557fa1f2017-02-07 12:51:00 -0800193void MinuiBackendAdf::Blank(bool blank) {
194 adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800195}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700196
Tao Bao557fa1f2017-02-07 12:51:00 -0800197MinuiBackendAdf::~MinuiBackendAdf() {
198 adf_device_close(&dev);
Tao Bao557fa1f2017-02-07 12:51:00 -0800199 if (intf_fd >= 0) close(intf_fd);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700200}