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 
53 enum {
54  GFX_OPENGL = 0
55 };
56 
58 public:
60  virtual ~OpenGLGraphicsManager();
61 
62  // GraphicsManager API
63  bool hasFeature(OSystem::Feature f) const override;
64  void setFeatureState(OSystem::Feature f, bool enable) override;
65  bool getFeatureState(OSystem::Feature f) const override;
66 
67  const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
68  int getDefaultGraphicsMode() const override;
69  bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override;
70  int getGraphicsMode() const override;
71 
72 #ifdef USE_RGB_COLOR
73  Graphics::PixelFormat getScreenFormat() const override;
74  Common::List<Graphics::PixelFormat> getSupportedFormats() const override;
75 #endif
76 
77  const OSystem::GraphicsMode *getSupportedStretchModes() const override;
78  int getDefaultStretchMode() const override;
79  bool setStretchMode(int mode) override;
80  int getStretchMode() const override;
81 
82 #ifdef USE_SCALERS
83  uint getDefaultScaler() const override;
84  uint getDefaultScaleFactor() const override;
85  bool setScaler(uint mode, int factor) override;
86  uint getScaler() const override;
87  uint getScaleFactor() const override;
88 #endif
89 
90 #if !USE_FORCED_GLES
91  bool setShader(const Common::Path &fileNode) override;
92 #endif
93 
94  void beginGFXTransaction() override;
95  OSystem::TransactionError endGFXTransaction() override;
96 
97  int getScreenChangeID() const override;
98 
99  void initSize(uint width, uint height, const Graphics::PixelFormat *format) override;
100 
101  int16 getWidth() const override;
102  int16 getHeight() const override;
103 
104  void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h) override;
105  void fillScreen(uint32 col) override;
106  void fillScreen(const Common::Rect &r, uint32 col) override;
107 
108  void updateScreen() override;
109 
110  Graphics::Surface *lockScreen() override;
111  void unlockScreen() override;
112 
113  void setFocusRectangle(const Common::Rect& rect) override;
114  void clearFocusRectangle() override;
115 
116  int16 getOverlayWidth() const override;
117  int16 getOverlayHeight() const override;
118 
119  Graphics::PixelFormat getOverlayFormat() const override;
120 
121  void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h) override;
122  void clearOverlay() override;
123  void grabOverlay(Graphics::Surface &surface) const override;
124 
125  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;
126  void setCursorPalette(const byte *colors, uint start, uint num) override;
127 
128  void displayMessageOnOSD(const Common::U32String &msg) override;
129  void displayActivityIconOnOSD(const Graphics::Surface *icon) override;
130 
131  // PaletteManager interface
132  void setPalette(const byte *colors, uint start, uint num) override;
133  void grabPalette(byte *colors, uint start, uint num) const override;
134 
135 protected:
136  void renderCursor();
137 
141  bool isGLESContext() const { return OpenGLContext.type == kContextGLES || OpenGLContext.type == kContextGLES2; }
142 
153  void notifyContextCreate(
154  ContextType type,
155  Framebuffer *target,
156  const Graphics::PixelFormat &defaultFormat,
157  const Graphics::PixelFormat &defaultFormatAlpha);
158 
165  void notifyContextDestroy();
166 
177  Surface *createSurface(const Graphics::PixelFormat &format, bool wantAlpha = false, bool wantScaler = false, bool wantMask = false);
178 
179  //
180  // Transaction support
181  //
182  struct VideoState {
183  VideoState() : valid(false), gameWidth(0), gameHeight(0),
184 #ifdef USE_RGB_COLOR
185  gameFormat(),
186 #endif
187  aspectRatioCorrection(false), graphicsMode(GFX_OPENGL), filtering(true),
188  scalerIndex(0), scaleFactor(1), shader() {
189  }
190 
191  bool valid;
192 
193  uint gameWidth, gameHeight;
194 #ifdef USE_RGB_COLOR
195  Graphics::PixelFormat gameFormat;
196 #endif
197  bool aspectRatioCorrection;
198  int graphicsMode;
199  bool filtering;
200 
201  uint scalerIndex;
202  int scaleFactor;
203 
204  Common::Path shader;
205 
206  bool operator==(const VideoState &right) {
207  return gameWidth == right.gameWidth && gameHeight == right.gameHeight
208 #ifdef USE_RGB_COLOR
209  && gameFormat == right.gameFormat
210 #endif
211  && aspectRatioCorrection == right.aspectRatioCorrection
212  && graphicsMode == right.graphicsMode
213  && filtering == right.filtering
214  && shader == right.shader;
215  }
216 
217  bool operator!=(const VideoState &right) {
218  return !(*this == right);
219  }
220  };
221 
226 
231 
232 protected:
233  enum TransactionMode {
234  kTransactionNone = 0,
235  kTransactionActive = 1,
236  kTransactionRollback = 2
237  };
238 
239  TransactionMode getTransactionMode() const { return _transactionMode; }
240 
241 private:
245  TransactionMode _transactionMode;
246 
250  int _screenChangeID;
251 
255  int _stretchMode;
256 
260  Common::Point _shakeOffsetScaled;
261 
262 protected:
276  virtual bool loadVideoMode(uint requestedWidth, uint requestedHeight, const Graphics::PixelFormat &format) = 0;
277 
278  bool loadShader(const Common::Path &fileName);
279 
283  virtual void refreshScreen() = 0;
284 
291  bool saveScreenshot(const Common::Path &filename) const;
292 
293  // Do not hide the argument-less saveScreenshot from the base class
294  using WindowedGraphicsManager::saveScreenshot;
295 
296 private:
297  //
298  // OpenGL utilities
299  //
300 
304  void initializeGLContext();
305 
309  Pipeline *_pipeline;
310 
311 #if !USE_FORCED_GLES
312 
315  LibRetroPipeline *_libretroPipeline;
316 #endif
317 
318 protected:
324  bool getGLPixelFormat(const Graphics::PixelFormat &pixelFormat, GLenum &glIntFormat, GLenum &glFormat, GLenum &glType) const;
325 
326  bool gameNeedsAspectRatioCorrection() const override;
327  int getGameRenderScale() const override;
328  void recalculateDisplayAreas() override;
329  void handleResizeImpl(const int width, const int height) override;
330 
331  void updateLinearFiltering();
332 
333  Pipeline *getPipeline() const { return _pipeline; }
334 
339 
344 
349 
354 
358  byte _gamePalette[3 * 256];
359 
360  //
361  // Overlay
362  //
363 
368 
369  //
370  // Cursor
371  //
372 
376  void updateCursorPalette();
377 
382 
387 
392 
397 
402  void recalculateCursorScaling();
403 
409 
415 
420 
425 
430 
435 
440 
445 
449  byte _cursorPalette[3 * 256];
450 
451 #ifdef USE_SCALERS
452 
455  const PluginList &_scalerPlugins;
456 #endif
457 
458 #ifdef USE_OSD
459  //
460  // OSD
461  //
462 protected:
466  virtual const Graphics::Font *getFontOSD() const;
467 
468 private:
472  bool _osdMessageChangeRequest;
473 
480  Common::U32String _osdMessageNextData;
481 
485  void osdMessageUpdateSurface();
486 
490  Surface *_osdMessageSurface;
491 
495  uint8 _osdMessageAlpha;
496 
500  uint32 _osdMessageFadeStartTime;
501 
502  enum {
503  kOSDMessageFadeOutDelay = 2 * 1000,
504  kOSDMessageFadeOutDuration = 500,
505  kOSDMessageInitialAlpha = 80
506  };
507 
511  Surface *_osdIconSurface;
512 
513  enum {
514  kOSDIconTopMargin = 10,
515  kOSDIconRightMargin = 10
516  };
517 #endif
518 };
519 
520 } // End of namespace OpenGL
521 
522 #endif
Definition: font.h:83
Definition: surface.h:67
Definition: system.h:779
Definition: libretro.h:50
VideoState _oldState
Definition: opengl-graphics.h:230
Definition: array.h:52
bool _cursorPaletteEnabled
Definition: opengl-graphics.h:444
Definition: pixelformat.h:138
Definition: system.h:730
Feature
Definition: system.h:403
Definition: framebuffer.h:38
int _cursorHotspotY
Definition: opengl-graphics.h:396
Definition: list.h:44
Definition: rect.h:144
Definition: path.h:52
bool _cursorDontScale
Definition: opengl-graphics.h:439
Surface * _overlay
Definition: opengl-graphics.h:367
Surface * _cursorMask
Definition: opengl-graphics.h:386
float _cursorWidthScaled
Definition: opengl-graphics.h:419
Definition: windowed.h:52
Surface * _cursor
Definition: opengl-graphics.h:381
Definition: ustr.h:57
int _cursorHotspotXScaled
Definition: opengl-graphics.h:408
TransactionError
Definition: system.h:1155
Framebuffer * _targetBuffer
Definition: opengl-graphics.h:348
uint32 _cursorKeyColor
Definition: opengl-graphics.h:429
Definition: opengl-graphics.h:57
Definition: formatinfo.h:28
Definition: rect.h:45
Definition: renderbuffer.h:27
VideoState _currentState
Definition: opengl-graphics.h:225
bool isGLESContext() const
Definition: opengl-graphics.h:141
Graphics::PixelFormat _defaultFormat
Definition: opengl-graphics.h:338
float _cursorHeightScaled
Definition: opengl-graphics.h:424
int _cursorHotspotYScaled
Definition: opengl-graphics.h:414
int _cursorHotspotX
Definition: opengl-graphics.h:391
Definition: texture.h:166
bool _cursorUseKey
Definition: opengl-graphics.h:434
Surface * _gameScreen
Definition: opengl-graphics.h:353
Definition: opengl-graphics.h:182
Graphics::PixelFormat _defaultFormatAlpha
Definition: opengl-graphics.h:343
Definition: pipeline.h:42