From 8f3040e62ce3d0414aea1322d78cc2ee5c4190c2 Mon Sep 17 00:00:00 2001 From: rachytski Date: Sat, 16 Jul 2011 19:56:15 +0300 Subject: [PATCH] separated benchmarking code into Framework ancestor. --- map/benchmark_framework.cpp | 301 ++++++++ map/benchmark_framework.hpp | 74 ++ map/framework.cpp | 1352 ++++++++++++++--------------------- map/framework.hpp | 114 ++- map/framework_factory.cpp | 18 + map/framework_factory.hpp | 13 + map/map.pro | 8 +- map/render_policy.hpp | 1 - platform/platform.cpp | 4 +- qt/draw_widget.cpp | 68 +- qt/draw_widget.hpp | 4 +- 11 files changed, 1036 insertions(+), 921 deletions(-) create mode 100644 map/benchmark_framework.cpp create mode 100644 map/benchmark_framework.hpp create mode 100644 map/framework_factory.cpp create mode 100644 map/framework_factory.hpp diff --git a/map/benchmark_framework.cpp b/map/benchmark_framework.cpp new file mode 100644 index 0000000000..401cbcd69e --- /dev/null +++ b/map/benchmark_framework.cpp @@ -0,0 +1,301 @@ +#include "../base/SRC_FIRST.hpp" + +#include "benchmark_framework.hpp" +#include "benchmark_provider.hpp" +#include "feature_vec_model.hpp" + +#include "../coding/file_container.hpp" + +#include "../std/fstream.hpp" + +#include "../platform/settings.hpp" + +#include "../version/version.hpp" + +#include "tiling_render_policy_st.hpp" + +template class DoGetBenchmarks +{ + set m_processed; + vector & m_benchmarks; + Platform & m_pl; + +public: + DoGetBenchmarks(vector & benchmarks) + : m_benchmarks(benchmarks), m_pl(GetPlatform()) + { + } + + void operator() (vector const & v) + { + T b; + b.m_name = v[1]; + + m2::RectD r; + if (m_processed.insert(v[0]).second) + { + try + { + feature::DataHeader header; + header.Load(FilesContainerR(m_pl.GetReader(v[0])).GetReader(HEADER_FILE_TAG)); + r = header.GetBounds(); + } + catch (RootException const & e) + { + LOG(LINFO, ("Cannot add ", v[0], " file to benchmark: ", e.what())); + return; + } + } + + int lastScale; + if (v.size() > 3) + { + double x0, y0, x1, y1; + strings::to_double(v[2], x0); + strings::to_double(v[3], y0); + strings::to_double(v[4], x1); + strings::to_double(v[5], y1); + r = m2::RectD(x0, y0, x1, y1); + strings::to_int(v[6], lastScale); + } + else + strings::to_int(v[2], lastScale); + + ASSERT ( r != m2::RectD::GetEmptyRect(), (r) ); + b.m_provider.reset(new BenchmarkRectProvider(scales::GetScaleLevel(r), r, lastScale)); + + m_benchmarks.push_back(b); + } +}; + +template +void ForEachBenchmarkRecord(ToDo & toDo) +{ + Platform & pl = GetPlatform(); + + string buffer; + try + { + string configPath; + Settings::Get("BenchmarkConfig", configPath); + ReaderPtr(pl.GetReader(configPath)).ReadAsString(buffer); + } + catch (RootException const & e) + { + LOG(LERROR, ("Error reading benchmarks: ", e.what())); + return; + } + + istringstream stream(buffer); + + string line; + while (stream.good()) + { + getline(stream, line); + + vector parts; + strings::SimpleTokenizer it(line, " "); + while (it) + { + parts.push_back(*it); + ++it; + } + + if (!parts.empty()) + toDo(parts); + } +} + +class FirstReaderAdder : public ReadersAdder +{ + typedef ReadersAdder base_type; +public: + FirstReaderAdder(maps_list_t & lst) : base_type(GetPlatform(), lst) {} + void operator() (vector const & v) + { + base_type::operator() (v[0]); + } +}; + +template +void BenchmarkFramework::EnumLocalMaps(typename base_type::maps_list_t & filesList) +{ + FirstReaderAdder adder(filesList); + ForEachBenchmarkRecord(adder); +} + +template +BenchmarkFramework::BenchmarkFramework(shared_ptr const & wh, + size_t bottomShift) + : base_type(wh, bottomShift), + m_paintDuration(0), + m_maxDuration(0), + m_isBenchmarkInitialized(false) +{ + base_type::SetRenderPolicy(make_shared_ptr(new TilingRenderPolicyST(wh, bind(&base_type::DrawModel, this, _1, _2, _3, _4)))); + + m_startTime = my::FormatCurrentTime(); + +#ifdef DEBUG + base_type::m_informationDisplay.enableGlobalRect(false); +#endif + + base_type::m_informationDisplay.enableBenchmarkInfo(true); +} + +template +void BenchmarkFramework::BenchmarkCommandFinished() +{ + double duration = m_paintDuration; + + if (duration > m_maxDuration) + { + m_maxDuration = duration; + m_maxDurationRect = m_curBenchmarkRect; + base_type::m_informationDisplay.addBenchmarkInfo("maxDurationRect: ", m_maxDurationRect, m_maxDuration); + } + + BenchmarkResult res; + res.m_name = m_benchmarks[m_curBenchmark].m_name; + res.m_rect = m_curBenchmarkRect; + res.m_time = duration; + m_benchmarkResults.push_back(res); + + if (m_benchmarkResults.size() > 100) + SaveBenchmarkResults(); + + + m_paintDuration = 0; + NextBenchmarkCommand(); +} + +template +void BenchmarkFramework::SaveBenchmarkResults() +{ + string resultsPath; + Settings::Get("BenchmarkResults", resultsPath); + LOG(LINFO, (resultsPath)); + ofstream fout(GetPlatform().WritablePathForFile(resultsPath).c_str(), ios::app); + + for (size_t i = 0; i < m_benchmarkResults.size(); ++i) + { + fout << GetPlatform().DeviceID() << " " + << VERSION_STRING << " " + << m_startTime << " " + << m_benchmarkResults[i].m_name << " " + << m_benchmarkResults[i].m_rect.minX() << " " + << m_benchmarkResults[i].m_rect.minY() << " " + << m_benchmarkResults[i].m_rect.maxX() << " " + << m_benchmarkResults[i].m_rect.maxY() << " " + << m_benchmarkResults[i].m_time << endl; + } + + m_benchmarkResults.clear(); +} + +template +void BenchmarkFramework::SendBenchmarkResults() +{ +// ofstream fout(GetPlatform().WritablePathForFile("benchmarks/results.txt").c_str(), ios::app); +// fout << "[COMPLETED]"; +// fout.close(); + /// send to server for adding to statistics graphics + /// and delete results file +} + +template +void BenchmarkFramework::MarkBenchmarkResultsEnd() +{ + string resultsPath; + Settings::Get("BenchmarkResults", resultsPath); + LOG(LINFO, (resultsPath)); + ofstream fout(GetPlatform().WritablePathForFile(resultsPath).c_str(), ios::app); + fout << "END " << m_startTime << endl; +} + +template +void BenchmarkFramework::MarkBenchmarkResultsStart() +{ + string resultsPath; + Settings::Get("BenchmarkResults", resultsPath); + LOG(LINFO, (resultsPath)); + ofstream fout(GetPlatform().WritablePathForFile(resultsPath).c_str(), ios::app); + fout << "START " << m_startTime << endl; +} + +template +void BenchmarkFramework::NextBenchmarkCommand() +{ + if ((m_benchmarks[m_curBenchmark].m_provider->hasRect()) || (++m_curBenchmark < m_benchmarks.size())) + { + m_curBenchmarkRect = m_benchmarks[m_curBenchmark].m_provider->nextRect(); + base_type::m_navigator.SetFromRect(m_curBenchmarkRect); + base_type::Invalidate(); + } + else + { + static bool isFirstTime = true; + if (isFirstTime) + { + isFirstTime = false; + SaveBenchmarkResults(); + MarkBenchmarkResultsEnd(); + SendBenchmarkResults(); + LOG(LINFO, ("Bechmarks took ", m_benchmarksTimer.ElapsedSeconds(), " seconds to complete")); + } + } +} + +template +void BenchmarkFramework::InitBenchmark() +{ + DoGetBenchmarks doGet(m_benchmarks); + ForEachBenchmarkRecord(doGet); + + m_curBenchmark = 0; + + //base_type::m_renderPolicy->addAfterFrame(bind(&this_type::BenchmarkCommandFinished, this)); + + m_benchmarksTimer.Reset(); + + MarkBenchmarkResultsStart(); + NextBenchmarkCommand(); + + base_type::Invalidate(); +} + +template +void BenchmarkFramework::OnSize(int w, int h) +{ + Framework::OnSize(w, h); + if (!m_isBenchmarkInitialized) + { + m_isBenchmarkInitialized = true; + InitBenchmark(); + } +} + +template +void BenchmarkFramework::Paint(shared_ptr e) +{ +/* m2::PointD const center = base_type::m_renderQueue.renderState().m_actualScreen.ClipRect().Center(); + base_type::m_informationDisplay.setScreen(m_renderQueue.renderState().m_actualScreen); + base_type::m_informationDisplay.setCenter(m2::PointD(MercatorBounds::XToLon(center.x), MercatorBounds::YToLat(center.y))); + + if (!m_isBenchmarkInitialized) + { + e->drawer()->screen()->beginFrame(); + e->drawer()->screen()->clear(base_type::m_renderPolicy->bgColor()); + base_type::m_informationDisplay.setDisplayRect(m2::RectI(0, 0, 100, 100)); + base_type::m_informationDisplay.enableRuler(false); + base_type::m_informationDisplay.doDraw(e->drawer().get()); + e->drawer()->screen()->endFrame(); + } + else*/ + double s = m_benchmarksTimer.ElapsedSeconds(); + Framework::Paint(e); + m_paintDuration += m_benchmarksTimer.ElapsedSeconds() - s; + BenchmarkCommandFinished(); +} + +template class BenchmarkFramework; diff --git a/map/benchmark_framework.hpp b/map/benchmark_framework.hpp new file mode 100644 index 0000000000..a62c7ed004 --- /dev/null +++ b/map/benchmark_framework.hpp @@ -0,0 +1,74 @@ +#pragma once + +#include "framework.hpp" + +#include "../geometry/rect2d.hpp" + +#include "../base/timer.hpp" + +#include "../std/string.hpp" +#include "../std/vector.hpp" +#include "../std/shared_ptr.hpp" + +#include "../search/engine.hpp" + +class BenchmarkRectProvider; +class WindowHandle; +class PaintEvent; + +template +class BenchmarkFramework : public Framework +{ +private: + + typedef Framework base_type; + + double m_paintDuration; + double m_maxDuration; + m2::RectD m_maxDurationRect; + m2::RectD m_curBenchmarkRect; + + string m_startTime; + + struct BenchmarkResult + { + string m_name; + m2::RectD m_rect; + double m_time; + }; + + vector m_benchmarkResults; + my::Timer m_benchmarksTimer; + + struct Benchmark + { + shared_ptr m_provider; + string m_name; + }; + + vector m_benchmarks; + size_t m_curBenchmark; + + bool m_isBenchmarkInitialized; + + void BenchmarkCommandFinished(); + void NextBenchmarkCommand(); + void SaveBenchmarkResults(); + void SendBenchmarkResults(); + + void MarkBenchmarkResultsStart(); + void MarkBenchmarkResultsEnd(); + + void InitBenchmark(); + + void EnumLocalMaps(typename base_type::maps_list_t & filesList); + +public: + + BenchmarkFramework(shared_ptr const & wh, + size_t bottomShift); + + void OnSize(int w, int h); + + virtual void Paint(shared_ptr e); +}; diff --git a/map/framework.cpp b/map/framework.cpp index 83c42bc198..b343ec7af2 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -24,109 +24,97 @@ #include "../std/algorithm.hpp" #include "../std/fstream.hpp" -#include "../version/version.hpp" - -#include "../yg/internal/opengl.hpp" -#include "../yg/info_layer.hpp" - #include "tiling_render_policy_st.hpp" #include "tiling_render_policy_mt.hpp" using namespace feature; - template - void FrameWork::AddMap(string const & file) +template +void Framework::AddMap(string const & file) +{ + // update rect for Show All button + feature::DataHeader header; + header.Load(FilesContainerR(GetPlatform().GetReader(file)).GetReader(HEADER_FILE_TAG)); + m_model.AddWorldRect(header.GetBounds()); + + threads::MutexGuard lock(m_modelSyn); + m_model.AddMap(file); +} + +template +void Framework::RemoveMap(string const & datFile) +{ + threads::MutexGuard lock(m_modelSyn); + m_model.RemoveMap(datFile); +} + +template +void Framework::OnGpsUpdate(location::GpsInfo const & info) +{ + // notify GUI that we received gps position + if (!(m_locationState & location::State::EGps) && m_locationObserver) + m_locationObserver(); + + m_locationState.UpdateGps(info); + if (m_centeringMode == ECenterAndScale) { - // update rect for Show All button - feature::DataHeader header; - header.Load(FilesContainerR(GetPlatform().GetReader(file)).GetReader(HEADER_FILE_TAG)); - m_model.AddWorldRect(header.GetBounds()); - - threads::MutexGuard lock(m_modelSyn); - m_model.AddMap(file); - } - - template - void FrameWork::RemoveMap(string const & datFile) - { - threads::MutexGuard lock(m_modelSyn); - m_model.RemoveMap(datFile); - } - - template - void FrameWork::OnGpsUpdate(location::GpsInfo const & info) - { - // notify GUI that we received gps position - if (!(m_locationState & location::State::EGps) && m_locationObserver) - m_locationObserver(); - - m_locationState.UpdateGps(info); - if (m_centeringMode == ECenterAndScale) - { - CenterAndScaleViewport(); - m_centeringMode = ECenterOnly; - } - else if (m_centeringMode == ECenterOnly) - CenterViewport(m_locationState.Position()); - Invalidate(); + CenterAndScaleViewport(); + m_centeringMode = ECenterOnly; } + else if (m_centeringMode == ECenterOnly) + CenterViewport(m_locationState.Position()); + Invalidate(); +} template void FrameWork::OnCompassUpdate(location::CompassInfo const & info) { -// if (info.m_timestamp < location::POSITION_TIMEOUT_SECONDS) -// { + if (info.m_timestamp < location::POSITION_TIMEOUT_SECONDS) + { m_locationState.UpdateCompass(info); - UpdateNow(); + Invalidate(); } } - template - FrameWork::FrameWork(shared_ptr windowHandle, - size_t bottomShift) - : m_windowHandle(windowHandle), - m_renderPolicy(new TilingRenderPolicyMT(windowHandle, bind(&this_type::PaintImpl, this, _1, _2, _3, _4))), - m_isBenchmarking(GetPlatform().IsBenchmarking()), - m_isBenchmarkInitialized(false), - m_metresMinWidth(20), - m_minRulerWidth(97), - m_centeringMode(EDoNothing), - m_maxDuration(0), - m_tileSize(GetPlatform().TileSize()) - { - m_startTime = my::FormatCurrentTime(); - - m_informationDisplay.setBottomShift(bottomShift); +template +Framework::Framework(shared_ptr windowHandle, + size_t bottomShift) + : m_windowHandle(windowHandle), + m_metresMinWidth(20), + m_minRulerWidth(97), + m_centeringMode(EDoNothing), + m_tileSize(GetPlatform().TileSize()) +{ + SetRenderPolicy(make_shared_ptr(new TilingRenderPolicyMT(windowHandle, bind(&this_type::DrawModel, this, _1, _2, _3, _4)))); + m_informationDisplay.setBottomShift(bottomShift); #ifdef DRAW_TOUCH_POINTS - m_informationDisplay.enableDebugPoints(true); + m_informationDisplay.enableDebugPoints(true); #endif #ifdef DEBUG - m_informationDisplay.enableGlobalRect(!m_isBenchmarking); + m_informationDisplay.enableGlobalRect(true); #endif - m_informationDisplay.enableCenter(true); + m_informationDisplay.enableCenter(true); - m_informationDisplay.enableRuler(true); - m_informationDisplay.setRulerParams(m_minRulerWidth, m_metresMinWidth); - m_navigator.SetMinScreenParams(m_minRulerWidth, m_metresMinWidth); + m_informationDisplay.enableRuler(true); + m_informationDisplay.setRulerParams(m_minRulerWidth, m_metresMinWidth); + m_navigator.SetMinScreenParams(m_minRulerWidth, m_metresMinWidth); #ifdef DEBUG - m_informationDisplay.enableDebugInfo(true); + m_informationDisplay.enableDebugInfo(true); #endif - m_informationDisplay.enableLog(GetPlatform().IsVisualLog(), m_windowHandle.get()); + m_informationDisplay.enableLog(GetPlatform().IsVisualLog(), m_windowHandle.get()); - m_informationDisplay.enableBenchmarkInfo(m_isBenchmarking); + m_informationDisplay.setVisualScale(GetPlatform().VisualScale()); - m_informationDisplay.setVisualScale(GetPlatform().VisualScale()); - - // initialize gps and compass subsystem - GetLocationManager().SetGpsObserver( - bind(&this_type::OnGpsUpdate, this, _1)); - GetLocationManager().SetCompassObserver( - bind(&this_type::OnCompassUpdate, this, _1)); + // initialize gps and compass subsystem + GetLocationManager().SetGpsObserver( + bind(&this_type::OnGpsUpdate, this, _1)); + GetLocationManager().SetCompassObserver( + bind(&this_type::OnCompassUpdate, this, _1)); // set language priorities languages::CodesT langCodes; @@ -134,759 +122,487 @@ using namespace feature; languages::SaveSettings(langCodes); } - template - FrameWork::~FrameWork() +template +void Framework::EnumLocalMaps(maps_list_t & filesList) +{ + Platform & pl = GetPlatform(); + + // scan for pre-installed maps in resources + string const resPath = pl.ResourcesDir(); + Platform::FilesList resFiles; + pl.GetFilesInDir(resPath, "*" DATA_FILE_EXTENSION, resFiles); + + // scan for probably updated maps in data dir + string const dataPath = pl.WritableDir(); + Platform::FilesList dataFiles; + pl.GetFilesInDir(dataPath, "*" DATA_FILE_EXTENSION, dataFiles); + + // wipe out same maps from resources, which have updated + // downloaded versions in data path + for (Platform::FilesList::iterator it = resFiles.begin(); it != resFiles.end();) { - } - - template - void FrameWork::BenchmarkCommandFinished() - { -/* double duration = m_renderQueue.renderState().m_duration; - if (duration > m_maxDuration) - { - m_maxDuration = duration; - m_maxDurationRect = m_curBenchmarkRect; - m_informationDisplay.addBenchmarkInfo("maxDurationRect: ", m_maxDurationRect, m_maxDuration); - } - - BenchmarkResult res; - res.m_name = m_benchmarks[m_curBenchmark].m_name; - res.m_rect = m_curBenchmarkRect; - res.m_time = duration; - m_benchmarkResults.push_back(res); - - if (m_benchmarkResults.size() > 100) - SaveBenchmarkResults(); - -// NextBenchmarkCommand();*/ - } - - template - void FrameWork::SaveBenchmarkResults() - { -/* string resultsPath; - Settings::Get("BenchmarkResults", resultsPath); - LOG(LINFO, (resultsPath)); - ofstream fout(GetPlatform().WritablePathForFile(resultsPath).c_str(), ios::app); - - for (size_t i = 0; i < m_benchmarkResults.size(); ++i) - { - fout << GetPlatform().DeviceID() << " " - << VERSION_STRING << " " - << m_startTime << " " - << m_benchmarkResults[i].m_name << " " - << m_benchmarkResults[i].m_rect.minX() << " " - << m_benchmarkResults[i].m_rect.minY() << " " - << m_benchmarkResults[i].m_rect.maxX() << " " - << m_benchmarkResults[i].m_rect.maxY() << " " - << m_benchmarkResults[i].m_time << endl; - } - - m_benchmarkResults.clear();*/ - } - - template - void FrameWork::SendBenchmarkResults() - { -// ofstream fout(GetPlatform().WritablePathForFile("benchmarks/results.txt").c_str(), ios::app); -// fout << "[COMPLETED]"; -// fout.close(); - /// send to server for adding to statistics graphics - /// and delete results file - } - - template - void FrameWork::MarkBenchmarkResultsEnd() - { - string resultsPath; - Settings::Get("BenchmarkResults", resultsPath); - LOG(LINFO, (resultsPath)); - ofstream fout(GetPlatform().WritablePathForFile(resultsPath).c_str(), ios::app); - fout << "END " << m_startTime << endl; - } - - template - void FrameWork::MarkBenchmarkResultsStart() - { - string resultsPath; - Settings::Get("BenchmarkResults", resultsPath); - LOG(LINFO, (resultsPath)); - ofstream fout(GetPlatform().WritablePathForFile(resultsPath).c_str(), ios::app); - fout << "START " << m_startTime << endl; - } - -/* template - void FrameWork::NextBenchmarkCommand() - { - if ((m_benchmarks[m_curBenchmark].m_provider->hasRect()) || (++m_curBenchmark < m_benchmarks.size())) - { - m_curBenchmarkRect = m_benchmarks[m_curBenchmark].m_provider->nextRect(); - m_navigator.SetFromRect(m_curBenchmarkRect); - m_renderQueue.AddBenchmarkCommand(bind(&this_type::PaintImpl, this, _1, _2, _3, _4), m_navigator.Screen()); - } + Platform::FilesList::iterator found = find(dataFiles.begin(), dataFiles.end(), *it); + if (found != dataFiles.end()) + it = resFiles.erase(it); else - { - SaveBenchmarkResults(); - MarkBenchmarkResultsEnd(); - SendBenchmarkResults(); - LOG(LINFO, ("Bechmarks took ", m_benchmarksTimer.ElapsedSeconds(), " seconds to complete")); - } - } -*/ - struct PathAppender - { - string const & m_path; - PathAppender(string const & path) : m_path(path) {} - void operator()(string & elem) - { - elem.insert(elem.begin(), m_path.begin(), m_path.end()); - } - }; - - class ReadersAdder - { - protected: - typedef vector maps_list_t; - - private: - Platform & m_pl; - maps_list_t & m_lst; - - public: - ReadersAdder(Platform & pl, maps_list_t & lst) : m_pl(pl), m_lst(lst) {} - - void operator() (string const & name) - { - m_lst.push_back(name); - } - }; - - template - void FrameWork::EnumLocalMaps(maps_list_t & filesList) - { - Platform & pl = GetPlatform(); - - // scan for pre-installed maps in resources - string const resPath = pl.ResourcesDir(); - Platform::FilesList resFiles; - pl.GetFilesInDir(resPath, "*" DATA_FILE_EXTENSION, resFiles); - - // scan for probably updated maps in data dir - string const dataPath = pl.WritableDir(); - Platform::FilesList dataFiles; - pl.GetFilesInDir(dataPath, "*" DATA_FILE_EXTENSION, dataFiles); - - // wipe out same maps from resources, which have updated - // downloaded versions in data path - for (Platform::FilesList::iterator it = resFiles.begin(); it != resFiles.end();) - { - Platform::FilesList::iterator found = find(dataFiles.begin(), dataFiles.end(), *it); - if (found != dataFiles.end()) - it = resFiles.erase(it); - else - ++it; - } - - try - { - filesList.clear(); - for_each(resFiles.begin(), resFiles.end(), ReadersAdder(pl, filesList)); - for_each(dataFiles.begin(), dataFiles.end(), ReadersAdder(pl, filesList)); - } - catch (RootException const & e) - { - LOG(LERROR, ("Can't add map: ", e.what())); - } + ++it; } - template - void ForEachBenchmarkRecord(ToDo & toDo) + try { - Platform & pl = GetPlatform(); - - string buffer; - try - { - string configPath; - Settings::Get("BenchmarkConfig", configPath); - ReaderPtr(pl.GetReader(configPath)).ReadAsString(buffer); - } - catch (RootException const & e) - { - LOG(LERROR, ("Error reading benchmarks: ", e.what())); - return; - } - - istringstream stream(buffer); - - string line; - while (stream.good()) - { - getline(stream, line); - - vector parts; - strings::SimpleTokenizer it(line, " "); - while (it) - { - parts.push_back(*it); - ++it; - } - - if (!parts.empty()) - toDo(parts); - } + filesList.clear(); + for_each(resFiles.begin(), resFiles.end(), ReadersAdder(pl, filesList)); + for_each(dataFiles.begin(), dataFiles.end(), ReadersAdder(pl, filesList)); } - - class FirstReaderAdder : public ReadersAdder + catch (RootException const & e) { - typedef ReadersAdder base_type; - public: - FirstReaderAdder(maps_list_t & lst) : base_type(GetPlatform(), lst) {} - void operator() (vector const & v) - { - base_type::operator() (v[0]); - } - }; - - template - void FrameWork::EnumBenchmarkMaps(maps_list_t & filesList) - { - FirstReaderAdder adder(filesList); - ForEachBenchmarkRecord(adder); + LOG(LERROR, ("Can't add map: ", e.what())); } - - template class DoGetBenchmarks - { - set m_processed; - vector & m_benchmarks; - Platform & m_pl; - - public: - DoGetBenchmarks(vector & benchmarks) - : m_benchmarks(benchmarks), m_pl(GetPlatform()) - { - } - - void operator() (vector const & v) - { - T b; - b.m_name = v[1]; - - m2::RectD r; - if (m_processed.insert(v[0]).second) - { - try - { - feature::DataHeader header; - header.Load(FilesContainerR(m_pl.GetReader(v[0])).GetReader(HEADER_FILE_TAG)); - r = header.GetBounds(); - } - catch (RootException const & e) - { - LOG(LINFO, ("Cannot add ", v[0], " file to benchmark: ", e.what())); - return; - } - } - - int lastScale; - if (v.size() > 3) - { - double x0, y0, x1, y1; - strings::to_double(v[2], x0); - strings::to_double(v[3], y0); - strings::to_double(v[4], x1); - strings::to_double(v[5], y1); - r = m2::RectD(x0, y0, x1, y1); - strings::to_int(v[6], lastScale); - } - else - strings::to_int(v[2], lastScale); - - ASSERT ( r != m2::RectD::GetEmptyRect(), (r) ); - b.m_provider.reset(new BenchmarkRectProvider(scales::GetScaleLevel(r), r, lastScale)); - - m_benchmarks.push_back(b); - } - }; - - template - void FrameWork::InitBenchmark() - { -// DoGetBenchmarks doGet(m_benchmarks); -// ForEachBenchmarkRecord(doGet); - -// m_curBenchmark = 0; - -// m_renderQueue.AddRenderCommandFinishedFn(bind(&this_type::BenchmarkCommandFinished, this)); -// m_benchmarksTimer.Reset(); - -// MarkBenchmarkResultsStart(); -// NextBenchmarkCommand(); - - Invalidate(); - } - - template - void FrameWork::initializeGL( - shared_ptr const & primaryContext, - shared_ptr const & resourceManager) - { - m_renderPolicy->initialize(primaryContext, resourceManager); - } - - template - TModel & FrameWork::get_model() - { - return m_model; - } - - template - void FrameWork::StartLocationService(LocationRetrievedCallbackT observer) - { - m_locationObserver = observer; - m_centeringMode = ECenterAndScale; - // by default, we always start in accurate mode - GetLocationManager().StartUpdate(true); - } - - template - void FrameWork::StopLocationService() - { - // reset callback - m_locationObserver.clear(); - m_centeringMode = EDoNothing; - GetLocationManager().StopUpdate(); - m_locationState.TurnOff(); - Invalidate(); - } - - template - bool FrameWork::IsEmptyModel() - { - return m_model.GetWorldRect() == m2::RectD::GetEmptyRect(); - } - - // Cleanup. - template - void FrameWork::Clean() - { - m_model.Clean(); - } - - template - void FrameWork::PrepareToShutdown() - { - if (m_pSearchEngine) - m_pSearchEngine->StopEverything(); - } - - template - void FrameWork::SetMaxWorldRect() - { - m_navigator.SetFromRect(m_model.GetWorldRect()); - } - - template - void FrameWork::Invalidate() - { - m_windowHandle->invalidate(); - } - - template - void FrameWork::SaveState() - { - m_navigator.SaveState(); - } - - template - bool FrameWork::LoadState() - { - if (!m_navigator.LoadState()) - return false; - - return true; - } - //@} - - /// Resize event from window. - template - void FrameWork::OnSize(int w, int h) - { - if (w < 2) w = 2; - if (h < 2) h = 2; - - m_informationDisplay.setDisplayRect(m2::RectI(m2::PointI(0, 0), m2::PointU(w, h))); - - m_navigator.OnSize(0, 0, w, h); - - m_renderPolicy->onSize(w, h); - - if ((m_isBenchmarking) && (!m_isBenchmarkInitialized)) - { - m_isBenchmarkInitialized = true; - InitBenchmark(); - } - } - - template - bool FrameWork::SetUpdatesEnabled(bool doEnable) - { - return m_windowHandle->setUpdatesEnabled(doEnable); - } - - /// respond to device orientation changes - template - void FrameWork::SetOrientation(EOrientation orientation) - { - m_navigator.SetOrientation(orientation); - m_locationState.SetOrientation(orientation); - Invalidate(); - } - - template - double FrameWork::GetCurrentScale() const - { - m2::PointD textureCenter(m_navigator.Screen().PixelRect().Center()); - m2::RectD glbRect; - - unsigned scaleEtalonSize = GetPlatform().ScaleEtalonSize(); - m_navigator.Screen().PtoG(m2::RectD(textureCenter - m2::PointD(scaleEtalonSize / 2, scaleEtalonSize / 2), - textureCenter + m2::PointD(scaleEtalonSize / 2, scaleEtalonSize / 2)), - glbRect); - return scales::GetScaleLevelD(glbRect); - } - - /// Actual rendering function. - template - void FrameWork::PaintImpl(shared_ptr e, - ScreenBase const & screen, - m2::RectD const & selectRect, - int scaleLevel) - { - fwork::DrawProcessor doDraw(selectRect, screen, e, scaleLevel, e->drawer()->screen()->glyphCache()); - - try - { - threads::MutexGuard lock(m_modelSyn); - m_model.ForEachFeatureWithScale(selectRect, bind(ref(doDraw), _1), scaleLevel); - } - catch (redraw_operation_cancelled const &) - { -// m_renderQueue.renderStatePtr()->m_isEmptyModelCurrent = false; -// m_renderQueue.renderStatePtr()->m_isEmptyModelActual = false; - } - - if (m_navigator.Update(m_timer.ElapsedSeconds())) - Invalidate(); - } - - /// Function for calling from platform dependent-paint function. - template - void FrameWork::Paint(shared_ptr e) - { - DrawerYG * pDrawer = e->drawer().get(); - - m_informationDisplay.setScreen(m_navigator.Screen()); - -// m_informationDisplay.setDebugInfo(m_renderQueue.renderState().m_duration, my::rounds(GetCurrentScale())); - - m_informationDisplay.enableRuler(!IsEmptyModel()); - - m2::PointD const center = m_navigator.Screen().ClipRect().Center(); - - m_informationDisplay.setGlobalRect(m_navigator.Screen().GlobalRect()); - m_informationDisplay.setCenter(m2::PointD(MercatorBounds::XToLon(center.x), MercatorBounds::YToLat(center.y))); - -/* if (m_isBenchmarking) - { - m2::PointD const center = m_renderQueue.renderState().m_actualScreen.ClipRect().Center(); - m_informationDisplay.setScreen(m_renderQueue.renderState().m_actualScreen); - m_informationDisplay.setCenter(m2::PointD(MercatorBounds::XToLon(center.x), MercatorBounds::YToLat(center.y))); - - if (!m_isBenchmarkInitialized) - { - e->drawer()->screen()->beginFrame(); - e->drawer()->screen()->clear(m_bgColor); - m_informationDisplay.setDisplayRect(m2::RectI(0, 0, 100, 100)); - m_informationDisplay.enableRuler(false); - m_informationDisplay.doDraw(e->drawer().get()); - e->drawer()->screen()->endFrame(); - } - }*/ - - e->drawer()->screen()->beginFrame(); - - m_renderPolicy->drawFrame(e, m_navigator.Screen()); - - m_informationDisplay.doDraw(pDrawer); - - m_locationState.DrawMyPosition(*pDrawer, m_navigator.Screen()); - - e->drawer()->screen()->endFrame(); - } - - template - void FrameWork::CenterViewport(m2::PointD const & pt) - { - m_navigator.CenterViewport(pt); - Invalidate(); - } - - int const theMetersFactor = 6; - - template - void FrameWork::ShowRect(m2::RectD rect) - { - double const minSizeX = MercatorBounds::ConvertMetresToX(rect.minX(), theMetersFactor * m_metresMinWidth); - double const minSizeY = MercatorBounds::ConvertMetresToY(rect.minY(), theMetersFactor * m_metresMinWidth); - if (rect.SizeX() < minSizeX && rect.SizeY() < minSizeY) - rect.SetSizes(minSizeX, minSizeY); - - m_navigator.SetFromRect(rect); - Invalidate(); - } - - template - void FrameWork::MemoryWarning() - { - // clearing caches on memory warning. - m_model.ClearCaches(); - LOG(LINFO, ("MemoryWarning")); - } - - template - void FrameWork::EnterBackground() - { - // clearing caches on entering background. - m_model.ClearCaches(); - } - - template - void FrameWork::EnterForeground() - { - } - - /// @TODO refactor to accept point and min visible length - template - void FrameWork::CenterAndScaleViewport() - { - m2::PointD const pt = m_locationState.Position(); - m_navigator.CenterViewport(pt); - - m2::RectD clipRect = m_navigator.Screen().ClipRect(); - - double const xMinSize = theMetersFactor * max(m_locationState.ErrorRadius(), - MercatorBounds::ConvertMetresToX(pt.x, m_metresMinWidth)); - double const yMinSize = theMetersFactor * max(m_locationState.ErrorRadius(), - MercatorBounds::ConvertMetresToY(pt.y, m_metresMinWidth)); - - bool needToScale = false; - - if (clipRect.SizeX() < clipRect.SizeY()) - needToScale = clipRect.SizeX() > xMinSize * 3; - else - needToScale = clipRect.SizeY() > yMinSize * 3; - - //if ((ClipRect.SizeX() < 3 * errorRadius) || (ClipRect.SizeY() < 3 * errorRadius)) - // needToScale = true; - - if (needToScale) - { - double const k = max(xMinSize / clipRect.SizeX(), - yMinSize / clipRect.SizeY()); - - clipRect.Scale(k); - m_navigator.SetFromRect(clipRect); - } - - Invalidate(); - } - - /// Show all model by it's world rect. - template - void FrameWork::ShowAll() - { - SetMaxWorldRect(); - Invalidate(); - } - - template - void FrameWork::InvalidateRect(m2::RectD const & rect) - { - if (m_navigator.Screen().GlobalRect().IsIntersect(rect)) - Invalidate(); - } - - /// @name Drag implementation. - //@{ - template - void FrameWork::StartDrag(DragEvent const & e) - { - m2::PointD pos = m_navigator.OrientPoint(e.Pos()); - m_navigator.StartDrag(pos, m_timer.ElapsedSeconds()); - -#ifdef DRAW_TOUCH_POINTS - m_informationDisplay.setDebugPoint(0, pos); -#endif - - Invalidate(); - } - - template - void FrameWork::DoDrag(DragEvent const & e) - { - m_centeringMode = EDoNothing; - - m2::PointD pos = m_navigator.OrientPoint(e.Pos()); - m_navigator.DoDrag(pos, m_timer.ElapsedSeconds()); - -#ifdef DRAW_TOUCH_POINTS - m_informationDisplay.setDebugPoint(0, pos); -#endif - - Invalidate(); - } - - template - void FrameWork::StopDrag(DragEvent const & e) - { - m2::PointD pos = m_navigator.OrientPoint(e.Pos()); - - m_navigator.StopDrag(pos, m_timer.ElapsedSeconds(), true); - -#ifdef DRAW_TOUCH_POINTS - m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); -#endif - - Invalidate(); - } - - template - void FrameWork::Move(double azDir, double factor) - { - m_navigator.Move(azDir, factor); - //m_tiler.seed(m_navigator.Screen(), m_tileSize); - Invalidate(); - } - //@} - - /// @name Scaling. - //@{ - template - void FrameWork::ScaleToPoint(ScaleToPointEvent const & e) - { - m2::PointD const pt = (m_centeringMode == EDoNothing) - ? m_navigator.OrientPoint(e.Pt()) - : m_navigator.Screen().PixelRect().Center(); - - m_navigator.ScaleToPoint(pt, e.ScaleFactor(), m_timer.ElapsedSeconds()); - - Invalidate(); - } - - template - void FrameWork::ScaleDefault(bool enlarge) - { - Scale(enlarge ? 1.5 : 2.0/3.0); - } - - template - void FrameWork::Scale(double scale) - { - m_navigator.Scale(scale); - //m_tiler.seed(m_navigator.Screen(), m_tileSize); - - Invalidate(); - } - - template - void FrameWork::StartScale(ScaleEvent const & e) - { - m2::PointD pt1 = m_navigator.OrientPoint(e.Pt1()); - m2::PointD pt2 = m_navigator.OrientPoint(e.Pt2()); - - if ((m_locationState & location::State::EGps) && (m_centeringMode == ECenterOnly)) - { - m2::PointD ptC = (pt1 + pt2) / 2; - m2::PointD ptDiff = m_navigator.Screen().PixelRect().Center() - ptC; - pt1 += ptDiff; - pt2 += ptDiff; - } - - m_navigator.StartScale(pt1, pt2, m_timer.ElapsedSeconds()); - -#ifdef DRAW_TOUCH_POINTS - m_informationDisplay.setDebugPoint(0, pt1); - m_informationDisplay.setDebugPoint(1, pt2); -#endif - - Invalidate(); - } - - template - void FrameWork::DoScale(ScaleEvent const & e) - { - m2::PointD pt1 = m_navigator.OrientPoint(e.Pt1()); - m2::PointD pt2 = m_navigator.OrientPoint(e.Pt2()); - - if ((m_locationState & location::State::EGps) && (m_centeringMode == ECenterOnly)) - { - m2::PointD ptC = (pt1 + pt2) / 2; - m2::PointD ptDiff = m_navigator.Screen().PixelRect().Center() - ptC; - pt1 += ptDiff; - pt2 += ptDiff; - } - - m_navigator.DoScale(pt1, pt2, m_timer.ElapsedSeconds()); - -#ifdef DRAW_TOUCH_POINTS - m_informationDisplay.setDebugPoint(0, pt1); - m_informationDisplay.setDebugPoint(1, pt2); -#endif - - Invalidate(); - } - - template - void FrameWork::StopScale(ScaleEvent const & e) - { - m2::PointD pt1 = m_navigator.OrientPoint(e.Pt1()); - m2::PointD pt2 = m_navigator.OrientPoint(e.Pt2()); - - if ((m_locationState & location::State::EGps) && (m_centeringMode == ECenterOnly)) - { - m2::PointD ptC = (pt1 + pt2) / 2; - m2::PointD ptDiff = m_navigator.Screen().PixelRect().Center() - ptC; - pt1 += ptDiff; - pt2 += ptDiff; - } - - m_navigator.StopScale(pt1, pt2, m_timer.ElapsedSeconds()); - -#ifdef DRAW_TOUCH_POINTS - m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); - m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); -#endif - - Invalidate(); - } - - template - void FrameWork::Search(string const & text, SearchCallbackT callback) +} + +template +void Framework::initializeGL( + shared_ptr const & primaryContext, + shared_ptr const & resourceManager) +{ + m_renderPolicy->initialize(primaryContext, resourceManager); +} + +template +TModel & Framework::get_model() +{ + return m_model; +} + +template +void Framework::StartLocationService(LocationRetrievedCallbackT observer) +{ + m_locationObserver = observer; + m_centeringMode = ECenterAndScale; + // by default, we always start in accurate mode + GetLocationManager().StartUpdate(true); +} + +template +void Framework::StopLocationService() +{ + // reset callback + m_locationObserver.clear(); + m_centeringMode = EDoNothing; + GetLocationManager().StopUpdate(); + m_locationState.TurnOff(); + Invalidate(); +} + +template +bool Framework::IsEmptyModel() +{ + return m_model.GetWorldRect() == m2::RectD::GetEmptyRect(); +} + +// Cleanup. +template +void Framework::Clean() +{ + m_model.Clean(); +} + +template +void Framework::PrepareToShutdown() +{ + if (m_pSearchEngine) + m_pSearchEngine->StopEverything(); +} + +template +void Framework::SetMaxWorldRect() +{ + m_navigator.SetFromRect(m_model.GetWorldRect()); +} + +template +void Framework::Invalidate() +{ + m_windowHandle->invalidate(); +} + +template +void Framework::SaveState() +{ + m_navigator.SaveState(); +} + +template +bool Framework::LoadState() +{ + if (!m_navigator.LoadState()) + return false; + + return true; +} +//@} + +/// Resize event from window. +template +void Framework::OnSize(int w, int h) +{ + if (w < 2) w = 2; + if (h < 2) h = 2; + + m_informationDisplay.setDisplayRect(m2::RectI(m2::PointI(0, 0), m2::PointU(w, h))); + + m_navigator.OnSize(0, 0, w, h); + + m_renderPolicy->onSize(w, h); +} + +template +bool Framework::SetUpdatesEnabled(bool doEnable) +{ + return m_windowHandle->setUpdatesEnabled(doEnable); +} + +/// respond to device orientation changes +template +void Framework::SetOrientation(EOrientation orientation) +{ + m_navigator.SetOrientation(orientation); + m_locationState.SetOrientation(orientation); + Invalidate(); +} + +template +double Framework::GetCurrentScale() const +{ + m2::PointD textureCenter(m_navigator.Screen().PixelRect().Center()); + m2::RectD glbRect; + + unsigned scaleEtalonSize = GetPlatform().ScaleEtalonSize(); + m_navigator.Screen().PtoG(m2::RectD(textureCenter - m2::PointD(scaleEtalonSize / 2, scaleEtalonSize / 2), + textureCenter + m2::PointD(scaleEtalonSize / 2, scaleEtalonSize / 2)), + glbRect); + return scales::GetScaleLevelD(glbRect); +} + +/// Actual rendering function. +template +void Framework::DrawModel(shared_ptr e, + ScreenBase const & screen, + m2::RectD const & selectRect, + int scaleLevel) +{ + fwork::DrawProcessor doDraw(selectRect, screen, e, scaleLevel, e->drawer()->screen()->glyphCache()); + + try { threads::MutexGuard lock(m_modelSyn); - - if (!m_pSearchEngine.get()) - { - search::CategoriesHolder holder; - string buffer; - ReaderT(GetPlatform().GetReader(SEARCH_CATEGORIES_FILE_NAME)).ReadAsString(buffer); - holder.LoadFromStream(buffer); - m_pSearchEngine.reset(new search::Engine(&m_model.GetIndex(), holder)); - } - - m_pSearchEngine->Search(text, m_navigator.Screen().GlobalRect(), callback); + m_model.ForEachFeatureWithScale(selectRect, bind(ref(doDraw), _1), scaleLevel); + } + catch (redraw_operation_cancelled const &) + { +// m_renderQueue.renderStatePtr()->m_isEmptyModelCurrent = false; +// m_renderQueue.renderStatePtr()->m_isEmptyModelActual = false; } -template class FrameWork; + if (m_navigator.Update(m_timer.ElapsedSeconds())) + Invalidate(); +} + +/// Function for calling from platform dependent-paint function. +template +void Framework::Paint(shared_ptr e) +{ + DrawerYG * pDrawer = e->drawer().get(); + + m_informationDisplay.setScreen(m_navigator.Screen()); + +// m_informationDisplay.setDebugInfo(m_renderQueue.renderState().m_duration, my::rounds(GetCurrentScale())); + + m_informationDisplay.enableRuler(!IsEmptyModel()); + + m2::PointD const center = m_navigator.Screen().ClipRect().Center(); + + m_informationDisplay.setGlobalRect(m_navigator.Screen().GlobalRect()); + m_informationDisplay.setCenter(m2::PointD(MercatorBounds::XToLon(center.x), MercatorBounds::YToLat(center.y))); + + e->drawer()->screen()->beginFrame(); + + m_renderPolicy->drawFrame(e, m_navigator.Screen()); + + m_informationDisplay.doDraw(pDrawer); + + m_locationState.DrawMyPosition(*pDrawer, m_navigator.Screen()); + + e->drawer()->screen()->endFrame(); +} + +template +void Framework::CenterViewport(m2::PointD const & pt) +{ + m_navigator.CenterViewport(pt); + Invalidate(); + } + +int const theMetersFactor = 6; + +template +void Framework::ShowRect(m2::RectD rect) +{ + double const minSizeX = MercatorBounds::ConvertMetresToX(rect.minX(), theMetersFactor * m_metresMinWidth); + double const minSizeY = MercatorBounds::ConvertMetresToY(rect.minY(), theMetersFactor * m_metresMinWidth); + if (rect.SizeX() < minSizeX && rect.SizeY() < minSizeY) + rect.SetSizes(minSizeX, minSizeY); + + m_navigator.SetFromRect(rect); + Invalidate(); +} + +template +void Framework::MemoryWarning() +{ + // clearing caches on memory warning. + m_model.ClearCaches(); + LOG(LINFO, ("MemoryWarning")); +} + +template +void Framework::EnterBackground() +{ + // clearing caches on entering background. + m_model.ClearCaches(); +} + +template +void Framework::EnterForeground() +{ +} + +/// @TODO refactor to accept point and min visible length +template +void Framework::CenterAndScaleViewport() +{ + m2::PointD const pt = m_locationState.Position(); + m_navigator.CenterViewport(pt); + + m2::RectD clipRect = m_navigator.Screen().ClipRect(); + + double const xMinSize = theMetersFactor * max(m_locationState.ErrorRadius(), + MercatorBounds::ConvertMetresToX(pt.x, m_metresMinWidth)); + double const yMinSize = theMetersFactor * max(m_locationState.ErrorRadius(), + MercatorBounds::ConvertMetresToY(pt.y, m_metresMinWidth)); + + bool needToScale = false; + + if (clipRect.SizeX() < clipRect.SizeY()) + needToScale = clipRect.SizeX() > xMinSize * 3; + else + needToScale = clipRect.SizeY() > yMinSize * 3; + + //if ((ClipRect.SizeX() < 3 * errorRadius) || (ClipRect.SizeY() < 3 * errorRadius)) + // needToScale = true; + + if (needToScale) + { + double const k = max(xMinSize / clipRect.SizeX(), + yMinSize / clipRect.SizeY()); + + clipRect.Scale(k); + m_navigator.SetFromRect(clipRect); + } + + Invalidate(); +} + +/// Show all model by it's world rect. +template +void Framework::ShowAll() +{ + SetMaxWorldRect(); + Invalidate(); +} + +template +void Framework::InvalidateRect(m2::RectD const & rect) +{ + if (m_navigator.Screen().GlobalRect().IsIntersect(rect)) + Invalidate(); +} + +/// @name Drag implementation. +///@{ +template +void Framework::StartDrag(DragEvent const & e) +{ + m2::PointD pos = m_navigator.OrientPoint(e.Pos()); + m_navigator.StartDrag(pos, m_timer.ElapsedSeconds()); + +#ifdef DRAW_TOUCH_POINTS + m_informationDisplay.setDebugPoint(0, pos); +#endif + + Invalidate(); +} + +template +void Framework::DoDrag(DragEvent const & e) +{ + m_centeringMode = EDoNothing; + + m2::PointD pos = m_navigator.OrientPoint(e.Pos()); + m_navigator.DoDrag(pos, m_timer.ElapsedSeconds()); + +#ifdef DRAW_TOUCH_POINTS + m_informationDisplay.setDebugPoint(0, pos); +#endif + + Invalidate(); +} + +template +void Framework::StopDrag(DragEvent const & e) +{ + m2::PointD pos = m_navigator.OrientPoint(e.Pos()); + + m_navigator.StopDrag(pos, m_timer.ElapsedSeconds(), true); + +#ifdef DRAW_TOUCH_POINTS + m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); +#endif + + Invalidate(); +} + +template +void Framework::Move(double azDir, double factor) +{ + m_navigator.Move(azDir, factor); + //m_tiler.seed(m_navigator.Screen(), m_tileSize); + Invalidate(); +} +//@} + +/// @name Scaling. +//@{ +template +void Framework::ScaleToPoint(ScaleToPointEvent const & e) +{ + m2::PointD const pt = (m_centeringMode == EDoNothing) + ? m_navigator.OrientPoint(e.Pt()) + : m_navigator.Screen().PixelRect().Center(); + + m_navigator.ScaleToPoint(pt, e.ScaleFactor(), m_timer.ElapsedSeconds()); + + Invalidate(); +} + +template +void Framework::ScaleDefault(bool enlarge) +{ + Scale(enlarge ? 1.5 : 2.0/3.0); +} + +template +void Framework::Scale(double scale) +{ + m_navigator.Scale(scale); + //m_tiler.seed(m_navigator.Screen(), m_tileSize); + + Invalidate(); +} + +template +void Framework::StartScale(ScaleEvent const & e) +{ + m2::PointD pt1 = m_navigator.OrientPoint(e.Pt1()); + m2::PointD pt2 = m_navigator.OrientPoint(e.Pt2()); + + if ((m_locationState & location::State::EGps) && (m_centeringMode == ECenterOnly)) + { + m2::PointD ptC = (pt1 + pt2) / 2; + m2::PointD ptDiff = m_navigator.Screen().PixelRect().Center() - ptC; + pt1 += ptDiff; + pt2 += ptDiff; + } + + m_navigator.StartScale(pt1, pt2, m_timer.ElapsedSeconds()); + +#ifdef DRAW_TOUCH_POINTS + m_informationDisplay.setDebugPoint(0, pt1); + m_informationDisplay.setDebugPoint(1, pt2); +#endif + + Invalidate(); +} + +template +void Framework::DoScale(ScaleEvent const & e) +{ + m2::PointD pt1 = m_navigator.OrientPoint(e.Pt1()); + m2::PointD pt2 = m_navigator.OrientPoint(e.Pt2()); + + if ((m_locationState & location::State::EGps) && (m_centeringMode == ECenterOnly)) + { + m2::PointD ptC = (pt1 + pt2) / 2; + m2::PointD ptDiff = m_navigator.Screen().PixelRect().Center() - ptC; + pt1 += ptDiff; + pt2 += ptDiff; + } + + m_navigator.DoScale(pt1, pt2, m_timer.ElapsedSeconds()); + +#ifdef DRAW_TOUCH_POINTS + m_informationDisplay.setDebugPoint(0, pt1); + m_informationDisplay.setDebugPoint(1, pt2); +#endif + + Invalidate(); +} + +template +void Framework::StopScale(ScaleEvent const & e) +{ + m2::PointD pt1 = m_navigator.OrientPoint(e.Pt1()); + m2::PointD pt2 = m_navigator.OrientPoint(e.Pt2()); + + if ((m_locationState & location::State::EGps) && (m_centeringMode == ECenterOnly)) + { + m2::PointD ptC = (pt1 + pt2) / 2; + m2::PointD ptDiff = m_navigator.Screen().PixelRect().Center() - ptC; + pt1 += ptDiff; + pt2 += ptDiff; + } + + m_navigator.StopScale(pt1, pt2, m_timer.ElapsedSeconds()); + +#ifdef DRAW_TOUCH_POINTS + m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); + m_informationDisplay.setDebugPoint(0, m2::PointD(0, 0)); +#endif + + Invalidate(); +} + +template +void Framework::Search(string const & text, SearchCallbackT callback) +{ + threads::MutexGuard lock(m_modelSyn); + + if (!m_pSearchEngine.get()) + { + search::CategoriesHolder holder; + string buffer; + ReaderT(GetPlatform().GetReader(SEARCH_CATEGORIES_FILE_NAME)).ReadAsString(buffer); + holder.LoadFromStream(buffer); + m_pSearchEngine.reset(new search::Engine(&m_model.GetIndex(), holder)); + } + + m_pSearchEngine->Search(text, m_navigator.Screen().GlobalRect(), callback); +} + +template +void Framework::SetRenderPolicy(shared_ptr const & renderPolicy) +{ + m_renderPolicy = renderPolicy; +} + +template class Framework; diff --git a/map/framework.hpp b/map/framework.hpp index 558c342bea..ed070da5f7 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -43,6 +43,7 @@ #include "../std/shared_ptr.hpp" #include "../std/target_os.hpp" +#include "../search/engine.hpp" //#define DRAW_TOUCH_POINTS @@ -58,15 +59,45 @@ typedef function LocationRetrievedCallbackT; class DrawerYG; class RenderPolicy; +struct PathAppender +{ + string const & m_path; + PathAppender(string const & path) : m_path(path) {} + void operator()(string & elem) + { + elem.insert(elem.begin(), m_path.begin(), m_path.end()); + } +}; + +class ReadersAdder +{ +protected: + + typedef vector maps_list_t; + +private: + Platform & m_pl; + maps_list_t & m_lst; + +public: + ReadersAdder(Platform & pl, maps_list_t & lst) : m_pl(pl), m_lst(lst) {} + + void operator() (string const & name) + { + m_lst.push_back(name); + } +}; + template < class TModel > -class FrameWork +class Framework { +protected: typedef TModel model_t; - typedef FrameWork this_type; + typedef Framework this_type; scoped_ptr m_pSearchEngine; model_t m_model; @@ -75,10 +106,6 @@ class FrameWork shared_ptr m_windowHandle; shared_ptr m_renderPolicy; - bool m_isBenchmarking; - bool m_isBenchmarkInitialized; - - shared_ptr m_resourceManager; InformationDisplay m_informationDisplay; double const m_metresMinWidth; @@ -97,43 +124,10 @@ class FrameWork mutable threads::Mutex m_modelSyn; - double m_maxDuration; - m2::RectD m_maxDurationRect; - m2::RectD m_curBenchmarkRect; - int m_tileSize; - struct BenchmarkResult - { - string m_name; - m2::RectD m_rect; - double m_time; - }; - - vector m_benchmarkResults; - my::Timer m_benchmarksTimer; - string m_startTime; my::Timer m_timer; - struct Benchmark - { - shared_ptr m_provider; - string m_name; - }; - - vector m_benchmarks; - size_t m_curBenchmark; - - yg::Tiler m_tiler; - - void BenchmarkCommandFinished(); -// void NextBenchmarkCommand(); - void SaveBenchmarkResults(); - void SendBenchmarkResults(); - - void MarkBenchmarkResultsStart(); - void MarkBenchmarkResultsEnd(); - typedef typename TModel::ReaderT ReaderT; void AddMap(string const & file); @@ -143,12 +137,11 @@ class FrameWork void OnCompassUpdate(location::CompassInfo const & info); -public: - FrameWork(shared_ptr windowHandle, - size_t bottomShift); - ~FrameWork(); + void SetRenderPolicy(shared_ptr const & rp); - void InitBenchmark(); +public: + Framework(shared_ptr windowHandle, + size_t bottomShift); void initializeGL(shared_ptr const & primaryContext, shared_ptr const & resourceManager); @@ -156,8 +149,7 @@ public: model_t & get_model(); typedef vector maps_list_t; - void EnumLocalMaps(maps_list_t & filesList); - void EnumBenchmarkMaps(maps_list_t & filesList); + virtual void EnumLocalMaps(maps_list_t & filesList); /// Initialization. template @@ -166,16 +158,14 @@ public: m_model.InitClassificator(); typename TStorage::TEnumMapsFunction enumMapsFn; - if (m_isBenchmarking) - enumMapsFn = bind(&FrameWork::EnumBenchmarkMaps, this, _1); - else - enumMapsFn = bind(&FrameWork::EnumLocalMaps, this, _1); + + enumMapsFn = bind(&Framework::EnumLocalMaps, this, _1); LOG(LINFO, ("Initializing storage")); // initializes model with locally downloaded maps - storage.Init(bind(&FrameWork::AddMap, this, _1), - bind(&FrameWork::RemoveMap, this, _1), - bind(&FrameWork::InvalidateRect, this, _1), + storage.Init(bind(&Framework::AddMap, this, _1), + bind(&Framework::RemoveMap, this, _1), + bind(&Framework::InvalidateRect, this, _1), enumMapsFn); LOG(LINFO, ("Storage initialized")); } @@ -192,6 +182,11 @@ public: public: + void DrawModel(shared_ptr e, + ScreenBase const & screen, + m2::RectD const & selectRect, + int scaleLevel); + void Search(string const & text, SearchCallbackT callback); void SetMaxWorldRect(); @@ -203,7 +198,7 @@ public: bool LoadState(); /// Resize event from window. - void OnSize(int w, int h); + virtual void OnSize(int w, int h); bool SetUpdatesEnabled(bool doEnable); @@ -212,16 +207,8 @@ public: double GetCurrentScale() const; - /// Actual rendering function. - /// Called, as the renderQueue processes RenderCommand - /// Usually it happens in the separate thread. - void PaintImpl(shared_ptr e, - ScreenBase const & screen, - m2::RectD const & selectRect, - int scaleLevel); - /// Function for calling from platform dependent-paint function. - void Paint(shared_ptr e); + virtual void Paint(shared_ptr e); void CenterViewport(m2::PointD const & pt); @@ -257,3 +244,4 @@ public: void StopScale(ScaleEvent const & e); //@} }; + diff --git a/map/framework_factory.cpp b/map/framework_factory.cpp new file mode 100644 index 0000000000..372e9cb443 --- /dev/null +++ b/map/framework_factory.cpp @@ -0,0 +1,18 @@ +#include "../base/SRC_FIRST.hpp" + +#include "framework_factory.hpp" +#include "benchmark_framework.hpp" +#include "feature_vec_model.hpp" + +#include "../platform/platform.hpp" + +template +Framework * FrameworkFactory::CreateFramework(shared_ptr const & wh, size_t bottomShift) +{ + if (GetPlatform().IsBenchmarking()) + return new BenchmarkFramework(wh, bottomShift); + else + return new Framework(wh, bottomShift); +} + +template class FrameworkFactory; diff --git a/map/framework_factory.hpp b/map/framework_factory.hpp new file mode 100644 index 0000000000..8446b51f44 --- /dev/null +++ b/map/framework_factory.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "framework.hpp" +#include "../std/shared_ptr.hpp" + +class WindowHandle; + +template +class FrameworkFactory +{ +public: + static Framework * CreateFramework(shared_ptr const & wh, size_t bottomShift); +}; diff --git a/map/map.pro b/map/map.pro index 22b2290602..7e519005e5 100644 --- a/map/map.pro +++ b/map/map.pro @@ -27,7 +27,9 @@ HEADERS += \ render_policy.hpp \ tiling_render_policy_mt.hpp \ tiling_render_policy_st.hpp \ - tiling_render_policy.hpp + tiling_render_policy.hpp \ + benchmark_framework.hpp \ + framework_factory.hpp SOURCES += \ feature_vec_model.cpp \ @@ -44,7 +46,9 @@ SOURCES += \ render_policy.cpp \ tiling_render_policy_mt.cpp \ tiling_render_policy_st.cpp \ - tiling_render_policy.cpp + tiling_render_policy.cpp \ + benchmark_framework.cpp \ + framework_factory.cpp !iphone*:!bada*:!android* { HEADERS += qgl_render_context.hpp diff --git a/map/render_policy.hpp b/map/render_policy.hpp index cdc7ab24c9..70708b22e2 100644 --- a/map/render_policy.hpp +++ b/map/render_policy.hpp @@ -53,5 +53,4 @@ public: /// initialize render policy virtual void initialize(shared_ptr const & primaryContext, shared_ptr const & resourceManager) = 0; - }; diff --git a/platform/platform.cpp b/platform/platform.cpp index 706fd808a0..f24e40765f 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -107,7 +107,7 @@ bool BasePlatformImpl::IsBenchmarking() const bool res = false; (void)Settings::Get("IsBenchmarking", res); -#ifndef OMIM_PRODUCTION +/*#ifndef OMIM_PRODUCTION if (res) { static bool first = true; @@ -118,7 +118,7 @@ bool BasePlatformImpl::IsBenchmarking() const } res = false; } -#endif +#endif*/ return res; } diff --git a/qt/draw_widget.cpp b/qt/draw_widget.cpp index 58b1ddf220..7252f1b0d3 100644 --- a/qt/draw_widget.cpp +++ b/qt/draw_widget.cpp @@ -8,9 +8,9 @@ #include +#include "../map/framework_factory.hpp" #include "../base/start_mem_debug.hpp" - using namespace storage; namespace qt @@ -18,19 +18,19 @@ namespace qt DrawWidget::DrawWidget(QWidget * pParent, Storage & storage) : base_type(pParent), m_handle(new qt::WindowHandle(this)), - m_framework(m_handle, 0), + m_framework(FrameworkFactory::CreateFramework(m_handle, 0)), m_isDrag(false), m_redrawInterval(100), m_pScale(0) { - m_framework.InitStorage(storage); + m_framework->InitStorage(storage); m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(ScaleTimerElapsed())); } void DrawWidget::PrepareShutdown() { - m_framework.PrepareToShutdown(); + m_framework->PrepareToShutdown(); } void DrawWidget::SetScaleControl(QScaleSlider * pScale) @@ -42,7 +42,7 @@ namespace qt void DrawWidget::UpdateNow() { - m_framework.Invalidate(); + m_framework->Invalidate(); } bool DrawWidget::LoadState() @@ -51,9 +51,9 @@ namespace qt if (!Settings::Get("DrawWidgetSize", widthAndHeight)) return false; - m_framework.OnSize(widthAndHeight.first, widthAndHeight.second); + m_framework->OnSize(widthAndHeight.first, widthAndHeight.second); - if (!m_framework.LoadState()) + if (!m_framework->LoadState()) return false; //m_framework.UpdateNow(); @@ -66,7 +66,7 @@ namespace qt pair widthAndHeight(width(), height()); Settings::Set("DrawWidgetSize", widthAndHeight); - m_framework.SaveState(); + m_framework->SaveState(); } //void DrawWidget::ShowFeature(Feature const & p) @@ -76,76 +76,76 @@ namespace qt void DrawWidget::OnEnableMyPosition(LocationRetrievedCallbackT observer) { - m_framework.StartLocationService(observer); + m_framework->StartLocationService(observer); } void DrawWidget::OnDisableMyPosition() { - m_framework.StopLocationService(); + m_framework->StopLocationService(); } void DrawWidget::MoveLeft() { - m_framework.Move(math::pi, 0.5); + m_framework->Move(math::pi, 0.5); emit ViewportChanged(); } void DrawWidget::MoveRight() { - m_framework.Move(0.0, 0.5); + m_framework->Move(0.0, 0.5); emit ViewportChanged(); } void DrawWidget::MoveUp() { - m_framework.Move(math::pi/2.0, 0.5); + m_framework->Move(math::pi/2.0, 0.5); emit ViewportChanged(); } void DrawWidget::MoveDown() { - m_framework.Move(-math::pi/2.0, 0.5); + m_framework->Move(-math::pi/2.0, 0.5); emit ViewportChanged(); } void DrawWidget::ScalePlus() { - m_framework.Scale(2.0); + m_framework->Scale(2.0); UpdateScaleControl(); emit ViewportChanged(); } void DrawWidget::ScaleMinus() { - m_framework.Scale(0.5); + m_framework->Scale(0.5); UpdateScaleControl(); emit ViewportChanged(); } void DrawWidget::ScalePlusLight() { - m_framework.Scale(1.5); + m_framework->Scale(1.5); UpdateScaleControl(); emit ViewportChanged(); } void DrawWidget::ScaleMinusLight() { - m_framework.Scale(2.0/3.0); + m_framework->Scale(2.0/3.0); UpdateScaleControl(); emit ViewportChanged(); } void DrawWidget::ShowAll() { - m_framework.ShowAll(); + m_framework->ShowAll(); UpdateScaleControl(); emit ViewportChanged(); } void DrawWidget::Repaint() { - m_framework.Invalidate(); + m_framework->Invalidate(); } void DrawWidget::ScaleChanged(int action) @@ -155,7 +155,7 @@ namespace qt double const factor = m_pScale->GetScaleFactor(); if (factor != 1.0) { - m_framework.Scale(factor); + m_framework->Scale(factor); emit ViewportChanged(); } } @@ -166,19 +166,19 @@ namespace qt widget_type::initializeGL(); m_handle->setRenderContext(renderContext()); //m_handle->setDrawer(GetDrawer()); - m_framework.initializeGL(renderContext(), resourceManager()); + m_framework->initializeGL(renderContext(), resourceManager()); } void DrawWidget::DoDraw(shared_ptr p) { shared_ptr paintEvent(new PaintEvent(p)); - m_framework.Paint(paintEvent); + m_framework->Paint(paintEvent); } void DrawWidget::DoResize(int w, int h) { - m_framework.OnSize(w, h); - m_framework.Invalidate(); + m_framework->OnSize(w, h); + m_framework->Invalidate(); UpdateScaleControl(); emit ViewportChanged(); } @@ -198,7 +198,7 @@ namespace qt if (e->button() == Qt::LeftButton) { - m_framework.StartDrag(get_drag_event(e)); + m_framework->StartDrag(get_drag_event(e)); setCursor(Qt::CrossCursor); m_isDrag = true; @@ -213,7 +213,7 @@ namespace qt { StopDragging(e); - m_framework.ScaleToPoint(ScaleToPointEvent(e->pos().x(), e->pos().y(), 1.5)); + m_framework->ScaleToPoint(ScaleToPointEvent(e->pos().x(), e->pos().y(), 1.5)); UpdateScaleControl(); emit ViewportChanged(); @@ -225,7 +225,7 @@ namespace qt base_type::mouseMoveEvent(e); if (m_isDrag) - m_framework.DoDrag(get_drag_event(e)); + m_framework->DoDrag(get_drag_event(e)); } void DrawWidget::mouseReleaseEvent(QMouseEvent * e) @@ -240,7 +240,7 @@ namespace qt { if (m_isDrag && e->button() == Qt::LeftButton) { - m_framework.StopDrag(get_drag_event(e)); + m_framework->StopDrag(get_drag_event(e)); setCursor(Qt::ArrowCursor); m_isDrag = false; @@ -261,8 +261,8 @@ namespace qt m_timer->stop(); m_timer->start(m_redrawInterval); - //m_framework.Scale(exp(e->delta() / 360.0)); - m_framework.ScaleToPoint(ScaleToPointEvent(e->pos().x(), e->pos().y(), exp(e->delta() / 360.0))); + //m_framework->Scale(exp(e->delta() / 360.0)); + m_framework->ScaleToPoint(ScaleToPointEvent(e->pos().x(), e->pos().y(), exp(e->delta() / 360.0))); UpdateScaleControl(); emit ViewportChanged(); } @@ -273,18 +273,18 @@ namespace qt if (m_pScale) { // don't send ScaleChanged - m_pScale->SetPosWithBlockedSignals(m_framework.GetCurrentScale()); + m_pScale->SetPosWithBlockedSignals(m_framework->GetCurrentScale()); } } void DrawWidget::Search(const string & text, SearchCallbackT callback) { - m_framework.Search(text, callback); + m_framework->Search(text, callback); } void DrawWidget::ShowFeature(m2::RectD const & rect) { - m_framework.ShowRect(rect); + m_framework->ShowRect(rect); UpdateScaleControl(); } } diff --git a/qt/draw_widget.hpp b/qt/draw_widget.hpp index ce50b65ddc..40daa385ac 100644 --- a/qt/draw_widget.hpp +++ b/qt/draw_widget.hpp @@ -7,6 +7,8 @@ #include "../map/framework.hpp" #include "../map/navigator.hpp" +#include "../std/auto_ptr.hpp" + #include //class FileReader; @@ -30,7 +32,7 @@ namespace qt typedef model::FeaturesFetcher model_t; - FrameWork m_framework; + auto_ptr > m_framework; bool m_isDrag;