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  void setAlpha(float alpha) {
151  _alpha = alpha;
152  }
153 
154 protected:
155  void initSize();
156 
157  virtual void *prepareTextureBuffer(const Common::Rect &rect) = 0;
158 
159  inline void setDirty() {
160  _all_dirty = true;
161  }
162 
163  inline void clearDirty() {
164  _all_dirty = false;
165  _dirty_rect.top = 0;
166  _dirty_rect.left = 0;
167  _dirty_rect.bottom = 0;
168  _dirty_rect.right = 0;
169  }
170 
171  inline void setDirtyRect(const Common::Rect &r) {
172  if (!_all_dirty) {
173  if (_dirty_rect.isEmpty()) {
174  _dirty_rect = r;
175  } else {
176  _dirty_rect.extend(r);
177  }
178  }
179  }
180 
181  GLenum _glFormat;
182  GLenum _glType;
183  GLint _glFilter;
184 
185  GLuint _texture_name;
186  Graphics::Surface _surface;
187  GLuint _texture_width;
188  GLuint _texture_height;
189 
190  Common::Rect _draw_rect;
191 
192  bool _all_dirty;
193  Common::Rect _dirty_rect;
194 
195  Graphics::PixelFormat _pixelFormat;
196  Graphics::PixelFormat _palettePixelFormat;
197 
198  bool _is_game_texture;
199 
200  GLfloat _alpha;
201 
202  static bool _npot_supported;
203  static OpenGL::Shader *_box_shader;
204  static GLuint _verticesVBO;
205 
206 };
207 
208 class GLESTexture : public GLESBaseTexture {
209 protected:
210  GLESTexture(GLenum glFormat, GLenum glType,
211  Graphics::PixelFormat pixelFormat);
212 
213 public:
214  virtual ~GLESTexture();
215 
216  void allocBuffer(GLuint w, GLuint h) override;
217 
218  void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
219  const void *buf, int pitch_buf) override;
220  void fillBuffer(uint32 color) override;
221 
222  void setPalette(const byte *colors, uint start, uint num) override {}
223  void setKeycolor(byte color) override {};
224  void grabPalette(byte *colors, uint start, uint num) const override {}
225 
226  void readPixels();
227 
228 protected:
229  void *prepareTextureBuffer(const Common::Rect &rect) override;
230 
231  byte *_pixels;
232  byte *_buf;
233 };
234 
235 // RGBA4444 texture
236 class GLES4444Texture : public GLESTexture {
237 public:
238  GLES4444Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixelFormat()) {}
239  virtual ~GLES4444Texture() {}
240 
241  static Graphics::PixelFormat pixelFormat() {
242  return Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0);
243  }
244 };
245 
246 // RGBA5551 texture
247 class GLES5551Texture : public GLESTexture {
248 public:
249  GLES5551Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, pixelFormat()) {}
250  virtual ~GLES5551Texture() {}
251 
252  static inline Graphics::PixelFormat pixelFormat() {
253  return Graphics::PixelFormat(2, 5, 5, 5, 1, 11, 6, 1, 0);
254  }
255 };
256 
257 // RGB565 texture
258 class GLES565Texture : public GLESTexture {
259 public:
260  GLES565Texture() : GLESTexture(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixelFormat()) {}
261  virtual ~GLES565Texture() {}
262 
263  static inline Graphics::PixelFormat pixelFormat() {
264  return Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
265  }
266 };
267 
268 class GLES888Texture : public GLESTexture {
269 public:
270  GLES888Texture() : GLESTexture(GL_RGB, GL_UNSIGNED_BYTE, pixelFormat()) {}
271  virtual ~GLES888Texture() {}
272 
273  static Graphics::PixelFormat pixelFormat() {
274 #ifdef SCUMM_BIG_ENDIAN
275  return Graphics::PixelFormat(3, 8, 8, 8, 0, 16, 8, 0, 0);
276 #else
277  return Graphics::PixelFormat(3, 8, 8, 8, 0, 0, 8, 16, 0);
278 #endif
279  }
280 };
281 
282 class GLES8888Texture : public GLESTexture {
283 public:
284  GLES8888Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_BYTE, pixelFormat()) {}
285  virtual ~GLES8888Texture() {}
286 
287  static Graphics::PixelFormat pixelFormat() {
288 #ifdef SCUMM_BIG_ENDIAN
289  return Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
290 #else
291  return Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
292 #endif
293  }
294 };
295 
297 protected:
298  GLESFakePaletteTexture(GLenum glFormat, GLenum glType,
299  Graphics::PixelFormat pixelFormat);
300 
301 public:
302  virtual ~GLESFakePaletteTexture();
303 
304  void allocBuffer(GLuint w, GLuint h) override;
305  void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
306  const void *buf, int pitch_buf) override;
307  void fillBuffer(uint32 color) override;
308 
309  const Graphics::PixelFormat &getPixelFormat() const override {
310  return _fake_format;
311  }
312 
313 protected:
314  Graphics::PixelFormat _fake_format;
315  byte *_pixels;
316 };
317 
319 protected:
320  GLESFakePalette16Texture(GLenum glFormat, GLenum glType,
321  Graphics::PixelFormat pixelFormat);
322 public:
323  virtual ~GLESFakePalette16Texture();
324 
325  void allocBuffer(GLuint w, GLuint h) override;
326 
327  void setPalette(const byte *colors, uint start, uint num) override;
328  void grabPalette(byte *colors, uint start, uint num) const override;
329 
330 protected:
331  void *prepareTextureBuffer(const Common::Rect &rect) override;
332 
333  uint16 *_palette;
334  uint16 *_buf;
335 };
336 
338 public:
340  virtual ~GLESFakePalette565Texture() {}
341 
342  void setKeycolor(byte color) override {};
343 };
344 
346 public:
348  virtual ~GLESFakePalette5551Texture() {}
349 
350  void setKeycolor(byte color) override;
351 
352 protected:
353  byte _keycolor;
354 };
355 
357 public:
359  virtual ~GLESFakePalette888Texture();
360 
361  void allocBuffer(GLuint w, GLuint h) override;
362 
363  void setPalette(const byte *colors, uint start, uint num) override;
364  void setKeycolor(byte color) override {};
365  void grabPalette(byte *colors, uint start, uint num) const override;
366 
367 protected:
368  void *prepareTextureBuffer(const Common::Rect &rect) override;
369 
370  byte *_palette;
371  byte *_buf;
372 };
373 
375 public:
377  virtual ~GLESFakePalette8888Texture();
378 
379  void allocBuffer(GLuint w, GLuint h) override;
380 
381  void setPalette(const byte *colors, uint start, uint num) override;
382  void setKeycolor(byte color) override;
383  void grabPalette(byte *colors, uint start, uint num) const override;
384 
385 protected:
386  void *prepareTextureBuffer(const Common::Rect &rect) override;
387 
388  uint32 *_palette;
389  uint32 *_buf;
390  byte _keycolor;
391 };
392 
393 #endif
Definition: texture.h:208
Definition: surface.h:67
Definition: texture.h:345
void extend(const Rect &r)
Definition: rect.h:278
Definition: pixelformat.h:138
Definition: rect.h:144
Definition: texture.h:337
Definition: texture.h:374
Definition: shader.h:56
Definition: texture.h:236
Definition: texture.h:318
Definition: texture.h:247
Definition: renderbuffer.h:27
Definition: texture.h:282
Definition: texture.h:258
Definition: texture.h:356
Definition: texture.h:296
Definition: texture.h:268
Definition: texture.h:38