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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "GUIImage.h"
#include "TextureManager.h"
#include "utils/log.h"
#include "utils/TimeUtils.h"
using namespace std;
CGUIImage::CGUIImage(int parentID, int controlID, float posX, float posY, float width, float height, const CTextureInfo& texture)
: CGUIControl(parentID, controlID, posX, posY, width, height)
, m_texture(posX, posY, width, height, texture)
{
m_crossFadeTime = 0;
m_currentFadeTime = 0;
m_lastRenderTime = 0;
ControlType = GUICONTROL_IMAGE;
m_bDynamicResourceAlloc=false;
}
CGUIImage::CGUIImage(const CGUIImage &left)
: CGUIControl(left), m_texture(left.m_texture)
{
m_info = left.m_info;
m_crossFadeTime = left.m_crossFadeTime;
// defaults
m_currentFadeTime = 0;
m_lastRenderTime = 0;
ControlType = GUICONTROL_IMAGE;
m_bDynamicResourceAlloc=false;
}
CGUIImage::~CGUIImage(void)
{
}
void CGUIImage::UpdateVisibility(const CGUIListItem *item)
{
CGUIControl::UpdateVisibility(item);
// now that we've checked for conditional info, we can
// check for allocation
AllocateOnDemand();
}
void CGUIImage::UpdateInfo(const CGUIListItem *item)
{
if (m_info.IsConstant())
return; // nothing to do
// don't allow image to change while animating out
if (HasRendered() && IsAnimating(ANIM_TYPE_HIDDEN) && !IsVisibleFromSkin())
return;
if (item)
SetFileName(m_info.GetItemLabel(item, true));
else
SetFileName(m_info.GetLabel(m_parentID, true));
}
void CGUIImage::AllocateOnDemand()
{
// if we're hidden, we can free our resources and return
if (!IsVisible() && m_visible != DELAYED)
{
if (m_bDynamicResourceAlloc && m_texture.IsAllocated())
FreeResourcesButNotAnims();
return;
}
// either visible or delayed - we need the resources allocated in either case
if (!m_texture.IsAllocated())
AllocResources();
}
void CGUIImage::Render()
{
if (!IsVisible()) return;
// check whether our image failed to allocate, and if so drop back to the fallback image
if (m_texture.FailedToAlloc() && !m_texture.GetFileName().Equals(m_info.GetFallback()))
m_texture.SetFileName(m_info.GetFallback());
if (m_crossFadeTime)
{
// make sure our texture has started allocating
m_texture.AllocResources();
// compute the frame time
unsigned int frameTime = 0;
unsigned int currentTime = CTimeUtils::GetFrameTime();
if (m_lastRenderTime)
frameTime = currentTime - m_lastRenderTime;
m_lastRenderTime = currentTime;
if (m_fadingTextures.size()) // have some fading images
{ // anything other than the last old texture needs to be faded out as per usual
for (vector<CFadingTexture *>::iterator i = m_fadingTextures.begin(); i != m_fadingTextures.end() - 1;)
{
if (!RenderFading(*i, frameTime))
i = m_fadingTextures.erase(i);
else
i++;
}
if (m_texture.ReadyToRender() || m_texture.GetFileName().IsEmpty())
{ // fade out the last one as well
if (!RenderFading(m_fadingTextures[m_fadingTextures.size() - 1], frameTime))
m_fadingTextures.erase(m_fadingTextures.end() - 1);
}
else
{ // keep the last one fading in
CFadingTexture *texture = m_fadingTextures[m_fadingTextures.size() - 1];
texture->m_fadeTime += frameTime;
if (texture->m_fadeTime > m_crossFadeTime)
texture->m_fadeTime = m_crossFadeTime;
texture->m_texture->SetAlpha(GetFadeLevel(texture->m_fadeTime));
texture->m_texture->SetDiffuseColor(m_diffuseColor);
texture->m_texture->Render();
}
}
if (m_texture.ReadyToRender() || m_texture.GetFileName().IsEmpty())
{ // fade the new one in
m_currentFadeTime += frameTime;
if (m_currentFadeTime > m_crossFadeTime || frameTime == 0) // for if we allocate straight away on creation
m_currentFadeTime = m_crossFadeTime;
}
m_texture.SetAlpha(GetFadeLevel(m_currentFadeTime));
}
m_texture.SetDiffuseColor(m_diffuseColor);
m_texture.Render();
CGUIControl::Render();
}
bool CGUIImage::RenderFading(CGUIImage::CFadingTexture *texture, unsigned int frameTime)
{
assert(texture);
if (texture->m_fadeTime <= frameTime)
{ // time to kill off the texture
delete texture;
return false;
}
// render this texture
texture->m_fadeTime -= frameTime;
texture->m_texture->SetAlpha(GetFadeLevel(texture->m_fadeTime));
texture->m_texture->SetDiffuseColor(m_diffuseColor);
texture->m_texture->Render();
return true;
}
bool CGUIImage::OnAction(const CAction &action)
{
return false;
}
bool CGUIImage::OnMessage(CGUIMessage& message)
{
if (message.GetMessage() == GUI_MSG_REFRESH_THUMBS)
{
if (!m_info.IsConstant())
FreeTextures(true); // true as we want to free the texture immediately
return true;
}
return CGUIControl::OnMessage(message);
}
void CGUIImage::AllocResources()
{
if (m_texture.GetFileName().IsEmpty())
return;
CGUIControl::AllocResources();
m_texture.AllocResources();
}
void CGUIImage::FreeTextures(bool immediately /* = false */)
{
m_texture.FreeResources(immediately);
for (unsigned int i = 0; i < m_fadingTextures.size(); i++)
delete m_fadingTextures[i];
m_fadingTextures.clear();
}
void CGUIImage::FreeResources(bool immediately)
{
FreeTextures(immediately);
CGUIControl::FreeResources(immediately);
}
void CGUIImage::SetInvalid()
{
m_texture.SetInvalid();
CGUIControl::SetInvalid();
}
// WORKAROUND - we are currently resetting all animations when this is called, which shouldn't be the case
// see CGUIControl::FreeResources() - this needs remedying.
void CGUIImage::FreeResourcesButNotAnims()
{
FreeTextures();
m_bAllocated=false;
m_hasRendered = false;
}
void CGUIImage::DynamicResourceAlloc(bool bOnOff)
{
m_bDynamicResourceAlloc = bOnOff;
m_texture.DynamicResourceAlloc(bOnOff);
CGUIControl::DynamicResourceAlloc(bOnOff);
}
bool CGUIImage::CanFocus() const
{
return false;
}
float CGUIImage::GetTextureWidth() const
{
return m_texture.GetTextureWidth();
}
float CGUIImage::GetTextureHeight() const
{
return m_texture.GetTextureHeight();
}
const CStdString &CGUIImage::GetFileName() const
{
return m_texture.GetFileName();
}
void CGUIImage::SetAspectRatio(const CAspectRatio &aspect)
{
m_texture.SetAspectRatio(aspect);
}
void CGUIImage::SetCrossFade(unsigned int time)
{
m_crossFadeTime = time;
if (!m_crossFadeTime && m_texture.IsLazyLoaded() && !m_info.GetFallback().IsEmpty())
m_crossFadeTime = 1;
}
void CGUIImage::SetFileName(const CStdString& strFileName, bool setConstant)
{
if (setConstant)
m_info.SetLabel(strFileName, "");
if (m_crossFadeTime)
{
// set filename on the next texture
if (m_currentTexture.Equals(strFileName))
return; // nothing to do - we already have this image
if (m_texture.ReadyToRender() || m_texture.GetFileName().IsEmpty())
{ // save the current image
m_fadingTextures.push_back(new CFadingTexture(m_texture, m_currentFadeTime));
}
m_currentFadeTime = 0;
}
if (!m_currentTexture.Equals(strFileName))
{ // texture is changing - attempt to load it, and save the name in m_currentTexture.
// we'll check whether it loaded or not in Render()
m_currentTexture = strFileName;
m_texture.SetFileName(m_currentTexture);
}
}
#ifdef _DEBUG
void CGUIImage::DumpTextureUse()
{
if (m_texture.IsAllocated())
{
if (GetID())
CLog::Log(LOGDEBUG, "Image control %u using texture %s",
GetID(), m_texture.GetFileName().c_str());
else
CLog::Log(LOGDEBUG, "Using texture %s", m_texture.GetFileName().c_str());
}
}
#endif
void CGUIImage::SetWidth(float width)
{
m_texture.SetWidth(width);
CGUIControl::SetWidth(m_texture.GetWidth());
}
void CGUIImage::SetHeight(float height)
{
m_texture.SetHeight(height);
CGUIControl::SetHeight(m_texture.GetHeight());
}
void CGUIImage::SetPosition(float posX, float posY)
{
m_texture.SetPosition(posX, posY);
CGUIControl::SetPosition(posX, posY);
}
void CGUIImage::SetInfo(const CGUIInfoLabel &info)
{
m_info = info;
// a constant image never needs updating
if (m_info.IsConstant())
m_texture.SetFileName(m_info.GetLabel(0));
}
unsigned char CGUIImage::GetFadeLevel(unsigned int time) const
{
float amount = (float)time / m_crossFadeTime;
// we want a semi-transparent image, so we need to use a more complicated
// fade technique. Assuming a black background (not generally true, but still...)
// we have
// b(t) = [a - b(1-t)*a] / a*(1-b(1-t)*a),
// where a = alpha, and b(t):[0,1] -> [0,1] is the blend function.
// solving, we get
// b(t) = [1 - (1-a)^t] / a
const float alpha = 0.7f;
return (unsigned char)(255.0f * (1 - pow(1-alpha, amount))/alpha);
}
CStdString CGUIImage::GetDescription(void) const
{
return GetFileName();
}
|