30#include <wx/progdlg.h>
34#if defined(__ANDROID__)
36#elif defined(__WXQT__) || defined(__WXGTK__)
40#include <wx/datetime.h>
42#include <wx/filename.h>
46#include <wx/progdlg.h>
47#include <wx/stopwatch.h>
52#include <wx/listimpl.cpp>
53#include <wx/arrimpl.cpp>
55#include "mipmap/mipmap.h"
81#ifndef GL_ETC1_RGB8_OES
82#define GL_ETC1_RGB8_OES 0x8D64
85using JobList = std::list<JobTicket *>;
91static bool bthread_debug;
93wxString CompressedCachePath(wxString path) {
95 int colon = path.find(
':', 0);
96 if (colon != wxNOT_FOUND) path.Remove(colon, 1);
100 wxChar separator = wxFileName::GetPathSeparator();
101 for (
unsigned int pos = 0; pos < path.size(); pos = path.find(separator, pos))
102 path.replace(pos, 1,
"!");
106 wxCharBuffer buf = path.ToUTF8();
107 unsigned char sha1_out[20];
108 sha1((
unsigned char *)buf.data(), strlen(buf.data()), sha1_out);
111 for (
unsigned int i = 0; i < 20; i++) {
113 s.Printf(
"%02X", sha1_out[i]);
117 return g_Platform->GetPrivateDataDir() + separator +
"raster_texture_cache" +
122OCPN_CompressProgressEvent::OCPN_CompressProgressEvent(wxEventType commandType,
int id)
123:wxEvent(id, commandType)
127OCPN_CompressProgressEvent::~OCPN_CompressProgressEvent()
131wxEvent* OCPN_CompressProgressEvent::Clone()
const
133 OCPN_CompressProgressEvent *newevent=
new OCPN_CompressProgressEvent(*
this);
134 newevent->m_string=this->m_string;
135 newevent->count=this->count;
136 newevent->thread=this->thread;
141static double chart_dist(
int index) {
147 if (cte.GetBBox().Contains(
gLat,
gLon))
152 clon = (cte.GetLonMax() + cte.GetLonMin()) / 2;
153 d = DistGreatCircle(cte.GetLatMax(), clon,
gLat,
gLon);
154 t = DistGreatCircle(cte.GetLatMin(), clon,
gLat,
gLon);
157 clat = (cte.GetLatMax() + cte.GetLatMin()) / 2;
158 t = DistGreatCircle(clat, cte.GetLonMin(),
gLat,
gLon);
160 t = DistGreatCircle(clat, cte.GetLonMax(),
gLat,
gLon);
166WX_DEFINE_SORTED_ARRAY_INT(
int, MySortedArrayInt);
167int CompareInts(
int n1,
int n2) {
168 double d1 = chart_dist(n1);
169 double d2 = chart_dist(n2);
170 return (
int)(d1 - d2);
173static MySortedArrayInt idx_sorted_by_distance(CompareInts);
184JobTicket::JobTicket() {
185 for (
int i = 0; i < 10; i++) {
186 compcomp_size_array[i] = 0;
187 comp_bits_array[i] = NULL;
188 compcomp_bits_array[i] = NULL;
198void FlattenColorsForCompression(
unsigned char *data,
int dim,
bool swap_colors=
true)
203 for(
int i = 0; i<dim*dim; i++) {
205 unsigned char t = data[off + 0];
206 data[off + 0] = data[off + 2] & 0xfc;
207 data[off + 1] &= 0xf8;
208 data[off + 2] = t & 0xfc;
212 for(
int i = 0; i<dim*dim; i++) {
214 data[off + 0] &= 0xfc;
215 data[off + 1] &= 0xf8;
216 data[off + 2] &= 0xfc;
222static void CompressDataETC(
const unsigned char *data,
int dim,
int size,
223 unsigned char *tex_data,
volatile bool &b_abort) {
224 wxASSERT(dim * dim == 2 * size || (dim < 4 && size == 8));
225 uint64_t *tex_data64 = (uint64_t *)tex_data;
227 int mbrow = wxMin(4, dim), mbcol = wxMin(4, dim);
228 uint8_t
block[48] = {};
229 for (
int row = 0; row < dim; row += 4) {
230 for (
int col = 0; col < dim; col += 4) {
231 for (
int brow = 0; brow < mbrow; brow++)
232 for (
int bcol = 0; bcol < mbcol; bcol++)
233 memcpy(
block + (bcol * 4 + brow) * 3,
234 data + ((row + brow) * dim + col + bcol) * 3, 3);
236 extern uint64_t ProcessRGB(
const uint8_t *src);
237 *tex_data64++ = ProcessRGB(
block);
243static bool CompressUsingGPU(
const unsigned char *data,
int dim,
int size,
244 unsigned char *tex_data,
int level,
bool inplace) {
245#ifndef USE_ANDROID_GLES2
249 glGenTextures(1, &comp_tex);
250 glBindTexture(GL_TEXTURE_2D, comp_tex);
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
256 glTexImage2D(GL_TEXTURE_2D, level,
g_raster_format, dim, dim, 0, GL_RGB,
257 GL_UNSIGNED_BYTE, data);
260 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB,
263 if (compressed == GL_TRUE) {
265 GLint compressedSize;
266 glGetTexLevelParameteriv(GL_TEXTURE_2D, level,
267 GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &compressedSize);
269 if (compressedSize != size)
return false;
272 glGetCompressedTexImage(GL_TEXTURE_2D, level, tex_data);
275 if (!inplace) glDeleteTextures(1, &comp_tex);
284 wxString &chart_path) {
287 ptd->map_array[0] = 0;
292 (
unsigned char *)calloc(ncrect.width * ncrect.height * 4, 1);
300 unsigned char *t_buf =
301 (
unsigned char *)malloc(ncrect.width * ncrect.height * 4);
302 pBSBChart->GetChartBits(ncrect, t_buf, 1);
305 ptd->map_array[0] = t_buf;
308 (
unsigned char *)calloc(ncrect.width * ncrect.height * 4, 1);
314 wxString chart_path,
int level) {
317 if (ptd->map_array[level])
return;
321 for (first_level = level; first_level; first_level--)
322 if (ptd->map_array[first_level - 1])
break;
326 GetLevel0Map(ptd, rect, chart_path);
330 int dim = g_GLOptions.m_iTextureDimension;
331 for (
int i = 0; i <= level; i++) {
332 if (i >= first_level) {
333 ptd->map_array[i] = (
unsigned char *)malloc(dim * dim * 3);
334 MipMap_24(2 * dim, 2 * dim, ptd->map_array[i - 1], ptd->map_array[i]);
340int TextureDim(
int level) {
341 int dim = g_GLOptions.m_iTextureDimension;
342 for (
int i = 0; i < level; i++) dim /= 2;
346int TextureTileSize(
int level,
bool compressed) {
352 for (
int i = 0; i < level; i++) {
354 if (size < 8) size = 8;
357 size = g_uncompressed_tile_size;
358 for (
int i = 0; i < level; i++) size /= 4;
364bool JobTicket::DoJob() {
365 if (!m_rect.IsEmpty())
return DoJob(m_rect);
368 ChartBase *pchart =
ChartData->OpenChartFromDB(m_ChartPath, FULL_INIT);
369 if (!pchart)
return false;
371 ChartBaseBSB *pBSBChart =
dynamic_cast<ChartBaseBSB *
>(pchart);
372 if (!pBSBChart)
return false;
374 int size_X = pBSBChart->GetSize_X();
375 int size_Y = pBSBChart->GetSize_Y();
377 int dim = g_GLOptions.m_iTextureDimension;
379 int nx_tex = ceil((
float)size_X / dim);
380 int ny_tex = ceil((
float)size_Y / dim);
386 for (
int y = 0; y < ny_tex; y++) {
387 if (pthread && pthread->m_pMessageTarget) {
388 OCPN_CompressionThreadEvent Nevent(wxEVT_OCPN_COMPRESSIONTHREAD, 0);
390 Nevent.nstat_max = ny_tex;
392 Nevent.SetTicket(
this);
393 pthread->m_pMessageTarget->AddPendingEvent(Nevent);
397 for (
int x = 0; x < nx_tex; x++) {
398 if (!DoJob(rect))
return false;
400 pFact->UpdateCacheAllLevels(rect, global_color_scheme,
401 compcomp_bits_array, compcomp_size_array);
404 free(comp_bits_array[i]), comp_bits_array[i] = 0;
405 free(compcomp_bits_array[i]), compcomp_bits_array[i] = 0;
408 rect.x += rect.width;
410 rect.y += rect.height;
421 OCPNStopWatch() { Start(); }
422 void Start() { clock_gettime(CLOCK_REALTIME, &tp); }
426 clock_gettime(CLOCK_REALTIME, &tp_end);
427 return (tp_end.tv_sec - tp.tv_sec) * 1.e3 + (tp_end.tv_nsec - tp.tv_nsec) / 1.e6;
437static void throttle_func(
void *data) {
438 if (!wxThread::IsMain()) {
440 if (sww->Time() > 1) {
447static wxMutex s_mutexProtectingChartBitRead;
449bool JobTicket::DoJob(
const wxRect &rect) {
450 unsigned char *bit_array[10];
451 for (
int i = 0; i < 10; i++) bit_array[i] = 0;
455 bit_array[0] = level0_bits;
466 wxMutexLocker lock(s_mutexProtectingChartBitRead);
468 index =
ChartData->FinddbIndex(m_ChartPath);
469 pchart =
ChartData->OpenChartFromDBAndLock(index, FULL_INIT);
471 if (pchart &&
ChartData->IsChartLocked(index)) {
472 ChartBaseBSB *pBSBChart =
dynamic_cast<ChartBaseBSB *
>(pchart);
475 (
unsigned char *)malloc(ncrect.width * ncrect.height * 4);
476 pBSBChart->GetChartBits(ncrect, bit_array[0], 1);
486 if (!bit_array[0])
return false;
489 dim = g_GLOptions.m_iTextureDimension;
492 size_t nmalloc = wxMax(dim * dim * 3, 4 * 4 * 3);
493 bit_array[i] = (
unsigned char *)malloc(nmalloc);
494 MipMap_24(2 * dim, 2 * dim, bit_array[i - 1], bit_array[i]);
498 int texture_level = 0;
500 int dim = TextureDim(level);
501 int size = TextureTileSize(level,
true);
502 unsigned char *tex_data = (
unsigned char *)malloc(size);
505 int flags = squish::kDxt1 | squish::kColourRangeFit;
507 if (g_GLOptions.m_bTextureCompressionCaching) {
511 flags = squish::kDxt1 | squish::kColourClusterFit;
515 squish::CompressImageRGBpow2_Flatten_Throttle_Abort(
516 bit_array[level], dim, dim, tex_data, flags,
true,
517 b_throttle ? throttle_func : 0, &sww, b_abort);
520 CompressDataETC(bit_array[level], dim, size, tex_data, b_abort);
522 if (!CompressUsingGPU(bit_array[level], dim, size, tex_data,
523 texture_level, binplace)) {
528 if (binplace) g_tex_mem_used += size;
532 comp_bits_array[level] = tex_data;
549 if (b_throttle) wxThread::Sleep(1);
551 if (b_abort)
return false;
553 if (bpost_zip_compress) {
554 int max_compressed_size = LZ4_COMPRESSBOUND(g_tile_size);
557 if (b_abort)
return false;
559 unsigned char *compressed_data =
560 (
unsigned char *)malloc(max_compressed_size);
561 int csize = TextureTileSize(level,
true);
563 char *src = (
char *)comp_bits_array[level];
564#if LZ4_VERSION_MAJOR > 1 || (LZ4_VERSION_MAJOR == 1 && LZ4_VERSION_MINOR >= 7)
566 int compressed_size = LZ4_compress_HC(src, (
char *)compressed_data, csize,
567 max_compressed_size, 4);
570 int compressed_size =
571 LZ4_compressHC2(src, (
char *)compressed_data, csize, 4);
577 (
unsigned char *)realloc(compressed_data, compressed_size);
578 compcomp_bits_array[level] = compressed_data;
579 compcomp_size_array[level] = compressed_size;
602 SE_Exception(
unsigned int n) : nSE(n) {}
604 unsigned int getSeNumber() {
return nSE; }
607void my_translate(
unsigned int code, _EXCEPTION_POINTERS *ep) {
608 throw SE_Exception();
612OCPN_CompressionThreadEvent::OCPN_CompressionThreadEvent(
613 wxEventType commandType,
int id)
614 : wxEvent(id, commandType) {
618OCPN_CompressionThreadEvent::~OCPN_CompressionThreadEvent() {}
620wxEvent *OCPN_CompressionThreadEvent::Clone()
const {
621 OCPN_CompressionThreadEvent *newevent =
622 new OCPN_CompressionThreadEvent(*
this);
623 newevent->m_ticket = this->m_ticket;
624 newevent->type = this->type;
625 newevent->nstat = this->nstat;
626 newevent->nstat_max = this->nstat_max;
658CompressionPoolThread::CompressionPoolThread(
JobTicket *ticket,
659 wxEvtHandler *message_target) {
660 m_pMessageTarget = message_target;
666void *CompressionPoolThread::Entry() {
668 _set_se_translator(my_translate);
678 SetPriority(WXTHREAD_MIN_PRIORITY);
680 if (!m_ticket->DoJob()) m_ticket->b_isaborted =
true;
682 if (m_pMessageTarget) {
683 OCPN_CompressionThreadEvent Nevent(wxEVT_OCPN_COMPRESSIONTHREAD, 0);
684 Nevent.SetTicket(m_ticket);
686 m_pMessageTarget->QueueEvent(Nevent.Clone());
694 catch (SE_Exception e) {
695 if (m_pMessageTarget) {
696 OCPN_CompressionThreadEvent Nevent(wxEVT_OCPN_COMPRESSIONTHREAD, 0);
697 m_ticket->b_isaborted =
true;
698 Nevent.SetTicket(m_ticket);
700 m_pMessageTarget->QueueEvent(Nevent.Clone());
711glTextureManager::glTextureManager() {
714 int nCPU = wxMax(1, wxThread::GetCPUCount());
715 if (g_nCPUCount > 0) nCPU = g_nCPUCount;
722 m_max_jobs = wxMax(nCPU / 2, 1);
726 if (bthread_debug) printf(
" nCPU: %d m_max_jobs :%d\n", nCPU, m_max_jobs);
730 for (
int i = 0; i < m_max_jobs; i++) progList.push_back(
new ProgressInfoItem);
735 wxEVT_OCPN_COMPRESSIONTHREAD,
736 (wxObjectEventFunction)(wxEventFunction)&glTextureManager::OnEvtThread);
743 m_timer.Connect(wxEVT_TIMER, wxTimerEventHandler(glTextureManager::OnTimer),
748glTextureManager::~glTextureManager() {
751 for (
int i = 0; i < m_max_jobs; i++) {
752 auto it = progList.begin();
757 for (
auto hash : m_chart_texfactory_hash) {
760 m_chart_texfactory_hash.clear();
763#define NBAR_LENGTH 40
766 JobTicket *ticket =
event.GetTicket();
768 if (event.type == 1) {
775 ProgressInfoItem *item;
776 for (
auto tnode = progList.begin(); tnode != progList.end(); tnode++) {
778 if (item->file_path == ticket->m_ChartPath) {
786 for (
auto tnode = progList.begin(); tnode != progList.end(); tnode++) {
788 if (item->file_path.IsEmpty()) {
790 item->file_path = ticket->m_ChartPath;
799 int bar_length = NBAR_LENGTH;
800 if (m_bcompact) bar_length = 20;
803 wxString
block = wxString::Format(
"%c", 0x2588);
805 if (event.nstat_max != 0)
806 cutoff = ((
event.nstat + 1) / (float)
event.nstat_max) * bar_length;
807 for (
int i = 0; i < bar_length; i++) {
817 msgy.Printf(
" [%3d/%3d] ", event.nstat + 1, event.nstat_max);
820 wxFileName fn(ticket->m_ChartPath);
821 msgx += fn.GetFullName();
824 msgx.Printf(
"\n %3d/%3d", event.nstat + 1, event.nstat_max);
831 for (
auto tnode = progList.begin(); tnode != progList.end(); tnode++) {
833 msg += item->msgx +
"\n";
836 if (m_skipout) m_progMsg =
"Skipping, please wait...\n\n";
838 if (!m_progDialog->Update(m_jcnt, m_progMsg + msg, &m_skip)) m_skip =
true;
839 if (m_skip) m_skipout =
true;
843 if (ticket->b_isaborted || ticket->b_abort) {
845 free(ticket->comp_bits_array[i]);
846 free(ticket->compcomp_bits_array[i]);
851 " Abort job: %08X Jobs running: %d Job count: %lu "
853 ticket->ident, GetRunningJobCount(), (
unsigned long)todo_list.size());
854 }
else if (!ticket->b_inCompressAll) {
856 glTextureDescriptor *ptd = ticket->pFact->GetpTD(ticket->m_rect);
859 ptd->comp_array[i] = ticket->comp_bits_array[i];
861 if (ticket->bpost_zip_compress) {
863 ptd->compcomp_array[i] = ticket->compcomp_bits_array[i];
864 ptd->compcomp_size[i] = ticket->compcomp_size_array[i];
871 top_frame::Get()->InvalidateAllGL();
872 ptd->compdata_ticks = 10;
877 " Finished job: %08X Jobs running: %d Job count: %lu "
879 ticket->ident, GetRunningJobCount(), (
unsigned long)todo_list.size());
883 if (ticket->b_inCompressAll) {
885 ChartData->OpenChartFromDB(ticket->m_ChartPath, FULL_INIT);
887 delete ticket->pFact;
890 for (
auto tnode = progList.begin(); tnode != progList.end(); ++tnode) {
891 ProgressInfoItem *item = *tnode;
892 if (item->file_path == ticket->m_ChartPath) item->file_path =
"";
896 auto found = std::find(running_list.begin(), running_list.end(), ticket);
897 if (found != running_list.end()) running_list.erase(found);
904void glTextureManager::OnTimer(wxTimerEvent &event) {
910 if (g_GLOptions.m_bTextureCompression) {
911 for (ChartPathHashTexfactType::iterator itt =
912 m_chart_texfactory_hash.begin();
913 itt != m_chart_texfactory_hash.end(); ++itt) {
914 glTexFactory *ptf = itt->second;
915 if (ptf && ptf->OnTimer()) {
922 if((m_ticks % 4) == 0){
925 int mem_total, mem_used;
930 int compcomp_size = 0;
932 for(ChartPathHashTexfactType::iterator itt = m_chart_texfactory_hash.begin();
933 itt != m_chart_texfactory_hash.end(); ++itt ) {
934 glTexFactory *ptf = itt->second;
936 ptf->AccumulateMemStatistics(map_size, comp_size, compcomp_size);
939 int m1 = 1024 * 1024;
941 printf(
"%6d %6ld Map: %10d Comp:%10d CompComp: %10d \n", mem_used/1024, g_tex_mem_used/m1, map_size, comp_size, compcomp_size);
948bool glTextureManager::ScheduleJob(
glTexFactory *client,
const wxRect &rect,
949 int level,
bool b_throttle_thread,
950 bool b_nolimit,
bool b_postZip,
952 wxString chart_path = client->GetChartPath();
954 if (todo_list.size() >= 50) {
956 auto node = todo_list.rbegin();
957 JobTicket *ticket = *node;
958 auto found = std::find(todo_list.begin(), todo_list.end(), ticket);
959 todo_list.erase(found);
965 for (
auto node = todo_list.begin(); node != todo_list.end(); ++node) {
966 JobTicket *ticket = *node;
967 if (ticket->m_ChartPath == chart_path && ticket->m_rect == rect) {
970 todo_list.splice(todo_list.begin(), todo_list, node);
971 ticket->level_min_request = level;
977 for (
auto node = todo_list.begin(); node != todo_list.end(); ++node) {
978 JobTicket *ticket = *node;
979 if (ticket->m_rect == rect && ticket->m_ChartPath == chart_path) {
985 JobTicket *pt =
new JobTicket;
988 pt->level_min_request = level;
989 glTextureDescriptor *ptd = client->GetOrCreateTD(pt->m_rect);
990 pt->ident = (ptd->tex_name << 16) + level;
991 pt->b_throttle = b_throttle_thread;
992 pt->m_ChartPath = chart_path;
994 pt->level0_bits = NULL;
996 pt->b_isaborted =
false;
997 pt->bpost_zip_compress = b_postZip;
998 pt->binplace = b_inplace;
1012 todo_list.insert(todo_list.begin(), pt);
1013 if (bthread_debug) {
1016 printf(
"Adding job: %08X Job Count: %lu mem_used %d\n", pt->ident,
1017 (
unsigned long)todo_list.size(), mem_used);
1023 pt->level0_bits = ptd->map_array[0];
1024 ptd->map_array[0] = NULL;
1028 OCPN_CompressionThreadEvent Nevent(wxEVT_OCPN_COMPRESSIONTHREAD, 0);
1030 Nevent.SetTicket(pt);
1031 ProcessEventLocally(Nevent);
1037bool glTextureManager::StartTopJob() {
1038 auto node = todo_list.begin();
1039 if (node == todo_list.end())
return false;
1041 JobTicket *ticket = *node;
1044 if (GetRunningJobCount() >= wxMax(m_max_jobs - ticket->b_throttle, 1))
1046 auto found = std::find(todo_list.begin(), todo_list.end(), ticket);
1047 if (found != todo_list.end()) todo_list.erase(found);
1049 glTextureDescriptor *ptd = ticket->pFact->GetpTD(ticket->m_rect);
1051 if (ptd->comp_array[0]) {
1053 return StartTopJob();
1056 if (ptd->map_array[0]) {
1057 if (ticket->level_min_request == 0) {
1059 ticket->level0_bits = ptd->map_array[0];
1060 ptd->map_array[0] = NULL;
1063 int size = TextureTileSize(0,
false);
1064 ticket->level0_bits = (
unsigned char *)malloc(size);
1065 memcpy(ticket->level0_bits, ptd->map_array[0], size);
1069 running_list.push_back(ticket);
1070 DoThreadJob(ticket);
1075bool glTextureManager::DoThreadJob(
JobTicket *pticket) {
1077 printf(
" Starting job: %08X Jobs running: %d Jobs left: %lu\n",
1078 pticket->ident, GetRunningJobCount(),
1079 (
unsigned long)todo_list.size());
1083 CompressionPoolThread *t =
new CompressionPoolThread(pticket,
this);
1084 pticket->pthread = t;
1091bool glTextureManager::AsJob(wxString
const &chart_path)
const {
1092 if (chart_path.Len()) {
1093 for (
auto node = todo_list.begin(); node != todo_list.end(); ++node) {
1094 JobTicket *ticket = *node;
1095 if (ticket->m_ChartPath.IsSameAs(chart_path)) {
1103void glTextureManager::PurgeJobList(wxString chart_path) {
1104 if (chart_path.Len()) {
1106 auto &list = todo_list;
1107 auto removed_begin =
1108 std::remove_if(list.begin(), list.end(), [chart_path](JobTicket *t) {
1109 bool is_chart_match = t->m_ChartPath == chart_path;
1110 if (is_chart_match) delete t;
1111 return is_chart_match;
1113 list.erase(removed_begin, list.end());
1116 for (
auto node = running_list.begin(); node != running_list.end(); ++node) {
1117 JobTicket *ticket = *node;
1118 if (ticket->m_ChartPath == chart_path) ticket->b_abort =
true;
1122 std::cout <<
"Pool: Purge, todo count: " << list.size() <<
"\n";
1124 for (
auto node = todo_list.begin(); node != todo_list.end(); ++node) {
1125 JobTicket *ticket = *node;
1130 for (
auto node = running_list.begin(); node != running_list.end(); ++node) {
1131 JobTicket *ticket = *node;
1132 ticket->b_abort =
true;
1137void glTextureManager::ClearJobList() {
1138 for (
auto node = todo_list.begin(); node != todo_list.end(); ++node) {
1139 JobTicket *ticket = *node;
1145void glTextureManager::ClearAllRasterTextures() {
1147 ChartPathHashTexfactType::iterator itt;
1148 for (itt = m_chart_texfactory_hash.begin();
1149 itt != m_chart_texfactory_hash.end(); ++itt) {
1150 glTexFactory *ptf = itt->second;
1154 m_chart_texfactory_hash.clear();
1156 if (g_tex_mem_used != 0)
1157 wxLogMessage(
"Texture memory use calculation error\n");
1160bool glTextureManager::PurgeChartTextures(
ChartBase *pc,
bool b_purge_factory) {
1162 ChartPathHashTexfactType::iterator ittf =
1163 m_chart_texfactory_hash.find(pc->GetHashKey());
1166 if (ittf != m_chart_texfactory_hash.end()) {
1167 glTexFactory *pTexFact = ittf->second;
1170 if (b_purge_factory) {
1171 m_chart_texfactory_hash.erase(ittf);
1178 m_chart_texfactory_hash.erase(ittf);
1185bool glTextureManager::TextureCrunch(
double factor) {
1186 double hysteresis = 0.90;
1190 (double)(g_GLOptions.m_iTextureMemorySize * 1024 * 1024) * factor;
1191 if (!bGLMemCrunch)
return false;
1193 ChartPathHashTexfactType::iterator it0;
1194 for (it0 = m_chart_texfactory_hash.begin();
1195 it0 != m_chart_texfactory_hash.end(); ++it0) {
1196 glTexFactory *ptf = it0->second;
1198 wxString chart_full_path = ptf->GetChartPath();
1200 bGLMemCrunch = g_tex_mem_used >
1201 (double)(g_GLOptions.m_iTextureMemorySize * 1024 * 1024) *
1202 factor * hysteresis;
1203 if (!bGLMemCrunch)
break;
1206 for (
unsigned int i = 0; i <
g_canvasArray.GetCount(); i++) {
1209 if (cc->GetVP().b_quilt)
1211 if (cc->m_pQuilt && cc->m_pQuilt->IsComposed() &&
1212 !cc->m_pQuilt->IsChartInQuilt(chart_full_path)) {
1213 ptf->DeleteSomeTextures(g_GLOptions.m_iTextureMemorySize * 1024 *
1214 1024 * factor * hysteresis);
1218 if (!cc->m_singleChart->GetFullPath().IsSameAs(chart_full_path)) {
1219 ptf->DeleteSomeTextures(g_GLOptions.m_iTextureMemorySize * 1024 *
1220 1024 * factor * hysteresis);
1230#define MAX_CACHE_FACTORY 50
1231bool glTextureManager::FactoryCrunch(
double factor) {
1232 if (m_chart_texfactory_hash.size() == 0) {
1239 double hysteresis = 0.90;
1240 ChartPathHashTexfactType::iterator it0;
1244 ((mem_used > (double)(g_memCacheLimit)*factor * hysteresis &&
1245 mem_used > (double)(m_prevMemUsed)*factor * hysteresis) ||
1246 (m_chart_texfactory_hash.size() > MAX_CACHE_FACTORY)));
1248 if (!bMemCrunch)
return false;
1252 int lru_oldest = 2147483647;
1253 glTexFactory *ptf_oldest = NULL;
1255 for (it0 = m_chart_texfactory_hash.begin();
1256 it0 != m_chart_texfactory_hash.end(); ++it0) {
1257 glTexFactory *ptf = it0->second;
1259 wxString chart_full_path = ptf->GetChartPath();
1265 for (
unsigned int i = 0; i <
g_canvasArray.GetCount(); i++) {
1268 if (cc->GetVP().b_quilt)
1270 if (cc->m_pQuilt && cc->m_pQuilt->IsComposed() &&
1271 !cc->m_pQuilt->IsChartInQuilt(chart_full_path)) {
1272 int lru = ptf->GetLRUTime();
1273 if (lru < lru_oldest && !ptf->BackgroundCompressionAsJob()) {
1279 if (!cc->m_singleChart->GetFullPath().IsSameAs(chart_full_path)) {
1280 int lru = ptf->GetLRUTime();
1281 if (lru < lru_oldest && !ptf->BackgroundCompressionAsJob()) {
1292 if (!ptf_oldest)
return false;
1294 ptf_oldest->FreeSome(g_memCacheLimit * factor * hysteresis);
1298 bMemCrunch = (g_memCacheLimit &&
1299 ((mem_used > (double)(g_memCacheLimit)*factor * hysteresis &&
1300 mem_used > (double)(m_prevMemUsed)*factor * hysteresis) ||
1301 (m_chart_texfactory_hash.size() > MAX_CACHE_FACTORY)));
1303 if (!bMemCrunch)
return false;
1307 m_chart_texfactory_hash.erase(
1308 ptf_oldest->GetHashKey());
1315void glTextureManager::BuildCompressedCache() {
1316 idx_sorted_by_distance.Clear();
1324 for (
int i = 0; i <
ChartData->GetChartTableEntries(); i++) {
1326 const ChartTableEntry &cte =
ChartData->GetChartTableEntry(i);
1327 ChartTypeEnum chart_type = (ChartTypeEnum)cte.GetChartType();
1328 if (chart_type == CHART_TYPE_PLUGIN) {
1329 if (cte.GetChartFamily() != CHART_FAMILY_RASTER)
continue;
1331 if (chart_type != CHART_TYPE_KAP)
continue;
1334 wxString CompressedCacheFilePath =
1335 CompressedCachePath(
ChartData->GetDBChartFileName(i));
1336 wxFileName fn(CompressedCacheFilePath);
1340 idx_sorted_by_distance.Add(i);
1345 if (count == 0)
return;
1347 wxLogMessage(wxString::Format(
"BuildCompressedCache() count = %d", count));
1351 if (GetRunningJobCount()) {
1352 wxLogMessage(
"Starting compressor pool drain");
1353 wxDateTime now = wxDateTime::Now();
1354 time_t stall = now.GetTicks();
1355#define THREAD_WAIT_SECONDS 5
1356 time_t end = stall + THREAD_WAIT_SECONDS;
1359 while (stall < end) {
1360 wxDateTime later = wxDateTime::Now();
1361 stall = later.GetTicks();
1364 msg.Printf(
"Time: %d Job Count: %d", n_comploop, GetRunningJobCount());
1366 if (!GetRunningJobCount())
break;
1372 fmsg.Printf(
"Finished compressor pool drain..Time: %d Job Count: %d",
1373 n_comploop, GetRunningJobCount());
1376 ClearAllRasterTextures();
1383 ArrayOfCompressTargets ct_array;
1384 for (
unsigned int j = 0; j < idx_sorted_by_distance.GetCount(); j++) {
1385 int i = idx_sorted_by_distance[j];
1387 const ChartTableEntry &cte =
ChartData->GetChartTableEntry(i);
1388 double distance = chart_dist(i);
1390 wxString filename = cte.GetFullSystemPath();
1392 compress_target *pct =
new compress_target;
1393 pct->distance = distance;
1394 pct->chart_path = filename;
1400 long style = wxPD_SMOOTH | wxPD_ELAPSED_TIME | wxPD_ESTIMATED_TIME |
1401 wxPD_REMAINING_TIME | wxPD_CAN_ABORT;
1411 "longgggggggggggggggggggggggggggggggggggggggggggg\ngggggggggggggggggg"
1412 "gggggggggggggggggggggggggg top line ";
1415 for (
int i = 0; i < m_max_jobs + 1; i++)
1418 m_progDialog =
new wxGenericProgressDialog();
1421 int fontSize = qFont->GetPointSize();
1423 wxSize csz = wxTheApp->GetTopWindow()->GetClientSize();
1424 if (csz.x < 500 || csz.y < 500)
1426 10, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
1430 wxFONTWEIGHT_NORMAL);
1432 m_progDialog->SetFont(*sFont);
1437 sdc.GetTextExtent(
"[WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW]", &width, &height, NULL,
1439 if (width > (csz.x / 2)) m_bcompact =
true;
1441 m_progDialog->Create(_(
"OpenCPN Compressed Cache Update"), msg0, count + 1,
1445 m_progDialog->Hide();
1446 wxSize sz = m_progDialog->GetSize();
1447 sz.x = csz.x * 9 / 10;
1448 m_progDialog->SetSize(sz);
1450 m_progDialog->Layout();
1451 wxSize sza = m_progDialog->GetSize();
1453 m_progDialog->Centre();
1454 m_progDialog->Show();
1455 m_progDialog->Raise();
1461 for (m_jcnt = 0; m_jcnt < ct_array.GetCount(); m_jcnt++) {
1462 wxString filename = ct_array[m_jcnt].chart_path;
1463 wxString CompressedCacheFilePath = CompressedCachePath(filename);
1464 double distance = ct_array[m_jcnt].distance;
1466 ChartBase *pchart =
ChartData->OpenChartFromDBAndLock(filename, FULL_INIT);
1473 ChartBaseBSB *pBSBChart =
dynamic_cast<ChartBaseBSB *
>(pchart);
1474 if (pBSBChart == 0)
continue;
1478 m_progMsg.Printf(_(
"Distance from Ownship: %4.0f NMi"), distance);
1480 m_progMsg.Prepend(
"Preparing RNC Cache...\n");
1489 int size_X = pBSBChart->GetSize_X();
1490 int size_Y = pBSBChart->GetSize_Y();
1492 int tex_dim = g_GLOptions.m_iTextureDimension;
1494 int nx_tex = ceil((
float)size_X / tex_dim);
1495 int ny_tex = ceil((
float)size_Y / tex_dim);
1499 rect.width = tex_dim;
1500 rect.height = tex_dim;
1501 for (
int y = 0; y < ny_tex; y++) {
1503 for (
int x = 0; x < nx_tex; x++) {
1505 if (!tex_fact->IsLevelInCache(level, rect, global_color_scheme)) {
1509 rect.x += rect.width;
1511 rect.y += rect.height;
1521 if (!m_progDialog->Update(m_jcnt)) {
1532 ScheduleJob(tex_fact, wxRect(), 0,
false,
true,
true,
false);
1535 int cnt = GetJobCount() - GetRunningJobCount();
1548 while (GetRunningJobCount()) {
1556 delete m_progDialog;
1557 m_progDialog =
nullptr;
General chart base definitions.
ChartDB * ChartData
Global instance.
Charts database management
arrayofCanvasPtr g_canvasArray
Global instance.
Generic Chart canvas base.
Base class for BSB (Maptech/NOS) format nautical charts.
Base class for all chart types.
wxFont * FindOrCreateFont(int point_size, wxFontFamily family, wxFontStyle style, wxFontWeight weight, bool underline=false, const wxString &facename=wxEmptyString, wxFontEncoding encoding=wxFONTENCODING_DEFAULT)
Creates or finds a matching font in the font cache.
Global variables stored in configuration file.
GLuint g_raster_format
Global instance.
OpenGL chart rendering canvas.
glTextureManager * g_glTextureManager
Global instance.
OpenGL texture container.
int g_mipmap_max_level
Global instance.
wxFont * GetOCPNScaledFont(wxString item, int default_size)
Retrieves a font from FontMgr, optionally scaled for physical readability.
General purpose GUI support.
int g_mipmap_max_level
Global instance.
bool b_inCompressAllCharts
Flag to control adaptive UI scaling.
Miscellaneous globals primarely used by gui layer, not persisted in configuration file.
double gLat
Vessel's current latitude in decimal degrees.
double gLon
Vessel's current longitude in decimal degrees.
Position, course, speed, etc.
struct block block
Runtime representation of a plugin block.
Represents an entry in the chart table, containing information about a single chart.
Runtime representation of a plugin block.
Abstract gFrame/MyFrame interface.
Geographic projection and coordinate transformations.