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 #include "magma_enc_util.h"
16 
17 #include <cstring>
18 
19 namespace magma_enc_util {
20 
size_command_descriptor(magma_command_descriptor * descriptor)21 size_t size_command_descriptor(magma_command_descriptor* descriptor) {
22     uint64_t size = sizeof(magma_command_descriptor);
23     size += sizeof(magma_exec_resource) * descriptor->resource_count;
24     size += sizeof(magma_exec_command_buffer) * descriptor->command_buffer_count;
25     size +=
26         sizeof(uint64_t) * (descriptor->wait_semaphore_count + descriptor->signal_semaphore_count);
27     return size;
28 }
29 
pack_command_descriptor(void * void_ptr,magma_connection_t connection,uint32_t context_id,magma_command_descriptor * descriptor)30 void pack_command_descriptor(void* void_ptr, magma_connection_t connection, uint32_t context_id,
31                              magma_command_descriptor* descriptor) {
32     magma_exec_resource* resources = descriptor->resources;
33     magma_exec_command_buffer* command_buffers = descriptor->command_buffers;
34     uint64_t* semaphore_ids = descriptor->semaphore_ids;
35 
36     magma_command_descriptor desc_copy = *descriptor;
37     desc_copy.resources = 0;
38     desc_copy.command_buffers = 0;
39     desc_copy.semaphore_ids = 0;
40 
41     auto ptr = reinterpret_cast<uint8_t*>(void_ptr);
42 
43     memcpy(ptr, &desc_copy, sizeof(magma_command_descriptor));
44     ptr += sizeof(magma_command_descriptor);
45 
46     memcpy(ptr, resources, sizeof(magma_exec_resource) * descriptor->resource_count);
47     ptr += sizeof(magma_exec_resource) * descriptor->resource_count;
48 
49     memcpy(ptr, command_buffers,
50            sizeof(magma_exec_command_buffer) * descriptor->command_buffer_count);
51     ptr += sizeof(magma_exec_command_buffer) * descriptor->command_buffer_count;
52 
53     memcpy(
54         ptr, semaphore_ids,
55         sizeof(uint64_t) * (descriptor->wait_semaphore_count + descriptor->signal_semaphore_count));
56 }
57 
58 }  // namespace magma_enc_util
59