Back to index

unity  6.0.0
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes
unity::IconLoader::Impl Class Reference
Collaboration diagram for unity::IconLoader::Impl:
Collaboration graph
[legend]

List of all members.

Classes

struct  IconLoaderTask

Public Member Functions

 Impl ()
Handle LoadFromIconName (std::string const &icon_name, unsigned size, IconLoaderCallback slot)
Handle LoadFromGIconString (std::string const &gicon_string, unsigned size, IconLoaderCallback slot)
Handle LoadFromFilename (std::string const &filename, unsigned size, IconLoaderCallback slot)
Handle LoadFromURI (std::string const &uri, unsigned size, IconLoaderCallback slot)
void DisconnectHandle (Handle handle)

Static Public Member Functions

typedef int Handle

Private Types

enum  IconLoaderRequestType { REQUEST_TYPE_ICON_NAME = 0, REQUEST_TYPE_GICON_STRING, REQUEST_TYPE_URI }

Private Member Functions

Handle ReturnCachedOrQueue (std::string const &data, unsigned size, IconLoaderCallback slot, IconLoaderRequestType type)
Handle QueueTask (std::string const &key, std::string const &data, unsigned size, IconLoaderCallback slot, IconLoaderRequestType type)
std::string Hash (std::string const &data, unsigned size)
bool CacheLookup (std::string const &key, std::string const &data, unsigned size, IconLoaderCallback slot)
bool Iteration ()
bool CoalesceTasksCb ()

Private Attributes

std::map< std::string,
glib::Object< GdkPixbuf > > 
cache_
std::map< std::string,
IconLoaderTask::Ptr
queued_tasks_
std::queue< IconLoaderTask::Ptrtasks_
std::map< Handle,
IconLoaderTask::Ptr
task_map_
std::vector< IconLoaderTask * > finished_tasks_
bool no_load_
GtkIconTheme * theme_
Handle handle_counter_
glib::Source::UniquePtr idle_
glib::Source::UniquePtr coalesce_timeout_
glib::Signal< void,
GtkIconTheme * > 
theme_changed_signal_

Detailed Description

Definition at line 40 of file IconLoader.cpp.


Member Typedef Documentation

typedef int unity::IconLoader::Impl::Handle [static]

Definition at line 45 of file IconLoader.cpp.


Member Enumeration Documentation

Enumerator:
REQUEST_TYPE_ICON_NAME 
REQUEST_TYPE_GICON_STRING 
REQUEST_TYPE_URI 

Definition at line 69 of file IconLoader.cpp.


Constructor & Destructor Documentation

Definition at line 355 of file IconLoader.cpp.

  : // Option to disable loading, if you're testing performance of other things
    no_load_(::getenv("UNITY_ICON_LOADER_DISABLE"))
  , theme_(::gtk_icon_theme_get_default())
  , handle_counter_(0)
{
  theme_changed_signal_.Connect(theme_, "changed", [&] (GtkIconTheme*) {
    /* Since the theme has been changed we can clear the cache, however we
     * could include two improvements here:
     *  1) clear only the themed icons in cache
     *  2) make the clients of this class to update their icons forcing them
     *     to reload the pixbufs and erase the cached textures, to make this
     *     apply immediately. */
    cache_.clear();
  });
}

Here is the call graph for this function:


Member Function Documentation

bool unity::IconLoader::Impl::CacheLookup ( std::string const &  key,
std::string const &  data,
unsigned  size,
IconLoaderCallback  slot 
) [private]

Definition at line 498 of file IconLoader.cpp.

{
  auto iter = cache_.find(key);
  bool found = iter != cache_.end();

  if (found)
  {
    glib::Object<GdkPixbuf> const& pixbuf = iter->second;
    slot(data, size, pixbuf);
  }

  return found;
}

Here is the caller graph for this function:

Definition at line 515 of file IconLoader.cpp.

{
  for (auto task : finished_tasks_)
  {
    task->InvokeSlot();

    // this was all async, we need to erase the task from the task_map
    task_map_.erase(task->handle);
    queued_tasks_.erase(task->key);
  }

  finished_tasks_.clear();
  coalesce_timeout_.reset();

  return false;
}

Here is the caller graph for this function:

Definition at line 421 of file IconLoader.cpp.

{
  auto iter = task_map_.find(handle);

  if (iter != task_map_.end())
  {
    iter->second->slot.disconnect();
  }
}
std::string unity::IconLoader::Impl::Hash ( std::string const &  data,
unsigned  size 
) [private]

Definition at line 491 of file IconLoader.cpp.

{
  std::ostringstream sout;
  sout << data << ":" << size;
  return sout.str();
}

Definition at line 532 of file IconLoader.cpp.

