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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "GUIImage.h"
#include "GUIMessage.h"
#include "utils/log.h"
#include <cassert>
using namespace KODI::GUILIB;
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(CGUITexture::CreateTexture(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_image(left.m_image),
m_info(left.m_info),
m_texture(left.m_texture->Clone()),
m_fadingTextures(),
m_currentTexture(),
m_currentFallback()
{
m_crossFadeTime = left.m_crossFadeTime;
// defaults
m_currentFadeTime = 0;
m_lastRenderTime = 0;
ControlType = GUICONTROL_IMAGE;
m_bDynamicResourceAlloc=false;
}
CGUIImage::~CGUIImage(void) = default;
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::UpdateDiffuseColor(const CGUIListItem* item)
{
if (m_texture->SetDiffuseColor(m_diffuseColor, item))
{
MarkDirtyRegion();
}
}
void CGUIImage::UpdateInfo(const CGUIListItem *item)
{
// The texture may also depend on info conditions. Update the diffuse color in that case.
if (m_texture->GetDiffuseColor().HasInfo())
UpdateDiffuseColor(item);
if (m_info.IsConstant())
return; // nothing to do
// don't allow image to change while animating out
if (HasProcessed() && IsAnimating(ANIM_TYPE_HIDDEN) && !IsVisibleFromSkin())
return;
if (item)
SetFileName(m_info.GetItemLabel(item, true, &m_currentFallback));
else
SetFileName(m_info.GetLabel(m_parentID, true, &m_currentFallback));
}
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::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
{
// check whether our image failed to allocate, and if so drop back to the fallback image
if (m_texture->FailedToAlloc() && m_texture->GetFileName() != m_info.GetFallback())
{
if (!m_currentFallback.empty() && m_texture->GetFileName() != m_currentFallback)
m_texture->SetFileName(m_currentFallback);
else
m_texture->SetFileName(m_info.GetFallback());
}
if (m_crossFadeTime)
{
// make sure our texture has started allocating
if (m_texture->AllocResources())
MarkDirtyRegion();
// compute the frame time
unsigned int frameTime = 0;
if (m_lastRenderTime)
frameTime = currentTime - m_lastRenderTime;
if (!frameTime)
frameTime = (unsigned int)(1000 / CServiceBroker::GetWinSystem()->GetGfxContext().GetFPS());
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 (std::vector<CFadingTexture *>::iterator i = m_fadingTextures.begin(); i != m_fadingTextures.end() - 1;)
{
if (!ProcessFading(*i, frameTime, currentTime))
i = m_fadingTextures.erase(i);
else
++i;
}
if (m_texture->ReadyToRender() || m_texture->GetFileName().empty())
{ // fade out the last one as well
if (!ProcessFading(m_fadingTextures[m_fadingTextures.size() - 1], frameTime, currentTime))
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;
if (texture->m_texture->SetAlpha(GetFadeLevel(texture->m_fadeTime)))
MarkDirtyRegion();
if (texture->m_texture->SetDiffuseColor(m_diffuseColor))
MarkDirtyRegion();
if (texture->m_texture->Process(currentTime))
MarkDirtyRegion();
}
}
if (m_texture->ReadyToRender() || m_texture->GetFileName().empty())
{ // 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;
}
if (m_texture->SetAlpha(GetFadeLevel(m_currentFadeTime)))
MarkDirtyRegion();
}
if (!m_texture->GetDiffuseColor().HasInfo())
UpdateDiffuseColor(nullptr);
if (m_texture->Process(currentTime))
MarkDirtyRegion();
CGUIControl::Process(currentTime, dirtyregions);
}
void CGUIImage::Render()
{
if (!IsVisible()) return;
for (auto& itr : m_fadingTextures)
itr->m_texture->Render();
m_texture->Render();
CGUIControl::Render();
}
bool CGUIImage::ProcessFading(CGUIImage::CFadingTexture *texture, unsigned int frameTime, unsigned int currentTime)
{
assert(texture);
if (texture->m_fadeTime <= frameTime)
{ // time to kill off the texture
MarkDirtyRegion();
delete texture;
return false;
}
// render this texture
texture->m_fadeTime -= frameTime;
if (texture->m_texture->SetAlpha(GetFadeLevel(texture->m_fadeTime)))
MarkDirtyRegion();
if (texture->m_texture->SetDiffuseColor(m_diffuseColor))
MarkDirtyRegion();
if (texture->m_texture->Process(currentTime))
MarkDirtyRegion();
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;
}
else if (message.GetMessage() == GUI_MSG_SET_FILENAME)
{
SetFileName(message.GetLabel());
return true;
}
else if (message.GetMessage() == GUI_MSG_GET_FILENAME)
{
message.SetLabel(GetFileName());
return true;
}
return CGUIControl::OnMessage(message);
}
void CGUIImage::AllocResources()
{
if (m_texture->GetFileName().empty())
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();
m_currentTexture.clear();
if (!m_info.IsConstant()) // constant textures never change
m_texture->SetFileName("");
}
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_hasProcessed = 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();
}
CRect CGUIImage::CalcRenderRegion() const
{
CRect region = m_texture->GetRenderRect();
for (const auto& itr : m_fadingTextures)
region.Union(itr->m_texture->GetRenderRect());
return CGUIControl::CalcRenderRegion().Intersect(region);
}
const std::string &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().empty())
m_crossFadeTime = 1;
}
void CGUIImage::SetFileName(const std::string& strFileName, bool setConstant, const bool useCache)
{
if (setConstant)
m_info.SetLabel(strFileName, "", GetParentID());
// Set whether or not to use cache
m_texture->SetUseCache(useCache);
if (m_crossFadeTime)
{
// set filename on the next texture
if (m_currentTexture == strFileName)
return; // nothing to do - we already have this image
if (m_texture->ReadyToRender() || m_texture->GetFileName().empty())
{ // save the current image
m_fadingTextures.push_back(new CFadingTexture(m_texture.get(), m_currentFadeTime));
MarkDirtyRegion();
}
m_currentFadeTime = 0;
}
if (m_currentTexture != 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;
if (m_texture->SetFileName(m_currentTexture))
MarkDirtyRegion();
}
}
#ifdef _DEBUG
void CGUIImage::DumpTextureUse()
{
if (m_texture->IsAllocated())
{
if (GetID())
CLog::Log(LOGDEBUG, "Image control {} using texture {}", GetID(), m_texture->GetFileName());
else
CLog::Log(LOGDEBUG, "Using texture {}", m_texture->GetFileName());
}
}
#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 GUIINFO::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);
}
std::string CGUIImage::GetDescription(void) const
{
return GetFileName();
}
|