Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/platform/windows/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// standard includes
#include <chrono>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -874,10 +875,29 @@ namespace platf::dxgi {
* @brief A session of a mosaic stream, as listed in osms-mosaic.txt.
*/
struct mosaic_entry_t {
uint32_t session; ///< Terminal Services session id.
uint32_t session; ///< Terminal Services session id; 0 while the session is (re)starting.
int cells; ///< Cells of the 2x2 grid it covers: 1 top-left, 2 top-right, 4 bottom-left, 8 bottom-right.
std::string waiting; ///< Image shown while the session publishes no frames (see read_waiting_image); empty = black.
};

/**
* @brief Image that OpenStreamMS draws for a part whose session is (re)starting.
*/
struct waiting_image_t {
int width = 0; ///< Width in pixels.
int height = 0; ///< Height in pixels.
std::vector<uint8_t> bgra; ///< Pixels, BGRA, top to bottom, width * 4 bytes per row.
};

/**
* @brief Read a waiting image: "OSWP", width and height as little-endian int32, then the
* BGRA pixels.
*
* @param bytes File content.
* @return The image, or nullopt when the content is not a valid image.
*/
std::optional<waiting_image_t> parse_waiting_image(const std::string &bytes);

/**
* @brief Content of osms-mosaic.txt.
*/
Expand Down Expand Up @@ -909,8 +929,9 @@ namespace platf::dxgi {
/**
* @brief Parse osms-mosaic.txt.
*
* Lines are "canvas <width> <height>" or "<session id> <part>"; lines starting with #
* are comments. Lines that do not parse, and parts that overlap an earlier one, are skipped.
* Lines are "canvas <width> <height>" or "<session id> <part> [<waiting image path>]";
* lines starting with # are comments. Session 0 keeps the part while its session is
* (re)starting. Lines that do not parse, and parts that overlap an earlier one, are skipped.
*
* @param text File content.
* @return Parsed layout.
Expand Down Expand Up @@ -995,6 +1016,8 @@ namespace platf::dxgi {
uint32_t frame_width = 0; ///< Size of the last frame copied, to notice a shrink.
uint32_t frame_height = 0; ///< Size of the last frame copied, to notice a shrink.
std::chrono::steady_clock::time_point next_open; ///< When to retry opening the slot.
texture2d_t waiting; ///< Image shown until the session publishes a frame; null = black.
bool showing_waiting = false; ///< The waiting image is on the canvas.
};

/**
Expand All @@ -1012,6 +1035,17 @@ namespace platf::dxgi {
* @brief Clear the canvas to black and copy every tile again on the next snapshot.
*/
void clear_canvas();
/**
* @brief Load the waiting image of a tile into a texture; on failure the tile stays black.
* @param tile Tile.
* @param path Image file written by OpenStreamMS.
*/
void load_waiting(mosaic_tile_t &tile, const std::string &path);
/**
* @brief Copy the waiting image of a tile onto its part of the canvas.
* @param tile Tile with a waiting image.
*/
void draw_waiting(mosaic_tile_t &tile);
/**
* @brief Snapshot in mosaic mode: copy the tiles that changed and then the canvas.
*/
Expand Down
73 changes: 71 additions & 2 deletions src/platform/windows/display_remoteidd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ namespace platf::dxgi {
}
continue;
}
// Rest of the line: the waiting image path (it may contain spaces).
std::string waiting;
std::getline(fields, waiting);
waiting.erase(0, waiting.find_first_not_of(" \t"));
uint32_t session = 0;
auto [end, ec] = std::from_chars(first.data(), first.data() + first.size(), session);
int cells = mosaic_area_cells(second);
Expand All @@ -142,11 +146,28 @@ namespace platf::dxgi {
continue;
}
used |= cells;
layout.entries.push_back({session, cells});
layout.entries.push_back({session, cells, std::move(waiting)});
}
return layout;
}

std::optional<waiting_image_t> parse_waiting_image(const std::string &bytes) {
if (bytes.size() < 12 || bytes.compare(0, 4, "OSWP") != 0) {
return std::nullopt;
}
auto read_int = [&](size_t at) {
return static_cast<int32_t>(static_cast<uint8_t>(bytes[at]) | (static_cast<uint8_t>(bytes[at + 1]) << 8) | (static_cast<uint8_t>(bytes[at + 2]) << 16) | (static_cast<uint32_t>(static_cast<uint8_t>(bytes[at + 3])) << 24));
};
waiting_image_t image;
image.width = read_int(4);
image.height = read_int(8);
if (image.width <= 0 || image.height <= 0 || image.width > 16384 || image.height > 16384 || bytes.size() != 12 + static_cast<size_t>(image.width) * image.height * 4) {
return std::nullopt;
}
image.bgra.assign(bytes.begin() + 12, bytes.end());
return image;
}

