OpenCPN Partial API docs
glTexCache.h
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 "model/ocpn_types.h"
34 #include "color_types.h"
35 #include "bbox.h"
36 #include "viewport.h"
37 
39 
40 #define COMPRESSED_CACHE_MAGIC 0xf013 // change this when the format changes
41 
42 #define FACTORY_TIMER 10000
43 
44 void HalfScaleChartBits(int width, int height, unsigned char *source,
45  unsigned char *target);
46 
47 class ChartBaseBSB;
48 class ChartPlugInWrapper;
49 
51  uint32_t magic;
52  uint32_t format;
53  uint32_t chartdate;
54  uint32_t m_nentries;
55  uint32_t catalog_offset;
56  uint32_t chartfile_date;
57  uint32_t chartfile_size;
58 };
59 
61  int mip_level;
62  ColorScheme tcolorscheme;
63  int x;
64  int y;
65 };
66 
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 public:
76  CatalogEntry();
77  ~CatalogEntry();
78  CatalogEntry(int level, int x0, int y0, ColorScheme colorscheme);
79  int GetSerialSize();
80  void Serialize(unsigned char *);
81  void DeSerialize(unsigned char *);
84 };
85 
86 WX_DEFINE_ARRAY(CatalogEntry *, ArrayOfCatalogEntries);
87 
88 class glTexTile {
89 public:
90  glTexTile() {
91  m_coords = m_texcoords = NULL;
92  m_ncoords = 0;
93  }
94  virtual ~glTexTile() {
95  delete[] m_coords;
96  delete[] m_texcoords;
97  }
98 
99  wxRect rect;
100  LLBBox box;
101  // LLRegion region;
102 
103  int m_ncoords;
104  float *m_coords, *m_texcoords;
105 };
106 
107 #define MAX_TEX_LEVEL 10
108 
110 public:
111  glTexFactory(ChartBase *chart, int raster_format);
112  ~glTexFactory();
113 
114  glTextureDescriptor *GetOrCreateTD(const wxRect &rect);
115  bool BuildTexture(glTextureDescriptor *ptd, int base_level,
116  const wxRect &rect);
117  bool PrepareTexture(int base_level, const wxRect &rect,
118  ColorScheme color_scheme, int mem_used);
119  int GetTextureLevel(glTextureDescriptor *ptd, const wxRect &rect, int level,
120  ColorScheme color_scheme);
121  bool UpdateCacheAllLevels(const wxRect &rect, ColorScheme color_scheme,
122  unsigned char **compcomp_array, int *compcomp_size);
123  bool IsLevelInCache(int level, const wxRect &rect, ColorScheme color_scheme);
124  wxString GetChartPath() { return m_ChartPath; }
125  wxString GetHashKey() { return m_HashKey; }
126  void SetHashKey(wxString key) { m_HashKey = key; }
127  bool OnTimer();
128  void AccumulateMemStatistics(int &map_size, int &comp_size,
129  int &compcomp_size);
130  void DeleteTexture(const wxRect &rect);
131  void DeleteAllTextures(void);
132  void DeleteSomeTextures(long target);
133  void DeleteAllDescriptors(void);
134  bool BackgroundCompressionAsJob() const;
135  void PurgeBackgroundCompressionPool();
136  void SetLRUTime(int lru) { m_LRUtime = lru; }
137  int GetLRUTime() { return m_LRUtime; }
138  void FreeSome(long target);
139  void FreeIfCached();
140 
141  glTextureDescriptor *GetpTD(wxRect &rect);
142 
143  void PrepareTiles(const ViewPort &vp, bool use_norm_vp, ChartBase *pChart);
144  glTexTile **GetTiles(int &num) {
145  num = m_ntex;
146  return m_tiles;
147  }
148  void GetCenter(double &lat, double &lon) { lat = m_clat, lon = m_clon; }
149 
150 private:
151  bool LoadCatalog(void);
152  bool LoadHeader(void);
153  bool WriteCatalogAndHeader();
154 
155  bool UpdateCachePrecomp(unsigned char *data, int data_size,
156  const wxRect &rect, int level,
157  ColorScheme color_scheme, bool write_catalog = true);
158  bool UpdateCacheLevel(const wxRect &rect, int level, ColorScheme color_scheme,
159  unsigned char *data, int size);
160 
161  void DeleteSingleTexture(glTextureDescriptor *ptd);
162 
163  CatalogEntryValue *GetCacheEntryValue(int level, int x, int y,
164  ColorScheme color_scheme);
165  bool AddCacheEntryValue(const CatalogEntry &p);
166  int ArrayIndex(int x, int y) const {
167  return ((y / m_tex_dim) * m_stride) + (x / m_tex_dim);
168  }
169  void ArrayXY(wxRect *r, int index) const;
170 
171  int n_catalog_entries;
172 
173  CatalogEntryValue *m_cache[N_COLOR_SCHEMES][MAX_TEX_LEVEL];
174 
175  wxString m_ChartPath;
176  wxString m_HashKey;
177  wxString m_CompressedCacheFilePath;
178 
179  int m_catalog_offset;
180  bool m_hdrOK;
181  bool m_catalogOK;
182  bool m_newCatalog;
183 
184  bool m_catalogCorrupted;
185 
186  wxFFile *m_fs;
187  uint32_t m_chart_date_binary;
188  uint32_t m_chartfile_date_binary;
189  uint32_t m_chartfile_size;
190 
191  int m_stride;
192  int m_ntex;
193  int m_tex_dim;
194  int m_size_X;
195  int m_size_Y;
196  int m_nx_tex;
197  int m_ny_tex;
198 
199  int m_LRUtime;
200 
201  glTextureDescriptor **m_td_array;
202 
203  double m_clat, m_clon;
204  glTexTile **m_tiles;
205  int m_prepared_projection_type;
206  bool m_north; // used for polar projection
207 };
208 
209 #endif