diff --git a/Assets/uWindowCapture/Examples/Buffer.meta b/Assets/uWindowCapture/Examples/Buffer.meta new file mode 100644 index 0000000..18d9adc --- /dev/null +++ b/Assets/uWindowCapture/Examples/Buffer.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d12d0334b47757d44b007087a9c67f17 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uWindowCapture/Examples/Buffer/Buffer.unity b/Assets/uWindowCapture/Examples/Buffer/Buffer.unity new file mode 100644 index 0000000..b27aa00 Binary files /dev/null and b/Assets/uWindowCapture/Examples/Buffer/Buffer.unity differ diff --git a/Assets/uWindowCapture/Examples/Buffer/Buffer.unity.meta b/Assets/uWindowCapture/Examples/Buffer/Buffer.unity.meta new file mode 100644 index 0000000..ac07ffe --- /dev/null +++ b/Assets/uWindowCapture/Examples/Buffer/Buffer.unity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b4f1c89692f972c4a8ccd8183ed116fe +timeCreated: 1480840219 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs b/Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs new file mode 100644 index 0000000..078be37 --- /dev/null +++ b/Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs @@ -0,0 +1,86 @@ +using UnityEngine; +using System; +using System.Runtime.InteropServices; + +namespace uWindowCapture +{ + +public class UwcGetBufferExample : MonoBehaviour +{ + [SerializeField] + UwcWindowTexture uwcTexture; + + Texture2D texture_; + Color32[] pixels_; + GCHandle handle_; + IntPtr ptr_ = IntPtr.Zero; + + [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + public static extern IntPtr memcpy(IntPtr dest, IntPtr src, int count); + + bool isValid + { + get + { + if (!uwcTexture) return false; + + var window = uwcTexture.window; + return window != null && window.buffer != IntPtr.Zero; + } + } + + void OnDestroy() + { + if (ptr_ != IntPtr.Zero) { + handle_.Free(); + } + } + + void Update() + { + if (!isValid) return; + + var window = uwcTexture.window; + if (texture_ == null || + window.bufferWidth != texture_.width || + window.bufferHeight != texture_.height) { + UpdateTexture(); + } + + CopyTexture(); + } + + void UpdateTexture() + { + if (!isValid) return; + + var window = uwcTexture.window; + var width = window.bufferWidth; + var height = window.bufferHeight; + + texture_ = new Texture2D(width, height, TextureFormat.RGBA32, false); + texture_.filterMode = FilterMode.Bilinear; + pixels_ = texture_.GetPixels32(); + handle_ = GCHandle.Alloc(pixels_, GCHandleType.Pinned); + ptr_ = handle_.AddrOfPinnedObject(); + + GetComponent().material.mainTexture = texture_; + } + + void CopyTexture() + { + if (!isValid) return; + + var window = uwcTexture.window; + var width = window.bufferWidth; + var height = window.bufferHeight; + var buffer = window.buffer; + + memcpy(ptr_, buffer, width * height * sizeof(Byte) * 4); + + texture_.SetPixels32(pixels_); + texture_.Apply(); + } +} + +} \ No newline at end of file diff --git a/Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs.meta b/Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs.meta new file mode 100644 index 0000000..0a1790b --- /dev/null +++ b/Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fbc4bb60993b4f4f8c648e6578c4299 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uWindowCapture/Plugins/x86/uWindowCapture.dll b/Assets/uWindowCapture/Plugins/x86/uWindowCapture.dll index 0994dbb..77ee2fa 100644 Binary files a/Assets/uWindowCapture/Plugins/x86/uWindowCapture.dll and b/Assets/uWindowCapture/Plugins/x86/uWindowCapture.dll differ diff --git a/Assets/uWindowCapture/Plugins/x86_64/uWindowCapture.dll b/Assets/uWindowCapture/Plugins/x86_64/uWindowCapture.dll index 19a0dee..c6f8dbf 100644 Binary files a/Assets/uWindowCapture/Plugins/x86_64/uWindowCapture.dll and b/Assets/uWindowCapture/Plugins/x86_64/uWindowCapture.dll differ diff --git a/Assets/uWindowCapture/Scripts/UwcLib.cs b/Assets/uWindowCapture/Scripts/UwcLib.cs index db746bb..757f7b7 100644 --- a/Assets/uWindowCapture/Scripts/UwcLib.cs +++ b/Assets/uWindowCapture/Scripts/UwcLib.cs @@ -121,6 +121,8 @@ public static class Lib public static extern int GetWindowHeight(int id); [DllImport(name, EntryPoint = "UwcGetWindowZOrder")] public static extern int GetWindowZOrder(int id); + [DllImport(name, EntryPoint = "UwcGetWindowBuffer")] + public static extern IntPtr GetWindowBuffer(int id); [DllImport(name, EntryPoint = "UwcGetWindowBufferWidth")] public static extern int GetWindowBufferWidth(int id); [DllImport(name, EntryPoint = "UwcGetWindowBufferHeight")] diff --git a/Assets/uWindowCapture/Scripts/UwcWindow.cs b/Assets/uWindowCapture/Scripts/UwcWindow.cs index 5f2b781..ae27267 100644 --- a/Assets/uWindowCapture/Scripts/UwcWindow.cs +++ b/Assets/uWindowCapture/Scripts/UwcWindow.cs @@ -186,6 +186,11 @@ public int zOrder get { return Lib.GetWindowZOrder(id); } } + public System.IntPtr buffer + { + get { return Lib.GetWindowBuffer(id); } + } + public int bufferWidth { get { return Lib.GetWindowBufferWidth(id); } diff --git a/Plugins/uWindowCapture/uWindowCapture/Main.cpp b/Plugins/uWindowCapture/uWindowCapture/Main.cpp index 71517be..e44e4c1 100644 --- a/Plugins/uWindowCapture/uWindowCapture/Main.cpp +++ b/Plugins/uWindowCapture/uWindowCapture/Main.cpp @@ -282,6 +282,15 @@ extern "C" return 0; } + UNITY_INTERFACE_EXPORT BYTE* UNITY_INTERFACE_API UwcGetWindowBuffer(int id) + { + if (auto window = GetWindow(id)) + { + return window->GetBuffer(); + } + return nullptr; + } + UNITY_INTERFACE_EXPORT UINT UNITY_INTERFACE_API UwcGetWindowBufferWidth(int id) { if (auto window = GetWindow(id)) diff --git a/Plugins/uWindowCapture/uWindowCapture/Window.cpp b/Plugins/uWindowCapture/uWindowCapture/Window.cpp index 3e8dd51..7995b43 100644 --- a/Plugins/uWindowCapture/uWindowCapture/Window.cpp +++ b/Plugins/uWindowCapture/uWindowCapture/Window.cpp @@ -199,6 +199,12 @@ UINT Window::GetZOrder() const } +BYTE* Window::GetBuffer() const +{ + return windowTexture_->GetBuffer(); +} + + UINT Window::GetBufferWidth() const { return windowTexture_->GetWidth(); diff --git a/Plugins/uWindowCapture/uWindowCapture/Window.h b/Plugins/uWindowCapture/uWindowCapture/Window.h index 10865c2..d3a3816 100644 --- a/Plugins/uWindowCapture/uWindowCapture/Window.h +++ b/Plugins/uWindowCapture/uWindowCapture/Window.h @@ -63,6 +63,7 @@ friend class WindowManager; UINT GetClientWidth() const; UINT GetClientHeight() const; UINT GetZOrder() const; + BYTE* GetBuffer() const; UINT GetBufferWidth() const; UINT GetBufferHeight() const; UINT GetIconWidth() const; diff --git a/Plugins/uWindowCapture/uWindowCapture/WindowTexture.cpp b/Plugins/uWindowCapture/uWindowCapture/WindowTexture.cpp index 0b3c717..091a13f 100644 --- a/Plugins/uWindowCapture/uWindowCapture/WindowTexture.cpp +++ b/Plugins/uWindowCapture/uWindowCapture/WindowTexture.cpp @@ -386,6 +386,12 @@ bool WindowTexture::Render() } +BYTE* WindowTexture::GetBuffer() const +{ + return buffer_.Empty() ? nullptr : buffer_.Get(); +} + + UINT WindowTexture::GetPixel(int x, int y) const { BYTE output[4]; diff --git a/Plugins/uWindowCapture/uWindowCapture/WindowTexture.h b/Plugins/uWindowCapture/uWindowCapture/WindowTexture.h index 52b634e..e7c2aee 100644 --- a/Plugins/uWindowCapture/uWindowCapture/WindowTexture.h +++ b/Plugins/uWindowCapture/uWindowCapture/WindowTexture.h @@ -42,6 +42,8 @@ class WindowTexture bool Upload(); bool Render(); + BYTE* GetBuffer() const; + UINT GetPixel(int x, int y) const; bool GetPixels(BYTE* output, int x, int y, int width, int height) const;