diff options
author | Tao Bao | 2017-02-09 14:07:08 -0600 |
---|---|---|
committer | Gerrit Code Review | 2017-02-09 14:07:09 -0600 |
commit | d592e1d5e1d8dc9d4d51ccf2479c59b33a0edba5 (patch) | |
tree | 48f27f815502a2d5a17a98760f9b04a4e633bca3 /minui | |
parent | 648f252c71b179f394dbc9a21b43402255d90d6e (diff) | |
parent | 8f0e21b2710ecbac64f84352ae104246e7bc1647 (diff) | |
download | platform-bootable-recovery-d592e1d5e1d8dc9d4d51ccf2479c59b33a0edba5.tar.gz platform-bootable-recovery-d592e1d5e1d8dc9d4d51ccf2479c59b33a0edba5.tar.xz platform-bootable-recovery-d592e1d5e1d8dc9d4d51ccf2479c59b33a0edba5.zip |
Merge "minui: Clean up graphics_adf.cpp."
Diffstat (limited to 'minui')
-rw-r--r-- | minui/graphics_adf.cpp | 347 |
1 files changed, 156 insertions, 191 deletions
diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index 9e262b04..17f30d1d 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp | |||
@@ -16,14 +16,10 @@ | |||
16 | 16 | ||
17 | #include <errno.h> | 17 | #include <errno.h> |
18 | #include <fcntl.h> | 18 | #include <fcntl.h> |
19 | #include <stdbool.h> | ||
20 | #include <stdio.h> | 19 | #include <stdio.h> |
21 | #include <stdlib.h> | 20 | #include <stdlib.h> |
22 | #include <string.h> | ||
23 | #include <unistd.h> | ||
24 | |||
25 | #include <sys/cdefs.h> | ||
26 | #include <sys/mman.h> | 21 | #include <sys/mman.h> |
22 | #include <unistd.h> | ||
27 | 23 | ||
28 | #include <adf/adf.h> | 24 | #include <adf/adf.h> |
29 | #include <sync/sync.h> | 25 | #include <sync/sync.h> |
@@ -31,243 +27,212 @@ | |||
31 | #include "graphics.h" | 27 | #include "graphics.h" |
32 | 28 | ||
33 | struct adf_surface_pdata { | 29 | struct adf_surface_pdata { |
34 | GRSurface base; | 30 | GRSurface base; |
35 | int fence_fd; | 31 | int fence_fd; |
36 | int fd; | 32 | int fd; |
37 | __u32 offset; | 33 | __u32 offset; |
38 | __u32 pitch; | 34 | __u32 pitch; |
39 | }; | 35 | }; |
40 | 36 | ||
41 | struct adf_pdata { | 37 | struct adf_pdata { |
42 | minui_backend base; | 38 | minui_backend base; |
43 | int intf_fd; | 39 | int intf_fd; |
44 | adf_id_t eng_id; | 40 | adf_id_t eng_id; |
45 | __u32 format; | 41 | __u32 format; |
46 | 42 | ||
47 | adf_device dev; | 43 | adf_device dev; |
48 | 44 | ||
49 | unsigned int current_surface; | 45 | unsigned int current_surface; |
50 | unsigned int n_surfaces; | 46 | unsigned int n_surfaces; |
51 | adf_surface_pdata surfaces[2]; | 47 | adf_surface_pdata surfaces[2]; |
52 | }; | 48 | }; |
53 | 49 | ||
54 | static GRSurface* adf_flip(minui_backend *backend); | 50 | static GRSurface* adf_flip(minui_backend* backend); |
55 | static void adf_blank(minui_backend *backend, bool blank); | 51 | static void adf_blank(minui_backend* backend, bool blank); |
56 | 52 | ||
57 | static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) { | 53 | static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surface_pdata* surf) { |
58 | memset(surf, 0, sizeof(*surf)); | 54 | *surf = {}; |
59 | 55 | surf->fence_fd = -1; | |
60 | surf->fence_fd = -1; | 56 | surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, mode->vdisplay, |
61 | surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, | 57 | pdata->format, &surf->offset, &surf->pitch); |
62 | mode->vdisplay, pdata->format, &surf->offset, &surf->pitch); | 58 | if (surf->fd < 0) { |
63 | if (surf->fd < 0) | 59 | return surf->fd; |
64 | return surf->fd; | 60 | } |
65 | 61 | ||
66 | surf->base.width = mode->hdisplay; | 62 | surf->base.width = mode->hdisplay; |
67 | surf->base.height = mode->vdisplay; | 63 | surf->base.height = mode->vdisplay; |
68 | surf->base.row_bytes = surf->pitch; | 64 | surf->base.row_bytes = surf->pitch; |
69 | surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; | 65 | surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; |
70 | 66 | ||
71 | surf->base.data = static_cast<uint8_t*>(mmap(NULL, | 67 | surf->base.data = static_cast<uint8_t*>(mmap(nullptr, surf->pitch * surf->base.height, PROT_WRITE, |
72 | surf->pitch * surf->base.height, PROT_WRITE, | 68 | MAP_SHARED, surf->fd, surf->offset)); |
73 | MAP_SHARED, surf->fd, surf->offset)); | 69 | if (surf->base.data == MAP_FAILED) { |
74 | if (surf->base.data == MAP_FAILED) { | 70 | close(surf->fd); |
75 | close(surf->fd); | 71 | return -errno; |
76 | return -errno; | 72 | } |
77 | } | ||
78 | 73 | ||
79 | return 0; | 74 | return 0; |
80 | } | 75 | } |
81 | 76 | ||
82 | static int adf_interface_init(adf_pdata *pdata) | 77 | static int adf_interface_init(adf_pdata* pdata) { |
83 | { | 78 | adf_interface_data intf_data; |
84 | adf_interface_data intf_data; | 79 | int err = adf_get_interface_data(pdata->intf_fd, &intf_data); |
85 | int ret = 0; | 80 | if (err < 0) return err; |
86 | int err; | 81 | |
87 | 82 | int ret = 0; | |
88 | err = adf_get_interface_data(pdata->intf_fd, &intf_data); | 83 | err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); |
89 | if (err < 0) | 84 | if (err < 0) { |
90 | return err; | 85 | fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); |
91 | 86 | ret = err; | |
92 | err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); | 87 | goto done; |
93 | if (err < 0) { | 88 | } |
94 | fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); | 89 | |
95 | ret = err; | 90 | err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[1]); |
96 | goto done; | 91 | if (err < 0) { |
97 | } | 92 | fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); |
98 | 93 | pdata->surfaces[1] = {}; | |
99 | err = adf_surface_init(pdata, &intf_data.current_mode, | 94 | pdata->n_surfaces = 1; |
100 | &pdata->surfaces[1]); | 95 | } else { |
101 | if (err < 0) { | 96 | pdata->n_surfaces = 2; |
102 | fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); | 97 | } |
103 | memset(&pdata->surfaces[1], 0, sizeof(pdata->surfaces[1])); | ||
104 | pdata->n_surfaces = 1; | ||
105 | } else { | ||
106 | pdata->n_surfaces = 2; | ||
107 | } | ||
108 | 98 | ||
109 | done: | 99 | done: |
110 | adf_free_interface_data(&intf_data); | 100 | adf_free_interface_data(&intf_data); |
111 | return ret; | 101 | return ret; |
112 | } | 102 | } |
113 | 103 | ||
114 | static int adf_device_init(adf_pdata *pdata, adf_device *dev) | 104 | static int adf_device_init(adf_pdata* pdata, adf_device* dev) { |
115 | { | 105 | adf_id_t intf_id; |
116 | adf_id_t intf_id; | 106 | int err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, &pdata->eng_id); |
117 | int intf_fd; | 107 | if (err < 0) return err; |
118 | int err; | ||
119 | 108 | ||
120 | err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, | 109 | err = adf_device_attach(dev, pdata->eng_id, intf_id); |
121 | &pdata->eng_id); | 110 | if (err < 0 && err != -EALREADY) return err; |
122 | if (err < 0) | ||
123 | return err; | ||
124 | 111 | ||
125 | err = adf_device_attach(dev, pdata->eng_id, intf_id); | 112 | pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); |
126 | if (err < 0 && err != -EALREADY) | 113 | if (pdata->intf_fd < 0) return pdata->intf_fd; |
127 | return err; | ||
128 | 114 | ||
129 | pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); | 115 | err = adf_interface_init(pdata); |
130 | if (pdata->intf_fd < 0) | 116 | if (err < 0) { |
131 | return pdata->intf_fd; | 117 | close(pdata->intf_fd); |
132 | 118 | pdata->intf_fd = -1; | |
133 | err = adf_interface_init(pdata); | 119 | } |
134 | if (err < 0) { | ||
135 | close(pdata->intf_fd); | ||
136 | pdata->intf_fd = -1; | ||
137 | } | ||
138 | 120 | ||
139 | return err; | 121 | return err; |
140 | } | 122 | } |
141 | 123 | ||
142 | static GRSurface* adf_init(minui_backend *backend) | 124 | static GRSurface* adf_init(minui_backend* backend) { |
143 | { | 125 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
144 | adf_pdata *pdata = (adf_pdata *)backend; | ||
145 | adf_id_t *dev_ids = NULL; | ||
146 | ssize_t n_dev_ids, i; | ||
147 | GRSurface* ret; | ||
148 | 126 | ||
149 | #if defined(RECOVERY_ABGR) | 127 | #if defined(RECOVERY_ABGR) |
150 | pdata->format = DRM_FORMAT_ABGR8888; | 128 | pdata->format = DRM_FORMAT_ABGR8888; |
151 | #elif defined(RECOVERY_BGRA) | 129 | #elif defined(RECOVERY_BGRA) |
152 | pdata->format = DRM_FORMAT_BGRA8888; | 130 | pdata->format = DRM_FORMAT_BGRA8888; |
153 | #elif defined(RECOVERY_RGBX) | 131 | #elif defined(RECOVERY_RGBX) |
154 | pdata->format = DRM_FORMAT_RGBX8888; | 132 | pdata->format = DRM_FORMAT_RGBX8888; |
155 | #else | 133 | #else |
156 | pdata->format = DRM_FORMAT_RGB565; | 134 | pdata->format = DRM_FORMAT_RGB565; |
157 | #endif | 135 | #endif |
158 | 136 | ||
159 | n_dev_ids = adf_devices(&dev_ids); | 137 | adf_id_t* dev_ids = nullptr; |
160 | if (n_dev_ids == 0) { | 138 | ssize_t n_dev_ids = adf_devices(&dev_ids); |
161 | return NULL; | 139 | if (n_dev_ids == 0) { |
162 | } else if (n_dev_ids < 0) { | 140 | return nullptr; |
163 | fprintf(stderr, "enumerating adf devices failed: %s\n", | 141 | } else if (n_dev_ids < 0) { |
164 | strerror(-n_dev_ids)); | 142 | fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids)); |
165 | return NULL; | 143 | return nullptr; |
166 | } | 144 | } |
167 | 145 | ||
168 | pdata->intf_fd = -1; | 146 | pdata->intf_fd = -1; |
169 | 147 | ||
170 | for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { | 148 | for (ssize_t i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { |
171 | 149 | int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev); | |
172 | int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev); | 150 | if (err < 0) { |
173 | if (err < 0) { | 151 | fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err)); |
174 | fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], | 152 | continue; |
175 | strerror(-err)); | ||
176 | continue; | ||
177 | } | ||
178 | |||
179 | err = adf_device_init(pdata, &pdata->dev); | ||
180 | if (err < 0) { | ||
181 | fprintf(stderr, "initializing adf device %u failed: %s\n", | ||
182 | dev_ids[i], strerror(-err)); | ||
183 | adf_device_close(&pdata->dev); | ||
184 | } | ||
185 | } | 153 | } |
186 | 154 | ||
187 | free(dev_ids); | 155 | err = adf_device_init(pdata, &pdata->dev); |
156 | if (err < 0) { | ||
157 | fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err)); | ||
158 | adf_device_close(&pdata->dev); | ||
159 | } | ||
160 | } | ||
188 | 161 | ||
189 | if (pdata->intf_fd < 0) | 162 | free(dev_ids); |
190 | return NULL; | ||
191 | 163 | ||
192 | ret = adf_flip(backend); | 164 | if (pdata->intf_fd < 0) return nullptr; |
193 | 165 | ||
194 | adf_blank(backend, true); | 166 | GRSurface* ret = adf_flip(backend); |
195 | adf_blank(backend, false); | ||
196 | 167 | ||
197 | return ret; | 168 | adf_blank(backend, true); |
198 | } | 169 | adf_blank(backend, false); |
199 | 170 | ||
200 | static void adf_sync(adf_surface_pdata *surf) | 171 | return ret; |
201 | { | 172 | } |
202 | unsigned int warningTimeout = 3000; | ||
203 | 173 | ||
204 | if (surf == NULL) | 174 | static void adf_sync(adf_surface_pdata* surf) { |
205 | return; | 175 | static constexpr unsigned int warningTimeout = 3000; |
206 | 176 | ||
207 | if (surf->fence_fd >= 0){ | 177 | if (surf == nullptr) return; |
208 | int err = sync_wait(surf->fence_fd, warningTimeout); | ||
209 | if (err < 0) | ||
210 | perror("adf sync fence wait error\n"); | ||
211 | 178 | ||
212 | close(surf->fence_fd); | 179 | if (surf->fence_fd >= 0) { |
213 | surf->fence_fd = -1; | 180 | int err = sync_wait(surf->fence_fd, warningTimeout); |
181 | if (err < 0) { | ||
182 | perror("adf sync fence wait error\n"); | ||
214 | } | 183 | } |
184 | |||
185 | close(surf->fence_fd); | ||
186 | surf->fence_fd = -1; | ||
187 | } | ||
215 | } | 188 | } |
216 | 189 | ||
217 | static GRSurface* adf_flip(minui_backend *backend) | 190 | static GRSurface* adf_flip(minui_backend* backend) { |
218 | { | 191 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
219 | adf_pdata *pdata = (adf_pdata *)backend; | 192 | adf_surface_pdata* surf = &pdata->surfaces[pdata->current_surface]; |
220 | adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface]; | ||
221 | 193 | ||
222 | int fence_fd = adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, | 194 | int fence_fd = |
223 | surf->base.width, surf->base.height, pdata->format, surf->fd, | 195 | adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, surf->base.width, surf->base.height, |
224 | surf->offset, surf->pitch, -1); | 196 | pdata->format, surf->fd, surf->offset, surf->pitch, -1); |
225 | if (fence_fd >= 0) | 197 | if (fence_fd >= 0) surf->fence_fd = fence_fd; |
226 | surf->fence_fd = fence_fd; | ||
227 | 198 | ||
228 | pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; | 199 | pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; |
229 | adf_sync(&pdata->surfaces[pdata->current_surface]); | 200 | adf_sync(&pdata->surfaces[pdata->current_surface]); |
230 | return &pdata->surfaces[pdata->current_surface].base; | 201 | return &pdata->surfaces[pdata->current_surface].base; |
231 | } | 202 | } |
232 | 203 | ||
233 | static void adf_blank(minui_backend *backend, bool blank) | 204 | static void adf_blank(minui_backend* backend, bool blank) { |
234 | { | 205 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
235 | adf_pdata *pdata = (adf_pdata *)backend; | 206 | adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); |
236 | adf_interface_blank(pdata->intf_fd, | ||
237 | blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); | ||
238 | } | 207 | } |
239 | 208 | ||
240 | static void adf_surface_destroy(adf_surface_pdata *surf) | 209 | static void adf_surface_destroy(adf_surface_pdata* surf) { |
241 | { | 210 | munmap(surf->base.data, surf->pitch * surf->base.height); |
242 | munmap(surf->base.data, surf->pitch * surf->base.height); | 211 | close(surf->fence_fd); |
243 | close(surf->fence_fd); | 212 | close(surf->fd); |
244 | close(surf->fd); | ||
245 | } | 213 | } |
246 | 214 | ||
247 | static void adf_exit(minui_backend *backend) | 215 | static void adf_exit(minui_backend* backend) { |
248 | { | 216 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
249 | adf_pdata *pdata = (adf_pdata *)backend; | 217 | adf_device_close(&pdata->dev); |
250 | unsigned int i; | 218 | for (unsigned int i = 0; i < pdata->n_surfaces; i++) { |
251 | 219 | adf_surface_destroy(&pdata->surfaces[i]); | |
252 | adf_device_close(&pdata->dev); | 220 | } |
253 | for (i = 0; i < pdata->n_surfaces; i++) | 221 | if (pdata->intf_fd >= 0) close(pdata->intf_fd); |
254 | adf_surface_destroy(&pdata->surfaces[i]); | 222 | free(pdata); |
255 | if (pdata->intf_fd >= 0) | ||
256 | close(pdata->intf_fd); | ||
257 | free(pdata); | ||
258 | } | 223 | } |
259 | 224 | ||
260 | minui_backend *open_adf() | 225 | minui_backend* open_adf() { |
261 | { | 226 | adf_pdata* pdata = static_cast<adf_pdata*>(calloc(1, sizeof(*pdata))); |
262 | adf_pdata* pdata = static_cast<adf_pdata*>(calloc(1, sizeof(*pdata))); | 227 | if (!pdata) { |
263 | if (!pdata) { | 228 | perror("allocating adf backend failed"); |
264 | perror("allocating adf backend failed"); | 229 | return nullptr; |
265 | return NULL; | 230 | } |
266 | } | 231 | |
232 | pdata->base.init = adf_init; | ||
233 | pdata->base.flip = adf_flip; | ||
234 | pdata->base.blank = adf_blank; | ||
235 | pdata->base.exit = adf_exit; | ||
267 | 236 | ||
268 | pdata->base.init = adf_init; | 237 | return &pdata->base; |
269 | pdata->base.flip = adf_flip; | ||
270 | pdata->base.blank = adf_blank; | ||
271 | pdata->base.exit = adf_exit; | ||
272 | return &pdata->base; | ||
273 | } | 238 | } |