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 "common/frac.h"
29 #include "common/mutex.h"
30 #include "common/ustr.h"
31 
32 #include "graphics/surface.h"
33 
34 namespace Graphics {
35 class Font;
36 } // End of namespace Graphics
37 
38 namespace OpenGL {
39 
40 // HACK: We use glColor in the OSD code. This might not be working on GL ES but
41 // we still enable it because Tizen already shipped with it. Also, the
42 // SurfaceSDL backend enables it and disabling it can cause issues in sdl.cpp.
43 #define USE_OSD 1
44 
45 class Surface;
46 class Pipeline;
47 #if !USE_FORCED_GLES
48 class LibRetroPipeline;
49 #endif
50 
51 enum {
52  GFX_OPENGL = 0
53 };
54 
56 public:
58  virtual ~OpenGLGraphicsManager();
59 
60  // GraphicsManager API
61  bool hasFeature(OSystem::Feature f) const override;
62  void setFeatureState(OSystem::Feature f, bool enable) override;
63  bool getFeatureState(OSystem::Feature f) const override;
64 
65  const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
66  int getDefaultGraphicsMode() const override;
67  bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override;
68  int getGraphicsMode() const override;
69 
70 #ifdef USE_RGB_COLOR
71  Graphics::PixelFormat getScreenFormat() const override;
72  Common::List<Graphics::PixelFormat> getSupportedFormats() const override;
73 #endif
74 
75  const OSystem::GraphicsMode *getSupportedStretchModes() const override;
76  int getDefaultStretchMode() const override;
77  bool setStretchMode(int mode) override;
78  int getStretchMode() const override;
79 
80 #ifdef USE_SCALERS
81  uint getDefaultScaler() const override;
82  uint getDefaultScaleFactor() const override;
83  bool setScaler(uint mode, int factor) override;
84  uint getScaler() const override;
85  uint getScaleFactor() const override;
86 #endif
87 
88 #if !USE_FORCED_GLES
89  bool setShader(const Common::Path &fileNode) override;
90 #endif
91 
92  void beginGFXTransaction() override;
93  OSystem::TransactionError endGFXTransaction() override;
94 
95  int getScreenChangeID() const override;
96 
97  void initSize(uint width, uint height, const Graphics::PixelFormat *format) override;
98 
99  int16 getWidth() const override;
100  int16 getHeight() const override;
101 
102  void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h) override;
103  void fillScreen(uint32 col) override;
104  void fillScreen(const Common::Rect &r, uint32 col) override;
105 
106  void updateScreen() override;
107 
108  Graphics::Surface *lockScreen() override;
109  void unlockScreen() override;
110 
111  void setFocusRectangle(const Common::Rect& rect) override;
112  void clearFocusRectangle() override;
113 
114  int16 getOverlayWidth() const override;
115  int16 getOverlayHeight() const override;
116 
117  Graphics::PixelFormat getOverlayFormat() const override;
118 
119  void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h) override;
120  void clearOverlay() override;
121  void grabOverlay(Graphics::Surface &surface) const override;
122 
123  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;
124  void setCursorPalette(const byte *colors, uint start, uint num) override;
125 
126  void displayMessageOnOSD(const Common::U32String &msg) override;
127  void displayActivityIconOnOSD(const Graphics::Surface *icon) override;
128 
129  // PaletteManager interface
130  void setPalette(const byte *colors, uint start, uint num) override;
131  void grabPalette(byte *colors, uint start, uint num) const override;
132 
133 protected:
134  void renderCursor();
135 
139  bool isGLESContext() const { return OpenGLContext.type == kContextGLES || OpenGLContext.type == kContextGLES2; }
140 
151  void notifyContextCreate(
152  ContextType type,
153  Framebuffer *target,
154  const Graphics::PixelFormat &defaultFormat,
155  const Graphics::PixelFormat &defaultFormatAlpha);
156 
163  void notifyContextDestroy();
164 
175  Surface *createSurface(const Graphics::PixelFormat &format, bool wantAlpha = false, bool wantScaler = false, bool wantMask = false);
176 
177  //
178  // Transaction support
179  //
180  struct VideoState {
181  VideoState() : valid(false), gameWidth(0), gameHeight(0),
182 #ifdef USE_RGB_COLOR
183  gameFormat(),
184 #endif
185  aspectRatioCorrection(false), graphicsMode(GFX_OPENGL), filtering(true),
186  scalerIndex(0), scaleFactor(1), shader() {
187  }
188 
189  bool valid;
190 
191  uint gameWidth, gameHeight;
192 #ifdef USE_RGB_COLOR
193  Graphics::PixelFormat gameFormat;
194 #endif
195  bool aspectRatioCorrection;
196  int graphicsMode;
197  bool filtering;
198 
199  uint scalerIndex;
200  int scaleFactor;
201 
202  Common::Path shader;
203 
204  bool operator==(const VideoState &right) {
205  return gameWidth == right.gameWidth && gameHeight == right.gameHeight
206 #ifdef USE_RGB_COLOR
207  && gameFormat == right.gameFormat
208 #endif
209  && aspectRatioCorrection == right.aspectRatioCorrection
210  && graphicsMode == right.graphicsMode
211  && filtering == right.filtering
212  && shader == right.shader;
213  }
214 
215  bool operator!=(const VideoState &right) {
216  return !(*this == right);
217  }
218  };
219 
224 
229 
230 protected:
231  enum TransactionMode {
232  kTransactionNone = 0,
233  kTransactionActive = 1,
234  kTransactionRollback = 2
235  };
236 
237  TransactionMode getTransactionMode() const { return _transactionMode; }
238 
239 private:
243  TransactionMode _transactionMode;
244 
248  int _screenChangeID;
249 
253  int _stretchMode;
254 
258  Common::Point _shakeOffsetScaled;
259 
260 protected:
274  virtual bool loadVideoMode(uint requestedWidth, uint requestedHeight, const Graphics::PixelFormat &format) = 0;
275 
276  bool loadShader(const Common::Path &fileName);
277 
281  virtual void refreshScreen() = 0;
282 
289  bool saveScreenshot(const Common::Path &filename) const;
290 
291  // Do not hide the argument-less saveScreenshot from the base class
292  using WindowedGraphicsManager::saveScreenshot;
293 
294 private:
295  //
296  // OpenGL utilities
297  //
298 
302  void initializeGLContext();
303 
307  Pipeline *_pipeline;
308 
309 #if !USE_FORCED_GLES
310 
313  LibRetroPipeline *_libretroPipeline;
314 #endif
315 
316 protected:
322  bool getGLPixelFormat(const Graphics::PixelFormat &pixelFormat, GLenum &glIntFormat, GLenum &glFormat, GLenum &glType) const;
323 
324  bool gameNeedsAspectRatioCorrection() const override;
325  int getGameRenderScale() const override;
326  void recalculateDisplayAreas() override;
327  void handleResizeImpl(const int width, const int height) override;
328 
329  void updateLinearFiltering();
330 
331  Pipeline *getPipeline() const { return _pipeline; }
332 
337 
342 
347 
352 
356  byte _gamePalette[3 * 256];
357 
358  //
359  // Overlay
360  //
361 
366 
367  //
368  // Cursor
369  //
370 
374  void updateCursorPalette();
375 
380 
385 
390 
395 
400  void recalculateCursorScaling();
401 
407 
413 
418 
423 
428 
433 
438 
443 
447  byte _cursorPalette[3 * 256];
448 
449 #ifdef USE_SCALERS
450 
453  const PluginList &_scalerPlugins;
454 #endif
455 
456 #ifdef USE_OSD
457  //
458  // OSD
459  //
460 protected:
464  virtual const Graphics::Font *getFontOSD() const;
465 
466 private:
470  bool _osdMessageChangeRequest;
471 
478  Common::U32String _osdMessageNextData;
479 
483  void osdMessageUpdateSurface();
484 
488  Surface *_osdMessageSurface;
489 
493  uint8 _osdMessageAlpha;
494 
498  uint32 _osdMessageFadeStartTime;
499 
500  enum {
501  kOSDMessageFadeOutDelay = 2 * 1000,
502  kOSDMessageFadeOutDuration = 500,
503  kOSDMessageInitialAlpha = 80
504  };
505 
509  Surface *_osdIconSurface;
510 
511  enum {
512  kOSDIconTopMargin = 10,
513  kOSDIconRightMargin = 10
514  };
515 #endif
516 };
517 
518 } // End of namespace OpenGL
519 
520 #endif
Definition: font.h:82
Definition: surface.h:66
Definition: system.h:788
Definition: libretro.h:50
VideoState _oldState
Definition: opengl-graphics.h:228
Definition: array.h:52
bool _cursorPaletteEnabled
Definition: opengl-graphics.h:442
Definition: pixelformat.h:138
Definition: system.h:739
Feature
Definition: system.h:417
Definition: framebuffer.h:36
int _cursorHotspotY
Definition: opengl-graphics.h:394
Definition: list.h:44
Definition: rect.h:144
Definition: path.h:52
bool _cursorDontScale
Definition: opengl-graphics.h:437
Surface * _overlay
Definition: opengl-graphics.h:365
Surface * _cursorMask
Definition: opengl-graphics.h:384
float _cursorWidthScaled
Definition: opengl-graphics.h:417
Definition: windowed.h:52
Surface * _cursor
Definition: opengl-graphics.h:379
Definition: ustr.h:57
int _cursorHotspotXScaled
Definition: opengl-graphics.h:406
TransactionError
Definition: system.h:1148
Framebuffer * _targetBuffer
Definition: opengl-graphics.h:346
uint32 _cursorKeyColor
Definition: opengl-graphics.h:427
Definition: opengl-graphics.h:55
Definition: formatinfo.h:28
Definition: rect.h:45
Definition: renderbuffer.h:27
VideoState _currentState
Definition: opengl-graphics.h:223
bool isGLESContext() const
Definition: opengl-graphics.h:139
Graphics::PixelFormat _defaultFormat
Definition: opengl-graphics.h:336
float _cursorHeightScaled
Definition: opengl-graphics.h:422
int _cursorHotspotYScaled
Definition: opengl-graphics.h:412
int _cursorHotspotX
Definition: opengl-graphics.h:389
Definition: texture.h:166
bool _cursorUseKey
Definition: opengl-graphics.h:432
Surface * _gameScreen
Definition: opengl-graphics.h:351
Definition: opengl-graphics.h:180
Graphics::PixelFormat _defaultFormatAlpha
Definition: opengl-graphics.h:341
Definition: pipeline.h:42