1 /*
2 * test-binary-kernel.cpp - Compile the source kernel into binary kernel
3 *
4 * Copyright (c) 2015 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Yinhang Liu <yinhangx.liu@intel.com>
19 */
20
21 #include "test_common.h"
22 #include "test_inline.h"
23 #include "file_handle.h"
24 #include "ocl/cl_device.h"
25 #include "ocl/cl_context.h"
26 #include "ocl/cl_kernel.h"
27 #include <getopt.h>
28
29 using namespace XCam;
30
31 static void
print_help(const char * arg0)32 print_help (const char *arg0)
33 {
34 printf ("Usage: %s --src-kernel <source-kernel> --bin-kernel <binary-kernel> --kernel-name <kernel-name>\n"
35 "\t --src-kernel specify source kernel path\n"
36 "\t --bin-kernel specify binary kernel path\n"
37 "\t --kernel-name specify kernel name\n"
38 "\t --help help\n"
39 , arg0);
40 }
41
42 #define FAILED_STATEMENT { \
43 if (kernel_body) xcam_free (kernel_body); \
44 if (kernel_name) xcam_free (kernel_name); \
45 if (program_binaries) xcam_free (program_binaries); \
46 return -1; }
47
main(int argc,char * argv[])48 int main (int argc, char *argv[])
49 {
50 char *src_path = NULL, *bin_path = NULL;
51 size_t src_size = 0;
52 size_t bin_size = 0;
53 char *kernel_name = NULL;
54 char *kernel_body = NULL;
55 uint8_t *program_binaries = NULL;
56 FileHandle src_file, bin_file;
57 XCamReturn ret = XCAM_RETURN_NO_ERROR;
58
59 const struct option long_opts [] = {
60 {"src-kernel", required_argument, NULL, 's'},
61 {"bin-kernel", required_argument, NULL, 'b'},
62 {"kernel-name", required_argument, NULL, 'n'},
63 {"help", no_argument, NULL, 'h'},
64 {NULL, 0, NULL, 0}
65 };
66
67 int opt = 0;
68 while ((opt = getopt_long (argc, argv, "", long_opts, NULL)) != -1) {
69 switch (opt) {
70 case 's':
71 src_path = optarg;
72 break;
73 case 'b':
74 bin_path = optarg;
75 break;
76 case 'n':
77 kernel_name = strndup (optarg, 1024);
78 break;
79 case 'h':
80 print_help (argv[0]);
81 return 0;
82
83 default:
84 print_help (argv[0]);
85 return -1;
86 }
87 }
88
89 if (!src_path || !bin_path) {
90 XCAM_LOG_ERROR ("path of source/binary kernel is null");
91 return -1;
92 }
93 if (!kernel_name) {
94 XCAM_LOG_ERROR ("kernel name is null");
95 return -1;
96 }
97
98 if (src_file.open (src_path, "r") != XCAM_RETURN_NO_ERROR ||
99 bin_file.open (bin_path, "wb") != XCAM_RETURN_NO_ERROR) {
100 XCAM_LOG_ERROR ("open source/binary kernel failed");
101 return -1;
102 }
103
104 ret = src_file.get_file_size (src_size);
105 CHECK_STATEMENT (ret, FAILED_STATEMENT, "get source sizes from %s failed", src_path);
106
107 kernel_body = (char *) xcam_malloc0 (sizeof (char) * (src_size + 1));
108 XCAM_ASSERT(kernel_body);
109
110 src_file.read_file (kernel_body, src_size);
111 CHECK_STATEMENT (ret, FAILED_STATEMENT, "read source from %s failed", src_path);
112 kernel_body[src_size] = '\0';
113
114 SmartPtr<CLContext> context;
115 context = CLDevice::instance ()->get_context ();
116 SmartPtr<CLKernel> kernel = new CLKernel (context, kernel_name);
117 kernel->load_from_source (kernel_body, strlen (kernel_body), &program_binaries, &bin_size);
118
119 ret = bin_file.write_file (program_binaries, bin_size);
120 CHECK_STATEMENT (ret, FAILED_STATEMENT, "write binary to %s failed", bin_path);
121
122 xcam_free (kernel_name);
123 xcam_free (kernel_body);
124 xcam_free (program_binaries);
125 return 0;
126 }
127