arm_compute v18.05
[platform/upstream/armcl.git] / src / runtime / GLES_COMPUTE / GCTensorAllocator.cpp
1 /*
2  * Copyright (c) 2017-2018 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 #include "arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h"
26
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
30 #include "support/ToolchainSupport.h"
31
32 using namespace arm_compute;
33
34 GCTensorAllocator::GCTensorAllocator(GCTensor *owner)
35     : _associated_memory_group(nullptr), _gl_buffer(), _mapping(nullptr), _owner(owner)
36 {
37 }
38
39 GCTensorAllocator::~GCTensorAllocator()
40 {
41     _gl_buffer = support::cpp14::make_unique<GLBufferWrapper>();
42 }
43
44 uint8_t *GCTensorAllocator::data()
45 {
46     return _mapping;
47 }
48
49 void GCTensorAllocator::allocate()
50 {
51     if(_associated_memory_group == nullptr)
52     {
53         _gl_buffer = support::cpp14::make_unique<GLBufferWrapper>();
54         ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
55         ARM_COMPUTE_GL_CHECK(glBufferData(GL_SHADER_STORAGE_BUFFER, static_cast<GLsizeiptr>(info().total_size()), nullptr, GL_STATIC_DRAW));
56         ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
57     }
58     else
59     {
60         _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(&_gl_buffer), info().total_size());
61     }
62     info().set_is_resizable(false);
63 }
64
65 void GCTensorAllocator::free()
66 {
67     if(_associated_memory_group == nullptr)
68     {
69         _gl_buffer.reset();
70         info().set_is_resizable(true);
71     }
72 }
73
74 void GCTensorAllocator::set_associated_memory_group(GCMemoryGroup *associated_memory_group)
75 {
76     ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
77     ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
78     ARM_COMPUTE_ERROR_ON(_gl_buffer.get() != nullptr);
79     _associated_memory_group = associated_memory_group;
80 }
81
82 uint8_t *GCTensorAllocator::lock()
83 {
84     return map(true);
85 }
86
87 void GCTensorAllocator::unlock()
88 {
89     unmap();
90 }
91
92 GLuint GCTensorAllocator::get_gl_ssbo_name() const
93 {
94     return _gl_buffer->_ssbo_name;
95 }
96
97 uint8_t *GCTensorAllocator::map(bool blocking)
98 {
99     ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
100     ARM_COMPUTE_UNUSED(blocking);
101
102     ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
103     void *p  = ARM_COMPUTE_GL_CHECK(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, static_cast<GLsizeiptr>(info().total_size()), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT));
104     _mapping = reinterpret_cast<uint8_t *>(p);
105
106     return _mapping;
107 }
108
109 void GCTensorAllocator::unmap()
110 {
111     ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
112
113     ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
114     ARM_COMPUTE_GL_CHECK(glUnmapBuffer(GL_SHADER_STORAGE_BUFFER));
115     ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
116     _mapping = nullptr;
117 }