blob: cccc6a75c6acb628d8792844cd907a94ec439d60 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michalis Spyroucaa7dee2019-09-09 19:23:39 +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
25#include "arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h"
26
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/TensorInfo.h"
Georgios Pinitasdf310362018-11-14 13:16:56 +000029#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010030#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
31#include "support/ToolchainSupport.h"
32
33using namespace arm_compute;
34
Georgios Pinitas26014cf2019-09-09 19:00:57 +010035GCTensorAllocator::GCTensorAllocator(IMemoryManageable *owner)
36 : _owner(owner), _associated_memory_group(nullptr), _memory(), _mapping(nullptr)
Anthony Barbier7068f992017-10-26 15:23:08 +010037{
38}
39
40uint8_t *GCTensorAllocator::data()
41{
42 return _mapping;
43}
44
45void GCTensorAllocator::allocate()
46{
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000047 if(_associated_memory_group == nullptr)
48 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000049 _memory.set_owned_region(support::cpp14::make_unique<GCBufferMemoryRegion>(info().total_size()));
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000050 }
51 else
52 {
Georgios Pinitas26014cf2019-09-09 19:00:57 +010053 _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment());
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000054 }
Anthony Barbier7068f992017-10-26 15:23:08 +010055 info().set_is_resizable(false);
56}
57
58void GCTensorAllocator::free()
59{
Georgios Pinitasdf310362018-11-14 13:16:56 +000060 _mapping = nullptr;
61 _memory.set_region(nullptr);
62 info().set_is_resizable(true);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000063}
64
Georgios Pinitas26014cf2019-09-09 19:00:57 +010065void GCTensorAllocator::set_associated_memory_group(IMemoryGroup *associated_memory_group)
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000066{
67 ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
Michalis Spyroucaa7dee2019-09-09 19:23:39 +010068 ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
Georgios Pinitasdf310362018-11-14 13:16:56 +000069 ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.gc_region()->gc_ssbo_name() != 0);
70
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000071 _associated_memory_group = associated_memory_group;
Anthony Barbier7068f992017-10-26 15:23:08 +010072}
73
74uint8_t *GCTensorAllocator::lock()
75{
76 return map(true);
77}
78
79void GCTensorAllocator::unlock()
80{
81 unmap();
82}
83
84GLuint GCTensorAllocator::get_gl_ssbo_name() const
85{
Georgios Pinitasdf310362018-11-14 13:16:56 +000086 return (_memory.region() == nullptr) ? static_cast<GLuint>(0) : _memory.gc_region()->gc_ssbo_name();
Anthony Barbier7068f992017-10-26 15:23:08 +010087}
88
89uint8_t *GCTensorAllocator::map(bool blocking)
90{
91 ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +000092 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
Anthony Barbier7068f992017-10-26 15:23:08 +010093
Georgios Pinitasdf310362018-11-14 13:16:56 +000094 _mapping = reinterpret_cast<uint8_t *>(_memory.gc_region()->map(blocking));
Anthony Barbier7068f992017-10-26 15:23:08 +010095 return _mapping;
96}
97
98void GCTensorAllocator::unmap()
99{
100 ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000101 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
Anthony Barbier7068f992017-10-26 15:23:08 +0100102
Georgios Pinitasdf310362018-11-14 13:16:56 +0000103 _memory.gc_region()->unmap();
Anthony Barbier7068f992017-10-26 15:23:08 +0100104 _mapping = nullptr;
105}