ScummVM API documentation
texture.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef BACKENDS_GRAPHICS3D_ANDROID_TEXTURE_H
23 #define BACKENDS_GRAPHICS3D_ANDROID_TEXTURE_H
24 
25 #define GL_GLEXT_PROTOTYPES
26 #include <GLES/gl.h>
27 
28 #include "graphics/surface.h"
29 #include "graphics/pixelformat.h"
30 
31 #include "common/rect.h"
32 #include "common/array.h"
33 
34 namespace OpenGL {
35 class Shader;
36 }
37 
39 public:
40  static void initGL();
41  static void unbindShader();
42 
43 protected:
44  GLESBaseTexture(GLenum glFormat, GLenum glType,
45  Graphics::PixelFormat pixelFormat);
46 
47 public:
48  virtual ~GLESBaseTexture();
49 
50  void release();
51  void reinit();
52 
53  void setLinearFilter(bool value);
54 
55  virtual void allocBuffer(GLuint w, GLuint h);
56 
57  virtual void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
58  const void *buf, int pitch_buf) = 0;
59  virtual void fillBuffer(uint32 color) = 0;
60 
61  void drawTexture(GLshort x, GLshort y, GLshort w, GLshort h) {
62  drawTexture(x, y, w, h, Common::Rect(0, 0, width(), height()));
63  }
64  void drawTexture(GLshort x, GLshort y, GLshort w, GLshort h, const Common::Rect &clip);
65 
66  inline void setDrawRect(const Common::Rect &rect) {
67  _draw_rect = rect;
68  }
69 
70  inline void setDrawRect(int16 w, int16 h) {
71  _draw_rect = Common::Rect(w, h);
72  }
73 
74  inline void setDrawRect(int16 x1, int16 y1, int16 x2, int16 y2) {
75  _draw_rect = Common::Rect(x1, y1, x2, y2);
76  }
77 
78  inline const Common::Rect &getDrawRect() const {
79  return _draw_rect;
80  }
81 
82  inline void drawTextureRect() {
83  drawTexture(_draw_rect.left, _draw_rect.top,
84  _draw_rect.width(), _draw_rect.height());
85  }
86 
87  inline void drawTextureOrigin() {
88  drawTexture(0, 0, _surface.w, _surface.h);
89  }
90 
91  inline GLuint width() const {
92  return _surface.w;
93  }
94 
95  inline GLuint height() const {
96  return _surface.h;
97  }
98 
99  inline GLuint texWidth() const {
100  return _texture_width;
101  }
102 
103  inline GLuint texHeight() const {
104  return _texture_height;
105  }
106 
107  inline uint16 pitch() const {
108  return _surface.pitch;
109  }
110 
111  inline bool isEmpty() const {
112  return _surface.w == 0 || _surface.h == 0;
113  }
114 
115  inline const Graphics::Surface *surface_const() const {
116  return &_surface;
117  }
118 
119  inline Graphics::Surface *surface() {
120  setDirty();
121  return &_surface;
122  }
123 
124  virtual void setPalette(const byte *colors, uint start, uint num) = 0;
125  virtual void setKeycolor(byte color) = 0;
126  virtual void grabPalette(byte *colors, uint start, uint num) const = 0;
127 
128  inline bool hasPalette() const {
129  return _palettePixelFormat.bytesPerPixel > 0;
130  }
131 
132  inline bool dirty() const {
133  return _all_dirty || !_dirty_rect.isEmpty();
134  }
135 
136  virtual const Graphics::PixelFormat &getPixelFormat() const;
137 
138  inline const Graphics::PixelFormat &getPalettePixelFormat() const {
139  return _palettePixelFormat;
140  }
141 
142  GLuint getTextureName() const {
143  return _texture_name;
144  }
145 
146  void setGameTexture() {
147  _is_game_texture = true;
148  }
149 
150 protected:
151  void initSize();
152 
153  virtual void *prepareTextureBuffer(const Common::Rect &rect) = 0;
154 
155  inline void setDirty() {
156  _all_dirty = true;
157  }
158 
159  inline void clearDirty() {
160  _all_dirty = false;
161  _dirty_rect.top = 0;
162  _dirty_rect.left = 0;
163  _dirty_rect.bottom = 0;
164  _dirty_rect.right = 0;
165  }
166 
167  inline void setDirtyRect(const Common::Rect &r) {
168  if (!_all_dirty) {
169  if (_dirty_rect.isEmpty()) {
170  _dirty_rect = r;
171  } else {
172  _dirty_rect.extend(r);
173  }
174  }
175  }
176 
177  GLenum _glFormat;
178  GLenum _glType;
179  GLint _glFilter;
180 
181  GLuint _texture_name;
182  Graphics::Surface _surface;
183  GLuint _texture_width;
184  GLuint _texture_height;
185 
186  Common::Rect _draw_rect;
187 
188  bool _all_dirty;
189  Common::Rect _dirty_rect;
190 
191  Graphics::PixelFormat _pixelFormat;
192  Graphics::PixelFormat _palettePixelFormat;
193 
194  bool _is_game_texture;
195 
196  static bool _npot_supported;
197  static OpenGL::Shader *_box_shader;
198  static GLuint _verticesVBO;
199 
200 };
201 
202 class GLESTexture : public GLESBaseTexture {
203 protected:
204  GLESTexture(GLenum glFormat, GLenum glType,
205  Graphics::PixelFormat pixelFormat);
206 
207 public:
208  virtual ~GLESTexture();
209 
210  void allocBuffer(GLuint w, GLuint h) override;
211 
212  void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
213  const void *buf, int pitch_buf) override;
214  void fillBuffer(uint32 color) override;
215 
216  void setPalette(const byte *colors, uint start, uint num) override {}
217  void setKeycolor(byte color) override {};
218  void grabPalette(byte *colors, uint start, uint num) const override {}
219 
220  void readPixels();
221 
222 protected:
223  void *prepareTextureBuffer(const Common::Rect &rect) override;
224 
225  byte *_pixels;
226  byte *_buf;
227 };
228 
229 // RGBA4444 texture
230 class GLES4444Texture : public GLESTexture {
231 public:
232  GLES4444Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixelFormat()) {}
233  virtual ~GLES4444Texture() {}
234 
235  static Graphics::PixelFormat pixelFormat() {
236  return Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0);
237  }
238 };
239 
240 // RGBA5551 texture
241 class GLES5551Texture : public GLESTexture {
242 public:
243  GLES5551Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, pixelFormat()) {}
244  virtual ~GLES5551Texture() {}
245 
246  static inline Graphics::PixelFormat pixelFormat() {
247  return Graphics::PixelFormat(2, 5, 5, 5, 1, 11, 6, 1, 0);
248  }
249 };
250 
251 // RGB565 texture
252 class GLES565Texture : public GLESTexture {
253 public:
254  GLES565Texture() : GLESTexture(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixelFormat()) {}
255  virtual ~GLES565Texture() {}
256 
257  static inline Graphics::PixelFormat pixelFormat() {
258  return Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
259  }
260 };
261 
262 class GLES888Texture : public GLESTexture {
263 public:
264  GLES888Texture() : GLESTexture(GL_RGB, GL_UNSIGNED_BYTE, pixelFormat()) {}
265  virtual ~GLES888Texture() {}
266 
267  static Graphics::PixelFormat pixelFormat() {
268 #ifdef SCUMM_BIG_ENDIAN
269  return Graphics::PixelFormat(3, 8, 8, 8, 0, 16, 8, 0, 0);
270 #else
271  return Graphics::PixelFormat(3, 8, 8, 8, 0, 0, 8, 16, 0);
272 #endif
273  }
274 };
275 
276 class GLES8888Texture : public GLESTexture {
277 public:
278  GLES8888Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_BYTE, pixelFormat()) {}
279  virtual ~GLES8888Texture() {}
280 
281  static Graphics::PixelFormat pixelFormat() {
282 #ifdef SCUMM_BIG_ENDIAN
283  return Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
284 #else
285  return Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
286 #endif
287  }
288 };
289 
291 protected:
292  GLESFakePaletteTexture(GLenum glFormat, GLenum glType,
293  Graphics::PixelFormat pixelFormat);
294 
295 public:
296  virtual ~GLESFakePaletteTexture();
297 
298  void allocBuffer(GLuint w, GLuint h) override;
299  void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
300  const void *buf, int pitch_buf) override;
301  void fillBuffer(uint32 color) override;
302 
303  const Graphics::PixelFormat &getPixelFormat() const override {
304  return _fake_format;
305  }
306 
307 protected:
308  Graphics::PixelFormat _fake_format;
309  byte *_pixels;
310 };
311 
313 protected:
314  GLESFakePalette16Texture(GLenum glFormat, GLenum glType,
315  Graphics::PixelFormat pixelFormat);
316 public:
317  virtual ~GLESFakePalette16Texture();
318 
319  void allocBuffer(GLuint w, GLuint h) override;
320 
321  void setPalette(const byte *colors, uint start, uint num) override;
322  void grabPalette(byte *colors, uint start, uint num) const override;
323 
324 protected:
325  void *prepareTextureBuffer(const Common::Rect &rect) override;
326 
327  uint16 *_palette;
328  uint16 *_buf;
329 };
330 
332 public:
334  virtual ~GLESFakePalette565Texture() {}
335 
336  void setKeycolor(byte color) override {};
337 };
338 
340 public:
342  virtual ~GLESFakePalette5551Texture() {}
343 
344  void setKeycolor(byte color) override;
345 
346 protected:
347  byte _keycolor;
348 };
349 
351 public:
353  virtual ~GLESFakePalette888Texture();
354 
355  void allocBuffer(GLuint w, GLuint h) override;
356 
357  void setPalette(const byte *colors, uint start, uint num) override;
358  void setKeycolor(byte color) override {};
359  void grabPalette(byte *colors, uint start, uint num) const override;
360 
361 protected:
362  void *prepareTextureBuffer(const Common::Rect &rect) override;
363 
364  byte *_palette;
365  byte *_buf;
366 };
367 
369 public:
371  virtual ~GLESFakePalette8888Texture();
372 
373  void allocBuffer(GLuint w, GLuint h) override;
374 
375  void setPalette(const byte *colors, uint start, uint num) override;
376  void setKeycolor(byte color) override;
377  void grabPalette(byte *colors, uint start, uint num) const override;
378 
379 protected:
380  void *prepareTextureBuffer(const Common::Rect &rect) override;
381 
382  uint32 *_palette;
383  uint32 *_buf;
384  byte _keycolor;
385 };
386 
387 #endif
Definition: texture.h:202
Definition: surface.h:67
Definition: texture.h:339
void extend(const Rect &r)
Definition: rect.h:278
Definition: pixelformat.h:138
Definition: rect.h:144
Definition: texture.h:331
Definition: texture.h:368
Definition: shader.h:56
Definition: texture.h:230
Definition: texture.h:312
Definition: texture.h:241
Definition: renderbuffer.h:27
Definition: texture.h:276
Definition: texture.h:252
Definition: texture.h:350
Definition: texture.h:290
Definition: texture.h:262
Definition: texture.h:38