Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 | |
| 25 | #ifndef __ARM_COMPUTE_GCTENSORALLOCATOR_H__ |
| 26 | #define __ARM_COMPUTE_GCTENSORALLOCATOR_H__ |
| 27 | |
| 28 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 29 | #include "arm_compute/runtime/ITensorAllocator.h" |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/MemoryGroupBase.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 31 | |
| 32 | #include <memory> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 36 | class GCTensor; |
| 37 | template <typename> |
| 38 | class MemoryGroupBase; |
| 39 | using GCMemoryGroup = MemoryGroupBase<GCTensor>; |
| 40 | |
| 41 | class GLBufferWrapper |
| 42 | { |
| 43 | public: |
| 44 | GLBufferWrapper() |
| 45 | : _ssbo_name(0) |
| 46 | { |
| 47 | ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_ssbo_name)); |
| 48 | } |
| 49 | ~GLBufferWrapper() |
| 50 | { |
| 51 | ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_ssbo_name)); |
| 52 | } |
| 53 | GLuint _ssbo_name; |
| 54 | }; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 55 | /** Basic implementation of a GLES memory tensor allocator. */ |
| 56 | class GCTensorAllocator : public ITensorAllocator |
| 57 | { |
| 58 | public: |
| 59 | /** Default constructor. */ |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 60 | GCTensorAllocator(GCTensor *owner = nullptr); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 61 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 62 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 63 | GCTensorAllocator(const GCTensorAllocator &) = delete; |
| 64 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 65 | /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 66 | GCTensorAllocator &operator=(const GCTensorAllocator &) = delete; |
| 67 | |
| 68 | /** Allow instances of this class to be moved */ |
| 69 | GCTensorAllocator(GCTensorAllocator &&) = default; |
| 70 | |
| 71 | /** Allow instances of this class to be moved */ |
| 72 | GCTensorAllocator &operator=(GCTensorAllocator &&) = default; |
| 73 | |
| 74 | /** Default destructor */ |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 75 | ~GCTensorAllocator(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 76 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 77 | /** Interface to be implemented by the child class to return the pointer to the mapped data. |
| 78 | * |
| 79 | * @return a pointer to the data. |
| 80 | */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 81 | uint8_t *data(); |
| 82 | |
| 83 | /** Get the OpenGL ES buffer object name |
| 84 | * |
| 85 | * @return The buffer object name |
| 86 | */ |
| 87 | GLuint get_gl_ssbo_name() const; |
| 88 | |
| 89 | /** Enqueue a map operation of the allocated buffer on the given queue. |
| 90 | * |
| 91 | * @param[in] blocking If true, then the mapping will be ready to use by the time |
| 92 | * this method returns, else it is the caller's responsibility |
| 93 | * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. |
| 94 | * |
| 95 | * @return The mapping address. |
| 96 | */ |
| 97 | uint8_t *map(bool blocking); |
| 98 | |
| 99 | /** Enqueue an unmap operation of the allocated buffer on the given queue. |
| 100 | * |
| 101 | * @note This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before |
| 102 | * the memory is accessed by the device. |
| 103 | * |
| 104 | */ |
| 105 | void unmap(); |
| 106 | |
| 107 | /** Allocate size specified by TensorInfo of GLES memory. |
| 108 | * |
| 109 | * @note: The tensor must not already be allocated when calling this function. |
| 110 | * |
| 111 | */ |
| 112 | void allocate() override; |
| 113 | |
| 114 | /** Free allocated GLES memory. |
| 115 | * |
| 116 | * @note The tensor must have been allocated when calling this function. |
| 117 | * |
| 118 | */ |
| 119 | void free() override; |
| 120 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 121 | /** Associates the tensor with a memory group |
| 122 | * |
| 123 | * @param[in] associated_memory_group Memory group to associate the tensor with |
| 124 | */ |
| 125 | void set_associated_memory_group(GCMemoryGroup *associated_memory_group); |
| 126 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 127 | protected: |
| 128 | /** Call map() on the SSBO. |
| 129 | * |
| 130 | * @return A pointer to the beginning of the tensor's allocation. |
| 131 | */ |
| 132 | uint8_t *lock() override; |
| 133 | |
| 134 | /** Call unmap() on the SSBO. */ |
| 135 | void unlock() override; |
| 136 | |
| 137 | private: |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 138 | GCMemoryGroup *_associated_memory_group; /**< Registered memory group */ |
| 139 | std::unique_ptr<GLBufferWrapper> _gl_buffer; /**< OpenGL ES object containing the tensor data. */ |
| 140 | uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenGL ES buffer. */ |
| 141 | GCTensor *_owner; /**< Owner of the allocator */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 142 | }; |
| 143 | } |
| 144 | |
| 145 | #endif /* __ARM_COMPUTE_GCTENSORALLOCATOR_H__ */ |