1 /* 2 * Copyright (C) 2018-2021 Intel Corporation 3 * 4 * SPDX-License-Identifier: MIT 5 * 6 */ 7 8 #pragma once 9 #include "shared/source/helpers/debug_helpers.h" 10 #include "shared/source/helpers/ptr_math.h" 11 12 #include <atomic> 13 #include <cstddef> 14 #include <cstdint> 15 16 namespace NEO { 17 class GraphicsAllocation; 18 19 class LinearStream { 20 public: 21 virtual ~LinearStream() = default; 22 LinearStream(); 23 LinearStream(void *buffer, size_t bufferSize); 24 LinearStream(GraphicsAllocation *buffer); 25 LinearStream(GraphicsAllocation *gfxAllocation, void *buffer, size_t bufferSize); 26 void *getCpuBase() const; 27 void *getSpace(size_t size); 28 size_t getMaxAvailableSpace() const; 29 size_t getAvailableSpace() const; 30 size_t getUsed() const; 31 void overrideMaxSize(size_t newMaxSize); 32 void replaceBuffer(void *buffer, size_t bufferSize); 33 GraphicsAllocation *getGraphicsAllocation() const; 34 void replaceGraphicsAllocation(GraphicsAllocation *gfxAllocation); 35 36 template <typename Cmd> getSpaceForCmd()37 Cmd *getSpaceForCmd() { 38 auto ptr = getSpace(sizeof(Cmd)); 39 return reinterpret_cast<Cmd *>(ptr); 40 } 41 42 protected: 43 std::atomic<size_t> sizeUsed; 44 size_t maxAvailableSpace; 45 void *buffer; 46 GraphicsAllocation *graphicsAllocation; 47 }; 48 getCpuBase()49inline void *LinearStream::getCpuBase() const { 50 return buffer; 51 } 52 getSpace(size_t size)53inline void *LinearStream::getSpace(size_t size) { 54 UNRECOVERABLE_IF(sizeUsed + size > maxAvailableSpace); 55 auto memory = ptrOffset(buffer, sizeUsed); 56 sizeUsed += size; 57 return memory; 58 } 59 getMaxAvailableSpace()60inline size_t LinearStream::getMaxAvailableSpace() const { 61 return maxAvailableSpace; 62 } 63 getAvailableSpace()64inline size_t LinearStream::getAvailableSpace() const { 65 DEBUG_BREAK_IF(sizeUsed > maxAvailableSpace); 66 return maxAvailableSpace - sizeUsed; 67 } 68 getUsed()69inline size_t LinearStream::getUsed() const { 70 return sizeUsed; 71 } 72 overrideMaxSize(size_t newMaxSize)73inline void LinearStream::overrideMaxSize(size_t newMaxSize) { 74 maxAvailableSpace = newMaxSize; 75 } 76 replaceBuffer(void * buffer,size_t bufferSize)77inline void LinearStream::replaceBuffer(void *buffer, size_t bufferSize) { 78 this->buffer = buffer; 79 maxAvailableSpace = bufferSize; 80 sizeUsed = 0; 81 } 82 getGraphicsAllocation()83inline GraphicsAllocation *LinearStream::getGraphicsAllocation() const { 84 return graphicsAllocation; 85 } 86 replaceGraphicsAllocation(GraphicsAllocation * gfxAllocation)87inline void LinearStream::replaceGraphicsAllocation(GraphicsAllocation *gfxAllocation) { 88 graphicsAllocation = gfxAllocation; 89 } 90 } // namespace NEO 91