14#include <drm_fourcc.h>
19#include <libavutil/frame.h>
20#include <libavutil/pixfmt.h>
26#include "pyrowave_fence.h"
30using namespace std::literals;
36 constexpr EGLenum sync_native_fence_android = 0x3144;
37 using dup_native_fence_fd_fn = EGLint (*)(EGLDisplay, EGLSync);
39 struct device_deleter_t {
40 void operator()(pyrowave_device_opaque *device)
const {
41 pyrowave_device_destroy(device);
45 using device_ptr = std::unique_ptr<pyrowave_device_opaque, device_deleter_t>;
47 bool succeeded(pyrowave_result result, std::string_view operation) {
48 if (result == PYROWAVE_SUCCESS) {
51 BOOST_LOG(error) <<
"[PyroWave] "sv << operation <<
" failed with status "sv << static_cast<int>(result);
63 device_ptr create_device(
int render_fd) {
64 std::uint32_t vendor = 0;
65 std::uint32_t product = 0;
67 if (drmGetDevice2(render_fd, 0, &drm_device) == 0 && drm_device) {
69 vendor =
drm_device->deviceinfo.pci->vendor_id;
70 product =
drm_device->deviceinfo.pci->device_id;
72 drmFreeDevice(&drm_device);
75 pyrowave_device raw {};
76 if (!succeeded(pyrowave_create_device_by_compat(vendor, product,
nullptr,
nullptr,
nullptr, &raw),
"Vulkan device creation"sv)) {
79 device_ptr device {raw};
80 if (!pyrowave_device_confirm_interop_support(device.get())) {
81 BOOST_LOG(error) <<
"[PyroWave] The Vulkan driver for "sv << util::hex(vendor).to_string_view() <<
':'
82 << util::hex(product).to_string_view()
83 <<
" cannot import DMA-BUFs and sync files, so frames cannot stay on the GPU."sv;
88 pyrowave_device_set_queue_type(device.get(), VK_QUEUE_COMPUTE_BIT);
95 plane_t(
const plane_t &) =
delete;
96 plane_t &operator=(
const plane_t &) =
delete;
104 pyrowave_image_destroy(image);
114 pyrowave_image image {};
117 std::uint32_t fourcc {};
118 std::uint64_t modifier {DRM_FORMAT_MOD_INVALID};
127 bool allocate(gbm_device *gbm, plane_t &plane,
int width,
int height, std::uint32_t fourcc,
bool linear) {
130 plane.height = height;
131 plane.fourcc = fourcc;
132 plane.bo = gbm_bo_create(gbm, width, height, fourcc, GBM_BO_USE_RENDERING | (linear ? GBM_BO_USE_LINEAR : 0));
136 plane.modifier = gbm_bo_get_modifier(plane.bo);
137 if (plane.modifier == DRM_FORMAT_MOD_INVALID && linear) {
138 plane.modifier = DRM_FORMAT_MOD_LINEAR;
140 plane.plane_count = gbm_bo_get_plane_count(plane.bo);
143 if (plane.modifier == DRM_FORMAT_MOD_INVALID || plane.plane_count < 1 || plane.plane_count > 4) {
150 egl::surface_descriptor_t
describe(
const plane_t &plane,
int fd) {
151 egl::surface_descriptor_t sd {};
152 sd.width = plane.width;
153 sd.height = plane.height;
154 sd.fourcc = plane.fourcc;
155 sd.modifier = plane.modifier;
156 std::fill_n(sd.fds, 4, -1);
157 for (
int x = 0; x < plane.plane_count; ++x) {
159 sd.pitches[x] = gbm_bo_get_stride_for_plane(plane.bo, x);
160 sd.offsets[x] = gbm_bo_get_offset(plane.bo, x);
165 bool import_plane(pyrowave_device device, plane_t &plane, VkFormat format) {
166 std::array<VkSubresourceLayout, 4> layouts {};
167 for (
int x = 0; x < plane.plane_count; ++x) {
168 layouts[x].offset = gbm_bo_get_offset(plane.bo, x);
169 layouts[x].rowPitch = gbm_bo_get_stride_for_plane(plane.bo, x);
172 VkImageDrmFormatModifierExplicitCreateInfoEXT modifier_info {VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT};
173 modifier_info.drmFormatModifier = plane.modifier;
174 modifier_info.drmFormatModifierPlaneCount =
static_cast<std::uint32_t
>(plane.plane_count);
175 modifier_info.pPlaneLayouts = layouts.data();
177 VkImageCreateInfo image_info {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
178 image_info.pNext = &modifier_info;
179 image_info.imageType = VK_IMAGE_TYPE_2D;
180 image_info.format = format;
181 image_info.extent = {
static_cast<std::uint32_t
>(plane.width),
static_cast<std::uint32_t
>(plane.height), 1};
182 image_info.mipLevels = 1;
183 image_info.arrayLayers = 1;
184 image_info.samples = VK_SAMPLE_COUNT_1_BIT;
185 image_info.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
186 image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
187 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
188 image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
190 const int fd = gbm_bo_get_fd(plane.bo);
194 pyrowave_image_create_info create_info {};
195 create_info.device = device;
196 create_info.external_handle =
static_cast<pyrowave_os_handle
>(fd);
197 create_info.handle_type = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
198 create_info.image_create_info = &image_info;
202 return pyrowave_image_create(&create_info, &plane.image) == PYROWAVE_SUCCESS;
205 struct av_frame_deleter_t {
206 void operator()(AVFrame *frame)
const {
207 av_frame_free(&frame);
219 int init(
int in_width,
int in_height, file_t &&render_device,
int offset_x,
int offset_y,
bool vram, std::uint32_t ram_fourcc) {
220 file = std::move(render_device);
223 this->offset_x = offset_x;
224 this->offset_y = offset_y;
226 this->ram_fourcc = ram_fourcc;
228 if (!gbm::create_device) {
229 BOOST_LOG(warning) <<
"[PyroWave] libgbm is not loaded."sv;
232 gbm.reset(gbm::create_device(file.el));
234 BOOST_LOG(error) <<
"[PyroWave] Could not create a GBM device: "sv << std::strerror(errno);
237 display = egl::make_display(gbm.get());
241 auto ctx_opt = egl::make_ctx(display.get());
245 ctx = std::move(*ctx_opt);
247 const char *extensions = eglQueryString(display.get(), EGL_EXTENSIONS);
248 if (extensions && std::strstr(extensions,
"EGL_ANDROID_native_fence_sync") && eglCreateSync && eglDestroySync) {
249 dup_native_fence_fd =
reinterpret_cast<dup_native_fence_fd_fn
>(eglGetProcAddress(
"eglDupNativeFenceFDANDROID"));
251 if (!dup_native_fence_fd) {
252 BOOST_LOG(info) <<
"[PyroWave] EGL cannot export fences; each frame is finished on the CPU before it is encoded."sv;
255 pyro = create_device(file.el);
264 if (!import_targets(64, 64,
false) && !import_targets(64, 64,
true)) {
265 BOOST_LOG(warning) <<
"[PyroWave] This GPU cannot share planes between OpenGL and Vulkan."sv;
271 pyrowave_device device()
const override {
275 int set_frame(AVFrame *frame, AVBufferRef * )
override {
276 owned_frame.reset(frame);
278 if (frame->width <= 0 || frame->height <= 0 || (frame->width & 1) || (frame->height & 1)) {
279 BOOST_LOG(error) <<
"[PyroWave] 4:2:0 needs even dimensions, not "sv << frame->width <<
'x' << frame->height;
284 if (!import_targets(frame->width, frame->height,
false)) {
286 if (!import_targets(frame->width, frame->height,
true)) {
287 BOOST_LOG(error) <<
"[PyroWave] Could not share a "sv << frame->width <<
'x' << frame->height
288 <<
" target between OpenGL and Vulkan on this GPU."sv;
292 BOOST_LOG(info) <<
"[PyroWave] Converting "sv << (vram ?
"captured GPU buffers"sv :
"frames uploaded from system memory"sv)
293 <<
" on the GPU into "sv << (linear ?
"linear"sv :
"tiled"sv) <<
' ' << frame->width <<
'x'
294 << frame->height <<
" planes (modifier "sv << util::hex(luma.modifier).to_string_view() <<
')';
296 auto sws_opt = egl::sws_t::make(width, height, frame->width, frame->height, AV_PIX_FMT_NV12);
300 sws = std::move(*sws_opt);
304 void apply_colorspace()
override {
305 sws.apply_colorspace(colorspace);
312 if (sws.load_ram(img, ram_fourcc)) {
315 sws.convert(nv12->buf);
317 fence_fd = export_fence();
324 if (descriptor.sequence == 0) {
327 }
else if (descriptor.sequence > sequence) {
328 sequence = descriptor.sequence;
330 auto rgb_opt = egl::import_source(display.get(), descriptor.sd);
334 rgb = std::move(*rgb_opt);
337 sws.load_vram(descriptor, offset_x, offset_y, rgb->tex[0]);
338 sws.convert(nv12->buf);
341 fence_fd = export_fence();
346 bool planes(pyrowave_gpu_buffers &out,
const pyrowave_gpu_sync_operation *&acquire,
const pyrowave_gpu_sync_operation *&release)
override {
352 acquire_op.sync = {};
354 pyrowave_sync_object_create_info sync_info {};
355 sync_info.device = pyro.get();
356 sync_info.external_handle =
static_cast<pyrowave_os_handle
>(fence_fd);
357 sync_info.handle_type = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
358 sync_info.semaphore_type = VK_SEMAPHORE_TYPE_BINARY;
359 sync_info.import_flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT;
360 if (pyrowave_sync_object_create(&sync_info, &sync) == PYROWAVE_SUCCESS) {
362 acquire_op.sync.semaphore = pyrowave_sync_object_get_semaphore(sync);
367 const bool ready = wait_for_conversion_fence(fence_fd, 1000ms);
371 BOOST_LOG(warning) <<
"[PyroWave] Conversion fence timed out or failed; dropping the frame."sv;
378 acquire = &acquire_op;
379 release = &release_op;
383 void encoded()
override {
395 if (dup_native_fence_fd) {
396 const EGLAttrib attribs[] {EGL_NONE};
397 EGLSync fence = eglCreateSync(display.get(), sync_native_fence_android, attribs);
398 if (fence != EGL_NO_SYNC) {
401 const int fd = dup_native_fence_fd(display.get(), fence);
402 eglDestroySync(display.get(), fence);
412 bool import_targets(
int out_width,
int out_height,
bool linear) {
413 nv12 = egl::nv12_t {};
414 auto *native =
reinterpret_cast<gbm_device *
>(gbm.get());
415 if (!allocate(native, luma, out_width, out_height, DRM_FORMAT_R8, linear) || !allocate(native, chroma, out_width / 2, out_height / 2, DRM_FORMAT_GR88, linear)) {
419 std::array<file_t, egl::nv12_img_t::num_fds> fds;
420 fds[0] = gbm_bo_get_fd(luma.bo);
421 fds[1] = gbm_bo_get_fd(chroma.bo);
422 if (fds[0].el < 0 || fds[1].el < 0) {
425 const auto luma_sd = describe(luma, fds[0].el);
426 const auto chroma_sd = describe(chroma, fds[1].el);
427 auto nv12_opt = egl::import_target(display.get(), std::move(fds), luma_sd, chroma_sd);
433 if (!import_plane(pyro.get(), luma, VK_FORMAT_R8_UNORM) || !import_plane(pyro.get(), chroma, VK_FORMAT_R8G8_UNORM)) {
434 BOOST_LOG(info) <<
"[PyroWave] Vulkan could not import the "sv << (linear ?
"linear" :
"driver-chosen")
435 <<
" layout (modifier "sv << util::hex(luma.modifier).to_string_view() <<
')';
440 if (!succeeded(pyrowave_image_get_image_view(luma.image, VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_USAGE_SAMPLED_BIT, &buffers.planes[0]),
"luma view"sv) || !succeeded(pyrowave_image_get_image_view(chroma.image, VK_IMAGE_ASPECT_PLANE_1_BIT, VK_IMAGE_USAGE_SAMPLED_BIT, &buffers.planes[1]),
"Cb view"sv) || !succeeded(pyrowave_image_get_image_view(chroma.image, VK_IMAGE_ASPECT_PLANE_2_BIT, VK_IMAGE_USAGE_SAMPLED_BIT, &buffers.planes[2]),
"Cr view"sv)) {
445 references[0] = {luma.image, VK_QUEUE_FAMILY_EXTERNAL};
446 references[1] = {chroma.image, VK_QUEUE_FAMILY_EXTERNAL};
447 acquire_op = {references.data(), references.size(), {}};
448 release_op = {references.data(), references.size(), {}};
450 nv12 = std::move(*nv12_opt);
463 pyrowave_sync_object_destroy(sync);
473 egl::display_t display;
479 pyrowave_gpu_buffers buffers {};
480 std::array<pyrowave_gpu_external_reference, 2> references {};
481 pyrowave_gpu_sync_operation acquire_op {};
482 pyrowave_gpu_sync_operation release_op {};
483 pyrowave_sync_object sync {};
485 bool converted {
false};
487 std::unique_ptr<AVFrame, av_frame_deleter_t> owned_frame;
491 std::uint64_t sequence {};
493 dup_native_fence_fd_fn dup_native_fence_fd {};
495 std::uint32_t ram_fourcc {};
503 file_t open_adapter() {
504 const auto *render_device = config::video.adapter_name.empty() ?
"/dev/dri/renderD128" : config::video.adapter_name.c_str();
505 file_t file = ::open(render_device, O_RDWR | O_CLOEXEC);
507 BOOST_LOG(error) <<
"[PyroWave] Could not open "sv << render_device <<
": "sv << std::strerror(errno);
513 std::unique_ptr<gpu_encode_device_t> make_gpu_encode_device(
int width,
int height, file_t &&render_device,
int offset_x,
int offset_y) {
514 auto device = std::make_unique<egl_encode_device_t>();
515 if (device->init(width, height, std::move(render_device), offset_x, offset_y,
true, 0)) {
521 std::unique_ptr<gpu_encode_device_t> make_gpu_encode_device(
int width,
int height,
int offset_x,
int offset_y) {
522 file_t file = open_adapter();
526 return make_gpu_encode_device(width, height, std::move(file), offset_x, offset_y);
529 std::unique_ptr<platf::avcodec_encode_device_t> make_ram_encode_device(
int width,
int height, file_t &&render_device, std::uint32_t fourcc) {
530 if (render_device.el >= 0) {
531 auto device = std::make_unique<egl_encode_device_t>();
532 if (!device->init(width, height, std::move(render_device), 0, 0,
false, fourcc)) {
536 BOOST_LOG(warning) <<
"[PyroWave] Frames will be converted on the CPU, which takes several milliseconds per frame."sv;
537 return std::make_unique<platf::avcodec_encode_device_t>();
540 std::unique_ptr<platf::avcodec_encode_device_t> make_ram_encode_device(
int width,
int height, std::uint32_t fourcc) {
541 return make_ram_encode_device(width, height, open_adapter(), fourcc);
Declarations for the configuration of Sunshine.
@ drm_device
–drm-device, the shorter downstream patch reported in Hermes#57.
rgb_t create_blank(platf::img_t &img)
Create a black RGB texture of the specified image size.
Declarations for graphics related functions.
Declarations for logging related functions.
std::string_view describe(virtual_display_source_e source)
GPU conversion for the experimental PyroWave codec.