blob: ffb2ccd9ee337c6da193c8c14fb731c62471dc4c [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001// Copyright 2017 The NXT Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Corentin Wallezfffe6df2017-07-06 14:41:13 -040015#include "backend/CommandAllocator.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Corentin Wallezfd589f32017-07-10 13:46:05 -040017#include "common/Assert.h"
Corentin Wallezfffe6df2017-07-06 14:41:13 -040018#include "common/Math.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040019
Corentin Wallez944b60f2017-05-29 11:33:33 -070020#include <algorithm>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040021#include <climits>
22#include <cstdlib>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040023
24namespace backend {
25
26 constexpr uint32_t EndOfBlock = UINT_MAX;//std::numeric_limits<uint32_t>::max();
27 constexpr uint32_t AdditionalData = UINT_MAX - 1;//std::numeric_limits<uint32_t>::max();
28
29 // TODO(cwallez@chromium.org): figure out a way to have more type safety for the iterator
30
31 CommandIterator::CommandIterator()
Corentin Wallezfbecc282017-11-23 10:32:51 -080032 : mEndOfBlock(EndOfBlock) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040033 Reset();
34 }
35
36 CommandIterator::~CommandIterator() {
Corentin Wallezfbecc282017-11-23 10:32:51 -080037 ASSERT(mDataWasDestroyed);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040038
39 if (!IsEmpty()) {
Corentin Wallezfbecc282017-11-23 10:32:51 -080040 for (auto& block : mBlocks) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040041 free(block.block);
42 }
43 }
44 }
45
46 CommandIterator::CommandIterator(CommandIterator&& other)
Corentin Wallezfbecc282017-11-23 10:32:51 -080047 : mEndOfBlock(EndOfBlock) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048 if (!other.IsEmpty()) {
Corentin Wallezfbecc282017-11-23 10:32:51 -080049 mBlocks = std::move(other.mBlocks);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040050 other.Reset();
51 }
52 other.DataWasDestroyed();
53 Reset();
54 }
55
56 CommandIterator& CommandIterator::operator=(CommandIterator&& other) {
57 if (!other.IsEmpty()) {
Corentin Wallezfbecc282017-11-23 10:32:51 -080058 mBlocks = std::move(other.mBlocks);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040059 other.Reset();
60 } else {
Corentin Wallezfbecc282017-11-23 10:32:51 -080061 mBlocks.clear();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040062 }
63 other.DataWasDestroyed();
64 Reset();
65 return *this;
66 }
67
68 CommandIterator::CommandIterator(CommandAllocator&& allocator)
Corentin Wallezfbecc282017-11-23 10:32:51 -080069 : mBlocks(allocator.AcquireBlocks()), mEndOfBlock(EndOfBlock) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040070 Reset();
71 }
72
73 CommandIterator& CommandIterator::operator=(CommandAllocator&& allocator) {
Corentin Wallezfbecc282017-11-23 10:32:51 -080074 mBlocks = allocator.AcquireBlocks();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040075 Reset();
76 return *this;
77 }
78
79 void CommandIterator::Reset() {
Corentin Wallezfbecc282017-11-23 10:32:51 -080080 mCurrentBlock = 0;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040081
Corentin Wallezfbecc282017-11-23 10:32:51 -080082 if (mBlocks.empty()) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040083 // This will case the first NextCommandId call to try to move to the next
84 // block and stop the iteration immediately, without special casing the
85 // initialization.
Corentin Wallezfbecc282017-11-23 10:32:51 -080086 mCurrentPtr = reinterpret_cast<uint8_t*>(&mEndOfBlock);
87 mBlocks.emplace_back();
88 mBlocks[0].size = sizeof(mEndOfBlock);
89 mBlocks[0].block = mCurrentPtr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040090 } else {
Corentin Wallezfbecc282017-11-23 10:32:51 -080091 mCurrentPtr = AlignPtr(mBlocks[0].block, alignof(uint32_t));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040092 }
93 }
94
95 void CommandIterator::DataWasDestroyed() {
Corentin Wallezfbecc282017-11-23 10:32:51 -080096 mDataWasDestroyed = true;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040097 }
98
99 bool CommandIterator::IsEmpty() const {
Corentin Wallezfbecc282017-11-23 10:32:51 -0800100 return mBlocks[0].block == reinterpret_cast<const uint8_t*>(&mEndOfBlock);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400101 }
102
103 bool CommandIterator::NextCommandId(uint32_t* commandId) {
Corentin Wallezfbecc282017-11-23 10:32:51 -0800104 uint8_t* idPtr = AlignPtr(mCurrentPtr, alignof(uint32_t));
105 ASSERT(idPtr + sizeof(uint32_t) <= mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400106
107 uint32_t id = *reinterpret_cast<uint32_t*>(idPtr);
108
109 if (id == EndOfBlock) {
Corentin Wallezfbecc282017-11-23 10:32:51 -0800110 mCurrentBlock++;
111 if (mCurrentBlock >= mBlocks.size()) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400112 Reset();
Corentin Wallez770f25f2017-07-20 14:30:04 -0400113 *commandId = EndOfBlock;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400114 return false;
115 }
Corentin Wallezfbecc282017-11-23 10:32:51 -0800116 mCurrentPtr = AlignPtr(mBlocks[mCurrentBlock].block, alignof(uint32_t));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400117 return NextCommandId(commandId);
118 }
119
Corentin Wallezfbecc282017-11-23 10:32:51 -0800120 mCurrentPtr = idPtr + sizeof(uint32_t);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400121 *commandId = id;
122 return true;
123 }
124
125 void* CommandIterator::NextCommand(size_t commandSize, size_t commandAlignment) {
Corentin Wallezfbecc282017-11-23 10:32:51 -0800126 uint8_t* commandPtr = AlignPtr(mCurrentPtr, commandAlignment);
127 ASSERT(commandPtr + sizeof(commandSize) <= mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400128
Corentin Wallezfbecc282017-11-23 10:32:51 -0800129 mCurrentPtr = commandPtr + commandSize;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400130 return commandPtr;
131 }
132
133 void* CommandIterator::NextData(size_t dataSize, size_t dataAlignment) {
134 uint32_t id;
135 bool hasId = NextCommandId(&id);
136 ASSERT(hasId);
137 ASSERT(id == AdditionalData);
138
139 return NextCommand(dataSize, dataAlignment);
140 }
141
142 // Potential TODO(cwallez@chromium.org):
143 // - Host the size and pointer to next block in the block itself to avoid having an allocation in the vector
144 // - Assume T's alignof is, say 64bits, static assert it, and make commandAlignment a constant in Allocate
145 // - Be able to optimize allocation to one block, for command buffers expected to live long to avoid cache misses
146 // - Better block allocation, maybe have NXT API to say command buffer is going to have size close to another
147
148 CommandAllocator::CommandAllocator()
Corentin Wallezfbecc282017-11-23 10:32:51 -0800149 : mCurrentPtr(reinterpret_cast<uint8_t*>(&mDummyEnum[0])), mEndPtr(reinterpret_cast<uint8_t*>(&mDummyEnum[1])) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400150 }
151
152 CommandAllocator::~CommandAllocator() {
Corentin Wallezfbecc282017-11-23 10:32:51 -0800153 ASSERT(mBlocks.empty());
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400154 }
155
156 CommandBlocks&& CommandAllocator::AcquireBlocks() {
Corentin Wallezfbecc282017-11-23 10:32:51 -0800157 ASSERT(mCurrentPtr != nullptr && mEndPtr != nullptr);
158 ASSERT(IsPtrAligned(mCurrentPtr, alignof(uint32_t)));
159 ASSERT(mCurrentPtr + sizeof(uint32_t) <= mEndPtr);
160 *reinterpret_cast<uint32_t*>(mCurrentPtr) = EndOfBlock;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400161
Corentin Wallezfbecc282017-11-23 10:32:51 -0800162 mCurrentPtr = nullptr;
163 mEndPtr = nullptr;
164 return std::move(mBlocks);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400165 }
166
167 uint8_t* CommandAllocator::Allocate(uint32_t commandId, size_t commandSize, size_t commandAlignment) {
Corentin Wallezfbecc282017-11-23 10:32:51 -0800168 ASSERT(mCurrentPtr != nullptr);
169 ASSERT(mEndPtr != nullptr);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400170 ASSERT(commandId != EndOfBlock);
171
172 // It should always be possible to allocate one id, for EndOfBlock tagging,
Corentin Wallezfbecc282017-11-23 10:32:51 -0800173 ASSERT(IsPtrAligned(mCurrentPtr, alignof(uint32_t)));
174 ASSERT(mCurrentPtr + sizeof(uint32_t) <= mEndPtr);
175 uint32_t* idAlloc = reinterpret_cast<uint32_t*>(mCurrentPtr);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400176
Corentin Wallezfbecc282017-11-23 10:32:51 -0800177 uint8_t* commandAlloc = AlignPtr(mCurrentPtr + sizeof(uint32_t), commandAlignment);
Austin Eng8867e5d2017-07-14 18:53:07 -0400178 uint8_t* nextPtr = AlignPtr(commandAlloc + commandSize, alignof(uint32_t));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400179
180 // When there is not enough space, we signal the EndOfBlock, so that the iterator nows to
181 // move to the next one. EndOfBlock on the last block means the end of the commands.
Corentin Wallezfbecc282017-11-23 10:32:51 -0800182 if (nextPtr + sizeof(uint32_t) > mEndPtr) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400183
184 // Even if we are not able to get another block, the list of commands will be well-formed
185 // and iterable as this block will be that last one.
186 *idAlloc = EndOfBlock;
187
188 // Make sure we have space for current allocation, plus end of block and alignment padding
189 // for the first id.
Corentin Wallezfbecc282017-11-23 10:32:51 -0800190 ASSERT(nextPtr > mCurrentPtr);
191 if (!GetNewBlock(static_cast<size_t>(nextPtr - mCurrentPtr) + sizeof(uint32_t) + alignof(uint32_t))) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400192 return nullptr;
193 }
194 return Allocate(commandId, commandSize, commandAlignment);
195 }
196
197 *idAlloc = commandId;
Corentin Wallezfbecc282017-11-23 10:32:51 -0800198 mCurrentPtr = nextPtr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400199 return commandAlloc;
200 }
201
202 uint8_t* CommandAllocator::AllocateData(size_t commandSize, size_t commandAlignment) {
203 return Allocate(AdditionalData, commandSize, commandAlignment);
204 }
205
206 bool CommandAllocator::GetNewBlock(size_t minimumSize) {
207 // Allocate blocks doubling sizes each time, to a maximum of 16k (or at least minimumSize).
Corentin Wallezfbecc282017-11-23 10:32:51 -0800208 mLastAllocationSize = std::max(minimumSize, std::min(mLastAllocationSize * 2, size_t(16384)));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400209
Corentin Wallezfbecc282017-11-23 10:32:51 -0800210 uint8_t* block = reinterpret_cast<uint8_t*>(malloc(mLastAllocationSize));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400211 if (block == nullptr) {
212 return false;
213 }
214
Corentin Wallezfbecc282017-11-23 10:32:51 -0800215 mBlocks.push_back({mLastAllocationSize, block});
216 mCurrentPtr = AlignPtr(block, alignof(uint32_t));
217 mEndPtr = block + mLastAllocationSize;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400218 return true;
219 }
220
221}