forked from tenstorrent/whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteFrameBuffer.cpp
More file actions
153 lines (122 loc) · 3.51 KB
/
Copy pathRemoteFrameBuffer.cpp
File metadata and controls
153 lines (122 loc) · 3.51 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <sys/mman.h>
#include <cassert>
#include <thread>
#include <chrono>
#include <fstream>
#include <cstring>
#include "RemoteFrameBuffer.hpp"
#include <rfb/rfb.h>
using namespace WdRiscv;
// Don't change these
enum {
RFB_BITS_PER_SAMPLE = 8,
RFB_SAMPLES_PER_PIXEL = 3
};
// Time between RFB updates
enum {
RFB_FRAME_TIME_US = 100000
};
RemoteFrameBuffer::RemoteFrameBuffer(uint64_t addr, uint64_t width, uint64_t height, uint64_t bytes_per_pixel, int port)
: IoDevice("frame_buffer", addr, width*height*bytes_per_pixel), width_(width), height_(height), bytes_per_pixel_(bytes_per_pixel), port_(port) {
// TODO: either hard code it to be 4 or support 1,2,4 bpp
assert(bytes_per_pixel == 4 && "bytes per pixel must be 4");
// each value in the frame buffer represents a pixel
frame_buffer_.resize(size() >> 2 /* size in bytes */, 0);
auto func = [this]() { this->vncServerLoop(); };
displayThread_ = std::thread(func);
}
RemoteFrameBuffer::~RemoteFrameBuffer()
{
terminate_ = true;
if (displayThread_.joinable())
displayThread_.join();
}
void
RemoteFrameBuffer::vncServerLoop()
{
#ifdef REMOTE_FRAME_BUFFER
// libvncserver needs command line args passed to it
int rfbArgc = 1;
char programName[] = "whisper";
char *rfbArgv[] = {programName, nullptr};
rfbScreenInfoPtr rfbScreen = rfbGetScreen(&rfbArgc, rfbArgv, width_, height_, RFB_BITS_PER_SAMPLE, RFB_SAMPLES_PER_PIXEL, bytes_per_pixel_);
if (!rfbScreen)
return;
rfbScreen->desktopName = "Whisper VNC";
rfbScreen->frameBuffer = (char*) frame_buffer_.data();
rfbScreen->alwaysShared = TRUE;
rfbScreen->port = port_;
rfbInitServer(rfbScreen);
while(rfbIsActive(rfbScreen) && !terminate_)
{
rfbProcessEvents(rfbScreen, RFB_FRAME_TIME_US);
if (frame_buffer_updated_) {
rfbMarkRectAsModified(rfbScreen, 0, 0, width_, height_);
frame_buffer_updated_ = false;
}
}
rfbScreenCleanup(rfbScreen);
#endif
}
uint32_t
RemoteFrameBuffer::read(uint64_t addr)
{
uint64_t offset = addr - address();
if (offset >= size())
return 0;
return frame_buffer_.at(offset/4);
}
void
RemoteFrameBuffer::write(uint64_t addr, uint32_t value)
{
uint64_t offset = addr - address();
assert(offset < size() && "RemoteFrameBuffer: Writing outside of buffer range");
frame_buffer_.at(offset/4) = value;
frame_buffer_updated_ = true;
}
void
RemoteFrameBuffer::enable()
{
}
void
RemoteFrameBuffer::disable()
{
}
bool
RemoteFrameBuffer::saveSnapshot(const std::string& filename) const
{
std::ofstream ofs(filename, std::ios::binary);
if (!ofs)
{
std::cerr << "Error: failed to open snapshot file for writing: " << filename << "\n";
return false;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
ofs.write(reinterpret_cast<const char*>(frame_buffer_.data()), long(size()));
if (!ofs)
{
std::cerr << "Error: failed to write frame buffer data to: " << filename << "\n";
return false;
}
return true;
}
bool
RemoteFrameBuffer::loadSnapshot(const std::string& filename)
{
std::ifstream ifs(filename, std::ios::binary);
if (!ifs)
{
std::cerr << "Warning: failed to open snapshot file " << filename << "\n";
return false;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
ifs.read(reinterpret_cast<char*>(frame_buffer_.data()), long(size()));
if (!ifs)
{
std::cerr << "Error: failed to read frame buffer data from " << filename << "\n";
return false;
}
frame_buffer_updated_ = true;
return true;
}