add logs.

This commit is contained in:
hecomi
2016-11-15 01:00:26 +09:00
parent 931ff6b2db
commit 0a39e37659
19 changed files with 194 additions and 113 deletions
@@ -8,12 +8,12 @@ public class UddSceneManager : MonoBehaviour
[SerializeField] int sceneNo = 0;
void Awake()
{
if (!instance) {
instance = this;
} else {
Destroy(gameObject);
}
{
if (!instance) {
instance = this;
} else {
Destroy(gameObject);
}
}
void Start()
@@ -24,17 +24,25 @@ public class UddSceneManager : MonoBehaviour
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow)) {
Next();
if (Input.GetKeyDown(KeyCode.RightArrow)) {
Next();
} else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
Prev();
}
if (Input.GetKeyDown(KeyCode.R) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))) {
uDesktopDuplication.Manager.instance.Reinitialize();
}
if (Input.GetKeyDown(KeyCode.Escape)) {
Application.Quit();
}
}
void Next()
{
sceneNo = (sceneNo + 1) % scenes.Length;
Load();
{
sceneNo = (sceneNo + 1) % scenes.Length;
Load();
}
void Prev()
@@ -44,7 +52,7 @@ public class UddSceneManager : MonoBehaviour
}
void Load()
{
SceneManager.LoadScene(scenes[sceneNo]);
{
SceneManager.LoadScene(scenes[Mathf.Clamp(sceneNo, 0, scenes.Length - 1)]);
}
}
+3 -1
View File
@@ -9,6 +9,7 @@ public enum Message
{
None = -1,
Reinitialized = 0,
TextureSizeChanged = 1,
}
public enum CursorShapeType
@@ -36,7 +37,8 @@ public enum MonitorState
Unsupported = 3,
CurrentlyNotAvailable = 4,
SessionDisconnected = 5,
AccessLost = 6,
AccessLost = 6,
TextureSizeInconsistent = 7,
Unknown = 999,
}
+9 -10
View File
@@ -1,7 +1,6 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace uDesktopDuplication
{
@@ -113,7 +112,7 @@ public class Manager : MonoBehaviour
}
[ContextMenu("Reinitialize")]
void Reinitialize()
public void Reinitialize()
{
Debug.Log("[uDD] Reinitialize");
Lib.Reinitialize();
@@ -152,10 +151,14 @@ public class Manager : MonoBehaviour
{
var message = Lib.PopMessage();
while (message != Message.None) {
Debug.Log("[uDD] " + message);
switch (message) {
case Message.Reinitialized:
ReinitializeMonitors();
break;
case Message.TextureSizeChanged:
RecreateTextures();
break;
default:
break;
}
@@ -195,15 +198,11 @@ public class Manager : MonoBehaviour
}
}
void WaitThenDo(System.Action func, float sec)
void RecreateTextures()
{
StartCoroutine(_WaitThenDo(func, sec));
}
IEnumerator _WaitThenDo(System.Action func, float sec)
{
yield return new WaitForSeconds(sec);
func();
for (int i = 0; i < monitorCount; ++i) {
monitors[i].CreateTexture();
}
}
}
@@ -1,5 +1,4 @@
using UnityEngine;
using System.Runtime.InteropServices;
namespace uDesktopDuplication
{
@@ -204,13 +203,17 @@ public class Monitor
set;
}
private Texture2D errorTexture_;
private static readonly string errorTexturePath = "uDesktopDuplication/Textures/NotAvailable";
private Texture2D texture_;
public Texture2D texture
{
get
{
if (!available) {
return Resources.Load<Texture2D>("uDesktopDuplication/Textures/NotAvailable");
return errorTexture_ ??
(errorTexture_ = Resources.Load<Texture2D>(errorTexturePath));
}
if (texture_ == null) {
CreateTexture();
@@ -242,7 +245,10 @@ public class Monitor
if (texture_) {
if (texture_.width != w || texture_.height != h) {
if (texture_) Object.DestroyImmediate(texture_);
if (texture_) {
Object.Destroy(texture_);
texture_ = null;
}
shouldCreate = true;
} else {
shouldCreate = false;
@@ -1,4 +1,5 @@
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
namespace uDesktopDuplication
@@ -25,6 +26,17 @@ public static class Utility
}
return p;
}
public static void WaitThenDo(System.Action func, float sec)
{
Manager.instance.StartCoroutine(_WaitThenDo(func, sec));
}
public static IEnumerator _WaitThenDo(System.Action func, float sec)
{
yield return new WaitForSeconds(sec);
func();
}
}
}
@@ -1,14 +1,14 @@
#pragma once
#include <queue>
#include <unordered_map>
#include <iostream>
#include <d3d11.h>
#include "IUnityInterface.h"
#include "IUnityGraphicsD3D11.h"
#include "Common.h"
using namespace Microsoft::WRL;
extern IUnityInterfaces* g_unity;
extern std::unique_ptr<MonitorManager> g_manager;
@@ -21,7 +21,7 @@ IUnityInterfaces* GetUnity()
}
ID3D11Device* GetDevice()
ComPtr<ID3D11Device> GetDevice()
{
return GetUnity()->Get<IUnityGraphicsD3D11>()->GetDevice();
}
@@ -1,8 +1,7 @@
#pragma once
#include <memory>
#include <string>
#include <functional>
#include <wrl/client.h>
// Unity interface and ID3D11Device getters
@@ -10,7 +9,7 @@ struct IUnityInterfaces;
IUnityInterfaces* GetUnity();
struct ID3D11Device;
ID3D11Device* GetDevice();
Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
// Manager getter
@@ -23,6 +22,7 @@ enum class Message
{
None = -1,
Reinitialized = 0,
TextureSizeChanged = 1,
};
void SendMessageToUnity(Message message);
@@ -58,13 +58,12 @@ 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_,
reinterpret_cast<void*>(apiBuffer_.get()),
&bufferSize,
&shapeInfo);
if (FAILED(hr))
if (FAILED(
monitor_->GetDeskDupl()->GetFramePointerShape(
apiBufferSize_,
reinterpret_cast<void*>(apiBuffer_.get()),
&bufferSize,
&shapeInfo)))
{
Debug::Error("Cursor::UpdateBuffer() => GetFramePointerShape() failed.");
apiBuffer_.reset();
@@ -105,8 +104,6 @@ void Cursor::UpdateTexture()
// If masked, copy the desktop image and merge it with masked image.
if (isMono || isColorMask)
{
HRESULT hr;
const auto mw = monitor_->GetWidth();
const auto mh = monitor_->GetHeight();
auto x = x_;
@@ -153,8 +150,7 @@ void Cursor::UpdateTexture()
desc.MiscFlags = 0;
ComPtr<ID3D11Texture2D> texture;
hr = GetDevice()->CreateTexture2D(&desc, nullptr, &texture);
if (FAILED(hr))
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &texture)))
{
Debug::Error("Cursor::UpdateTexture() => GetDevice()->CreateTexture2D() failed.");
return;
@@ -174,23 +170,19 @@ void Cursor::UpdateTexture()
return;
}
{
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
}
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
ComPtr<IDXGISurface> surface;
hr = texture.As<IDXGISurface>(&surface);
if (FAILED(hr))
if (FAILED(texture.As<IDXGISurface>(&surface)))
{
Debug::Error("Cursor::UpdateTexture() => texture->QueryInterface() failed.");
return;
}
DXGI_MAPPED_RECT mappedSurface;
hr = surface->Map(&mappedSurface, DXGI_MAP_READ);
if (FAILED(hr))
if (FAILED(surface->Map(&mappedSurface, DXGI_MAP_READ)))
{
Debug::Error("Cursor::UpdateTexture() => surface->Map() failed.");
return;
@@ -243,8 +235,7 @@ void Cursor::UpdateTexture()
}
}
hr = surface->Unmap();
if (FAILED(hr))
if (FAILED(surface->Unmap()))
{
return;
}
@@ -28,4 +28,6 @@ void Debug::Finalize()
Debug::Log("Stop");
fs_.close();
}
Debug::SetLogFunc(nullptr);
Debug::SetErrorFunc(nullptr);
}
@@ -1,4 +1,5 @@
#pragma once
#include <time.h>
#include <fstream>
#include <sstream>
#include "IUnityInterface.h"
@@ -86,18 +87,34 @@ private:
Flush(level);
}
static void OutputTime()
{
auto t = time(nullptr);
tm tm;
localtime_s(&tm, &t);
char buf[64];
strftime(buf, 64, "%F %T", &tm);
Output("[");;
Output(buf);
Output("]");
}
public:
template <class Arg, class... RestArgs>
static void Log(Arg&& arg, RestArgs&&... restArgs)
{
Output("[uDD::Log] ");
Output("[uDD::Log]");
OutputTime();
Output(" ");
_Log(Level::Log, std::forward<Arg>(arg), std::forward<RestArgs>(restArgs)...);
}
template <class Arg, class... RestArgs>
static void Error(Arg&& arg, RestArgs&&... restArgs)
{
Output("[uDD::Err] ");
Output("[uDD::Err]");
OutputTime();
Output(" ");
_Log(Level::Error, std::forward<Arg>(arg), std::forward<RestArgs>(restArgs)...);
}
@@ -23,54 +23,84 @@ Monitor::~Monitor()
void Monitor::Initialize(IDXGIOutput* output)
{
output->GetDesc(&outputDesc_);
monitorInfo_.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(outputDesc_.Monitor, &monitorInfo_);
if (FAILED(output->GetDesc(&outputDesc_)))
{
Debug::Error("Monitor::Initialize() => IDXGIOutput::GetDesc() failed.");
return;
}
GetDpiForMonitor(outputDesc_.Monitor, MDT_RAW_DPI, &dpiX_, &dpiY_);
monitorInfo_.cbSize = sizeof(MONITORINFOEX);
if (!GetMonitorInfo(outputDesc_.Monitor, &monitorInfo_))
{
Debug::Error("Monitor::Initialize() => GetMonitorInfo() failed.");
return;
}
else
{
const auto rect = monitorInfo_.rcMonitor;
width_ = rect.right - rect.left;
height_ = rect.bottom - rect.top;
}
if (FAILED(GetDpiForMonitor(outputDesc_.Monitor, MDT_RAW_DPI, &dpiX_, &dpiY_)))
{
Debug::Error("Monitor::Initialize() => GetDpiForMonitor() failed.");
return;
}
auto output1 = reinterpret_cast<IDXGIOutput1*>(output);
const auto hr = output1->DuplicateOutput(GetDevice(), &deskDupl_);
// TODO: error check
switch (hr)
switch (output1->DuplicateOutput(GetDevice().Get(), &deskDupl_))
{
case S_OK:
state_ = State::Available;
Debug::Log("Monitor::Initialize() => OK.");
Debug::Log(" ID : ", GetId());
Debug::Log(" Size : (", GetWidth(), ", ", GetHeight(), ")");
Debug::Log(" DPI : (", GetDpiX(), ", ", GetDpiY(), ")");
break;
{
state_ = State::Available;
Debug::Log("Monitor::Initialize() => OK.");
Debug::Log(" ID : ", GetId());
Debug::Log(" Size : (", GetWidth(), ", ", GetHeight(), ")");
Debug::Log(" DPI : (", GetDpiX(), ", ", GetDpiY(), ")");
break;
}
case E_INVALIDARG:
state_ = State::InvalidArg;
Debug::Error("Monitor::Initialize() => Invalid arguments.");
break;
{
state_ = State::InvalidArg;
Debug::Error("Monitor::Initialize() => Invalid arguments.");
break;
}
case E_ACCESSDENIED:
// For example, when the user presses Ctrl + Alt + Delete and the screen
// switches to admin screen, this error occurs.
state_ = State::AccessDenied;
Debug::Error("Monitor::Initialize() => Access denied.");
break;
{
// For example, when the user presses Ctrl + Alt + Delete and the screen
// switches to admin screen, this error occurs.
state_ = State::AccessDenied;
Debug::Error("Monitor::Initialize() => Access denied.");
break;
}
case DXGI_ERROR_UNSUPPORTED:
// If the display adapter on the computer is running under the Microsoft Hybrid system,
// this error occurs.
state_ = State::Unsupported;
Debug::Error("Monitor::Initialize() => Unsupported display.");
break;
{
// If the display adapter on the computer is running under the Microsoft Hybrid system,
// this error occurs.
state_ = State::Unsupported;
Debug::Error("Monitor::Initialize() => Unsupported display.");
break;
}
case DXGI_ERROR_NOT_CURRENTLY_AVAILABLE:
// When other application use Desktop Duplication API, this error occurs.
state_ = State::CurrentlyNotAvailable;
Debug::Error("Monitor::Initialize() => Currently not available.");
break;
{
// When other application use Desktop Duplication API, this error occurs.
state_ = State::CurrentlyNotAvailable;
Debug::Error("Monitor::Initialize() => Currently not available.");
break;
}
case DXGI_ERROR_SESSION_DISCONNECTED:
{
state_ = State::SessionDisconnected;
Debug::Error("Monitor::Initialize() => Session disconnected.");
break;
break;
}
default:
state_ = State::Unknown;
Debug::Error("Monitor::Render() => Unknown Error.");
break;
{
state_ = State::Unknown;
Debug::Error("Monitor::Render() => Unknown Error.");
break;
}
}
}
@@ -81,9 +111,8 @@ void Monitor::Render(UINT timeout)
ComPtr<IDXGIResource> resource;
DXGI_OUTDUPL_FRAME_INFO frameInfo;
HRESULT hr;
hr = deskDupl_->AcquireNextFrame(timeout, &frameInfo, &resource);
const auto hr = deskDupl_->AcquireNextFrame(timeout, &frameInfo, &resource);
if (FAILED(hr))
{
switch (hr)
@@ -115,17 +144,26 @@ void Monitor::Render(UINT timeout)
if (unityTexture_)
{
ComPtr<ID3D11Texture2D> texture;
resource.As<ID3D11Texture2D>(&texture);
if (FAILED(resource.As<ID3D11Texture2D>(&texture)))
{
Debug::Error("Monitor::Render() => resource.As() failed.");
return;
}
D3D11_TEXTURE2D_DESC srcDesc, dstDesc;
texture->GetDesc(&srcDesc);
texture->GetDesc(&dstDesc);
unityTexture_->GetDesc(&dstDesc);
if (srcDesc.Width != dstDesc.Width ||
srcDesc.Height != dstDesc.Height)
{
Debug::Error("Monitor::Render() => Texture sizes are defferent.");
Debug::Error(" Source : (", srcDesc.Width, ", ", srcDesc.Height, ")");
Debug::Error(" Dest : (", dstDesc.Width, ", ", dstDesc.Height, ")");
Debug::Log(" => Try modifying width/height using reported value from DDA.");
width_ = srcDesc.Width;
height_ = srcDesc.Height;
state_ = MonitorState::TextureSizeInconsistent;
SendMessageToUnity(Message::TextureSizeChanged);
}
else
{
@@ -138,8 +176,7 @@ void Monitor::Render(UINT timeout)
cursor_->UpdateBuffer(frameInfo);
cursor_->UpdateTexture();
hr = deskDupl_->ReleaseFrame();
if (FAILED(hr))
if (FAILED(deskDupl_->ReleaseFrame()))
{
Debug::Error("Monitor::Render() => ReleaseFrame() failed.");
}
@@ -244,13 +281,11 @@ int Monitor::GetDpiY() const
int Monitor::GetWidth() const
{
const auto rect = monitorInfo_.rcMonitor;
return rect.right - rect.left;
return width_;
}
int Monitor::GetHeight() const
{
const auto rect = monitorInfo_.rcMonitor;
return rect.bottom - rect.top;
return height_;
}
@@ -17,6 +17,7 @@ enum class MonitorState
CurrentlyNotAvailable = 4,
SessionDisconnected = 5,
AccessLost = 6,
TextureSizeInconsistent = 7,
Unknown = 999,
};
@@ -53,6 +54,7 @@ public:
private:
int id_ = -1;
UINT dpiX_ = -1, dpiY_ = -1;
int width_ = -1, height_ = -1;
State state_ = State::NotSet;
std::unique_ptr<Cursor> cursor_;
Microsoft::WRL::ComPtr<IDXGIOutputDuplication> deskDupl_;
@@ -33,17 +33,22 @@ void MonitorManager::Initialize()
{
Finalize();
// Get factory
ComPtr<IDXGIFactory1> factory;
CreateDXGIFactory1(IID_PPV_ARGS(&factory));
ComPtr<IDXGIAdapter1> adapter;
ComPtr<IDXGIOutput> output;
// Get factory
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))))
{
Debug::Error("MonitorManager::Initialize() => CreateDXGIFactory1() failed.");
return;
}
// Check all display adapters
int id = 0;
ComPtr<IDXGIAdapter1> adapter;
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
{
// Search the main monitor from all outputs
ComPtr<IDXGIOutput> output;
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
{
auto monitor = std::make_shared<Monitor>(id++);
@@ -77,13 +82,18 @@ void MonitorManager::Reinitialize()
void MonitorManager::CheckMonitorNumbers()
{
ComPtr<IDXGIFactory1> factory;
CreateDXGIFactory1(IID_PPV_ARGS(&factory));
ComPtr<IDXGIAdapter1> adapter;
ComPtr<IDXGIOutput> output;
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))))
{
Debug::Error("MonitorManager::CheckMonitorNumbers() => CreateDXGIFactory1() failed.");
return;
}
int id = 0;
ComPtr<IDXGIAdapter1> adapter;
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
{
ComPtr<IDXGIOutput> output;
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
{
id++;
@@ -92,7 +102,6 @@ void MonitorManager::CheckMonitorNumbers()
if (GetMonitorCount() != id)
{
Debug::Log("Monitor number changed: ", GetMonitorCount(), " => ", id);
RequireReinitilization();
}
}
@@ -43,8 +43,6 @@ extern "C"
std::queue<Message> empty;
g_messages.swap(empty);
Debug::SetLogFunc(nullptr);
Debug::SetErrorFunc(nullptr);
Debug::Finalize();
}
}
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1,2 +1,2 @@
m_EditorVersion: 5.4.1f1
m_EditorVersion: 5.4.2f2
m_StandardAssetsVersion: 0