Serenity Operating System
at master 75 lines 2.4 kB view raw
1/* 2 * Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org> 3 * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <AK/HashMap.h> 9#include <AK/WeakPtr.h> 10#include <LibGPU/Driver.h> 11#include <dlfcn.h> 12 13namespace GPU { 14 15// FIXME: Think of a better way to configure these paths. Maybe use ConfigServer? 16// clang-format off 17static HashMap<StringView, char const*> const s_driver_path_map 18{ 19#if defined(AK_OS_SERENITY) 20 { "softgpu"sv, "libsoftgpu.so.serenity" }, 21 { "virtgpu"sv, "libvirtgpu.so.serenity" }, 22#elif defined(AK_OS_MACOS) 23 { "softgpu"sv, "liblagom-softgpu.dylib" }, 24#else 25 { "softgpu"sv, "liblagom-softgpu.so.0" }, 26#endif 27}; 28// clang-format on 29 30static HashMap<DeprecatedString, WeakPtr<Driver>> s_loaded_drivers; 31 32ErrorOr<NonnullRefPtr<Driver>> Driver::try_create(StringView driver_name) 33{ 34 // Check if the library for this driver is already loaded 35 auto already_loaded_driver = s_loaded_drivers.find(driver_name); 36 if (already_loaded_driver != s_loaded_drivers.end() && !already_loaded_driver->value.is_null()) 37 return *already_loaded_driver->value; 38 39 // Nope, we need to load the library 40 auto it = s_driver_path_map.find(driver_name); 41 if (it == s_driver_path_map.end()) 42 return Error::from_string_literal("The requested GPU driver was not found in the list of allowed driver libraries"); 43 44 auto lib = dlopen(it->value, RTLD_NOW); 45 if (!lib) 46 return Error::from_string_literal("The library for the requested GPU driver could not be opened"); 47 48 auto serenity_gpu_create_device = reinterpret_cast<serenity_gpu_create_device_t>(dlsym(lib, "serenity_gpu_create_device")); 49 if (!serenity_gpu_create_device) { 50 dlclose(lib); 51 return Error::from_string_literal("The library for the requested GPU driver does not contain serenity_gpu_create_device()"); 52 } 53 54 auto driver = adopt_ref(*new Driver(lib, serenity_gpu_create_device)); 55 56 s_loaded_drivers.set(driver_name, driver->make_weak_ptr()); 57 58 return driver; 59} 60 61Driver::~Driver() 62{ 63 dlclose(m_dlopen_result); 64} 65 66ErrorOr<NonnullOwnPtr<Device>> Driver::try_create_device(Gfx::IntSize size) 67{ 68 auto device_or_null = m_serenity_gpu_create_device(size); 69 if (!device_or_null) 70 return Error::from_string_literal("Could not create GPU device"); 71 72 return adopt_own(*device_or_null); 73} 74 75}