diff options
Diffstat (limited to 'public/panorama/data')
| -rw-r--r-- | public/panorama/data/iimagesource.h | 93 | ||||
| -rw-r--r-- | public/panorama/data/imageloader.h | 229 | ||||
| -rw-r--r-- | public/panorama/data/panoramavideoplayer.h | 196 |
3 files changed, 518 insertions, 0 deletions
diff --git a/public/panorama/data/iimagesource.h b/public/panorama/data/iimagesource.h new file mode 100644 index 0000000..d6be4f4 --- /dev/null +++ b/public/panorama/data/iimagesource.h @@ -0,0 +1,93 @@ +//=========== Copyright Valve Corporation, All rights reserved. ===============// +// +// Purpose: +//=============================================================================// + +#ifndef IIMAGESOURCE_H +#define IIMAGESOURCE_H + +#ifdef _WIN32 +#pragma once +#endif + +namespace panorama +{ + +enum EImageFormat +{ + k_EImageFormatUnknown, + k_EImageFormatR8G8B8A8, + k_EImageFormatB8G8R8A8_PreMultiplied, + k_EImageFormatA8 +}; + +class CPanel2D; + +// +// Data source for image data used to render an asset +// +class IImageSource: public panorama::IUIJSObject +{ +public: + virtual bool BIsValid() = 0; + virtual uint32 GetTextureID() = 0; + virtual int GetWidth() = 0; + virtual int GetHeight() = 0; + virtual EImageFormat ImageFormat() = 0; + virtual bool BIsAnimating() = 0; + + // Ref counting + virtual int GetRefCount() = 0; + virtual int AddRef() = 0; + virtual int Release() = 0; + + virtual const char *GetJSTypeName() { return "IImageSource"; } + +#ifdef DBGFLAG_VALIDATE + virtual void Validate( CValidator &validator, const tchar *pchName ) = 0; +#endif +protected: + + friend class CImageResourceManager; +}; + + +// +// main interface to load images for display in the ui +// +const int k_ResizeNone = -1; +class IUIImageManager +{ +public: + + // load image data from a URL (file://blah, http://blah/bob.tga, etc), pchDefaultResourceURL may be null, and is what will be used while the real resource is loaded + // if it is set. As such it must be a local file. bPrioritizeLoad will make your request jump to the head of the queue if it is an image over HTTP, use sparingly! + // nResizeWidth and nResizeHeight do nothing if -1, if one is set and the other -1 the image is resized before being made into a texture, but aspect ratio is maintained + // with the specified dimension at the set size, if both are set the image will be stretched if needed. + virtual IImageSource *LoadImageFromURL( const IUIPanel *pPanel, const char *pchDefaultResourceURL, const char *pchResourceURL, bool bPrioritizeLoad, EImageFormat imgFormatOut, int32 nResizeWidth = k_ResizeNone, int32 nResizeHeight = k_ResizeNone, bool bAllowAnimation = true ) = 0; + + // load image data from image file (png/jpg/tga) bytes you already have in memory, pchDefaultResourceURL may be null, and is what will be used while the real resource is loaded + // if it is set. As such it must be a local file. + virtual IImageSource *LoadImageFileFromMemory( const IUIPanel *pPanel, const char *pchResourceURLDefault, const CUtlBuffer &bufFile, int nResizeWidth = panorama::k_ResizeNone, int nResizeHeight = panorama::k_ResizeNone, bool bAllowAnimation = true ) = 0; + + // load image data from RGBA bytes you already have in memory, pchDefaultResourceURL may be null, and is what will be used while the real resource is loaded + // if it is set. As such it must be a local file. + virtual IImageSource *LoadImageFromMemory( const IUIPanel *pPanel, const char *pchDefaultResourceURL, const CUtlBuffer &bufData, int nWide, int nTall, EImageFormat imgFormatIn = k_EImageFormatR8G8B8A8, int nResizeWidth = k_ResizeNone, int nResizeHeight = k_ResizeNone, bool bAllowAnimation = true ) = 0; + + virtual CUtlString GetPchImageSourcePath( IImageSource *pImageSource ) = 0; + + virtual void ReloadChangedImage( IImageSource *pImageToReload ) = 0; + + virtual void ReloadChangedFile( const char *pchFile ) = 0; + +#ifdef DBGFLAG_VALIDATE + virtual void Validate( CValidator &validator, const tchar *pchName ) = 0; +#endif +}; + +DECLARE_PANEL_EVENT1( ImageLoaded, IImageSource * ); +DECLARE_PANEL_EVENT1( ImageFailedLoad, IImageSource * ); + +} // namespace panorama + +#endif // IIMAGESOURCE_H
\ No newline at end of file diff --git a/public/panorama/data/imageloader.h b/public/panorama/data/imageloader.h new file mode 100644 index 0000000..891dd32 --- /dev/null +++ b/public/panorama/data/imageloader.h @@ -0,0 +1,229 @@ +//=========== Copyright Valve Corporation, All rights reserved. ===============// +// +// Purpose: +//=============================================================================// + +#ifndef IMAGESOURCE_H +#define IMAGESOURCE_H + +#ifdef _WIN32 +#pragma once +#endif + +#include "panorama/data/iimagesource.h" +#include "panorama/controls/panelptr.h" +#include "tier1/utlbuffer.h" +#include "tier1/fileio.h" +#include "tier1/utlmap.h" +#include "refcount.h" +#include "../../panorama/uifileresource.h" +#include "tier1/utldelegate.h" + +namespace panorama +{ + +// Helper to determine byte size of a pixel in a given format +int GetFormatPixelBytes( EImageFormat format ); + +class CMovie; +class IUIRenderEngine; +class IUITexture; +class CImageData; + + +// Helper for converting rgba8 to a8 throwing out rgb channels +void ConvertRGBA8ToA8( CUtlBuffer &bufIn, CUtlBuffer &bufOut, uint32 unWide, uint32 unTall ); + +class CImageProxySource; +class CImageLoaderTask; + +#if defined( SOURCE2_PANORAMA ) +class CLoadFromVTexTask; +#endif + +enum ESourceFormats +{ + k_ESourceFormatUnknown, + k_ESourceFormatTGA, + k_ESourceFormatPNG, + k_ESourceFormatJPG, + k_ESourceFormatRawRGBA, + k_ESourceFormatGIF, + k_ESourceFormatVTEX +}; + +class CImageData; +typedef void (ImageDecodeCallback_t)( bool bSuccess, CImageData *pNewImage, CUtlBuffer *pBufDecoded ); + +class CImageDecodeWorkItem +{ +public: + CImageDecodeWorkItem( IUIRenderEngine *pRenderEngine, CUtlBuffer &bufDataInMayModify, const char *pchFilePath, int nWide, int nTall, int nResizeWidth, int nResizeHeight, + EImageFormat formatOut, bool bAllowAnimation, CUtlDelegate< ImageDecodeCallback_t > del ); + ~CImageDecodeWorkItem(); + + void RunWorkItem(); + void DispatchResult(); + +private: + + bool m_bSuccess; + IUIRenderEngine *m_pSurface; + CUtlBuffer *m_pBuffer; + CUtlString m_strFilePath; + int m_nWide; + int m_nTall; + int m_nResizeWidth; + int m_nResizeHeight; + EImageFormat m_eFormat; + bool m_bAllowAnimation; + CImageData *m_pNewImage; + + CUtlDelegate< ImageDecodeCallback_t > m_Del; +}; + +class CImageDecodeWorkThreadPool; +class CImageDecodeThread : public CThread +{ +public: + CImageDecodeThread( CImageDecodeWorkThreadPool *pParent ) + { + m_bExit = false; + m_pParent = pParent; + } + + ~CImageDecodeThread() + { + + } + + void Stop() { m_bExit = true; } + + virtual int Run() OVERRIDE; + +private: + volatile bool m_bExit; + CImageDecodeWorkThreadPool *m_pParent; +}; + +class CImageDecodeWorkThreadPool +{ +public: + CImageDecodeWorkThreadPool(); + ~CImageDecodeWorkThreadPool(); + + // Run frame on main thread + void RunFrame(); + + void AddWorkItem( CImageDecodeWorkItem *pWorkItem ); + +private: + + friend class CImageDecodeThread; + CImageDecodeThread * m_pWorkThreads[1]; + + CThreadMutex m_AsyncIoLock; + CThreadEvent m_ThreadEvent; + CUtlLinkedList< CImageDecodeWorkItem *, int > m_llAsyncIORequests; + CUtlLinkedList< CImageDecodeWorkItem *, int > m_llAsyncIOResults; +}; + +// +// A container of images we have loaded +// +class CImageResourceManager : public IUIImageManager +{ +public: + CImageResourceManager( IUIRenderEngine *pSurface ); + ~CImageResourceManager(); + virtual void Shutdown(); + + virtual IImageSource *LoadImageFromURL( const IUIPanel *pPanel, const char *pchResourceURLDefault, const char *pchResourceURL, bool bPrioritizeLoad, EImageFormat imgFormatOut, int32 nResizeWidth = panorama::k_ResizeNone, int32 nResizeHeight = panorama::k_ResizeNone, bool bAllowAnimation = true ); + virtual IImageSource *LoadImageFileFromMemory( const IUIPanel *pPanel, const char *pchResourceURLDefault, const CUtlBuffer &bufFile, int nResizeWidth = panorama::k_ResizeNone, int nResizeHeight = panorama::k_ResizeNone, bool bAllowAnimation = true ); + virtual IImageSource *LoadImageFromMemory( const IUIPanel *pPanel, const char *pchResourceURLDefault, const CUtlBuffer &bufRGBA, int nWide, int nTall, EImageFormat imgFormatIn = k_EImageFormatR8G8B8A8, int nResizeWidth = panorama::k_ResizeNone, int nResizeHeight = panorama::k_ResizeNone, bool bAllowAnimation = true ); + virtual CUtlString GetPchImageSourcePath( IImageSource *pImageSource ) OVERRIDE; + + virtual void ReloadChangedFile( const char *pchFile ) OVERRIDE; + virtual void ReloadChangedImage( IImageSource *pImageToReload ) OVERRIDE; + + bool OnImageUnreferenced( CImageProxySource *pImage ); + + void RunFrame(); + + void QueueImageDecodeWorkItem( CImageDecodeWorkItem *pWorkItem ); + +#ifdef DBGFLAG_VALIDATE + virtual void Validate( CValidator &validator, const tchar *pchName ); +#endif + +private: + IImageSource *LoadImageInternal( const IUIPanel *pPanel, CFileResource &fileResourceDefault, CFileResource &fileResource, bool bPrioritizeLoad, EImageFormat imgFormatOut, int32 nResizeWidth, int32 nResizeHeight, bool bAllowAnimation ); + CImageProxySource *GetDefaultImage( CFileResource &fileDefault, EImageFormat imgFormatOut, bool bAllowAnimation ); + + friend class CLoadFileURLTask; + friend class CLoadFileLocalTask; + friend class CImageLoaderTask; +#if defined( SOURCE2_PANORAMA ) + friend class CLoadFromVTexTask; +#endif + + // Internally adds image to our tracking maps + void AddImageToManager( CFileResource &resource, CImageProxySource *pImageProxy, int32 nResizeWidth, int32 nResizeHeight, bool bAllowAnimation ); + bool RemoveImageFromManager( IImageSource *pImage ); + + // Loads a resource, returns vector index + bool OnImageLoaded( CFileResource & resource, CImageData *pImage, int32 nResizeWidth, int32 nResizeHeight, bool bAllowAnimation ); + bool OnFailedImageLoad( CFileResource & resource, int32 nResizeWidth, int32 nResizeHeight, bool bAllowAnimation ); + void AddLoad( CFileResource &resource, EImageFormat eFormat, bool bPrioritizeLoad, int32 nResizeWidth, int32 nResizeHeight, bool bAllowAnimation ); + + // Synchronous load only used for initial global default image + bool LoadLocalFileSynchronous( CFileResource &resource, EImageFormat eFormat, bool bAllowAnimation ); + +#if defined( SOURCE2_PANORAMA ) + bool FixupFileResourceToCompiledImage( CFileResource &fileResource ); +#endif + + struct UrlImageKey_t + { + CFileResource fileResource; + int32 nTargetWidth; + int32 nTargetHeight; + bool bAllowAnimation; + + // Sort on size only + bool operator <( const UrlImageKey_t &l ) const + { + if ( nTargetWidth < l.nTargetWidth ) + return true; + else if ( nTargetWidth > l.nTargetWidth ) + return false; + + if ( nTargetHeight < l.nTargetHeight ) + return true; + else if ( nTargetHeight > l.nTargetHeight ) + return false; + + if ( bAllowAnimation && !l.bAllowAnimation ) + return true; + else if ( !bAllowAnimation && l.bAllowAnimation ) + return false; + + return fileResource < l.fileResource; + } + }; + CUtlMap< UrlImageKey_t, CImageProxySource *, int, CDefLess< UrlImageKey_t > > m_mapImagesByURL; + CUtlMap< IImageSource *, UrlImageKey_t, int, CDefLess< IImageSource *> > m_mapAllImages; + + CUtlVector< CImageLoaderTask * > m_vecLoaderTasksToStart; + CUtlRBTree< CImageLoaderTask *, int, CDefLess< CImageLoaderTask * > > m_treeLoadTasks; + + IUIRenderEngine *m_pSurface; + + CImageDecodeWorkThreadPool *m_pImageDecodePool; + + bool m_bInited; +}; + +} // namespace panorama + +#endif // IMAGESOURCE_H
\ No newline at end of file diff --git a/public/panorama/data/panoramavideoplayer.h b/public/panorama/data/panoramavideoplayer.h new file mode 100644 index 0000000..5478dbd --- /dev/null +++ b/public/panorama/data/panoramavideoplayer.h @@ -0,0 +1,196 @@ +//=========== Copyright Valve Corporation, All rights reserved. ===============// +// +// Purpose: Panorama specific video player code +//=============================================================================// + +#ifndef PANORAMA_VIDEO_PLAYER_H +#define PANORAMA_VIDEO_PLAYER_H + +#ifdef _WIN32 +#pragma once +#endif + +#include "tier1/smartptr.h" +#include "../common/video/ivideoplayer.h" +#include "panorama/controls/panelptr.h" + +namespace panorama +{ + +class IUIRenderEngine; +class IUIDoubleBufferedYUV420Texture; +class CPanoramaVideoPlayer; + + +//----------------------------------------------------------------------------- +// Purpose: Renders video frames from video player for tenfoot +//----------------------------------------------------------------------------- +class CVideoPlayerVideoRenderer : public IVideoPlayerVideoCallback +{ +public: + CVideoPlayerVideoRenderer( IUIRenderEngine *pSurface ); + virtual ~CVideoPlayerVideoRenderer(); + + uint32 GetTextureID() { return m_unTextureID; } + uint32 GetTextureWidth(); + uint32 GetTextureHeight(); + + // IVideoPlayerVideoCallback + virtual bool BPresentYUV420Texture( uint nWidth, uint nHeight, void *pPlaneY, void *pPlaneU, void *pPlaneV, uint unStrideY, uint unStrideU, uint unStrideV ) OVERRIDE; + +#ifdef DBGFLAG_VALIDATE + void Validate( CValidator &validator, const char *pchName ); +#endif + +private: + CInterlockedUInt m_unTextureID; + + // used by video threads + IUIRenderEngine *m_pSurface; + IUIDoubleBufferedYUV420Texture *m_pYUV420DoubleBufferedTexture; +}; + + +//----------------------------------------------------------------------------- +// Purpose: Renders audio frames from video player for tenfoot +//----------------------------------------------------------------------------- +class CVideoPlayerAudioRenderer : public IVideoPlayerAudioCallback +{ +public: + CVideoPlayerAudioRenderer(); + virtual ~CVideoPlayerAudioRenderer(); + + void SetPlaybackVolume( float flVolume ); + void MarkShuttingDown(); + void Shutdown(); + + // IVideoPlayerAudioCallback + virtual bool InitAudioOutput( int nSampleRate, int nChannels ) OVERRIDE; + virtual void FreeAudioOutput() OVERRIDE; + virtual bool IsReadyForAudioData() OVERRIDE; + virtual void *GetAudioBuffer() OVERRIDE; + virtual uint32 GetAudioBufferSize() OVERRIDE; + virtual uint32 GetAudioBufferMinSize() OVERRIDE; + virtual void CommitAudioBuffer( uint32 unBytes ) OVERRIDE; + virtual uint32 GetRemainingCommittedAudio() OVERRIDE; + virtual uint32 GetMixedMilliseconds() OVERRIDE; + virtual uint32 GetPlaybackLatency() OVERRIDE; + virtual void Pause() OVERRIDE; + virtual void Resume() OVERRIDE; + +#ifdef DBGFLAG_VALIDATE + void Validate( CValidator &validator, const char *pchName ); +#endif + +private: + bool OnInitAudioMainThread( CVideoPlayerAudioRenderer *pThis, int nSampleRate, int nChannels ); + bool OnFreeAudioMainThread( CVideoPlayerAudioRenderer *pThis ); + + CInterlockedInt m_bShuttingDown; + CThreadEvent m_eventWait; +#ifdef SUPPORTS_AUDIO + IAudioOutputStream *m_pAudioStream; +#endif + float m_flVolume; +}; + + +//----------------------------------------------------------------------------- +// Purpose: Dispatches events to main panorama thread +//----------------------------------------------------------------------------- +typedef CUtlDelegate< void ( EVideoPlayerEvent ) > VideoPlayerEventDelegate_t; +class CVideoPlayerEventDispatcher : public IVideoPlayerEventCallback +{ +public: + CVideoPlayerEventDispatcher( CPanoramaVideoPlayer *pPlayer ); + ~CVideoPlayerEventDispatcher(); + + void RegisterEventListener( IUIPanel *pPanel ); + void UnregisterEventListener( IUIPanel *pPanel ); + + void RegisterEventCallback( VideoPlayerEventDelegate_t del ); + void UnregisterEventCallback( VideoPlayerEventDelegate_t del ); + + bool VideoPlayerEventUIThread( CVideoPlayerEventDispatcher *pDispatcher, EVideoPlayerEvent eEvent ); + + // IVideoPlayerEventCallback + virtual void VideoPlayerEvent( EVideoPlayerEvent eEvent ) OVERRIDE; + +#ifdef DBGFLAG_VALIDATE + void Validate( CValidator &validator, const char *pchName ); +#endif + +private: + void DispatchVideoEvent( EVideoPlayerEvent eEvent, IUIPanel *pTarget ); + + CPanoramaVideoPlayer *m_pPlayer; + CUtlVector< CPanelPtr< IUIPanel > > m_vecListeners; + CUtlVector< VideoPlayerEventDelegate_t > m_vecCallbacks; + double m_flLastRepeatEventDispatch; +}; + + +//----------------------------------------------------------------------------- +// Purpose: Helper to create a tenfoot video player +//----------------------------------------------------------------------------- +class CPanoramaVideoPlayer : public IVideoPlayer, public ::CRefCount +{ +public: + CPanoramaVideoPlayer( IUIPanel *pPanel ); + CPanoramaVideoPlayer( IUIRenderEngine *pSurface ); + virtual ~CPanoramaVideoPlayer(); + + virtual uint32 GetTextureID() { return m_videoCallback.GetTextureID(); } + uint32 GetTextureWidth() { return m_videoCallback.GetTextureWidth(); } + uint32 GetTextureHeight() { return m_videoCallback.GetTextureHeight(); } + + void RegisterEventListener( IUIPanel *pPanel ) { m_eventCallback.RegisterEventListener( pPanel ); } + void UnregisterEventListener( IUIPanel *pPanel ) { m_eventCallback.UnregisterEventListener( pPanel ); } + + void RegisterEventCallback( VideoPlayerEventDelegate_t del ) { m_eventCallback.RegisterEventCallback( del ); } + void UnregisterEventCallback( VideoPlayerEventDelegate_t del ) { m_eventCallback.UnregisterEventCallback( del ); } + + void SetPlaybackVolume( float flVolume ) { m_audioCallback.SetPlaybackVolume( flVolume ); } + + // IVideoPlayer + virtual bool BLoad( const char *pchURL ) OVERRIDE; + virtual bool BLoad( const byte *pubData, uint cubData ) OVERRIDE; + virtual void Play() OVERRIDE { m_pVideoPlayer->Play(); } + virtual void Stop() OVERRIDE; + virtual void Pause() OVERRIDE { m_pVideoPlayer->Pause(); } + virtual void SetPlaybackSpeed( float flPlaybackSpeed ) OVERRIDE { m_pVideoPlayer->SetPlaybackSpeed( flPlaybackSpeed ); } + virtual void Seek( uint unSeekMS ) OVERRIDE { m_pVideoPlayer->Seek( unSeekMS ); } + virtual void SetRepeat( bool bRepeat ) OVERRIDE { m_pVideoPlayer->SetRepeat( bRepeat ); } + virtual void SuggestMaxVeritcalResolution( int nHeight ) OVERRIDE { m_pVideoPlayer->SuggestMaxVeritcalResolution( nHeight ); } + virtual EVideoPlayerPlaybackState GetPlaybackState() OVERRIDE { return m_pVideoPlayer->GetPlaybackState(); } + virtual bool IsStoppedForBuffering() OVERRIDE { return m_pVideoPlayer->IsStoppedForBuffering(); } + virtual float GetPlaybackSpeed() OVERRIDE { return m_pVideoPlayer->GetPlaybackSpeed(); } + virtual uint32 GetDuration() OVERRIDE { return m_pVideoPlayer->GetDuration(); } + virtual uint32 GetCurrentPlaybackTime() OVERRIDE { return m_pVideoPlayer->GetCurrentPlaybackTime(); } + virtual EVideoPlayerPlaybackError GetPlaybackError() OVERRIDE { return m_pVideoPlayer->GetPlaybackError(); } + virtual void GetVideoResolution( int *pnWidth, int *pnHeight ) OVERRIDE { m_pVideoPlayer->GetVideoResolution( pnWidth, pnHeight ); } + virtual int GetVideoDownloadRate() OVERRIDE { return m_pVideoPlayer->GetVideoDownloadRate(); } + virtual int GetVideoRepresentationCount() OVERRIDE { return m_pVideoPlayer->GetVideoRepresentationCount(); } + virtual bool BGetVideoRepresentationInfo( int iRep, int *pnWidth, int *pnHeight ) OVERRIDE { return m_pVideoPlayer->BGetVideoRepresentationInfo( iRep, pnWidth, pnHeight ); } + virtual int GetCurrentVideoRepresentation() OVERRIDE { return m_pVideoPlayer->GetCurrentVideoRepresentation(); } + virtual void ForceVideoRepresentation( int iRep ) OVERRIDE { return m_pVideoPlayer->ForceVideoRepresentation( iRep ); } + virtual void GetVideoSegmentInfo( int *pnCurrent, int *pnTotal ) OVERRIDE { m_pVideoPlayer->GetVideoSegmentInfo( pnCurrent, pnTotal ); } + virtual bool BHasAudioTrack() OVERRIDE { return m_pVideoPlayer->BHasAudioTrack(); } + +#ifdef DBGFLAG_VALIDATE + void Validate( CValidator &validator, const char *pchName ); +#endif + +private: + IVideoPlayer *m_pVideoPlayer; + CVideoPlayerVideoRenderer m_videoCallback; + CVideoPlayerAudioRenderer m_audioCallback; + CVideoPlayerEventDispatcher m_eventCallback; +}; + +typedef CSmartPtr< CPanoramaVideoPlayer > CVideoPlayerPtr; + +} // namespace panorama + + +#endif // PANORAMA_VIDEO_PLAYER_H
\ No newline at end of file |