void USoundWave::FreeResources()
{
	check(IsInAudioThread());

	// Housekeeping of stats
	DEC_FLOAT_STAT_BY( STAT_AudioBufferTime, Duration );
	DEC_FLOAT_STAT_BY( STAT_AudioBufferTimeChannels, NumChannels * Duration );

	// GEngine is NULL during script compilation and GEngine->Client and its audio device might be
	// destroyed first during the exit purge.
	if( GEngine && !GExitPurge )
	{
		// Notify the audio device to free the bulk data associated with this wave.
		FAudioDeviceManager* AudioDeviceManager = GEngine->GetAudioDeviceManager();
		if (AudioDeviceManager)
		{
			AudioDeviceManager->StopSoundsUsingResource(this);
			AudioDeviceManager->FreeResource(this);
		}
	}

	if (CachedRealtimeFirstBuffer)
	{
		FMemory::Free(CachedRealtimeFirstBuffer);
		CachedRealtimeFirstBuffer = nullptr;
	}

	// Just in case the data was created but never uploaded
	if (RawPCMData)
	{
		FMemory::Free(RawPCMData);
		RawPCMData = nullptr;
	}

	// Remove the compressed copy of the data
	RemoveAudioResource();

	// Stat housekeeping
	DEC_DWORD_STAT_BY(STAT_AudioMemorySize, TrackedMemoryUsage);
	DEC_DWORD_STAT_BY(STAT_AudioMemory, TrackedMemoryUsage);
	TrackedMemoryUsage = 0;

	ResourceID = 0;
	bDynamicResource = false;
	DecompressionType = DTYPE_Setup;
	bDecompressedFromOgg = 0;

	USoundWave* SoundWave = this;
	FAudioThread::RunCommandOnGameThread([SoundWave]()
	{
		SoundWave->ResourceState = ESoundWaveResourceState::Freed;
	}, TStatId());
}
Example #2
0
void runGestureOnGameThread(int a, int b, TFunction< void()> InFunction)
{
	if (a == b)
	{
		FFunctionGraphTask::CreateAndDispatchWhenReady(InFunction, TStatId(), nullptr, ENamedThreads::GameThread);
	}
}
// maybe called in another thread
void UOVRLipSyncContextComponent::ProcessFrame(const uint8* AudioData, const int32 BufferSize)
{
    check(BufferSize >= VISEME_BUF_SIZE);
    if (CurrentContext == 0)
        return;

    for (uint32 i = 0; i < VISEME_SAMPLES; i++)
    {
        int16 Sample = (int16)(AudioData[i * 2 + 1] << 8 | AudioData[i * 2]);
        SampleBuffer[i * 2] = Sample / (float)SHRT_MAX;
        SampleBuffer[i * 2 + 1] = Sample / (float)SHRT_MAX;
    }
    FOVRLipSyncFrame TempFrame;
    FOVRLipSync::ProcessFrameInterleaved(CurrentContext, SampleBuffer, ovrLipSyncFlag::None, &TempFrame);

    // only lock the frame copy operation
    {
        FScopeLock LipSyncFrameLock(&LipSyncFrameCriticalSection);
        CurrentFrame = TempFrame;
    }

    // dispatch the event
    if (VisemeGenerated.IsBound())
    {
        FFunctionGraphTask::CreateAndDispatchWhenReady([=]()
        {
            VisemeGenerated.Broadcast(CurrentFrame);
        }
        , TStatId(), nullptr, ENamedThreads::GameThread);
    }
}
Example #4
0
void UOscDispatcher::Callback(const FArrayReaderPtr& data, const FIPv4Endpoint&)
{
    UE_LOG(LogOSC, Verbose, TEXT("OSC Received"));
    const osc::ReceivedPacket packet((const char *)data->GetData(), data->Num());
    if(packet.IsBundle())
    {
        SendBundle(_pendingMessages, osc::ReceivedBundle(packet));
    }
    else
    {
        SendMessage(_pendingMessages, osc::ReceivedMessage(packet));
    }
    
    if(!_pendingMessages.IsEmpty())
    {
        FSimpleDelegateGraphTask::CreateAndDispatchWhenReady(
            FSimpleDelegateGraphTask::FDelegate::CreateUObject(this, &UOscDispatcher::CallbackMainThread),
#if OSC_ENGINE_VERSION < 40500
            TEXT("OscDispatcherProcessMessages"),
#else
            TStatId(),
#endif
            nullptr,
            ENamedThreads::GameThread);
    }
}
void FDiagnoseReportWorker::DoWork()
{
	const FText ReportText = CrashReportClient->ErrorReport.DiagnoseReport();
	// Inform the game thread that we are done.
	FSimpleDelegateGraphTask::CreateAndDispatchWhenReady
	(
		FSimpleDelegateGraphTask::FDelegate::CreateRaw( CrashReportClient, &FCrashReportClient::FinalizeDiagnoseReportWorker, ReportText ),
		TStatId(), nullptr, ENamedThreads::GameThread
	);
}
FGraphEventRef UBackgroundTask::RunTaskOnGameThread(FSimpleDelegateGraphTask::FDelegate InFunction)
{
	return FSimpleDelegateGraphTask::CreateAndDispatchWhenReady
		(
		InFunction
		, TStatId()
		, nullptr
		, ENamedThreads::GameThread
		);
}
void ASpeechRecognitionActor::WordsSpoken_method(FRecognisedPhrases text)
{
	FSimpleDelegateGraphTask::CreateAndDispatchWhenReady
		(
			FSimpleDelegateGraphTask::FDelegate::CreateStatic(&WordsSpoken_trigger, OnWordsSpoken, text)
			, TStatId()
			, nullptr
			, ENamedThreads::GameThread
			);
}
void ASpeechRecognitionActor::StoppedSpeaking_method()
{
	FSimpleDelegateGraphTask::CreateAndDispatchWhenReady
		(
			FSimpleDelegateGraphTask::FDelegate::CreateStatic(&StoppedSpeaking_trigger, OnStoppedSpeaking)
			, TStatId()
			, nullptr
			, ENamedThreads::GameThread
			);
}
void USocketIOClientComponent::Bind(FString Name)
{
    PrivateClient.socket()->on(StdString(Name), sio::socket::event_listener_aux([&](std::string const& name, sio::message::ptr const& data, bool isAck, sio::message::list &ack_resp) {

        const FString SafeName = FStringFromStd(name);
        const FString SafeData = FStringFromStd(data->get_string());
        FFunctionGraphTask::CreateAndDispatchWhenReady([&, SafeName, SafeData]
        {
            On.Broadcast(SafeName, SafeData);
        }, TStatId(), nullptr, ENamedThreads::GameThread);
    }));
}
void ASpeechRecognitionActor::WordSpoken_method(FString text)
{
	// TODO: Split by phrases
	// for now, split by whitespace
	TArray<FString> words;
	int32 wordCount = text.ParseIntoArrayWS(words, _T("\n"), true);

	for (int i = 0; i < wordCount; i++) {
		FSimpleDelegateGraphTask::CreateAndDispatchWhenReady
			(
			FSimpleDelegateGraphTask::FDelegate::CreateStatic(&WordSpoken_trigger, OnWordSpoken, words[i])
			, TStatId()
			, nullptr
			, ENamedThreads::GameThread
			);
	}

}
	FORCEINLINE TStatId GetStatId() const
	{
		return TStatId();
		//RETURN_QUICK_DECLARE_CYCLE_STAT(FAsyncNetworkWriteWorker, STATGROUP_ThreadPoolAsyncTasks);
	}
