Compare commits
No commits in common. "ba5ccea2fe35142a31494780952932d21dbda10c" and "758df315a17bd7645f12f18a8e0eea04f6cd379a" have entirely different histories.
ba5ccea2fe
...
758df315a1
5 changed files with 158 additions and 140 deletions
|
@ -30,11 +30,7 @@ namespace hazelnut {
|
|||
|
||||
virtual bool Initialize() = 0;
|
||||
|
||||
/// New CaptureFrame() API; now capture is copied directly to output buffer
|
||||
/// instead of copy->copy.
|
||||
virtual DisplayCaptureResult CaptureFrameTemp(u32* pOut) = 0;
|
||||
|
||||
/// Performs the capture. THIS VERSION IS DEPRECATED AND WILL BE REMOVED
|
||||
/// Performs the capture.
|
||||
virtual DisplayCaptureResult CaptureFrame() = 0;
|
||||
|
||||
/// Get framebuffer information.
|
||||
|
@ -47,7 +43,6 @@ namespace hazelnut {
|
|||
enum class DisplayCaptureInterface : u32 {
|
||||
Invalid,
|
||||
NVFBC, // NVFBC capture
|
||||
|
||||
// FIXME: DXGI support
|
||||
};
|
||||
|
||||
|
|
|
@ -71,14 +71,9 @@ namespace hazelnut {
|
|||
unique_buffer<u32> convertedFramebuffer;
|
||||
|
||||
u8* pDiffMap;
|
||||
|
||||
u32 diffmapWidth;
|
||||
u32 diffmapHeight;
|
||||
|
||||
// cached
|
||||
u32 tileWidth;
|
||||
u32 tileHeight;
|
||||
|
||||
public:
|
||||
virtual ~NVFBCDisplayCapture() { Shutdown(); }
|
||||
|
||||
|
@ -169,104 +164,6 @@ namespace hazelnut {
|
|||
return true;
|
||||
}
|
||||
|
||||
void PaintWithTileOptimization(u32* pBufferData) {
|
||||
bool copiedTiles = false;
|
||||
|
||||
for(u32 dy = 0; dy < diffmapHeight; ++dy) {
|
||||
for(u32 dx = 0; dx < diffmapWidth; ++dx) {
|
||||
auto& bl = pDiffMap[dy * diffmapWidth + dx];
|
||||
|
||||
if(bl != 0) {
|
||||
copiedTiles = true;
|
||||
|
||||
#if 0
|
||||
x * (framebuffer.width / diff.diffmapWidth), // x
|
||||
y * (framebuffer.height / diff.diffMapHeight), // y
|
||||
framebuffer.width / diff.diffmapWidth, // width
|
||||
framebuffer.height / diff.diffMapHeight // height
|
||||
#endif
|
||||
|
||||
u32 xOffset = dx * tileWidth;
|
||||
u32 yOffset = dy * tileHeight;
|
||||
|
||||
for(u32 y = 0; y < tileHeight; ++y) {
|
||||
memcpy(&pBufferData[((y + yOffset) * width + xOffset)],
|
||||
&pRawFramebuffer[((y + yOffset) * grabInfo.dwBufferWidth + xOffset)], tileWidth * 4);
|
||||
}
|
||||
|
||||
// printf("copied %ux%u @ %ux%u tile\n", tileWidth, tileHeight, xOffset, yOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PaintFull(u32* pBufferData) {
|
||||
for(u32 y = 0; y < height; ++y) {
|
||||
// Convert to BGRA
|
||||
// FIXME: Make this SIMD. I can't into this very well
|
||||
#if 0
|
||||
usize srcStart = (y * grabInfo.dwBufferWidth) * 4;
|
||||
usize dstStart = (y * width) * 4;
|
||||
for(u32 x = 0; x < grabInfo.dwWidth * 4; x += 4) {
|
||||
pBufferData[(dstStart + x) + 0] = pSrcData[(srcStart + x) + 2]; // B
|
||||
pBufferData[(dstStart + x) + 1] = pSrcData[(srcStart + x) + 1]; // G
|
||||
pBufferData[(dstStart + x) + 2] = pSrcData[(srcStart + x) + 0]; // R
|
||||
pBufferData[(dstStart + x) + 3] = 0xff; // A
|
||||
}
|
||||
#endif
|
||||
|
||||
memcpy(&pBufferData[(y * width)], &pRawFramebuffer[(y * grabInfo.dwBufferWidth)], width * 4);
|
||||
}
|
||||
}
|
||||
|
||||
DisplayCaptureResult CaptureFrameTemp(u32* pOut) override {
|
||||
auto nvStatus = nvfbc->NvFBCToSysGrabFrame(&this->fbcSysGrabParams);
|
||||
|
||||
switch(nvStatus) {
|
||||
case NVFBC_SUCCESS: break;
|
||||
|
||||
// Need to recreate the session. If it fails then we fail too.
|
||||
case NVFBC_ERROR_INVALIDATED_SESSION: {
|
||||
if(!NvfbcInitSession())
|
||||
return DisplayCaptureResult::Fail;
|
||||
|
||||
// Recurse. This looks naughty, but will allow us to directly retry
|
||||
// the capture. (Plus if this causes issues whatever has happened is probably beyond saving)
|
||||
return CaptureFrame();
|
||||
} break;
|
||||
|
||||
default: return DisplayCaptureResult::Fail;
|
||||
}
|
||||
|
||||
auto result = DisplayCaptureResult::Ok;
|
||||
bool useTilePainting = true;
|
||||
|
||||
if(width != grabInfo.dwWidth || height != grabInfo.dwHeight) {
|
||||
width = grabInfo.dwWidth;
|
||||
height = grabInfo.dwHeight;
|
||||
|
||||
// update diffmap stuff
|
||||
diffmapWidth = DiffMapDimension(width, blockSize);
|
||||
diffmapHeight = DiffMapDimension(height, blockSize);
|
||||
|
||||
tileWidth = width / diffmapWidth;
|
||||
tileHeight = height / diffmapHeight;
|
||||
|
||||
result = DisplayCaptureResult::OkButResized;
|
||||
|
||||
// Disable tile-painting optimization for the first frame
|
||||
useTilePainting = false;
|
||||
}
|
||||
|
||||
if(useTilePainting) [[likely]] {
|
||||
PaintWithTileOptimization(&pOut[0]);
|
||||
} else {
|
||||
PaintFull(&pOut[0]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
DisplayCaptureResult CaptureFrame() override {
|
||||
auto nvStatus = nvfbc->NvFBCToSysGrabFrame(&this->fbcSysGrabParams);
|
||||
|
||||
|
@ -328,6 +225,8 @@ namespace hazelnut {
|
|||
// diffmapWidth = (u32)ceil((f32)width / 32);
|
||||
// diffmapHeight = (u32)ceil((f32)height / 32);
|
||||
|
||||
diffmapWidth = DiffMapDimension(width, blockSize);
|
||||
diffmapHeight = DiffMapDimension(height, blockSize);
|
||||
return DiffInformation { pDiffMap, diffmapWidth, diffmapHeight };
|
||||
}
|
||||
};
|
||||
|
|
|
@ -6,23 +6,28 @@
|
|||
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
|
||||
#include "capture.hpp"
|
||||
#include "Utils.hpp"
|
||||
// clang-format on
|
||||
|
||||
#include "atomic_spinlock.hpp"
|
||||
#include "ivshmem_protocol.hpp"
|
||||
|
||||
struct tileRect {
|
||||
u32 x, y, width, height;
|
||||
};
|
||||
|
||||
struct Test {
|
||||
hazelnut::AtomicSpinlock lk {};
|
||||
std::atomic<u32> sessionId {};
|
||||
std::atomic<u32> pingPong {};
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
hazelnut::IvshmemDevice dev;
|
||||
|
||||
// Create a capture interface
|
||||
auto pCaptureInterface = hazelnut::CreateDisplayCapture(hazelnut::GuessBestCaptureInterface());
|
||||
if(!pCaptureInterface) {
|
||||
printf("Failed to create a capture interface\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!dev.Open()) {
|
||||
printf("Failed to open ivshmem device\n");
|
||||
return 1;
|
||||
|
@ -36,39 +41,162 @@ int main(int argc, char** argv) {
|
|||
// wipe the first 1mb
|
||||
memset(&ptr[0], 0, 1 * (1024 * 1024));
|
||||
|
||||
// initalize laid-out IVSHMEM structs
|
||||
printf("wiped memory\n");
|
||||
|
||||
// sex
|
||||
auto* pHeader = new(&ptr[0]) hazelnut::IvshHeader {};
|
||||
auto* pFrameHeader = new(&ptr[0x1000]) hazelnut::FrameHeader {};
|
||||
|
||||
// Reset session ID
|
||||
// reset pingpong counter
|
||||
|
||||
pHeader->serverSessionId.store(rand());
|
||||
|
||||
printf("Agent loop starting\n");
|
||||
// Create a capture interface
|
||||
auto capture = hazelnut::CreateDisplayCapture(hazelnut::GuessBestCaptureInterface());
|
||||
if(!capture) {
|
||||
printf("Failed to create a capture interface\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Successfully created a framebuffer capture interface\n");
|
||||
|
||||
bool firstFrame = true;
|
||||
std::vector<tileRect> tiles {};
|
||||
hazelnut::FramebufferInformation framebuffer {};
|
||||
hazelnut::DiffInformation diff {};
|
||||
|
||||
while(true) {
|
||||
{
|
||||
auto guard = pHeader->lock.lock();
|
||||
auto result = pCaptureInterface->CaptureFrameTemp(pFrameHeader->bits());
|
||||
auto result = capture->CaptureFrame();
|
||||
if(result == hazelnut::DisplayCaptureResult::Ok) {
|
||||
tiles.clear();
|
||||
#if 0
|
||||
if(firstFrame == false) {
|
||||
for(u32 y = 0; y < diff.diffMapHeight; ++y) {
|
||||
for(u32 x = 0; x < diff.diffmapWidth; ++x) {
|
||||
auto& bl = diff.pDiffMap[y * diff.diffmapWidth + x];
|
||||
if(bl != 0) {
|
||||
tiles.push_back(tileRect {
|
||||
x * (framebuffer.width / diff.diffmapWidth), // x
|
||||
y * (framebuffer.height / diff.diffMapHeight), // y
|
||||
framebuffer.width / diff.diffmapWidth, // width
|
||||
framebuffer.height / diff.diffMapHeight // height
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(result == hazelnut::DisplayCaptureResult::Ok) {
|
||||
// Do nothing.
|
||||
} else if(result == hazelnut::DisplayCaptureResult::OkButResized) {
|
||||
// We resized. Notify of that
|
||||
framebuffer = pCaptureInterface->GetFramebufferInformation();
|
||||
diff = pCaptureInterface->GetDiffInformation();
|
||||
} else {
|
||||
printf("Failed to capture\n");
|
||||
break;
|
||||
if(tiles.empty())
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
auto guard = pHeader->lock.lock();
|
||||
|
||||
pFrameHeader->serial.fetch_add(1);
|
||||
pFrameHeader->width.store(framebuffer.width);
|
||||
pFrameHeader->height.store(framebuffer.height);
|
||||
|
||||
if(framebuffer.pFramebuffer == nullptr)
|
||||
continue;
|
||||
|
||||
memcpy(pFrameHeader->bits(), &framebuffer.pFramebuffer[0], (framebuffer.width * framebuffer.height) * 4);
|
||||
|
||||
//printf("FRAME SERIAL %u loaded\n", pFrameHeader->serial.load());
|
||||
}
|
||||
|
||||
pFrameHeader->serial.fetch_add(1);
|
||||
pFrameHeader->width.store(framebuffer.width);
|
||||
pFrameHeader->height.store(framebuffer.height);
|
||||
if(firstFrame)
|
||||
firstFrame = false;
|
||||
} else if(result == hazelnut::DisplayCaptureResult::OkButResized) {
|
||||
// We resized. Notify of that
|
||||
framebuffer = capture->GetFramebufferInformation();
|
||||
diff = capture->GetDiffInformation();
|
||||
firstFrame = true;
|
||||
|
||||
} else {
|
||||
printf("Failed to capture\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
while(true) {
|
||||
// lock
|
||||
{
|
||||
auto guard = pHeader->lk.lock();
|
||||
Sleep(5);
|
||||
}
|
||||
|
||||
|
||||
printf("pingpong %u\n", pHeader->pingPong.load());
|
||||
|
||||
if(tries++ == curTries) {
|
||||
tries = 0;
|
||||
pHeader->pingPong.fetch_add(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
||||
#if 0
|
||||
printf("Hazelnut agent\n");
|
||||
|
||||
// Create a capture interface
|
||||
auto capture = hazelnut::CreateDisplayCapture(hazelnut::GuessBestCaptureInterface());
|
||||
if(!capture) {
|
||||
printf("Failed to create a capture interface\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Successfully created a framebuffer capture interface\n");
|
||||
|
||||
|
||||
bool firstFrame = true;
|
||||
std::vector<tileRect> tiles {};
|
||||
hazelnut::FramebufferInformation framebuffer {};
|
||||
hazelnut::DiffInformation diff {};
|
||||
|
||||
while(true) {
|
||||
auto result = capture->CaptureFrame();
|
||||
if(result == hazelnut::DisplayCaptureResult::Ok) {
|
||||
tiles.clear();
|
||||
|
||||
if(firstFrame == false) {
|
||||
for(u32 y = 0; y < diff.diffMapHeight; ++y) {
|
||||
for(u32 x = 0; x < diff.diffmapWidth; ++x) {
|
||||
auto& bl = diff.pDiffMap[y * diff.diffmapWidth + x];
|
||||
if(bl != 0) {
|
||||
tiles.push_back(tileRect {
|
||||
x * (framebuffer.width / diff.diffmapWidth), // x
|
||||
y * (framebuffer.height / diff.diffMapHeight), // y
|
||||
framebuffer.width / diff.diffmapWidth, // width
|
||||
framebuffer.height / diff.diffMapHeight // height
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(tiles.empty())
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(firstFrame)
|
||||
firstFrame = false;
|
||||
} else if(result == hazelnut::DisplayCaptureResult::OkButResized) {
|
||||
// We resized. Notify of that
|
||||
framebuffer = capture->GetFramebufferInformation();
|
||||
diff = capture->GetDiffInformation();
|
||||
firstFrame = true;
|
||||
|
||||
} else {
|
||||
printf("Failed to capture\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
# shared code
|
||||
|
||||
This is all of the things shared between the agent and client ends.
|
||||
|
||||
It is cross platform C++20.
|
||||
This is shared between the two ends. It is cross platform C++20.
|
|
@ -30,10 +30,8 @@ namespace hazelnut {
|
|||
void lock_manually() {
|
||||
u32 expected = 0;
|
||||
while(!__lock.compare_exchange_strong(expected, 1, std::memory_order::seq_cst)) {
|
||||
//printf("LOCK CONTENDED\n");
|
||||
expected = 0;
|
||||
#ifdef _WIN32
|
||||
//Sleep(1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue