1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
|