| // Copyright 2016 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "magma.h" |
| #include "magma_sysmem.h" |
| #include "magma_util/command_buffer.h" |
| #include "magma_util/macros.h" |
| #include "platform_connection.h" |
| #include "platform_connection_client.h" |
| #include "platform_handle.h" |
| #include "platform_port.h" |
| #include "platform_semaphore.h" |
| #include "platform_sysmem_connection.h" |
| #include "platform_thread.h" |
| #include "platform_trace.h" |
| #include <chrono> |
| #include <vector> |
| |
| magma_status_t magma_create_connection2(int32_t file_descriptor, magma_connection_t* connection_out) |
| { |
| return magma_create_connection(file_descriptor, connection_out); |
| } |
| |
| magma_status_t magma_create_connection(int32_t file_descriptor, magma_connection_t* connection_out) |
| { |
| uint32_t primary_channel; |
| uint32_t notification_channel; |
| if (!magma::PlatformConnectionClient::GetHandles(file_descriptor, &primary_channel, |
| ¬ification_channel)) |
| return DRET_MSG(MAGMA_STATUS_INVALID_ARGS, "couldn't get handles from file_descriptor %d", |
| file_descriptor); |
| |
| *connection_out = |
| magma::PlatformConnectionClient::Create(primary_channel, notification_channel).release(); |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_release_connection(magma_connection_t connection) |
| { |
| // TODO(MA-109): close the connection |
| delete magma::PlatformConnectionClient::cast(connection); |
| } |
| |
| magma_status_t magma_get_error(magma_connection_t connection) |
| { |
| return magma::PlatformConnectionClient::cast(connection)->GetError(); |
| } |
| |
| magma_status_t magma_query(int fd, uint64_t id, uint64_t* value_out) |
| { |
| if (!value_out) |
| return DRET_MSG(MAGMA_STATUS_INVALID_ARGS, "bad value_out address"); |
| |
| if (!magma::PlatformConnectionClient::Query(fd, id, value_out)) |
| return DRET_MSG(MAGMA_STATUS_INTERNAL_ERROR, |
| "magma::PlatformConnectionClient::Query failed"); |
| |
| DLOG("magma_query id %" PRIu64 " returned 0x%" PRIx64, id, *value_out); |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_create_context(magma_connection_t connection, uint32_t* context_id_out) |
| { |
| magma::PlatformConnectionClient::cast(connection)->CreateContext(context_id_out); |
| } |
| |
| void magma_release_context(magma_connection_t connection, uint32_t context_id) |
| { |
| magma::PlatformConnectionClient::cast(connection)->DestroyContext(context_id); |
| } |
| |
| magma_status_t magma_create_buffer(magma_connection_t connection, uint64_t size, uint64_t* size_out, |
| magma_buffer_t* buffer_out) |
| { |
| auto platform_buffer = magma::PlatformBuffer::Create(size, "magma_create_buffer"); |
| if (!platform_buffer) |
| return DRET(MAGMA_STATUS_MEMORY_ERROR); |
| |
| magma_status_t result = |
| magma::PlatformConnectionClient::cast(connection)->ImportBuffer(platform_buffer.get()); |
| if (result != MAGMA_STATUS_OK) |
| return DRET(result); |
| |
| *size_out = platform_buffer->size(); |
| *buffer_out = |
| reinterpret_cast<magma_buffer_t>(platform_buffer.release()); // Ownership passed across abi |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_release_buffer(magma_connection_t connection, magma_buffer_t buffer) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| magma::PlatformConnectionClient::cast(connection)->ReleaseBuffer(platform_buffer->id()); |
| delete platform_buffer; |
| } |
| |
| magma_status_t magma_set_cache_policy(magma_buffer_t buffer, magma_cache_policy_t policy) |
| { |
| bool result = reinterpret_cast<magma::PlatformBuffer*>(buffer)->SetCachePolicy(policy); |
| return result ? MAGMA_STATUS_OK : MAGMA_STATUS_INTERNAL_ERROR; |
| } |
| |
| uint64_t magma_get_buffer_id(magma_buffer_t buffer) |
| { |
| return reinterpret_cast<magma::PlatformBuffer*>(buffer)->id(); |
| } |
| |
| uint64_t magma_get_buffer_size(magma_buffer_t buffer) |
| { |
| return reinterpret_cast<magma::PlatformBuffer*>(buffer)->size(); |
| } |
| |
| magma_status_t magma_duplicate_handle(uint32_t buffer_handle, uint32_t* buffer_handle_out) |
| { |
| if (!magma::PlatformHandle::duplicate_handle(buffer_handle, buffer_handle_out)) |
| return DRET(MAGMA_STATUS_INTERNAL_ERROR); |
| return MAGMA_STATUS_OK; |
| } |
| |
| int magma_get_notification_channel_fd(magma_connection_t connection) |
| { |
| return magma::PlatformConnectionClient::cast(connection)->GetNotificationChannelFd(); |
| } |
| |
| uint32_t magma_get_notification_channel_handle(magma_connection_t connection) |
| { |
| return magma::PlatformConnectionClient::cast(connection)->GetNotificationChannelHandle(); |
| } |
| |
| magma_status_t magma_read_notification_channel(magma_connection_t connection, void* buffer, |
| uint64_t buffer_size, uint64_t* buffer_size_out) |
| { |
| return magma::PlatformConnectionClient::cast(connection) |
| ->ReadNotificationChannel(buffer, buffer_size, buffer_size_out); |
| } |
| |
| magma_status_t magma_clean_cache(magma_buffer_t buffer, uint64_t offset, uint64_t size, |
| magma_cache_operation_t operation) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| bool invalidate; |
| switch (operation) { |
| case MAGMA_CACHE_OPERATION_CLEAN: |
| invalidate = false; |
| break; |
| case MAGMA_CACHE_OPERATION_CLEAN_INVALIDATE: |
| invalidate = true; |
| break; |
| default: |
| return DRET_MSG(MAGMA_STATUS_INVALID_ARGS, "invalid cache operations"); |
| } |
| |
| bool result = platform_buffer->CleanCache(offset, size, invalidate); |
| return result ? MAGMA_STATUS_OK : MAGMA_STATUS_INTERNAL_ERROR; |
| } |
| |
| magma_status_t magma_import(magma_connection_t connection, uint32_t buffer_handle, |
| magma_buffer_t* buffer_out) |
| { |
| auto platform_buffer = magma::PlatformBuffer::Import(buffer_handle); |
| if (!platform_buffer) |
| return DRET_MSG(MAGMA_STATUS_INVALID_ARGS, "PlatformBuffer::Import failed"); |
| |
| magma_status_t result = |
| magma::PlatformConnectionClient::cast(connection)->ImportBuffer(platform_buffer.get()); |
| if (result != MAGMA_STATUS_OK) |
| return DRET_MSG(result, "ImportBuffer failed"); |
| |
| *buffer_out = reinterpret_cast<magma_buffer_t>(platform_buffer.release()); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_export(magma_connection_t connection, magma_buffer_t buffer, |
| uint32_t* buffer_handle_out) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| |
| if (!platform_buffer->duplicate_handle(buffer_handle_out)) |
| return DRET(MAGMA_STATUS_INVALID_ARGS); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_map(magma_connection_t connection, magma_buffer_t buffer, void** addr_out) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| |
| if (!platform_buffer->MapCpu(addr_out)) |
| return DRET(MAGMA_STATUS_MEMORY_ERROR); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_map_aligned(magma_connection_t connection, magma_buffer_t buffer, |
| uint64_t alignment, void** addr_out) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| |
| if (!platform_buffer->MapCpu(addr_out, alignment)) |
| return DRET(MAGMA_STATUS_MEMORY_ERROR); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_map_specific(magma_connection_t connection, magma_buffer_t buffer, |
| uint64_t addr, uint64_t offset, uint64_t length) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| |
| if (!platform_buffer->MapAtCpuAddr(addr, offset, length)) |
| return DRET(MAGMA_STATUS_MEMORY_ERROR); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_unmap(magma_connection_t connection, magma_buffer_t buffer) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| |
| if (!platform_buffer->UnmapCpu()) |
| return DRET(MAGMA_STATUS_MEMORY_ERROR); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_map_buffer_gpu(magma_connection_t connection, magma_buffer_t buffer, |
| uint64_t page_offset, uint64_t page_count, uint64_t gpu_va, |
| uint64_t map_flags) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| uint64_t buffer_id = platform_buffer->id(); |
| magma::PlatformConnectionClient::cast(connection) |
| ->MapBufferGpu(buffer_id, gpu_va, page_offset, page_count, map_flags); |
| } |
| |
| magma_status_t magma_get_buffer_cache_policy(magma_buffer_t buffer, |
| magma_cache_policy_t* cache_policy_out) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| return platform_buffer->GetCachePolicy(cache_policy_out); |
| } |
| |
| void magma_unmap_buffer_gpu(magma_connection_t connection, magma_buffer_t buffer, uint64_t gpu_va) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| uint64_t buffer_id = platform_buffer->id(); |
| magma::PlatformConnectionClient::cast(connection)->UnmapBufferGpu(buffer_id, gpu_va); |
| } |
| |
| void magma_commit_buffer(magma_connection_t connection, magma_buffer_t buffer, uint64_t page_offset, |
| uint64_t page_count) |
| { |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(buffer); |
| uint64_t buffer_id = platform_buffer->id(); |
| magma::PlatformConnectionClient::cast(connection) |
| ->CommitBuffer(buffer_id, page_offset, page_count); |
| } |
| |
| magma_status_t magma_create_command_buffer(magma_connection_t connection, uint64_t size, |
| magma_buffer_t* buffer_out) |
| { |
| auto platform_buffer = magma::PlatformBuffer::Create(size, "magma_command_buffer"); |
| if (!platform_buffer) |
| return DRET(MAGMA_STATUS_MEMORY_ERROR); |
| |
| // Mapping can be expensive (mostly due to aslr); |
| // and we want to map this buffer on submit to pull out the batch buffer id (see below). |
| // To avoid mapping twice, we do an initial map here, so that when the client does map-unmap |
| // then submits, the mapping will probably still be alive inside the platform buffer. |
| void* data; |
| platform_buffer->MapCpu(&data); |
| |
| *buffer_out = |
| reinterpret_cast<magma_buffer_t>(platform_buffer.release()); // Ownership passed across abi |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_release_command_buffer(magma_connection_t connection, magma_buffer_t command_buffer) |
| { |
| delete reinterpret_cast<magma::PlatformBuffer*>(command_buffer); |
| } |
| |
| void magma_submit_command_buffer(magma_connection_t connection, magma_buffer_t command_buffer, |
| uint32_t context_id) |
| { |
| TRACE_DURATION("magma", "submit_command_buffer"); |
| |
| auto platform_buffer = reinterpret_cast<magma::PlatformBuffer*>(command_buffer); |
| |
| class CommandBufferInterpreter : public magma::CommandBuffer { |
| public: |
| CommandBufferInterpreter(magma::PlatformBuffer* platform_buffer) |
| : platform_buffer_(platform_buffer) |
| { |
| } |
| |
| magma::PlatformBuffer* platform_buffer() override { return platform_buffer_; } |
| |
| private: |
| magma::PlatformBuffer* platform_buffer_; |
| }; |
| |
| CommandBufferInterpreter interpreter(platform_buffer); |
| if (!interpreter.Initialize()) { |
| DLOG("failed to initialize interpreter"); |
| return; |
| } |
| |
| uint32_t buffer_handle; |
| if (!platform_buffer->duplicate_handle(&buffer_handle)) { |
| DLOG("failed to duplicate handle"); |
| return; |
| } |
| |
| uint64_t ATTRIBUTE_UNUSED batch_buffer_id = |
| interpreter.resource(interpreter.batch_buffer_resource_index()).buffer_id(); |
| TRACE_FLOW_BEGIN("magma", "command_buffer", batch_buffer_id); |
| |
| magma::PlatformConnectionClient::cast(connection) |
| ->ExecuteCommandBuffer(buffer_handle, context_id); |
| |
| delete platform_buffer; |
| } |
| |
| void magma_execute_immediate_commands(magma_connection_t connection, uint32_t context_id, |
| uint64_t command_count, |
| magma_system_inline_command_buffer* command_buffers) |
| { |
| magma::PlatformConnectionClient::cast(connection) |
| ->ExecuteImmediateCommands(context_id, command_count, command_buffers); |
| } |
| |
| magma_status_t magma_create_semaphore(magma_connection_t connection, |
| magma_semaphore_t* semaphore_out) |
| { |
| auto semaphore = magma::PlatformSemaphore::Create(); |
| if (!semaphore) |
| return MAGMA_STATUS_MEMORY_ERROR; |
| |
| uint32_t handle; |
| if (!semaphore->duplicate_handle(&handle)) |
| return DRET_MSG(MAGMA_STATUS_ACCESS_DENIED, "failed to duplicate handle"); |
| |
| magma_status_t result = magma::PlatformConnectionClient::cast(connection) |
| ->ImportObject(handle, magma::PlatformObject::SEMAPHORE); |
| if (result != MAGMA_STATUS_OK) |
| return DRET_MSG(MAGMA_STATUS_INTERNAL_ERROR, "failed to ImportObject"); |
| |
| *semaphore_out = reinterpret_cast<magma_semaphore_t>(semaphore.release()); |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_release_semaphore(magma_connection_t connection, magma_semaphore_t semaphore) |
| { |
| auto platform_semaphore = reinterpret_cast<magma::PlatformSemaphore*>(semaphore); |
| magma::PlatformConnectionClient::cast(connection) |
| ->ReleaseObject(platform_semaphore->id(), magma::PlatformObject::SEMAPHORE); |
| delete platform_semaphore; |
| } |
| |
| uint64_t magma_get_semaphore_id(magma_semaphore_t semaphore) |
| { |
| return reinterpret_cast<magma::PlatformSemaphore*>(semaphore)->id(); |
| } |
| |
| void magma_signal_semaphore(magma_semaphore_t semaphore) |
| { |
| reinterpret_cast<magma::PlatformSemaphore*>(semaphore)->Signal(); |
| } |
| |
| void magma_reset_semaphore(magma_semaphore_t semaphore) |
| { |
| reinterpret_cast<magma::PlatformSemaphore*>(semaphore)->Reset(); |
| } |
| |
| magma_status_t magma_wait_semaphores(const magma_semaphore_t* semaphores, uint32_t count, |
| uint64_t timeout_ms, magma_bool_t wait_all) |
| { |
| if (count == 1) { |
| if (!reinterpret_cast<magma::PlatformSemaphore*>(semaphores[0])->WaitNoReset(timeout_ms)) |
| return MAGMA_STATUS_TIMED_OUT; |
| return MAGMA_STATUS_OK; |
| } |
| |
| std::unique_ptr<magma::PlatformPort> port = magma::PlatformPort::Create(); |
| for (uint32_t i = 0; i < count; i++) { |
| if (!reinterpret_cast<magma::PlatformSemaphore*>(semaphores[i])->WaitAsync(port.get())) |
| return DRET_MSG(MAGMA_STATUS_INTERNAL_ERROR, "WaitAsync failed"); |
| } |
| |
| if (!wait_all) { |
| uint64_t key; |
| return port->Wait(&key, timeout_ms).get(); |
| } |
| |
| auto end_time = timeout_ms == UINT64_MAX |
| ? std::chrono::steady_clock::time_point::max() |
| : std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); |
| |
| for (uint32_t i = 0; i < count; i++) { |
| uint64_t key; |
| auto time_remaining = std::chrono::duration_cast<std::chrono::milliseconds>( |
| end_time - std::chrono::steady_clock::now()); |
| magma::Status status = |
| port->Wait(&key, time_remaining.count() > 0 ? time_remaining.count() : 0); |
| if (!status) |
| return status.get(); |
| } |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_export_semaphore(magma_connection_t connection, magma_semaphore_t semaphore, |
| uint32_t* semaphore_handle_out) |
| { |
| auto platform_semaphore = reinterpret_cast<magma::PlatformSemaphore*>(semaphore); |
| |
| if (!platform_semaphore->duplicate_handle(semaphore_handle_out)) |
| return DRET_MSG(MAGMA_STATUS_INVALID_ARGS, "duplicate_handle failed"); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_import_semaphore(magma_connection_t connection, uint32_t semaphore_handle, |
| magma_semaphore_t* semaphore_out) |
| { |
| auto platform_semaphore = magma::PlatformSemaphore::Import(semaphore_handle); |
| if (!platform_semaphore) |
| return DRET_MSG(MAGMA_STATUS_INVALID_ARGS, "PlatformSemaphore::Import failed"); |
| |
| uint32_t handle; |
| if (!platform_semaphore->duplicate_handle(&handle)) |
| return DRET_MSG(MAGMA_STATUS_ACCESS_DENIED, "failed to duplicate handle"); |
| |
| magma_status_t result = magma::PlatformConnectionClient::cast(connection) |
| ->ImportObject(handle, magma::PlatformObject::SEMAPHORE); |
| if (result != MAGMA_STATUS_OK) |
| return DRET_MSG(result, "ImportObject failed: %d", result); |
| |
| *semaphore_out = reinterpret_cast<magma_semaphore_t>(platform_semaphore.release()); |
| |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t magma_sysmem_connection_create(magma_sysmem_connection_t* connection_out) |
| { |
| auto platform_connection = magma::PlatformSysmemConnection::Create(); |
| if (!platform_connection) { |
| return DRET_MSG(MAGMA_STATUS_INTERNAL_ERROR, "Failed to create sysmem connection"); |
| } |
| *connection_out = reinterpret_cast<magma_sysmem_connection_t>(platform_connection.release()); |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_sysmem_connection_release(magma_sysmem_connection_t connection) |
| { |
| delete reinterpret_cast<magma::PlatformSysmemConnection*>(connection); |
| } |
| |
| magma_status_t magma_sysmem_allocate_buffer(magma_sysmem_connection_t connection, uint32_t flags, |
| uint64_t size, uint32_t* buffer_handle_out) |
| { |
| std::unique_ptr<magma::PlatformBuffer> buffer; |
| auto sysmem_connection = reinterpret_cast<magma::PlatformSysmemConnection*>(connection); |
| |
| magma_status_t result; |
| result = sysmem_connection->AllocateBuffer(flags, size, &buffer); |
| if (result != MAGMA_STATUS_OK) { |
| return DRET_MSG(result, "AllocateBuffer failed: %d", result); |
| } |
| |
| if (!buffer->duplicate_handle(buffer_handle_out)) { |
| return DRET_MSG(MAGMA_STATUS_INTERNAL_ERROR, "duplicate_handle failed"); |
| } |
| return MAGMA_STATUS_OK; |
| } |
| |
| magma_status_t |
| magma_sysmem_allocate_texture(magma_sysmem_connection_t connection, uint32_t flags, uint32_t format, |
| uint32_t width, uint32_t height, uint32_t* buffer_handle_out, |
| magma_buffer_format_description_t* buffer_format_description_out) |
| { |
| std::unique_ptr<magma::PlatformBuffer> buffer; |
| auto sysmem_connection = reinterpret_cast<magma::PlatformSysmemConnection*>(connection); |
| |
| *buffer_format_description_out = 0; |
| std::unique_ptr<magma::PlatformSysmemConnection::BufferDescription> description; |
| magma_status_t result; |
| result = |
| sysmem_connection->AllocateTexture(flags, format, width, height, &buffer, &description); |
| if (result != MAGMA_STATUS_OK) { |
| return DRET_MSG(result, "AllocateTexture failed: %d", result); |
| } |
| |
| if (!buffer->duplicate_handle(buffer_handle_out)) { |
| return DRET_MSG(MAGMA_STATUS_INTERNAL_ERROR, "duplicate_handle failed"); |
| } |
| |
| *buffer_format_description_out = |
| reinterpret_cast<magma_buffer_format_description_t>(description.release()); |
| return MAGMA_STATUS_OK; |
| } |
| |
| void magma_buffer_format_description_release(magma_buffer_format_description_t description) |
| { |
| delete reinterpret_cast<magma::PlatformSysmemConnection::BufferDescription*>(description); |
| } |
| |
| // |image_planes_out| must be an array with MAGMA_MAX_IMAGE_PLANES elements. |
| magma_status_t magma_get_buffer_format_plane_info(magma_buffer_format_description_t description, |
| magma_image_plane_t* image_planes_out) |
| { |
| if (!description) { |
| return DRET_MSG(MAGMA_STATUS_INVALID_ARGS, "Null description"); |
| } |
| auto buffer_description = |
| reinterpret_cast<magma::PlatformSysmemConnection::BufferDescription*>(description); |
| memcpy(image_planes_out, buffer_description->planes, sizeof(buffer_description->planes)); |
| return MAGMA_STATUS_OK; |
| } |