1 /* 2 * Copyright (c) 2017-2019 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 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/GLES_COMPUTE/GCMemory.h" 30 #include "arm_compute/runtime/ITensorAllocator.h" 31 #include "arm_compute/runtime/MemoryGroup.h" 32 33 #include <memory> 34 35 namespace arm_compute 36 { 37 class GCTensor; 38 39 /** Basic implementation of a GLES memory tensor allocator. */ 40 class GCTensorAllocator : public ITensorAllocator 41 { 42 public: 43 /** Default constructor. */ 44 GCTensorAllocator(IMemoryManageable *owner = nullptr); 45 46 /** Prevent instances of this class from being copied (As this class contains pointers) */ 47 GCTensorAllocator(const GCTensorAllocator &) = delete; 48 49 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ 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 */ 59 ~GCTensorAllocator() = default; 60 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 */ 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 105 /** Associates the tensor with a memory group 106 * 107 * @param[in] associated_memory_group Memory group to associate the tensor with 108 */ 109 void set_associated_memory_group(IMemoryGroup *associated_memory_group); 110 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: 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. */ 126 }; 127 } // namespace arm_compute 128 #endif /* ARM_COMPUTE_GCTENSORALLOCATOR_H */ 129