blob: 1bd3582b6c823b36fbb18b3359fce0f90dc45974 [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"
29#include "arm_compute/runtime/ITensorAllocator.h"
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000030#include "arm_compute/runtime/MemoryGroupBase.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010031
32#include <memory>
33
34namespace arm_compute
35{
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000036class GCTensor;
37template <typename>
38class MemoryGroupBase;
39using GCMemoryGroup = MemoryGroupBase<GCTensor>;
40
41class GLBufferWrapper
42{
43public:
44 GLBufferWrapper()
45 : _ssbo_name(0)
46 {
47 ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_ssbo_name));
48 }
49 ~GLBufferWrapper()
50 {
51 ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_ssbo_name));
52 }
53 GLuint _ssbo_name;
54};
Anthony Barbier7068f992017-10-26 15:23:08 +010055/** Basic implementation of a GLES memory tensor allocator. */
56class GCTensorAllocator : public ITensorAllocator
57{
58public:
59 /** Default constructor. */
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000060 GCTensorAllocator(GCTensor *owner = nullptr);
Anthony Barbier7068f992017-10-26 15:23:08 +010061
Alex Gildayc357c472018-03-21 13:54:09 +000062 /** Prevent instances of this class from being copied (As this class contains pointers) */
Anthony Barbier7068f992017-10-26 15:23:08 +010063 GCTensorAllocator(const GCTensorAllocator &) = delete;
64
Alex Gildayc357c472018-03-21 13:54:09 +000065 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Anthony Barbier7068f992017-10-26 15:23:08 +010066 GCTensorAllocator &operator=(const GCTensorAllocator &) = delete;
67
68 /** Allow instances of this class to be moved */
69 GCTensorAllocator(GCTensorAllocator &&) = default;
70
71 /** Allow instances of this class to be moved */
72 GCTensorAllocator &operator=(GCTensorAllocator &&) = default;
73
74 /** Default destructor */
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000075 ~GCTensorAllocator();
Anthony Barbier7068f992017-10-26 15:23:08 +010076
Alex Gildayc357c472018-03-21 13:54:09 +000077 /** Interface to be implemented by the child class to return the pointer to the mapped data.
78 *
79 * @return a pointer to the data.
80 */
Anthony Barbier7068f992017-10-26 15:23:08 +010081 uint8_t *data();
82
83 /** Get the OpenGL ES buffer object name
84 *
85 * @return The buffer object name
86 */
87 GLuint get_gl_ssbo_name() const;
88
89 /** Enqueue a map operation of the allocated buffer on the given queue.
90 *
91 * @param[in] blocking If true, then the mapping will be ready to use by the time
92 * this method returns, else it is the caller's responsibility
93 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
94 *
95 * @return The mapping address.
96 */
97 uint8_t *map(bool blocking);
98
99 /** Enqueue an unmap operation of the allocated buffer on the given queue.
100 *
101 * @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
102 * the memory is accessed by the device.
103 *
104 */
105 void unmap();
106
107 /** Allocate size specified by TensorInfo of GLES memory.
108 *
109 * @note: The tensor must not already be allocated when calling this function.
110 *
111 */
112 void allocate() override;
113
114 /** Free allocated GLES memory.
115 *
116 * @note The tensor must have been allocated when calling this function.
117 *
118 */
119 void free() override;
120
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000121 /** Associates the tensor with a memory group
122 *
123 * @param[in] associated_memory_group Memory group to associate the tensor with
124 */
125 void set_associated_memory_group(GCMemoryGroup *associated_memory_group);
126
Anthony Barbier7068f992017-10-26 15:23:08 +0100127protected:
128 /** Call map() on the SSBO.
129 *
130 * @return A pointer to the beginning of the tensor's allocation.
131 */
132 uint8_t *lock() override;
133
134 /** Call unmap() on the SSBO. */
135 void unlock() override;
136
137private:
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000138 GCMemoryGroup *_associated_memory_group; /**< Registered memory group */
139 std::unique_ptr<GLBufferWrapper> _gl_buffer; /**< OpenGL ES object containing the tensor data. */
140 uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenGL ES buffer. */
141 GCTensor *_owner; /**< Owner of the allocator */
Anthony Barbier7068f992017-10-26 15:23:08 +0100142};
143}
144
145#endif /* __ARM_COMPUTE_GCTENSORALLOCATOR_H__ */