aboutsummaryrefslogtreecommitdiff
path: root/src/animationloader.cpp
blob: 074c1a94ea9ef7114d05004dfbb18701cd5fd3ea (plain)
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
#include "animationloader.h"

#include <QMutexLocker>
#include <QtConcurrent/QtConcurrent>

namespace kal
{
AnimationLoader::AnimationLoader(QThreadPool *threadPool)
    : m_thread_pool(threadPool)
{}

AnimationLoader::~AnimationLoader()
{
  stopLoading();
}

QString AnimationLoader::loadedFileName() const
{
  return m_file_name;
}

void AnimationLoader::load(const QString &fileName)
{
  if (m_file_name == fileName)
  {
    return;
  }
  stopLoading();
  m_file_name = fileName;
  QImageReader *reader = new QImageReader;
  reader->setFileName(fileName);
  m_size = reader->size();
  m_frames.clear();
  m_frame_count = reader->imageCount();
  m_loop_count = reader->loopCount();
  m_exit_task = false;
  m_task = QtConcurrent::run(m_thread_pool, [this, reader]() { populateVector(reader); });
}

void AnimationLoader::stopLoading()
{
  m_exit_task = true;
  if (m_task.isRunning())
  {
    m_task.waitForFinished();
  }
}

QSize AnimationLoader::size()
{
  return m_size;
}

int AnimationLoader::frameCount()
{
  return m_frame_count;
}

AnimationFrame AnimationLoader::frame(int frameNumber)
{
  if (m_frame_count <= 0)
  {
    return AnimationFrame();
  }

  m_task_lock.lock();
  while (m_frames.size() < frameNumber + 1)
  {
#ifdef DEBUG_MOVIE
    qDebug().noquote() << "Waiting for frame" << frameNumber << QString("(file: %1, frame count: %2)").arg(m_file_name).arg(m_frame_count);
#endif
    m_task_signal.wait(&m_task_lock);
  }

  AnimationFrame frame = std::as_const(m_frames)[frameNumber];
  m_task_lock.unlock();

  return frame;
}

int AnimationLoader::loopCount()
{
  return m_loop_count;
}

void AnimationLoader::populateVector(QImageReader *reader)
{
  int loaded_frame_count = 0;
  int frame_count = reader->imageCount();
  while (!m_exit_task && loaded_frame_count < frame_count)
  {
    {
      QMutexLocker locker(&m_task_lock);
      AnimationFrame frame;
      frame.texture = QPixmap::fromImage(reader->read());
      frame.duration = reader->nextImageDelay();
      m_frames.append(frame);
      ++loaded_frame_count;
    }
    m_task_signal.wakeAll();
  }

  delete reader;
}
} // namespace kal