D3D11: Make DebugAnnotator11 thread safe.

Bug: chromium:1366778
Change-Id: I50662895be8ec40de4ded8c4f84bde59ae40e98b
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3917936
Reviewed-by: Jonah Ryan-Davis <jonahr@google.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Quyen Le <lehoangquyen@chromium.org>
This commit is contained in:
Le Hoang Quyen 2022-09-26 21:25:18 +08:00 коммит произвёл Angle LUCI CQ
Родитель da44ba32b6
Коммит d64c54e34d
16 изменённых файлов: 125 добавлений и 58 удалений

Просмотреть файл

@ -84,19 +84,19 @@ bool ShouldCreatePlatformLogMessage(LogSeverity severity)
std::ostream *gSwallowStream;
} // namespace priv
bool DebugAnnotationsActive()
bool DebugAnnotationsActive(const gl::Context *context)
{
#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) || defined(ANGLE_ENABLE_DEBUG_TRACE)
return g_debugAnnotator != nullptr && g_debugAnnotator->getStatus();
return g_debugAnnotator != nullptr && g_debugAnnotator->getStatus(context);
#else
return false;
#endif
}
bool ShouldBeginScopedEvent()
bool ShouldBeginScopedEvent(const gl::Context *context)
{
#if defined(ANGLE_ENABLE_ANNOTATOR_RUN_TIME_CHECKS)
return DebugAnnotationsActive();
return DebugAnnotationsActive(context);
#else
return true;
#endif // defined(ANGLE_ENABLE_ANNOTATOR_RUN_TIME_CHECKS)
@ -219,7 +219,7 @@ void Trace(LogSeverity severity, const char *message)
std::string str(message);
if (DebugAnnotationsActive())
if (DebugAnnotationsActive(/*context=*/nullptr))
{
switch (severity)
@ -228,7 +228,7 @@ void Trace(LogSeverity severity, const char *message)
// Debugging logging done in ScopedPerfEventHelper
break;
default:
g_debugAnnotator->setMarker(message);
g_debugAnnotator->setMarker(/*context=*/nullptr, message);
break;
}
}

Просмотреть файл

