• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/runtime/CL/functions/CLFastCorners.h"
25 
26 #include "arm_compute/core/CL/OpenCL.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/core/Validate.h"
30 #include "arm_compute/runtime/CL/CLScheduler.h"
31 #include "arm_compute/runtime/ITensorAllocator.h"
32 #include "src/core/CL/kernels/CLFastCornersKernel.h"
33 #include "src/core/CL/kernels/CLFillBorderKernel.h"
34 #include "support/MemorySupport.h"
35 
36 #include <algorithm>
37 #include <cstring>
38 
39 using namespace arm_compute;
40 
CLFastCorners(std::shared_ptr<IMemoryManager> memory_manager)41 CLFastCorners::CLFastCorners(std::shared_ptr<IMemoryManager> memory_manager)
42     : _memory_group(std::move(memory_manager)),
43       _fast_corners_kernel(support::cpp14::make_unique<CLFastCornersKernel>()),
44       _suppr_func(),
45       _copy_array_kernel(support::cpp14::make_unique<CLCopyToArrayKernel>()),
46       _output(),
47       _suppr(),
48       _win(),
49       _non_max(false),
50       _num_corners(nullptr),
51       _num_buffer(),
52       _corners(nullptr),
53       _constant_border_value(0)
54 {
55 }
56 
57 CLFastCorners::~CLFastCorners() = default;
58 
configure(const ICLImage * input,float threshold,bool nonmax_suppression,ICLKeyPointArray * corners,unsigned int * num_corners,BorderMode border_mode,uint8_t constant_border_value)59 void CLFastCorners::configure(const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners,
60                               unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value)
61 {
62     configure(CLKernelLibrary::get().get_compile_context(), input, threshold, nonmax_suppression, corners, num_corners, border_mode, constant_border_value);
63 }
64 
configure(const CLCompileContext & compile_context,const ICLImage * input,float threshold,bool nonmax_suppression,ICLKeyPointArray * corners,unsigned int * num_corners,BorderMode border_mode,uint8_t constant_border_value)65 void CLFastCorners::configure(const CLCompileContext &compile_context, const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners,
66                               unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value)
67 {
68     ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
69     ARM_COMPUTE_ERROR_ON(BorderMode::UNDEFINED != border_mode);
70     ARM_COMPUTE_ERROR_ON(nullptr == corners);
71     ARM_COMPUTE_ERROR_ON(threshold < 1 && threshold > 255);
72 
73     TensorInfo tensor_info(input->info()->tensor_shape(), 1, DataType::U8);
74     _output.allocator()->init(tensor_info);
75 
76     _non_max               = nonmax_suppression;
77     _num_corners           = num_corners;
78     _corners               = corners;
79     _num_buffer            = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(unsigned int));
80     _constant_border_value = constant_border_value;
81 
82     const bool update_number = (nullptr != _num_corners);
83 
84     _memory_group.manage(&_output);
85     _fast_corners_kernel->configure(compile_context, input, &_output, threshold, nonmax_suppression, border_mode);
86 
87     if(!_non_max)
88     {
89         _copy_array_kernel->configure(compile_context, &_output, update_number, _corners, &_num_buffer);
90     }
91     else
92     {
93         _suppr.allocator()->init(tensor_info);
94         _memory_group.manage(&_suppr);
95 
96         _suppr_func.configure(compile_context, &_output, &_suppr, border_mode);
97         _copy_array_kernel->configure(compile_context, &_suppr, update_number, _corners, &_num_buffer);
98 
99         _suppr.allocator()->allocate();
100     }
101 
102     // Allocate intermediate tensors
103     _output.allocator()->allocate();
104 }
105 
run()106 void CLFastCorners::run()
107 {
108     cl::CommandQueue q = CLScheduler::get().queue();
109 
110     MemoryGroupResourceScope scope_mg(_memory_group);
111 
112     if(_non_max)
113     {
114         ARM_COMPUTE_ERROR_ON_MSG(_output.cl_buffer().get() == nullptr, "Unconfigured function");
115         const auto out_buffer = static_cast<unsigned char *>(q.enqueueMapBuffer(_output.cl_buffer(), CL_TRUE, CL_MAP_WRITE, 0, _output.info()->total_size()));
116         memset(out_buffer, 0, _output.info()->total_size());
117         q.enqueueUnmapMemObject(_output.cl_buffer(), out_buffer);
118     }
119 
120     CLScheduler::get().enqueue(*_fast_corners_kernel, false);
121 
122     if(_non_max)
123     {
124         _suppr_func.run();
125     }
126 
127     CLScheduler::get().enqueue(*_copy_array_kernel, false);
128 
129     unsigned int get_num_corners = 0;
130     q.enqueueReadBuffer(_num_buffer, CL_TRUE, 0, sizeof(unsigned int), &get_num_corners);
131 
132     size_t corner_size = std::min(static_cast<size_t>(get_num_corners), _corners->max_num_values());
133 
134     _corners->resize(corner_size);
135 
136     if(_num_corners != nullptr)
137     {
138         *_num_corners = get_num_corners;
139     }
140 
141     q.flush();
142 }
143