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 #include "graphics/opengl/texture.h"
28 
29 #include "graphics/pixelformat.h"
30 #include "graphics/surface.h"
31 #include "graphics/blit.h"
32 
33 #include "common/rect.h"
34 #include "common/rotationmode.h"
35 
36 class Scaler;
37 
38 namespace OpenGL {
39 
43 class Surface {
44 public:
45  Surface();
46  virtual ~Surface() {}
47 
51  virtual void destroy() = 0;
52 
56  virtual void recreate() = 0;
57 
63  virtual void enableLinearFiltering(bool enable) = 0;
64 
70  virtual void setRotation(Common::RotationMode rotation) = 0;
71 
78  virtual void allocate(uint width, uint height) = 0;
79 
85  virtual void setMask(const byte *mask) {}
86 
100  void copyRectToTexture(uint x, uint y, uint w, uint h, const void *src, uint srcPitch);
101 
107  void fill(uint32 color);
108  void fill(const Common::Rect &r, uint32 color);
109 
110  void flagDirty() { _allDirty = true; }
111  virtual bool isDirty() const { return _allDirty || !_dirtyArea.isEmpty(); }
112 
113  virtual uint getWidth() const = 0;
114  virtual uint getHeight() const = 0;
115 
119  virtual Graphics::PixelFormat getFormat() const = 0;
120 
121  virtual Graphics::Surface *getSurface() = 0;
122  virtual const Graphics::Surface *getSurface() const = 0;
123 
127  virtual bool hasPalette() const { return false; }
128 
136  virtual void setColorKey(uint colorKey) {}
137  virtual void setPalette(uint start, uint colors, const byte *palData) {}
138 
139  virtual void setScaler(uint scalerIndex, int scaleFactor) {}
140 
144  virtual void updateGLTexture() = 0;
145 
149  virtual const Texture &getGLTexture() const = 0;
150 protected:
151  void clearDirty() { _allDirty = false; _dirtyArea = Common::Rect(); }
152 
153  void addDirtyArea(const Common::Rect &r);
154  Common::Rect getDirtyArea() const;
155 private:
156  bool _allDirty;
157  Common::Rect _dirtyArea;
158 };
159 
164 class TextureSurface : public Surface {
165 public:
174  TextureSurface(GLenum glIntFormat, GLenum glFormat, GLenum glType, const Graphics::PixelFormat &format);
175  ~TextureSurface() override;
176 
177  void destroy() override;
178 
179  void recreate() override;
180 
181  void enableLinearFiltering(bool enable) override;
182  void setRotation(Common::RotationMode rotation) override;
183 
184  void allocate(uint width, uint height) override;
185 
186  uint getWidth() const override { return _userPixelData.w; }
187  uint getHeight() const override { return _userPixelData.h; }
188 
192  Graphics::PixelFormat getFormat() const override { return _format; }
193 
194  Graphics::Surface *getSurface() override { return &_userPixelData; }
195  const Graphics::Surface *getSurface() const override { return &_userPixelData; }
196 
197  void updateGLTexture() override;
198  const Texture &getGLTexture() const override { return _glTexture; }
199 protected:
200  const Graphics::PixelFormat _format;
201 
202  void updateGLTexture(Common::Rect &dirtyArea);
203 
204 private:
205  Texture _glTexture;
206 
207  Graphics::Surface _textureData;
208  Graphics::Surface _userPixelData;
209 };
210 
212 public:
213  FakeTextureSurface(GLenum glIntFormat, GLenum glFormat, GLenum glType, const Graphics::PixelFormat &format, const Graphics::PixelFormat &fakeFormat);
214  ~FakeTextureSurface() override;
215 
216  void allocate(uint width, uint height) override;
217  void setMask(const byte *mask) override;
218 
219  Graphics::PixelFormat getFormat() const override { return _fakeFormat; }
220 
221  bool hasPalette() const override { return (_palette != nullptr); }
222 
223  void setColorKey(uint colorKey) override;
224  void setPalette(uint start, uint colors, const byte *palData) override;
225 
226  Graphics::Surface *getSurface() override { return &_rgbData; }
227  const Graphics::Surface *getSurface() const override { return &_rgbData; }
228 
229  void updateGLTexture() override;
230 protected:
231  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;
232 
233  Graphics::Surface _rgbData;
234  Graphics::PixelFormat _fakeFormat;
235  Graphics::FastBlitFunc _blitFunc;
236  uint32 *_palette;
237  uint8 *_mask;
238 };
239 
241 public:
243  ~TextureSurfaceRGB555() override {}
244 
245  void updateGLTexture() override;
246 };
247 
248 #ifdef USE_SCALERS
249 class ScaledTextureSurface : public FakeTextureSurface {
250 public:
251  ScaledTextureSurface(GLenum glIntFormat, GLenum glFormat, GLenum glType, const Graphics::PixelFormat &format, const Graphics::PixelFormat &fakeFormat);
252  ~ScaledTextureSurface() override;
253 
254  void allocate(uint width, uint height) override;
255 
256  uint getWidth() const override { return _rgbData.w; }
257  uint getHeight() const override { return _rgbData.h; }
258  Graphics::PixelFormat getFormat() const override { return _fakeFormat; }
259 
260  bool hasPalette() const override { return (_palette != nullptr); }
261 
262  Graphics::Surface *getSurface() override { return &_rgbData; }
263  const Graphics::Surface *getSurface() const override { return &_rgbData; }
264 
265  void updateGLTexture() override;
266 
267  void setScaler(uint scalerIndex, int scaleFactor) override;
268 protected:
269  Graphics::Surface *_convData;
270  Scaler *_scaler;
271  uint _scalerIndex;
272  uint _extraPixels;
273  uint _scaleFactor;
274 };
275 #endif
276 
277 #if !USE_FORCED_GLES
278 class TextureTarget;
279 class CLUT8LookUpPipeline;
280 
282 public:
284  ~TextureSurfaceCLUT8GPU() override;
285 
286  void destroy() override;
287 
288  void recreate() override;
289 
290  void enableLinearFiltering(bool enable) override;
291  void setRotation(Common::RotationMode rotation) override;
292 
293  void allocate(uint width, uint height) override;
294 
295  bool isDirty() const override { return _paletteDirty || Surface::isDirty(); }
296 
297  uint getWidth() const override { return _userPixelData.w; }
298  uint getHeight() const override { return _userPixelData.h; }
299 
300  Graphics::PixelFormat getFormat() const override;
301 
302  bool hasPalette() const override { return true; }
303 
304  void setColorKey(uint colorKey) override;
305  void setPalette(uint start, uint colors, const byte *palData) override;
306 
307  Graphics::Surface *getSurface() override { return &_userPixelData; }
308  const Graphics::Surface *getSurface() const override { return &_userPixelData; }
309 
310  void updateGLTexture() override;
311  const Texture &getGLTexture() const override;
312 
313  static bool isSupportedByContext() {
314  return OpenGLContext.shadersSupported
315  && OpenGLContext.multitextureSupported
316  && OpenGLContext.framebufferObjectSupported
317  // With 2^-8 precision this is too prone to approximation errors
318  && OpenGLContext.textureLookupPrecision > 8;
319  }
320 private:
321  void lookUpColors();
322 
323  Texture _clut8Texture;
324  Texture _paletteTexture;
325 
326  TextureTarget *_target;
327  CLUT8LookUpPipeline *_clut8Pipeline;
328 
329  GLfloat _clut8Vertices[4*2];
330 
331  Graphics::Surface _clut8Data;
332  Graphics::Surface _userPixelData;
333 
334  byte _palette[4 * 256];
335  bool _paletteDirty;
336 };
337 #endif // !USE_FORCED_GLES
338 
339 } // End of namespace OpenGL
340 
341 #endif
Definition: surface.h:67
virtual void setColorKey(uint colorKey)
Definition: texture.h:136
Definition: texture.h:211
virtual bool hasPalette() const
Definition: texture.h:127
Definition: pixelformat.h:138
const Texture & getGLTexture() const override
Definition: texture.h:198
Graphics::PixelFormat getFormat() const override
Definition: texture.h:219
Definition: rect.h:524
virtual void recreate()=0
virtual void setMask(const byte *mask)
Definition: texture.h:85
virtual void setRotation(Common::RotationMode rotation)=0
Graphics::PixelFormat getFormat() const override
Definition: texture.h:192
bool hasPalette() const override
Definition: texture.h:302
bool hasPalette() const override
Definition: texture.h:221
virtual void allocate(uint width, uint height)=0
void copyRectToTexture(uint x, uint y, uint w, uint h, const void *src, uint srcPitch)
Definition: framebuffer.h:203
virtual void enableLinearFiltering(bool enable)=0
bool isEmpty() const
Definition: rect.h:373
Definition: renderbuffer.h:27
Definition: texture.h:48
virtual void updateGLTexture()=0
Definition: texture.h:240
virtual Graphics::PixelFormat getFormat() const =0
Definition: texture.h:164
Definition: texture.h:281
Definition: texture.h:43
RotationMode
Definition: rotationmode.h:44
Definition: scalerplugin.h:28
Definition: clut8.h:30
virtual const Texture & getGLTexture() const =0
void fill(uint32 color)
virtual void destroy()=0