summaryrefslogtreecommitdiffstats
path: root/src/audio_core/adsp/mailbox.h
blob: c31b7371777ae3cd043180bab05ec78d017302fd (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: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "common/bounded_threadsafe_queue.h"
#include "common/common_types.h"

namespace AudioCore::ADSP {

enum class AppMailboxId : u32 {
    Invalid = 0,
    AudioRenderer = 50,
    AudioRendererMemoryMapUnmap = 51,
};

enum class Direction : u32 {
    Host,
    DSP,
};

struct MailboxMessage {
    u32 msg;
    std::span<u8> data;
};

class Mailbox {
public:
    void Initialize(AppMailboxId id_) {
        Reset();
        id = id_;
    }

    AppMailboxId Id() const noexcept {
        return id;
    }

    void Send(Direction dir, MailboxMessage&& message) {
        auto& queue = dir == Direction::Host ? host_queue : adsp_queue;
        queue.EmplaceWait(std::move(message));
    }

    MailboxMessage Receive(Direction dir, bool block = true) {
        auto& queue = dir == Direction::Host ? host_queue : adsp_queue;
        MailboxMessage t;
        if (block) {
            queue.PopWait(t);
        } else {
            queue.TryPop(t);
        }
        return t;
    }

    void Reset() {
        id = AppMailboxId::Invalid;
        MailboxMessage t;
        while (host_queue.TryPop(t)) {
        }
        while (adsp_queue.TryPop(t)) {
        }
    }

private:
    AppMailboxId id{0};
    Common::SPSCQueue<MailboxMessage> host_queue;
    Common::SPSCQueue<MailboxMessage> adsp_queue;
};

} // namespace AudioCore::ADSP