RavEngine
Loading...
Searching...
No Matches
PxProfileDataBuffer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_PROFILE_DATA_BUFFER_H
30#define PX_PROFILE_DATA_BUFFER_H
31
32#include "PxProfileAllocatorWrapper.h"
33#include "PxProfileMemoryBuffer.h"
34#include "PxProfileEventBufferClient.h"
35
36namespace physx { namespace profile {
37
38 template<typename TMutex
39 , typename TScopedLock>
40 class DataBuffer //base class for buffers that cache data and then dump the data to clients.
41 {
42 public:
43 typedef TMutex TMutexType;
44 typedef TScopedLock TScopedLockType;
46
49
50 protected:
51
53 TMemoryBufferType mDataArray;
54 TBufferClientArray mBufferClients;
55 uint32_t mBufferFullAmount;
56 EventContextInformation mEventContextInformation;
57 TMutexType* mBufferMutex;
58 volatile bool mHasClients;
60
61 public:
62
63 DataBuffer( PxAllocatorCallback* inFoundation
64 , uint32_t inBufferFullAmount
65 , TMutexType* inBufferMutex
66 , const char* inAllocationName )
67 : mWrapper( inFoundation )
68 , mDataArray( TU8AllocatorType( mWrapper, inAllocationName ) )
69 , mBufferClients( mWrapper )
70 , mBufferFullAmount( inBufferFullAmount )
71 , mBufferMutex( inBufferMutex )
72 , mHasClients( false )
73 , mSerializer( &mDataArray )
74 {
75 //The data array is never resized really. We ensure
76 //it is bigger than it will ever need to be.
77 mDataArray.reserve( inBufferFullAmount + 68 );
78 }
79
80 virtual ~DataBuffer()
81 {
82 while(mBufferClients.size() )
83 {
84 removeClient( *mBufferClients[0] );
85 }
86 }
87
88 PxProfileAllocatorWrapper& getWrapper() { return mWrapper; }
89 TMutexType* getBufferMutex() { return mBufferMutex; }
90 void setBufferMutex(TMutexType* mutex) { mBufferMutex = mutex; }
91
92 void addClient( PxProfileEventBufferClient& inClient )
93 {
94 TScopedLockType lock( mBufferMutex );
95 mBufferClients.pushBack( &inClient );
96 mHasClients = true;
97 }
98
99 void removeClient( PxProfileEventBufferClient& inClient )
100 {
101 TScopedLockType lock( mBufferMutex );
102 for ( uint32_t idx =0; idx < mBufferClients.size(); ++idx )
103 {
104 if (mBufferClients[idx] == &inClient )
105 {
106 inClient.handleClientRemoved();
107 mBufferClients.replaceWithLast( idx );
108 break;
109 }
110 }
111 mHasClients = mBufferClients.size() != 0;
112 }
113
114
115 bool hasClients() const
116 {
117 return mHasClients;
118 }
119
120 virtual void flushEvents()
121 {
122 TScopedLockType lock(mBufferMutex);
123 const uint8_t* theData = mDataArray.begin();
124 uint32_t theDataSize = mDataArray.size();
125 sendDataToClients(theData, theDataSize);
126 mDataArray.clear();
127 clearCachedData();
128 }
129
130 //Used for chaining together event buffers.
131 virtual void handleBufferFlush( const uint8_t* inData, uint32_t inDataSize )
132 {
133 TScopedLockType lock( mBufferMutex );
134 if ( inData && inDataSize )
135 {
136 clearCachedData();
137 if ( mDataArray.size() + inDataSize >= mBufferFullAmount )
138 flushEvents();
139 if ( inDataSize >= mBufferFullAmount )
140 sendDataToClients( inData, inDataSize );
141 else
142 mDataArray.write( inData, inDataSize );
143 }
144 }
145
146 protected:
147 virtual void clearCachedData()
148 {
149 }
150
151 private:
152
153 void sendDataToClients( const uint8_t* inData, uint32_t inDataSize )
154 {
155 uint32_t clientCount = mBufferClients.size();
156 for( uint32_t idx =0; idx < clientCount; ++idx )
157 mBufferClients[idx]->handleBufferFlush( inData, inDataSize );
158 }
159
160 };
161
162}}
163
164#endif
165
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
PX_INLINE void replaceWithLast(uint32_t i)
Definition PxArray.h:345
Definition PxProfileDataBuffer.h:41
Definition PxProfileMemoryBuffer.h:39
Client handles the data when an event buffer flushes. This data can be parsed (PxProfileEventHandler....
Definition PxProfileEventBufferClient.h:39
virtual void handleClientRemoved()=0
Happens if something removes all the clients from the manager.
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxProfileEvents.h:322
Definition PxProfileEventSerialization.h:44
Helper struct to encapsulate the user allocator callback Useful for array and hash templates.
Definition PxProfileAllocatorWrapper.h:46
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:152
Helper class to encapsulate the named allocator.
Definition PxProfileAllocatorWrapper.h:115