Compare commits

..

12 Commits

Author SHA1 Message Date
hecomi
306a656c4e add gaze point analyzer for multiple monitors. 2016-11-20 15:15:19 +09:00
hecomi
faf65b4200 support hot-reload. 2016-11-20 14:32:31 +09:00
hecomi
ab25af516e do not loop generation of monitors in creator. 2016-11-20 12:38:24 +09:00
hecomi
45b0ab2cd8 save scale even after reinitialization. 2016-11-20 03:27:16 +09:00
hecomi
68227e9af6 remove unused debug log. 2016-11-20 03:03:40 +09:00
hecomi
be9871ba92 fix thickness bug. 2016-11-20 02:57:31 +09:00
hecomi
110d5d7bda fix boudary condition of cursor texture. 2016-11-20 02:54:38 +09:00
hecomi
533a4c9f28 add gaze point analyzer. 2016-11-20 02:49:58 +09:00
hecomi
88e8342d7a tweak scenes. 2016-11-19 21:55:25 +09:00
hecomi
72376b9e91 implement dirty rects and move rects. 2016-11-19 21:45:32 +09:00
hecomi
a10d5aa5c1 add buffer class. 2016-11-19 15:37:35 +09:00
hecomi
306185a1e9 add badges. 2016-11-19 13:56:16 +09:00
30 changed files with 731 additions and 83 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: aac40d1b58f3f8940be815779d89b5a4
timeCreated: 1479544409
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,181 @@
using UnityEngine;
using MeshForwardDirection = uDesktopDuplication.Texture.MeshForwardDirection;
[RequireComponent(typeof(uDesktopDuplication.Texture))]
public class GazePointAnalyzer : MonoBehaviour
{
uDesktopDuplication.Texture uddTexture_;
[Tooltip("This needs a small calculation cost.")]
public bool calcAveragePos = true;
private Vector2 averageCoord_ = Vector2.zero;
private Vector2 averageCoordVelocity_ = Vector2.zero;
public Vector3 averagePos
{
get { return GetWorldPositionFromCoord((int)averageCoord_.x, (int)averageCoord_.y); }
}
private Vector2 preCursorCoord_ = Vector2.zero;
[Header("Filters")]
[Range(0f, 1f)] public float moveRectFilter = 0.05f;
[Range(0f, 1f)] public float mouseFilter = 0.05f;
[Range(0f, 1f)] public float dirtyRectFilter = 0.01f;
[Range(0f, 1f)] public float noEventFilter = 0.01f;
[Range(0f, 1f)] public float velocityFilter = 0.1f;
[Header("Debug")]
public bool drawAveragePos;
public bool drawMoveRects;
public bool drawDirtyRects;
void Start()
{
uddTexture_ = GetComponent<uDesktopDuplication.Texture>();
averageCoord_ = new Vector2(uddTexture_.monitor.width / 2, uddTexture_.monitor.height / 2);
}
public Vector3 GetWorldPositionFromCoord(int u, int v)
{
var monitor = uddTexture_.monitor;
// Mesh & Scale information
var mesh = GetComponent<MeshFilter>().sharedMesh;
var width = transform.localScale.x * (mesh.bounds.extents.x * 2f);
var height = transform.localScale.y * (mesh.bounds.extents.y * 2f);
// Local position (scale included).
var x = (float)(u - monitor.width / 2) / monitor.width;
var y = -(float)(v - monitor.height / 2) / monitor.height;
var localPos = new Vector3(width * x, height * y, 0f);
// Bending
if (uddTexture_.bend) {
var radius = uddTexture_.radius;
var angle = localPos.x / radius;
if (uddTexture_.meshForwardDirection == MeshForwardDirection.Y) {
localPos.y -= radius * (1f - Mathf.Cos(angle));
} else {
localPos.z -= radius * (1f - Mathf.Cos(angle));
}
localPos.x = radius * Mathf.Sin(angle);
}
// To world position
return transform.position + (transform.rotation * localPos);
}
void CalcAveragePos()
{
if (!calcAveragePos) return;
var coord = Vector2.zero;
var monitor = uddTexture_.monitor;
var cursorCoord = new Vector2(monitor.cursorX, monitor.cursorY);
var filter = 0f;
// move rect
if (monitor.moveRectCount > 0) {
foreach (var rects in monitor.moveRects) {
var rect = rects.destination;
var center = new Vector2(
(rect.right + rect.left) / 2,
(rect.bottom + rect.top) / 2);
coord += center;
}
coord /= monitor.moveRectCount;
filter = moveRectFilter;
}
// mouse
else if (
monitor.isCursorVisible &&
cursorCoord != preCursorCoord_ &&
(cursorCoord - preCursorCoord_).magnitude > 5 &&
monitor.cursorX >= 0 &&
monitor.cursorY >= 0)
{
coord = cursorCoord;
filter = mouseFilter;
}
// dirty rect
else if (monitor.dirtyRectCount > 0) {
var totalWeights = 0f;
foreach (var rect in monitor.dirtyRects) {
var center = new Vector2(
(rect.right + rect.left) / 2,
(rect.bottom + rect.top) / 2);
var weight = 1f / Mathf.Sqrt((rect.right - rect.left) * (rect.bottom - rect.top));
coord += center * weight;
totalWeights += weight;
}
coord /= totalWeights;
filter = dirtyRectFilter;
}
// no event
else {
coord = new Vector2(monitor.width / 2, monitor.height / 2);
filter = noEventFilter;
}
var cf = (filter / ((1f / 60) / Time.deltaTime));
var vf = (velocityFilter / ((1f / 60) / Time.deltaTime));
var targetCoord = averageCoord_ + (coord - averageCoord_) * cf;
var targetVelocity = targetCoord - averageCoord_;
if (float.IsNaN(targetCoord.x) || float.IsNaN(targetCoord.y)) return;
if (float.IsNaN(targetVelocity.x) || float.IsNaN(targetVelocity.y)) return;
averageCoordVelocity_ += (targetVelocity - averageCoordVelocity_) * vf;
averageCoord_ += averageCoordVelocity_;
averageCoord_.x = Mathf.Clamp(averageCoord_.x, 0, monitor.width);
averageCoord_.y = Mathf.Clamp(averageCoord_.y, 0, monitor.height);
preCursorCoord_ = cursorCoord;
}
void Update()
{
CalcAveragePos();
DebugDraw();
}
void DebugDraw()
{
if (drawAveragePos) DrawAveragePos();
if (drawDirtyRects) DrawDirtyRects();
if (drawMoveRects) DrawMoveRects();
}
void DrawRect(uDesktopDuplication.RECT rect, Color color)
{
var p0 = GetWorldPositionFromCoord(rect.left, rect.top);
var p1 = GetWorldPositionFromCoord(rect.right, rect.top);
var p2 = GetWorldPositionFromCoord(rect.right, rect.bottom);
var p3 = GetWorldPositionFromCoord(rect.left, rect.bottom);
Debug.DrawLine(p0, p1, color);
Debug.DrawLine(p1, p2, color);
Debug.DrawLine(p2, p3, color);
Debug.DrawLine(p3, p0, color);
}
void DrawAveragePos()
{
Debug.DrawLine(transform.position, averagePos, Color.yellow);
}
void DrawMoveRects()
{
foreach (var rect in uddTexture_.monitor.moveRects) {
DrawRect(rect.source, Color.blue);
DrawRect(rect.destination, Color.green);
}
}
void DrawDirtyRects()
{
foreach (var rect in uddTexture_.monitor.dirtyRects) {
DrawRect(rect, Color.red);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 52d402bdca823cc42925c9d65993ee59
timeCreated: 1479546123
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,18 +1,22 @@
using UnityEngine;
[RequireComponent(typeof(uDesktopDuplication.Texture)),
RequireComponent(typeof(uDesktopDuplication.Cursor))]
public class Loupe : MonoBehaviour
{
private uDesktopDuplication.Texture uddTexture_;
private uDesktopDuplication.Cursor uddCursor_;
public float zoom = 3f;
public float aspect = 1f;
void Start()
{
uddTexture_ = GetComponent<uDesktopDuplication.Texture>();
uddCursor_ = GetComponent<uDesktopDuplication.Cursor>();
uddTexture_.useClip = true;
}
void Update()
void LateUpdate()
{
CheckVariables();
@@ -26,11 +30,12 @@ public class Loupe : MonoBehaviour
}
var monitor = uddTexture_.monitor;
var cursorX = monitor.isCursorVisible ? monitor.cursorX : monitor.systemCursorX;
var cursorY = monitor.isCursorVisible ? monitor.cursorY : monitor.systemCursorY;
var x = (float)cursorX / monitor.width;
var y = (float)cursorY / monitor.height;
var x = monitor.isCursorVisible ?
uddCursor_.coord.x :
(float)monitor.systemCursorX / monitor.width;
var y = monitor.isCursorVisible ?
uddCursor_.coord.y :
(float)monitor.systemCursorY / monitor.height;
var w = 1f / zoom;
var h = w / aspect * monitor.aspect;
x = Mathf.Clamp(x - w / 2, 0f, 1f - w);
@@ -40,7 +45,7 @@ public class Loupe : MonoBehaviour
}
void CheckVariables()
{
{
if (zoom < 1f) zoom = 1f;
if (aspect < 0.01f) aspect = 0.01f;
}

View File

@@ -0,0 +1,69 @@
using UnityEngine;
[RequireComponent(typeof(MultipleMonitorCreator))]
public class MultipleMonitorAnalyzer : MonoBehaviour
{
MultipleMonitorCreator creator_;
Vector3 gazePoint_ = Vector3.zero;
public Vector3 gazePoint
{
get { return gazePoint_; }
private set
{
gazePoint_ += (value - gazePoint) * (gazePointFilter / (1f / 60 / Time.deltaTime));
}
}
[Header("Filters")]
[Range(0f, 1f)] public float gazePointFilter = 0.1f;
[Range(0f, 1f)] public float moveRectFilter = 0.05f;
[Range(0f, 1f)] public float mouseFilter = 0.05f;
[Range(0f, 1f)] public float dirtyRectFilter = 0.01f;
[Range(0f, 1f)] public float noEventFilter = 0.01f;
[Range(0f, 1f)] public float velocityFilter = 0.1f;
[Header("Debug")]
[SerializeField] bool drawGazePoint;
[SerializeField] bool drawAveragePos;
[SerializeField] bool drawMoveRects;
[SerializeField] bool drawDirtyRects;
void Start()
{
creator_ = GetComponent<MultipleMonitorCreator>();
}
void Update()
{
var cursorMonitorId = uDesktopDuplication.Manager.cursorMonitorId;
for (int i = 0; i < creator_.monitors.Count; ++i) {
var info = creator_.monitors[i];
var analyzer =
info.gameObject.GetComponent<GazePointAnalyzer>() ??
info.gameObject.AddComponent<GazePointAnalyzer>();
UpdateAnalyzer(analyzer);
if (info.uddTexture.monitorId == cursorMonitorId) {
gazePoint = analyzer.averagePos;
}
}
if (drawGazePoint) DrawGazePoint();
}
void DrawGazePoint()
{
Debug.DrawLine(transform.position, gazePoint, Color.magenta);
}
void UpdateAnalyzer(GazePointAnalyzer analyzer)
{
analyzer.moveRectFilter = moveRectFilter;
analyzer.mouseFilter = mouseFilter;
analyzer.dirtyRectFilter = dirtyRectFilter;
analyzer.noEventFilter = noEventFilter;
analyzer.velocityFilter = velocityFilter;
analyzer.drawAveragePos = drawAveragePos;
analyzer.drawMoveRects = drawMoveRects;
analyzer.drawDirtyRects = drawDirtyRects;
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: dbdfccef47068644b81b671bb4e6e166
timeCreated: 1479620455
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,6 +1,8 @@
using UnityEngine;
using UnityEngine.Assertions;
using System.Collections.Generic;
using MeshForwardDirection = uDesktopDuplication.Texture.MeshForwardDirection;
using MonitorState = uDesktopDuplication.MonitorState;
public class MultipleMonitorCreator : MonoBehaviour
{
@@ -39,6 +41,7 @@ public class MultipleMonitorCreator : MonoBehaviour
{
public GameObject gameObject { get; set; }
public Quaternion originalRotation { get; set; }
public Vector3 originalLocalScale { get; set; }
public uDesktopDuplication.Texture uddTexture { get; set; }
public Mesh mesh { get; set; }
}
@@ -46,6 +49,15 @@ public class MultipleMonitorCreator : MonoBehaviour
private List<MonitorInfo> monitors_ = new List<MonitorInfo>();
public List<MonitorInfo> monitors { get { return monitors_; } }
public class SavedMonitorInfo
{
public float widthScale = 1f;
public float heightScale = 1f;
}
private List<SavedMonitorInfo> savedInfoList_ = new List<SavedMonitorInfo>();
public List<SavedMonitorInfo> savedInfoList { get { return savedInfoList_; } }
void Start()
{
uDesktopDuplication.Manager.CreateInstance();
@@ -79,12 +91,12 @@ public class MultipleMonitorCreator : MonoBehaviour
removeWaitTimer_ += Time.deltaTime;
if (removeWaitTimer_ > removeWaitDuration) {
hasMonitorUnsupportStateChecked_ = true;
foreach (var info in monitors_) {
if (info.uddTexture.monitor.state == uDesktopDuplication.MonitorState.Unsupported) {
foreach (var info in monitors) {
if (info.uddTexture.monitor.state == MonitorState.Unsupported) {
Destroy(info.gameObject);
}
}
monitors_.RemoveAll(info => info.uddTexture.monitor.state == uDesktopDuplication.MonitorState.Unsupported);
monitors.RemoveAll(info => info.uddTexture.monitor.state == MonitorState.Unsupported);
}
}
}
@@ -100,13 +112,21 @@ public class MultipleMonitorCreator : MonoBehaviour
ResetRemoveTimer();
// Create monitors
for (int i = 0; i < uDesktopDuplication.Manager.monitorCount; ++i) {
var n = uDesktopDuplication.Manager.monitors.Count;
for (int i = 0; i < n; ++i) {
// Create monitor obeject
var go = Instantiate(monitorPrefab);
go.name = "Monitor " + i;
// Saved infomation
if (savedInfoList.Count == i) {
savedInfoList.Add(new SavedMonitorInfo());
Assert.AreEqual(i, savedInfoList.Count - 1);
}
var savedInfo = savedInfoList[i];
// Expand AABB
var mesh = go.GetComponent<MeshFilter>().sharedMesh;
var mesh = go.GetComponent<MeshFilter>().mesh; // clone
var aabbScale = mesh.bounds.size;
aabbScale.y = Mathf.Max(aabbScale.y, aabbScale.x);
aabbScale.z = Mathf.Max(aabbScale.z, aabbScale.x);
@@ -133,6 +153,10 @@ public class MultipleMonitorCreator : MonoBehaviour
height = scale * (monitor.isHorizontal ? 1f / monitor.aspect : 1f) * ((float)monitor.width / 1920);
break;
}
width *= savedInfo.widthScale;
height *= savedInfo.heightScale;
if (meshForwardDirection == MeshForwardDirection.Y) {
go.transform.localScale = new Vector3(width, go.transform.localScale.y, height);
} else {
@@ -146,18 +170,19 @@ public class MultipleMonitorCreator : MonoBehaviour
var info = new MonitorInfo();
info.gameObject = go;
info.originalRotation = go.transform.rotation;
info.originalLocalScale = go.transform.localScale;
info.uddTexture = texture;
info.mesh = mesh;
monitors_.Add(info);
monitors.Add(info);
}
// Sort monitors in coordinate order
monitors_.Sort((a, b) => a.uddTexture.monitor.left - b.uddTexture.monitor.left);
monitors.Sort((a, b) => a.uddTexture.monitor.left - b.uddTexture.monitor.left);
}
void Clear()
{
foreach (var info in monitors_) {
foreach (var info in monitors) {
Destroy(info.gameObject);
}
if (removeChildrenWhenClear) {
@@ -165,7 +190,7 @@ public class MultipleMonitorCreator : MonoBehaviour
Destroy(transform.GetChild(i).gameObject);
}
}
monitors_.Clear();
monitors.Clear();
}
[ContextMenu("Recreate")]

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using MeshForwardDirection = uDesktopDuplication.Texture.MeshForwardDirection;
public class MultipleMonitorRoundLayouter : MultipleMonitorLayouter
{
@@ -17,10 +18,21 @@ public class MultipleMonitorRoundLayouter : MultipleMonitorLayouter
var monitors = creator_.monitors;
var n = monitors.Count;
// keep the local scale z of monitors as 1 to bend them correctly.
foreach (var info in monitors) {
// Keep the local scale z of monitors as 1 to bend them correctly.
// And save width / height to use same values after reinitialization.
for (int i = 0; i < n; ++i) {
var info = monitors[i];
var savedInfo = creator_.savedInfoList[i];
var scale = info.gameObject.transform.localScale;
scale.z = 1f;
if (creator_.meshForwardDirection == MeshForwardDirection.Y) {
scale.y = 1f;
savedInfo.widthScale = scale.x / info.originalLocalScale.x;
savedInfo.heightScale = scale.z / info.originalLocalScale.z;
} else {
scale.z = 1f;
savedInfo.widthScale = scale.x / info.originalLocalScale.x;
savedInfo.heightScale = scale.y / info.originalLocalScale.y;
}
info.gameObject.transform.localScale = scale;
}

View File

@@ -6,10 +6,12 @@ public class ToggleMonitors : MonoBehaviour
{
if (Input.GetKeyDown(KeyCode.Tab)) {
var texture = GetComponent<uDesktopDuplication.Texture>();
var id = texture.monitorId;
var n = uDesktopDuplication.Manager.monitorCount;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) {
texture.monitorId--;
texture.monitorId = (id - 1 < 0) ? 0 : (id - 1);
} else {
texture.monitorId++;
texture.monitorId = (id + 1 >= n) ? (n - 1) : (id + 1);
}
}
}

View File

@@ -10,7 +10,8 @@ public class Cursor : MonoBehaviour
{
[SerializeField] Vector2 modelScale = Vector2.one;
Vector3 worldPosition { get; set; }
public Vector3 worldPosition { get; set; }
public Vector2 coord { get; set; }
private Texture uddTexture_;
private Monitor monitor { get { return uddTexture_.monitor; } }
@@ -65,6 +66,8 @@ public class Cursor : MonoBehaviour
var w = (float)monitor.cursorShapeWidth / monitor.width;
var h = (float)monitor.cursorShapeHeight / monitor.height;
uddTexture_.material.SetVector("_CursorPositionScale", new Vector4(x, y, w, h));
coord = new Vector2(x, y);
}
}

View File

@@ -50,6 +50,26 @@ public enum DebugMode
UnityLog = 2, /* currently has bug when app exits. */
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
[MarshalAs(UnmanagedType.I4)]
public int left;
[MarshalAs(UnmanagedType.I4)]
public int top;
[MarshalAs(UnmanagedType.I4)]
public int right;
[MarshalAs(UnmanagedType.I4)]
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct DXGI_OUTDUPL_MOVE_RECT
{
public RECT source;
public RECT destination;
}
public static class Lib
{
public delegate void MessageHandler(Message message);
@@ -134,6 +154,14 @@ public static class Lib
public static extern void GetCursorTexture(int id, System.IntPtr ptr);
[DllImport("uDesktopDuplication")]
public static extern int SetTexturePtr(int id, IntPtr ptr);
[DllImport("uDesktopDuplication")]
public static extern int GetMoveRectCount(int id);
[DllImport("uDesktopDuplication", EntryPoint = "GetMoveRects")]
private static extern IntPtr GetMoveRects_Internal(int id);
[DllImport("uDesktopDuplication")]
public static extern int GetDirtyRectCount(int id);
[DllImport("uDesktopDuplication", EntryPoint = "GetDirtyRects")]
private static extern IntPtr GetDirtyRects_Internal(int id);
public static string GetName(int id)
{
@@ -141,6 +169,32 @@ public static class Lib
GetName(id, buf, buf.Capacity);
return buf.ToString();
}
public static DXGI_OUTDUPL_MOVE_RECT[] GetMoveRects(int id)
{
var count = GetMoveRectCount(id);
var rects = new DXGI_OUTDUPL_MOVE_RECT[count];
var ptr = GetMoveRects_Internal(id);
var size = Marshal.SizeOf(typeof(DXGI_OUTDUPL_MOVE_RECT));
for (int i = 0; i < count; ++i) {
var data = new IntPtr(ptr.ToInt64() + size * i);
rects[i] = (DXGI_OUTDUPL_MOVE_RECT)Marshal.PtrToStructure(data, typeof(DXGI_OUTDUPL_MOVE_RECT));
}
return rects;
}
public static RECT[] GetDirtyRects(int id)
{
var count = GetDirtyRectCount(id);
var rects = new RECT[count];
var ptr = GetDirtyRects_Internal(id);
var size = Marshal.SizeOf(typeof(RECT));
for (int i = 0; i < count; ++i) {
var data = new IntPtr(ptr.ToInt64() + size * i);
rects[i] = (RECT)Marshal.PtrToStructure(data, typeof(RECT));
}
return rects;
}
}
}

View File

@@ -57,6 +57,7 @@ public class Manager : MonoBehaviour
private Coroutine renderCoroutine_ = null;
private bool shouldReinitialize_ = false;
private float reinitializationTimer_ = 0f;
private bool isFirstFrame_ = true;
public delegate void ReinitializeHandler();
public static event ReinitializeHandler onReinitialized;
@@ -72,16 +73,17 @@ public class Manager : MonoBehaviour
void Awake()
{
Lib.SetDebugMode(debugMode);
Lib.SetTimeout(desktopDuplicationApiTimeout);
Lib.InitializeUDD();
// for simple singleton
if (instance_ != null) {
Destroy(gameObject);
return;
}
instance_ = this;
Lib.SetDebugMode(debugMode);
Lib.SetTimeout(desktopDuplicationApiTimeout);
Lib.InitializeUDD();
CreateMonitors();
}
@@ -94,6 +96,9 @@ public class Manager : MonoBehaviour
void OnEnable()
{
renderCoroutine_ = StartCoroutine(OnRender());
if (!isFirstFrame_) {
Reinitialize();
}
}
void OnDisable()
@@ -109,6 +114,7 @@ public class Manager : MonoBehaviour
Lib.Update();
ReinitializeIfNeeded();
UpdateMessage();
isFirstFrame_ = false;
}
[ContextMenu("Reinitialize")]

View File

@@ -202,6 +202,26 @@ public class Monitor
get { return Lib.GetCursorShapeType(id); }
}
public int moveRectCount
{
get { return Lib.GetMoveRectCount(id); }
}
public DXGI_OUTDUPL_MOVE_RECT[] moveRects
{
get { return Lib.GetMoveRects(id); }
}
public int dirtyRectCount
{
get { return Lib.GetDirtyRectCount(id); }
}
public RECT[] dirtyRects
{
get { return Lib.GetDirtyRects(id); }
}
public bool shouldBeUpdated
{
get;

View File

@@ -21,6 +21,7 @@ public class Texture : MonoBehaviour
}
}
private int lastMonitorId_ = 0;
public int monitorId
{
get { return monitor.id; }
@@ -52,8 +53,10 @@ public class Texture : MonoBehaviour
{
if (value) {
material.EnableKeyword("BEND_ON");
material.SetInt("_Bend", 1);
} else {
material.DisableKeyword("BEND_ON");
material.SetInt("_Bend", 0);
}
}
}
@@ -69,13 +72,13 @@ public class Texture : MonoBehaviour
switch (value) {
case MeshForwardDirection.Y:
material.SetInt("_Forward", 0);
material.EnableKeyword("_BEND_Y");
material.DisableKeyword("_BEND_Z");
material.EnableKeyword("_FORWARD_Y");
material.DisableKeyword("_FORWARD_Z");
break;
case MeshForwardDirection.Z:
material.SetInt("_Forward", 1);
material.DisableKeyword("_BEND_Y");
material.EnableKeyword("_BEND_Z");
material.DisableKeyword("_FORWARD_Y");
material.EnableKeyword("_FORWARD_Z");
break;
}
}
@@ -125,10 +128,20 @@ public class Texture : MonoBehaviour
void Update()
{
KeepMonitor();
monitor.shouldBeUpdated = true;
UpdateMaterial();
}
void KeepMonitor()
{
if (monitor == null) {
Reinitialize();
} else {
lastMonitorId_ = monitorId;
}
}
void AddCursorIfNotAttached()
{
if (!GetComponent<Cursor>())
@@ -140,7 +153,7 @@ public class Texture : MonoBehaviour
void Reinitialize()
{
// Monitor instance is released here when initialized.
monitor = Manager.GetMonitor(monitor.id);
monitor = Manager.GetMonitor(lastMonitorId_);
}
void UpdateMaterial()

View File

@@ -97,13 +97,13 @@ inline void uddBendVertex(inout float4 v, half radius, half width, half thicknes
#ifdef BEND_ON
half angle = width * v.x / radius;
#ifdef _FORWARD_Z
v.y *= thickness;
radius -= v.y;
v.y += radius * (1 - cos(angle));
#elif _FORWARD_Y
v.z *= thickness;
radius -= v.z;
v.z += radius * (1 - cos(angle));
radius += v.z;
v.z -= radius * (1 - cos(angle));
#elif _FORWARD_Y
v.y *= thickness;
radius += v.y;
v.y += radius * (1 - cos(angle));
#endif
v.x = radius * sin(angle) / width;
#else

View File

@@ -36,4 +36,4 @@ const std::unique_ptr<MonitorManager>& GetMonitorManager()
void SendMessageToUnity(Message message)
{
g_messages.push(message);
}
}

View File

@@ -17,17 +17,6 @@ class MonitorManager;
const std::unique_ptr<MonitorManager>& GetMonitorManager();
template <class T>
auto MakeUniqueWithReleaser(T* ptr)
{
const auto deleter = [](T* ptr)
{
if (ptr != nullptr) ptr->Release();
};
return std::unique_ptr<T, decltype(deleter)>(ptr, deleter);
}
// Message is pooled and fetch from Unity.
enum class Message
{
@@ -36,4 +25,75 @@ enum class Message
TextureSizeChanged = 1,
};
void SendMessageToUnity(Message message);
void SendMessageToUnity(Message message);
// Buffer
template <class T>
class Buffer
{
public:
Buffer() {}
~Buffer() {}
void ExpandIfNeeded(UINT size)
{
if (size > size_)
{
size_ = size;
value_ = std::make_unique<T[]>(size);
}
}
void Reset()
{
value_.reset();
size_ = 0;
}
UINT Size() const
{
return size_;
}
T* Get() const
{
return value_.get();
}
T* Get(UINT offset) const
{
return (value_.get() + offset);
}
template <class U>
U* As() const
{
return reinterpret_cast<U*>(Get());
}
template <class U>
U* As(UINT offset) const
{
return reinterpret_cast<U*>(Get(offset));
}
operator bool() const
{
return value_ != nullptr;
}
T operator [](UINT index) const
{
if (index >= size_)
{
Debug::Error("Array index out of range: ", index, size_);
return T(0);
}
return value_[index];
}
private:
std::unique_ptr<T[]> value_;
UINT size_ = 0;
};

View File

@@ -1,6 +1,5 @@
#include <d3d11.h>
#include <wrl/client.h>
#include "Common.h"
#include "Debug.h"
#include "MonitorManager.h"
#include "Monitor.h"
@@ -47,13 +46,7 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
return;
}
// Increase the buffer size if needed
if (frameInfo.PointerShapeBufferSize > apiBufferSize_)
{
apiBufferSize_ = frameInfo.PointerShapeBufferSize;
apiBuffer_ = std::make_unique<BYTE[]>(apiBufferSize_);
}
apiBuffer_.ExpandIfNeeded(frameInfo.PointerShapeBufferSize);
if (!apiBuffer_)
{
return;
@@ -63,16 +56,15 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
UINT bufferSize;
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
const auto hr = monitor_->GetDeskDupl()->GetFramePointerShape(
apiBufferSize_,
apiBuffer_.get(),
apiBuffer_.Size(),
apiBuffer_.Get(),
&bufferSize,
&shapeInfo);
if (FAILED(hr))
{
Debug::Error("Cursor::UpdateBuffer() => GetFramePointerShape() failed.");
apiBuffer_.reset();
apiBufferSize_ = 0;
apiBuffer_.Reset();
return;
}
@@ -100,11 +92,7 @@ void Cursor::UpdateTexture()
// Convert the buffer given by API into BGRA32
const UINT bgraBufferSize = cursorImageWidth * cursorImageHeight * 4;
if (bgraBufferSize > bgra32BufferSize_)
{
bgra32BufferSize_ = bgraBufferSize;
bgra32Buffer_ = std::make_unique<BYTE[]>(bgra32BufferSize_);
}
bgra32Buffer_.ExpandIfNeeded(bgraBufferSize);
// Check buffers
if (!bgra32Buffer_ || !apiBuffer_)
@@ -197,8 +185,8 @@ void Cursor::UpdateTexture()
if (box.left < 0 ||
box.top < 0 ||
box.right >= static_cast<UINT>(desktopImageWidth) ||
box.bottom >= static_cast<UINT>(desktopImageHeight))
box.right > static_cast<UINT>(desktopImageWidth) ||
box.bottom > static_cast<UINT>(desktopImageHeight))
{
Debug::Error("Cursor::UpdateTexture() => box is out of area.");
Debug::Error(
@@ -281,7 +269,7 @@ void Cursor::UpdateTexture()
};
// Access RGBA values at the same time
auto output32 = reinterpret_cast<UINT*>(bgra32Buffer_.get());
auto output32 = reinterpret_cast<UINT*>(bgra32Buffer_.Get());
if (isMono)
{
@@ -300,7 +288,7 @@ void Cursor::UpdateTexture()
}
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR
{
const auto buffer32 = reinterpret_cast<UINT*>(apiBuffer_.get());
const auto buffer32 = reinterpret_cast<UINT*>(apiBuffer_.Get());
for (int row = rowMin, y = 0; row < rowMax; ++row, ++y)
{
@@ -329,8 +317,8 @@ void Cursor::UpdateTexture()
}
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR
{
auto output32 = reinterpret_cast<UINT*>(bgra32Buffer_.get());
const auto buffer32 = reinterpret_cast<UINT*>(apiBuffer_.get());
auto output32 = reinterpret_cast<UINT*>(bgra32Buffer_.Get());
const auto buffer32 = reinterpret_cast<UINT*>(apiBuffer_.Get());
for (int i = 0; i < cursorImageWidth * cursorImageHeight; ++i)
{
output32[i] = buffer32[i];
@@ -341,7 +329,7 @@ void Cursor::UpdateTexture()
void Cursor::GetTexture(ID3D11Texture2D* texture)
{
if (bgra32Buffer_ == nullptr)
if (!bgra32Buffer_)
{
Debug::Error("Cursor::GetTexture() => bgra32Buffer is null.");
return;
@@ -368,7 +356,7 @@ void Cursor::GetTexture(ID3D11Texture2D* texture)
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->UpdateSubresource(texture, 0, nullptr, bgra32Buffer_.get(), GetWidth() * 4, 0);
context->UpdateSubresource(texture, 0, nullptr, bgra32Buffer_.Get(), GetWidth() * 4, 0);
}

View File

@@ -3,6 +3,7 @@
#include <d3d11.h>
#include <dxgi1_2.h>
#include <memory>
#include "Common.h"
class Monitor;
@@ -30,10 +31,8 @@ private:
bool isVisible_ = false;
int x_ = -1;
int y_ = -1;
std::unique_ptr<BYTE[]> apiBuffer_ = nullptr;
UINT apiBufferSize_ = 0;
std::unique_ptr<BYTE[]> bgra32Buffer_ = nullptr;
UINT bgra32BufferSize_ = 0;
Buffer<BYTE> apiBuffer_;
Buffer<BYTE> bgra32Buffer_;
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo_;
LARGE_INTEGER timestamp_;
};

View File

@@ -1,6 +1,5 @@
#include <d3d11.h>
#include <ShellScalingAPI.h>
#include "Common.h"
#include "Debug.h"
#include "Cursor.h"
#include "MonitorManager.h"
@@ -50,7 +49,7 @@ void Monitor::Initialize(IDXGIOutput* output)
if (FAILED(GetDpiForMonitor(outputDesc_.Monitor, MDT_RAW_DPI, &dpiX_, &dpiY_)))
{
Debug::Error("Monitor::Initialize() => GetDpiForMonitor() failed.");
// DPI is set as -1, so the application has to use the appropriate value.
// DPI is set as -1, so the application has to use the appropriate value.
}
auto output1 = reinterpret_cast<IDXGIOutput1*>(output);
@@ -156,6 +155,7 @@ void Monitor::Render(UINT timeout)
return;
}
// Get texture
if (unityTexture_)
{
ID3D11Texture2D* texture;
@@ -188,8 +188,8 @@ void Monitor::Render(UINT timeout)
}
}
cursor_->UpdateBuffer(frameInfo);
cursor_->UpdateTexture();
UpdateMetadata(frameInfo);
UpdateCursor(frameInfo);
if (FAILED(deskDupl_->ReleaseFrame()))
{
@@ -198,6 +198,109 @@ void Monitor::Render(UINT timeout)
}
void Monitor::UpdateCursor(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
{
cursor_->UpdateBuffer(frameInfo);
cursor_->UpdateTexture();
}
void Monitor::UpdateMetadata(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
{
metaData_.ExpandIfNeeded(frameInfo.TotalMetadataBufferSize);
UpdateMoveRects(frameInfo);
UpdateDirtyRects(frameInfo);
}
void Monitor::UpdateMoveRects(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
{
moveRectSize_ = metaData_.Size();
const auto hr = deskDupl_->GetFrameMoveRects(
moveRectSize_,
metaData_.As<DXGI_OUTDUPL_MOVE_RECT>(),
&moveRectSize_);
if (FAILED(hr))
{
switch (hr)
{
case DXGI_ERROR_ACCESS_LOST:
{
Debug::Log("Monitor::Render() => DXGI_ERROR_ACCESS_LOST (GetFrameMoveRects()).");
break;
}
case DXGI_ERROR_MORE_DATA:
{
Debug::Error("Monitor::Render() => DXGI_ERROR_MORE_DATA (GetFrameMoveRects()).");
break;
}
case DXGI_ERROR_INVALID_CALL:
{
Debug::Error("Monitor::Render() => DXGI_ERROR_INVALID_CALL (GetFrameMoveRects()).");
break;
}
case E_INVALIDARG:
{
Debug::Error("Monitor::Render() => E_INVALIDARG (GetFrameMoveRects()).");
break;
}
default:
{
Debug::Error("Monitor::Render() => Unknown Error (GetFrameMoveRects()).");
break;
}
}
return;
}
}
void Monitor::UpdateDirtyRects(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
{
dirtyRectSize_ = metaData_.Size() - moveRectSize_;
const auto hr = deskDupl_->GetFrameDirtyRects(
dirtyRectSize_,
metaData_.As<RECT>(moveRectSize_ /* offset */),
&dirtyRectSize_);
if (FAILED(hr))
{
switch (hr)
{
case DXGI_ERROR_ACCESS_LOST:
{
Debug::Log("Monitor::Render() => DXGI_ERROR_ACCESS_LOST (GetFrameDirtyRects()).");
break;
}
case DXGI_ERROR_MORE_DATA:
{
Debug::Error("Monitor::Render() => DXGI_ERROR_MORE_DATA (GetFrameDirtyRects()).");
break;
}
case DXGI_ERROR_INVALID_CALL:
{
Debug::Error("Monitor::Render() => DXGI_ERROR_INVALID_CALL (GetFrameDirtyRects()).");
break;
}
case E_INVALIDARG:
{
Debug::Error("Monitor::Render() => E_INVALIDARG (GetFrameDirtyRects()).");
break;
}
default:
{
Debug::Error("Monitor::Render() => Unknown Error (GetFrameDirtyRects()).");
break;
}
}
return;
}
}
int Monitor::GetId() const
{
return id_;
@@ -303,4 +406,28 @@ int Monitor::GetWidth() const
int Monitor::GetHeight() const
{
return height_;
}
int Monitor::GetMoveRectCount() const
{
return moveRectSize_ / sizeof(DXGI_OUTDUPL_MOVE_RECT);
}
DXGI_OUTDUPL_MOVE_RECT* Monitor::GetMoveRects() const
{
return metaData_.As<DXGI_OUTDUPL_MOVE_RECT>();
}
int Monitor::GetDirtyRectCount() const
{
return dirtyRectSize_ / sizeof(RECT);
}
RECT* Monitor::GetDirtyRects() const
{
return metaData_.As<RECT>(moveRectSize_);
}

View File

@@ -4,6 +4,7 @@
#include <dxgi1_2.h>
#include <wrl/client.h>
#include <memory>
#include "Common.h"
class Cursor;
@@ -50,8 +51,17 @@ public:
int GetDpiY() const;
IDXGIOutputDuplication* GetDeskDupl();
const std::unique_ptr<Cursor>& GetCursor();
int GetMoveRectCount() const;
DXGI_OUTDUPL_MOVE_RECT* GetMoveRects() const;
int GetDirtyRectCount() const;
RECT* GetDirtyRects() const;
private:
void UpdateCursor(const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
void UpdateMetadata(const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
void UpdateMoveRects(const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
void UpdateDirtyRects(const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
int id_ = -1;
UINT dpiX_ = -1, dpiY_ = -1;
int width_ = -1, height_ = -1;
@@ -61,4 +71,7 @@ private:
ID3D11Texture2D* unityTexture_ = nullptr;
DXGI_OUTPUT_DESC outputDesc_;
MONITORINFOEX monitorInfo_;
Buffer<BYTE> metaData_;
UINT moveRectSize_ = 0;
UINT dirtyRectSize_ = 0;;
};

View File

@@ -383,4 +383,40 @@ extern "C"
monitor->SetUnityTexture(d3d11Texture);
}
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetMoveRectCount(int id)
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetMoveRectCount();
}
}
UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetMoveRects(int id)
{
if (!g_manager) return nullptr;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetMoveRects();
}
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetDirtyRectCount(int id)
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetDirtyRectCount();
}
}
UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetDirtyRects(int id)
{
if (!g_manager) return nullptr;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetDirtyRects();
}
}
}

View File

@@ -1,6 +1,9 @@
uDesktopDuplication
===================
![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)
![Build Status](http://unitybuildbadge.azurewebsites.net/api/status/7566d616-2916-4c99-bb14-e7ab9501cf7e)
**uDesktopDuplication** is an Unity asset to use the realtime screen capture as `Texture2D` using Desktop Duplication API.