diff options
Diffstat (limited to 'src/core/frontend/graphics_context.h')
| -rw-r--r-- | src/core/frontend/graphics_context.h | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/core/frontend/graphics_context.h b/src/core/frontend/graphics_context.h new file mode 100644 index 000000000..7554c1583 --- /dev/null +++ b/src/core/frontend/graphics_context.h | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <memory> | ||
| 7 | |||
| 8 | #include "common/dynamic_library.h" | ||
| 9 | |||
| 10 | namespace Core::Frontend { | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Represents a drawing context that supports graphics operations. | ||
| 14 | */ | ||
| 15 | class GraphicsContext { | ||
| 16 | public: | ||
| 17 | virtual ~GraphicsContext() = default; | ||
| 18 | |||
| 19 | /// Inform the driver to swap the front/back buffers and present the current image | ||
| 20 | virtual void SwapBuffers() {} | ||
| 21 | |||
| 22 | /// Makes the graphics context current for the caller thread | ||
| 23 | virtual void MakeCurrent() {} | ||
| 24 | |||
| 25 | /// Releases (dunno if this is the "right" word) the context from the caller thread | ||
| 26 | virtual void DoneCurrent() {} | ||
| 27 | |||
| 28 | /// Gets the GPU driver library (used by Android only) | ||
| 29 | virtual std::shared_ptr<Common::DynamicLibrary> GetDriverLibrary() { | ||
| 30 | return {}; | ||
| 31 | } | ||
| 32 | |||
| 33 | class Scoped { | ||
| 34 | public: | ||
| 35 | [[nodiscard]] explicit Scoped(GraphicsContext& context_) : context(context_) { | ||
| 36 | context.MakeCurrent(); | ||
| 37 | } | ||
| 38 | ~Scoped() { | ||
| 39 | if (active) { | ||
| 40 | context.DoneCurrent(); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | /// In the event that context was destroyed before the Scoped is destroyed, this provides a | ||
| 45 | /// mechanism to prevent calling a destroyed object's method during the deconstructor | ||
| 46 | void Cancel() { | ||
| 47 | active = false; | ||
| 48 | } | ||
| 49 | |||
| 50 | private: | ||
| 51 | GraphicsContext& context; | ||
| 52 | bool active{true}; | ||
| 53 | }; | ||
| 54 | |||
| 55 | /// Calls MakeCurrent on the context and calls DoneCurrent when the scope for the returned value | ||
| 56 | /// ends | ||
| 57 | [[nodiscard]] Scoped Acquire() { | ||
| 58 | return Scoped{*this}; | ||
| 59 | } | ||
| 60 | }; | ||
| 61 | |||
| 62 | } // namespace Core::Frontend | ||