Compare commits
38 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
81bc23d0bc | ||
|
|
583af4ff46 | ||
|
|
7e3df8ec2e | ||
|
|
32adf6c2d0 | ||
|
|
a24472bd92 | ||
|
|
cfee22832a | ||
|
|
9c467a0e6f | ||
|
|
87557ed5ff | ||
|
|
67c90073d9 | ||
|
|
ce9c148282 | ||
|
|
8201a22523 | ||
|
|
5cfbd70e0b | ||
|
|
f2b6a99af3 | ||
|
|
c48bf7fecf | ||
|
|
6673734b9c | ||
|
|
3364c19adf | ||
|
|
f7eba2434f | ||
|
|
ddbfad6e95 | ||
|
|
49aef432e7 | ||
|
|
fc81892917 | ||
|
|
306a656c4e | ||
|
|
faf65b4200 | ||
|
|
ab25af516e | ||
|
|
45b0ab2cd8 | ||
|
|
68227e9af6 | ||
|
|
be9871ba92 | ||
|
|
110d5d7bda | ||
|
|
533a4c9f28 | ||
|
|
88e8342d7a | ||
|
|
72376b9e91 | ||
|
|
a10d5aa5c1 | ||
|
|
306185a1e9 | ||
|
|
60889d53f4 | ||
|
|
f207e65952 | ||
|
|
aea8d112c6 | ||
|
|
9f6971dd45 | ||
|
|
1be698af63 | ||
|
|
064db9f137 |
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57d4a6e606848364c9880f2adec16d90
|
||||
timeCreated: 1480074774
|
||||
licenseType: Pro
|
||||
NativeFormatImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Binary file not shown.
BIN
Assets/uDesktopDuplication/Examples/Scenes/Displacement.unity
Normal file
BIN
Assets/uDesktopDuplication/Examples/Scenes/Displacement.unity
Normal file
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e1efec2c71c5b94e8522880dd9636c8
|
||||
timeCreated: 1480074355
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
BIN
Assets/uDesktopDuplication/Examples/Scenes/GetPixels.unity
Normal file
BIN
Assets/uDesktopDuplication/Examples/Scenes/GetPixels.unity
Normal file
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38a5db360d5147c46ae925104162b881
|
||||
timeCreated: 1480420198
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
BIN
Assets/uDesktopDuplication/Examples/Scenes/Meta Data.unity
Normal file
BIN
Assets/uDesktopDuplication/Examples/Scenes/Meta Data.unity
Normal file
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aac40d1b58f3f8940be815779d89b5a4
|
||||
timeCreated: 1479544409
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
[RequireComponent(typeof(uDesktopDuplication.Texture))]
|
||||
public class DisplacementMapping : MonoBehaviour
|
||||
{
|
||||
public enum TargetMonitor
|
||||
{
|
||||
Self,
|
||||
Next,
|
||||
Prev,
|
||||
}
|
||||
public TargetMonitor target = TargetMonitor.Self;
|
||||
|
||||
uDesktopDuplication.Texture uddTexture_;
|
||||
|
||||
int dispTexId_;
|
||||
int dispFactorId_;
|
||||
int tessMinDistId_;
|
||||
int tessMaxDistId_;
|
||||
int tessFactorId_;
|
||||
|
||||
[Range(0.0f, 10f)] public float displacementFactor = 0.1f;
|
||||
[Range(0.1f, 10f)] public float tessellationMinDist = 0.5f;
|
||||
[Range(1.0f, 50f)] public float tessellationMaxDist = 25f;
|
||||
[Range(1.0f, 50f)] public float tessellationFactor = 25f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
uddTexture_ = GetComponent<uDesktopDuplication.Texture>();
|
||||
Assert.IsNotNull(uddTexture_);
|
||||
|
||||
dispTexId_ = Shader.PropertyToID("_DispTex");
|
||||
dispFactorId_ = Shader.PropertyToID("_DispFactor");
|
||||
tessMinDistId_ = Shader.PropertyToID("_TessMinDist");
|
||||
tessMaxDistId_ = Shader.PropertyToID("_TessMaxDist");
|
||||
tessFactorId_ = Shader.PropertyToID("_TessFactor");
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
var id = uddTexture_.monitor.id;
|
||||
switch (target) {
|
||||
case TargetMonitor.Self: break;
|
||||
case TargetMonitor.Next: ++id; break;
|
||||
case TargetMonitor.Prev: --id; break;
|
||||
}
|
||||
id = Mathf.Clamp(id, 0, uDesktopDuplication.Manager.monitorCount - 1);
|
||||
var monitor = uDesktopDuplication.Manager.GetMonitor(id);
|
||||
monitor.shouldBeUpdated = true;
|
||||
|
||||
uddTexture_.material.SetTexture(dispTexId_, monitor.texture);
|
||||
uddTexture_.material.SetFloat(dispFactorId_, displacementFactor);
|
||||
uddTexture_.material.SetFloat(tessMinDistId_, tessellationMinDist);
|
||||
uddTexture_.material.SetFloat(tessMaxDistId_, tessellationMaxDist);
|
||||
uddTexture_.material.SetFloat(tessFactorId_, tessellationFactor);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2d9e4ca459ecb44da8815bafe9655c5
|
||||
timeCreated: 1477635630
|
||||
guid: 859a2446b86e55c43932212872c82748
|
||||
timeCreated: 1480146517
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 89616e59e1cccb346bd4e959257990ca, type: 3}
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
168
Assets/uDesktopDuplication/Examples/Scripts/GazePointAnalyzer.cs
Normal file
168
Assets/uDesktopDuplication/Examples/Scripts/GazePointAnalyzer.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
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); }
|
||||
}
|
||||
|
||||
public Vector3 cursorPos
|
||||
{
|
||||
get { return GetWorldPositionFromCoord(uddTexture_.monitor.cursorX, uddTexture_.monitor.cursorY); }
|
||||
}
|
||||
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 drawCursorPos;
|
||||
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)
|
||||
{
|
||||
return uddTexture_.GetWorldPositionFromCoord(u, v);
|
||||
}
|
||||
|
||||
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 (drawCursorPos) DrawCursorPos();
|
||||
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 DrawCursorPos()
|
||||
{
|
||||
Debug.DrawLine(transform.position, cursorPos, Color.grey);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 52d402bdca823cc42925c9d65993ee59
|
||||
timeCreated: 1479546123
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class GetPixelsExample : MonoBehaviour
|
||||
{
|
||||
[SerializeField] uDesktopDuplication.Texture uddTexture;
|
||||
|
||||
[SerializeField] int x = 100;
|
||||
[SerializeField] int y = 100;
|
||||
const int width = 64;
|
||||
const int height = 32;
|
||||
|
||||
public Texture2D texture;
|
||||
Color32[] colors = new Color32[width * height];
|
||||
|
||||
void Start()
|
||||
{
|
||||
texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
|
||||
GetComponent<Renderer>().material.mainTexture = texture;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// must be called (performance will be slightly down).
|
||||
uDesktopDuplication.Manager.primary.useGetPixels = true;
|
||||
|
||||
var monitor = uddTexture.monitor;
|
||||
if (!monitor.hasBeenUpdated) return;
|
||||
|
||||
if (monitor.GetPixels(colors, x, y, width, height)) {
|
||||
texture.SetPixels32(colors);
|
||||
texture.Apply();
|
||||
}
|
||||
|
||||
Debug.Log(monitor.GetPixel(monitor.cursorX, monitor.cursorY));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd3f838ff57152e44a1810274a9df49b
|
||||
timeCreated: 1480420430
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(uDesktopDuplication.Texture))]
|
||||
public class Loupe : MonoBehaviour
|
||||
{
|
||||
private uDesktopDuplication.Texture uddTexture_;
|
||||
@@ -12,7 +13,7 @@ public class Loupe : MonoBehaviour
|
||||
uddTexture_.useClip = true;
|
||||
}
|
||||
|
||||
void Update()
|
||||
void LateUpdate()
|
||||
{
|
||||
CheckVariables();
|
||||
|
||||
@@ -26,11 +27,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 ?
|
||||
(float)monitor.cursorX / monitor.width :
|
||||
(float)monitor.systemCursorX / monitor.width;
|
||||
var y = monitor.isCursorVisible ?
|
||||
(float)monitor.cursorY / monitor.height :
|
||||
(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 +42,7 @@ public class Loupe : MonoBehaviour
|
||||
}
|
||||
|
||||
void CheckVariables()
|
||||
{
|
||||
{
|
||||
if (zoom < 1f) zoom = 1f;
|
||||
if (aspect < 0.01f) aspect = 0.01f;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dbdfccef47068644b81b671bb4e6e166
|
||||
timeCreated: 1479620455
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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);
|
||||
@@ -125,14 +145,18 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
height = monitor.heightMeter;
|
||||
break;
|
||||
case ScaleMode.Fixed:
|
||||
width = scale * (monitor.isHorizontal ? 1f : monitor.aspect);
|
||||
height = scale * (monitor.isHorizontal ? 1f / monitor.aspect : 1f);
|
||||
width = scale * (monitor.isHorizontal ? monitor.aspect : 1f);
|
||||
height = scale * (monitor.isHorizontal ? 1f : 1f / monitor.aspect);
|
||||
break;
|
||||
case ScaleMode.Pixel:
|
||||
width = scale * (monitor.isHorizontal ? 1f : monitor.aspect) * ((float)monitor.width / 1920);
|
||||
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")]
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
public class MultipleMonitorLayouter : MonoBehaviour
|
||||
{
|
||||
protected MultipleMonitorCreator creator_;
|
||||
[SerializeField] bool updateEveryFrame = true;
|
||||
[SerializeField] protected float margin = 0.1f;
|
||||
[SerializeField][Range(0f, 10f)] protected float thickness = 1f;
|
||||
public bool updateEveryFrame = true;
|
||||
public float margin = 0.1f;
|
||||
[Range(0f, 10f)] public float thickness = 1f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -30,7 +30,7 @@ public class MultipleMonitorLayouter : MonoBehaviour
|
||||
|
||||
var totalWidth = 0f;
|
||||
foreach (var info in monitors) {
|
||||
var width = info.gameObject.transform.localEulerAngles.x * (info.mesh.bounds.extents.x * 2f);
|
||||
var width = info.gameObject.transform.localScale.x * (info.mesh.bounds.extents.x * 2f);
|
||||
totalWidth += width;
|
||||
}
|
||||
totalWidth += margin * (n - 1);
|
||||
@@ -38,7 +38,7 @@ public class MultipleMonitorLayouter : MonoBehaviour
|
||||
var x = -totalWidth / 2;
|
||||
|
||||
foreach (var info in monitors) {
|
||||
var width = info.gameObject.transform.localEulerAngles.x;
|
||||
var width = info.gameObject.transform.localScale.x;
|
||||
x += (width * info.mesh.bounds.extents.x);
|
||||
info.gameObject.transform.localPosition = new Vector3(x, 0f, 0f);
|
||||
info.gameObject.transform.localRotation = info.originalRotation;
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using UnityEngine;
|
||||
using MeshForwardDirection = uDesktopDuplication.Texture.MeshForwardDirection;
|
||||
|
||||
public class MultipleMonitorRoundLayouter : MultipleMonitorLayouter
|
||||
{
|
||||
[SerializeField] bool debugDraw = true;
|
||||
|
||||
public float radius = 10f;
|
||||
public Vector3 offsetAngle = Vector3.zero;
|
||||
|
||||
@@ -17,6 +20,28 @@ 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.
|
||||
// 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;
|
||||
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;
|
||||
}
|
||||
|
||||
// keep thicness plus value.
|
||||
thickness = Mathf.Max(thickness, 0f);
|
||||
|
||||
// calculate total width
|
||||
var totalWidth = 0f;
|
||||
foreach (var info in monitors) {
|
||||
var width = info.gameObject.transform.localScale.x * (info.mesh.bounds.extents.x * 2f);
|
||||
@@ -24,9 +49,13 @@ public class MultipleMonitorRoundLayouter : MultipleMonitorLayouter
|
||||
}
|
||||
totalWidth += margin * (n - 1);
|
||||
|
||||
// expand radius if total width is larger than the circumference.
|
||||
radius = Mathf.Max(radius, (totalWidth + margin) / (2 * Mathf.PI));
|
||||
|
||||
// total angle of monitors
|
||||
var totalAngle = totalWidth / radius;
|
||||
|
||||
// layout
|
||||
float angle = -totalAngle / 2;
|
||||
var offsetRot = Quaternion.Euler(offsetAngle);
|
||||
foreach (var info in monitors) {
|
||||
@@ -53,7 +82,12 @@ public class MultipleMonitorRoundLayouter : MultipleMonitorLayouter
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
if (debugDraw) DebugDraw();
|
||||
}
|
||||
|
||||
void DebugDraw()
|
||||
{
|
||||
// draw the circumference in the scene view.
|
||||
var scale = transform.localScale.x;
|
||||
var center = transform.position - Vector3.forward * radius * scale;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 596fd740aeeee0d4cb4f962d4072954e
|
||||
timeCreated: 1480074277
|
||||
licenseType: Pro
|
||||
NativeFormatImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 37 KiB |
Binary file not shown.
@@ -39,11 +39,11 @@ ModelImporter:
|
||||
globalScale: 0.1
|
||||
meshCompression: 0
|
||||
addColliders: 0
|
||||
importBlendShapes: 1
|
||||
importBlendShapes: 0
|
||||
swapUVChannels: 0
|
||||
generateSecondaryUV: 0
|
||||
useFileUnits: 1
|
||||
optimizeMeshForGPU: 1
|
||||
optimizeMeshForGPU: 0
|
||||
keepQuads: 0
|
||||
weldVertices: 1
|
||||
secondaryUVAngleDistortion: 8
|
||||
|
||||
BIN
Assets/uDesktopDuplication/Models/uDD_Plane.fbx
Normal file
BIN
Assets/uDesktopDuplication/Models/uDD_Plane.fbx
Normal file
Binary file not shown.
87
Assets/uDesktopDuplication/Models/uDD_Plane.fbx.meta
Normal file
87
Assets/uDesktopDuplication/Models/uDD_Plane.fbx.meta
Normal file
@@ -0,0 +1,87 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f30f6cfd6aa37554ca886424cada7cbb
|
||||
timeCreated: 1480072865
|
||||
licenseType: Pro
|
||||
ModelImporter:
|
||||
serializedVersion: 19
|
||||
fileIDToRecycleName:
|
||||
100000: //RootNode
|
||||
100002: uDD_Plane_MeshPart0
|
||||
100004: uDD_Plane_MeshPart1
|
||||
400000: //RootNode
|
||||
400002: uDD_Plane_MeshPart0
|
||||
400004: uDD_Plane_MeshPart1
|
||||
2300000: //RootNode
|
||||
2300002: uDD_Plane_MeshPart0
|
||||
2300004: uDD_Plane_MeshPart1
|
||||
3300000: //RootNode
|
||||
3300002: uDD_Plane_MeshPart0
|
||||
3300004: uDD_Plane_MeshPart1
|
||||
4300000: uDD_Plane
|
||||
4300002: uDD_Plane_MeshPart0
|
||||
4300004: uDD_Plane_MeshPart1
|
||||
9500000: //RootNode
|
||||
materials:
|
||||
importMaterials: 0
|
||||
materialName: 0
|
||||
materialSearch: 1
|
||||
animations:
|
||||
legacyGenerateAnimations: 4
|
||||
bakeSimulation: 0
|
||||
resampleCurves: 1
|
||||
optimizeGameObjects: 0
|
||||
motionNodeName:
|
||||
animationImportErrors:
|
||||
animationImportWarnings:
|
||||
animationRetargetingWarnings:
|
||||
animationDoRetargetingWarnings: 0
|
||||
animationCompression: 1
|
||||
animationRotationError: 0.5
|
||||
animationPositionError: 0.5
|
||||
animationScaleError: 0.5
|
||||
animationWrapMode: 0
|
||||
extraExposedTransformPaths: []
|
||||
clipAnimations: []
|
||||
isReadable: 0
|
||||
meshes:
|
||||
lODScreenPercentages: []
|
||||
globalScale: 0.1
|
||||
meshCompression: 0
|
||||
addColliders: 0
|
||||
importBlendShapes: 0
|
||||
swapUVChannels: 0
|
||||
generateSecondaryUV: 0
|
||||
useFileUnits: 1
|
||||
optimizeMeshForGPU: 1
|
||||
keepQuads: 0
|
||||
weldVertices: 1
|
||||
secondaryUVAngleDistortion: 8
|
||||
secondaryUVAreaDistortion: 15.000001
|
||||
secondaryUVHardAngle: 88
|
||||
secondaryUVPackMargin: 4
|
||||
useFileScale: 1
|
||||
tangentSpace:
|
||||
normalSmoothAngle: 60
|
||||
normalImportMode: 0
|
||||
tangentImportMode: 2
|
||||
importAnimation: 0
|
||||
copyAvatar: 0
|
||||
humanDescription:
|
||||
human: []
|
||||
skeleton: []
|
||||
armTwist: 0.5
|
||||
foreArmTwist: 0.5
|
||||
upperLegTwist: 0.5
|
||||
legTwist: 0.5
|
||||
armStretch: 0.05
|
||||
legStretch: 0.05
|
||||
feetSpacing: 0
|
||||
rootMotionBoneName:
|
||||
hasTranslationDoF: 0
|
||||
lastHumanDescriptionAvatarSource: {instanceID: 0}
|
||||
animationType: 0
|
||||
humanoidOversampling: 1
|
||||
additionalBone: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Binary file not shown.
@@ -1,71 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace uDesktopDuplication
|
||||
{
|
||||
|
||||
[AddComponentMenu("uDesktopDuplication/Cursor"), RequireComponent(typeof(Texture))]
|
||||
public class Cursor : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Vector2 modelScale = Vector2.one;
|
||||
|
||||
Vector3 worldPosition { get; set; }
|
||||
|
||||
private Texture uddTexture_;
|
||||
private Monitor monitor { get { return uddTexture_.monitor; } }
|
||||
private Dictionary<int, Texture2D> textures_ = new Dictionary<int, Texture2D>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
uddTexture_ = GetComponent<Texture>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (monitor.isCursorVisible) {
|
||||
UpdatePosition();
|
||||
UpdateTexture();
|
||||
}
|
||||
UpdateMaterial();
|
||||
}
|
||||
|
||||
void UpdatePosition()
|
||||
{
|
||||
var x = (1f * monitor.cursorX / monitor.width - 0.5f) * modelScale.x;
|
||||
var y = (1f * monitor.cursorY / monitor.height - 0.5f) * modelScale.y;
|
||||
var iy = uddTexture_.invertY ? -1 : +1;
|
||||
var localPos = transform.right * x + iy * transform.up * y;
|
||||
worldPosition = transform.TransformPoint(localPos);
|
||||
}
|
||||
|
||||
void UpdateTexture()
|
||||
{
|
||||
var w = monitor.cursorShapeWidth;
|
||||
var h = monitor.cursorShapeHeight;
|
||||
if (w == 0 || h == 0) return;
|
||||
|
||||
var key = w + h * 100;
|
||||
if (!textures_.ContainsKey(key)) {
|
||||
var texture = new Texture2D(w, h, TextureFormat.BGRA32, false);
|
||||
texture.wrapMode = TextureWrapMode.Clamp;
|
||||
textures_.Add(key, texture);
|
||||
}
|
||||
|
||||
var cursorTexture = textures_[key];
|
||||
Assert.IsNotNull(cursorTexture);
|
||||
monitor.GetCursorTexture(cursorTexture.GetNativeTexturePtr());
|
||||
uddTexture_.material.SetTexture("_CursorTex", cursorTexture);
|
||||
}
|
||||
|
||||
void UpdateMaterial()
|
||||
{
|
||||
var x = monitor.isCursorVisible ? (float)monitor.cursorX / monitor.width : -9999f;
|
||||
var y = monitor.isCursorVisible ? (float)monitor.cursorY / monitor.height : -9999f;
|
||||
var w = (float)monitor.cursorShapeWidth / monitor.width;
|
||||
var h = (float)monitor.cursorShapeHeight / monitor.height;
|
||||
uddTexture_.material.SetVector("_CursorPositionScale", new Vector4(x, y, w, h));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
#pragma warning disable 114, 465
|
||||
|
||||
namespace uDesktopDuplication
|
||||
{
|
||||
|
||||
@@ -14,6 +17,7 @@ public enum Message
|
||||
|
||||
public enum CursorShapeType
|
||||
{
|
||||
Unspecified = 0,
|
||||
MonoChrome = 1,
|
||||
Color = 2,
|
||||
MaskedColor = 4,
|
||||
@@ -49,6 +53,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);
|
||||
@@ -56,9 +80,11 @@ public static class Lib
|
||||
public delegate void DebugLogDelegate(string str);
|
||||
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern void InitializeUDD();
|
||||
public static extern bool IsInitialized();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern void FinalizeUDD();
|
||||
public static extern void Initialize();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern void Finalize();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern void Reinitialize();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
@@ -72,9 +98,9 @@ public static class Lib
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern void SetDebugMode(DebugMode mode);
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern void SetLogFunc(IntPtr func);
|
||||
public static extern void SetLogFunc(DebugLogDelegate func);
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern void SetErrorFunc(IntPtr func);
|
||||
public static extern void SetErrorFunc(DebugLogDelegate func);
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern int GetMonitorCount();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
@@ -118,21 +144,35 @@ public static class Lib
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern bool IsCursorVisible(int id);
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern int GetCursorX(int id);
|
||||
public static extern int GetCursorX();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern int GetCursorY(int id);
|
||||
public static extern int GetCursorY();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern int GetCursorShapeWidth(int id);
|
||||
public static extern int GetCursorShapeWidth();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern int GetCursorShapeHeight(int id);
|
||||
public static extern int GetCursorShapeHeight();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern int GetCursorShapePitch(int id);
|
||||
public static extern int GetCursorShapePitch();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern CursorShapeType GetCursorShapeType(int id);
|
||||
public static extern CursorShapeType GetCursorShapeType();
|
||||
[DllImport("uDesktopDuplication")]
|
||||
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);
|
||||
[DllImport("uDesktopDuplication", EntryPoint = "GetPixels")]
|
||||
private static extern bool GetPixels_Internal(int id, System.IntPtr ptr, int x, int y, int width, int height);
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern bool HasBeenUpdated(int id);
|
||||
[DllImport("uDesktopDuplication")]
|
||||
public static extern bool UseGetPixels(int id, bool use);
|
||||
|
||||
public static string GetName(int id)
|
||||
{
|
||||
@@ -140,6 +180,60 @@ 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;
|
||||
}
|
||||
|
||||
public static Color32[] GetPixels(int id, int x, int y, int width, int height)
|
||||
{
|
||||
var color = new Color32[width * height];
|
||||
GetPixels(id, color, x, y, width, height);
|
||||
return color;
|
||||
}
|
||||
|
||||
public static bool GetPixels(int id, Color32[] colors, int x, int y, int width, int height)
|
||||
{
|
||||
if (colors.Length < width * height) {
|
||||
Debug.LogErrorFormat("colors is small.", id, x, y, width, height);
|
||||
return false;
|
||||
}
|
||||
var handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
|
||||
var ptr = handle.AddrOfPinnedObject();
|
||||
if (!GetPixels_Internal(id, ptr, x, y, width, height)) {
|
||||
Debug.LogErrorFormat("GetPixels({0}, {1}, {2}, {3}, {4}) failed.", id, x, y, width, height);
|
||||
return false;
|
||||
}
|
||||
handle.Free();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Color32 GetPixel(int id, int x, int y)
|
||||
{
|
||||
return GetPixels(id, x, y, 1, 1)[0];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,15 +15,17 @@ public class Manager : MonoBehaviour
|
||||
|
||||
public static Manager CreateInstance()
|
||||
{
|
||||
if (instance_) {
|
||||
return instance_;
|
||||
}
|
||||
if (instance_ != null) return instance_;
|
||||
|
||||
var manager = FindObjectOfType<Manager>();
|
||||
if (manager) return manager;
|
||||
if (manager) {
|
||||
instance_ = manager;
|
||||
return manager;
|
||||
}
|
||||
|
||||
var go = new GameObject("uDesktopDuplicationManager");
|
||||
return go.AddComponent<Manager>();
|
||||
instance_ = go.AddComponent<Manager>();
|
||||
return instance_;
|
||||
}
|
||||
|
||||
private List<Monitor> monitors_ = new List<Monitor>();
|
||||
@@ -50,13 +52,19 @@ public class Manager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
[Tooltip("Debug mode is not applied while running.")]
|
||||
[SerializeField] DebugMode debugMode = DebugMode.File;
|
||||
|
||||
[SerializeField] int desktopDuplicationApiTimeout = 0;
|
||||
[SerializeField] float retryReinitializationDuration = 1f;
|
||||
|
||||
private Coroutine renderCoroutine_ = null;
|
||||
private bool shouldReinitialize_ = false;
|
||||
private float reinitializationTimer_ = 0f;
|
||||
private bool isFirstFrame_ = true;
|
||||
|
||||
public static event Lib.DebugLogDelegate onDebugLog = msg => Debug.Log(msg);
|
||||
public static event Lib.DebugLogDelegate onDebugErr = msg => Debug.LogError(msg);
|
||||
|
||||
public delegate void ReinitializeHandler();
|
||||
public static event ReinitializeHandler onReinitialized;
|
||||
@@ -72,28 +80,44 @@ public class Manager : MonoBehaviour
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Lib.SetDebugMode(debugMode);
|
||||
Lib.InitializeUDD();
|
||||
Lib.SetTimeout(desktopDuplicationApiTimeout);
|
||||
// for simple singleton
|
||||
|
||||
if (instance_ != null) {
|
||||
if (instance_ == this) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (instance_ != null && instance_ != this) {
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
instance_ = this;
|
||||
|
||||
Lib.SetDebugMode(debugMode);
|
||||
Lib.SetLogFunc(onDebugLog);
|
||||
Lib.SetErrorFunc(onDebugErr);
|
||||
|
||||
Lib.SetTimeout(desktopDuplicationApiTimeout);
|
||||
Lib.Initialize();
|
||||
|
||||
CreateMonitors();
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
Lib.FinalizeUDD();
|
||||
Lib.Finalize();
|
||||
DestroyMonitors();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
renderCoroutine_ = StartCoroutine(OnRender());
|
||||
if (!isFirstFrame_) {
|
||||
Reinitialize();
|
||||
}
|
||||
|
||||
Lib.SetDebugMode(debugMode);
|
||||
Lib.SetLogFunc(onDebugLog);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
@@ -102,6 +126,9 @@ public class Manager : MonoBehaviour
|
||||
StopCoroutine(renderCoroutine_);
|
||||
renderCoroutine_ = null;
|
||||
}
|
||||
|
||||
Lib.SetLogFunc(null);
|
||||
Lib.SetErrorFunc(null);
|
||||
}
|
||||
|
||||
void Update()
|
||||
@@ -109,6 +136,7 @@ public class Manager : MonoBehaviour
|
||||
Lib.Update();
|
||||
ReinitializeIfNeeded();
|
||||
UpdateMessage();
|
||||
isFirstFrame_ = false;
|
||||
}
|
||||
|
||||
[ContextMenu("Reinitialize")]
|
||||
@@ -128,11 +156,13 @@ public class Manager : MonoBehaviour
|
||||
|
||||
for (int i = 0; i < monitors.Count; ++i) {
|
||||
var monitor = monitors[i];
|
||||
var state = monitor.state;
|
||||
if (
|
||||
monitor.state == MonitorState.NotSet ||
|
||||
monitor.state == MonitorState.AccessLost ||
|
||||
monitor.state == MonitorState.AccessDenied ||
|
||||
monitor.state == MonitorState.SessionDisconnected
|
||||
state == MonitorState.NotSet ||
|
||||
state == MonitorState.AccessLost ||
|
||||
state == MonitorState.AccessDenied ||
|
||||
state == MonitorState.SessionDisconnected ||
|
||||
state == MonitorState.Unknown
|
||||
) {
|
||||
reinitializeNeeded = true;
|
||||
break;
|
||||
|
||||
@@ -40,7 +40,6 @@ public class Monitor
|
||||
|
||||
~Monitor()
|
||||
{
|
||||
DestroyTexture();
|
||||
}
|
||||
|
||||
public int id
|
||||
@@ -146,12 +145,22 @@ public class Monitor
|
||||
|
||||
public bool isHorizontal
|
||||
{
|
||||
get { return width > height; }
|
||||
get
|
||||
{
|
||||
return
|
||||
(rotation == MonitorRotation.Identity) ||
|
||||
(rotation == MonitorRotation.Rotate180);
|
||||
}
|
||||
}
|
||||
|
||||
public bool isVertical
|
||||
{
|
||||
get { return height > width; }
|
||||
get
|
||||
{
|
||||
return
|
||||
(rotation == MonitorRotation.Rotate90) ||
|
||||
(rotation == MonitorRotation.Rotate270);
|
||||
}
|
||||
}
|
||||
|
||||
public bool isCursorVisible
|
||||
@@ -161,12 +170,12 @@ public class Monitor
|
||||
|
||||
public int cursorX
|
||||
{
|
||||
get { return Lib.GetCursorX(id); }
|
||||
get { return Lib.GetCursorMonitorId() == id ? Lib.GetCursorX() : -1; }
|
||||
}
|
||||
|
||||
public int cursorY
|
||||
{
|
||||
get { return Lib.GetCursorY(id); }
|
||||
get { return Lib.GetCursorMonitorId() == id ? Lib.GetCursorY() : -1; }
|
||||
}
|
||||
|
||||
public int systemCursorX
|
||||
@@ -189,17 +198,56 @@ public class Monitor
|
||||
|
||||
public int cursorShapeWidth
|
||||
{
|
||||
get { return Lib.GetCursorShapeWidth(id); }
|
||||
get { return Lib.GetCursorShapeWidth(); }
|
||||
}
|
||||
|
||||
public int cursorShapeHeight
|
||||
{
|
||||
get { return Lib.GetCursorShapeHeight(id); }
|
||||
get { return Lib.GetCursorShapeHeight(); }
|
||||
}
|
||||
|
||||
public CursorShapeType cursorShapeType
|
||||
{
|
||||
get { return Lib.GetCursorShapeType(id); }
|
||||
get { return Lib.GetCursorShapeType(); }
|
||||
}
|
||||
|
||||
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 hasBeenUpdated
|
||||
{
|
||||
get { return Lib.HasBeenUpdated(id); }
|
||||
}
|
||||
|
||||
bool useGetPixels_ = false;
|
||||
public bool useGetPixels
|
||||
{
|
||||
get
|
||||
{
|
||||
return useGetPixels_;
|
||||
}
|
||||
set
|
||||
{
|
||||
useGetPixels_ = value;
|
||||
Lib.UseGetPixels(id, value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool shouldBeUpdated
|
||||
@@ -220,6 +268,7 @@ public class Monitor
|
||||
}
|
||||
|
||||
private Texture2D texture_;
|
||||
private System.IntPtr texturePtr_;
|
||||
public Texture2D texture
|
||||
{
|
||||
get
|
||||
@@ -232,8 +281,8 @@ public class Monitor
|
||||
|
||||
public void Render()
|
||||
{
|
||||
if (texture_ && available) {
|
||||
Lib.SetTexturePtr(id, texture_.GetNativeTexturePtr());
|
||||
if (texture_ && available && texturePtr_ != System.IntPtr.Zero) {
|
||||
Lib.SetTexturePtr(id, texturePtr_);
|
||||
GL.IssuePluginEvent(Lib.GetRenderEventFunc(), id);
|
||||
}
|
||||
}
|
||||
@@ -270,6 +319,7 @@ public class Monitor
|
||||
var w = isHorizontal ? width : height;
|
||||
var h = isHorizontal ? height : width;
|
||||
texture_ = new Texture2D(w, h, TextureFormat.BGRA32, false);
|
||||
texturePtr_ = texture_.GetNativeTexturePtr();
|
||||
}
|
||||
|
||||
public void DestroyTexture()
|
||||
@@ -277,6 +327,7 @@ public class Monitor
|
||||
if (texture_) {
|
||||
Object.Destroy(texture_);
|
||||
texture_ = null;
|
||||
texturePtr_ = System.IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,6 +335,33 @@ public class Monitor
|
||||
{
|
||||
CreateTextureIfNeeded();
|
||||
}
|
||||
|
||||
public Color32[] GetPixels(int x, int y, int width, int height)
|
||||
{
|
||||
if (!useGetPixels_) {
|
||||
Debug.LogErrorFormat("Please set Monitor[{0}].useGetPixels as true.", id);
|
||||
return null;
|
||||
}
|
||||
return Lib.GetPixels(id, x, y, width, height);
|
||||
}
|
||||
|
||||
public bool GetPixels(Color32[] colors, int x, int y, int width, int height)
|
||||
{
|
||||
if (!useGetPixels_) {
|
||||
Debug.LogErrorFormat("Please set Monitor[{0}].useGetPixels as true.", id);
|
||||
return false;
|
||||
}
|
||||
return Lib.GetPixels(id, colors, x, y, width, height);
|
||||
}
|
||||
|
||||
public Color32 GetPixel(int x, int y)
|
||||
{
|
||||
if (!useGetPixels_) {
|
||||
Debug.LogErrorFormat("Please set Monitor[{0}].useGetPixels as true.", id);
|
||||
return Color.black;
|
||||
}
|
||||
return Lib.GetPixel(id, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace uDesktopDuplication
|
||||
{
|
||||
|
||||
[AddComponentMenu("uDesktopDuplication/Texture"), RequireComponent(typeof(Cursor))]
|
||||
[AddComponentMenu("uDesktopDuplication/Texture")]
|
||||
public class Texture : MonoBehaviour
|
||||
{
|
||||
private Monitor monitor_;
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -105,11 +108,6 @@ public class Texture : MonoBehaviour
|
||||
private set;
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
AddCursorIfNotAttached();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (monitor == null) {
|
||||
@@ -125,22 +123,24 @@ public class Texture : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
KeepMonitor();
|
||||
monitor.shouldBeUpdated = true;
|
||||
UpdateMaterial();
|
||||
}
|
||||
|
||||
void AddCursorIfNotAttached()
|
||||
void KeepMonitor()
|
||||
{
|
||||
if (!GetComponent<Cursor>())
|
||||
{
|
||||
gameObject.AddComponent<Cursor>();
|
||||
if (monitor == null) {
|
||||
Reinitialize();
|
||||
} else {
|
||||
lastMonitorId_ = monitorId;
|
||||
}
|
||||
}
|
||||
|
||||
void Reinitialize()
|
||||
{
|
||||
// Monitor instance is released here when initialized.
|
||||
monitor = Manager.GetMonitor(monitor.id);
|
||||
monitor = Manager.GetMonitor(lastMonitorId_);
|
||||
}
|
||||
|
||||
void UpdateMaterial()
|
||||
@@ -203,6 +203,33 @@ public class Texture : MonoBehaviour
|
||||
material.DisableKeyword("USE_CLIP");
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 GetWorldPositionFromCoord(int u, int v)
|
||||
{
|
||||
// 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 (bend) {
|
||||
var angle = localPos.x / radius;
|
||||
if (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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -63,47 +63,34 @@ inline void uddConvertToLinearIfNeeded(inout fixed3 rgb)
|
||||
}
|
||||
}
|
||||
|
||||
inline fixed4 uddGetScreenTexture(float2 uv)
|
||||
{
|
||||
fixed4 c = tex2D(_MainTex, uv);
|
||||
return c;
|
||||
}
|
||||
|
||||
inline fixed4 uddGetCursorTexture(float2 uv)
|
||||
{
|
||||
uv.x = (uv.x - _CursorX) / _CursorWidth;
|
||||
uv.y = (uv.y - _CursorY) / _CursorHeight;
|
||||
fixed4 c = tex2D(_CursorTex, uv);
|
||||
fixed a = step(0, uv.x) * step(0, uv.y) * step(uv.x, 1) * step(uv.y, 1);
|
||||
c.a *= step(0.01, a);
|
||||
return c;
|
||||
}
|
||||
|
||||
inline fixed4 uddGetScreenTextureWithCursor(float2 uv)
|
||||
inline fixed4 uddGetTexture(sampler2D tex, float2 uv)
|
||||
{
|
||||
uv = uddInvertUV(uv);
|
||||
#ifdef USE_CLIP
|
||||
uv = uddClipUV(uv);
|
||||
#endif
|
||||
fixed4 screen = uddGetScreenTexture(uddRotateUV(uv));
|
||||
fixed4 cursor = uddGetCursorTexture(uv);
|
||||
fixed4 color = lerp(screen, cursor, cursor.a);
|
||||
uddConvertToLinearIfNeeded(color.rgb);
|
||||
return color;
|
||||
fixed4 c = tex2D(tex, uddRotateUV(uv));
|
||||
uddConvertToLinearIfNeeded(c.rgb);
|
||||
return c;
|
||||
}
|
||||
|
||||
inline void uddBendVertex(inout float4 v, half radius, half width, half thickness)
|
||||
inline fixed4 uddGetScreenTexture(float2 uv)
|
||||
{
|
||||
return uddGetTexture(_MainTex, uv);
|
||||
}
|
||||
|
||||
inline void uddBendVertex(inout float3 v, half radius, half width, half thickness)
|
||||
{
|
||||
#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
|
||||
@@ -115,4 +102,30 @@ inline void uddBendVertex(inout float4 v, half radius, half width, half thicknes
|
||||
#endif
|
||||
}
|
||||
|
||||
inline float3 uddRotateY(float3 n, float angle)
|
||||
{
|
||||
float c = cos(angle);
|
||||
float s = sin(angle);
|
||||
return float3(c * n.x - s * n.z, n.y, s * n.x + c * n.z);
|
||||
}
|
||||
|
||||
inline float3 uddRotateX(float3 n, float angle)
|
||||
{
|
||||
float c = cos(angle);
|
||||
float s = sin(angle);
|
||||
return float3(n.x, c * n.y + s * n.z, -s * n.y + c * n.z);
|
||||
}
|
||||
|
||||
inline void uddBendNormal(float4 x, inout float3 n, half radius, half width)
|
||||
{
|
||||
#ifdef BEND_ON
|
||||
half angle = width * x / radius;
|
||||
#ifdef _FORWARD_Z
|
||||
n = uddRotateY(n, -angle);
|
||||
#elif _FORWARD_Y
|
||||
n = uddRotateX(n, -angle);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -34,7 +34,7 @@ SubShader
|
||||
|
||||
void surf(Input IN, inout SurfaceOutputStandard o)
|
||||
{
|
||||
fixed4 c = uddGetScreenTextureWithCursor(IN.uv_MainTex) * _Color;
|
||||
fixed4 c = uddGetScreenTexture(IN.uv_MainTex) * _Color;
|
||||
o.Albedo = c.rgb;
|
||||
o.Metallic = _Metallic;
|
||||
o.Smoothness = _Glossiness;
|
||||
|
||||
@@ -5,11 +5,11 @@ Properties
|
||||
{
|
||||
_Color ("Color", Color) = (1, 1, 1, 1)
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
_CursorTex ("Cursor Texture", 2D) = "white" {}
|
||||
[KeywordEnum(Y, Z)] _Forward("Mesh Forward Direction", Int) = 0
|
||||
[Toggle(BEND_ON)] _Bend("Use Bend", Int) = 0
|
||||
[PowerSlider(10.0)] _Radius("Bend Radius", Range(1, 100)) = 30
|
||||
[PowerSlider(10.0)] _Thickness("Thickness", Range(0.01, 10)) = 1
|
||||
_Width("Width", Range(0.0, 10.0)) = 1.0
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ half _Thickness;
|
||||
v2f vert(appdata v)
|
||||
{
|
||||
v2f o;
|
||||
uddBendVertex(v.vertex, _Radius, _Width, _Thickness);
|
||||
uddBendVertex(v.vertex.xyz, _Radius, _Width, _Thickness);
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||
return o;
|
||||
@@ -39,7 +39,7 @@ v2f vert(appdata v)
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
return uddGetScreenTextureWithCursor(i.uv);
|
||||
return uddGetScreenTexture(i.uv);
|
||||
}
|
||||
|
||||
ENDCG
|
||||
|
||||
@@ -6,11 +6,11 @@ Properties
|
||||
_Color ("Color", Color) = (1, 1, 1, 1)
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
_Mask ("Mask", Range(0, 1)) = 0.1
|
||||
_CursorTex ("Cursor Texture", 2D) = "white" {}
|
||||
[KeywordEnum(Y, Z)] _Forward("Mesh Forward Direction", Int) = 0
|
||||
[Toggle(BEND_ON)] _Bend("Use Bend", Int) = 0
|
||||
[PowerSlider(10.0)] _Radius("Bend Radius", Range(1, 100)) = 30
|
||||
[PowerSlider(10.0)] _Thickness("Thickness", Range(0.01, 10)) = 1
|
||||
_Width("Width", Range(0.0, 10.0)) = 1.0
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ half _Thickness;
|
||||
v2f vert(appdata v)
|
||||
{
|
||||
v2f o;
|
||||
uddBendVertex(v.vertex, _Radius, _Width, _Thickness);
|
||||
uddBendVertex(v.vertex.xyz, _Radius, _Width, _Thickness);
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||
return o;
|
||||
@@ -43,7 +43,7 @@ v2f vert(appdata v)
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
fixed4 tex = uddGetScreenTextureWithCursor(i.uv);
|
||||
fixed4 tex = uddGetScreenTexture(i.uv);
|
||||
fixed alpha = pow((tex.r + tex.g + tex.b) / 3.0, _Mask);
|
||||
return fixed4(tex.rgb * _Color.rgb, alpha * _Color.a);
|
||||
}
|
||||
|
||||
178
Assets/uDesktopDuplication/Shaders/uDD_Unlit_Displacement.shader
Normal file
178
Assets/uDesktopDuplication/Shaders/uDD_Unlit_Displacement.shader
Normal file
@@ -0,0 +1,178 @@
|
||||
Shader "uDesktopDuplication/Unlit_Displacement"
|
||||
{
|
||||
|
||||
Properties
|
||||
{
|
||||
_Color ("Color", Color) = (1, 1, 1, 1)
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
[KeywordEnum(Y, Z)] _Forward("Mesh Forward Direction", Int) = 0
|
||||
[Toggle(BEND_ON)] _Bend("Use Bend", Int) = 0
|
||||
[PowerSlider(10.0)] _Radius("Bend Radius", Range(1, 100)) = 30
|
||||
_Width ("Width", Range(0.0, 10.0)) = 1.92
|
||||
[PowerSlider(10.0)] _Thickness("Thickness", Range(0.01, 10)) = 1
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
_DispTex ("Displacement Map", 2D) = "black" {}
|
||||
_DispFactor("Displacement Factor", Range(0, 5.0)) = 1
|
||||
_TessMinDist("Tessellation Min Distance", Range(0.1, 100.0)) = 1.0
|
||||
_TessMaxDist("Tessellation Max Distance", Range(0.1, 100.0)) = 5.0
|
||||
_TessFactor("Tessellation Factor", Range(0.1, 50.0)) = 10.0
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
|
||||
Tags { "RenderType"="Opaque" }
|
||||
|
||||
Cull [_Cull]
|
||||
|
||||
CGINCLUDE
|
||||
|
||||
#include "./uDD_Common.cginc"
|
||||
#include "Tessellation.cginc"
|
||||
|
||||
half _Radius;
|
||||
half _Width;
|
||||
half _Thickness;
|
||||
Texture2D _DispTex;
|
||||
SamplerState sampler_DispTex;
|
||||
half _DispFactor;
|
||||
half _TessMinDist;
|
||||
half _TessMaxDist;
|
||||
half _TessFactor;
|
||||
|
||||
struct VsInput
|
||||
{
|
||||
float3 vertex : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 texcoord : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct HsInput
|
||||
{
|
||||
float4 f4Position : POS;
|
||||
float3 f3Normal : NORMAL;
|
||||
float2 f2TexCoord : TEXCOORD;
|
||||
};
|
||||
|
||||
struct HsControlPointOutput
|
||||
{
|
||||
float3 f3Position : POS;
|
||||
float3 f3Normal : NORMAL;
|
||||
float2 f2TexCoord : TEXCOORD;
|
||||
};
|
||||
|
||||
struct HsConstantOutput
|
||||
{
|
||||
float fTessFactor[3] : SV_TessFactor;
|
||||
float fInsideTessFactor : SV_InsideTessFactor;
|
||||
};
|
||||
|
||||
struct DsOutput
|
||||
{
|
||||
float4 f4Position : SV_Position;
|
||||
float2 f2TexCoord : TEXCOORD0;
|
||||
};
|
||||
|
||||
HsInput vert(VsInput i)
|
||||
{
|
||||
HsInput o;
|
||||
o.f4Position = float4(i.vertex, 1.0);
|
||||
o.f3Normal = i.normal;
|
||||
o.f2TexCoord = i.texcoord;
|
||||
return o;
|
||||
}
|
||||
|
||||
[domain("tri")]
|
||||
[partitioning("integer")]
|
||||
[outputtopology("triangle_cw")]
|
||||
[patchconstantfunc("hullConst")]
|
||||
[outputcontrolpoints(3)]
|
||||
HsControlPointOutput hull(InputPatch<HsInput, 3> i, uint id : SV_OutputControlPointID)
|
||||
{
|
||||
HsControlPointOutput o = (HsControlPointOutput)0;
|
||||
o.f3Position = i[id].f4Position.xyz;
|
||||
o.f3Normal = i[id].f3Normal;
|
||||
o.f2TexCoord = i[id].f2TexCoord;
|
||||
return o;
|
||||
}
|
||||
|
||||
HsConstantOutput hullConst(InputPatch<HsInput, 3> i)
|
||||
{
|
||||
HsConstantOutput o = (HsConstantOutput)0;
|
||||
|
||||
float4 p0 = i[0].f4Position;
|
||||
float4 p1 = i[1].f4Position;
|
||||
float4 p2 = i[2].f4Position;
|
||||
float4 tessFactor = UnityDistanceBasedTess(p0, p1, p2, _TessMinDist, _TessMaxDist, _TessFactor);
|
||||
|
||||
o.fTessFactor[0] = tessFactor.x;
|
||||
o.fTessFactor[1] = tessFactor.y;
|
||||
o.fTessFactor[2] = tessFactor.z;
|
||||
o.fInsideTessFactor = tessFactor.w;
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
[domain("tri")]
|
||||
DsOutput domain(
|
||||
HsConstantOutput hsConst,
|
||||
const OutputPatch<HsControlPointOutput, 3> i,
|
||||
float3 bary : SV_DomainLocation)
|
||||
{
|
||||
DsOutput o = (DsOutput)0;
|
||||
|
||||
float3 f3Position =
|
||||
bary.x * i[0].f3Position +
|
||||
bary.y * i[1].f3Position +
|
||||
bary.z * i[2].f3Position;
|
||||
|
||||
float3 f3Normal = normalize(
|
||||
bary.x * i[0].f3Normal +
|
||||
bary.y * i[1].f3Normal +
|
||||
bary.z * i[2].f3Normal);
|
||||
|
||||
o.f2TexCoord =
|
||||
bary.x * i[0].f2TexCoord +
|
||||
bary.y * i[1].f2TexCoord +
|
||||
bary.z * i[2].f2TexCoord;
|
||||
|
||||
uddBendNormal(f3Position.x, f3Normal, _Radius, _Width);
|
||||
uddBendVertex(f3Position, _Radius, _Width, _Thickness);
|
||||
|
||||
float disp = length(_DispTex.SampleLevel(sampler_DispTex, o.f2TexCoord, 0)) * _DispFactor;
|
||||
f3Position.xyz += f3Normal * disp;
|
||||
|
||||
o.f4Position = mul(UNITY_MATRIX_MVP, float4(f3Position.xyz, 1.0));
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
fixed4 frag(DsOutput i) : SV_Target
|
||||
{
|
||||
return uddGetScreenTexture(i.f2TexCoord);
|
||||
}
|
||||
|
||||
ENDCG
|
||||
|
||||
Pass
|
||||
{
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#pragma hull hull
|
||||
#pragma domain domain
|
||||
#pragma multi_compile ___ INVERT_X
|
||||
#pragma multi_compile ___ INVERT_Y
|
||||
#pragma multi_compile ___ ROTATE90 ROTATE180 ROTATE270
|
||||
#pragma multi_compile ___ USE_CLIP
|
||||
#pragma multi_compile ___ BEND_ON
|
||||
#pragma multi_compile _FORWARD_Y _FORWARD_Z
|
||||
ENDCG
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Fallback "Unlit/Texture"
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 05df46eb51b13c84eabbdf4c53cc8db7
|
||||
timeCreated: 1480072905
|
||||
licenseType: Pro
|
||||
ShaderImporter:
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -5,11 +5,11 @@ Properties
|
||||
{
|
||||
_Color ("Color", Color) = (1, 1, 1, 1)
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
_CursorTex ("Cursor Texture", 2D) = "white" {}
|
||||
[KeywordEnum(Y, Z)] _Forward("Mesh Forward Direction", Int) = 0
|
||||
[Toggle(BEND_ON)] _Bend("Use Bend", Int) = 0
|
||||
[PowerSlider(10.0)] _Radius("Bend Radius", Range(1, 100)) = 30
|
||||
[PowerSlider(10.0)] _Thickness("Thickness", Range(0.01, 10)) = 1
|
||||
_Width("Width", Range(0.0, 10.0)) = 1.0
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ half _Thickness;
|
||||
v2f vert(appdata v)
|
||||
{
|
||||
v2f o;
|
||||
uddBendVertex(v.vertex, _Radius, _Width, _Thickness);
|
||||
uddBendVertex(v.vertex.xyz, _Radius, _Width, _Thickness);
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
|
||||
return o;
|
||||
@@ -41,7 +41,7 @@ v2f vert(appdata v)
|
||||
|
||||
fixed4 frag(v2f i) : SV_Target
|
||||
{
|
||||
return fixed4(uddGetScreenTextureWithCursor(i.uv).rgb, 1.0) * _Color;
|
||||
return fixed4(uddGetScreenTexture(i.uv).rgb, 1.0) * _Color;
|
||||
}
|
||||
|
||||
ENDCG
|
||||
|
||||
@@ -36,4 +36,4 @@ const std::unique_ptr<MonitorManager>& GetMonitorManager()
|
||||
void SendMessageToUnity(Message message)
|
||||
{
|
||||
g_messages.push(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,85 @@ 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;
|
||||
}
|
||||
|
||||
const T operator [](UINT index) const
|
||||
{
|
||||
if (index >= size_)
|
||||
{
|
||||
Debug::Error("Array index out of range: ", index, size_);
|
||||
return T(0);
|
||||
}
|
||||
return value_[index];
|
||||
}
|
||||
|
||||
T& operator [](UINT index)
|
||||
{
|
||||
if (index >= size_)
|
||||
{
|
||||
Debug::Error("Array index out of range: ", index, size_);
|
||||
return value_[0];
|
||||
}
|
||||
return value_[index];
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<T[]> value_;
|
||||
UINT size_ = 0;
|
||||
};
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <d3d11.h>
|
||||
#include <wrl/client.h>
|
||||
#include "Common.h"
|
||||
#include "Debug.h"
|
||||
#include "MonitorManager.h"
|
||||
#include "Monitor.h"
|
||||
@@ -9,8 +8,7 @@
|
||||
using namespace Microsoft::WRL;
|
||||
|
||||
|
||||
Cursor::Cursor(Monitor* monitor)
|
||||
: monitor_(monitor)
|
||||
Cursor::Cursor()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -20,7 +18,7 @@ Cursor::~Cursor()
|
||||
}
|
||||
|
||||
|
||||
void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
|
||||
void Cursor::UpdateBuffer(Monitor* monitor, const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
|
||||
{
|
||||
if (frameInfo.LastMouseUpdateTime.QuadPart == 0)
|
||||
{
|
||||
@@ -30,31 +28,20 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
|
||||
isVisible_ = frameInfo.PointerPosition.Visible != 0;
|
||||
if (isVisible_)
|
||||
{
|
||||
GetMonitorManager()->SetCursorMonitorId(monitor_->GetId());
|
||||
GetMonitorManager()->SetCursorMonitorId(monitor->GetId());
|
||||
}
|
||||
|
||||
x_ = frameInfo.PointerPosition.Position.x;
|
||||
y_ = frameInfo.PointerPosition.Position.y;
|
||||
timestamp_ = frameInfo.LastMouseUpdateTime;
|
||||
|
||||
if (!IsCursorOnParentMonitor())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (frameInfo.PointerShapeBufferSize == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Increase the buffer size if needed
|
||||
if (frameInfo.PointerShapeBufferSize > apiBufferSize_)
|
||||
{
|
||||
apiBufferSize_ = frameInfo.PointerShapeBufferSize;
|
||||
apiBuffer_ = std::make_unique<BYTE[]>(apiBufferSize_);
|
||||
}
|
||||
|
||||
if (!apiBuffer_)
|
||||
buffer_.ExpandIfNeeded(frameInfo.PointerShapeBufferSize);
|
||||
if (!buffer_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -62,286 +49,332 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
|
||||
// Get mouse pointer information
|
||||
UINT bufferSize;
|
||||
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
|
||||
const auto hr = monitor_->GetDeskDupl()->GetFramePointerShape(
|
||||
apiBufferSize_,
|
||||
apiBuffer_.get(),
|
||||
const auto hr = monitor->GetDeskDupl()->GetFramePointerShape(
|
||||
buffer_.Size(),
|
||||
buffer_.Get(),
|
||||
&bufferSize,
|
||||
&shapeInfo);
|
||||
|
||||
if (FAILED(hr))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateBuffer() => GetFramePointerShape() failed.");
|
||||
apiBuffer_.reset();
|
||||
apiBufferSize_ = 0;
|
||||
return;
|
||||
buffer_.Reset();
|
||||
return;
|
||||
}
|
||||
|
||||
shapeInfo_ = shapeInfo;
|
||||
}
|
||||
|
||||
|
||||
void Cursor::UpdateTexture()
|
||||
void Cursor::Draw(Monitor* monitor)
|
||||
{
|
||||
if (!IsCursorOnParentMonitor())
|
||||
// Check desktop texure
|
||||
if (monitor->GetUnityTexture() == nullptr)
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => Monitor::GetUnityTexture() is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Cursor information
|
||||
const bool isMono = GetType() == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME;
|
||||
const bool isColorMask = GetType() == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR;
|
||||
const auto cursorImageWidth = GetWidth();
|
||||
const auto cursorImageWidth = GetWidth();
|
||||
const auto cursorImageHeight = GetHeight();
|
||||
const auto cursorImagePitch = GetPitch();
|
||||
const auto cursorImagePitch = GetPitch();
|
||||
|
||||
// Captured area
|
||||
auto capturedImageWidth = cursorImageWidth;
|
||||
auto capturedImageHeight = cursorImageHeight;
|
||||
// Monitor orientation
|
||||
const auto monitorRot = static_cast<DXGI_MODE_ROTATION>(monitor->GetRotation());
|
||||
const auto isMonitorPortrait =
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE90 ||
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE270;
|
||||
|
||||
// Captured size (desktop cooridinates).
|
||||
auto capturedImageWidth = !isMonitorPortrait ? cursorImageWidth : cursorImageHeight;
|
||||
auto capturedImageHeight = !isMonitorPortrait ? cursorImageHeight : cursorImageWidth;
|
||||
|
||||
// Convert the buffer given by API into BGRA32
|
||||
const UINT bgraBufferSize = cursorImageWidth * cursorImageHeight * 4;
|
||||
if (bgraBufferSize > bgra32BufferSize_)
|
||||
bgraBuffer_.ExpandIfNeeded(bgraBufferSize);
|
||||
|
||||
// Check buffers
|
||||
if (!bgraBuffer_ || !buffer_)
|
||||
{
|
||||
bgra32BufferSize_ = bgraBufferSize;
|
||||
bgra32Buffer_ = std::make_unique<BYTE[]>(bgra32BufferSize_);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check buffers
|
||||
if (!bgra32Buffer_ || !apiBuffer_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// If masked, copy the desktop image and merge it with masked image.
|
||||
if (isMono || isColorMask)
|
||||
// Desktop size
|
||||
const int monitorWidth = monitor->GetWidth();
|
||||
const int monitorHeight = monitor->GetHeight();
|
||||
const int desktopImageWidth = !isMonitorPortrait ? monitorWidth : monitorHeight;
|
||||
const int desktopImageHeight = !isMonitorPortrait ? monitorHeight : monitorWidth;
|
||||
|
||||
// x_, y_ are cooridinates in rotated monitor.
|
||||
// desktopX, desktopY are coordinates in captured desktop image (always landscape).
|
||||
int desktopX, desktopY;
|
||||
switch (monitorRot)
|
||||
{
|
||||
const auto monitorWidth = monitor_->GetWidth();
|
||||
const auto monitorHeight = monitor_->GetHeight();
|
||||
const auto monitorRot = static_cast<DXGI_MODE_ROTATION>(monitor_->GetRotation());
|
||||
const auto isVertical =
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE90 ||
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE270;
|
||||
const auto desktopImageWidth = !isVertical ? monitorWidth : monitorHeight;
|
||||
const auto desktopImageHeight = !isVertical ? monitorHeight : monitorWidth;
|
||||
|
||||
auto x = x_;
|
||||
auto y = y_;
|
||||
auto colMin = 0;
|
||||
auto colMax = cursorImageWidth;
|
||||
auto rowMin = 0;
|
||||
auto rowMax = cursorImageHeight;
|
||||
|
||||
if (x < 0)
|
||||
case DXGI_MODE_ROTATION_ROTATE90:
|
||||
{
|
||||
x = 0;
|
||||
capturedImageWidth = cursorImageWidth + x_;
|
||||
colMin = cursorImageWidth - capturedImageWidth;
|
||||
desktopX = y_;
|
||||
desktopY = (desktopImageHeight - 1) - x_ - cursorImageWidth;
|
||||
break;
|
||||
}
|
||||
if (x + capturedImageWidth >= monitorWidth)
|
||||
case DXGI_MODE_ROTATION_ROTATE180:
|
||||
{
|
||||
capturedImageWidth = monitorWidth - x_;
|
||||
colMax = capturedImageWidth;
|
||||
desktopX = (desktopImageWidth - 1) - x_ - cursorImageWidth;
|
||||
desktopY = (desktopImageHeight - 1) - y_ - cursorImageHeight;
|
||||
break;
|
||||
}
|
||||
if (y < 0)
|
||||
case DXGI_MODE_ROTATION_ROTATE270:
|
||||
{
|
||||
y = 0;
|
||||
capturedImageHeight = cursorImageHeight + y_;
|
||||
rowMin = cursorImageHeight - capturedImageHeight;
|
||||
desktopX = (desktopImageWidth - 1) - y_ - cursorImageHeight;
|
||||
desktopY = x_;
|
||||
break;
|
||||
}
|
||||
if (y + capturedImageHeight >= monitorHeight)
|
||||
case DXGI_MODE_ROTATION_IDENTITY:
|
||||
case DXGI_MODE_ROTATION_UNSPECIFIED:
|
||||
default:
|
||||
{
|
||||
capturedImageHeight = monitorHeight - y_;
|
||||
rowMax = capturedImageHeight;
|
||||
desktopX = x_;
|
||||
desktopY = y_;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
D3D11_BOX box;
|
||||
box.front = 0;
|
||||
box.back = 1;
|
||||
// Calculate information to capture desktop image under cursor.
|
||||
int cursorOffsetX = 0;
|
||||
int cursorOffsetY = 0;
|
||||
int capturedImageLeft = desktopX;
|
||||
int capturedImageTop = desktopY;
|
||||
int capturedImageRight = desktopX + capturedImageWidth;
|
||||
int capturedImageBottom = desktopY + capturedImageHeight;
|
||||
|
||||
switch (monitorRot)
|
||||
{
|
||||
case DXGI_MODE_ROTATION_ROTATE90:
|
||||
{
|
||||
box.left = y;
|
||||
box.top = monitorWidth - x - capturedImageWidth;
|
||||
box.right = y + capturedImageWidth;
|
||||
box.bottom = monitorWidth - x;
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_ROTATE180:
|
||||
{
|
||||
box.left = monitorWidth - x - capturedImageWidth;
|
||||
box.top = monitorHeight - y - capturedImageHeight;
|
||||
box.right = monitorWidth - x;
|
||||
box.bottom = monitorHeight - y;
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_ROTATE270:
|
||||
{
|
||||
box.left = monitorHeight - y - capturedImageHeight;
|
||||
box.top = x;
|
||||
box.right = monitorHeight - y;
|
||||
box.bottom = x + capturedImageWidth;
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_IDENTITY:
|
||||
case DXGI_MODE_ROTATION_UNSPECIFIED:
|
||||
{
|
||||
box.left = x;
|
||||
box.top = y;
|
||||
box.right = x + capturedImageWidth;
|
||||
box.bottom = y + capturedImageHeight;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (capturedImageLeft < 0)
|
||||
{
|
||||
capturedImageWidth -= -desktopX;
|
||||
cursorOffsetX = -desktopX;
|
||||
capturedImageLeft = 0;
|
||||
}
|
||||
|
||||
if (box.left < 0 ||
|
||||
box.top < 0 ||
|
||||
box.right >= static_cast<UINT>(desktopImageWidth) ||
|
||||
box.bottom >= static_cast<UINT>(desktopImageHeight))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => box is out of area.");
|
||||
Debug::Error(
|
||||
" ",
|
||||
"(", box.left, ", ", box.top, ")",
|
||||
" ~ (", box.right, ", ", box.bottom, ") > ",
|
||||
"(", desktopImageWidth, ", ", desktopImageHeight, ")");
|
||||
return;
|
||||
}
|
||||
if (capturedImageRight >= desktopImageWidth)
|
||||
{
|
||||
capturedImageWidth -= capturedImageRight - desktopImageWidth;
|
||||
capturedImageRight = desktopImageWidth - 1;
|
||||
}
|
||||
|
||||
if (monitor_->GetUnityTexture() == nullptr)
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => Monitor::GetUnityTexture() is null.");
|
||||
return;
|
||||
}
|
||||
if (capturedImageTop < 0)
|
||||
{
|
||||
capturedImageHeight -= -desktopY;
|
||||
cursorOffsetY = -desktopY;
|
||||
capturedImageTop = 0;
|
||||
}
|
||||
|
||||
if (capturedImageBottom >= desktopImageHeight)
|
||||
{
|
||||
capturedImageHeight -= capturedImageBottom - desktopImageHeight;
|
||||
capturedImageBottom = desktopImageHeight - 1;
|
||||
}
|
||||
|
||||
// Check if box is inner desktop area
|
||||
if (capturedImageLeft < 0 ||
|
||||
capturedImageTop < 0 ||
|
||||
capturedImageRight >= desktopImageWidth ||
|
||||
capturedImageBottom >= desktopImageHeight)
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => box is out of area.");
|
||||
Debug::Error(
|
||||
" ",
|
||||
"(", capturedImageLeft, ", ", capturedImageTop, ")",
|
||||
" ~ (", capturedImageRight, ", ", capturedImageBottom, ") > ",
|
||||
"(", desktopImageWidth, ", ", desktopImageHeight, ")");
|
||||
return;
|
||||
}
|
||||
|
||||
if (capturedImageWidth == 0 || capturedImageHeight == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_BOX capturedImageArea
|
||||
{
|
||||
static_cast<UINT>(capturedImageLeft),
|
||||
static_cast<UINT>(capturedImageTop),
|
||||
0,
|
||||
static_cast<UINT>(capturedImageRight),
|
||||
static_cast<UINT>(capturedImageBottom),
|
||||
1
|
||||
};
|
||||
|
||||
// Create texture for capturing desktop image
|
||||
ComPtr<ID3D11Texture2D> texture;
|
||||
{
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
desc.Width = capturedImageWidth;
|
||||
desc.Height = capturedImageHeight;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Width = capturedImageWidth;
|
||||
desc.Height = capturedImageHeight;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.MiscFlags = 0;
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.MiscFlags = 0;
|
||||
|
||||
ComPtr<ID3D11Texture2D> texture;
|
||||
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &texture)))
|
||||
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &texture)))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => GetDevice()->CreateTexture2D() failed.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy desktop image to the texture
|
||||
{
|
||||
ComPtr<ID3D11DeviceContext> context;
|
||||
GetDevice()->GetImmediateContext(&context);
|
||||
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor->GetUnityTexture(), 0, &capturedImageArea);
|
||||
}
|
||||
|
||||
// Get mapped surface to access pixels in CPU
|
||||
ComPtr<IDXGISurface> surface;
|
||||
if (FAILED(texture.As(&surface)))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => texture.As() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
DXGI_MAPPED_RECT mappedSurface;
|
||||
if (FAILED(surface->Map(&mappedSurface, DXGI_MAP_READ)))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => surface->Map() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Finally, get the desktop texture under the mouse cursor.
|
||||
const auto desktop32 = reinterpret_cast<UINT*>(mappedSurface.pBits);
|
||||
const UINT desktopPitch = mappedSurface.Pitch / sizeof(UINT);
|
||||
|
||||
// Rotate cursor image to match the monitor orientation
|
||||
Buffer<BYTE> rotatedBuffer_;
|
||||
rotatedBuffer_.ExpandIfNeeded(buffer_.Size());
|
||||
|
||||
for (int y = 0; y < capturedImageHeight; ++y)
|
||||
{
|
||||
for (int x = 0; x < capturedImageWidth; ++x)
|
||||
{
|
||||
ComPtr<ID3D11DeviceContext> context;
|
||||
GetDevice()->GetImmediateContext(&context);
|
||||
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
|
||||
}
|
||||
// Cursor coordinates
|
||||
int cursorX, cursorY;
|
||||
|
||||
ComPtr<IDXGISurface> surface;
|
||||
if (FAILED(texture.As(&surface)))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => texture->QueryInterface() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
DXGI_MAPPED_RECT mappedSurface;
|
||||
if (FAILED(surface->Map(&mappedSurface, DXGI_MAP_READ)))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => surface->Map() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Finally, get the texture behind the mouse cursor.
|
||||
const auto desktop32 = reinterpret_cast<UINT*>(mappedSurface.pBits);
|
||||
const UINT desktopPitch = mappedSurface.Pitch / sizeof(UINT);
|
||||
|
||||
// Take the monitor orientation into consideration.
|
||||
const auto getDesktop32 = [&](int col, int row)
|
||||
{
|
||||
switch (monitorRot)
|
||||
{
|
||||
case DXGI_MODE_ROTATION_ROTATE90:
|
||||
return desktop32[(capturedImageWidth - 1 - col) * desktopPitch + row];
|
||||
break;
|
||||
case DXGI_MODE_ROTATION_ROTATE180:
|
||||
return desktop32[(capturedImageHeight - 1 - row) * desktopPitch + (capturedImageWidth - 1 - col)];
|
||||
break;
|
||||
case DXGI_MODE_ROTATION_ROTATE270:
|
||||
return desktop32[col * desktopPitch + (capturedImageHeight - 1 - row)];
|
||||
break;
|
||||
case DXGI_MODE_ROTATION_IDENTITY:
|
||||
case DXGI_MODE_ROTATION_UNSPECIFIED:
|
||||
return desktop32[row * desktopPitch + col];
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
// Access RGBA values at the same time
|
||||
auto output32 = reinterpret_cast<UINT*>(bgra32Buffer_.get());
|
||||
|
||||
if (isMono)
|
||||
{
|
||||
for (int row = rowMin, y = 0; row < rowMax; ++row, ++y)
|
||||
switch (monitorRot)
|
||||
{
|
||||
for (int col = colMin, x = 0; col < colMax; ++col, ++x)
|
||||
case DXGI_MODE_ROTATION_ROTATE90:
|
||||
{
|
||||
BYTE mask = 0b10000000 >> (col % 8);
|
||||
const BYTE andMask = apiBuffer_[col / 8 + row * cursorImagePitch] & mask;
|
||||
const BYTE xorMask = apiBuffer_[col / 8 + (row + capturedImageHeight) * cursorImagePitch] & mask;
|
||||
const UINT andMask32 = andMask ? 0xFFFFFFFF : 0x00000000;
|
||||
const UINT xorMask32 = xorMask ? 0xFFFFFFFF : 0x00000000;
|
||||
output32[row * cursorImageWidth + col] = (getDesktop32(x, y) & andMask32) ^ xorMask32;
|
||||
cursorX = (cursorImageWidth - 1) - (y + cursorOffsetY);
|
||||
cursorY = (x + cursorOffsetX);
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_ROTATE180:
|
||||
{
|
||||
cursorX = (cursorImageWidth - 1) - (x + cursorOffsetX);
|
||||
cursorY = (cursorImageHeight - 1) - (y + cursorOffsetY);
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_ROTATE270:
|
||||
{
|
||||
cursorX = (y + cursorOffsetY);
|
||||
cursorY = (cursorImageHeight - 1) - (x + cursorOffsetX);
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_IDENTITY:
|
||||
case DXGI_MODE_ROTATION_UNSPECIFIED:
|
||||
default:
|
||||
{
|
||||
cursorX = (x + cursorOffsetX);
|
||||
cursorY = (y + cursorOffsetY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR
|
||||
{
|
||||
const auto buffer32 = reinterpret_cast<UINT*>(apiBuffer_.get());
|
||||
|
||||
for (int row = rowMin, y = 0; row < rowMax; ++row, ++y)
|
||||
const auto outputIndex = y * capturedImageWidth + x;
|
||||
const auto desktopIndex = y * desktopPitch + x;
|
||||
const auto cursorIndex = cursorY * cursorImageWidth + cursorX;
|
||||
const auto buffer32 = buffer_.As<UINT>();
|
||||
auto output32 = bgraBuffer_.As<UINT>();
|
||||
|
||||
switch (GetType())
|
||||
{
|
||||
for (int col = colMin, x = 0; col < colMax; ++col, ++x)
|
||||
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME:
|
||||
{
|
||||
const int i = col + row * cursorImageWidth;
|
||||
const int j = col + row * cursorImagePitch / sizeof(UINT);
|
||||
|
||||
UINT mask = 0xFF000000 & buffer32[j];
|
||||
BYTE mask = 0b10000000 >> (cursorX % 8);
|
||||
const BYTE andMask = buffer_[cursorX / 8 + cursorY * cursorImagePitch] & mask;
|
||||
const BYTE xorMask = buffer_[cursorX / 8 + (cursorY + cursorImageHeight) * cursorImagePitch] & mask;
|
||||
const UINT andMask32 = andMask ? 0xFFFFFFFF : 0x00000000;
|
||||
const UINT xorMask32 = xorMask ? 0xFFFFFFFF : 0x00000000;
|
||||
output32[outputIndex] = (desktop32[desktopIndex] & andMask32) ^ xorMask32;
|
||||
break;
|
||||
}
|
||||
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR:
|
||||
{
|
||||
UINT mask = 0xFF000000 & buffer32[cursorIndex];
|
||||
if (mask)
|
||||
{
|
||||
output32[i] = (getDesktop32(x, y) ^ buffer32[j]) | 0xFF000000;
|
||||
output32[outputIndex] = (desktop32[desktopIndex] ^ buffer32[cursorIndex]) | 0xFF000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
output32[i] = buffer32[j] | 0xFF000000;
|
||||
output32[outputIndex] = buffer32[cursorIndex] | 0xFF000000;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR:
|
||||
{
|
||||
const auto desktop = reinterpret_cast<BYTE*>(&desktop32[desktopIndex]);
|
||||
const auto desktopB = desktop[0];
|
||||
const auto desktopG = desktop[1];
|
||||
const auto desktopR = desktop[2];
|
||||
const auto desktopA = desktop[3];
|
||||
|
||||
const auto cursor = buffer_.Get(cursorIndex * 4);
|
||||
const auto cursorB = cursor[0];
|
||||
const auto cursorG = cursor[1];
|
||||
const auto cursorR = cursor[2];
|
||||
const auto cursorA = cursor[3];
|
||||
|
||||
const auto a0 = cursorA / 255.f;
|
||||
const auto a1 = 1.f - a0;
|
||||
|
||||
auto output = reinterpret_cast<BYTE*>(&output32[outputIndex]);
|
||||
output[0] = static_cast<BYTE>(cursorB * a0 + desktopB * a1);
|
||||
output[1] = static_cast<BYTE>(cursorG * a0 + desktopG * a1);
|
||||
output[2] = static_cast<BYTE>(cursorR * a0 + desktopR * a1);
|
||||
output[3] = desktopA;
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => Unknown cursor type");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (FAILED(surface->Unmap()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR
|
||||
|
||||
{
|
||||
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];
|
||||
}
|
||||
ComPtr<ID3D11DeviceContext> context;
|
||||
GetDevice()->GetImmediateContext(&context);
|
||||
context->UpdateSubresource(monitor->GetUnityTexture(), 0, &capturedImageArea, bgraBuffer_.Get(), capturedImageWidth * 4, 0);
|
||||
}
|
||||
|
||||
if (FAILED(surface->Unmap()))
|
||||
{
|
||||
Debug::Error("Cursor::UpdateTexture() => surface->Unmap() failed.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Cursor::GetTexture(ID3D11Texture2D* texture)
|
||||
{
|
||||
if (bgra32Buffer_ == nullptr)
|
||||
if (!bgraBuffer_)
|
||||
{
|
||||
Debug::Error("Cursor::GetTexture() => bgra32Buffer is null.");
|
||||
return;
|
||||
@@ -368,7 +401,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, bgraBuffer_.Get(), GetWidth() * 4, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -413,10 +446,4 @@ int Cursor::GetPitch() const
|
||||
int Cursor::GetType() const
|
||||
{
|
||||
return shapeInfo_.Type;
|
||||
}
|
||||
|
||||
|
||||
bool Cursor::IsCursorOnParentMonitor() const
|
||||
{
|
||||
return GetMonitorManager()->GetCursorMonitorId() == monitor_->GetId();
|
||||
}
|
||||
@@ -3,16 +3,17 @@
|
||||
#include <d3d11.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <memory>
|
||||
#include "Common.h"
|
||||
|
||||
class Monitor;
|
||||
|
||||
class Cursor
|
||||
{
|
||||
public:
|
||||
explicit Cursor(Monitor* monitor);
|
||||
explicit Cursor();
|
||||
~Cursor();
|
||||
void UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
|
||||
void UpdateTexture();
|
||||
void UpdateBuffer(Monitor* monitor, const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
|
||||
void Draw(Monitor* monitor);
|
||||
void GetTexture(ID3D11Texture2D* texture);
|
||||
|
||||
bool IsVisible() const;
|
||||
@@ -24,16 +25,11 @@ public:
|
||||
int GetType() const;
|
||||
|
||||
private:
|
||||
bool IsCursorOnParentMonitor() const;
|
||||
|
||||
Monitor* monitor_;
|
||||
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> buffer_;
|
||||
Buffer<BYTE> bgraBuffer_;
|
||||
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo_;
|
||||
LARGE_INTEGER timestamp_;
|
||||
};
|
||||
@@ -4,15 +4,19 @@
|
||||
#include "Debug.h"
|
||||
|
||||
|
||||
decltype(Debug::mode_) Debug::mode_ = Debug::Mode::File;
|
||||
decltype(Debug::logFunc_) Debug::logFunc_ = nullptr;
|
||||
decltype(Debug::errFunc_) Debug::errFunc_ = nullptr;
|
||||
decltype(Debug::fs_) Debug::fs_;
|
||||
decltype(Debug::ss_) Debug::ss_;
|
||||
decltype(Debug::isInitialized_) Debug::isInitialized_ = false;
|
||||
decltype(Debug::mode_) Debug::mode_ = Debug::Mode::File;
|
||||
decltype(Debug::logFunc_) Debug::logFunc_ = nullptr;
|
||||
decltype(Debug::errFunc_) Debug::errFunc_ = nullptr;
|
||||
decltype(Debug::fs_) Debug::fs_;
|
||||
decltype(Debug::ss_) Debug::ss_;
|
||||
|
||||
|
||||
void Debug::Initialize()
|
||||
{
|
||||
if (isInitialized_) return;
|
||||
isInitialized_ = true;
|
||||
|
||||
if (mode_ == Mode::File)
|
||||
{
|
||||
fs_.open("uDesktopDuplication.log");
|
||||
@@ -23,6 +27,9 @@ void Debug::Initialize()
|
||||
|
||||
void Debug::Finalize()
|
||||
{
|
||||
if (!isInitialized_) return;
|
||||
isInitialized_ = false;
|
||||
|
||||
if (mode_ == Mode::File)
|
||||
{
|
||||
Debug::Log("Stop");
|
||||
|
||||
@@ -64,8 +64,12 @@ private:
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case Level::Log : logFunc_(ss_.str().c_str()); break;
|
||||
case Level::Error : errFunc_(ss_.str().c_str()); break;
|
||||
case Level::Log :
|
||||
if (logFunc_) logFunc_(ss_.str().c_str());
|
||||
break;
|
||||
case Level::Error :
|
||||
if (errFunc_) errFunc_(ss_.str().c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -119,6 +123,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
static bool isInitialized_;
|
||||
static Mode mode_;
|
||||
static std::ofstream fs_;
|
||||
static std::ostringstream ss_;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <d3d11.h>
|
||||
#include <ShellScalingAPI.h>
|
||||
#include "Common.h"
|
||||
#include "Debug.h"
|
||||
#include "Cursor.h"
|
||||
#include "MonitorManager.h"
|
||||
@@ -11,7 +10,6 @@ using namespace Microsoft::WRL;
|
||||
|
||||
Monitor::Monitor(int id)
|
||||
: id_(id)
|
||||
, cursor_(std::make_unique<Cursor>(this))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -50,7 +48,7 @@ void Monitor::Initialize(IDXGIOutput* output)
|
||||
if (FAILED(GetDpiForMonitor(outputDesc_.Monitor, MDT_RAW_DPI, &dpiX_, &dpiY_)))
|
||||
{
|
||||
Debug::Error("Monitor::Initialize() => GetDpiForMonitor() failed.");
|
||||
return;
|
||||
// DPI is set as -1, so the application has to use the appropriate value.
|
||||
}
|
||||
|
||||
auto output1 = reinterpret_cast<IDXGIOutput1*>(output);
|
||||
@@ -59,10 +57,17 @@ void Monitor::Initialize(IDXGIOutput* output)
|
||||
case S_OK:
|
||||
{
|
||||
state_ = State::Available;
|
||||
const auto rot = static_cast<DXGI_MODE_ROTATION>(GetRotation());
|
||||
Debug::Log("Monitor::Initialize() => OK.");
|
||||
Debug::Log(" ID : ", GetId());
|
||||
Debug::Log(" Size : (", GetWidth(), ", ", GetHeight(), ")");
|
||||
Debug::Log(" DPI : (", GetDpiX(), ", ", GetDpiY(), ")");
|
||||
Debug::Log(" Rot : ",
|
||||
rot == DXGI_MODE_ROTATION_IDENTITY ? "Landscape" :
|
||||
rot == DXGI_MODE_ROTATION_ROTATE90 ? "Portrait" :
|
||||
rot == DXGI_MODE_ROTATION_ROTATE180 ? "Landscape (flipped)" :
|
||||
rot == DXGI_MODE_ROTATION_ROTATE270 ? "Portrait (flipped)" :
|
||||
"Unspecified");
|
||||
break;
|
||||
}
|
||||
case E_INVALIDARG:
|
||||
@@ -114,10 +119,11 @@ void Monitor::Render(UINT timeout)
|
||||
{
|
||||
if (!deskDupl_) return;
|
||||
|
||||
HRESULT hr;
|
||||
ComPtr<IDXGIResource> resource;
|
||||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||||
|
||||
const auto hr = deskDupl_->AcquireNextFrame(timeout, &frameInfo, &resource);
|
||||
hr = deskDupl_->AcquireNextFrame(timeout, &frameInfo, &resource);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
switch (hr)
|
||||
@@ -156,15 +162,16 @@ void Monitor::Render(UINT timeout)
|
||||
return;
|
||||
}
|
||||
|
||||
ID3D11Texture2D* texture;
|
||||
if (FAILED(resource.CopyTo(&texture)))
|
||||
{
|
||||
Debug::Error("Monitor::Render() => resource.As() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get texture
|
||||
if (unityTexture_)
|
||||
{
|
||||
ID3D11Texture2D* texture;
|
||||
if (FAILED(resource.CopyTo(&texture)))
|
||||
{
|
||||
Debug::Error("Monitor::Render() => resource.As() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
D3D11_TEXTURE2D_DESC srcDesc, dstDesc;
|
||||
texture->GetDesc(&srcDesc);
|
||||
unityTexture_->GetDesc(&dstDesc);
|
||||
@@ -188,12 +195,153 @@ void Monitor::Render(UINT timeout)
|
||||
}
|
||||
}
|
||||
|
||||
cursor_->UpdateBuffer(frameInfo);
|
||||
cursor_->UpdateTexture();
|
||||
UpdateMetadata(frameInfo);
|
||||
|
||||
if (FAILED(deskDupl_->ReleaseFrame()))
|
||||
if (frameInfo.PointerPosition.Visible)
|
||||
{
|
||||
Debug::Error("Monitor::Render() => ReleaseFrame() failed.");
|
||||
GetMonitorManager()->SetCursorMonitorId(id_);
|
||||
}
|
||||
|
||||
if (GetMonitorManager()->GetCursorMonitorId() == id_)
|
||||
{
|
||||
UpdateCursor(frameInfo);
|
||||
}
|
||||
|
||||
if (UseGetPixels())
|
||||
{
|
||||
CopyTextureFromGpuToCpu(texture);
|
||||
}
|
||||
|
||||
hr = deskDupl_->ReleaseFrame();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
switch (hr)
|
||||
{
|
||||
case DXGI_ERROR_ACCESS_LOST:
|
||||
{
|
||||
Debug::Log("Monitor::Render() => DXGI_ERROR_ACCESS_LOST.");
|
||||
state_ = State::AccessLost;
|
||||
break;
|
||||
}
|
||||
case DXGI_ERROR_INVALID_CALL:
|
||||
{
|
||||
Debug::Error("Monitor::Render() => DXGI_ERROR_INVALID_CALL.");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
state_ = State::Unknown;
|
||||
Debug::Error("Monitor::Render() => Unknown Error.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
hasBeenUpdated_ = true;
|
||||
}
|
||||
|
||||
|
||||
void Monitor::UpdateCursor(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
|
||||
{
|
||||
auto cursor_ = GetMonitorManager()->GetCursor();
|
||||
cursor_->UpdateBuffer(this, frameInfo);
|
||||
cursor_->Draw(this);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,18 +376,6 @@ IDXGIOutputDuplication* Monitor::GetDeskDupl()
|
||||
}
|
||||
|
||||
|
||||
const std::unique_ptr<Cursor>& Monitor::GetCursor()
|
||||
{
|
||||
return cursor_;
|
||||
}
|
||||
|
||||
|
||||
void Monitor::GetCursorTexture(ID3D11Texture2D* texture)
|
||||
{
|
||||
cursor_->GetTexture(texture);
|
||||
}
|
||||
|
||||
|
||||
void Monitor::GetName(char* buf, int len) const
|
||||
{
|
||||
strcpy_s(buf, len, monitorInfo_.szDevice);
|
||||
@@ -252,6 +388,12 @@ bool Monitor::IsPrimary() const
|
||||
}
|
||||
|
||||
|
||||
bool Monitor::HasBeenUpdated() const
|
||||
{
|
||||
return hasBeenUpdated_;
|
||||
}
|
||||
|
||||
|
||||
int Monitor::GetLeft() const
|
||||
{
|
||||
return static_cast<int>(outputDesc_.DesktopCoordinates.left);
|
||||
@@ -303,4 +445,228 @@ 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_);
|
||||
}
|
||||
|
||||
|
||||
void Monitor::UseGetPixels(bool use)
|
||||
{
|
||||
useGetPixels_ = use;
|
||||
}
|
||||
|
||||
|
||||
bool Monitor::UseGetPixels() const
|
||||
{
|
||||
return useGetPixels_;
|
||||
}
|
||||
|
||||
|
||||
void Monitor::CopyTextureFromGpuToCpu(ID3D11Texture2D* texture)
|
||||
{
|
||||
const auto monitorRot = static_cast<DXGI_MODE_ROTATION>(GetRotation());
|
||||
const auto monitorWidth = GetWidth();
|
||||
const auto monitorHeight = GetHeight();
|
||||
const auto isVertical =
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE90 ||
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE270;
|
||||
const auto desktopImageWidth = !isVertical ? monitorWidth : monitorHeight;
|
||||
const auto desktopImageHeight = !isVertical ? monitorHeight : monitorWidth;
|
||||
|
||||
if (!textureForGetPixels_)
|
||||
{
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
desc.Width = desktopImageWidth;
|
||||
desc.Height = desktopImageHeight;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.MiscFlags = 0;
|
||||
|
||||
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &textureForGetPixels_)))
|
||||
{
|
||||
Debug::Error("Monitor::CopyTextureFromGpuToCpu() => GetDevice()->CreateTexture2D() failed.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ComPtr<ID3D11DeviceContext> context;
|
||||
GetDevice()->GetImmediateContext(&context);
|
||||
context->CopyResource(textureForGetPixels_.Get(), texture);
|
||||
}
|
||||
|
||||
ComPtr<IDXGISurface> surface;
|
||||
if (FAILED(textureForGetPixels_.As(&surface)))
|
||||
{
|
||||
Debug::Error("Monitor::CopyTextureFromGpuToCpu() => texture.As() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
DXGI_MAPPED_RECT mappedSurface;
|
||||
if (FAILED(surface->Map(&mappedSurface, DXGI_MAP_READ)))
|
||||
{
|
||||
Debug::Error("Monitor::CopyTextureFromGpuToCpu() => surface->Map() failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
const UINT size = desktopImageWidth * desktopImageHeight * sizeof(UINT);
|
||||
bufferForGetPixels_.ExpandIfNeeded(size);
|
||||
std::memcpy(bufferForGetPixels_.Get(), mappedSurface.pBits, size);
|
||||
|
||||
if (FAILED(surface->Unmap()))
|
||||
{
|
||||
Debug::Error("Monitor::CopyTextureFromGpuToCpu() => surface->Unmap() failed.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Monitor::GetPixels(BYTE* output, int x, int y, int width, int height)
|
||||
{
|
||||
if (!UseGetPixels())
|
||||
{
|
||||
Debug::Error("Monitor::GetPixels() => UseGetPixels(true) must have been called when you want to use GetPixels().");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!bufferForGetPixels_)
|
||||
{
|
||||
Debug::Error("Monitor::GetPixels() => CopyTextureFromGpuToCpu() has not been called yet.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto monitorRot = static_cast<DXGI_MODE_ROTATION>(GetRotation());
|
||||
const auto monitorWidth = GetWidth();
|
||||
const auto monitorHeight = GetHeight();
|
||||
const auto isVertical =
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE90 ||
|
||||
monitorRot == DXGI_MODE_ROTATION_ROTATE270;
|
||||
const auto desktopImageWidth = !isVertical ? monitorWidth : monitorHeight;
|
||||
const auto desktopImageHeight = !isVertical ? monitorHeight : monitorWidth;
|
||||
|
||||
// check area in destop coorinates.
|
||||
int left, top, right, bottom;
|
||||
|
||||
switch (monitorRot)
|
||||
{
|
||||
case DXGI_MODE_ROTATION_ROTATE90:
|
||||
{
|
||||
left = y;
|
||||
top = monitorWidth - x - width;
|
||||
right = y + width;
|
||||
bottom = monitorWidth - x;
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_ROTATE180:
|
||||
{
|
||||
left = monitorWidth - x - width;
|
||||
top = monitorHeight - y - height;
|
||||
right = monitorWidth - x;
|
||||
bottom = monitorHeight - y;
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_ROTATE270:
|
||||
{
|
||||
left = monitorHeight - y - height;
|
||||
top = x;
|
||||
right = monitorHeight - y;
|
||||
bottom = x + width;
|
||||
break;
|
||||
}
|
||||
case DXGI_MODE_ROTATION_IDENTITY:
|
||||
case DXGI_MODE_ROTATION_UNSPECIFIED:
|
||||
default:
|
||||
{
|
||||
left = x;
|
||||
top = y;
|
||||
right = x + width;
|
||||
bottom = y + height;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (left < 0 ||
|
||||
top < 0 ||
|
||||
right >= desktopImageWidth ||
|
||||
bottom >= desktopImageHeight)
|
||||
{
|
||||
Debug::Error("Monitor::GetPixels() => is out of area.");
|
||||
Debug::Error(
|
||||
" ",
|
||||
"(", left, ", ", top, ")",
|
||||
" ~ (", right, ", ", bottom, ") > ",
|
||||
"(", desktopImageWidth, ", ", desktopImageHeight, ")");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int row = 0; row < height; ++row)
|
||||
{
|
||||
for (int col = 0; col < width; ++col)
|
||||
{
|
||||
int inRow, inCol;
|
||||
switch (monitorRot)
|
||||
{
|
||||
case DXGI_MODE_ROTATION_ROTATE90:
|
||||
inCol = left + row;
|
||||
inRow = bottom - 1 - col;
|
||||
break;
|
||||
case DXGI_MODE_ROTATION_ROTATE180:
|
||||
inCol = right - 1 - col;
|
||||
inRow = bottom - 1 - row;
|
||||
break;
|
||||
case DXGI_MODE_ROTATION_ROTATE270:
|
||||
inCol = right - 1 - row;
|
||||
inRow = top + col;
|
||||
break;
|
||||
case DXGI_MODE_ROTATION_IDENTITY:
|
||||
case DXGI_MODE_ROTATION_UNSPECIFIED:
|
||||
default:
|
||||
inCol = left + col;
|
||||
inRow = top + row;
|
||||
break;
|
||||
}
|
||||
const auto inIndex = 4 * (inRow * desktopImageWidth + inCol);
|
||||
|
||||
const auto outRow = height - 1 - row;
|
||||
const auto outCol = col;
|
||||
const auto outIndex = 4 * (outRow * width + outCol);
|
||||
|
||||
|
||||
// BGRA -> RGBA
|
||||
output[outIndex + 0] = bufferForGetPixels_[inIndex + 2];
|
||||
output[outIndex + 1] = bufferForGetPixels_[inIndex + 1];
|
||||
output[outIndex + 2] = bufferForGetPixels_[inIndex + 0];
|
||||
output[outIndex + 3] = bufferForGetPixels_[inIndex + 3];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include <dxgi1_2.h>
|
||||
#include <wrl/client.h>
|
||||
#include <memory>
|
||||
|
||||
class Cursor;
|
||||
#include <mutex>
|
||||
#include "Common.h"
|
||||
|
||||
enum class MonitorState
|
||||
{
|
||||
@@ -30,7 +30,6 @@ public:
|
||||
~Monitor();
|
||||
void Initialize(IDXGIOutput* output);
|
||||
void Render(UINT timeout = 0);
|
||||
void GetCursorTexture(ID3D11Texture2D* texture);
|
||||
|
||||
public:
|
||||
int GetId() const;
|
||||
@@ -39,6 +38,7 @@ public:
|
||||
ID3D11Texture2D* GetUnityTexture() const;
|
||||
void GetName(char* buf, int len) const;
|
||||
bool IsPrimary() const;
|
||||
bool HasBeenUpdated() const;
|
||||
int GetLeft() const;
|
||||
int GetRight() const;
|
||||
int GetTop() const;
|
||||
@@ -49,16 +49,34 @@ public:
|
||||
int GetDpiX() const;
|
||||
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;
|
||||
void UseGetPixels(bool use);
|
||||
bool UseGetPixels() const;
|
||||
bool GetPixels(BYTE* output, int x, int y, int width, int height);
|
||||
|
||||
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);
|
||||
void CopyTextureFromGpuToCpu(ID3D11Texture2D* texture);
|
||||
|
||||
int id_ = -1;
|
||||
UINT dpiX_ = -1, dpiY_ = -1;
|
||||
int width_ = -1, height_ = -1;
|
||||
bool hasBeenUpdated_ = false;
|
||||
bool useGetPixels_ = false;
|
||||
State state_ = State::NotSet;
|
||||
std::unique_ptr<Cursor> cursor_;
|
||||
IDXGIOutputDuplication* deskDupl_ = nullptr;
|
||||
ID3D11Texture2D* unityTexture_ = nullptr;
|
||||
DXGI_OUTPUT_DESC outputDesc_;
|
||||
MONITORINFOEX monitorInfo_;
|
||||
Buffer<BYTE> metaData_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> textureForGetPixels_;
|
||||
Buffer<BYTE> bufferForGetPixels_;
|
||||
UINT moveRectSize_ = 0;
|
||||
UINT dirtyRectSize_ = 0;;
|
||||
};
|
||||
@@ -112,6 +112,12 @@ std::shared_ptr<Monitor> MonitorManager::GetMonitor(int id) const
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<Cursor> MonitorManager::GetCursor() const
|
||||
{
|
||||
return cursor_;
|
||||
}
|
||||
|
||||
|
||||
void MonitorManager::Update()
|
||||
{
|
||||
if (isReinitializationRequired_)
|
||||
|
||||
@@ -21,6 +21,7 @@ public:
|
||||
void SetCursorMonitorId(int id) { cursorMonitorId_ = id; }
|
||||
int GetCursorMonitorId() const { return cursorMonitorId_; }
|
||||
std::shared_ptr<Monitor> GetMonitor(int id) const;
|
||||
std::shared_ptr<Cursor> GetCursor() const;
|
||||
|
||||
private:
|
||||
void Initialize();
|
||||
@@ -40,8 +41,9 @@ public:
|
||||
|
||||
private:
|
||||
int timeout_ = 10;
|
||||
bool enableTextureCopyFromGpuToCpu_ = false;
|
||||
std::vector<std::shared_ptr<Monitor>> monitors_;
|
||||
std::shared_ptr<Cursor> cursor_;
|
||||
std::shared_ptr<Cursor> cursor_ = std::make_shared<Cursor>();
|
||||
int cursorMonitorId_ = -1;
|
||||
bool isReinitializationRequired_ = false;
|
||||
};
|
||||
@@ -21,11 +21,17 @@
|
||||
IUnityInterfaces* g_unity = nullptr;
|
||||
std::unique_ptr<MonitorManager> g_manager;
|
||||
std::queue<Message> g_messages;
|
||||
ID3D11DeviceContext* g_deviceContextForMainThread = nullptr;
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API InitializeUDD()
|
||||
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsInitialized()
|
||||
{
|
||||
return g_unity && g_manager;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API Initialize()
|
||||
{
|
||||
if (g_unity && !g_manager)
|
||||
{
|
||||
@@ -34,28 +40,45 @@ extern "C"
|
||||
}
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API FinalizeUDD()
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API Finalize()
|
||||
{
|
||||
if (g_manager)
|
||||
if (!g_manager) return;
|
||||
g_manager.reset();
|
||||
|
||||
std::queue<Message> empty;
|
||||
g_messages.swap(empty);
|
||||
|
||||
Debug::Finalize();
|
||||
}
|
||||
|
||||
void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
g_manager.reset();
|
||||
|
||||
std::queue<Message> empty;
|
||||
g_messages.swap(empty);
|
||||
|
||||
Debug::Finalize();
|
||||
case kUnityGfxDeviceEventInitialize:
|
||||
{
|
||||
Initialize();
|
||||
break;
|
||||
}
|
||||
case kUnityGfxDeviceEventShutdown:
|
||||
{
|
||||
Finalize();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
|
||||
{
|
||||
g_unity = unityInterfaces;
|
||||
InitializeUDD();
|
||||
auto unityGraphics = g_unity->Get<IUnityGraphics>();
|
||||
unityGraphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginUnload()
|
||||
{
|
||||
FinalizeUDD();
|
||||
auto unityGraphics = g_unity->Get<IUnityGraphics>();
|
||||
unityGraphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
|
||||
g_unity = nullptr;
|
||||
}
|
||||
|
||||
@@ -273,83 +296,52 @@ extern "C"
|
||||
return -1;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible(int id)
|
||||
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible()
|
||||
{
|
||||
if (!g_manager) return false;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetCursor()->IsVisible();
|
||||
}
|
||||
return false;
|
||||
return g_manager->GetCursor()->IsVisible();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorX(int id)
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorX()
|
||||
{
|
||||
if (!g_manager) return -1;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetCursor()->GetX();
|
||||
}
|
||||
return -1;
|
||||
return g_manager->GetCursor()->GetX();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorY(int id)
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorY()
|
||||
{
|
||||
if (!g_manager) return -1;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetCursor()->GetY();
|
||||
}
|
||||
return -1;
|
||||
return g_manager->GetCursor()->GetY();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeWidth(int id)
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeWidth()
|
||||
{
|
||||
if (!g_manager) return -1;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetCursor()->GetWidth();
|
||||
}
|
||||
return -1;
|
||||
return g_manager->GetCursor()->GetWidth();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeHeight(int id)
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeHeight()
|
||||
{
|
||||
if (!g_manager) return -1;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetCursor()->GetHeight();
|
||||
}
|
||||
return -1;
|
||||
return g_manager->GetCursor()->GetHeight();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapePitch(int id)
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapePitch()
|
||||
{
|
||||
if (!g_manager) return -1;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetCursor()->GetPitch();
|
||||
}
|
||||
return -1;
|
||||
return g_manager->GetCursor()->GetPitch();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeType(int id)
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeType()
|
||||
{
|
||||
if (!g_manager) return -1;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetCursor()->GetType();
|
||||
}
|
||||
return -1;
|
||||
return g_manager->GetCursor()->GetType();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetCursorTexture(int id, ID3D11Texture2D* texture)
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetCursorTexture(ID3D11Texture2D* texture)
|
||||
{
|
||||
if (!g_manager) return;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
monitor->GetCursorTexture(texture);
|
||||
}
|
||||
return g_manager->GetCursor()->GetTexture(texture);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTexturePtr(int id, void* texture)
|
||||
@@ -361,4 +353,73 @@ 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();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API GetPixels(int id, BYTE* output, int x, int y, int width, int height)
|
||||
{
|
||||
if (!g_manager) return nullptr;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->GetPixels(output, x, y, width, height);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API HasBeenUpdated(int id)
|
||||
{
|
||||
if (!g_manager) return nullptr;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->HasBeenUpdated();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UseGetPixels(int id, bool use)
|
||||
{
|
||||
if (!g_manager) return;
|
||||
if (auto monitor = g_manager->GetMonitor(id))
|
||||
{
|
||||
return monitor->UseGetPixels(use);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,10 +112,11 @@
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<Optimization>Full</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,9 @@
|
||||
uDesktopDuplication
|
||||
===================
|
||||
|
||||

|
||||

|
||||
|
||||
**uDesktopDuplication** is an Unity asset to use the realtime screen capture as `Texture2D` using Desktop Duplication API.
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user