blob: d6cda91cea92d44b3cd1ed752e237dfebc383dbd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Abe Mbise25a340f2017-12-19 13:00:58 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/CL/kernels/CLFastCornersKernel.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32#include "arm_compute/runtime/ITensorAllocator.h"
33
34#include <algorithm>
35#include <cstring>
36
37using namespace arm_compute;
38
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010039CLFastCorners::CLFastCorners(std::shared_ptr<IMemoryManager> memory_manager)
40 : _memory_group(std::move(memory_manager)),
41 _fast_corners_kernel(),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042 _suppr_func(),
43 _copy_array_kernel(),
44 _output(),
45 _suppr(),
46 _win(),
47 _non_max(false),
48 _num_corners(nullptr),
49 _num_buffer(),
50 _corners(nullptr),
51 _constant_border_value(0)
52{
53}
54
Abe Mbise25a340f2017-12-19 13:00:58 +000055void CLFastCorners::configure(const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value)
57{
58 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
59 ARM_COMPUTE_ERROR_ON(BorderMode::UNDEFINED != border_mode);
60 ARM_COMPUTE_ERROR_ON(nullptr == corners);
61 ARM_COMPUTE_ERROR_ON(threshold < 1 && threshold > 255);
62
63 TensorInfo tensor_info(input->info()->tensor_shape(), 1, DataType::U8);
64 _output.allocator()->init(tensor_info);
65
66 _non_max = nonmax_suppression;
67 _num_corners = num_corners;
68 _corners = corners;
69 _num_buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(unsigned int));
70 _constant_border_value = constant_border_value;
71
72 const bool update_number = (nullptr != _num_corners);
73
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010074 _memory_group.manage(&_output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 _fast_corners_kernel.configure(input, &_output, threshold, nonmax_suppression, border_mode);
76
77 if(!_non_max)
78 {
Abe Mbise25a340f2017-12-19 13:00:58 +000079 _copy_array_kernel.configure(&_output, update_number, _corners, &_num_buffer);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 }
81 else
82 {
83 _suppr.allocator()->init(tensor_info);
Georgios Pinitas5701e2a2017-09-18 17:43:33 +010084 _memory_group.manage(&_suppr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085
86 _suppr_func.configure(&_output, &_suppr, border_mode);
Abe Mbise25a340f2017-12-19 13:00:58 +000087 _copy_array_kernel.configure(&_suppr, update_number, _corners, &_num_buffer);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
89 _suppr.allocator()->allocate();
90 }
91
92 // Allocate intermediate tensors
93 _output.allocator()->allocate();
94}
95
96void CLFastCorners::run()
97{
98 cl::CommandQueue q = CLScheduler::get().queue();
99
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100100 _memory_group.acquire();
101
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 if(_non_max)
103 {
104 ARM_COMPUTE_ERROR_ON_MSG(_output.cl_buffer().get() == nullptr, "Unconfigured function");
105 const auto out_buffer = static_cast<unsigned char *>(q.enqueueMapBuffer(_output.cl_buffer(), CL_TRUE, CL_MAP_WRITE, 0, _output.info()->total_size()));
106 memset(out_buffer, 0, _output.info()->total_size());
107 q.enqueueUnmapMemObject(_output.cl_buffer(), out_buffer);
108 }
109
110 CLScheduler::get().enqueue(_fast_corners_kernel, false);
111
112 if(_non_max)
113 {
114 _suppr_func.run();
115 }
116
117 CLScheduler::get().enqueue(_copy_array_kernel, false);
118
119 unsigned int get_num_corners = 0;
120 q.enqueueReadBuffer(_num_buffer, CL_TRUE, 0, sizeof(unsigned int), &get_num_corners);
121
122 size_t corner_size = std::min(static_cast<size_t>(get_num_corners), _corners->max_num_values());
123
124 _corners->resize(corner_size);
125
126 if(_num_corners != nullptr)
127 {
128 *_num_corners = get_num_corners;
129 }
130
131 q.flush();
Georgios Pinitas5701e2a2017-09-18 17:43:33 +0100132
133 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134}