Skip to content

Commit

Permalink
WebXRSession should not hold a unique ptr to WebXRViewerSpace which i…
Browse files Browse the repository at this point in the history
…s ref counted

https://bugs.webkit.org/show_bug.cgi?id=264200
rdar://115180471

Reviewed by Chris Dumez.

WebXRViewerSpace subclasses EventTarget and therefore should be ref-counted.

* Source/WebCore/Modules/webxr/WebXRSession.cpp:
(WebCore::WebXRSession::WebXRSession):
* Source/WebCore/Modules/webxr/WebXRSession.h:
* Source/WebCore/Modules/webxr/WebXRSpace.h:
(WebCore::WebXRViewerSpace::create):

Canonical link: https://commits.webkit.org/270230@main
  • Loading branch information
achan00 authored and cdumez committed Nov 4, 2023
1 parent f3dcaa1 commit 67e35fd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/Modules/webxr/WebXRSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ WebXRSession::WebXRSession(Document& document, WebXRSystem& system, XRSessionMod
, m_device(device)
, m_requestedFeatures(WTFMove(requestedFeatures))
, m_activeRenderState(WebXRRenderState::create(mode))
, m_viewerReferenceSpace(makeUnique<WebXRViewerSpace>(document, *this))
, m_viewerReferenceSpace(WebXRViewerSpace::create(document, *this))
, m_timeOrigin(MonotonicTime::now())
, m_views(device.views(mode))
{
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/Modules/webxr/WebXRSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class WebXRSession final : public RefCounted<WebXRSession>, public EventTarget,

const Vector<PlatformXR::Device::ViewData>& views() const { return m_views; }
const PlatformXR::Device::FrameData& frameData() const { return m_frameData; }
const WebXRViewerSpace& viewerReferenceSpace() const { return *m_viewerReferenceSpace; }
const WebXRViewerSpace& viewerReferenceSpace() const { return m_viewerReferenceSpace; }
bool posesCanBeReported(const Document&) const;

#if ENABLE(WEBXR_HANDS)
Expand Down Expand Up @@ -147,7 +147,7 @@ class WebXRSession final : public RefCounted<WebXRSession>, public EventTarget,
FeatureList m_requestedFeatures;
RefPtr<WebXRRenderState> m_activeRenderState;
RefPtr<WebXRRenderState> m_pendingRenderState;
std::unique_ptr<WebXRViewerSpace> m_viewerReferenceSpace;
Ref<WebXRViewerSpace> m_viewerReferenceSpace;
MonotonicTime m_timeOrigin;

unsigned m_nextCallbackId { 1 };
Expand Down
16 changes: 12 additions & 4 deletions Source/WebCore/Modules/webxr/WebXRSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,26 @@ class WebXRSpace : public EventTarget, public ContextDestructionObserver {
// https://immersive-web.github.io/webxr/#xrsession-viewer-reference-space
// This is a helper class to implement the viewer space owned by a WebXRSession.
// It avoids a circular reference between the session and the reference space.
class WebXRViewerSpace : public WebXRSpace {
class WebXRViewerSpace : public RefCounted<WebXRViewerSpace>, public WebXRSpace {
WTF_MAKE_ISO_ALLOCATED(WebXRViewerSpace);
public:
WebXRViewerSpace(Document&, WebXRSession&);
static Ref< WebXRViewerSpace> create(Document& document, WebXRSession& session)
{
return adoptRef(*new WebXRViewerSpace(document, session));
}
virtual ~WebXRViewerSpace();

using RefCounted::ref;
using RefCounted::deref;

private:
WebXRViewerSpace(Document&, WebXRSession&);

WebXRSession* session() const final { return m_session.get(); }
std::optional<TransformationMatrix> nativeOrigin() const final;

void refEventTarget() final { RELEASE_ASSERT_NOT_REACHED(); }
void derefEventTarget() final { RELEASE_ASSERT_NOT_REACHED(); }
void refEventTarget() final { ref(); }
void derefEventTarget() final { deref(); }

WeakPtr<WebXRSession> m_session;
};
Expand Down

0 comments on commit 67e35fd

Please sign in to comment.