1 // Copyright (C) 2023 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 
15 #pragma once
16 
17 #include <memory>
18 
19 #include "magma_dec.h"
20 
21 namespace gfxstream {
22 namespace magma {
23 
24 // Generic magma decoder.
25 class Decoder : public magma_decoder_context_t {
26    public:
27     static std::unique_ptr<Decoder> create(uint32_t context_id);
28 
29    protected:
30     Decoder();
31 
32    private:
33     // These methods map directly to the magma client API.
34     // clang-format off
35     virtual magma_status_t magma_device_import(magma_handle_t device_channel, magma_device_t* device_out);
36     virtual void magma_device_release(magma_device_t device);
37     virtual magma_status_t magma_device_query(magma_device_t device, uint64_t id, magma_handle_t* result_buffer_out, uint64_t* result_out);
38     virtual magma_status_t magma_device_query_fudge(magma_device_t device, uint64_t id, magma_bool_t host_allocate, uint64_t* result_buffer_mapping_id_inout, uint64_t* result_buffer_size_inout, uint64_t* result_out);
39     virtual magma_status_t magma_device_create_connection(magma_device_t device, magma_connection_t* connection_out);
40     virtual void magma_connection_release(magma_connection_t connection);
41     virtual magma_status_t magma_connection_get_error(magma_connection_t connection);
42     virtual magma_status_t magma_connection_create_context(magma_connection_t connection, uint32_t* context_id_out);
43     virtual void magma_connection_release_context(magma_connection_t connection, uint32_t context_id);
44     virtual magma_status_t magma_connection_create_buffer(magma_connection_t connection, uint64_t size, uint64_t* size_out, magma_buffer_t* buffer_out, magma_buffer_id_t* id_out);
45     virtual void magma_connection_release_buffer(magma_connection_t connection, magma_buffer_t buffer);
46     virtual magma_status_t magma_connection_import_buffer(magma_connection_t connection, magma_handle_t buffer_handle, uint64_t* size_out, magma_buffer_t* buffer_out, magma_buffer_id_t* id_out);
47     virtual magma_status_t magma_connection_create_semaphore(magma_connection_t magma_connection, magma_semaphore_t* semaphore_out, magma_semaphore_id_t* id_out);
48     virtual void magma_connection_release_semaphore(magma_connection_t connection, magma_semaphore_t semaphore);
49     virtual magma_status_t magma_connection_import_semaphore(magma_connection_t connection, magma_handle_t semaphore_handle, magma_semaphore_t* semaphore_out, magma_semaphore_id_t* id_out);
50     virtual magma_status_t magma_connection_perform_buffer_op(magma_connection_t connection, magma_buffer_t buffer, uint32_t options, uint64_t start_offset, uint64_t length);
51     virtual magma_status_t magma_connection_map_buffer(magma_connection_t connection, uint64_t hw_va, magma_buffer_t buffer, uint64_t offset, uint64_t length, uint64_t map_flags);
52     virtual void magma_connection_unmap_buffer(magma_connection_t connection, uint64_t hw_va, magma_buffer_t buffer);
53     virtual magma_status_t magma_connection_execute_command(magma_connection_t connection, uint32_t context_id, magma_command_descriptor_t* descriptor);
54     virtual magma_status_t magma_connection_execute_immediate_commands(magma_connection_t connection, uint32_t context_id, uint64_t command_count, magma_inline_command_buffer_t* command_buffers);
55     virtual magma_status_t magma_connection_flush(magma_connection_t connection);
56     virtual magma_handle_t magma_connection_get_notification_channel_handle(magma_connection_t connection);
57     virtual magma_status_t magma_connection_read_notification_channel(magma_connection_t connection, void* buffer, uint64_t buffer_size, uint64_t* buffer_size_out, magma_bool_t* more_data_out);
58     virtual magma_status_t magma_buffer_clean_cache(magma_buffer_t buffer, uint64_t offset, uint64_t size, magma_cache_operation_t operation);
59     virtual magma_status_t magma_buffer_set_cache_policy(magma_buffer_t buffer, magma_cache_policy_t policy);
60     virtual magma_status_t magma_buffer_get_cache_policy(magma_buffer_t buffer, magma_cache_policy_t* cache_policy_out);
61     virtual magma_status_t magma_buffer_set_name(magma_buffer_t buffer, const char* name);
62     virtual magma_status_t magma_buffer_get_info(magma_buffer_t buffer, magma_buffer_info_t* info_out);
63     virtual magma_status_t magma_buffer_get_handle(magma_buffer_t buffer, magma_handle_t* handle_out);
64     virtual magma_status_t magma_buffer_export(magma_buffer_t buffer, magma_handle_t* buffer_handle_out);
65     virtual void magma_semaphore_signal(magma_semaphore_t semaphore);
66     virtual void magma_semaphore_reset(magma_semaphore_t semaphore);
67     virtual magma_status_t magma_semaphore_export(magma_semaphore_t semaphore, magma_handle_t* semaphore_handle_out);
68     virtual magma_status_t magma_poll(magma_poll_item_t* items, uint32_t count, uint64_t timeout_ns);
69     virtual magma_status_t magma_initialize_tracing(magma_handle_t channel);
70     virtual magma_status_t magma_initialize_logging(magma_handle_t channel);
71     virtual magma_status_t magma_connection_enable_performance_counter_access(magma_connection_t connection, magma_handle_t channel);
72     virtual magma_status_t magma_connection_enable_performance_counters(magma_connection_t connection, uint64_t* counters, uint64_t counters_count);
73     virtual magma_status_t magma_connection_create_performance_counter_buffer_pool(magma_connection_t connection, magma_perf_count_pool_t* pool_id_out, magma_handle_t* notification_handle_out);
74     virtual magma_status_t magma_connection_release_performance_counter_buffer_pool(magma_connection_t connection, magma_perf_count_pool_t pool_id);
75     virtual magma_status_t magma_connection_add_performance_counter_buffer_offsets_to_pool(magma_connection_t connection, magma_perf_count_pool_t pool_id, const magma_buffer_offset_t* offsets, uint64_t offsets_count);
76     virtual magma_status_t magma_connection_remove_performance_counter_buffer_from_pool(magma_connection_t connection, magma_perf_count_pool_t pool_id, magma_buffer_t buffer);
77     virtual magma_status_t magma_connection_dump_performance_counters(magma_connection_t connection, magma_perf_count_pool_t pool_id, uint32_t trigger_id);
78     virtual magma_status_t magma_connection_clear_performance_counters(magma_connection_t connection, uint64_t* counters, uint64_t counters_count);
79     virtual magma_status_t magma_connection_read_performance_counter_completion(magma_connection_t connection, magma_perf_count_pool_t pool_id, uint32_t* trigger_id_out, uint64_t* buffer_id_out, uint32_t* buffer_offset_out, uint64_t* time_out, uint32_t* result_flags_out);
80     virtual magma_status_t magma_virt_connection_create_image(magma_connection_t connection, magma_image_create_info_t* create_info, uint64_t* size_out, magma_buffer_t* image_out, magma_buffer_id_t* buffer_id_out);
81     virtual magma_status_t magma_virt_connection_get_image_info(magma_connection_t connection, magma_buffer_t image, magma_image_info_t* image_info_out);
82     // clang-format on
83 
84     // These are "fudged" methods that alter the signature of a standard magma API, either to
85     // provide additional information necessary for efficient proxying, or to work around
86     // limitations in emugen.
87 
88     // `descriptor` contains the flattened descriptor.
89     virtual magma_status_t magma_connection_execute_command_fudge(magma_connection_t connection,
90                                                                   uint32_t context_id,
91                                                                   void* descriptor,
92                                                                   uint64_t descriptor_size);
93 
94     // `command_buffers` contains the flattened list of command buffers, with
95     // `command_buffer_offsets` containing a list of offsets into `command_buffers` that define the
96     // start of each flattened command buffer.
97     virtual magma_status_t magma_connection_execute_immediate_commands_fudge(
98         magma_connection_t connection, uint32_t context_id, uint64_t command_count,
99         void* command_buffers, uint64_t command_buffers_size, uint64_t* command_buffer_offsets);
100 
101     // `name` contains a null-terminated string. `name_size` is the size of the string including its
102     // null terminator.
103     virtual magma_status_t magma_buffer_set_name_fudge(magma_buffer_t buffer, void* name,
104                                                        uint64_t name_size);
105 };
106 
107 }  // namespace magma
108 }  // namespace gfxstream
109