{
  static const int MAX_MICRO_SECS = 1000;
  util::Timer timer;

  bool queue_empty = tasks_.empty();

  // always do at least one iteration if the queue isn't empty
  while (!queue_empty)
  {
    IconLoaderTask::Ptr const& task = tasks_.front();

    if (task->Process())
    {
      task_map_.erase(task->handle);
      queued_tasks_.erase(task->key);
    }

    tasks_.pop();
    queue_empty = tasks_.empty();

    if (timer.ElapsedMicroSeconds() >= MAX_MICRO_SECS) break;
  }

  LOG_DEBUG(logger) << "Iteration done, queue size now at " << tasks_.size();

  if (queue_empty)
  {
    if (task_map_.empty())
      handle_counter_ = 0;

    idle_.reset();
  }

  return !queue_empty;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int unity::IconLoader::Impl::LoadFromFilename ( std::string const &  filename,
unsigned  size,
IconLoaderCallback  slot 
)

Definition at line 398 of file IconLoader.cpp.

{
  if (no_load_ || filename.empty() || size < MIN_ICON_SIZE)
    return 0;

  glib::Object<GFile> file(::g_file_new_for_path(filename.c_str()));
  glib::String uri(::g_file_get_uri(file));

  return LoadFromURI(uri.Str(), size, slot);
}

Here is the call graph for this function:

int unity::IconLoader::Impl::LoadFromGIconString ( std::string const &  gicon_string,
unsigned  size,
IconLoaderCallback  slot 
)

Definition at line 388 of file IconLoader.cpp.

{
  if (no_load_ || gicon_string.empty() || size < MIN_ICON_SIZE)
    return 0;

  return ReturnCachedOrQueue(gicon_string, size, slot, REQUEST_TYPE_GICON_STRING);
}
int unity::IconLoader::Impl::LoadFromIconName ( std::string const &  icon_name,
unsigned  size,
IconLoaderCallback  slot 
)

Definition at line 372 of file IconLoader.cpp.

{
  if (no_load_ || icon_name.empty() || size < MIN_ICON_SIZE)
    return 0;

  // We need to check this because of legacy desktop files
  if (icon_name[0] == '/')
  {
    return LoadFromFilename(icon_name, size, slot);
  }

  return ReturnCachedOrQueue(icon_name, size, slot, REQUEST_TYPE_ICON_NAME);
}

Here is the call graph for this function:

int unity::IconLoader::Impl::LoadFromURI ( std::string const &  uri,
unsigned  size,
IconLoaderCallback  slot 
)

Definition at line 411 of file IconLoader.cpp.

{
  if (no_load_ || uri.empty() || size < MIN_ICON_SIZE)
    return 0;

  return ReturnCachedOrQueue(uri, size, slot, REQUEST_TYPE_URI);
}
int unity::IconLoader::Impl::QueueTask ( std::string const &  key,
std::string const &  data,
unsigned  size,
IconLoaderCallback  slot,
IconLoaderRequestType  type 
) [private]

Definition at line 451 of file IconLoader.cpp.

{
  auto task = std::make_shared<IconLoaderTask>(type, data, size, key, slot, ++handle_counter_, this);
  auto iter = queued_tasks_.find(key);

  if (iter != queued_tasks_.end())
  {
    IconLoaderTask::Ptr const& running_task = iter->second;
    running_task->shadow_tasks.push_back(task);
    // do NOT push the task into the tasks queue,
    // the parent task (which is in the queue) will handle it
    task_map_[task->handle] = task;

    LOG_DEBUG(logger) << "Appending shadow task  " << data
                      << ", queue size now at " << tasks_.size();

    return task->handle;
  }
  else
  {
    queued_tasks_[key] = task;
  }

  tasks_.push(task);
  task_map_[task->handle] = task;

  LOG_DEBUG(logger) << "Pushing task  " << data << " at size " << size
                    << ", queue size now at " << tasks_.size();

  if (!idle_)
  {
    idle_.reset(new glib::Idle(sigc::mem_fun(this, &Impl::Iteration), glib::Source::Priority::LOW));
  }
  return task->handle;
}

Here is the call graph for this function:

int unity::IconLoader::Impl::ReturnCachedOrQueue ( std::string const &  data,
unsigned  size,
IconLoaderCallback  slot,
IconLoaderRequestType  type 
) [private]

Definition at line 435 of file IconLoader.cpp.

{
  Handle result = 0;
  std::string key(Hash(data, size));

  if (!CacheLookup(key, data, size, slot))
  {
    result = QueueTask(key, data, size, slot, type);
  }
  return result;
}

Member Data Documentation

std::map<std::string, glib::Object<GdkPixbuf> > unity::IconLoader::Impl::cache_ [private]

Definition at line 340 of file IconLoader.cpp.

Definition at line 350 of file IconLoader.cpp.

Definition at line 344 of file IconLoader.cpp.

Definition at line 348 of file IconLoader.cpp.

Definition at line 349 of file IconLoader.cpp.

Definition at line 346 of file IconLoader.cpp.

Definition at line 341 of file IconLoader.cpp.

Definition at line 343 of file IconLoader.cpp.

Definition at line 342 of file IconLoader.cpp.

GtkIconTheme* unity::IconLoader::Impl::theme_ [private]

Definition at line 347 of file IconLoader.cpp.

Definition at line 351 of file IconLoader.cpp.


The documentation for this class was generated from the following file: