11/*
2- * Copyright (C) 2017-2019 Intel Corporation
2+ * Copyright (C) 2017-2020 Intel Corporation
33 *
44 * SPDX-License-Identifier: MIT
55 *
@@ -187,3 +187,102 @@ TEST_F(CommandContainerTest, givenCommandContainerWhenWantToAddAleradyAddedAlloc
187187
188188 EXPECT_EQ (sizeAfterFirstAdd, sizeAfterSecondAdd);
189189}
190+
191+ TEST_F (CommandContainerTest, givenAvailableSpaceWhenGetHeapWithRequiredSizeAndAlignmentCalledThenExistingAllocationIsReturned) {
192+ std::unique_ptr<CommandContainer> cmdContainer (new CommandContainer);
193+ cmdContainer->initialize (pDevice);
194+ cmdContainer->setDirtyStateForAllHeaps (false );
195+ auto heapAllocation = cmdContainer->getIndirectHeapAllocation (HeapType::SURFACE_STATE);
196+ auto heap = cmdContainer->getIndirectHeap (HeapType::SURFACE_STATE);
197+
198+ const size_t sizeRequested = 32 ;
199+ const size_t alignment = 32 ;
200+
201+ EXPECT_GE (heap->getAvailableSpace (), sizeRequested + alignment);
202+
203+ auto heapRequested = cmdContainer->getHeapWithRequiredSizeAndAlignment (HeapType::SURFACE_STATE, sizeRequested, alignment);
204+ auto newAllocation = heapRequested->getGraphicsAllocation ();
205+
206+ EXPECT_EQ (heap, heapRequested);
207+ EXPECT_EQ (heapAllocation, newAllocation);
208+
209+ EXPECT_TRUE ((reinterpret_cast <size_t >(heapRequested->getSpace (0 )) & (alignment - 1 )) == 0 );
210+ EXPECT_FALSE (cmdContainer->isHeapDirty (HeapType::SURFACE_STATE));
211+ }
212+
213+ TEST_F (CommandContainerTest, givenUnalignedAvailableSpaceWhenGetHeapWithRequiredSizeAndAlignmentCalledThenHeapReturnedIsCorrectlyAligned) {
214+ std::unique_ptr<CommandContainer> cmdContainer (new CommandContainer);
215+ cmdContainer->initialize (pDevice);
216+ cmdContainer->setDirtyStateForAllHeaps (false );
217+ auto heapAllocation = cmdContainer->getIndirectHeapAllocation (HeapType::SURFACE_STATE);
218+ auto heap = cmdContainer->getIndirectHeap (HeapType::SURFACE_STATE);
219+
220+ const size_t sizeRequested = 32 ;
221+ const size_t alignment = 32 ;
222+
223+ heap->getSpace (sizeRequested / 2 );
224+
225+ EXPECT_GE (heap->getAvailableSpace (), sizeRequested + alignment);
226+
227+ auto heapRequested = cmdContainer->getHeapWithRequiredSizeAndAlignment (HeapType::SURFACE_STATE, sizeRequested, alignment);
228+ auto newAllocation = heapRequested->getGraphicsAllocation ();
229+
230+ EXPECT_EQ (heap, heapRequested);
231+ EXPECT_EQ (heapAllocation, newAllocation);
232+
233+ EXPECT_TRUE ((reinterpret_cast <size_t >(heapRequested->getSpace (0 )) & (alignment - 1 )) == 0 );
234+ EXPECT_FALSE (cmdContainer->isHeapDirty (HeapType::SURFACE_STATE));
235+ }
236+
237+ TEST_F (CommandContainerTest, givenNoAlignmentAndAvailableSpaceWhenGetHeapWithRequiredSizeAndAlignmentCalledThenHeapReturnedIsNotAligned) {
238+ std::unique_ptr<CommandContainer> cmdContainer (new CommandContainer);
239+ cmdContainer->initialize (pDevice);
240+ cmdContainer->setDirtyStateForAllHeaps (false );
241+ auto heapAllocation = cmdContainer->getIndirectHeapAllocation (HeapType::SURFACE_STATE);
242+ auto heap = cmdContainer->getIndirectHeap (HeapType::SURFACE_STATE);
243+
244+ const size_t sizeRequested = 32 ;
245+ const size_t alignment = 0 ;
246+
247+ heap->getSpace (sizeRequested / 2 );
248+
249+ EXPECT_GE (heap->getAvailableSpace (), sizeRequested + alignment);
250+
251+ auto heapRequested = cmdContainer->getHeapWithRequiredSizeAndAlignment (HeapType::SURFACE_STATE, sizeRequested, alignment);
252+ auto newAllocation = heapRequested->getGraphicsAllocation ();
253+
254+ EXPECT_EQ (heap, heapRequested);
255+ EXPECT_EQ (heapAllocation, newAllocation);
256+
257+ EXPECT_TRUE ((reinterpret_cast <size_t >(heapRequested->getSpace (0 )) & (sizeRequested / 2 )) == sizeRequested / 2 );
258+ EXPECT_FALSE (cmdContainer->isHeapDirty (HeapType::SURFACE_STATE));
259+ }
260+
261+ TEST_F (CommandContainerTest, givenNotEnoughSpaceWhenGetHeapWithRequiredSizeAndAlignmentCalledThenNewAllocationIsReturned) {
262+ std::unique_ptr<CommandContainer> cmdContainer (new CommandContainer);
263+ cmdContainer->initialize (pDevice);
264+ cmdContainer->setDirtyStateForAllHeaps (false );
265+ auto heapAllocation = cmdContainer->getIndirectHeapAllocation (HeapType::SURFACE_STATE);
266+ auto heap = cmdContainer->getIndirectHeap (HeapType::SURFACE_STATE);
267+
268+ const size_t sizeRequested = 32 ;
269+ const size_t alignment = 32 ;
270+ size_t availableSize = heap->getAvailableSpace ();
271+
272+ heap->getSpace (availableSize - sizeRequested / 2 );
273+
274+ EXPECT_LT (heap->getAvailableSpace (), sizeRequested + alignment);
275+
276+ auto heapRequested = cmdContainer->getHeapWithRequiredSizeAndAlignment (HeapType::SURFACE_STATE, sizeRequested, alignment);
277+ auto newAllocation = heapRequested->getGraphicsAllocation ();
278+
279+ EXPECT_EQ (heap, heapRequested);
280+ EXPECT_NE (heapAllocation, newAllocation);
281+
282+ EXPECT_TRUE ((reinterpret_cast <size_t >(heapRequested->getSpace (0 )) & (alignment - 1 )) == 0 );
283+ EXPECT_TRUE (cmdContainer->isHeapDirty (HeapType::SURFACE_STATE));
284+
285+ for (auto deallocation : cmdContainer->getDeallocationContainer ()) {
286+ cmdContainer->getDevice ()->getMemoryManager ()->freeGraphicsMemory (deallocation);
287+ }
288+ }
0 commit comments