1 /******************************************************************************
2  *
3  * Project:  OpenCPN
4  *
5  ***************************************************************************
6  *   Copyright (C) 2013 by David S. Register                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
22  ***************************************************************************
23  */
24 
25 #ifndef __GLTEXTCACHE_H__
26 #define __GLTEXTCACHE_H__
27 
28 #include <wx/glcanvas.h>
29 #include <wx/ffile.h>
30 #include <wx/timer.h>
31 #include <stdint.h>
32 
33 #include "ocpn_types.h"
34 #include "bbox.h"
35 
36 class glTextureDescriptor;
37 
38 #define COMPRESSED_CACHE_MAGIC 0xf013  // change this when the format changes
39 
40 #define FACTORY_TIMER                   10000
41 
42 void HalfScaleChartBits( int width, int height, unsigned char *source, unsigned char *target );
43 
44 class ChartBaseBSB;
45 class ChartPlugInWrapper;
46 
47 struct CompressedCacheHeader
48 {
49     uint32_t magic;
50     uint32_t format;
51     uint32_t chartdate;
52     uint32_t m_nentries;
53     uint32_t catalog_offset;
54     uint32_t chartfile_date;
55     uint32_t chartfile_size;
56 };
57 
58 struct CatalogEntryKey
59 {
60     int         mip_level;
61     ColorScheme tcolorscheme;
62     int         x;
63     int         y;
64 };
65 
66 struct CatalogEntryValue
67 {
68     int         texture_offset;
69     uint32_t    compressed_size;
70 };
71 
72 #define CATALOG_ENTRY_SERIAL_SIZE 6 * sizeof(uint32_t)
73 
74 class CatalogEntry
75 {
76 public:
77     CatalogEntry();
78     ~CatalogEntry();
79     CatalogEntry(int level, int x0, int y0, ColorScheme colorscheme);
80     int GetSerialSize();
81     void Serialize(unsigned char *);
82     void DeSerialize(unsigned char *);
83     CatalogEntryKey k;
84     CatalogEntryValue v;
85 
86 };
87 
88 WX_DEFINE_ARRAY(CatalogEntry*, ArrayOfCatalogEntries);
89 
90 class glTexTile
91 {
92 public:
glTexTile()93     glTexTile() { m_coords = m_texcoords = NULL;  m_ncoords = 0;}
~glTexTile()94     virtual ~glTexTile() { delete [] m_coords; delete [] m_texcoords; }
95 
96     wxRect rect;
97     LLBBox box;
98 //    LLRegion region;
99 
100     int m_ncoords;
101     float *m_coords, *m_texcoords;
102 };
103 
104 #define MAX_TEX_LEVEL 10
105 
106 class glTexFactory
107 {
108 public:
109     glTexFactory(ChartBase *chart, int raster_format);
110     ~glTexFactory();
111 
112     glTextureDescriptor *GetOrCreateTD(const wxRect &rect);
113     bool BuildTexture(glTextureDescriptor *ptd, int base_level, const wxRect &rect);
114     bool PrepareTexture( int base_level, const wxRect &rect, ColorScheme color_scheme, int mem_used );
115     int GetTextureLevel( glTextureDescriptor *ptd, const wxRect &rect, int level,  ColorScheme color_scheme );
116     bool UpdateCacheAllLevels( const wxRect &rect, ColorScheme color_scheme, unsigned char **compcomp_array, int *compcomp_size);
117     bool IsLevelInCache( int level, const wxRect &rect, ColorScheme color_scheme );
GetChartPath()118     wxString GetChartPath(){ return m_ChartPath; }
GetHashKey()119     wxString GetHashKey(){ return m_HashKey; }
SetHashKey(wxString key)120     void SetHashKey( wxString key ){ m_HashKey = key; }
121     bool OnTimer();
122     void AccumulateMemStatistics(int &map_size, int &comp_size, int &compcomp_size);
123     void DeleteTexture(const wxRect &rect);
124     void DeleteAllTextures( void );
125     void DeleteSomeTextures( long target );
126     void DeleteAllDescriptors( void );
127     bool BackgroundCompressionAsJob() const;
128     void PurgeBackgroundCompressionPool();
SetLRUTime(int lru)129     void SetLRUTime(int lru) { m_LRUtime = lru; }
GetLRUTime()130     int	 GetLRUTime() { return m_LRUtime; }
131     void FreeSome( long target );
132     void FreeIfCached();
133 
134     glTextureDescriptor *GetpTD( wxRect & rect );
135 
136     void PrepareTiles(const ViewPort &vp, bool use_norm_vp, ChartBase *pChart);
GetTiles(int & num)137     glTexTile** GetTiles(int &num) { num = m_ntex; return m_tiles; }
GetCenter(double & lat,double & lon)138     void GetCenter(double &lat, double &lon) { lat = m_clat, lon = m_clon; }
139 
140 private:
141     bool LoadCatalog(void);
142     bool LoadHeader(void);
143     bool WriteCatalogAndHeader();
144 
145     bool UpdateCachePrecomp(unsigned char *data, int data_size, const wxRect &rect, int level,
146                                           ColorScheme color_scheme, bool write_catalog = true);
147     bool UpdateCacheLevel( const wxRect &rect, int level, ColorScheme color_scheme, unsigned char *data, int size);
148 
149     void DeleteSingleTexture( glTextureDescriptor *ptd );
150 
151     CatalogEntryValue *GetCacheEntryValue(int level, int x, int y, ColorScheme color_scheme);
152     bool AddCacheEntryValue(const CatalogEntry &p);
ArrayIndex(int x,int y)153     int  ArrayIndex(int x, int y) const { return ((y / m_tex_dim) * m_stride) + (x / m_tex_dim); }
154     void  ArrayXY(wxRect *r, int index) const;
155 
156     int         n_catalog_entries;
157 
158     CatalogEntryValue *m_cache[N_COLOR_SCHEMES][MAX_TEX_LEVEL];
159 
160 
161 
162     wxString    m_ChartPath;
163     wxString    m_HashKey;
164     wxString    m_CompressedCacheFilePath;
165 
166     int         m_catalog_offset;
167     bool        m_hdrOK;
168     bool        m_catalogOK;
169     bool        m_newCatalog;
170 
171     bool	m_catalogCorrupted;
172 
173     wxFFile     *m_fs;
174     uint32_t    m_chart_date_binary;
175     uint32_t    m_chartfile_date_binary;
176     uint32_t    m_chartfile_size;
177 
178     int         m_stride;
179     int         m_ntex;
180     int         m_tex_dim;
181     int         m_size_X;
182     int         m_size_Y;
183     int         m_nx_tex;
184     int         m_ny_tex;
185 
186     int		m_LRUtime;
187 
188     glTextureDescriptor  **m_td_array;
189 
190     double m_clat, m_clon;
191     glTexTile **m_tiles;
192     int m_prepared_projection_type;
193     bool m_north; // used for polar projection
194 
195 };
196 
197 
198 #endif
199