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_GRAPHICS_OPENGL_TEXTURE_H
23 #define BACKENDS_GRAPHICS_OPENGL_TEXTURE_H
24 
25 #include "graphics/opengl/system_headers.h"
26 #include "graphics/opengl/context.h"
27 
28 #include "graphics/pixelformat.h"
29 #include "graphics/surface.h"
30 
31 #include "common/rect.h"
32 
33 class Scaler;
34 
35 namespace OpenGL {
36 
37 enum WrapMode {
38  kWrapModeBorder,
39  kWrapModeEdge,
40  kWrapModeRepeat,
41  kWrapModeMirroredRepeat
42 };
43 
49 class GLTexture {
50 public:
58  GLTexture(GLenum glIntFormat, GLenum glFormat, GLenum glType);
59  ~GLTexture();
60 
66  void enableLinearFiltering(bool enable);
67 
71  bool isLinearFilteringEnabled() const { return (_glFilter == GL_LINEAR); }
72 
78  void setWrapMode(WrapMode wrapMode);
79 
83  void destroy();
84 
88  void create();
89 
93  void bind() const;
94 
105  bool setSize(uint width, uint height);
106 
115  void updateArea(const Common::Rect &area, const Graphics::Surface &src);
116 
120  uint getWidth() const { return _width; }
121 
125  uint getHeight() const { return _height; }
126 
130  uint getLogicalWidth() const { return _logicalWidth; }
131 
135  uint getLogicalHeight() const { return _logicalHeight; }
136 
140  const GLfloat *getTexCoords() const { return _texCoords; }
141 
148  GLuint getGLTexture() const { return _glTexture; }
149 private:
150  const GLenum _glIntFormat;
151  const GLenum _glFormat;
152  const GLenum _glType;
153 
154  uint _width, _height;
155  uint _logicalWidth, _logicalHeight;
156  GLfloat _texCoords[4*2];
157 
158  GLint _glFilter;
159 
160  GLuint _glTexture;
161 };
162 
166 class Surface {
167 public:
168  Surface();
169  virtual ~Surface() {}
170 
174  virtual void destroy() = 0;
175 
179  virtual void recreate() = 0;
180 
186  virtual void enableLinearFiltering(bool enable) = 0;
187 
194  virtual void allocate(uint width, uint height) = 0;
195 
201  virtual void setMask(const byte *mask) {}
202 
216  void copyRectToTexture(uint x, uint y, uint w, uint h, const void *src, uint srcPitch);
217 
223  void fill(uint32 color);
224  void fill(const Common::Rect &r, uint32 color);
225 
226  void flagDirty() { _allDirty = true; }
227  virtual bool isDirty() const { return _allDirty || !_dirtyArea.isEmpty(); }
228 
229  virtual uint getWidth() const = 0;
230  virtual uint getHeight() const = 0;
231 
235  virtual Graphics::PixelFormat getFormat() const = 0;
236 
237  virtual Graphics::Surface *getSurface() = 0;
238  virtual const Graphics::Surface *getSurface() const = 0;
239 
243  virtual bool hasPalette() const { return false; }
244 
252  virtual void setColorKey(uint colorKey) {}
253  virtual void setPalette(uint start, uint colors, const byte *palData) {}
254 
255  virtual void setScaler(uint scalerIndex, int scaleFactor) {}
256 
260  virtual void updateGLTexture() = 0;
261 
265  virtual const GLTexture &getGLTexture() const = 0;
266 protected:
267  void clearDirty() { _allDirty = false; _dirtyArea = Common::Rect(); }
268 
269  void addDirtyArea(const Common::Rect &r);
270  Common::Rect getDirtyArea() const;
271 private:
272  bool _allDirty;
273  Common::Rect _dirtyArea;
274 };
275 
280 class Texture : public Surface {
281 public:
290  Texture(GLenum glIntFormat, GLenum glFormat, GLenum glType, const Graphics::PixelFormat &format);
291  ~Texture() override;
292 
293  void destroy() override;
294 
295  void recreate() override;
296 
297  void enableLinearFiltering(bool enable) override;
298 
299  void allocate(uint width, uint height) override;
300 
301  uint getWidth() const override { return _userPixelData.w; }
302  uint getHeight() const override { return _userPixelData.h; }
303 
307  Graphics::PixelFormat getFormat() const override { return _format; }
308 
309  Graphics::Surface *getSurface() override { return &_userPixelData; }
310  const Graphics::Surface *getSurface() const override { return &_userPixelData; }
311 
312  void updateGLTexture() override;
313  const GLTexture &getGLTexture() const override { return _glTexture; }
314 protected:
315  const Graphics::PixelFormat _format;
316 
317  void updateGLTexture(Common::Rect &dirtyArea);
318 
319 private:
320  GLTexture _glTexture;
321 
322  Graphics::Surface _textureData;
323  Graphics::Surface _userPixelData;
324 };
325 
326 class FakeTexture : public Texture {
327 public:
328  FakeTexture(GLenum glIntFormat, GLenum glFormat, GLenum glType, const Graphics::PixelFormat &format, const Graphics::PixelFormat &fakeFormat);
329  ~FakeTexture() override;
330 
331  void allocate(uint width, uint height) override;
332  void setMask(const byte *mask) override;
333 
334  Graphics::PixelFormat getFormat() const override { return _fakeFormat; }
335 
336  bool hasPalette() const override { return (_palette != nullptr); }
337 
338  void setColorKey(uint colorKey) override;
339  void setPalette(uint start, uint colors, const byte *palData) override;
340 
341  Graphics::Surface *getSurface() override { return &_rgbData; }
342  const Graphics::Surface *getSurface() const override { return &_rgbData; }
343 
344  void updateGLTexture() override;
345 protected:
346  void applyPaletteAndMask(byte *dst, const byte *src, uint dstPitch, uint srcPitch, uint srcWidth, const Common::Rect &dirtyArea, const Graphics::PixelFormat &dstFormat, const Graphics::PixelFormat &srcFormat) const;
347 
348  Graphics::Surface _rgbData;
349  Graphics::PixelFormat _fakeFormat;
350  uint32 *_palette;
351  uint8 *_mask;
352 };
353 
354 class TextureRGB555 : public FakeTexture {
355 public:
356  TextureRGB555();
357  ~TextureRGB555() override {}
358 
359  void updateGLTexture() override;
360 };
361 
363 public:
365  ~TextureRGBA8888Swap() override {}
366 
367  void updateGLTexture() override;
368 };
369 
370 #ifdef USE_SCALERS
371 class ScaledTexture : public FakeTexture {
372 public:
373  ScaledTexture(GLenum glIntFormat, GLenum glFormat, GLenum glType, const Graphics::PixelFormat &format, const Graphics::PixelFormat &fakeFormat);
374  ~ScaledTexture() override;
375 
376  void allocate(uint width, uint height) override;
377 
378  uint getWidth() const override { return _rgbData.w; }
379  uint getHeight() const override { return _rgbData.h; }
380  Graphics::PixelFormat getFormat() const override { return _fakeFormat; }
381 
382  bool hasPalette() const override { return (_palette != nullptr); }
383 
384  Graphics::Surface *getSurface() override { return &_rgbData; }
385  const Graphics::Surface *getSurface() const override { return &_rgbData; }
386 
387  void updateGLTexture() override;
388 
389  void setScaler(uint scalerIndex, int scaleFactor) override;
390 protected:
391  Graphics::Surface *_convData;
392  Scaler *_scaler;
393  uint _scalerIndex;
394  uint _extraPixels;
395  uint _scaleFactor;
396 };
397 #endif
398 
399 #if !USE_FORCED_GLES
400 class TextureTarget;
401 class CLUT8LookUpPipeline;
402 
403 class TextureCLUT8GPU : public Surface {
404 public:
405  TextureCLUT8GPU();
406  ~TextureCLUT8GPU() override;
407 
408  void destroy() override;
409 
410  void recreate() override;
411 
412  void enableLinearFiltering(bool enable) override;
413 
414  void allocate(uint width, uint height) override;
415 
416  bool isDirty() const override { return _paletteDirty || Surface::isDirty(); }
417 
418  uint getWidth() const override { return _userPixelData.w; }
419  uint getHeight() const override { return _userPixelData.h; }
420 
421  Graphics::PixelFormat getFormat() const override;
422 
423  bool hasPalette() const override { return true; }
424 
425  void setColorKey(uint colorKey) override;
426  void setPalette(uint start, uint colors, const byte *palData) override;
427 
428  Graphics::Surface *getSurface() override { return &_userPixelData; }
429  const Graphics::Surface *getSurface() const override { return &_userPixelData; }
430 
431  void updateGLTexture() override;
432  const GLTexture &getGLTexture() const override;
433 
434  static bool isSupportedByContext() {
435  return OpenGLContext.shadersSupported
436  && OpenGLContext.multitextureSupported
437  && OpenGLContext.framebufferObjectSupported;
438  }
439 private:
440  void lookUpColors();
441 
442  GLTexture _clut8Texture;
443  GLTexture _paletteTexture;
444 
445  TextureTarget *_target;
446  CLUT8LookUpPipeline *_clut8Pipeline;
447 
448  GLfloat _clut8Vertices[4*2];
449 
450  Graphics::Surface _clut8Data;
451  Graphics::Surface _userPixelData;
452 
453  byte _palette[4 * 256];
454  bool _paletteDirty;
455 };
456 #endif // !USE_FORCED_GLES
457 
458 } // End of namespace OpenGL
459 
460 #endif
void updateArea(const Common::Rect &area, const Graphics::Surface &src)
Definition: texture.h:326
const GLTexture & getGLTexture() const override
Definition: texture.h:313
Definition: surface.h:67
Definition: texture.h:362
void setWrapMode(WrapMode wrapMode)
virtual void setColorKey(uint colorKey)
Definition: texture.h:252
virtual bool hasPalette() const
Definition: texture.h:243
Definition: pixelformat.h:138
GLuint getGLTexture() const
Definition: texture.h:148
Definition: texture.h:354
Definition: rect.h:144
Definition: texture.h:49
bool isLinearFilteringEnabled() const
Definition: texture.h:71
virtual void setMask(const byte *mask)
Definition: texture.h:201
const GLfloat * getTexCoords() const
Definition: texture.h:140
uint getWidth() const
Definition: texture.h:120
bool hasPalette() const override
Definition: texture.h:336
Definition: texture.h:403
Graphics::PixelFormat getFormat() const override
Definition: texture.h:334
Definition: framebuffer.h:201
bool setSize(uint width, uint height)
bool hasPalette() const override
Definition: texture.h:423
uint getLogicalHeight() const
Definition: texture.h:135
Definition: renderbuffer.h:27
Definition: texture.h:280
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
Graphics::PixelFormat getFormat() const override
Definition: texture.h:307
void enableLinearFiltering(bool enable)
Definition: texture.h:166
Definition: scalerplugin.h:28
Definition: clut8.h:30
void bind() const
GLTexture(GLenum glIntFormat, GLenum glFormat, GLenum glType)
uint getHeight() const
Definition: texture.h:125
uint getLogicalWidth() const
Definition: texture.h:130