Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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/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 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | CLFastCorners::CLFastCorners() |
| 40 | : _fast_corners_kernel(), |
| 41 | _suppr_func(), |
| 42 | _copy_array_kernel(), |
| 43 | _output(), |
| 44 | _suppr(), |
| 45 | _win(), |
| 46 | _non_max(false), |
| 47 | _num_corners(nullptr), |
| 48 | _num_buffer(), |
| 49 | _corners(nullptr), |
| 50 | _constant_border_value(0) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void CLFastCorners::configure(const ICLImage *input, float threshold, bool nonmax_suppression, CLKeyPointArray *const corners, |
| 55 | unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value) |
| 56 | { |
| 57 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 58 | ARM_COMPUTE_ERROR_ON(BorderMode::UNDEFINED != border_mode); |
| 59 | ARM_COMPUTE_ERROR_ON(nullptr == corners); |
| 60 | ARM_COMPUTE_ERROR_ON(threshold < 1 && threshold > 255); |
| 61 | |
| 62 | TensorInfo tensor_info(input->info()->tensor_shape(), 1, DataType::U8); |
| 63 | _output.allocator()->init(tensor_info); |
| 64 | |
| 65 | _non_max = nonmax_suppression; |
| 66 | _num_corners = num_corners; |
| 67 | _corners = corners; |
| 68 | _num_buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(unsigned int)); |
| 69 | _constant_border_value = constant_border_value; |
| 70 | |
| 71 | const bool update_number = (nullptr != _num_corners); |
| 72 | |
| 73 | _fast_corners_kernel.configure(input, &_output, threshold, nonmax_suppression, border_mode); |
| 74 | |
| 75 | if(!_non_max) |
| 76 | { |
| 77 | _copy_array_kernel.configure(&_output, update_number, corners, &_num_buffer); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | _suppr.allocator()->init(tensor_info); |
| 82 | |
| 83 | _suppr_func.configure(&_output, &_suppr, border_mode); |
| 84 | _copy_array_kernel.configure(&_suppr, update_number, corners, &_num_buffer); |
| 85 | |
| 86 | _suppr.allocator()->allocate(); |
| 87 | } |
| 88 | |
| 89 | // Allocate intermediate tensors |
| 90 | _output.allocator()->allocate(); |
| 91 | } |
| 92 | |
| 93 | void CLFastCorners::run() |
| 94 | { |
| 95 | cl::CommandQueue q = CLScheduler::get().queue(); |
| 96 | |
| 97 | if(_non_max) |
| 98 | { |
| 99 | ARM_COMPUTE_ERROR_ON_MSG(_output.cl_buffer().get() == nullptr, "Unconfigured function"); |
| 100 | const auto out_buffer = static_cast<unsigned char *>(q.enqueueMapBuffer(_output.cl_buffer(), CL_TRUE, CL_MAP_WRITE, 0, _output.info()->total_size())); |
| 101 | memset(out_buffer, 0, _output.info()->total_size()); |
| 102 | q.enqueueUnmapMemObject(_output.cl_buffer(), out_buffer); |
| 103 | } |
| 104 | |
| 105 | CLScheduler::get().enqueue(_fast_corners_kernel, false); |
| 106 | |
| 107 | if(_non_max) |
| 108 | { |
| 109 | _suppr_func.run(); |
| 110 | } |
| 111 | |
| 112 | CLScheduler::get().enqueue(_copy_array_kernel, false); |
| 113 | |
| 114 | unsigned int get_num_corners = 0; |
| 115 | q.enqueueReadBuffer(_num_buffer, CL_TRUE, 0, sizeof(unsigned int), &get_num_corners); |
| 116 | |
| 117 | size_t corner_size = std::min(static_cast<size_t>(get_num_corners), _corners->max_num_values()); |
| 118 | |
| 119 | _corners->resize(corner_size); |
| 120 | |
| 121 | if(_num_corners != nullptr) |
| 122 | { |
| 123 | *_num_corners = get_num_corners; |
| 124 | } |
| 125 | |
| 126 | q.flush(); |
| 127 | } |