ScummVM API documentation
opengl-graphics.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_OPENGL_GRAPHICS_H
23 #define BACKENDS_GRAPHICS_OPENGL_OPENGL_GRAPHICS_H
24 
25 #include "backends/graphics/opengl/framebuffer.h"
26 #include "backends/graphics/windowed.h"
27 
28 #include "base/plugins.h"
29 
30 #include "common/frac.h"
31 #include "common/mutex.h"
32 #include "common/ustr.h"
33 
34 #include "graphics/surface.h"
35 
36 namespace Graphics {
37 class Font;
38 } // End of namespace Graphics
39 
40 namespace OpenGL {
41 
42 // HACK: We use glColor in the OSD code. This might not be working on GL ES but
43 // we still enable it because Tizen already shipped with it. Also, the
44 // SurfaceSDL backend enables it and disabling it can cause issues in sdl.cpp.
45 #define USE_OSD 1
46 
47 class Surface;
48 class Pipeline;
49 #if !USE_FORCED_GLES
50 class LibRetroPipeline;
51 #endif
52 #if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
53 class Renderer3D;
54 #endif
55 
56 enum {
57  GFX_OPENGL = 0
58 };
59 
61 public:
63  virtual ~OpenGLGraphicsManager();
64 
65  // GraphicsManager API
66  bool hasFeature(OSystem::Feature f) const override;
67  void setFeatureState(OSystem::Feature f, bool enable) override;
68  bool getFeatureState(OSystem::Feature f) const override;
69 
70  const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
71  int getDefaultGraphicsMode() const override;
72  bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override;
73  int getGraphicsMode() const override;
74 
75 #ifdef USE_RGB_COLOR
76  Graphics::PixelFormat getScreenFormat() const override;
77  Common::List<Graphics::PixelFormat> getSupportedFormats() const override;
78 #endif
79 
80  const OSystem::GraphicsMode *getSupportedStretchModes() const override;
81  int getDefaultStretchMode() const override;
82  bool setStretchMode(int mode) override;
83  int getStretchMode() const override;
84 
85 #ifdef USE_SCALERS
86  uint getDefaultScaler() const override;
87  uint getDefaultScaleFactor() const override;
88  bool setScaler(uint mode, int factor) override;
89  uint getScaler() const override;
90  uint getScaleFactor() const override;
91 #endif
92 
93 #if !USE_FORCED_GLES
94  bool setShader(const Common::Path &fileNode) override;
95 #endif
96 
97  void beginGFXTransaction() override;
98  OSystem::TransactionError endGFXTransaction() override;
99 
100  int getScreenChangeID() const override;
101 
102  void initSize(uint width, uint height, const Graphics::PixelFormat *format) override;
103 
104  int16 getWidth() const override;
105  int16 getHeight() const override;
106 
107  void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h) override;
108  void fillScreen(uint32 col) override;
109  void fillScreen(const Common::Rect &r, uint32 col) override;
110 
111  void updateScreen() override;
112  void presentBuffer() override;
113 
114  Graphics::Surface *lockScreen() override;
115  void unlockScreen() override;
116 
117  void setFocusRectangle(const Common::Rect& rect) override;
118  void clearFocusRectangle() override;
119 
120  int16 getOverlayWidth() const override;
121  int16 getOverlayHeight() const override;
122  void showOverlay(bool inGUI) override;
123  void hideOverlay() override;
124 
125  Graphics::PixelFormat getOverlayFormat() const override;
126 
127  void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h) override;
128  void clearOverlay() override;
129  void grabOverlay(Graphics::Surface &surface) const override;
130 
131  void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format, const byte *mask) override;
132  void setCursorPalette(const byte *colors, uint start, uint num) override;
133 
134  void displayMessageOnOSD(const Common::U32String &msg) override;
135  void displayActivityIconOnOSD(const Graphics::Surface *icon) override;
136 
137  // PaletteManager interface
138  void setPalette(const byte *colors, uint start, uint num) override;
139  void grabPalette(byte *colors, uint start, uint num) const override;
140 
141 protected:
142  void renderCursor();
143 
147  bool isGLESContext() const { return OpenGLContext.type == kContextGLES || OpenGLContext.type == kContextGLES2; }
148 
159  void notifyContextCreate(
160  ContextType type,
161  Framebuffer *target,
162  const Graphics::PixelFormat &defaultFormat,
163  const Graphics::PixelFormat &defaultFormatAlpha);
164 
171  void notifyContextDestroy();
172 
183  Surface *createSurface(const Graphics::PixelFormat &format, bool wantAlpha = false, bool wantScaler = false, bool wantMask = false);
184 
185  //
186  // Transaction support
187  //
188  struct VideoState {
189  VideoState() : valid(false), gameWidth(0), gameHeight(0),
190 #ifdef USE_RGB_COLOR
191  gameFormat(),
192 #endif
193  aspectRatioCorrection(false), graphicsMode(GFX_OPENGL), flags(0),
194  filtering(true), scalerIndex(0), scaleFactor(1), shader() {
195  }
196 
197  bool valid;
198 
199  uint gameWidth, gameHeight;
200 #ifdef USE_RGB_COLOR
201  Graphics::PixelFormat gameFormat;
202 #endif
203  bool aspectRatioCorrection;
204  int graphicsMode;
205  uint flags;
206  bool filtering;
207 
208  uint scalerIndex;
209  int scaleFactor;
210 
211  Common::Path shader;
212 
213  bool operator==(const VideoState &right) {
214  return gameWidth == right.gameWidth && gameHeight == right.gameHeight
215 #ifdef USE_RGB_COLOR
216  && gameFormat == right.gameFormat
217 #endif
218  && aspectRatioCorrection == right.aspectRatioCorrection
219  && graphicsMode == right.graphicsMode
220  && flags == right.flags
221  && filtering == right.filtering
222  && shader == right.shader;
223  }
224 
225  bool operator!=(const VideoState &right) {
226  return !(*this == right);
227  }
228  };
229 
234 
239 
240 protected:
241  enum TransactionMode {
242  kTransactionNone = 0,
243  kTransactionActive = 1,
244  kTransactionRollback = 2
245  };
246 
247  TransactionMode getTransactionMode() const { return _transactionMode; }
248 
249 private:
253  TransactionMode _transactionMode;
254 
258  int _screenChangeID;
259 
263  int _stretchMode;
264 
268  Common::Point _shakeOffsetScaled;
269 
270 protected:
285  virtual bool loadVideoMode(uint requestedWidth, uint requestedHeight, bool resizable, int antialiasing) = 0;
286 
287  bool loadShader(const Common::Path &fileName);
288 
292  virtual void refreshScreen() = 0;
293 
300  bool saveScreenshot(const Common::Path &filename) const;
301 
302  // Do not hide the argument-less saveScreenshot from the base class
303  using WindowedGraphicsManager::saveScreenshot;
304 
305 private:
306  //
307  // OpenGL utilities
308  //
309 
313  void initializeGLContext();
314 
318  Pipeline *_pipeline;
319 
320 #if !USE_FORCED_GLES
321 
324  LibRetroPipeline *_libretroPipeline;
325 #endif
326 
327 protected:
333  bool getGLPixelFormat(const Graphics::PixelFormat &pixelFormat, GLenum &glIntFormat, GLenum &glFormat, GLenum &glType) const;
334 
335  bool gameNeedsAspectRatioCorrection() const override;
336  int getGameRenderScale() const override;
337  void recalculateDisplayAreas() override;
338  void handleResizeImpl(const int width, const int height) override;
339 
340  void updateTextureSettings();
341 
342  Pipeline *getPipeline() const { return _pipeline; }
343 
348 
353 
358 
363 
364 #if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
365 
368  Renderer3D *_renderer3d;
369 #endif
370 
374  byte _gamePalette[3 * 256];
375 
376  //
377  // Overlay
378  //
379 
384 
385  //
386  // Cursor
387  //
388 
392  void updateCursorPalette();
393 
398 
403 
408 
413 
418  void recalculateCursorScaling();
419 
425 
431 
436 
441 
446 
451 
456 
461 
465  byte _cursorPalette[3 * 256];
466 
467 #ifdef USE_SCALERS
468 
471  const PluginList &_scalerPlugins;
472 #endif
473 
474 #ifdef USE_OSD
475  //
476  // OSD
477  //
478 protected:
482  virtual const Graphics::Font *getFontOSD() const;
483 
484 private:
488  bool _osdMessageChangeRequest;
489 
496  Common::U32String _osdMessageNextData;
497 
501  void osdMessageUpdateSurface();
502 
506  Surface *_osdMessageSurface;
507 
511  uint8 _osdMessageAlpha;
512 
516  uint32 _osdMessageFadeStartTime;
517 
518  enum {
519  kOSDMessageFadeOutDelay = 2 * 1000,
520  kOSDMessageFadeOutDuration = 500,
521  kOSDMessageInitialAlpha = 80
522  };
523 
527  Surface *_osdIconSurface;
528 
529  enum {
530  kOSDIconTopMargin = 10,
531  kOSDIconRightMargin = 10
532  };
533 #endif
534 };
535 
536 } // End of namespace OpenGL
537 
538 #endif
Definition: font.h:83
Definition: surface.h:67
Definition: system.h:770
Definition: libretro.h:50
VideoState _oldState
Definition: opengl-graphics.h:238
Definition: array.h:52
bool _cursorPaletteEnabled
Definition: opengl-graphics.h:460
Definition: pixelformat.h:138
Definition: system.h:721
Feature
Definition: system.h:405
Definition: framebuffer.h:38
int _cursorHotspotY
Definition: opengl-graphics.h:412
Definition: list.h:44
Definition: rect.h:524
Definition: path.h:52
bool _cursorDontScale
Definition: opengl-graphics.h:455
Surface * _overlay
Definition: opengl-graphics.h:383
Surface * _cursorMask
Definition: opengl-graphics.h:402
float _cursorWidthScaled
Definition: opengl-graphics.h:435
Definition: windowed.h:52
Surface * _cursor
Definition: opengl-graphics.h:397
Definition: ustr.h:57
int _cursorHotspotXScaled
Definition: opengl-graphics.h:424
TransactionError
Definition: system.h:1168
Framebuffer * _targetBuffer
Definition: opengl-graphics.h:357
uint32 _cursorKeyColor
Definition: opengl-graphics.h:445
Definition: opengl-graphics.h:60
Definition: formatinfo.h:28
Definition: rect.h:144
Definition: renderbuffer.h:27
VideoState _currentState
Definition: opengl-graphics.h:233
bool isGLESContext() const
Definition: opengl-graphics.h:147
Graphics::PixelFormat _defaultFormat
Definition: opengl-graphics.h:347
float _cursorHeightScaled
Definition: opengl-graphics.h:440
int _cursorHotspotYScaled
Definition: opengl-graphics.h:430
int _cursorHotspotX
Definition: opengl-graphics.h:407
Definition: texture.h:43
bool _cursorUseKey
Definition: opengl-graphics.h:450
Surface * _gameScreen
Definition: opengl-graphics.h:362
Definition: opengl-graphics.h:188
Graphics::PixelFormat _defaultFormatAlpha
Definition: opengl-graphics.h:352
Definition: pipeline.h:40