smasherprog / screen_capture_lite

cross platform screen/window capturing library
MIT License
616 stars 156 forks source link

Buffix/unity il2cpp #122

Closed ptwohig closed 2 years ago

smasherprog commented 2 years ago

So, everything looks good. What stride copy code are you referring to Patrick? Also, I pushed up a different implementation of UnmanagedHandles class and called it UnmanagedHandlesV2 I didnt go through with implementing it, but I think the code accomplishes the same thing, and is more clear. Let me know if im correct in that assumption.

smasherprog commented 2 years ago

Overall, the PR looks great

smasherprog commented 2 years ago

I added finalizers to the c# managed classes. Please, review and let me know what you think

ptwohig commented 2 years ago

So, the UnmangedHandles thing came out of the following discoveries:

The UnmanagedHandles class is essentially a lock-free copy on write. It means that adding or removing a handle is a slow operation. However, that should be happening very infrequently. Reads, which are performance critical, are extremely fast and do not require the acquisition of a lock for every frame grab. I'm not sure if the ConcurrentList does that or not. I know that using a pointer as a handle introduces a bunch of indirection (cache misses) but I think this is the best we can do given the circumstances and the APIs we want. I haven't profiled which approach is faster so it's a guess at this point. I just wrote it how I write things.

The stride copy function is ...

void* SCL_Utility_CopyToContiguous(unsigned char *destination, SCL_ImageRef image);

Basically this should copy the strided data to a contiguous array into the supplied destination. It appears that Unity doesn't have a straightforward way to do that (it can be done using IntPtr and a bunch of crap) but I would rather let native code so as much of the heavy lifting when it comes to a copy operation. I just need to add an integration test that ensures it doesn't over-write or over-read.

ptwohig commented 2 years ago

@smasherprog Alright. I updated the code. I don't see any unit/integration tests so I just made it test the function at the beginning of the example program. I also worked it out so that function will properly and efficiently copy non-contiguous data to the memory location specified. If passed contiguous data, it results in a simple std::memcpy call. This should allow C# clients to avoid having to pick apart an image that happens to have a stride.

smasherprog commented 2 years ago

Working for the next 8 hours. After that ill give it another check.

smasherprog commented 2 years ago

Looks good to me! Ill take a look at the UnmangedHandles class a bit later. I think the concurrentqueue might accomplish what your trying to do with simpler code.