1 /*
2 * Copyright (c) 2017-2020 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/GCMemoryRegion.h"
30 #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
31 #include "support/MemorySupport.h"
32
33 using namespace arm_compute;
34
GCTensorAllocator(IMemoryManageable * owner)35 GCTensorAllocator::GCTensorAllocator(IMemoryManageable *owner)
36 : _owner(owner), _associated_memory_group(nullptr), _memory(), _mapping(nullptr)
37 {
38 }
39
data()40 uint8_t *GCTensorAllocator::data()
41 {
42 return _mapping;
43 }
44
allocate()45 void GCTensorAllocator::allocate()
46 {
47 if(_associated_memory_group == nullptr)
48 {
49 _memory.set_owned_region(support::cpp14::make_unique<GCBufferMemoryRegion>(info().total_size()));
50 }
51 else
52 {
53 _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment());
54 }
55 info().set_is_resizable(false);
56 }
57
free()58 void GCTensorAllocator::free()
59 {
60 _mapping = nullptr;
61 _memory.set_region(nullptr);
62 info().set_is_resizable(true);
63 }
64
set_associated_memory_group(IMemoryGroup * associated_memory_group)65 void GCTensorAllocator::set_associated_memory_group(IMemoryGroup *associated_memory_group)
66 {
67 ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
68 ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
69 ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.gc_region()->gc_ssbo_name() != 0);
70
71 _associated_memory_group = associated_memory_group;
72 }
73
lock()74 uint8_t *GCTensorAllocator::lock()
75 {
76 return map(true);
77 }
78
unlock()79 void GCTensorAllocator::unlock()
80 {
81 unmap();
82 }
83
get_gl_ssbo_name() const84 GLuint GCTensorAllocator::get_gl_ssbo_name() const
85 {
86 return (_memory.region() == nullptr) ? static_cast<GLuint>(0) : _memory.gc_region()->gc_ssbo_name();
87 }
88
map(bool blocking)89 uint8_t *GCTensorAllocator::map(bool blocking)
90 {
91 ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
92 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
93
94 _mapping = reinterpret_cast<uint8_t *>(_memory.gc_region()->map(blocking));
95 return _mapping;
96 }
97
unmap()98 void GCTensorAllocator::unmap()
99 {
100 ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
101 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
102
103 _memory.gc_region()->unmap();
104 _mapping = nullptr;
105 }
106