TStatId FAdvancedPreviewScene::GetStatId() const
{
	return TStatId();
}
TStatId USpatialInterface::GetStatId() const
{
    return TStatId();
}
		FORCEINLINE TStatId GetStatId() const
		{
			// TODO: This is called too early in engine startup.
			return TStatId();
			//RETURN_QUICK_DECLARE_CYCLE_STAT(FPakUncompressTask, STATGROUP_ThreadPoolAsyncTasks);
		}
Example #15
0
	}
	AllocateUObjectIndexForCurrentThread(this);
	check(InName != NAME_None && InternalIndex >= 0);
	HashObject(this);
	check(IsValidLowLevel());
}

/**
 * Just change the FName and Outer and rehash into name hash tables. For use by higher level rename functions.
 *
 * @param NewName	new name for this object
 * @param NewOuter	new outer for this object, if NULL, outer will be unchanged
 */
void UObjectBase::LowLevelRename(FName NewName,UObject *NewOuter)
{
	STAT(StatID = TStatId();) // reset the stat id since this thing now has a different name
	UnhashObject(this);
	check(InternalIndex >= 0);
	Name = NewName;
	if (NewOuter)
	{
		Outer = NewOuter;
	}
	HashObject(this);
}

void UObjectBase::SetClass(UClass* NewClass)
{
	STAT(StatID = TStatId();) // reset the stat id since this thing now has a different name

	UnhashObject(this);
Example #16
0
FGraphEventRef UBackgroundTask::RunLambdaOnGameThread(TFunction< void()> InFunction)
{
	return FFunctionGraphTask::CreateAndDispatchWhenReady(InFunction, TStatId(), nullptr, ENamedThreads::GameThread);
}
TStatId FRuntimeAssetCacheAsyncWorker::GetStatId()
{
	return TStatId();
}
void DebugLeakTest()
{
	if (CVarEnableLeakTest.GetValueOnGameThread() == 1)
	{
		if (GFrameCounter == 60)
		{
			DirectStatsCommand( TEXT( "stat namedmarker Frame-060" ), true );
		}

		if (GFrameCounter == 120)
		{
			DirectStatsCommand( TEXT( "stat namedmarker Frame-120" ), true );
		}


		if (GFrameCounter == 240)
		{
			DirectStatsCommand( TEXT( "stat namedmarker Frame-240" ), true );
		}

		if (GFrameCounter == 300)
		{
			GIsRequestingExit = true;
		}

		// Realloc.
		static TArray<uint8> Array;
		static int32 Initial = 1;
		{
			DECLARE_SCOPE_CYCLE_COUNTER( TEXT( "LeakTest::Realloc" ), Stat_LeakTest_Realloc, STATGROUP_Quick );
			Array.AddZeroed( Initial );
			Initial += 100;
		}

		if (GFrameCounter == 300)
		{
			UE_LOG( LogTemp, Warning, TEXT( "Stat_ReallocTest: %i / %i" ), Array.GetAllocatedSize(), Initial );
		}

		// General memory leak.
		{
			DECLARE_SCOPE_CYCLE_COUNTER( TEXT( "LeakTest::NewInt8" ), Stat_LeakTest_NewInt8, STATGROUP_Quick );
			int8* Leak = new int8[1000 * 1000];
		}


		if (GFrameCounter < 250)
		{
			// Background threads memory test.
			struct FAllocTask
			{
				static void Alloc()
				{
					DECLARE_SCOPE_CYCLE_COUNTER( TEXT( "FAllocTask::Alloc" ), Stat_FAllocTask_Alloc, STATGROUP_Quick );

					int8* IntAlloc = new int8[112233];
					int8* LeakTask = new int8[100000];
					delete[] IntAlloc;
				}
			};

			for (int32 Index = 0; Index < 40; ++Index)
			{
				FSimpleDelegateGraphTask::CreateAndDispatchWhenReady( FSimpleDelegateGraphTask::FDelegate::CreateStatic( FAllocTask::Alloc ), TStatId() );
			}

			class FAllocPool : public FNonAbandonableTask
			{
			public:
				void DoWork()
				{
					DECLARE_SCOPE_CYCLE_COUNTER( TEXT( "FAllocPool::DoWork" ), Stat_FAllocPool_DoWork, STATGROUP_Quick );

					int8* IntAlloc = new int8[223311];
					int8* LeakTask = new int8[100000];
					delete[] IntAlloc;
				}

				TStatId GetStatId() const
				{
					return TStatId();
				}
			};

			for (int32 Index = 0; Index < 40; ++Index)
			{
				(new FAutoDeleteAsyncTask<FAllocPool>())->StartBackgroundTask();
			}
		}

		for (int32 Index = 0; Index < 40; ++Index)
		{
			DECLARE_SCOPE_CYCLE_COUNTER( TEXT( "DebugLeakTest::Alloc" ), Stat_LeakTest_Alloc, STATGROUP_Quick );

			int8* IntAlloc = new int8[331122];
			int8* LeakTask = new int8[100000];
			delete[] IntAlloc;
		}

		if (GIsRequestingExit)
		{
			// If we are writing stats data, stop it now.
			DirectStatsCommand( TEXT( "stat stopfile" ), true );
		}
	}
}
	virtual TStatId GetHighPerformanceEnableForStat(FName StatShortName, const char* InGroup, const char* InCategory, bool bDefaultEnable, bool bShouldClearEveryFrame, EStatDataType::Type InStatType, TCHAR const* InDescription, bool bCycleStat, FPlatformMemory::EMemoryCounterRegion MemoryRegion = FPlatformMemory::MCR_Invalid) override
	{
		FScopeLock ScopeLock(&SynchronizationObject);

		FStatNameAndInfo LongName(StatShortName, InGroup, InCategory, InDescription, InStatType, bShouldClearEveryFrame, bCycleStat, MemoryRegion);

		FName Stat = LongName.GetEncodedName();

		FName Group(InGroup);
		FGroupEnable* Found = HighPerformanceEnable.Find(Group);
		if (Found)
		{
			if (Found->DefaultEnable != bDefaultEnable)
			{
				UE_LOG(LogStatGroupEnableManager, Fatal, TEXT("Stat group %s was was defined both on and off by default."), *Group.ToString());
			}
			TStatIdData** StatFound = Found->NamesInThisGroup.Find( Stat );
			TStatIdData** StatFoundAlways = Found->AlwaysEnabledNamesInThisGroup.Find( Stat );
			if( StatFound )
			{
				if( StatFoundAlways )
				{
					UE_LOG( LogStatGroupEnableManager, Fatal, TEXT( "Stat %s is both always enabled and not always enabled, so it was used for two different things." ), *Stat.ToString() );
				}
				return TStatId( *StatFound );
			}
			else if( StatFoundAlways )
			{
				return TStatId( *StatFoundAlways );
			}
		}
		else
		{
			Found = &HighPerformanceEnable.Add( Group, FGroupEnable( bDefaultEnable || !bShouldClearEveryFrame ) );

			// this was set up before we saw the group, so set the enable now
			if (EnableForNewGroup.Contains(Group))
			{
				Found->CurrentEnable = EnableForNewGroup.FindChecked(Group);
				EnableForNewGroup.Remove(Group); // by definition, we will never need this again
			}
			else if (UseEnableForNewGroups)
			{
				Found->CurrentEnable = EnableForNewGroups;
			}
		}
		if (PendingCount < 1)
		{
			PendingStatIds = new TStatIdData[NUM_PER_BLOCK];
			FMemory::Memzero( PendingStatIds, NUM_PER_BLOCK * sizeof( TStatIdData ) );
			PendingCount = NUM_PER_BLOCK;
		}
		--PendingCount;
		TStatIdData* Result = PendingStatIds;

		const FString StatDescription = InDescription ? InDescription : StatShortName.GetPlainNameString();

		// Get the wide stat description.
		const int32 StatDescLen = StatDescription.Len() + 1;
		// We are leaking this. @see STAT_StatDescMemory
		WIDECHAR* StatDescWide = new WIDECHAR[StatDescLen];
		TCString<WIDECHAR>::Strcpy( StatDescWide, StatDescLen, StringCast<WIDECHAR>( *StatDescription ).Get() );
		Result->WideString = reinterpret_cast<uint64>(StatDescWide);

		// Get the ansi stat description.
		// We are leaking this. @see STAT_StatDescMemory
		ANSICHAR* StatDescAnsi = new ANSICHAR[StatDescLen];
		TCString<ANSICHAR>::Strcpy( StatDescAnsi, StatDescLen, StringCast<ANSICHAR>( *StatDescription ).Get() );
		Result->AnsiString = reinterpret_cast<uint64>(StatDescAnsi);

		MemoryCounter.Add( StatDescLen*(sizeof( ANSICHAR ) + sizeof( WIDECHAR )) );
		
		++PendingStatIds;

		if( Found->CurrentEnable )
		{
			EnableStat( Stat, Result );
		}

		if( bShouldClearEveryFrame )
		{
			Found->NamesInThisGroup.Add( Stat, Result );
		}
		else
		{
			Found->AlwaysEnabledNamesInThisGroup.Add( Stat, Result );
		}
		return TStatId(Result);
	}
				TStatId GetStatId() const
				{
					return TStatId();
				}