1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #ifdef __cplusplus
17 #include <functional>
18 #include <future>
19 #endif
20 
21 /* virtio-gpu interface for buffers
22  * (triggered by minigbm/egl calling virtio-gpu ioctls) */
23 typedef void (*create_buffer_with_handle_t)(uint64_t size, uint32_t handle);
24 
25 /* virtio-gpu interface for color buffers
26  * (triggered by minigbm/egl calling virtio-gpu ioctls) */
27 typedef void (*create_color_buffer_with_handle_t)(uint32_t width, uint32_t height, uint32_t format,
28                                                   uint32_t fwkFormat, uint32_t handle, bool linear);
29 
30 /* create YUV textures with given width and height
31    type: FRAMEWORK_FORMAT_NV12 or FRAMEWORK_FORMAT_YUV_420_888
32    count: how many set of textures will be created
33    output: caller allocated storage to hold results: size should
34    be 2*count for NV12 or 3*cout for YUV_420_888
35  */
36 typedef void (*create_yuv_textures_t)(uint32_t type,
37                                       uint32_t count,
38                                       int width,
39                                       int height,
40                                       uint32_t* output);
41 
42 /* destroy YUV textures */
43 typedef void (*destroy_yuv_textures_t)(uint32_t type,
44                                        uint32_t count,
45                                        uint32_t* textures);
46 /* call the user provided func with privData to update the content
47    of textures.
48    type: FRAMEWORK_FORMAT_NV12 or FRAMEWORK_FORMAT_YUV_420_888
49    textures: size 2 (NV12) or 3 (YUV420)
50  */
51 typedef void (*update_yuv_textures_t)(uint32_t type,
52                                       uint32_t* textures,
53                                       void* privData,
54                                       void* func);
55 
56 /* swap out the yuv textures of the colorbuffer, and use the new yuv textures
57  * to update colorbuffer content; on return, textures will have the retired
58  * yuv textures that are free to hold new data
59  */
60 typedef void (*swap_textures_and_update_color_buffer_t)(
61         uint32_t colorbufferhandle,
62         int x,
63         int y,
64         int width,
65         int height,
66         uint32_t format,
67         uint32_t type,
68         uint32_t texture_type,
69         uint32_t* textures,
70         void* metadata);
71 
72 typedef void (*open_color_buffer_t)(uint32_t handle);
73 typedef void (*close_buffer_t)(uint32_t handle);
74 typedef void (*close_color_buffer_t)(uint32_t handle);
75 typedef void (*update_buffer_t)(uint32_t handle, uint64_t offset, uint64_t sizeToRead, void* bytes);
76 typedef void (*update_color_buffer_t)(
77     uint32_t handle, int x, int y, int width, int height,
78     uint32_t format, uint32_t type, void* pixels);
79 typedef void (*read_buffer_t)(uint32_t handle, uint64_t offset, uint64_t sizeToRead, void* bytes);
80 typedef void (*read_color_buffer_t)(
81     uint32_t handle, int x, int y, int width, int height,
82     uint32_t format, uint32_t type, void* pixels);
83 typedef void (*read_color_buffer2_t)(
84     uint32_t handle, int x, int y, int width, int height,
85     uint32_t format, uint32_t type, void* pixels, uint64_t pixels_size);
86 typedef void (*read_color_buffer_yuv_t)(
87     uint32_t handle, int x, int y, int width, int height,
88     void* pixels, uint32_t pixels_size);
89 typedef void (*post_color_buffer_t)(uint32_t handle);
90 #ifdef __cplusplus
91 using CpuCompletionCallback = std::function<void(std::shared_future<void> waitForGpu)>;
92 typedef void (*async_post_color_buffer_t)(uint32_t, CpuCompletionCallback);
93 #else
94 typedef void* async_post_color_buffer_t;
95 #endif
96 typedef void (*repost_t)(void);
97 typedef uint32_t (*get_last_posted_color_buffer_t)(void);
98 typedef void (*bind_color_buffer_to_texture_t)(uint32_t);
99 typedef void* (*get_global_egl_context_t)(void);
100 typedef void (*set_guest_managed_color_buffer_lifetime_t)(bool guest_managed);
101 typedef void (*update_color_buffer_from_framework_format_t)(uint32_t handle,
102                                                             int x,
103                                                             int y,
104                                                             int width,
105                                                             int height,
106                                                             uint32_t fwkFormat,
107                                                             uint32_t format,
108                                                             uint32_t type,
109                                                             void* pixels,
110                                                             void* metadata);
111 
112 #ifdef __cplusplus
113 using FenceCompletionCallback = std::function<void()>;
114 typedef void (*async_wait_for_gpu_with_cb_t)(uint64_t eglsync, FenceCompletionCallback);
115 typedef void (*async_wait_for_gpu_vulkan_with_cb_t)(uint64_t device, uint64_t fence, FenceCompletionCallback);
116 typedef void (*async_wait_for_gpu_vulkan_qsri_with_cb_t)(uint64_t image, FenceCompletionCallback);
117 #else
118 typedef void* async_wait_for_gpu_with_cb_t;
119 typedef void* async_wait_for_gpu_vulkan_with_cb_t;
120 typedef void* async_wait_for_gpu_vulkan_qsri_with_cb_t;
121 #endif
122 
123 struct AndroidVirtioGpuOps {
124     create_buffer_with_handle_t create_buffer_with_handle;
125     create_color_buffer_with_handle_t create_color_buffer_with_handle;
126     open_color_buffer_t open_color_buffer;
127     close_buffer_t close_buffer;
128     close_color_buffer_t close_color_buffer;
129     update_buffer_t update_buffer;
130     update_color_buffer_t update_color_buffer;
131     read_buffer_t read_buffer;
132     read_color_buffer_t read_color_buffer;
133     read_color_buffer2_t read_color_buffer2;
134     read_color_buffer_yuv_t read_color_buffer_yuv;
135     post_color_buffer_t post_color_buffer;
136     async_post_color_buffer_t async_post_color_buffer;
137     repost_t repost;
138     /* yuv texture related */
139     create_yuv_textures_t create_yuv_textures;
140     destroy_yuv_textures_t destroy_yuv_textures;
141     update_yuv_textures_t update_yuv_textures;
142     swap_textures_and_update_color_buffer_t
143             swap_textures_and_update_color_buffer;
144     /* virtual device widget related */
145     get_last_posted_color_buffer_t get_last_posted_color_buffer;
146     bind_color_buffer_to_texture_t bind_color_buffer_to_texture;
147     get_global_egl_context_t get_global_egl_context;
148 
149     set_guest_managed_color_buffer_lifetime_t set_guest_managed_color_buffer_lifetime;
150     async_wait_for_gpu_with_cb_t async_wait_for_gpu_with_cb;
151     async_wait_for_gpu_vulkan_with_cb_t async_wait_for_gpu_vulkan_with_cb;
152     async_wait_for_gpu_vulkan_qsri_with_cb_t async_wait_for_gpu_vulkan_qsri_with_cb;
153 
154     update_color_buffer_from_framework_format_t update_color_buffer_from_framework_format;
155 };
156