fix crash bugs and memory leaks.

This commit is contained in:
hecomi
2016-11-15 21:50:35 +09:00
parent 58e14d76d8
commit e83f43d7fd
12 changed files with 129 additions and 81 deletions
@@ -21,7 +21,7 @@ public class Loupe : MonoBehaviour
// To get other monitor textures, set dirty flag.
foreach (var target in uDesktopDuplication.Manager.monitors) {
target.CreateTexture();
target.CreateTextureIfNeeded();
target.shouldBeUpdated = true;
}
@@ -112,7 +112,7 @@ public class MultipleMonitorCreator : MonoBehaviour
go.name = "Monitor " + i;
// Expand AABB
var mesh = go.GetComponent<MeshFilter>().mesh; // clone
var mesh = go.GetComponent<MeshFilter>().sharedMesh;
var aabbScale = mesh.bounds.size;
aabbScale.y = Mathf.Max(aabbScale.y, aabbScale.x);
aabbScale.z = Mathf.Max(aabbScale.z, aabbScale.x);
@@ -78,6 +78,8 @@ public static class Lib
[DllImport("uDesktopDuplication")]
public static extern int GetMonitorCount();
[DllImport("uDesktopDuplication")]
public static extern bool HasMonitorCountChanged();
[DllImport("uDesktopDuplication")]
public static extern int GetCursorMonitorId();
[DllImport("uDesktopDuplication")]
public static extern int GetTotalWidth();
+46 -28
View File
@@ -13,6 +13,19 @@ public class Manager : MonoBehaviour
get { return CreateInstance(); }
}
public static Manager CreateInstance()
{
if (instance_) {
return instance_;
}
var manager = FindObjectOfType<Manager>();
if (manager) return manager;
var go = new GameObject("uDesktopDuplicationManager");
return go.AddComponent<Manager>();
}
private List<Monitor> monitors_ = new List<Monitor>();
static public List<Monitor> monitors
{
@@ -48,22 +61,6 @@ public class Manager : MonoBehaviour
public delegate void ReinitializeHandler();
public static event ReinitializeHandler onReinitialized;
public static Manager CreateInstance()
{
if (instance_) {
return instance_;
}
var manager = FindObjectOfType<Manager>();
if (manager) {
manager.Awake();
return manager;
}
var go = new GameObject("uDesktopDuplicationManager");
return go.AddComponent<Manager>();
}
public static Monitor GetMonitor(int id)
{
if (id < 0 || id >= Manager.monitors.Count) {
@@ -77,18 +74,21 @@ public class Manager : MonoBehaviour
{
Lib.SetDebugMode(debugMode);
Lib.InitializeUDD();
Lib.SetTimeout(desktopDuplicationApiTimeout);
if (instance_ != null) return;
if (instance_ != null) {
Destroy(gameObject);
return;
}
instance_ = this;
CreateMonitors();
Lib.SetTimeout(desktopDuplicationApiTimeout);
}
void OnApplicationQuit()
{
Lib.FinalizeUDD();
DestroyMonitors();
}
void OnEnable()
@@ -124,20 +124,30 @@ public class Manager : MonoBehaviour
void ReinitializeIfNeeded()
{
bool reinitializeNeeded = false;
for (int i = 0; i < monitors.Count; ++i) {
var monitor = monitors[i];
if (monitor.state == MonitorState.NotSet ||
if (
monitor.state == MonitorState.NotSet ||
monitor.state == MonitorState.AccessLost ||
monitor.state == MonitorState.AccessDenied ||
monitor.state == MonitorState.SessionDisconnected) {
if (!shouldReinitialize_) {
shouldReinitialize_ = true;
reinitializationTimer_ = 0f;
break;
}
monitor.state == MonitorState.SessionDisconnected
) {
reinitializeNeeded = true;
break;
}
}
if (Lib.HasMonitorCountChanged()) {
reinitializeNeeded = true;
}
if (!shouldReinitialize_ && reinitializeNeeded) {
shouldReinitialize_ = true;
reinitializationTimer_ = 0f;
}
if (shouldReinitialize_) {
if (reinitializationTimer_ > retryReinitializationDuration) {
Reinitialize();
@@ -182,12 +192,20 @@ public class Manager : MonoBehaviour
void CreateMonitors()
{
monitors.Clear();
DestroyMonitors();
for (int i = 0; i < monitorCount; ++i) {
monitors.Add(new Monitor(i));
}
}
void DestroyMonitors()
{
for (int i = 0; i < monitors.Count; ++i) {
monitors[i].DestroyTexture();
}
monitors.Clear();
}
void ReinitializeMonitors()
{
for (int i = 0; i < monitorCount; ++i) {
@@ -202,7 +220,7 @@ public class Manager : MonoBehaviour
void RecreateTextures()
{
for (int i = 0; i < monitorCount; ++i) {
monitors[i].CreateTexture();
monitors[i].CreateTextureIfNeeded();
}
}
}
+37 -18
View File
@@ -38,6 +38,11 @@ public class Monitor
}
}
~Monitor()
{
DestroyTexture();
}
public int id
{
get;
@@ -203,21 +208,24 @@ public class Monitor
set;
}
private static Texture2D errorTexture;
private static Texture2D errorTexture_;
private static readonly string errorTexturePath = "uDesktopDuplication/Textures/NotAvailable";
private Texture2D errorTexture
{
get
{
return errorTexture_ ??
(errorTexture_ = Resources.Load<Texture2D>(errorTexturePath));
}
}
private Texture2D texture_;
public Texture2D texture
{
get
{
if (!available) {
return errorTexture ??
(errorTexture = Resources.Load<Texture2D>(errorTexturePath));
}
if (texture_ == null) {
CreateTexture();
}
if (!available) return errorTexture;
if (texture_ == null) CreateTextureIfNeeded();
return texture_;
}
}
@@ -235,7 +243,7 @@ public class Monitor
Lib.GetCursorTexture(id, ptr);
}
public void CreateTexture()
public void CreateTextureIfNeeded()
{
if (!available) return;
@@ -243,13 +251,8 @@ public class Monitor
var h = isHorizontal ? height : width;
bool shouldCreate = true;
if (texture_) {
if (texture_.width != w || texture_.height != h) {
Object.DestroyImmediate(texture_);
texture_ = null;
} else {
shouldCreate = false;
}
if (texture_ && texture_.width == w && texture_.height == h) {
shouldCreate = false;
}
if (w <= 0 || h <= 0) {
@@ -257,13 +260,29 @@ public class Monitor
}
if (shouldCreate) {
texture_ = new Texture2D(w, h, TextureFormat.BGRA32, false);
CreateTexture();
}
}
void CreateTexture()
{
DestroyTexture();
var w = isHorizontal ? width : height;
var h = isHorizontal ? height : width;
texture_ = new Texture2D(w, h, TextureFormat.BGRA32, false);
}
public void DestroyTexture()
{
if (texture_) {
Object.Destroy(texture_);
texture_ = null;
}
}
public void Reinitialize()
{
CreateTexture();
CreateTextureIfNeeded();
}
}
@@ -118,6 +118,11 @@ public class Texture : MonoBehaviour
}
}
void OnDisable()
{
Destroy(material);
}
void Update()
{
monitor.shouldBeUpdated = true;