Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Sep 13, 2023
1 parent a5e5a32 commit cf35586
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 59 deletions.
33 changes: 17 additions & 16 deletions lib/tlTimelineUI/AudioClipItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <tlTimeline/RenderUtil.h>
#include <tlTimeline/Util.h>

#include <tlCore/StringFormat.h>

namespace tl
{
namespace timelineui
Expand Down Expand Up @@ -152,7 +154,7 @@ namespace tl
i->second.future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
const auto mesh = i->second.future.get();
_data->waveforms[fileName][i->first] = mesh;
_data->waveforms[_getWaveformKey(i->second.timeRange)] = mesh;
i = p.waveformRequests.erase(i);
_updates |= ui::Update::Draw;
}
Expand Down Expand Up @@ -202,6 +204,12 @@ namespace tl
}
}

std::string AudioClipItem::_getWaveformKey(const otime::TimeRange& timeRange) const
{
TLRENDER_P();
return string::Format("{0}_{1}").arg(p.path.get()).arg(timeRange);
}

void AudioClipItem::_drawWaveforms(
const math::Box2i& drawRect,
const ui::DrawEvent& event)
Expand Down Expand Up @@ -241,7 +249,6 @@ namespace tl

if (_options.waveformWidth > 0 && p.ioInfo && thumbnailSystem)
{
const std::string fileName = p.path.get();
const int w = _sizeHint.w;
for (int x = 0; x < w; x += _options.waveformWidth)
{
Expand Down Expand Up @@ -269,32 +276,26 @@ namespace tl
p.clip,
p.ioInfo->audio.sampleRate);

bool found = false;
const auto i = _data->waveforms.find(fileName);
const auto i = _data->waveforms.find(_getWaveformKey(mediaRange));
if (i != _data->waveforms.end())
{
const auto j = i->second.find(mediaRange.start_time());
if (j != i->second.end())
if (i->second)
{
found = true;
if (j->second)
{
event.render->drawMesh(
*j->second,
box.min,
image::Color4f(1.F, 1.F, 1.F));
}
event.render->drawMesh(
*i->second,
box.min,
image::Color4f(1.F, 1.F, 1.F));
}
}
if (!found && p.ioInfo && p.ioInfo->audio.isValid())
else if (p.ioInfo && p.ioInfo->audio.isValid())
{
const auto j = p.waveformRequests.find(mediaRange.start_time());
if (j == p.waveformRequests.end())
{
p.waveformRequests[mediaRange.start_time()] = thumbnailSystem->getWaveform(
box.getSize(),
p.path,
p.memoryRead,
box.getSize(),
mediaRange);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/tlTimelineUI/AudioClipItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace tl
const ui::DrawEvent&) override;

private:
std::string _getWaveformKey(const otime::TimeRange&) const;

void _drawWaveforms(
const math::Box2i&,
const ui::DrawEvent&);
Expand Down
8 changes: 2 additions & 6 deletions lib/tlTimelineUI/IItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ namespace tl
timeline::Options options;
std::shared_ptr<timeline::ITimeUnitsModel> timeUnitsModel;
std::map<std::string, std::shared_ptr<io::Info> > info;
std::map<
std::string,
std::map<otime::RationalTime, std::shared_ptr<image::Image> > > thumbnails;
std::map<
std::string,
std::map<otime::RationalTime, std::shared_ptr<geom::TriangleMesh2> > > waveforms;
std::map<std::string, std::shared_ptr<image::Image> > thumbnails;
std::map<std::string, std::shared_ptr<geom::TriangleMesh2> > waveforms;
};

//! In/out points display options.
Expand Down
23 changes: 15 additions & 8 deletions lib/tlTimelineUI/TimelineWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ namespace tl
TLRENDER_P();
p.scrollWidget->setScrollPos(math::Vector2i());
p.scale = _getTimelineScale();
if (p.timelineItem)
{
_setItemScale(p.timelineItem, p.scale);
}
_setItemScale();
_updates |= ui::Update::Size;
_updates |= ui::Update::Draw;
}
Expand Down Expand Up @@ -261,6 +258,9 @@ namespace tl
TLRENDER_P();
if (p.itemOptions->setIfChanged(value))
{
p.itemData->info.clear();
p.itemData->thumbnails.clear();
p.itemData->waveforms.clear();
if (p.timelineItem)
{
_setItemOptions(p.timelineItem, value);
Expand Down Expand Up @@ -445,10 +445,7 @@ namespace tl
if (zoomClamped != p.scale)
{
p.scale = zoomClamped;
if (p.timelineItem)
{
_setItemScale(p.timelineItem, p.scale);
}
_setItemScale();
const double s = zoomClamped / zoomPrev;
const math::Vector2i scrollPosNew(
(scrollPos.x + focus.x) * s - focus.x,
Expand Down Expand Up @@ -476,6 +473,16 @@ namespace tl
return out;
}

void TimelineWidget::_setItemScale()
{
TLRENDER_P();
p.itemData->waveforms.clear();
if (p.timelineItem)
{
_setItemScale(p.timelineItem, p.scale);
}
}

void TimelineWidget::_setItemScale(
const std::shared_ptr<IWidget>& widget,
double value)
Expand Down
1 change: 1 addition & 0 deletions lib/tlTimelineUI/TimelineWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ namespace tl

double _getTimelineScale() const;

void _setItemScale();
void _setItemScale(
const std::shared_ptr<IWidget>&,
double);
Expand Down
27 changes: 14 additions & 13 deletions lib/tlTimelineUI/VideoClipItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <tlTimeline/RenderUtil.h>
#include <tlTimeline/Util.h>

#include <tlCore/StringFormat.h>

namespace tl
{
namespace timelineui
Expand Down Expand Up @@ -149,7 +151,7 @@ namespace tl
i->second.future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
const auto image = i->second.future.get();
_data->thumbnails[fileName][i->first] = image;
_data->thumbnails[_getThumbnailKey(i->first)] = image;
i = p.thumbnailRequests.erase(i);
_updates |= ui::Update::Draw;
}
Expand Down Expand Up @@ -199,6 +201,12 @@ namespace tl
}
}

std::string VideoClipItem::_getThumbnailKey(const otime::RationalTime& time) const
{
TLRENDER_P();
return string::Format("{0}_{1}").arg(p.path.get()).arg(time);
}

void VideoClipItem::_drawThumbnails(
const math::Box2i& drawRect,
const ui::DrawEvent& event)
Expand Down Expand Up @@ -240,7 +248,6 @@ namespace tl
0;
if (thumbnailWidth > 0 && thumbnailSystem)
{
const std::string fileName = p.path.get();
const int w = _sizeHint.w;
for (int x = 0; x < w; x += thumbnailWidth)
{
Expand All @@ -263,29 +270,23 @@ namespace tl
p.clip,
p.ioInfo->videoTime.duration().rate());

bool found = false;
const auto i = _data->thumbnails.find(fileName);
const auto i = _data->thumbnails.find(_getThumbnailKey(mediaTime));
if (i != _data->thumbnails.end())
{
const auto j = i->second.find(mediaTime);
if (j != i->second.end())
if (i->second)
{
found = true;
if (j->second)
{
event.render->drawImage(j->second, box);
}
event.render->drawImage(i->second, box);
}
}
if (!found && p.ioInfo && !p.ioInfo->video.empty())
else if (p.ioInfo && !p.ioInfo->video.empty())
{
const auto k = p.thumbnailRequests.find(mediaTime);
if (k == p.thumbnailRequests.end())
{
p.thumbnailRequests[mediaTime] = thumbnailSystem->getThumbnail(
_options.thumbnailHeight,
p.path,
p.memoryRead,
_options.thumbnailHeight,
mediaTime);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/tlTimelineUI/VideoClipItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace tl
const ui::DrawEvent&) override;

private:
std::string _getThumbnailKey(const otime::RationalTime&) const;

void _drawInfo(
const math::Box2i&,
const ui::DrawEvent&);
Expand Down
4 changes: 2 additions & 2 deletions lib/tlUI/FileBrowserButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ namespace tl
{
p.thumbnail.init = false;
p.thumbnail.request = thumbnailSystem->getThumbnail(
p.options.thumbnailHeight,
p.fileInfo.getPath());
p.fileInfo.getPath(),
p.options.thumbnailHeight);
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions lib/tlUI/ThumbnailSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ namespace tl
struct ThumbnailRequest
{
uint64_t id = 0;
int height = 0;
file::Path path;
std::vector<file::MemoryRead> memoryRead;
int height = 0;
otime::RationalTime time = time::invalidTime;
uint16_t layer = 0;
std::promise<std::shared_ptr<image::Image> > promise;
Expand All @@ -56,9 +56,9 @@ namespace tl
struct WaveformRequest
{
uint64_t id = 0;
math::Size2i size;
file::Path path;
std::vector<file::MemoryRead> memoryRead;
math::Size2i size;
otime::TimeRange timeRange = time::invalidTimeRange;
std::promise<std::shared_ptr<geom::TriangleMesh2> > promise;
};
Expand Down Expand Up @@ -191,32 +191,34 @@ namespace tl
}

ThumbnailRequest ThumbnailSystem::getThumbnail(
int height,
const file::Path& path,
int height,
const otime::RationalTime& time,
uint16_t layer)
{
return getThumbnail(height, path, {}, time, layer);
return getThumbnail(path, {}, height, time, layer);
}

ThumbnailRequest ThumbnailSystem::getThumbnail(
int height,
const file::Path& path,
const std::vector<file::MemoryRead>& memoryRead,
int height,
const otime::RationalTime& time,
uint16_t layer)
{
TLRENDER_P();
(p.requestId)++;
auto request = std::make_shared<Private::ThumbnailRequest>();
request->id = p.requestId;
request->height = height;
request->path = path;
request->memoryRead = memoryRead;
request->height = height;
request->time = time;
request->layer = layer;
ThumbnailRequest out;
out.id = p.requestId;
out.height = height;
out.time = time;
out.future = request->promise.get_future();
bool valid = false;
{
Expand All @@ -239,29 +241,31 @@ namespace tl
}

WaveformRequest ThumbnailSystem::getWaveform(
const math::Size2i& size,
const file::Path& path,
const math::Size2i& size,
const otime::TimeRange& range)
{
return getWaveform(size, path, {}, range);
return getWaveform(path, {}, size, range);
}

WaveformRequest ThumbnailSystem::getWaveform(
const math::Size2i& size,
const file::Path& path,
const std::vector<file::MemoryRead>& memoryRead,
const math::Size2i& size,
const otime::TimeRange& timeRange)
{
TLRENDER_P();
(p.requestId)++;
auto request = std::make_shared<Private::WaveformRequest>();
request->id = p.requestId;
request->size = size;
request->path = path;
request->memoryRead = memoryRead;
request->size = size;
request->timeRange = timeRange;
WaveformRequest out;
out.id = p.requestId;
out.size = size;
out.timeRange = timeRange;
out.future = request->promise.get_future();
bool valid = false;
{
Expand Down
Loading

0 comments on commit cf35586

Please sign in to comment.