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 "backends/graphics3d/opengl/framebuffer.h"
29 #include "graphics/surface.h"
30 #include "graphics/pixelformat.h"
31 
32 #include "common/rect.h"
33 #include "common/array.h"
34 
35 namespace OpenGL {
36 class Shader;
37 }
38 
39 class AndroidFrameBuffer : public OpenGL::FrameBuffer {
40 public:
41  AndroidFrameBuffer(GLenum glIntFormat, GLenum glFormat, GLenum glType, GLuint texture_name, uint width, uint height, uint texture_width, uint texture_height);
43 };
44 
46 public:
47  static void initGL();
48  static void unbindShader();
49 
50 protected:
51  GLESBaseTexture(GLenum glFormat, GLenum glType,
52  Graphics::PixelFormat pixelFormat);
53 
54 public:
55  virtual ~GLESBaseTexture();
56 
57  void release();
58  void reinit();
59 
60  void setLinearFilter(bool value);
61 
62  virtual void allocBuffer(GLuint w, GLuint h);
63 
64  virtual void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
65  const void *buf, int pitch_buf) = 0;
66  virtual void fillBuffer(uint32 color) = 0;
67 
68  void drawTexture(GLshort x, GLshort y, GLshort w, GLshort h) {
69  drawTexture(x, y, w, h, Common::Rect(0, 0, width(), height()));
70  }
71  void drawTexture(GLshort x, GLshort y, GLshort w, GLshort h, const Common::Rect &clip);
72 
73  inline void setDrawRect(const Common::Rect &rect) {
74  _draw_rect = rect;
75  }
76 
77  inline void setDrawRect(int16 w, int16 h) {
78  _draw_rect = Common::Rect(w, h);
79  }
80 
81  inline void setDrawRect(int16 x1, int16 y1, int16 x2, int16 y2) {
82  _draw_rect = Common::Rect(x1, y1, x2, y2);
83  }
84 
85  inline const Common::Rect &getDrawRect() const {
86  return _draw_rect;
87  }
88 
89  inline void drawTextureRect() {
90  drawTexture(_draw_rect.left, _draw_rect.top,
91  _draw_rect.width(), _draw_rect.height());
92  }
93 
94  inline void drawTextureOrigin() {
95  drawTexture(0, 0, _surface.w, _surface.h);
96  }
97 
98  inline GLuint width() const {
99  return _surface.w;
100  }
101 
102  inline GLuint height() const {
103  return _surface.h;
104  }
105 
106  inline GLuint texWidth() const {
107  return _texture_width;
108  }
109 
110  inline GLuint texHeight() const {
111  return _texture_height;
112  }
113 
114  inline uint16 pitch() const {
115  return _surface.pitch;
116  }
117 
118  inline bool isEmpty() const {
119  return _surface.w == 0 || _surface.h == 0;
120  }
121 
122  inline const Graphics::Surface *surface_const() const {
123  return &_surface;
124  }
125 
126  inline Graphics::Surface *surface() {
127  setDirty();
128  return &_surface;
129  }
130 
131  virtual void setPalette(const byte *colors, uint start, uint num) = 0;
132  virtual void setKeycolor(byte color) = 0;
133  virtual void grabPalette(byte *colors, uint start, uint num) const = 0;
134 
135  inline bool hasPalette() const {
136  return _palettePixelFormat.bytesPerPixel > 0;
137  }
138 
139  inline bool dirty() const {
140  return _all_dirty || !_dirty_rect.isEmpty();
141  }
142 
143  virtual const Graphics::PixelFormat &getPixelFormat() const;
144 
145  inline const Graphics::PixelFormat &getPalettePixelFormat() const {
146  return _palettePixelFormat;
147  }
148 
149  GLuint getTextureName() const {
150  return _texture_name;
151  }
152 
153  GLenum getTextureFormat() const {
154  return _glFormat;
155  }
156 
157  GLenum getTextureType() const {
158  return _glType;
159  }
160 
161  void setGameTexture() {
162  _is_game_texture = true;
163  }
164 
165  void setAlpha(float alpha) {
166  _alpha = alpha;
167  }
168 
169 protected:
170  void initSize();
171 
172  virtual void *prepareTextureBuffer(const Common::Rect &rect) = 0;
173 
174  inline void setDirty() {
175  _all_dirty = true;
176  }
177 
178  inline void clearDirty() {
179  _all_dirty = false;
180  _dirty_rect.top = 0;
181  _dirty_rect.left = 0;
182  _dirty_rect.bottom = 0;
183  _dirty_rect.right = 0;
184  }
185 
186  inline void setDirtyRect(const Common::Rect &r) {
187  if (!_all_dirty) {
188  if (_dirty_rect.isEmpty()) {
189  _dirty_rect = r;
190  } else {
191  _dirty_rect.extend(r);
192  }
193  }
194  }
195 
196  GLenum _glFormat;
197  GLenum _glType;
198  GLint _glFilter;
199 
200  GLuint _texture_name;
201  Graphics::Surface _surface;
202  GLuint _texture_width;
203  GLuint _texture_height;
204 
205  Common::Rect _draw_rect;
206 
207  bool _all_dirty;
208  Common::Rect _dirty_rect;
209 
210  Graphics::PixelFormat _pixelFormat;
211  Graphics::PixelFormat _palettePixelFormat;
212 
213  bool _is_game_texture;
214 
215  GLfloat _alpha;
216 
217  static bool _npot_supported;
218  static OpenGL::Shader *_box_shader;
219  static GLuint _verticesVBO;
220 
221 };
222 
223 class GLESTexture : public GLESBaseTexture {
224 protected:
225  GLESTexture(GLenum glFormat, GLenum glType,
226  Graphics::PixelFormat pixelFormat);
227 
228 public:
229  virtual ~GLESTexture();
230 
231  void allocBuffer(GLuint w, GLuint h) override;
232 
233  void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
234  const void *buf, int pitch_buf) override;
235  void fillBuffer(uint32 color) override;
236 
237  void setPalette(const byte *colors, uint start, uint num) override {}
238  void setKeycolor(byte color) override {};
239  void grabPalette(byte *colors, uint start, uint num) const override {}
240 
241  void readPixels();
242 
243 protected:
244  void *prepareTextureBuffer(const Common::Rect &rect) override;
245 
246  byte *_pixels;
247  byte *_buf;
248 };
249 
250 // RGBA4444 texture
251 class GLES4444Texture : public GLESTexture {
252 public:
253  GLES4444Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixelFormat()) {}
254  virtual ~GLES4444Texture() {}
255 
256  static Graphics::PixelFormat pixelFormat() {
257  return Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0);
258  }
259 };
260 
261 // RGBA5551 texture
262 class GLES5551Texture : public GLESTexture {
263 public:
264  GLES5551Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, pixelFormat()) {}
265  virtual ~GLES5551Texture() {}
266 
267  static inline Graphics::PixelFormat pixelFormat() {
268  return Graphics::PixelFormat(2, 5, 5, 5, 1, 11, 6, 1, 0);
269  }
270 };
271 
272 // RGB565 texture
273 class GLES565Texture : public GLESTexture {
274 public:
275  GLES565Texture() : GLESTexture(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixelFormat()) {}
276  virtual ~GLES565Texture() {}
277 
278  static inline Graphics::PixelFormat pixelFormat() {
279  return Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
280  }
281 };
282 
283 class GLES888Texture : public GLESTexture {
284 public:
285  GLES888Texture() : GLESTexture(GL_RGB, GL_UNSIGNED_BYTE, pixelFormat()) {}
286  virtual ~GLES888Texture() {}
287 
288  static Graphics::PixelFormat pixelFormat() {
289 #ifdef SCUMM_BIG_ENDIAN
290  return Graphics::PixelFormat(3, 8, 8, 8, 0, 16, 8, 0, 0);
291 #else
292  return Graphics::PixelFormat(3, 8, 8, 8, 0, 0, 8, 16, 0);
293 #endif
294  }
295 };
296 
297 class GLES8888Texture : public GLESTexture {
298 public:
299  GLES8888Texture() : GLESTexture(GL_RGBA, GL_UNSIGNED_BYTE, pixelFormat()) {}
300  virtual ~GLES8888Texture() {}
301 
302  static Graphics::PixelFormat pixelFormat() {
303 #ifdef SCUMM_BIG_ENDIAN
304  return Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
305 #else
306  return Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
307 #endif
308  }
309 };
310 
312 protected:
313  GLESFakePaletteTexture(GLenum glFormat, GLenum glType,
314  Graphics::PixelFormat pixelFormat);
315 
316 public:
317  virtual ~GLESFakePaletteTexture();
318 
319  void allocBuffer(GLuint w, GLuint h) override;
320  void updateBuffer(GLuint x, GLuint y, GLuint width, GLuint height,
321  const void *buf, int pitch_buf) override;
322  void fillBuffer(uint32 color) override;
323 
324  const Graphics::PixelFormat &getPixelFormat() const override {
325  return _fake_format;
326  }
327 
328 protected:
329  Graphics::PixelFormat _fake_format;
330  byte *_pixels;
331 };
332 
334 protected:
335  GLESFakePalette16Texture(GLenum glFormat, GLenum glType,
336  Graphics::PixelFormat pixelFormat);
337 public:
338  virtual ~GLESFakePalette16Texture();
339 
340  void allocBuffer(GLuint w, GLuint h) override;
341 
342  void setPalette(const byte *colors, uint start, uint num) override;
343  void grabPalette(byte *colors, uint start, uint num) const override;
344 
345 protected:
346  void *prepareTextureBuffer(const Common::Rect &rect) override;
347 
348  uint16 *_palette;
349  uint16 *_buf;
350 };
351 
353 public:
355  virtual ~GLESFakePalette565Texture() {}
356 
357  void setKeycolor(byte color) override {};
358 };
359 
361 public:
363  virtual ~GLESFakePalette5551Texture() {}
364 
365  void setKeycolor(byte color) override;
366 
367 protected:
368  byte _keycolor;
369 };
370 
372 public:
374  virtual ~GLESFakePalette888Texture();
375 
376  void allocBuffer(GLuint w, GLuint h) override;
377 
378  void setPalette(const byte *colors, uint start, uint num) override;
379  void setKeycolor(byte color) override {};
380  void grabPalette(byte *colors, uint start, uint num) const override;
381 
382 protected:
383  void *prepareTextureBuffer(const Common::Rect &rect) override;
384 
385  byte *_palette;
386  byte *_buf;
387 };
388 
390 public:
392  virtual ~GLESFakePalette8888Texture();
393 
394  void allocBuffer(GLuint w, GLuint h) override;
395 
396  void setPalette(const byte *colors, uint start, uint num) override;
397  void setKeycolor(byte color) override;
398  void grabPalette(byte *colors, uint start, uint num) const override;
399 
400 protected:
401  void *prepareTextureBuffer(const Common::Rect &rect) override;
402 
403  uint32 *_palette;
404  uint32 *_buf;
405  byte _keycolor;
406 };
407 
408 #endif
Definition: texture.h:223
Definition: surface.h:67
Definition: texture.h:360
void extend(const Rect &r)
Definition: rect.h:278
Definition: pixelformat.h:138
Definition: rect.h:144
Definition: texture.h:352
Definition: texture.h:39
Definition: texture.h:389
Definition: shader.h:56
Definition: texture.h:251
Definition: texture.h:333
Definition: texture.h:262
Definition: renderbuffer.h:27
Definition: texture.h:297
Definition: texture.h:273
Definition: texture.h:371
Definition: texture.h:311
Definition: texture.h:283
Definition: texture.h:45