Skip to content

Commit

Permalink
add GetBuffer() api (wip).
Browse files Browse the repository at this point in the history
  • Loading branch information
hecomi committed Dec 30, 2018
1 parent 0397237 commit b031615
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/uWindowCapture/Examples/Buffer.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/uWindowCapture/Examples/Buffer/Buffer.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs
Original file line number Diff line number Diff line change
@@ -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<Renderer>().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();
}
}

}
11 changes: 11 additions & 0 deletions Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/uWindowCapture/Plugins/x86/uWindowCapture.dll
Binary file not shown.
Binary file modified Assets/uWindowCapture/Plugins/x86_64/uWindowCapture.dll
Binary file not shown.
2 changes: 2 additions & 0 deletions Assets/uWindowCapture/Scripts/UwcLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
5 changes: 5 additions & 0 deletions Assets/uWindowCapture/Scripts/UwcWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
9 changes: 9 additions & 0 deletions Plugins/uWindowCapture/uWindowCapture/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions Plugins/uWindowCapture/uWindowCapture/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ UINT Window::GetZOrder() const
}


BYTE* Window::GetBuffer() const
{
return windowTexture_->GetBuffer();
}


UINT Window::GetBufferWidth() const
{
return windowTexture_->GetWidth();
Expand Down
1 change: 1 addition & 0 deletions Plugins/uWindowCapture/uWindowCapture/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions Plugins/uWindowCapture/uWindowCapture/WindowTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 2 additions & 0 deletions Plugins/uWindowCapture/uWindowCapture/WindowTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit b031615

Please sign in to comment.