blob: ce52cbbbdc533c963278a7946440b459126fb59d [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
2 * Copyright (c) 2017 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/ITensorAllocator.h"
30
31#include <memory>
32
33namespace arm_compute
34{
35/** Basic implementation of a GLES memory tensor allocator. */
36class GCTensorAllocator : public ITensorAllocator
37{
38public:
39 /** Default constructor. */
40 GCTensorAllocator();
41
42 /** Prevent instances of this class from being copied (As this class contains pointers). */
43 GCTensorAllocator(const GCTensorAllocator &) = delete;
44
45 /** Prevent instances of this class from being copy assigned (As this class contains pointers). */
46 GCTensorAllocator &operator=(const GCTensorAllocator &) = delete;
47
48 /** Allow instances of this class to be moved */
49 GCTensorAllocator(GCTensorAllocator &&) = default;
50
51 /** Allow instances of this class to be moved */
52 GCTensorAllocator &operator=(GCTensorAllocator &&) = default;
53
54 /** Default destructor */
55 ~GCTensorAllocator() = default;
56
57 /** Interface to be implemented by the child class to return the pointer to the mapped data. */
58 uint8_t *data();
59
60 /** Get the OpenGL ES buffer object name
61 *
62 * @return The buffer object name
63 */
64 GLuint get_gl_ssbo_name() const;
65
66 /** Enqueue a map operation of the allocated buffer on the given queue.
67 *
68 * @param[in] blocking If true, then the mapping will be ready to use by the time
69 * this method returns, else it is the caller's responsibility
70 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
71 *
72 * @return The mapping address.
73 */
74 uint8_t *map(bool blocking);
75
76 /** Enqueue an unmap operation of the allocated buffer on the given queue.
77 *
78 * @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
79 * the memory is accessed by the device.
80 *
81 */
82 void unmap();
83
84 /** Allocate size specified by TensorInfo of GLES memory.
85 *
86 * @note: The tensor must not already be allocated when calling this function.
87 *
88 */
89 void allocate() override;
90
91 /** Free allocated GLES memory.
92 *
93 * @note The tensor must have been allocated when calling this function.
94 *
95 */
96 void free() override;
97
98protected:
99 /** Call map() on the SSBO.
100 *
101 * @return A pointer to the beginning of the tensor's allocation.
102 */
103 uint8_t *lock() override;
104
105 /** Call unmap() on the SSBO. */
106 void unlock() override;
107
108private:
109 class GLBufferWrapper
110 {
111 public:
112 GLBufferWrapper()
113 : _ssbo_name(0)
114 {
115 ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_ssbo_name));
116 }
117 ~GLBufferWrapper()
118 {
119 ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_ssbo_name));
120 }
121 GLuint _ssbo_name;
122 };
123 std::unique_ptr<GLBufferWrapper> _gl_buffer;
124 uint8_t *_mapping;
125};
126}
127
128#endif /* __ARM_COMPUTE_GCTENSORALLOCATOR_H__ */