mosaic_rect_t mosaic_rect(int cells, int canvas_width, int canvas_height) {
const bool left = cells & 0b0101;
const bool right = cells & 0b1010;
Expand Down Expand Up @@ -218,6 +239,9 @@ namespace platf::dxgi {
tile.y = rect.y;
tile.width = rect.width;
tile.height = rect.height;
if (!e.waiting.empty()) {
load_waiting(tile, e.waiting);
}

if (e.session == session) {
// Absolute mouse: the client points at the canvas, the desktop is this tile.
Expand Down Expand Up @@ -255,7 +279,9 @@ namespace platf::dxgi {
clear_canvas();

for (auto &tile : tiles) {
open_tile(tile);
if (tile.session) {
open_tile(tile);
}
}
BOOST_LOG(info) << "RemoteIdd mosaic: "sv << tiles.size() << " sessions on a "sv << canvas_w << 'x' << canvas_h << " canvas"sv;
return 0;
Expand Down Expand Up @@ -294,7 +320,50 @@ namespace platf::dxgi {
device_ctx->ClearRenderTargetView(canvas_rt.get(), black);
for (auto &tile : tiles) {
tile.last_frame = 0;
tile.showing_waiting = false;
}
compose_now = true;
}

void display_ridd_vram_t::load_waiting(mosaic_tile_t &tile, const std::string &path) {
std::ifstream file(std::filesystem::path(reinterpret_cast<const char8_t *>(path.c_str())), std::ios::binary);
std::ostringstream content;
if (file) {
content << file.rdbuf();
}
auto image = parse_waiting_image(content.str());
if (!image) {
BOOST_LOG(warning) << "RemoteIdd mosaic: no valid waiting image at "sv << path;
return;
}
D3D11_TEXTURE2D_DESC desc {};
desc.Width = image->width;
desc.Height = image->height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
D3D11_SUBRESOURCE_DATA data {image->bgra.data(), static_cast<UINT>(image->width * 4), 0};
ID3D11Texture2D *raw = nullptr;
if (FAILED(device->CreateTexture2D(&desc, &data, &raw))) {
BOOST_LOG(warning) << "RemoteIdd mosaic: failed to create the waiting image texture"sv;
return;
}
tile.waiting.reset(raw);
}

void display_ridd_vram_t::draw_waiting(mosaic_tile_t &tile) {
D3D11_TEXTURE2D_DESC desc;
tile.waiting->GetDesc(&desc);
// Same size as the part (OpenStreamMS draws it so); otherwise centred and cropped.
const auto w = std::min<UINT>(desc.Width, tile.width);
const auto h = std::min<UINT>(desc.Height, tile.height);
const auto sx = (desc.Width - w) / 2;
const auto sy = (desc.Height - h) / 2;
D3D11_BOX box {sx, sy, 0, sx + w, sy + h, 1};
device_ctx->CopySubresourceRegion(canvas.get(), 0, tile.x + (tile.width - static_cast<int>(w)) / 2, tile.y + (tile.height - static_cast<int>(h)) / 2, 0, tile.waiting.get(), 0, &box);
tile.showing_waiting = true;
}
} // namespace platf::dxgi
8 changes: 7 additions & 1 deletion src/platform/windows/display_vram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,7 @@ namespace platf::dxgi {
std::vector<HANDLE> events;
events.reserve(tiles.size());
for (auto &tile : tiles) {
if (!tile.consumer && now >= tile.next_open) {
if (!tile.consumer && tile.session && now >= tile.next_open) {
open_tile(tile);
}
if (tile.consumer) {
Expand Down Expand Up @@ -2035,6 +2035,11 @@ namespace platf::dxgi {
uint64_t newest_qpc = 0;
for (auto &tile : tiles) {
if (!tile.consumer) {
// Session (re)starting: its waiting image until it publishes again.
if (tile.waiting && !tile.showing_waiting) {
draw_waiting(tile);
updated = true;
}
continue;
}
auto id = tile.consumer->LatestFrameId();
Expand Down Expand Up @@ -2072,6 +2077,7 @@ namespace platf::dxgi {
// is cropped to its part rather than scaled.
D3D11_BOX box {0, 0, 0, std::min<UINT>(frame.Width, tile.width), std::min<UINT>(frame.Height, tile.height), 1};
device_ctx->CopySubresourceRegion(canvas.get(), 0, tile.x, tile.y, 0, src.get(), 0, &box);
tile.showing_waiting = false;
tile.last_frame = frame.FrameId;
newest_qpc = std::max<uint64_t>(newest_qpc, frame.QpcPresent);
updated = true;
Expand Down
Loading
Loading