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