summaryrefslogtreecommitdiffstats
path: root/src/core/frontend/graphics_context.h
blob: 064b19a9655c9751101bd2a30ebdac849968b1e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <optional>
#include <string>

namespace Core::Frontend {

/**
 * Represents a drawing context that supports graphics operations.
 */
class GraphicsContext {
public:
    virtual ~GraphicsContext() = default;

    /// Inform the driver to swap the front/back buffers and present the current image
    virtual void SwapBuffers() {}

    /// Makes the graphics context current for the caller thread
    virtual void MakeCurrent() {}

    /// Releases (dunno if this is the "right" word) the context from the caller thread
    virtual void DoneCurrent() {}

    /// Parameters used to configure custom drivers (used by Android only)
    struct CustomDriverParameters {
        std::string hook_lib_dir;
        std::string custom_driver_dir;
        std::string custom_driver_name;
        std::string file_redirect_dir;
    };

    /// Gets custom driver parameters configured by the frontend (used by Android only)
    virtual std::optional<CustomDriverParameters> GetCustomDriverParameters() {
        return {};
    }

    class Scoped {
    public:
        [[nodiscard]] explicit Scoped(GraphicsContext& context_) : context(context_) {
            context.MakeCurrent();
        }
        ~Scoped() {
            if (active) {
                context.DoneCurrent();
            }
        }

        /// In the event that context was destroyed before the Scoped is destroyed, this provides a
        /// mechanism to prevent calling a destroyed object's method during the deconstructor
        void Cancel() {
            active = false;
        }

    private:
        GraphicsContext& context;
        bool active{true};
    };

    /// Calls MakeCurrent on the context and calls DoneCurrent when the scope for the returned value
    /// ends
    [[nodiscard]] Scoped Acquire() {
        return Scoped{*this};
    }
};

} // namespace Core::Frontend