@ -102,22 +102,22 @@ class DebugAnnotator : angle::NonCopyable
virtual void beginEvent(gl::Context *context,
angle::EntryPoint entryPoint,
const char *eventName,
const char *eventMessage) = 0;
const char *eventMessage) = 0;
virtual void endEvent(gl::Context *context,
const char *eventName,
angle::EntryPoint entryPoint) = 0;
virtual void setMarker(const char *markerName) = 0;
virtual bool getStatus() = 0;
angle::EntryPoint entryPoint) = 0;
virtual void setMarker(gl::Context *context, const char *markerName) = 0;
virtual bool getStatus(const gl::Context *context) = 0;
// Log Message Handler that gets passed every log message,
// when debug annotations are initialized,
// replacing default handling by LogMessage.
virtual void logMessage(const LogMessage &msg) const = 0;
};
bool ShouldBeginScopedEvent();
bool ShouldBeginScopedEvent(const gl::Context *context);
void InitializeDebugAnnotations(DebugAnnotator *debugAnnotator);
void UninitializeDebugAnnotations();
bool DebugAnnotationsActive();
bool DebugAnnotationsActive(const gl::Context *context);
bool DebugAnnotationsInitialized();
void InitializeDebugMutexIfNeeded();
@ -279,7 +279,7 @@ std::ostream &FmtHex(std::ostream &os, T value)
context, angle::EntryPoint::entryPoint); \
do \
{ \
if (gl::ShouldBeginScopedEvent()) \
if (gl::ShouldBeginScopedEvent(context)) \
{ \
scopedPerfEventHelper##__LINE__.begin( \
"%s(" message ")", GetEntryPointName(angle::EntryPoint::entryPoint), \
@ -292,7 +292,7 @@ std::ostream &FmtHex(std::ostream &os, T value)
angle::EntryPoint::entryPoint); \
do \
{ \
if (gl::ShouldBeginScopedEvent()) \
if (gl::ShouldBeginScopedEvent(context)) \
{ \
scopedPerfEventHelper.begin("%s(" message ")", \
GetEntryPointName(angle::EntryPoint::entryPoint), \

Просмотреть файл

@ -13,7 +13,7 @@
namespace angle
{
bool LoggingAnnotator::getStatus()
bool LoggingAnnotator::getStatus(const gl::Context *context)
{
return false;
}
@ -31,7 +31,7 @@ void LoggingAnnotator::endEvent(gl::Context *context, const char *eventName, Ent
ANGLE_TRACE_EVENT_END0("gpu.angle", eventName);
}
void LoggingAnnotator::setMarker(const char *markerName)
void LoggingAnnotator::setMarker(gl::Context *context, const char *markerName)
{
ANGLE_TRACE_EVENT_INSTANT0("gpu.angle", markerName);
}

Просмотреть файл

@ -29,8 +29,8 @@ class LoggingAnnotator : public gl::DebugAnnotator
const char *eventName,
const char *eventMessage) override;
void endEvent(gl::Context *context, const char *eventName, EntryPoint entryPoint) override;
void setMarker(const char *markerName) override;
bool getStatus() override;
void setMarker(gl::Context *context, const char *markerName) override;
bool getStatus(const gl::Context *context) override;
void logMessage(const gl::LogMessage &msg) const override;
};

Просмотреть файл

@ -239,7 +239,7 @@ std::shared_ptr<WaitableCompileEvent> ShaderD3D::compile(const gl::Context *cont
const std::string &source = mState.getSource();
#if !defined(ANGLE_ENABLE_WINDOWS_UWP)
if (gl::DebugAnnotationsActive())
if (gl::DebugAnnotationsActive(context))
{
sourcePath = angle::CreateTemporaryFile().value();
writeFile(sourcePath.c_str(), source.c_str(), source.length());

Просмотреть файл

@ -750,14 +750,14 @@ gl::GraphicsResetStatus Context11::getResetStatus()
angle::Result Context11::insertEventMarker(GLsizei length, const char *marker)
{
mRenderer->getAnnotator()->setMarker(marker);
mRenderer->getDebugAnnotatorContext()->setMarker(marker);
return angle::Result::Continue;
}
angle::Result Context11::pushGroupMarker(GLsizei length, const char *marker)
{
mRenderer->getAnnotator()->beginEvent(nullptr, angle::EntryPoint::GLPushGroupMarkerEXT, marker,
marker);
mRenderer->getDebugAnnotatorContext()->beginEvent(angle::EntryPoint::GLPushGroupMarkerEXT,
marker, marker);
mMarkerStack.push(std::string(marker));
return angle::Result::Continue;
}
@ -769,8 +769,8 @@ angle::Result Context11::popGroupMarker()
{
marker = mMarkerStack.top().c_str();
mMarkerStack.pop();
mRenderer->getAnnotator()->endEvent(nullptr, marker,
angle::EntryPoint::GLPopGroupMarkerEXT);
mRenderer->getDebugAnnotatorContext()->endEvent(marker,
angle::EntryPoint::GLPopGroupMarkerEXT);
}
return angle::Result::Continue;
}

Просмотреть файл

@ -8,6 +8,8 @@
#include "libANGLE/renderer/d3d/d3d11/DebugAnnotator11.h"
#include "libANGLE/renderer/d3d/d3d11/Context11.h"
#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
#include <versionhelpers.h>
@ -17,6 +19,7 @@
namespace rx
{
// DebugAnnotator11 implementation
DebugAnnotator11::DebugAnnotator11() {}
DebugAnnotator11::~DebugAnnotator11() {}
@ -27,6 +30,57 @@ void DebugAnnotator11::beginEvent(gl::Context *context,
const char *eventMessage)
{
angle::LoggingAnnotator::beginEvent(context, entryPoint, eventName, eventMessage);
if (!context)
{
return;
}
Renderer11 *renderer11 = GetImplAs<Context11>(context)->getRenderer();
renderer11->getDebugAnnotatorContext()->beginEvent(entryPoint, eventName, eventMessage);
}
void DebugAnnotator11::endEvent(gl::Context *context,
const char *eventName,
angle::EntryPoint entryPoint)
{
angle::LoggingAnnotator::endEvent(context, eventName, entryPoint);
if (!context)
{
return;
}
Renderer11 *renderer11 = GetImplAs<Context11>(context)->getRenderer();
renderer11->getDebugAnnotatorContext()->endEvent(eventName, entryPoint);
}
void DebugAnnotator11::setMarker(gl::Context *context, const char *markerName)
{
angle::LoggingAnnotator::setMarker(context, markerName);
if (!context)
{
return;
}
Renderer11 *renderer11 = GetImplAs<Context11>(context)->getRenderer();
renderer11->getDebugAnnotatorContext()->setMarker(markerName);
}
bool DebugAnnotator11::getStatus(const gl::Context *context)
{
if (!context)
{
return false;
}
Renderer11 *renderer11 = GetImplAs<Context11>(context)->getRenderer();
return renderer11->getDebugAnnotatorContext()->getStatus();
}
// DebugAnnotatorContext11 implemenetation
DebugAnnotatorContext11::DebugAnnotatorContext11() = default;
DebugAnnotatorContext11::~DebugAnnotatorContext11() = default;
void DebugAnnotatorContext11::beginEvent(angle::EntryPoint entryPoint,
const char *eventName,
const char *eventMessage)
{
if (loggingEnabledForThisThread())
{
std::mbstate_t state = std::mbstate_t();
@ -35,20 +89,16 @@ void DebugAnnotator11::beginEvent(gl::Context *context,
}
}
void DebugAnnotator11::endEvent(gl::Context *context,
const char *eventName,
angle::EntryPoint entryPoint)
void DebugAnnotatorContext11::endEvent(const char *eventName, angle::EntryPoint entryPoint)
{
angle::LoggingAnnotator::endEvent(context, eventName, entryPoint);
if (loggingEnabledForThisThread())
{
mUserDefinedAnnotation->EndEvent();
}
}
void DebugAnnotator11::setMarker(const char *markerName)
void DebugAnnotatorContext11::setMarker(const char *markerName)
{
angle::LoggingAnnotator::setMarker(markerName);
if (loggingEnabledForThisThread())
{
std::mbstate_t state = std::mbstate_t();
@ -57,7 +107,7 @@ void DebugAnnotator11::setMarker(const char *markerName)
}
}
bool DebugAnnotator11::getStatus()
bool DebugAnnotatorContext11::getStatus() const
{
if (loggingEnabledForThisThread())
{
@ -67,17 +117,17 @@ bool DebugAnnotator11::getStatus()
return false;
}
bool DebugAnnotator11::loggingEnabledForThisThread() const
bool DebugAnnotatorContext11::loggingEnabledForThisThread() const
{
return mUserDefinedAnnotation != nullptr &&
angle::GetCurrentThreadUniqueId() == mAnnotationThread;
}
void DebugAnnotator11::initialize(ID3D11DeviceContext *context)
void DebugAnnotatorContext11::initialize(ID3D11DeviceContext *context)
{
#if !defined(ANGLE_ENABLE_WINDOWS_UWP)
// ID3DUserDefinedAnnotation.GetStatus only works on Windows10 or greater.
// Returning true unconditionally from DebugAnnotator11::getStatus() means
// Returning true unconditionally from DebugAnnotatorContext11::getStatus() means
// writing out all compiled shaders to temporary files even if debugging
// tools are not attached. See rx::ShaderD3D::prepareSourceAndReturnOptions.
// If you want debug annotations, you must use Windows 10.
@ -90,7 +140,7 @@ void DebugAnnotator11::initialize(ID3D11DeviceContext *context)
}
}
void DebugAnnotator11::release()
void DebugAnnotatorContext11::release()
{
mUserDefinedAnnotation.Reset();
}

Просмотреть файл

@ -14,13 +14,13 @@
namespace rx
{
// Note: To avoid any race conditions between threads, this class has no private data;
// DebugAnnotatorContext11 will be retrieved from Context11.
class DebugAnnotator11 : public angle::LoggingAnnotator
{
public:
DebugAnnotator11();
~DebugAnnotator11() override;
void initialize(ID3D11DeviceContext *context);
void release();
void beginEvent(gl::Context *context,
angle::EntryPoint entryPoint,
const char *eventName,
@ -28,8 +28,21 @@ class DebugAnnotator11 : public angle::LoggingAnnotator
void endEvent(gl::Context *context,
const char *eventName,
angle::EntryPoint entryPoint) override;
void setMarker(const char *markerName) override;
bool getStatus() override;
void setMarker(gl::Context *context, const char *markerName) override;
bool getStatus(const gl::Context *context) override;
};
class DebugAnnotatorContext11
{
public:
DebugAnnotatorContext11();
~DebugAnnotatorContext11();
void initialize(ID3D11DeviceContext *context);
void release();
void beginEvent(angle::EntryPoint entryPoint, const char *eventName, const char *eventMessage);
void endEvent(const char *eventName, angle::EntryPoint entryPoint);
void setMarker(const char *markerName);
bool getStatus() const;
private:
bool loggingEnabledForThisThread() const;

Просмотреть файл

@ -12,6 +12,7 @@
#include <versionhelpers.h>
#include <sstream>
#include "anglebase/no_destructor.h"
#include "common/tls.h"
#include "common/utilities.h"
#include "libANGLE/Buffer.h"
@ -979,8 +980,7 @@ egl::Error Renderer11::initializeD3DDevice()
d3d11::SetDebugName(mDeviceContext, "DeviceContext", nullptr);
mAnnotator.initialize(mDeviceContext);
gl::InitializeDebugAnnotations(&mAnnotator);
mAnnotatorContext.initialize(mDeviceContext);
mDevice->QueryInterface(__uuidof(ID3D11Device1), reinterpret_cast<void **>(&mDevice1));
@ -989,7 +989,11 @@ egl::Error Renderer11::initializeD3DDevice()
void Renderer11::setGlobalDebugAnnotator()
{
gl::InitializeDebugAnnotations(&mAnnotator);
static std::mutex gMutex;
static angle::base::NoDestructor<DebugAnnotator11> gGlobalAnnotator;
std::lock_guard<std::mutex> lg(gMutex);
gl::InitializeDebugAnnotations(gGlobalAnnotator.get());
}
// do any one-time device initialization
@ -2249,7 +2253,7 @@ void Renderer11::release()
{
mScratchMemoryBuffer.clear();
mAnnotator.release();
mAnnotatorContext.release();
gl::UninitializeDebugAnnotations();
releaseDeviceResources();
@ -2407,7 +2411,7 @@ bool Renderer11::getShareHandleSupport() const
}
// PIX doesn't seem to support using share handles, so disable them.
if (gl::DebugAnnotationsActive())
if (mAnnotatorContext.getStatus())
{
mSupportsShareHandles = false;
return false;
@ -4119,9 +4123,9 @@ gl::Version Renderer11::getMaxConformantESVersion() const
return std::min(getMaxSupportedESVersion(), gl::Version(3, 0));
}
gl::DebugAnnotator *Renderer11::getAnnotator()
DebugAnnotatorContext11 *Renderer11::getDebugAnnotatorContext()
{
return &mAnnotator;
return &mAnnotatorContext;
}
angle::Result Renderer11::dispatchCompute(const gl::Context *context,

Просмотреть файл

@ -323,7 +323,7 @@ class Renderer11 : public RendererD3D
Blit11 *getBlitter() { return mBlit; }
Clear11 *getClearer() { return mClear; }
gl::DebugAnnotator *getAnnotator();
DebugAnnotatorContext11 *getDebugAnnotatorContext();
// Buffer-to-texture and Texture-to-buffer copies
bool supportsFastCopyBufferToTexture(GLenum internalFormat) const override;
@ -620,7 +620,7 @@ class Renderer11 : public RendererD3D
angle::ScratchBuffer mScratchMemoryBuffer;
DebugAnnotator11 mAnnotator;
DebugAnnotatorContext11 mAnnotatorContext;
mutable Optional<bool> mSupportsShareHandles;
ResourceManager11 mResourceManager11;

Просмотреть файл

@ -381,7 +381,7 @@ gl::GraphicsResetStatus Context9::getResetStatus()
angle::Result Context9::insertEventMarker(GLsizei length, const char *marker)
{
mRenderer->getAnnotator()->setMarker(marker);
mRenderer->getAnnotator()->setMarker(/*context=*/nullptr, marker);
return angle::Result::Continue;
}

Просмотреть файл

@ -32,15 +32,15 @@ void DebugAnnotator9::endEvent(gl::Context *context,
D3DPERF_EndEvent();
}
void DebugAnnotator9::setMarker(const char *markerName)
void DebugAnnotator9::setMarker(gl::Context *context, const char *markerName)
{
angle::LoggingAnnotator::setMarker(markerName);
angle::LoggingAnnotator::setMarker(context, markerName);
std::mbstate_t state = std::mbstate_t();
std::mbsrtowcs(mWCharMessage, &markerName, kMaxMessageLength, &state);
D3DPERF_SetMarker(0, mWCharMessage);
}
bool DebugAnnotator9::getStatus()
bool DebugAnnotator9::getStatus(const gl::Context *context)
{
return !!D3DPERF_GetStatus();
}

Просмотреть файл

@ -25,8 +25,8 @@ class DebugAnnotator9 : public angle::LoggingAnnotator
void endEvent(gl::Context *context,
const char *eventName,
angle::EntryPoint entryPoint) override;
void setMarker(const char *markerName) override;
bool getStatus() override;
void setMarker(gl::Context *context, const char *markerName) override;
bool getStatus(const gl::Context *context) override;
private:
static constexpr size_t kMaxMessageLength = 256;

Просмотреть файл

@ -2403,7 +2403,7 @@ unsigned int Renderer9::getReservedFragmentUniformVectors() const
bool Renderer9::getShareHandleSupport() const
{
// PIX doesn't seem to support using share handles, so disable them.
return (mD3d9Ex != nullptr) && !gl::DebugAnnotationsActive();
return (mD3d9Ex != nullptr) && !gl::DebugAnnotationsActive(/*context=*/nullptr);
}
int Renderer9::getMajorShaderModel() const
@ -2706,7 +2706,7 @@ angle::Result Renderer9::compileToExecutable(d3d::Context *context,
flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
}
if (gl::DebugAnnotationsActive())
if (gl::DebugAnnotationsActive(/*context=*/nullptr))
{
#ifndef NDEBUG
flags = D3DCOMPILE_SKIP_OPTIMIZATION;

Просмотреть файл

@ -55,7 +55,7 @@ void DebugAnnotatorVk::endEvent(gl::Context *context,
}
}
bool DebugAnnotatorVk::getStatus()
bool DebugAnnotatorVk::getStatus(const gl::Context *context)
{
return true;
}

Просмотреть файл

@ -27,7 +27,7 @@ class DebugAnnotatorVk : public angle::LoggingAnnotator
void endEvent(gl::Context *context,
const char *eventName,
angle::EntryPoint entryPoint) override;
bool getStatus() override;
bool getStatus(const gl::Context *context) override;
};
} // namespace rx