blob: b7067664fcd397b5b08a67f8653cc9ae2dde5447 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
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 Pinitasdf310362018-11-14 13:16:56 +000029#include "arm_compute/runtime/GLES_COMPUTE/GCMemory.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010030#include "arm_compute/runtime/ITensorAllocator.h"
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000031#include "arm_compute/runtime/MemoryGroupBase.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010032
33#include <memory>
34
35namespace arm_compute
36{
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000037class GCTensor;
38template <typename>
39class MemoryGroupBase;
40using GCMemoryGroup = MemoryGroupBase<GCTensor>;
41
Anthony Barbier7068f992017-10-26 15:23:08 +010042/** Basic implementation of a GLES memory tensor allocator. */
43class GCTensorAllocator : public ITensorAllocator
44{
45public:
46 /** Default constructor. */
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000047 GCTensorAllocator(GCTensor *owner = nullptr);
Anthony Barbier7068f992017-10-26 15:23:08 +010048
Alex Gildayc357c472018-03-21 13:54:09 +000049 /** Prevent instances of this class from being copied (As this class contains pointers) */
Anthony Barbier7068f992017-10-26 15:23:08 +010050 GCTensorAllocator(const GCTensorAllocator &) = delete;
51
Alex Gildayc357c472018-03-21 13:54:09 +000052 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Anthony Barbier7068f992017-10-26 15:23:08 +010053 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 Pinitasdf310362018-11-14 13:16:56 +000062 ~GCTensorAllocator() = default;
Anthony Barbier7068f992017-10-26 15:23:08 +010063
Alex Gildayc357c472018-03-21 13:54:09 +000064 /** 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 Barbier7068f992017-10-26 15:23:08 +010068 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 Spyrou9e9cbaf2018-03-15 14:41:34 +0000108 /** 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 Barbier7068f992017-10-26 15:23:08 +0100114protected:
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
124private:
Georgios Pinitasdf310362018-11-14 13:16:56 +0000125 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 Barbier7068f992017-10-26 15:23:08 +0100129};
Georgios Pinitasdf310362018-11-14 13:16:56 +0000130} // namespace arm_compute
Anthony Barbier7068f992017-10-26 15:23:08 +0100131#endif /* __ARM_COMPUTE_GCTENSORALLOCATOR_H__ */