blob: ee9861cbd1854e8ae901a79af11db5fe2b7cbc58 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Georgios Pinitas26014cf2019-09-09 19:00:57 +01002 * Copyright (c) 2017-2019 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
Michalis Spyrouf4643372019-11-29 16:17:13 +000025#ifndef ARM_COMPUTE_GCTENSORALLOCATOR_H
26#define ARM_COMPUTE_GCTENSORALLOCATOR_H
Anthony Barbier7068f992017-10-26 15:23:08 +010027
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"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010031#include "arm_compute/runtime/MemoryGroup.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;
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000038
Anthony Barbier7068f992017-10-26 15:23:08 +010039/** Basic implementation of a GLES memory tensor allocator. */
40class GCTensorAllocator : public ITensorAllocator
41{
42public:
43 /** Default constructor. */
Georgios Pinitas26014cf2019-09-09 19:00:57 +010044 GCTensorAllocator(IMemoryManageable *owner = nullptr);
Anthony Barbier7068f992017-10-26 15:23:08 +010045
Alex Gildayc357c472018-03-21 13:54:09 +000046 /** Prevent instances of this class from being copied (As this class contains pointers) */
Anthony Barbier7068f992017-10-26 15:23:08 +010047 GCTensorAllocator(const GCTensorAllocator &) = delete;
48
Alex Gildayc357c472018-03-21 13:54:09 +000049 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Anthony Barbier7068f992017-10-26 15:23:08 +010050 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 Pinitasdf310362018-11-14 13:16:56 +000059 ~GCTensorAllocator() = default;
Anthony Barbier7068f992017-10-26 15:23:08 +010060
Alex Gildayc357c472018-03-21 13:54:09 +000061 /** 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 Barbier7068f992017-10-26 15:23:08 +010065 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 Spyrou9e9cbaf2018-03-15 14:41:34 +0000105 /** Associates the tensor with a memory group
106 *
107 * @param[in] associated_memory_group Memory group to associate the tensor with
108 */
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100109 void set_associated_memory_group(IMemoryGroup *associated_memory_group);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000110
Anthony Barbier7068f992017-10-26 15:23:08 +0100111protected:
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
121private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100122 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 Barbier7068f992017-10-26 15:23:08 +0100126};
Georgios Pinitasdf310362018-11-14 13:16:56 +0000127} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000128#endif /* ARM_COMPUTE_GCTENSORALLOCATOR_H */