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