1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef COMPUTEPIPE_RUNNER_INCLUDE_PREBUILT_INTERFACE_H_
18 #define COMPUTEPIPE_RUNNER_INCLUDE_PREBUILT_INTERFACE_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 #define COMPUTEPIPE_RUNNER(a) PrebuiltComputepipeRunner_##a
24 
25 extern "C" {
26 
27 // Enum value to report the error code for function calls.
28 enum PrebuiltComputepipeRunner_ErrorCode {
29     SUCCESS = 0,
30     INTERNAL_ERROR,
31     INVALID_ARGUMENT,
32     ILLEGAL_STATE,
33     NO_MEMORY,
34     FATAL_ERROR,
35     ERROR_CODE_MAX,
36 };
37 
38 enum PrebuiltComputepipeRunner_PixelDataFormat {
39     RGB = 0,
40     RGBA = 1,
41     GRAY = 2,
42     YUV_420 = 3,
43     PIXEL_DATA_FORMAT_MAX = 4,
44 };
45 
46 const char kEndOfInputStreamFlag[] = "**********EOF***********";
47 
48 // Gets the version of the library. The runner should check if the version of
49 // the prebuilt matches the version of android runner for which it was built
50 // and fail out if needed.
51 const unsigned char* COMPUTEPIPE_RUNNER(GetVersion)();
52 
53 // Gets the error code. This API is necessary because the graph execution is
54 // asynchronous and even if the function calls to execute the graph succeed, it
55 // could fail at a later point in the execution of the graph.
56 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(GetErrorCode)();
57 
58 // Gets the graph error message from the graph. The return value is not the
59 // graph error code but the error code returned if the call to the function
60 // fails.
61 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(GetErrorMessage)(
62     unsigned char* error_msg_buffer, size_t error_msg_buffer_size, size_t* error_msg_size);
63 
64 // Gets the supported graph config options. This is ideally generated once and
65 // cached for subsequent calls.
66 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(GetSupportedGraphConfigs)(
67     const void** config, size_t* config_size);
68 
69 // Sets the graph configuration or updates it if an incomplete config is passed.
70 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(UpdateGraphConfig)(
71     const unsigned char* graph_config, size_t graph_config_size);
72 
73 // Sets the stream contents. This can only be used after the graph has started
74 // running successfully. The contents of this stream are typically a serialized
75 // proto and would be deserialized and fed into the graph.
76 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(SetInputStreamData)(
77     int stream_index, int64_t timestamp, const unsigned char* stream_data, size_t stream_data_size);
78 
79 // Sets the pixel data as stream contents. This can be set only after the graph
80 // has started running successfully. Pixel data should be copied within this
81 // function as there are no guarantess on the lifetime of the pixel data beyond
82 // the return of this function call.
83 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(SetInputStreamPixelData)(
84     int stream_index, int64_t timestamp, const uint8_t* pixels, int width, int height, int step,
85     int format);
86 
87 // Sets a callback function for when a packet is generated. Note that a c-style
88 // function needs to be passed as no object context is being passed around here.
89 // The runner would be responsible for using the buffer provided in the callback
90 // immediately or copying it as there are no guarantees on its lifetime beyond
91 // the return of the callback.
92 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(SetOutputStreamCallback)(
93     void (*streamCallback)(void* cookie, int stream_index, int64_t timestamp,
94                            const unsigned char* data, size_t data_size));
95 
96 // Sets a callback function for when new pixel data is generated. C-style
97 // function pointers need to passed as no object context is being passed around.
98 // The runner would be responsible for immediately copying out the data. The
99 // prebuilt is expected to pass contiguous data.
100 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(SetOutputPixelStreamCallback)(
101     void (*streamCallback)(void* cookie, int stream_index, int64_t timestamp, const uint8_t* pixels,
102                            int width, int height, int step, int format));
103 
104 // Sets a callback function for when the graph terminates.
105 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(SetGraphTerminationCallback)(
106     void (*terminationCallback)(void* cookie, const unsigned char* termination_message,
107                                 size_t termination_message_size));
108 
109 // Starts the graph execution. Debugging can be enabled which will enable
110 // profiling. The profiling info can be obtained by calling getDebugInfo once
111 // the graph execution has stopped.
112 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(StartGraphExecution)(void* cookie,
113                                                                             bool debugging_enabled);
114 
115 // Stops the graph execution.
116 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(StopGraphExecution)(bool flushOutputFrames);
117 
118 // Resets the graph completely. Should be called only after graph execution has been stopped.
119 void COMPUTEPIPE_RUNNER(ResetGraph)();
120 
121 // Get debugging/profiling information. The function outputs the size of
122 // profiling information string and if the buffer size is larger than or equal
123 // to the size, then it copies it over to the buffer. Debugging info will be
124 // empty if the graph is started without debugging support.
125 PrebuiltComputepipeRunner_ErrorCode COMPUTEPIPE_RUNNER(GetDebugInfo)(unsigned char* debug_info,
126                                                                      size_t debug_info_buffer_size,
127                                                                      size_t* debug_info_size);
128 }
129 #endif  // COMPUTEPIPE_RUNNER_INCLUDE_PREBUILT_INTERFACE_H_
130